PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.23
MxChat – AI Chatbot & Content Generation for WordPress v3.2.23
3.2.25 3.2.24 3.2.23 3.2.22 3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 All 156 releases
← All changes | includes/admin-knowledge-page.php +816 -118 3.2.8 → 3.2.23 View file →
@@ -73,9 +73,9 @@
73 73 <span><?php esc_html_e('Auto-Sync', 'mxchat'); ?></span>
74 74 </button>
75 75 <button class="mxch-mobile-nav-link" data-target="chunking">
76 76 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
77 - <span><?php esc_html_e('Content Chunking', 'mxchat'); ?></span>
77 + <span><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
78 78 </button>
79 79 <button class="mxch-mobile-nav-link" data-target="role-restrictions">
80 80 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
81 81 <span><?php esc_html_e('Role Restrictions', 'mxchat'); ?></span>
@@ -172,9 +172,9 @@
172 172 <button class="mxch-nav-link" data-target="chunking">
173 173 <span class="mxch-nav-link-icon">
174 174 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
175 175 </span>
176 - <span class="mxch-nav-link-text"><?php esc_html_e('Content Chunking', 'mxchat'); ?></span>
176 + <span class="mxch-nav-link-text"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></span>
177 177 </button>
178 178 </div>
179 179
180 180 <div class="mxch-nav-item" data-section="role-restrictions">
@@ -275,8 +275,11 @@
275 275
276 276 // Render the Edit Entry Modal
277 277 mxchat_render_edit_entry_modal();
278 278
279 + // Render the read-only Knowledge Inspector modal (plan-d8cb4b)
280 + mxchat_render_inspect_entry_modal();
281 +
279 282 // Render the navigation JavaScript
280 283 mxchat_render_knowledge_page_scripts();
281 284 }
282 285
@@ -552,8 +555,30 @@
552 555 <h4><?php esc_html_e('PDF Upload', 'mxchat'); ?></h4>
553 556 <p><?php esc_html_e('Upload a PDF file from your computer.', 'mxchat'); ?></p>
554 557 </div>
555 558 </button>
559 +
560 + <!-- Document File Upload Option (plan 0485e5) -->
561 + <button type="button" class="mxchat-import-box" data-option="document-upload" data-type="document-upload">
562 + <div class="mxchat-import-icon">
563 + <span class="dashicons dashicons-media-text"></span>
564 + </div>
565 + <div class="mxchat-import-content">
566 + <h4><?php esc_html_e('Document Upload', 'mxchat'); ?></h4>
567 + <p><?php esc_html_e('Upload a Word document, text, or Markdown file (.docx, .txt, .md).', 'mxchat'); ?></p>
568 + </div>
569 + </button>
570 +
571 + <!-- YouTube Video Import Option -->
572 + <button type="button" class="mxchat-import-box" data-option="youtube" data-type="youtube">
573 + <div class="mxchat-import-icon">
574 + <span class="dashicons dashicons-youtube"></span>
575 + </div>
576 + <div class="mxchat-import-content">
577 + <h4><?php esc_html_e('YouTube', 'mxchat'); ?></h4>
578 + <p><?php esc_html_e('Index a video so the chat can surface it as a playable embed.', 'mxchat'); ?></p>
579 + </div>
580 + </button>
556 581 </div>
557 582
558 583 <!-- Detected Sitemaps Section (hidden by default, shown when Sitemap Import is clicked) -->
559 584 <div id="mxchat-detected-sitemaps" class="mxchat-detected-sitemaps" style="display: none;">
@@ -655,8 +680,72 @@
655 680 <?php esc_html_e('Import PDF', 'mxchat'); ?>
656 681 </button>
657 682 </form>
658 683 </div>
684 +
685 + <div class="mxchat-import-input-area" id="mxchat-document-upload-area" style="display: none; margin-top: 20px;">
686 + <form id="mxchat-document-upload-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_document_file')); ?>" enctype="multipart/form-data">
687 + <?php wp_nonce_field('mxchat_submit_document_file_action', 'mxchat_submit_document_file_nonce'); ?>
688 + <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
689 + <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
690 + <?php endif; ?>
691 + <div class="mxch-field">
692 + <input type="file" name="document_file" id="mxchat-document-file-input" accept=".docx,.txt,.md" required>
693 + </div>
694 + <p class="mxch-field-description">
695 + <?php esc_html_e('Select a .docx, .txt, or .md file to import into the knowledge base. The text is extracted and indexed; the file itself is not stored. Re-uploading a file with the same name replaces its previous content.', 'mxchat'); ?>
696 + </p>
697 + <button type="submit" name="submit_document_file" class="mxch-btn mxch-btn-primary">
698 + <?php esc_html_e('Import Document', 'mxchat'); ?>
699 + </button>
700 + </form>
701 + </div>
702 +
703 + <?php
704 + // Prefill state: set when an auto-import found no transcript — the
705 + // handler bounced back here so the admin can augment the entry with
706 + // a manual description (re-importing the same URL updates the row).
707 + $yt_prefill = isset($_GET['mxchat_yt_prefill']) && $_GET['mxchat_yt_prefill'] === '1';
708 + $yt_prefill_url = $yt_prefill && isset($_GET['yt_url']) ? esc_url_raw(rawurldecode(wp_unslash($_GET['yt_url']))) : '';
709 + $yt_prefill_title = $yt_prefill && isset($_GET['yt_title']) ? sanitize_text_field(rawurldecode(wp_unslash($_GET['yt_title']))) : '';
710 + ?>
711 + <div class="mxchat-import-input-area" id="mxchat-youtube-input-area" style="display: none; margin-top: 20px;">
712 + <form id="mxchat-youtube-form" method="post" action="<?php echo esc_url(admin_url('admin-post.php?action=mxchat_submit_youtube')); ?>"<?php echo $yt_prefill ? ' data-yt-prefill="1"' : ''; ?>>
713 + <?php wp_nonce_field('mxchat_submit_youtube_action', 'mxchat_submit_youtube_nonce'); ?>
714 + <?php if ($multibot_active && $current_bot_id !== 'default') : ?>
715 + <input type="hidden" name="bot_id" value="<?php echo esc_attr($current_bot_id); ?>">
716 + <?php endif; ?>
717 + <div class="mxch-field">
718 + <input type="url" name="youtube_url" id="mxchat-youtube-url" class="mxch-input" placeholder="<?php esc_attr_e('Paste a YouTube video URL (watch, youtu.be, or Shorts link)', 'mxchat'); ?>" required value="<?php echo esc_attr($yt_prefill_url); ?>">
719 + </div>
720 + <div class="mxch-field">
721 + <span class="mxch-field-label"><?php esc_html_e('How should this video be described for matching?', 'mxchat'); ?></span>
722 + <div style="display: flex; flex-direction: column; gap: 6px; margin-top: 8px;">
723 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
724 + <input type="radio" name="youtube_description_mode" value="auto" <?php checked(!$yt_prefill); ?>>
725 + <span>
726 + <strong><?php esc_html_e('Auto-fetch', 'mxchat'); ?></strong>
727 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('We pull the video\'s title, channel, and captions (when available) to describe it.', 'mxchat'); ?></span>
728 + </span>
729 + </label>
730 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
731 + <input type="radio" name="youtube_description_mode" value="manual" <?php checked($yt_prefill); ?>>
732 + <span>
733 + <strong><?php esc_html_e('Write my own', 'mxchat'); ?></strong>
734 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('You write the description the chatbot matches against — best for curated guides.', 'mxchat'); ?></span>
735 + </span>
736 + </label>
737 + </div>
738 + </div>
739 + <div class="mxch-field" id="mxchat-youtube-description-field" style="<?php echo $yt_prefill ? '' : 'display: none;'; ?>">
740 + <textarea name="youtube_description" id="mxchat-youtube-description" class="mxch-textarea" rows="5" placeholder="<?php esc_attr_e('Describe what this video covers and the questions it answers...', 'mxchat'); ?>"><?php echo esc_textarea($yt_prefill_title !== '' ? $yt_prefill_title . "\n\n" : ''); ?></textarea>
741 + <p class="mxch-field-description"><?php esc_html_e('This text is what gets matched against visitor questions. Importing the same video URL again updates its existing entry.', 'mxchat'); ?></p>
742 + </div>
743 + <button type="submit" name="submit_youtube" value="1" class="mxch-btn mxch-btn-primary">
744 + <?php esc_html_e('Import Video', 'mxchat'); ?>
745 + </button>
746 + </form>
747 + </div>
659 748 <?php endif; ?>
660 749 </div>
661 750 </div>
662 751 </div>
@@ -807,9 +896,10 @@
807 896 <div class="mxch-card">
808 897 <div class="mxch-card-header">
809 898 <h3 class="mxch-card-title">
810 899 <?php esc_html_e('Knowledge Entries', 'mxchat'); ?>
811 - <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);">(<?php echo esc_html($total_records); ?>)</span>
900 + <?php $count_unit = $count_unit ?? ''; ?>
901 + <span id="mxchat-entry-count" style="font-weight: normal; color: var(--mxch-text-secondary);" data-count="<?php echo esc_attr($total_records); ?>" data-unit="<?php echo esc_attr($count_unit); ?>"<?php if ($count_unit === 'vectors') : ?> title="<?php esc_attr_e('Pinecone reports vectors, not documents; a long entry is stored as several vectors.', 'mxchat'); ?>"<?php endif; ?>>(<?php echo esc_html(number_format_i18n($total_records)); ?><?php if ($count_unit === 'vectors') : ?> <?php esc_html_e('vectors', 'mxchat'); ?><?php endif; ?>)</span>
812 902 <?php if (!empty($use_vectorstore)) : ?>
813 903 <span style="display: inline-flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: 500; margin-left: 8px; background: #fff3e0; color: #e65100;">
814 904 <span class="dashicons dashicons-database" style="font-size: 14px;"></span>
815 905 <?php esc_html_e('Not in use', 'mxchat'); ?>
@@ -989,10 +1079,19 @@
989 1079 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual Content', 'mxchat'); ?></span>
990 1080 <?php endif; ?>
991 1081 </td>
992 1082 <td class="mxchat-actions-cell" style="padding: 12px 16px; white-space: nowrap;">
993 - <?php if ($data_source !== 'pinecone') : ?>
994 1083 <button type="button"
1084 + class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
1085 + data-source-url="<?php echo esc_attr($source_url); ?>"
1086 + data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
1087 + data-data-source="<?php echo esc_attr($data_source); ?>"
1088 + data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1089 + data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1090 + title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1091 + <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1092 + </button>
1093 + <button type="button"
995 1094 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
996 1095 data-source-url="<?php echo esc_attr($source_url); ?>"
997 1096 data-entry-id="<?php echo esc_attr($first_prompt->id); ?>"
998 1097 data-data-source="<?php echo esc_attr($data_source); ?>"
@@ -1000,9 +1099,8 @@
1000 1099 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1001 1100 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1002 1101 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1003 1102 </button>
1004 - <?php endif; ?>
1005 1103 <button type="button"
1006 1104 class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-group"
1007 1105 data-source-url="<?php echo esc_attr($source_url); ?>"
1008 1106 data-chunk-count="<?php echo esc_attr($chunk_count); ?>"
@@ -1139,10 +1237,19 @@
1139 1237 <span style="color: var(--mxch-text-muted);"><?php esc_html_e('Manual', 'mxchat'); ?></span>
1140 1238 <?php endif; ?>
1141 1239 </td>
1142 1240 <td style="padding: 12px 16px; white-space: nowrap;">
1143 - <?php if ($data_source !== 'pinecone') : ?>
1144 1241 <button type="button"
1242 + class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-inspect-entry-btn"
1243 + data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1244 + data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1245 + data-data-source="<?php echo esc_attr($data_source); ?>"
1246 + data-bot-id="<?php echo esc_attr($current_bot_id); ?>"
1247 + data-nonce="<?php echo wp_create_nonce('mxchat_inspect_entry_nonce'); ?>"
1248 + title="<?php esc_attr_e('View indexed content', 'mxchat'); ?>">
1249 + <span class="dashicons dashicons-visibility" style="font-size: 14px;"></span>
1250 + </button>
1251 + <button type="button"
1145 1252 class="mxch-btn mxch-btn-ghost mxch-btn-sm mxchat-edit-entry-btn"
1146 1253 data-source-url="<?php echo esc_attr($prompt->source_url ?? ''); ?>"
1147 1254 data-entry-id="<?php echo esc_attr($prompt->id); ?>"
1148 1255 data-data-source="<?php echo esc_attr($data_source); ?>"
@@ -1150,9 +1257,8 @@
1150 1257 data-nonce="<?php echo wp_create_nonce('mxchat_edit_entry_nonce'); ?>"
1151 1258 title="<?php esc_attr_e('Edit content', 'mxchat'); ?>">
1152 1259 <span class="dashicons dashicons-edit" style="font-size: 14px;"></span>
1153 1260 </button>
1154 - <?php endif; ?>
1155 1261 <?php if ($data_source === 'pinecone') : ?>
1156 1262 <button type="button" class="mxch-btn mxch-btn-ghost mxch-btn-sm delete-button-ajax" data-vector-id="<?php echo esc_attr($prompt->id); ?>" data-bot-id="<?php echo esc_attr($current_bot_id); ?>" data-nonce="<?php echo wp_create_nonce('mxchat_delete_pinecone_prompt_nonce'); ?>" style="color: var(--mxch-error);">
1157 1263 <span class="dashicons dashicons-trash" style="font-size: 14px;"></span>
1158 1264 </button>
@@ -1170,10 +1276,21 @@
1170 1276 </table>
1171 1277 </div>
1172 1278
1173 1279 <!-- Pagination wrapper - always present so JS can populate it after processing -->
1174 - <div id="mxchat-kb-pagination" class="mxchat-kb-pagination-wrapper" style="<?php echo $total_pages > 1 ? 'padding: 16px; border-top: 1px solid var(--mxch-card-border); text-align: center;' : ''; ?>" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>" data-search="<?php echo esc_attr($search_query); ?>" data-content-type="<?php echo esc_attr($content_type_filter); ?>">
1175 - <?php if ($total_pages > 1) : ?>
1280 + <?php
1281 + // Plan dd6e10: Pinecone's list endpoint is a cursor — there is no
1282 + // random access to page N, so over 500 vectors the screen offers
1283 + // Previous / Next only. The same markup is built for AJAX page
1284 + // loads by MxChat_Knowledge_Manager::mxchat_cursor_pagination_html().
1285 + $cursor_pagination = !empty($cursor_pagination);
1286 + $has_next = !empty($has_next);
1287 + $show_pagination = $cursor_pagination ? ($current_page > 1 || $has_next) : ($total_pages > 1);
1288 + ?>
1289 + <div id="mxchat-kb-pagination" class="mxchat-kb-pagination-wrapper" style="<?php echo $show_pagination ? 'padding: 16px; border-top: 1px solid var(--mxch-card-border); text-align: center;' : ''; ?>" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>" data-search="<?php echo esc_attr($search_query); ?>" data-content-type="<?php echo esc_attr($content_type_filter); ?>">
1290 + <?php if ($cursor_pagination) : ?>
1291 + <?php echo MxChat_Knowledge_Manager::mxchat_cursor_pagination_html($current_page, $has_next, $search_query, $content_type_filter); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped inside ?>
1292 + <?php elseif ($total_pages > 1) : ?>
1176 1293 <div class="mxchat-ajax-pagination" data-current-page="<?php echo esc_attr($current_page); ?>" data-total-pages="<?php echo esc_attr($total_pages); ?>">
1177 1294 <?php if ($current_page > 1) : ?>
1178 1295 <a href="#" class="mxchat-page-link" data-page="<?php echo ($current_page - 1); ?>"><?php esc_html_e('&laquo; Previous', 'mxchat'); ?></a>
1179 1296 <?php endif; ?>
@@ -1304,13 +1421,16 @@
1304 1421 $chunk_size = $chunking_settings['chunk_size'];
1305 1422 ?>
1306 1423 <div id="chunking" class="mxch-section">
1307 1424 <div class="mxch-content-header">
1308 - <h1 class="mxch-content-title"><?php esc_html_e('Content Chunking', 'mxchat'); ?></h1>
1309 - <p class="mxch-content-subtitle"><?php esc_html_e('Split large content into smaller segments for more accurate semantic search.', 'mxchat'); ?></p>
1425 + <h1 class="mxch-content-title"><?php esc_html_e('Chunking & Retrieval', 'mxchat'); ?></h1>
1426 + <p class="mxch-content-subtitle"><?php esc_html_e('Control how content is split for indexing and how the knowledge base is searched.', 'mxchat'); ?></p>
1310 1427 </div>
1311 1428
1312 1429 <div class="mxch-card">
1430 + <div class="mxch-card-header">
1431 + <h2 class="mxch-card-title"><?php esc_html_e('Chunking', 'mxchat'); ?></h2>
1432 + </div>
1313 1433 <div class="mxch-card-body mxchat-autosave-section">
1314 1434 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1315 1435 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1316 1436 <span><?php esc_html_e('Chunking improves retrieval quality for long documents. All chunks are reassembled before sending to the AI. Only applies to new submissions.', 'mxchat'); ?></span>
@@ -1332,8 +1452,83 @@
1332 1452 <p class="mxch-field-description"><?php esc_html_e('Recommended: 4000 characters (~1000 tokens). Range: 1000-10000. Larger chunks preserve more context.', 'mxchat'); ?></p>
1333 1453 </div>
1334 1454 </div>
1335 1455 </div>
1456 +
1457 + <?php
1458 + // Retrieval tuning (plan 11720c) — the hybrid keyword boost moved here
1459 + // from Settings → Behavior: it is KB retrieval tuning, so it lives with
1460 + // its natural siblings. SAME option key and save transport as before —
1461 + // the name routes to the mxchat_save_setting handler, whose
1462 + // mxchat_hybrid_keyword_toggle case still runs capability detection.
1463 + ?>
1464 + <div class="mxch-card">
1465 + <div class="mxch-card-header">
1466 + <h2 class="mxch-card-title"><?php esc_html_e('Retrieval', 'mxchat'); ?></h2>
1467 + </div>
1468 + <div class="mxch-card-body mxchat-autosave-section">
1469 + <div class="mxch-field">
1470 + <div style="display: flex; align-items: center; gap: 12px;">
1471 + <label class="mxchat-toggle-switch">
1472 + <input type="checkbox" name="mxchat_hybrid_keyword_toggle" id="mxchat_hybrid_keyword_toggle" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_hybrid_keyword_toggle', 'off'), 'on'); ?>>
1473 + <span class="mxchat-toggle-slider"></span>
1474 + </label>
1475 + <span style="font-weight: 500;"><?php esc_html_e('Hybrid keyword boost (WordPress database and Pinecone document indexes)', 'mxchat'); ?></span>
1476 + </div>
1477 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Combines keyword matching with vector similarity when searching your WordPress-database knowledge base or a Pinecone document index. Helps exact-token questions — product codes, SKUs, error codes, names — find the right document even when semantic similarity alone would miss it.', 'mxchat'); ?></p>
1478 + <p class="mxch-field-hint"><?php esc_html_e('Applies to the WordPress-database knowledge base and to Pinecone when the index type is Document index with full-text search; a classic Pinecone vector index is unaffected. Changes which sources are retrieved on existing installs, so test after enabling. The Transcripts sources panel labels how each match was found while this is on.', 'mxchat'); ?></p>
1479 + </div>
1480 + </div>
1481 + </div>
1482 +
1483 + <?php
1484 + // Video cards (plan f52492). Lives on the Knowledge screen because that
1485 + // is where the owner imported the video in the first place, and beside
1486 + // Retrieval because it is a retrieval-confidence question. Standalone
1487 + // options — see the mxchat_video_embed_* cases in class-ajax-handler.php.
1488 + $video_embed_enabled = MxChat_Utils::video_embed_enabled();
1489 + $video_embed_threshold = (int) round(MxChat_Utils::video_embed_threshold() * 100);
1490 + $mxchat_core_options = get_option('mxchat_options', array());
1491 + $site_threshold = isset($mxchat_core_options['similarity_threshold'])
1492 + ? (int) $mxchat_core_options['similarity_threshold']
1493 + : 35;
1494 + ?>
1495 + <div class="mxch-card">
1496 + <div class="mxch-card-header">
1497 + <h2 class="mxch-card-title"><?php esc_html_e('Video Cards', 'mxchat'); ?></h2>
1498 + </div>
1499 + <div class="mxch-card-body mxchat-autosave-section">
1500 + <div class="mxch-field" style="margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--mxch-card-border);">
1501 + <div style="display: flex; align-items: center; gap: 12px;">
1502 + <label class="mxchat-toggle-switch">
1503 + <input type="checkbox" name="mxchat_video_embed_enabled" id="mxchat_video_embed_enabled" class="mxchat-autosave-field" value="on" <?php checked($video_embed_enabled, true); ?>>
1504 + <span class="mxchat-toggle-slider"></span>
1505 + </label>
1506 + <span style="font-weight: 500;"><?php esc_html_e('Show a video card when a YouTube source answers the question', 'mxchat'); ?></span>
1507 + </div>
1508 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Adds a click-to-play card beneath the reply when the answer came from a YouTube video you imported. Turn this off to stop video cards entirely without removing the videos from your knowledge base.', 'mxchat'); ?></p>
1509 + </div>
1510 +
1511 + <div class="mxch-field" id="mxchat-video-embed-threshold-field" style="<?php echo $video_embed_enabled ? '' : 'display: none;'; ?>">
1512 + <label class="mxch-field-label" for="mxchat_video_embed_threshold"><?php esc_html_e('Video Card Match Threshold (%)', 'mxchat'); ?></label>
1513 + <?php // Slider matching the site-wide Similarity Threshold exactly
1514 + // (plan 7afd54): same .slider-container / .range-slider /
1515 + // .range-value markup; the shared admin JS live-updates the
1516 + // <id>_value span on input and autosaves on change (once per
1517 + // drag release, so no request storm). Range/default/clamp
1518 + // unchanged — presentation only. ?>
1519 + <div class="slider-container">
1520 + <input type="range" name="mxchat_video_embed_threshold" id="mxchat_video_embed_threshold" class="range-slider mxchat-autosave-field" value="<?php echo esc_attr($video_embed_threshold); ?>" min="20" max="95" step="1">
1521 + <span id="mxchat_video_embed_threshold_value" class="range-value"><?php echo esc_html((int) $video_embed_threshold); ?></span>
1522 + </div>
1523 + <p class="mxch-field-description"><?php
1524 + /* translators: %d is the site-wide Similarity Threshold percentage. */
1525 + printf(esc_html__('How closely the video must match the question before its card is shown. Higher means fewer, more relevant cards. This is separate from the site-wide Similarity Threshold (currently %d%%), which decides what the AI reads — a video is worth reading from at a lower score than it is worth putting on screen.', 'mxchat'), (int) $site_threshold);
1526 + ?></p>
1527 + <p class="mxch-field-hint"><?php esc_html_e('Range 20-95. Raise this if visitors are shown videos unrelated to what they asked; lower it if a video that clearly answers the question is not appearing. The right number depends on your embedding model: older models such as text-embedding-ada-002 score even unrelated content around 70%, so those sites usually need 80 or higher before this has any effect. The Transcripts sources panel shows the match percentage of each source, which is the number to compare against.', 'mxchat'); ?></p>
1528 + </div>
1529 + </div>
1530 + </div>
1336 1531 </div>
1337 1532 <?php
1338 1533 }
1339 1534
@@ -1340,22 +1535,22 @@
1340 1535 /**
1341 1536 * Render Role Restrictions Section
1342 1537 */
1343 1538 function mxchat_render_role_restrictions_section($knowledge_manager) {
1344 - $role_options = array(
1345 - 'public' => __('Public (Everyone)', 'mxchat'),
1346 - 'logged_in' => __('Logged In Users', 'mxchat'),
1347 - 'subscriber' => __('Subscribers & Above', 'mxchat'),
1348 - 'contributor' => __('Contributors & Above', 'mxchat'),
1349 - 'author' => __('Authors & Above', 'mxchat'),
1350 - 'editor' => __('Editors & Above', 'mxchat'),
1351 - 'administrator' => __('Administrators Only', 'mxchat')
1352 - );
1539 + // Same source as the per-entry dropdown, so the two selectors cannot drift
1540 + // and both offer the site's custom roles (plan 6ab002). This screen used to
1541 + // hold its own hardcoded copy of the five built-ins.
1542 + $role_options = (is_object($knowledge_manager) && method_exists($knowledge_manager, 'mxchat_get_role_options'))
1543 + ? $knowledge_manager->mxchat_get_role_options()
1544 + : array(
1545 + 'public' => __('Public (Everyone)', 'mxchat'),
1546 + 'logged_in' => __('Logged In Users', 'mxchat'),
1547 + );
1353 1548 ?>
1354 1549 <div id="role-restrictions" class="mxch-section">
1355 1550 <div class="mxch-content-header">
1356 1551 <h1 class="mxch-content-title"><?php esc_html_e('Role-Based Content Restrictions', 'mxchat'); ?></h1>
1357 - <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict content access based on WordPress tags.', 'mxchat'); ?></p>
1552 + <p class="mxch-content-subtitle"><?php esc_html_e('Automatically restrict tagged posts by role. Pages and untagged content are restricted per entry in the Knowledge list.', 'mxchat'); ?></p>
1358 1553 </div>
1359 1554
1360 1555 <div class="mxch-card">
1361 1556 <div class="mxch-card-body">
@@ -1360,16 +1555,17 @@
1360 1555 <div class="mxch-card">
1361 1556 <div class="mxch-card-body">
1362 1557 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1363 1558 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1364 - <span><strong><?php esc_html_e('How it works:', 'mxchat'); ?></strong> <?php esc_html_e('Add a tag below and select which role should have access. Content with that tag will be restricted to that role level.', 'mxchat'); ?></span>
1559 + <span><strong><?php esc_html_e('How it works:', 'mxchat'); ?></strong> <?php esc_html_e('Add a tag below and select which role should have access. Content with that tag will be restricted to that role level. Tags apply to posts — pages and other content have no tags, so restrict those from the role dropdown on the entry itself in the Knowledge list. Any role on this site can be chosen, including custom ones.', 'mxchat'); ?></span>
1365 1560 </div>
1366 1561
1367 1562 <h4 style="margin: 0 0 16px 0;"><?php esc_html_e('Add Tag-Role Mapping', 'mxchat'); ?></h4>
1368 1563 <div style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 24px;">
1369 1564 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1370 - <label class="mxch-field-label" for="mxchat-tag-input"><?php esc_html_e('Tag Name', 'mxchat'); ?></label>
1371 - <input type="text" id="mxchat-tag-input" class="mxch-input" placeholder="<?php esc_attr_e('e.g., premium, members-only', 'mxchat'); ?>">
1565 + <label class="mxch-field-label" for="mxchat-tag-input"><?php esc_html_e('Tag (name or slug)', 'mxchat'); ?></label>
1566 + <input type="text" id="mxchat-tag-input" class="mxch-input" placeholder="<?php esc_attr_e('e.g., Premium Content or premium-content', 'mxchat'); ?>">
1567 + <span class="mxch-field-hint"><?php esc_html_e('Enter an existing post tag by its display name or its slug.', 'mxchat'); ?></span>
1372 1568 </div>
1373 1569 <div class="mxch-field" style="flex: 1; min-width: 200px; margin-bottom: 0;">
1374 1570 <label class="mxch-field-label" for="mxchat-role-select"><?php esc_html_e('Required Role', 'mxchat'); ?></label>
1375 1571 <select id="mxchat-role-select" class="mxch-select">
@@ -1432,8 +1628,23 @@
1432 1628 </div>
1433 1629
1434 1630 <div class="mxch-card">
1435 1631 <div class="mxch-card-body mxchat-autosave-section">
1632 + <?php
1633 + // Install-level ACF→PDF import extraction (plan 11720c) — moved
1634 + // here from a per-import checkbox in the content-selector modal.
1635 + // Default OFF, matching its own "recommended only if…" guidance.
1636 + ?>
1637 + <div class="mxch-field" style="margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--mxch-card-border);">
1638 + <div style="display: flex; align-items: center; gap: 12px;">
1639 + <label class="mxchat-toggle-switch">
1640 + <input type="checkbox" name="mxchat_acf_pdf_extraction" id="mxchat_acf_pdf_extraction" class="mxchat-autosave-field" value="on" <?php checked(get_option('mxchat_acf_pdf_extraction', '0'), '1'); ?>>
1641 + <span class="mxchat-toggle-slider"></span>
1642 + </label>
1643 + <span style="font-weight: 500;"><?php esc_html_e('Extract text from PDFs linked in ACF File/Image fields during imports', 'mxchat'); ?></span>
1644 + </div>
1645 + <p class="mxch-field-description" style="margin-top: 8px;"><?php esc_html_e('Applies to WordPress Content imports. Adds 200-500ms per post and ~30 KB to each knowledge base entry. Recommended only if your ACF setup uses PDF attachments for primary content. Editor-save extraction has its own toggle under Auto-Sync.', 'mxchat'); ?></p>
1646 + </div>
1436 1647 <?php if (!function_exists('acf_get_field_groups')): ?>
1437 1648 <div class="mxch-notice mxch-notice-info">
1438 1649 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1439 1650 <span><?php esc_html_e('Advanced Custom Fields (ACF) plugin is not detected. Install and activate ACF to use this feature.', 'mxchat'); ?></span>
@@ -1448,30 +1659,55 @@
1448 1659 </div>
1449 1660 <?php else: ?>
1450 1661 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1451 1662 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1452 - <span><?php esc_html_e('Toggle fields ON to include them in knowledge base embeddings. Toggle OFF to exclude sensitive or irrelevant fields.', 'mxchat'); ?></span>
1663 + <span><?php esc_html_e('Toggle fields ON to include them in knowledge base embeddings. Toggle OFF to exclude sensitive or irrelevant fields. Exclusions apply everywhere a field appears — ACF location rules (post types, templates) do not scope this list.', 'mxchat'); ?></span>
1453 1664 </div>
1454 1665
1455 - <?php foreach ($all_acf_fields as $group_title => $fields): ?>
1456 - <div class="mxchat-acf-field-group" style="margin-bottom: 24px;">
1457 - <h4 style="margin: 0 0 12px 0; color: var(--mxch-text-primary); font-weight: 600;">
1458 - <?php echo esc_html($group_title); ?>
1459 - </h4>
1666 + <?php foreach ($all_acf_fields as $group_key => $group):
1667 + $fields = $group['fields'];
1668 + // Group toggle state derives from the per-field state —
1669 + // never stored on its own (plan bf57e0).
1670 + $excluded_count = 0;
1671 + foreach ($fields as $field) {
1672 + if (in_array($field['key'], $excluded_fields, true) || in_array($field['name'], $excluded_fields, true)) {
1673 + $excluded_count++;
1674 + }
1675 + }
1676 + $group_all_included = ($excluded_count === 0);
1677 + $group_partial = ($excluded_count > 0 && $excluded_count < count($fields));
1678 + ?>
1679 + <div class="mxchat-acf-field-group" style="margin-bottom: 24px;" data-mxchat-acf-group="<?php echo esc_attr($group_key); ?>">
1680 + <div style="display: flex; align-items: center; justify-content: space-between; margin: 0 0 12px 0;">
1681 + <h4 style="margin: 0; color: var(--mxch-text-primary); font-weight: 600;">
1682 + <?php echo esc_html($group['title']); ?>
1683 + </h4>
1684 + <label class="mxchat-toggle-switch" title="<?php esc_attr_e('Include or exclude every field in this group', 'mxchat'); ?>">
1685 + <input type="checkbox"
1686 + class="mxchat-acf-group-toggle"
1687 + data-group-key="<?php echo esc_attr($group_key); ?>"
1688 + data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1689 + <?php if ($group_partial): ?>data-indeterminate="1"<?php endif; ?>
1690 + <?php checked($group_all_included, true); ?>>
1691 + <span class="mxchat-toggle-slider"></span>
1692 + </label>
1693 + </div>
1460 1694 <div style="background: #f8fafc; border-radius: var(--mxch-radius-md); padding: 16px;">
1461 1695 <?php foreach ($fields as $field):
1462 - $field_name = $field['name'];
1463 - $is_enabled = !in_array($field_name, $excluded_fields);
1696 + // Toggles identify fields by ACF field KEY — names are shared
1697 + // across groups and collide (plan 30e81f). Legacy name entries
1698 + // (group inactive at migration time) still count as excluded.
1699 + $is_enabled = !in_array($field['key'], $excluded_fields, true) && !in_array($field['name'], $excluded_fields, true);
1464 1700 ?>
1465 1701 <div style="display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #e2e8f0;">
1466 1702 <div>
1467 1703 <span style="font-weight: 500;"><?php echo esc_html($field['label']); ?></span>
1468 - <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field_name); ?>)</span>
1704 + <span style="color: var(--mxch-text-muted); font-size: 12px; margin-left: 8px;">(<?php echo esc_html($field['name']); ?>)</span>
1469 1705 <span style="color: var(--mxch-text-muted); font-size: 11px; margin-left: 8px; background: #e2e8f0; padding: 2px 6px; border-radius: 4px;"><?php echo esc_html($field['type']); ?></span>
1470 1706 </div>
1471 1707 <label class="mxchat-toggle-switch">
1472 1708 <input type="checkbox"
1473 - name="mxchat_acf_field_<?php echo esc_attr($field_name); ?>"
1709 + name="mxchat_acf_field_<?php echo esc_attr($field['key']); ?>"
1474 1710 class="mxchat-autosave-field"
1475 1711 value="on"
1476 1712 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1477 1713 <?php checked($is_enabled, true); ?>>
@@ -1508,8 +1744,28 @@
1508 1744 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1509 1745 <span><?php esc_html_e('Enter post meta keys to include in embeddings. This is useful for OptionTree fields, theme meta boxes, or any custom post meta that is not managed by ACF.', 'mxchat'); ?></span>
1510 1746 </div>
1511 1747
1748 + <!-- Meta-key discovery scanner (plan-mxchat-20260709-fe8e4e): mirror the ACF picker's
1749 + discover-for-you UX for non-ACF meta, so owners click keys instead of typing them blind. -->
1750 + <div class="mxch-field mxchat-meta-scan">
1751 + <label class="mxch-field-label"><?php esc_html_e('Discover meta keys', 'mxchat'); ?></label>
1752 + <p class="mxch-field-description" style="margin-top: 0;">
1753 + <?php esc_html_e('Scan your published content for custom meta keys, then click any you want to include. This gives non-ACF meta the same discover-and-click experience as the ACF picker.', 'mxchat'); ?>
1754 + </p>
1755 + <div class="mxchat-meta-scan-controls">
1756 + <button type="button" id="mxchat-scan-meta-btn" class="mxch-btn mxch-btn-secondary" data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_prompts_setting_nonce')); ?>">
1757 + <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
1758 + <span class="mxchat-scan-label"><?php esc_html_e('Scan my content for meta keys', 'mxchat'); ?></span>
1759 + </button>
1760 + <label class="mxchat-meta-scan-internal">
1761 + <input type="checkbox" id="mxchat-scan-internal">
1762 + <?php esc_html_e('Include internal keys (underscore-prefixed)', 'mxchat'); ?>
1763 + </label>
1764 + </div>
1765 + <div id="mxchat-meta-scan-results" class="mxchat-meta-scan-results" aria-live="polite"></div>
1766 + </div>
1767 +
1512 1768 <div class="mxch-field">
1513 1769 <label class="mxch-field-label" for="mxchat_custom_meta_whitelist">
1514 1770 <?php esc_html_e('Meta Key Whitelist', 'mxchat'); ?>
1515 1771 </label>
@@ -1522,9 +1778,9 @@
1522 1778 data-nonce="<?php echo wp_create_nonce('mxchat_prompts_setting_nonce'); ?>"
1523 1779 style="font-family: monospace;"
1524 1780 ><?php echo esc_textarea($whitelist); ?></textarea>
1525 1781 <p class="mxch-field-description">
1526 - <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Only string values are included.', 'mxchat'); ?>
1782 + <?php esc_html_e('Enter one meta key per line. These fields will be appended to the content during embedding. Text values are added as-is; array or repeater values are flattened into a comma-separated list. Empty values are skipped.', 'mxchat'); ?>
1527 1783 </p>
1528 1784 </div>
1529 1785
1530 1786 <div style="margin-top: 20px; padding: 16px; background: #f8fafc; border-radius: var(--mxch-radius-md);">
@@ -1531,9 +1787,9 @@
1531 1787 <h4 style="margin: 0 0 12px 0; font-size: 14px;"><?php esc_html_e('How to find meta keys:', 'mxchat'); ?></h4>
1532 1788 <ul style="margin: 0; padding-left: 20px; color: var(--mxch-text-secondary); font-size: 13px;">
1533 1789 <li><?php esc_html_e('Check your theme documentation for meta key names', 'mxchat'); ?></li>
1534 1790 <li><?php esc_html_e('Look in the wp_postmeta database table', 'mxchat'); ?></li>
1535 - <li><?php esc_html_e('Use a plugin like "Show Post Meta" to view meta keys on any post', 'mxchat'); ?></li>
1791 + <li><?php esc_html_e('Use the Scan my content for meta keys button above to discover the keys on your published content automatically', 'mxchat'); ?></li>
1536 1792 <li><?php esc_html_e('Meta keys starting with underscore (_) are usually hidden/internal', 'mxchat'); ?></li>
1537 1793 </ul>
1538 1794 </div>
1539 1795 </div>
@@ -1593,8 +1849,55 @@
1593 1849 <a href="https://app.pinecone.io/" target="_blank"><?php esc_html_e('Open Pinecone Dashboard', 'mxchat'); ?></a>
1594 1850 </p>
1595 1851 </div>
1596 1852
1853 + <?php
1854 + // Index type (plan 362c31): the classic vector index (default) or a
1855 + // document index with full-text search. Same key, host and namespace.
1856 + $pc_index_type = (($pinecone_options['mxchat_pinecone_index_type'] ?? 'vector') === 'document') ? 'document' : 'vector';
1857 + $pc_docs_cloud = $pinecone_options['mxchat_pinecone_docs_cloud'] ?? 'aws';
1858 + $pc_docs_region = $pinecone_options['mxchat_pinecone_docs_region'] ?? 'us-east-1';
1859 + $pc_docs_lang = $pinecone_options['mxchat_pinecone_docs_language'] ?? 'en';
1860 + $pc_verified = $pinecone_options['mxchat_pinecone_docs_verified_host'] ?? '';
1861 + $pc_vector_host = $pinecone_options['mxchat_pinecone_vector_host'] ?? '';
1862 + $pc_current_host = $pinecone_options['mxchat_pinecone_host'] ?? '';
1863 + $pc_docs_class = class_exists('MxChat_Pinecone_Documents');
1864 + $pc_expected_dim = $pc_docs_class ? MxChat_Pinecone_Documents::expected_dimension() : 1536;
1865 + $pc_migration = $pc_docs_class ? MxChat_Pinecone_Documents::migration_get() : array();
1866 + $pc_host_verified = ($pc_current_host !== '' && strtolower(trim($pc_current_host, '/')) === strtolower($pc_verified));
1867 + $pc_clouds = $pc_docs_class ? MxChat_Pinecone_Documents::clouds() : array('aws' => 'AWS');
1868 + $pc_languages = $pc_docs_class ? MxChat_Pinecone_Documents::languages() : array('en' => 'English');
1869 + ?>
1870 + <div class="mxch-field" id="mxchat-pinecone-index-type-field">
1871 + <span class="mxch-field-label"><?php esc_html_e('Index type', 'mxchat'); ?></span>
1872 + <div style="display: flex; flex-direction: column; gap: 6px; margin-top: 8px;">
1873 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
1874 + <input type="radio" name="mxchat_pinecone_addon_options[mxchat_pinecone_index_type]" class="mxchat-pinecone-index-type" value="vector" <?php checked($pc_index_type, 'vector'); ?>>
1875 + <span>
1876 + <strong><?php esc_html_e('Vector index (current)', 'mxchat'); ?></strong>
1877 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('The classic Pinecone index. Nothing changes for existing setups.', 'mxchat'); ?></span>
1878 + </span>
1879 + </label>
1880 + <label style="display: flex; align-items: baseline; gap: 8px; cursor: pointer;">
1881 + <input type="radio" name="mxchat_pinecone_addon_options[mxchat_pinecone_index_type]" class="mxchat-pinecone-index-type" value="document" <?php checked($pc_index_type, 'document'); ?>>
1882 + <span>
1883 + <strong><?php esc_html_e('Document index with full-text search (new)', 'mxchat'); ?></strong>
1884 + <span class="mxch-field-description" style="display: block; margin: 2px 0 0;"><?php esc_html_e('A Pinecone document index keeps a keyword index of your content next to the vectors, so questions with part numbers, SKUs and codes find the exact record. It is a new index: create one below and copy your existing index into it, no re-embedding needed. Turn on the Hybrid keyword boost under Retrieval to use it.', 'mxchat'); ?></span>
1885 + </span>
1886 + </label>
1887 + </div>
1888 + </div>
1889 + <script type="application/json" id="mxchat-pinecone-docs-state"><?php echo wp_json_encode(array(
1890 + 'verified_host' => $pc_verified,
1891 + 'expected_dimension' => (int) $pc_expected_dim,
1892 + 'migration' => $pc_migration,
1893 + // 3e83e4: what the LIVE chatbot is on right now, so the
1894 + // page can tell a copy-in-progress from a switch.
1895 + 'index_type' => $pc_index_type,
1896 + 'stored_host' => strtolower(trim((string) $pc_current_host, '/')),
1897 + 'stored_index' => (string) ($pinecone_options['mxchat_pinecone_index'] ?? ''),
1898 + )); ?></script>
1899 +
1597 1900 <div class="mxch-field">
1598 1901 <label class="mxch-field-label" for="mxchat_pinecone_environment"><?php esc_html_e('Region', 'mxchat'); ?></label>
1599 1902 <input type="text" id="mxchat_pinecone_environment" name="mxchat_pinecone_addon_options[mxchat_pinecone_environment]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_environment'] ?? ''); ?>" class="mxch-input" placeholder="e.g., gcp-starter">
1600 1903 <p class="mxch-field-description"><?php esc_html_e('Your Pinecone environment/region (e.g., gcp-starter, us-west1-gcp)', 'mxchat'); ?></p>
@@ -1614,8 +1917,90 @@
1614 1917 </label>
1615 1918 <input type="text" id="mxchat_pinecone_host" name="mxchat_pinecone_addon_options[mxchat_pinecone_host]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_host'] ?? ''); ?>" class="mxch-input" placeholder="e.g., my-index-xyz123.svc.pinecone.io">
1616 1919 <p class="mxch-field-description"><?php esc_html_e('The hostname from your Pinecone index URL (exclude https://). Found in index details.', 'mxchat'); ?></p>
1617 1920 </div>
1921 +
1922 + <div class="mxchat-pinecone-docs-settings" <?php echo $pc_index_type === 'document' ? '' : 'style="display: none;"'; ?>>
1923 + <div class="mxch-notice <?php echo $pc_host_verified ? 'mxch-notice-success' : 'mxch-notice-warning'; ?>" id="mxchat-pinecone-docs-status" style="margin-bottom: 20px;">
1924 + <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1925 + <span id="mxchat-pinecone-docs-status-text"><?php
1926 + if ($pc_host_verified) {
1927 + printf(esc_html__('Document index checked: %1$s (%2$d dimensions).', 'mxchat'), esc_html($pc_verified), (int) $pc_expected_dim);
1928 + } else {
1929 + esc_html_e('Not checked yet. Create a document index below, or enter its host above and click Check index. The index type only saves once the host has been checked.', 'mxchat');
1930 + }
1931 + ?></span>
1932 + </div>
1933 +
1934 + <div class="mxch-field">
1935 + <label class="mxch-field-label" for="mxchat_pinecone_docs_cloud"><?php esc_html_e('Cloud and region for a new index', 'mxchat'); ?></label>
1936 + <div style="display: flex; gap: 8px; flex-wrap: wrap;">
1937 + <select id="mxchat_pinecone_docs_cloud" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_cloud]" class="mxch-select" style="max-width: 160px;">
1938 + <?php foreach ($pc_clouds as $pc_cloud_key => $pc_cloud_label) : ?>
1939 + <option value="<?php echo esc_attr($pc_cloud_key); ?>" <?php selected($pc_docs_cloud, $pc_cloud_key); ?>><?php echo esc_html($pc_cloud_label); ?></option>
1940 + <?php endforeach; ?>
1941 + </select>
1942 + <input type="text" id="mxchat_pinecone_docs_region" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_region]" value="<?php echo esc_attr($pc_docs_region); ?>" class="mxch-input" style="max-width: 220px;" placeholder="us-east-1">
1943 + <select id="mxchat_pinecone_docs_language" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_language]" class="mxch-select" style="max-width: 200px;">
1944 + <?php foreach ($pc_languages as $pc_lang_key => $pc_lang_label) : ?>
1945 + <option value="<?php echo esc_attr($pc_lang_key); ?>" <?php selected($pc_docs_lang, $pc_lang_key); ?>><?php echo esc_html($pc_lang_label); ?></option>
1946 + <?php endforeach; ?>
1947 + </select>
1948 + </div>
1949 + <p class="mxch-field-description"><?php printf(esc_html__('Create index for me makes a new document index named after the Index Name field (a free name ending in -docs is suggested when you switch to this type), sized for your embedding model (%d dimensions, cosine), with full-text search on the content in the chosen language, then fills in the host. Check index confirms the host above points at a document index MxChat can use. Neither moves your chatbot — that happens when you save.', 'mxchat'), (int) $pc_expected_dim); ?></p>
1950 + </div>
1951 +
1952 + <div class="mxch-field" style="display: flex; gap: 8px; flex-wrap: wrap;">
1953 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-create"><?php esc_html_e('Create index for me', 'mxchat'); ?></button>
1954 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-check"><?php esc_html_e('Check index', 'mxchat'); ?></button>
1955 + </div>
1956 + <div id="mxchat-pinecone-docs-result" style="display: none;"></div>
1957 + <p class="mxch-field-hint" id="mxchat-pinecone-docs-gate" style="display: none;"><?php esc_html_e('Check the document index host before saving.', 'mxchat'); ?></p>
1958 + <div class="mxch-field" id="mxchat-pinecone-docs-switch-gate" style="display: none;">
1959 + <p class="mxch-field-hint" id="mxchat-pinecone-docs-switch-gate-text"></p>
1960 + <label class="mxch-field-hint" style="display: flex; align-items: center; gap: 8px; cursor: pointer;">
1961 + <input type="checkbox" id="mxchat_pinecone_docs_switch_anyway" name="mxchat_pinecone_addon_options[mxchat_pinecone_docs_switch_anyway]" value="1">
1962 + <span><?php esc_html_e('Switch anyway — point the chatbot at the document index now and copy the rest afterwards', 'mxchat'); ?></span>
1963 + </label>
1964 + </div>
1965 +
1966 + <div class="mxch-field" style="margin-top: 20px;">
1967 + <label class="mxch-field-label" for="mxchat_pinecone_vector_host"><?php esc_html_e('Copy from vector index host', 'mxchat'); ?></label>
1968 + <input type="text" id="mxchat_pinecone_vector_host" name="mxchat_pinecone_addon_options[mxchat_pinecone_vector_host]" value="<?php echo esc_attr($pc_vector_host); ?>" class="mxch-input" placeholder="my-index-xyz123.svc.pinecone.io">
1969 + <p class="mxch-field-description"><?php esc_html_e('Migrate copies every record of that index into the document index above — text, vector and metadata, same ids — with no embedding calls. It runs while your chatbot keeps answering from the vector index; save the Pinecone settings afterwards to switch. Safe to run again: records already copied are simply replaced, and an interrupted copy continues where it stopped. Both indexes must use the same embedding dimension.', 'mxchat'); ?></p>
1970 + </div>
1971 + <div class="mxch-field" style="display: flex; gap: 12px; align-items: center; flex-wrap: wrap;">
1972 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-pinecone-docs-migrate"><?php esc_html_e('Migrate', 'mxchat'); ?></button>
1973 + <?php /* e50d2e: offered only when the source index holds records in more than one namespace (the JS shows it from the start response). No name attribute — it is not a saved setting. */ ?>
1974 + <label class="mxch-toggle" id="mxchat-pinecone-docs-all-namespaces" style="display: none;" title="<?php esc_attr_e('Copy the records of every namespace of the vector index, each into the same-named namespace of the document index, in one run.', 'mxchat'); ?>">
1975 + <input type="checkbox" class="mxch-toggle-input" id="mxchat_pinecone_docs_all_namespaces" value="1">
1976 + <span class="mxch-toggle-switch"></span>
1977 + <span class="mxch-toggle-label"><?php esc_html_e('Copy every namespace', 'mxchat'); ?></span>
1978 + </label>
1979 + <span id="mxchat-pinecone-docs-migrate-counts" class="mxch-field-description"></span>
1980 + </div>
1981 + <div id="mxchat-pinecone-docs-migrate-progress" style="display: none; margin-bottom: 16px;">
1982 + <div class="mxch-progress-bar"><div class="mxch-progress-bar-fill" style="width: 0%;"></div></div>
1983 + <div class="mxch-progress-label"></div>
1984 + </div>
1985 +
1986 + <div class="mxch-field" id="mxchat-pinecone-docs-delete-old" style="display: none;">
1987 + <label class="mxch-field-label" for="mxchat_pinecone_docs_delete_confirm"><?php esc_html_e('Delete old index', 'mxchat'); ?></label>
1988 + <p class="mxch-field-description"><?php esc_html_e('Available once the copy count matches. Type the old index name to confirm. This deletes the whole vector index at Pinecone and cannot be undone.', 'mxchat'); ?></p>
1989 + <div style="display: flex; gap: 8px; flex-wrap: wrap;">
1990 + <input type="text" id="mxchat_pinecone_docs_delete_confirm" class="mxch-input" style="max-width: 320px;" placeholder="<?php esc_attr_e('old index name', 'mxchat'); ?>" autocomplete="off">
1991 + <button type="button" class="mxch-btn mxch-btn-danger" id="mxchat-pinecone-docs-delete-old-btn"><?php esc_html_e('Delete old index', 'mxchat'); ?></button>
1992 + </div>
1993 + </div>
1994 + </div>
1995 +
1996 + <div class="mxch-field">
1997 + <label class="mxch-field-label" for="mxchat_pinecone_top_k">
1998 + <?php esc_html_e('Results Per Query (topK)', 'mxchat'); ?>
1999 + </label>
2000 + <input type="number" id="mxchat_pinecone_top_k" name="mxchat_pinecone_addon_options[mxchat_pinecone_top_k]" value="<?php echo esc_attr($pinecone_options['mxchat_pinecone_top_k'] ?? '50'); ?>" class="mxch-input" min="1" max="1000" step="1" placeholder="50">
2001 + <p class="mxch-field-description"><?php esc_html_e('How many candidate matches each Pinecone query requests (1-1000, default 50). Higher values improve grouping of chunked content at the cost of slightly larger responses.', 'mxchat'); ?></p>
2002 + </div>
1618 2003 </div>
1619 2004
1620 2005 <?php submit_button(__('Save Pinecone Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1621 2006 </form>
@@ -1632,12 +2017,16 @@
1632 2017 $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
1633 2018 $use_vectorstore = $vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0';
1634 2019 $vectorstore_ids = $vectorstore_options['mxchat_vectorstore_ids'] ?? '';
1635 2020 $max_results = $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5;
2021 + $sync_enabled = $vectorstore_options['mxchat_vectorstore_sync_enabled'] ?? '0';
2022 + $sync_store_id = $vectorstore_options['mxchat_vectorstore_sync_store_id'] ?? '';
2023 + $sync_config = class_exists('MxChat_Vectorstore_Manager') ? MxChat_Vectorstore_Manager::get_sync_config('default') : array('enabled' => false, 'store_id' => '');
2024 + $mapped_count = class_exists('MxChat_Vectorstore_Manager') ? MxChat_Vectorstore_Manager::mapped_file_count() : 0;
1636 2025
1637 2026 // Get current chat model to check compatibility
1638 2027 $mxchat_options = get_option('mxchat_options', array());
1639 - $current_model = $mxchat_options['model'] ?? 'gpt-5.1-chat-latest';
2028 + $current_model = $mxchat_options['model'] ?? 'gpt-5.6-sol';
1640 2029 $is_openai_model = preg_match('/^(gpt-|o1-|o3-)/', $current_model);
1641 2030 ?>
1642 2031 <div id="openai-vectorstore" class="mxch-section">
1643 2032 <div class="mxch-content-header">
@@ -1648,9 +2037,9 @@
1648 2037 <div class="mxch-card">
1649 2038 <div class="mxch-card-body">
1650 2039 <div class="mxch-notice mxch-notice-info" style="margin-bottom: 20px;">
1651 2040 <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
1652 - <span><strong><?php esc_html_e('Requires OpenAI Chat Model.', 'mxchat'); ?></strong> <?php esc_html_e('Vector Store search only works with OpenAI models (gpt-5.1-chat-latest, gpt-5-mini, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
2041 + <span><strong><?php esc_html_e('Requires OpenAI Chat Model.', 'mxchat'); ?></strong> <?php esc_html_e('Vector Store search only works with OpenAI models (gpt-5.6-sol, gpt-5.6-terra, etc.). Create Vector Stores in your OpenAI Dashboard.', 'mxchat'); ?></span>
1653 2042 </div>
1654 2043
1655 2044 <?php if (!$is_openai_model && $use_vectorstore === '1'): ?>
1656 2045 <div class="mxch-notice mxch-notice-warning" style="margin-bottom: 20px;">
@@ -1703,10 +2092,143 @@
1703 2092 <p class="mxch-field-description"><?php esc_html_e('Maximum number of content chunks to retrieve from the Vector Store.', 'mxchat'); ?></p>
1704 2093 </div>
1705 2094 </div>
1706 2095
2096 + <div class="mxch-field" style="border-top: 1px solid var(--mxch-border, #e2e4e7); padding-top: 20px; margin-top: 4px;">
2097 + <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 8px;">
2098 + <label class="mxchat-toggle-switch">
2099 + <input type="checkbox" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_sync_enabled]" value="1" <?php checked($sync_enabled, '1'); ?> id="mxchat_vectorstore_sync_enabled">
2100 + <span class="mxchat-toggle-slider"></span>
2101 + </label>
2102 + <span style="font-weight: 500;"><?php esc_html_e('Sync Knowledge Base to Vector Store', 'mxchat'); ?></span>
2103 + </div>
2104 + <p class="mxch-field-description" style="margin-bottom: 16px;">
2105 + <?php esc_html_e('Keeps your Vector Store up to date automatically: knowledge base additions, edits, and deletions are mirrored to the store (one file per entry, replaced on change). Without this, the store is frozen at whatever you uploaded manually. Role-restricted entries are never uploaded — OpenAI file search cannot filter by user role.', 'mxchat'); ?>
2106 + </p>
2107 +
2108 + <label class="mxch-field-label" for="mxchat_vectorstore_sync_store_id">
2109 + <?php esc_html_e('Sync Target Vector Store ID', 'mxchat'); ?>
2110 + </label>
2111 + <input type="text" id="mxchat_vectorstore_sync_store_id" name="mxchat_openai_vectorstore_options[mxchat_vectorstore_sync_store_id]" value="<?php echo esc_attr($sync_store_id); ?>" class="mxch-input" placeholder="<?php echo esc_attr($sync_config['store_id'] ?: 'vs_abc123xyz'); ?>">
2112 + <p class="mxch-field-description">
2113 + <?php esc_html_e('Leave blank to use the first Vector Store ID above. Syncing writes to exactly one store.', 'mxchat'); ?>
2114 + </p>
2115 + </div>
2116 +
1707 2117 <?php submit_button(__('Save Vector Store Settings', 'mxchat'), 'primary', 'submit', true, array('class' => 'mxch-btn mxch-btn-primary')); ?>
1708 2118 </form>
2119 +
2120 + <?php if ($sync_config['enabled']): ?>
2121 + <div class="mxch-field" id="mxchat-vs-import" style="border-top: 1px solid var(--mxch-border, #e2e4e7); padding-top: 20px;"
2122 + data-nonce="<?php echo esc_attr(wp_create_nonce('mxchat_admin_nonce')); ?>">
2123 + <label class="mxch-field-label"><?php esc_html_e('Import Existing Knowledge Base', 'mxchat'); ?></label>
2124 + <p class="mxch-field-description">
2125 + <?php printf(esc_html__('One-time (re-runnable) import of every knowledge base entry into the sync target store. Runs in the background via WP-Cron; safe to interrupt and resume — already-imported, unchanged entries are skipped. Currently mirrored: %s files.', 'mxchat'), '<strong id="mxchat-vs-mapped">' . esc_html(number_format_i18n($mapped_count)) . '</strong>'); ?>
2126 + </p>
2127 + <div style="display: flex; align-items: center; gap: 8px; margin: 10px 0;">
2128 + <button type="button" class="mxch-btn mxch-btn-secondary" id="mxchat-vs-import-start"><?php esc_html_e('Import Knowledge Base to Vector Store', 'mxchat'); ?></button>
2129 + <button type="button" class="mxch-btn" id="mxchat-vs-import-resume" style="display:none;"><?php esc_html_e('Resume Import', 'mxchat'); ?></button>
2130 + <button type="button" class="mxch-btn" id="mxchat-vs-import-cancel" style="display:none;"><?php esc_html_e('Cancel', 'mxchat'); ?></button>
2131 + </div>
2132 + <div id="mxchat-vs-import-bar" style="display:none; height: 8px; background: var(--mxch-border, #e2e4e7); border-radius: 4px; overflow: hidden; max-width: 420px;">
2133 + <div id="mxchat-vs-import-fill" style="height: 100%; width: 0%; background: #2271b1; transition: width .4s;"></div>
2134 + </div>
2135 + <p class="mxch-field-description" id="mxchat-vs-import-status" style="margin-top: 8px;"></p>
2136 + </div>
2137 +
2138 + <script>
2139 + (function() {
2140 + var wrap = document.getElementById('mxchat-vs-import');
2141 + if (!wrap) { return; }
2142 + var nonce = wrap.getAttribute('data-nonce');
2143 + var startBtn = document.getElementById('mxchat-vs-import-start');
2144 + var resumeBtn = document.getElementById('mxchat-vs-import-resume');
2145 + var cancelBtn = document.getElementById('mxchat-vs-import-cancel');
2146 + var bar = document.getElementById('mxchat-vs-import-bar');
2147 + var fill = document.getElementById('mxchat-vs-import-fill');
2148 + var statusEl = document.getElementById('mxchat-vs-import-status');
2149 + var mappedEl = document.getElementById('mxchat-vs-mapped');
2150 + var pollTimer = null;
2151 +
2152 + function post(action, done) {
2153 + var body = 'action=' + encodeURIComponent(action) + '&nonce=' + encodeURIComponent(nonce);
2154 + fetch(ajaxurl, {
2155 + method: 'POST',
2156 + credentials: 'same-origin',
2157 + headers: {'Content-Type': 'application/x-www-form-urlencoded'},
2158 + body: body
2159 + }).then(function(r) { return r.json(); }).then(done).catch(function() {
2160 + statusEl.textContent = '<?php echo esc_js(__('Request failed — check the browser console.', 'mxchat')); ?>';
2161 + });
2162 + }
2163 +
2164 + function render(state, mapped) {
2165 + if (typeof mapped === 'number' && mappedEl) { mappedEl.textContent = mapped.toLocaleString(); }
2166 + var running = state.status === 'running';
2167 + startBtn.style.display = running ? 'none' : '';
2168 + cancelBtn.style.display = running ? '' : 'none';
2169 + resumeBtn.style.display = (running && state.stalled) ? '' : 'none';
2170 + bar.style.display = (running || state.status === 'done') ? '' : 'none';
2171 + var pct = state.total > 0 ? Math.min(100, Math.round(state.processed / state.total * 100)) : 0;
2172 + if (state.status === 'done') { pct = 100; }
2173 + fill.style.width = pct + '%';
2174 +
2175 + var bits = [];
2176 + if (state.status === 'running') { bits.push(state.stalled ? '<?php echo esc_js(__('Import stalled (cron missed) — click Resume.', 'mxchat')); ?>' : '<?php echo esc_js(__('Importing…', 'mxchat')); ?>'); }
2177 + if (state.status === 'done') { bits.push('<?php echo esc_js(__('Import complete.', 'mxchat')); ?>'); }
2178 + if (state.status === 'cancelled') { bits.push('<?php echo esc_js(__('Import cancelled.', 'mxchat')); ?>'); }
2179 + if (state.status === 'error') { bits.push('<?php echo esc_js(__('Import error:', 'mxchat')); ?> ' + state.last_error); }
2180 + if (state.status !== 'idle') {
2181 + bits.push(state.processed + '/' + state.total
2182 + + ' — ' + state.imported + ' <?php echo esc_js(__('uploaded', 'mxchat')); ?>'
2183 + + ', ' + state.unchanged + ' <?php echo esc_js(__('unchanged', 'mxchat')); ?>'
2184 + + ', ' + state.skipped_restricted + ' <?php echo esc_js(__('skipped (role-restricted)', 'mxchat')); ?>'
2185 + + ', ' + state.failed + ' <?php echo esc_js(__('failed', 'mxchat')); ?>');
2186 + if (state.failed > 0 && state.last_error && state.status !== 'error') {
2187 + bits.push('<?php echo esc_js(__('Last error:', 'mxchat')); ?> ' + state.last_error);
2188 + }
2189 + }
2190 + statusEl.textContent = bits.join(' ');
2191 +
2192 + if (running && !pollTimer) {
2193 + pollTimer = setInterval(function() {
2194 + post('mxchat_vectorstore_import_status', function(res) {
2195 + if (res && res.success) { render(res.data.state, res.data.mapped); }
2196 + });
2197 + }, 3000);
2198 + } else if (!running && pollTimer) {
2199 + clearInterval(pollTimer);
2200 + pollTimer = null;
2201 + }
2202 + }
2203 +
2204 + startBtn.addEventListener('click', function() {
2205 + startBtn.disabled = true;
2206 + statusEl.textContent = '<?php echo esc_js(__('Starting…', 'mxchat')); ?>';
2207 + post('mxchat_vectorstore_import_start', function(res) {
2208 + startBtn.disabled = false;
2209 + if (res && res.success) { render(res.data.state); }
2210 + else { statusEl.textContent = (res && res.data && res.data.message) ? res.data.message : '<?php echo esc_js(__('Could not start the import.', 'mxchat')); ?>'; }
2211 + });
2212 + });
2213 + resumeBtn.addEventListener('click', function() {
2214 + post('mxchat_vectorstore_import_resume', function(res) {
2215 + if (res && res.success) { render(res.data.state); }
2216 + });
2217 + });
2218 + cancelBtn.addEventListener('click', function() {
2219 + post('mxchat_vectorstore_import_cancel', function(res) {
2220 + if (res && res.success) { render(res.data.state); }
2221 + });
2222 + });
2223 +
2224 + // Pick up an import already in flight when the page loads.
2225 + post('mxchat_vectorstore_import_status', function(res) {
2226 + if (res && res.success && res.data.state.status !== 'idle') { render(res.data.state, res.data.mapped); }
2227 + });
2228 + })();
2229 + </script>
2230 + <?php endif; ?>
1709 2231 </div>
1710 2232 </div>
1711 2233 </div>
1712 2234 <?php
@@ -1755,8 +2277,20 @@
1755 2277 <option value="unprocessed"><?php esc_html_e('Not In Knowledge Base', 'mxchat'); ?></option>
1756 2278 </select>
1757 2279 </div>
1758 2280 </div>
2281 + <?php
2282 + // Shown only while Media is the selected type (plan 01010a). Media lists
2283 + // just the files whose text MxChat can read; saying so here is clearer
2284 + // than listing images and audio as rows that cannot be selected.
2285 + ?>
2286 + <div class="mxchat-kb-filter-note" id="mxchat-kb-media-note" hidden>
2287 + <svg class="mxchat-kb-filter-note-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
2288 + <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
2289 + <path d="M14 2v6h6"></path>
2290 + </svg>
2291 + <span><?php esc_html_e('Media lists PDF, Word, text and Markdown files — the types MxChat can read text from. Images, audio and video are not shown.', 'mxchat'); ?></span>
2292 + </div>
1759 2293 <div class="mxchat-kb-content-selection">
1760 2294 <div class="mxchat-kb-selection-header">
1761 2295 <label>
1762 2296 <input type="checkbox" id="mxchat-kb-select-all">
@@ -1776,15 +2310,8 @@
1776 2310 </div>
1777 2311 </div>
1778 2312 <div class="mxchat-kb-modal-footer">
1779 2313 <div class="mxchat-kb-pagination"></div>
1780 - <div class="mxchat-kb-acf-pdf-option" style="display:flex; align-items:flex-start; gap:8px; padding:8px 0; font-size:12px; color: var(--mxch-text-secondary, #64748b);">
1781 - <input type="checkbox" id="mxchat-kb-acf-pdf-extract" style="margin-top:2px;">
1782 - <label for="mxchat-kb-acf-pdf-extract" style="cursor:pointer; line-height:1.4;">
1783 - <strong style="color: var(--mxch-text-primary, #1a1a2e);"><?php esc_html_e('Also extract text from PDFs linked in ACF File/Image fields', 'mxchat'); ?></strong><br>
1784 - <span><?php esc_html_e('Adds 200-500ms per post and ~30 KB to each KB entry. Recommended only if your ACF setup uses PDF attachments for primary content.', 'mxchat'); ?></span>
1785 - </label>
1786 - </div>
1787 2314 <div class="mxchat-kb-footer-actions">
1788 2315 <button type="button" id="mxchat-kb-process-selected" class="mxchat-kb-button-primary" disabled>
1789 2316 <?php esc_html_e('Process Selected Content', 'mxchat'); ?>
1790 2317 <span class="mxchat-kb-selected-count">(0)</span>
@@ -1838,93 +2365,235 @@
1838 2365 <?php
1839 2366 }
1840 2367
1841 2368 /**
1842 - * Render Knowledge Page Navigation Scripts
2369 + * Render the read-only Knowledge Inspector modal (plan-mxchat-20260628-d8cb4b).
2370 + * Shows exactly what was indexed for an entry: the per-chunk stored text + lengths
2371 + * and (for Pinecone) the stored metadata fields, rendered with the Testing tab's
2372 + * own card components so the two surfaces feel like one system. Self-contained
2373 + * markup + JS so it doesn't entangle the edit-modal init.
1843 2374 */
1844 -function mxchat_render_knowledge_page_scripts() {
2375 +function mxchat_render_inspect_entry_modal() {
1845 2376 ?>
2377 + <div id="mxchat-kb-inspect-modal" class="mxchat-kb-modal">
2378 + <div class="mxchat-kb-modal-content mxchat-kb-inspect-modal-content">
2379 + <div class="mxchat-kb-modal-header">
2380 + <h3 id="mxchat-kb-inspect-title"><?php esc_html_e('Indexed Content', 'mxchat'); ?></h3>
2381 + <span class="mxchat-kb-modal-close" id="mxchat-kb-inspect-close">&times;</span>
2382 + </div>
2383 + <div class="mxchat-kb-inspect-body">
2384 + <div class="mxchat-kb-edit-source" id="mxchat-kb-inspect-source"></div>
2385 + <div class="mxch-testing-results" id="mxchat-kb-inspect-results">
2386 + <div class="mxch-testing-no-data"><?php esc_html_e('Loading indexed content…', 'mxchat'); ?></div>
2387 + </div>
2388 + </div>
2389 + <div class="mxchat-kb-edit-footer">
2390 + <div class="mxchat-kb-edit-notice" id="mxchat-kb-inspect-notice"></div>
2391 + <div class="mxchat-kb-edit-actions">
2392 + <button type="button" id="mxchat-kb-inspect-close-btn" class="mxch-btn mxch-btn-primary">
2393 + <?php esc_html_e('Close', 'mxchat'); ?>
2394 + </button>
2395 + </div>
2396 + </div>
2397 + </div>
2398 + </div>
1846 2399 <script>
1847 2400 (function() {
1848 2401 'use strict';
1849 -
1850 - // Wait for DOM
1851 2402 document.addEventListener('DOMContentLoaded', function() {
1852 - initKnowledgeNavigation();
1853 - initMobileMenu();
1854 - initImportOptions();
1855 - initCustomPostTypesToggle();
1856 - // Sitemap detection is now initialized when user clicks the Sitemap Import option
1857 - });
2403 + var modal = document.getElementById('mxchat-kb-inspect-modal');
2404 + if (!modal) return;
2405 + var closeX = document.getElementById('mxchat-kb-inspect-close');
2406 + var closeBtn = document.getElementById('mxchat-kb-inspect-close-btn');
2407 + var sourceEl = document.getElementById('mxchat-kb-inspect-source');
2408 + var resultsEl= document.getElementById('mxchat-kb-inspect-results');
2409 + var titleEl = document.getElementById('mxchat-kb-inspect-title');
2410 + var noticeEl = document.getElementById('mxchat-kb-inspect-notice');
1858 2411
1859 - function initKnowledgeNavigation() {
1860 - const navLinks = document.querySelectorAll('.mxch-nav-link[data-target], .mxch-mobile-nav-link[data-target]');
1861 - const sections = document.querySelectorAll('.mxch-section');
2412 + var I18N = {
2413 + title: <?php echo wp_json_encode( esc_html__('Indexed Content', 'mxchat') ); ?>,
2414 + loading: <?php echo wp_json_encode( esc_html__('Loading indexed content…', 'mxchat') ); ?>,
2415 + manual: <?php echo wp_json_encode( esc_html__('Manual Content', 'mxchat') ); ?>,
2416 + source: <?php echo wp_json_encode( esc_html__('Source', 'mxchat') ); ?>,
2417 + store: <?php echo wp_json_encode( esc_html__('Storage', 'mxchat') ); ?>,
2418 + type: <?php echo wp_json_encode( esc_html__('Content type', 'mxchat') ); ?>,
2419 + chunksLbl:<?php echo wp_json_encode( esc_html__('Chunks', 'mxchat') ); ?>,
2420 + totalLen: <?php echo wp_json_encode( esc_html__('Total indexed length', 'mxchat') ); ?>,
2421 + chunk: <?php echo wp_json_encode( esc_html__('Chunk', 'mxchat') ); ?>,
2422 + indexed: <?php echo wp_json_encode( esc_html__('Indexed', 'mxchat') ); ?>,
2423 + chars: <?php echo wp_json_encode( esc_html__('chars', 'mxchat') ); ?>,
2424 + showText: <?php echo wp_json_encode( esc_html__('Show indexed text', 'mxchat') ); ?>,
2425 + hideText: <?php echo wp_json_encode( esc_html__('Hide indexed text', 'mxchat') ); ?>,
2426 + wpStore: <?php echo wp_json_encode( esc_html__('Local WordPress database', 'mxchat') ); ?>,
2427 + pcStore: <?php echo wp_json_encode( esc_html__('Pinecone', 'mxchat') ); ?>,
2428 + meta: <?php echo wp_json_encode( esc_html__('Vector metadata', 'mxchat') ); ?>,
2429 + loadFail: <?php echo wp_json_encode( esc_html__('Failed to load indexed content.', 'mxchat') ); ?>,
2430 + netErr: <?php echo wp_json_encode( esc_html__('Network error: ', 'mxchat') ); ?>
2431 + };
1862 2432
1863 - function showSection(targetId) {
1864 - // Hide all sections
1865 - sections.forEach(section => section.classList.remove('active'));
1866 - // Show target section
1867 - const target = document.getElementById(targetId);
1868 - if (target) {
1869 - target.classList.add('active');
1870 - }
1871 - // Update nav links
1872 - navLinks.forEach(link => {
1873 - link.classList.toggle('active', link.dataset.target === targetId);
1874 - });
1875 - // Update URL hash
1876 - history.replaceState(null, null, '#' + targetId);
2433 + function esc(str) {
2434 + var d = document.createElement('div');
2435 + d.textContent = (str === null || str === undefined) ? '' : String(str);
2436 + return d.innerHTML;
1877 2437 }
2438 + function fmt(n) { try { return Number(n).toLocaleString(); } catch (e) { return n; } }
1878 2439
1879 - navLinks.forEach(link => {
1880 - link.addEventListener('click', function(e) {
1881 - e.preventDefault();
1882 - const targetId = this.dataset.target;
1883 - showSection(targetId);
1884 - // Close mobile menu if open
1885 - const mobileMenu = document.querySelector('.mxch-mobile-menu');
1886 - const overlay = document.querySelector('.mxch-mobile-overlay');
1887 - if (mobileMenu) mobileMenu.classList.remove('open');
1888 - if (overlay) overlay.classList.remove('open');
1889 - });
2440 + document.addEventListener('click', function(e) {
2441 + var btn = e.target.closest('.mxchat-inspect-entry-btn');
2442 + if (btn) { e.preventDefault(); open(btn); }
1890 2443 });
2444 + if (closeX) closeX.addEventListener('click', close);
2445 + if (closeBtn) closeBtn.addEventListener('click', close);
2446 + // Overlay click closes, but only when the whole gesture happened on the
2447 + // overlay — otherwise selecting indexed text and releasing past the
2448 + // dialog edge dispatches the click on the overlay and closes it.
2449 + var downOnOverlay = false;
2450 + modal.addEventListener('mousedown', function(e) { downOnOverlay = (e.target === modal); });
2451 + modal.addEventListener('click', function(e) {
2452 + var overlayGesture = downOnOverlay;
2453 + downOnOverlay = false;
2454 + if (e.target === modal && overlayGesture) close();
2455 + });
2456 + document.addEventListener('keydown', function(e) {
2457 + if (e.key === 'Escape' && modal.classList.contains('active')) close();
2458 + });
1891 2459
1892 - // Handle initial hash
1893 - const hash = window.location.hash.substring(1);
1894 - if (hash && document.getElementById(hash)) {
1895 - showSection(hash);
2460 + function close() { modal.classList.remove('active'); }
2461 +
2462 + function open(btn) {
2463 + var entry = {
2464 + sourceUrl: btn.getAttribute('data-source-url') || '',
2465 + entryId: btn.getAttribute('data-entry-id') || '',
2466 + dataSource: btn.getAttribute('data-data-source') || 'wordpress',
2467 + botId: btn.getAttribute('data-bot-id') || 'default',
2468 + nonce: btn.getAttribute('data-nonce') || ''
2469 + };
2470 +
2471 + titleEl.textContent = I18N.title;
2472 + noticeEl.textContent = '';
2473 + noticeEl.className = 'mxchat-kb-edit-notice';
2474 + resultsEl.innerHTML = '<div class="mxch-testing-no-data">' + esc(I18N.loading) + '</div>';
2475 +
2476 + if (entry.sourceUrl && entry.sourceUrl.indexOf('mxchat://') !== 0 && entry.sourceUrl.indexOf('_ungrouped_') !== 0) {
2477 + sourceEl.innerHTML = esc(I18N.source) + ': <a href="' + esc(entry.sourceUrl) + '" target="_blank" rel="noopener">' + esc(entry.sourceUrl) + '</a>';
2478 + } else {
2479 + sourceEl.textContent = I18N.manual;
2480 + }
2481 +
2482 + modal.classList.add('active');
2483 +
2484 + var fd = new FormData();
2485 + fd.append('action', 'mxchat_inspect_entry');
2486 + fd.append('nonce', entry.nonce);
2487 + fd.append('source_url', entry.sourceUrl);
2488 + fd.append('entry_id', entry.entryId);
2489 + fd.append('data_source', entry.dataSource);
2490 + fd.append('bot_id', entry.botId);
2491 +
2492 + fetch(ajaxurl, { method: 'POST', body: fd })
2493 + .then(function(r) { return r.json(); })
2494 + .then(function(resp) {
2495 + if (resp && resp.success) {
2496 + render(resp.data);
2497 + } else {
2498 + var msg = (resp && resp.data && resp.data.message) || I18N.loadFail;
2499 + resultsEl.innerHTML = '';
2500 + showNotice(msg, 'error');
2501 + }
2502 + })
2503 + .catch(function(err) {
2504 + resultsEl.innerHTML = '';
2505 + showNotice(I18N.netErr + err.message, 'error');
2506 + });
1896 2507 }
1897 - }
1898 2508
1899 - function initMobileMenu() {
1900 - const menuBtn = document.querySelector('.mxch-mobile-menu-btn');
1901 - const closeBtn = document.querySelector('.mxch-mobile-menu-close');
1902 - const overlay = document.querySelector('.mxch-mobile-overlay');
1903 - const menu = document.querySelector('.mxch-mobile-menu');
2509 + function render(data) {
2510 + var storeLabel = data.store === 'pinecone' ? I18N.pcStore : I18N.wpStore;
2511 + var html = '';
1904 2512
1905 - if (menuBtn && menu) {
1906 - menuBtn.addEventListener('click', function() {
1907 - menu.classList.add('open');
1908 - if (overlay) overlay.classList.add('open');
2513 + // Summary card — what this entry is + how much was indexed.
2514 + html += '<div class="match-card above-threshold">' +
2515 + '<div class="match-header">' +
2516 + '<div class="match-title"><span class="status-icon">&#128230;</span> ' + esc(data.chunk_count) + ' ' + esc(I18N.chunksLbl.toLowerCase()) + '</div>' +
2517 + '<span class="context-label">' + esc(storeLabel) + '</span>' +
2518 + '</div>' +
2519 + '<div class="mxch-testing-field"><label>' + esc(I18N.type) + '</label><code>' + esc(data.content_type || '—') + '</code> ' +
2520 + '&nbsp; <label style="display:inline; text-transform:none; letter-spacing:0; font-weight:600;">' + esc(I18N.totalLen) + ':</label> ' + esc(fmt(data.assembled_length)) + ' ' + esc(I18N.chars) + '</div>';
2521 + if (data.metadata_note) {
2522 + html += '<div class="match-source"><span class="source-icon">&#8505;&#65039;</span> ' + esc(data.metadata_note) + '</div>';
2523 + }
2524 + html += '</div>';
2525 +
2526 + // One card per stored chunk — the actual embedded text.
2527 + var chunks = data.chunks || [];
2528 + chunks.forEach(function(chunk, i) {
2529 + var num = (chunk.index !== null && chunk.index !== undefined) ? (chunk.index + 1) : (i + 1);
2530 + var metaRows = '';
2531 + if (chunk.metadata && typeof chunk.metadata === 'object') {
2532 + var keys = Object.keys(chunk.metadata);
2533 + if (keys.length) {
2534 + metaRows += '<div class="chunk-detail-row chunk-used" style="display:block;"><span class="chunk-detail-num">' + esc(I18N.meta) + '</span></div>';
2535 + keys.forEach(function(k) {
2536 + metaRows += '<div class="chunk-detail-row"><span class="chunk-detail-num">' + esc(k) + '</span><span class="chunk-detail-score">' + esc(chunk.metadata[k]) + '</span></div>';
2537 + });
2538 + }
2539 + }
2540 + html += '<div class="match-card above-threshold">' +
2541 + '<div class="match-header">' +
2542 + '<div class="match-title"><span class="status-icon">&#10003;</span> ' + esc(I18N.chunk) + ' ' + esc(num) +
2543 + '<span class="chunk-summary">' + esc(fmt(chunk.length)) + ' ' + esc(I18N.chars) + '</span>' +
2544 + '</div>' +
2545 + '<span class="context-label">' + esc(I18N.indexed) + '</span>' +
2546 + '</div>' +
2547 + '<span class="chunk-expand-toggle" data-chunk="' + i + '">&#9654; ' + esc(I18N.showText) + '</span>' +
2548 + '<div class="chunk-details" data-chunk="' + i + '">' +
2549 + '<pre class="mxch-kb-inspect-chunk-text">' + esc(chunk.text) + '</pre>' +
2550 + metaRows +
2551 + '</div>' +
2552 + '</div>';
1909 2553 });
1910 - }
1911 2554
1912 - if (closeBtn && menu) {
1913 - closeBtn.addEventListener('click', function() {
1914 - menu.classList.remove('open');
1915 - if (overlay) overlay.classList.remove('open');
2555 + resultsEl.innerHTML = html;
2556 +
2557 + resultsEl.querySelectorAll('.chunk-expand-toggle').forEach(function(toggle) {
2558 + toggle.addEventListener('click', function() {
2559 + var id = this.getAttribute('data-chunk');
2560 + var details = resultsEl.querySelector('.chunk-details[data-chunk="' + id + '"]');
2561 + if (!details) return;
2562 + var expanded = details.classList.toggle('expanded');
2563 + this.innerHTML = (expanded ? '&#9660; ' + esc(I18N.hideText) : '&#9654; ' + esc(I18N.showText));
2564 + });
1916 2565 });
1917 2566 }
1918 2567
1919 - if (overlay && menu) {
1920 - overlay.addEventListener('click', function() {
1921 - menu.classList.remove('open');
1922 - overlay.classList.remove('open');
1923 - });
2568 + function showNotice(msg, type) {
2569 + noticeEl.textContent = msg;
2570 + noticeEl.className = 'mxchat-kb-edit-notice' + (type ? ' ' + type : '');
1924 2571 }
1925 - }
2572 + });
2573 + })();
2574 + </script>
2575 + <?php
2576 +}
1926 2577
2578 +/**
2579 + * Render Knowledge Page Navigation Scripts
2580 + */
2581 +function mxchat_render_knowledge_page_scripts() {
2582 + ?>
2583 + <script>
2584 + (function() {
2585 + 'use strict';
2586 +
2587 + // Wait for DOM
2588 + document.addEventListener('DOMContentLoaded', function() {
2589 + // Tab navigation + mobile menu live in the shared shell script
2590 + // (js/admin-sidebar.js), enqueued for this page by plan c192a4.
2591 + initImportOptions();
2592 + initCustomPostTypesToggle();
2593 + // Sitemap detection is now initialized when user clicks the Sitemap Import option
2594 + });
2595 +
1927 2596 function initImportOptions() {
1928 2597 const importBoxes = document.querySelectorAll('.mxchat-import-box');
1929 2598 const urlInputArea = document.getElementById('mxchat-url-input-area');
1930 2599 const contentInputArea = document.getElementById('mxchat-content-input-area');
@@ -2035,9 +2704,14 @@
2035 2704
2036 2705 if (!modal) return;
2037 2706
2038 2707 var currentEntry = {};
2708 + var loadedContent = '';
2039 2709
2710 + var EDIT_I18N = {
2711 + discard: <?php echo wp_json_encode( __('Discard your unsaved changes to this entry?', 'mxchat') ); ?>
2712 + };
2713 +
2040 2714 // Bind edit buttons (delegated)
2041 2715 document.addEventListener('click', function(e) {
2042 2716 var btn = e.target.closest('.mxchat-edit-entry-btn');
2043 2717 if (btn) {
@@ -2045,13 +2719,16 @@
2045 2719 openEditModal(btn);
2046 2720 }
2047 2721 });
2048 2722
2049 - // Close handlers
2050 - if (closeBtn) closeBtn.addEventListener('click', closeModal);
2051 - if (cancelBtn) cancelBtn.addEventListener('click', closeModal);
2052 - modal.addEventListener('click', function(e) {
2053 - if (e.target === modal) closeModal();
2723 + // Close handlers. The overlay deliberately has no click-to-close: a click
2724 + // fires on the common ancestor of its mousedown and mouseup, so releasing
2725 + // a text-selection drag past the dialog edge targets the overlay and would
2726 + // discard the edit. Closing is explicit only, and confirms when dirty.
2727 + if (closeBtn) closeBtn.addEventListener('click', requestClose);
2728 + if (cancelBtn) cancelBtn.addEventListener('click', requestClose);
2729 + document.addEventListener('keydown', function(e) {
2730 + if (e.key === 'Escape' && modal.classList.contains('active')) requestClose();
2054 2731 });
2055 2732
2056 2733 // Character count
2057 2734 if (textarea) {
@@ -2073,8 +2750,9 @@
2073 2750 };
2074 2751
2075 2752 // Reset state
2076 2753 textarea.value = '';
2754 + loadedContent = '';
2077 2755 textarea.placeholder = 'Loading content...';
2078 2756 textarea.disabled = true;
2079 2757 saveBtn.disabled = true;
2080 2758 notice.textContent = '';
@@ -2105,8 +2783,9 @@
2105 2783 .then(function(r) { return r.json(); })
2106 2784 .then(function(resp) {
2107 2785 if (resp.success) {
2108 2786 textarea.value = resp.data.content || '';
2787 + loadedContent = textarea.value;
2109 2788 textarea.disabled = false;
2110 2789 textarea.placeholder = 'Edit your content here...';
2111 2790 saveBtn.disabled = false;
2112 2791 currentEntry.contentType = resp.data.content_type || 'content';
@@ -2161,13 +2840,23 @@
2161 2840 if (saveSpin) saveSpin.style.display = 'none';
2162 2841 saveBtn.disabled = false;
2163 2842
2164 2843 if (resp.success) {
2165 - showNotice('Saved successfully!', 'success');
2166 - setTimeout(function() {
2167 - closeModal();
2168 - window.location.reload();
2169 - }, 1000);
2844 + if (currentEntry.dataSource === 'pinecone') {
2845 + // Pinecone list reads are eventually consistent — the reloaded
2846 + // table may show the old text for up to ~60 seconds.
2847 + showNotice('Saved! Pinecone changes may take up to 60 seconds to appear — use "Refresh Entries" if the list still shows the old text.', 'success');
2848 + setTimeout(function() {
2849 + closeModal();
2850 + window.location.reload();
2851 + }, 2500);
2852 + } else {
2853 + showNotice('Saved successfully!', 'success');
2854 + setTimeout(function() {
2855 + closeModal();
2856 + window.location.reload();
2857 + }, 1000);
2858 + }
2170 2859 } else {
2171 2860 showNotice((resp.data && resp.data.message) || 'Save failed.', 'error');
2172 2861 }
2173 2862 })
@@ -2176,8 +2865,17 @@
2176 2865 if (saveSpin) saveSpin.style.display = 'none';
2177 2866 saveBtn.disabled = false;
2178 2867 showNotice('Network error: ' + err.message, 'error');
2179 2868 });
2869 + }
2870 +
2871 + function isDirty() {
2872 + return !textarea.disabled && textarea.value !== loadedContent;
2873 + }
2874 +
2875 + function requestClose() {
2876 + if (isDirty() && !window.confirm(EDIT_I18N.discard)) return;
2877 + closeModal();
2180 2878 }
2181 2879
2182 2880 function closeModal() {
2183 2881 modal.classList.remove('active');