PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.6
MxChat – AI Chatbot & Content Generation for WordPress v2.4.6
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 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-integrator.php +1083 -5448 3.2.82.4.6 View file →
@@ -10,244 +10,11 @@
10 10 private $fallbackResponse;
11 11 private $productCardHtml;
12 12 private $word_handler;
13 13 private $last_similarity_analysis = null;
14 - private $current_valid_urls = [];
15 - private $last_vectorstore_error = null;
16 - private $is_streaming = false; // ADDED: Track if current request is streaming
17 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
18 14
19 -/**
20 - * Setup streaming headers - call this right before actually streaming
21 - * This delays header setup to allow actions/forms to return JSON responses
22 - */
23 -/**
24 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
25 - *
26 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
27 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
28 - * specific "overloaded" / "rate limit" body string. Returns immediately on
29 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
30 - *
31 - * Drop-in replacement for wp_remote_post — returns the same shape
32 - * (WP_Error or response array) so the caller's existing error-handling
33 - * code path is unchanged.
34 - *
35 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
36 - * paths (the *_response_openai / *_response_claude / etc functions).
37 - * For the *_stream variants, the cURL initial-connect happens inside a
38 - * read-chunks loop — retrying there safely (without re-emitting partial
39 - * stream chunks to the client) is a separate problem. Streaming paths
40 - * are NOT wrapped in this build; tracked as a follow-on.
41 - *
42 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
43 - * (default true). When false, behavior is identical to plain wp_remote_post.
44 - */
45 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
46 - $opts = is_array($this->options ?? null) ? $this->options : array();
47 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
48 - (string) $opts['auto_retry_on_transient_error'] !== '0';
49 15
50 - if (!$enabled) {
51 - return wp_remote_post($url, $args);
52 - }
53 -
54 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
55 - $last_response = null;
56 -
57 - foreach ($backoffs as $i => $delay_ms) {
58 - if ($delay_ms > 0) {
59 - usleep($delay_ms * 1000);
60 - }
61 - $response = wp_remote_post($url, $args);
62 - $last_response = $response;
63 -
64 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
65 - return $response;
66 - }
67 -
68 - if (defined('WP_DEBUG') && WP_DEBUG) {
69 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
70 - : (int) wp_remote_retrieve_response_code($response);
71 - error_log(sprintf(
72 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
73 - $provider_hint ?: 'unknown',
74 - $i + 1,
75 - $code_for_log,
76 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
77 - ));
78 - }
79 - }
80 -
81 - return $last_response;
82 -}
83 -
84 16 /**
85 - * Returns true if a wp_remote_post response represents a TRANSIENT
86 - * provider error worth retrying. Conservative — only retries on signals
87 - * that are very likely to clear within a few seconds.
88 - *
89 - * Transient signals:
90 - * - WP_Error with timeout / connection / dns / ssl
91 - * - HTTP 429, 502, 503, 504
92 - * - Provider-specific overload bodies (gemini "overloaded", openai
93 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
94 - *
95 - * NOT transient (return false — fail-fast):
96 - * - 200/2xx (success)
97 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
98 - * budget; the user needs to fix something)
99 - * - Any other 4xx (assume permanent unless explicitly listed above)
100 - * - 5xx other than the four listed above (e.g. 500 generic server error
101 - * is often a malformed request on our side, not a transient outage)
102 - */
103 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
104 - if (is_wp_error($response)) {
105 - $code = $response->get_error_code();
106 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
107 - || stripos((string) $response->get_error_message(), 'timed out') !== false
108 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
109 - }
110 -
111 - $status = (int) wp_remote_retrieve_response_code($response);
112 - if (in_array($status, array(429, 502, 503, 504), true)) {
113 - return true;
114 - }
115 - if ($status >= 200 && $status < 300) {
116 - return false;
117 - }
118 - // Permanent 4xx that should fail fast — even with no body.
119 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
120 - return false;
121 - }
122 -
123 - // Provider-specific body inspection for the cases where the upstream
124 - // returns 200 with an error envelope (gemini does this for overload).
125 - $body = (string) wp_remote_retrieve_body($response);
126 - if ($body === '') {
127 - return false;
128 - }
129 - $lower = strtolower($body);
130 - $hint = strtolower((string) $provider_hint);
131 -
132 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
133 - || strpos($lower, 'high demand') !== false
134 - || strpos($lower, 'model is overloaded') !== false)) {
135 - return true;
136 - }
137 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
138 - || strpos($lower, '"type":"server_error"') !== false
139 - || strpos($lower, '"code":"server_error"') !== false)) {
140 - return true;
141 - }
142 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
143 - || strpos($lower, 'overloaded_error') !== false)) {
144 - return true;
145 - }
146 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
147 - return true;
148 - }
149 -
150 - return false;
151 -}
152 -
153 -/**
154 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
155 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
156 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
157 - * collect status separately from a plain wp_remote_post array shape, so the
158 - * non-streaming helper above can't be called directly. This delegate keeps
159 - * the classification rules identical across both paths.
160 - */
161 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
162 - if ($curl_errno) {
163 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
164 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
165 - return in_array($curl_errno, array(
166 - CURLE_OPERATION_TIMEDOUT,
167 - CURLE_COULDNT_CONNECT,
168 - CURLE_COULDNT_RESOLVE_HOST,
169 - CURLE_SSL_CONNECT_ERROR,
170 - CURLE_GOT_NOTHING,
171 - CURLE_SEND_ERROR,
172 - CURLE_RECV_ERROR,
173 - ), true);
174 - }
175 -
176 - $status = (int) $http_code;
177 - if (in_array($status, array(429, 502, 503, 504), true)) {
178 - return true;
179 - }
180 - if ($status >= 200 && $status < 300) {
181 - return false;
182 - }
183 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
184 - return false;
185 - }
186 -
187 - $body = (string) $body;
188 - if ($body === '') {
189 - return false;
190 - }
191 - $lower = strtolower($body);
192 - $hint = strtolower((string) $provider_hint);
193 -
194 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
195 - || strpos($lower, 'high demand') !== false
196 - || strpos($lower, 'model is overloaded') !== false)) {
197 - return true;
198 - }
199 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
200 - || strpos($lower, '"type":"server_error"') !== false
201 - || strpos($lower, '"code":"server_error"') !== false)) {
202 - return true;
203 - }
204 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
205 - || strpos($lower, 'overloaded_error') !== false)) {
206 - return true;
207 - }
208 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
209 - return true;
210 - }
211 -
212 - return false;
213 -}
214 -
215 -/**
216 - * Whether transient-error auto-retry is enabled in admin settings.
217 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
218 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
219 - */
220 -private function mxchat_retry_enabled() {
221 - $opts = is_array($this->options ?? null) ? $this->options : array();
222 - return !isset($opts['auto_retry_on_transient_error']) ||
223 - (string) $opts['auto_retry_on_transient_error'] !== '0';
224 -}
225 -
226 -private function setup_streaming_headers() {
227 - if ($this->streaming_headers_sent || headers_sent()) {
228 - return false;
229 - }
230 -
231 - // Disable output buffering
232 - while (ob_get_level()) {
233 - ob_end_flush();
234 - }
235 -
236 - // Set headers for SSE
237 - header('Content-Type: text/event-stream');
238 - header('Cache-Control: no-cache');
239 - header('Connection: keep-alive');
240 - header('X-Accel-Buffering: no');
241 -
242 - ob_implicit_flush(true);
243 - flush();
244 -
245 - $this->streaming_headers_sent = true;
246 - return true;
247 -}
248 -
249 -/**
250 17 * Class constructor
251 18 */
252 19 public function __construct() {
253 20 $this->options = get_option('mxchat_options');
@@ -314,28 +81,13 @@
314 81 // Add chat mode checking actions
315 82 add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
316 83 add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
317 84
318 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
319 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
320 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
321 -
322 - // Auto-email transcript action
323 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
324 -
325 85 add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
326 86
327 87
328 88 }
329 89
330 -/**
331 - * Return a fresh nonce so cached pages can replace the stale one.
332 - */
333 -public function mxchat_refresh_nonce() {
334 - nocache_headers();
335 - wp_send_json_success(array('nonce' => wp_create_nonce('mxchat_chat_nonce')));
336 -}
337 -
338 90 // In your core plugin's check_actions_for_addons method:
339 91 public function check_actions_for_addons($default, $message, $user_id, $session_id) {
340 92 //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
341 93
@@ -358,22 +110,8 @@
358 110 wp_die();
359 111 }
360 112
361 113 $session_id = sanitize_text_field($_POST['session_id']);
362 -
363 - // SECURITY FIX: Verify session ownership before retrieving data
364 - // If IP/user changed, signal frontend to reset session instead of blocking
365 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
366 -
367 - // Check if this session has an owner recorded
368 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
369 -
370 - // Update session owner if it changed (e.g. IP changed due to network switch)
371 - // The session ID itself is the authentication — if the client has it, they own it
372 - if (!$session_owner || $session_owner !== $current_user_identifier) {
373 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
374 - }
375 -
376 114 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
377 115 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
378 116
379 117 if (empty($history)) {
@@ -390,25 +128,11 @@
390 128 'chat_mode' => $chat_mode
391 129 ]);
392 130 wp_die();
393 131 }
394 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
132 +
133 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
395 134 $history = get_option("mxchat_history_{$session_id}", []);
396 -
397 - // Check persistence setting - when OFF, only include messages from current page load
398 - $options = get_option('mxchat_options', []);
399 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
400 -
401 - // Filter history when persistence is OFF to match what the user sees
402 - if (!$persistence_enabled && $session_start_timestamp > 0) {
403 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
404 - // Include messages from this page load onwards
405 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
406 - });
407 - // Re-index array after filtering
408 - $history = array_values($history);
409 - }
410 -
411 135 $formatted_history = [];
412 136
413 137 // Adjusted for code-heavy conversations
414 138 $max_tokens = 120000; // Context window size
@@ -482,17 +206,8 @@
482 206
483 207 public function register_routes() {
484 208 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
485 209
486 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
487 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
488 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
489 - register_rest_route('mxchat/v1', '/nonce', [
490 - 'methods' => 'GET',
491 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
492 - 'permission_callback' => '__return_true',
493 - ]);
494 -
495 210 register_rest_route('mxchat/v1', '/stream', [
496 211 'methods' => 'GET',
497 212 'callback' => [$this, 'mxchat_stream_events'],
498 213 'permission_callback' => [$this, 'verify_chat_session'],
@@ -515,83 +230,12 @@
515 230 'callback' => [$this, 'handle_slack_messages'],
516 231 'permission_callback' => [$this, 'verify_slack_request'],
517 232 ]);
518 233
519 - // Telegram webhook endpoint
520 - register_rest_route('mxchat/v1', '/telegram-webhook', [
521 - 'methods' => 'POST',
522 - 'callback' => [$this, 'handle_telegram_webhook'],
523 - 'permission_callback' => [$this, 'verify_telegram_request'],
524 - ]);
525 -
526 234 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
527 235 }
528 236
529 237 /**
530 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
531 - * caches it for the session and includes it on every chat-send / stream-send /
532 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
533 - * we eliminate the entire class of "first-message Access denied" failures that
534 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
535 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
536 - * lives in the HTML body.
537 - *
538 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
539 - * client browser can't be used to flood the nonce-issuance path.
540 - *
541 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
542 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
543 - * backwards-compat window so cached pages still in users' browsers don't break
544 - * mid-session.
545 - *
546 - * @since 3.2.7
547 - */
548 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
549 - $ip = '';
550 - if (!empty($_SERVER['REMOTE_ADDR'])) {
551 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
552 - }
553 - if ($ip !== '') {
554 - // Best-effort rate limit. WP transients with sub-second TTL are racy
555 - // (parallel bursts can squeak through before set_transient completes);
556 - // we use 2s to make the gate slightly more reliable. Real production
557 - // rate-limiting at sub-second granularity needs Redis or DB row locks
558 - // — out of scope for this endpoint, which is already cheap.
559 - $key = 'mxchat_nonce_rl_' . md5($ip);
560 - if (get_transient($key)) {
561 - return new WP_REST_Response(array(
562 - 'error' => 'rate_limited',
563 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
564 - ), 429);
565 - }
566 - set_transient($key, 1, 2);
567 - }
568 -
569 - return new WP_REST_Response(array(
570 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
571 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
572 - ), 200);
573 -}
574 -
575 -/**
576 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
577 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
578 - * action (inline-localized in older cached HTML). The legacy acceptance is
579 - * a 30-day backwards-compat window — to be removed in a follow-up release
580 - * after 2026-06-27.
581 - *
582 - * @param string $posted_nonce
583 - * @return bool
584 - */
585 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
586 - if (!is_string($posted_nonce) || $posted_nonce === '') {
587 - return false;
588 - }
589 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
590 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
591 -}
592 -
593 -/**
594 238 * Verify valid chat session
595 239 */
596 240 public function verify_chat_session($request) {
597 241 $session_id = $request->get_param('session_id');
@@ -627,11 +271,10 @@
627 271 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
628 272 return false;
629 273 }
630 274
631 - // Get raw request body from the WP_REST_Request object
632 - // (php://input may already be consumed by WordPress at this point)
633 - $request_body = $request->get_body();
275 + // Get raw request body
276 + $request_body = file_get_contents('php://input');
634 277
635 278 // Create the signature base string
636 279 $sig_basestring = "v0:{$timestamp}:{$request_body}";
637 280
@@ -640,43 +283,8 @@
640 283
641 284 // Compare signatures
642 285 return hash_equals($my_signature, $slack_signature);
643 286 }
644 -
645 -/**
646 - * Verify request is coming from Telegram.
647 - *
648 - * @param WP_REST_Request $request
649 - * @return bool True if valid, false otherwise.
650 - */
651 -public function verify_telegram_request($request) {
652 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
653 -
654 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
655 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
656 -
657 - if (empty($secret_token)) {
658 - // If no secret is configured, allow the request (for initial setup)
659 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
660 - return true;
661 - }
662 -
663 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
664 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
665 -
666 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
667 -
668 - if (empty($request_token)) {
669 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
670 - return false;
671 - }
672 -
673 - // Timing-safe comparison
674 - $result = hash_equals($secret_token, $request_token);
675 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
676 - return $result;
677 -}
678 -
679 287 public function mxchat_stream_events(WP_REST_Request $request) {
680 288 header('Content-Type: text/event-stream');
681 289 header('Cache-Control: no-cache');
682 290 header('Connection: keep-alive');
@@ -710,9 +318,9 @@
710 318
711 319
712 320
713 321
714 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
322 +private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null) {
715 323 global $wpdb;
716 324 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
717 325 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
718 326
@@ -730,20 +338,8 @@
730 338 //error_log("[DEBUG] This is a NEW session - first message");
731 339 }
732 340 }
733 341
734 - // SECURITY FIX: Set session ownership for new sessions
735 - if ($is_new_session && $role === 'user') {
736 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
737 - $session_owner_key = "mxchat_session_owner_{$session_id}";
738 -
739 - // Only set ownership if not already set
740 - if (!get_option($session_owner_key)) {
741 - update_option($session_owner_key, $current_user_identifier, 'no');
742 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
743 - }
744 - }
745 -
746 342 // 1) Extract agent name if present
747 343 $agent_name = '';
748 344 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
749 345 $agent_name = $matches[1];
@@ -886,17 +482,9 @@
886 482 $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
887 483 }
888 484 }
889 485 }
890 -
891 - // Add RAG context if provided (for bot messages)
892 - if ($rag_context !== null && $role === 'bot') {
893 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
894 - if ($rag_context_column_exists) {
895 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
896 - }
897 - }
898 -
486 +
899 487 $wpdb->insert($table_name, $insert_data);
900 488 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
901 489
902 490 // 9) Send notification email if this is the first user message in a new session
@@ -907,17 +495,11 @@
907 495 'ip' => $_SERVER['REMOTE_ADDR']
908 496 ));
909 497 }
910 498
911 - // 10) Schedule delayed transcript email if enabled and message is from user
912 - if ($wpdb->insert_id && $role === 'user') {
913 - $this->schedule_delayed_transcript_email($session_id);
914 - }
915 -
916 499 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
917 500 return $message_id;
918 501 }
919 -
920 502 private function send_new_chat_notification($session_id, $user_info = array()) {
921 503 $options = get_option('mxchat_transcripts_options');
922 504
923 505 // Check if notifications are enabled
@@ -960,202 +542,14 @@
960 542 // Send email
961 543 return wp_mail($to, $subject, $message);
962 544 }
963 545
964 -/**
965 - * Schedule delayed transcript email for a session
966 - * Reschedules if a new user message is received
967 - */
968 -private function schedule_delayed_transcript_email($session_id) {
969 - $options = get_option('mxchat_transcripts_options');
970 -
971 - // Check if auto-email is enabled
972 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
973 - return;
974 - }
975 -
976 - // Get notification email
977 - $email = !empty($options['mxchat_notification_email']) ?
978 - $options['mxchat_notification_email'] :
979 - get_option('admin_email');
980 -
981 - if (!is_email($email)) {
982 - return;
983 - }
984 -
985 - // Get delay in minutes (default 30)
986 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
987 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
988 -
989 - // Clear any existing scheduled event for this session
990 - $hook = 'mxchat_send_delayed_transcript';
991 - $args = array($session_id);
992 - $timestamp = wp_next_scheduled($hook, $args);
993 -
994 - if ($timestamp) {
995 - wp_unschedule_event($timestamp, $hook, $args);
996 - }
997 -
998 - // Schedule new event
999 - $schedule_time = time() + ($delay_minutes * 60);
1000 - wp_schedule_single_event($schedule_time, $hook, $args);
1001 -}
1002 -
1003 -/**
1004 - * Check if chat messages contain contact information (email or phone number)
1005 - *
1006 - * @param array $messages Array of message objects with 'message' property
1007 - * @param object|null $session_data Session data object with user_email property
1008 - * @return bool True if contact info found, false otherwise
1009 - */
1010 -private function chat_contains_contact_info($messages, $session_data = null) {
1011 - // Check if session already has a stored email
1012 - if ($session_data && !empty($session_data->user_email)) {
1013 - return true;
1014 - }
1015 -
1016 - // Email regex pattern
1017 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1018 -
1019 - // Phone number patterns (covers various formats including international, WhatsApp style)
1020 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1021 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1022 -
1023 - // Only check user messages (not assistant responses)
1024 - foreach ($messages as $msg) {
1025 - if ($msg->role !== 'user') {
1026 - continue;
1027 - }
1028 -
1029 - $message_text = $msg->message;
1030 -
1031 - // Check for email
1032 - if (preg_match($email_pattern, $message_text)) {
1033 - return true;
1034 - }
1035 -
1036 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1037 - if (preg_match($phone_pattern, $message_text, $matches)) {
1038 - // Count actual digits to avoid matching short numbers
1039 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1040 - if (strlen($digits_only) >= 7) {
1041 - return true;
1042 - }
1043 - }
1044 - }
1045 -
1046 - return false;
1047 -}
1048 -
1049 -/**
1050 - * Send the delayed transcript email with .txt attachment
1051 - */
1052 -public function mxchat_send_delayed_transcript($session_id) {
1053 - global $wpdb;
1054 -
1055 - $options = get_option('mxchat_transcripts_options');
1056 -
1057 - // Get notification email
1058 - $to = !empty($options['mxchat_notification_email']) ?
1059 - $options['mxchat_notification_email'] :
1060 - get_option('admin_email');
1061 -
1062 - if (!is_email($to)) {
1063 - return false;
1064 - }
1065 -
1066 - // Get all messages for this session
1067 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1068 - $messages = $wpdb->get_results($wpdb->prepare(
1069 - "SELECT role, message, timestamp FROM {$table_name}
1070 - WHERE session_id = %s
1071 - ORDER BY timestamp ASC",
1072 - $session_id
1073 - ));
1074 -
1075 - if (empty($messages)) {
1076 - return false;
1077 - }
1078 -
1079 - // Get session metadata
1080 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1081 - $session_data = $wpdb->get_row($wpdb->prepare(
1082 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1083 - $session_id
1084 - ));
1085 -
1086 - // Check if contact info is required and if it's present
1087 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1088 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1089 - // Contact info required but not found - skip sending
1090 - return false;
1091 - }
1092 -
1093 - // Build transcript content
1094 - $transcript_content = "Chat Transcript\n";
1095 - $transcript_content .= "================\n\n";
1096 - $transcript_content .= "Session ID: " . $session_id . "\n";
1097 -
1098 - if ($session_data) {
1099 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1100 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1101 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1102 - }
1103 -
1104 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1105 -
1106 - // Add messages
1107 - foreach ($messages as $msg) {
1108 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1109 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1110 - $transcript_content .= $msg->message . "\n\n";
1111 - }
1112 -
1113 - // Create temporary file for attachment using WP_Filesystem
1114 - $upload_dir = wp_upload_dir();
1115 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1116 - global $wp_filesystem;
1117 - if (empty($wp_filesystem)) {
1118 - require_once ABSPATH . 'wp-admin/includes/file.php';
1119 - WP_Filesystem();
1120 - }
1121 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1122 -
1123 - // Prepare email
1124 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1125 -
1126 - $message = "Please find attached the full chat transcript.\n\n";
1127 - $message .= "Session ID: {$session_id}\n";
1128 -
1129 - if ($session_data) {
1130 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1131 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1132 - }
1133 -
1134 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1135 -
1136 - // Send email with attachment
1137 - $attachments = array($temp_file);
1138 - $result = wp_mail($to, $subject, $message, '', $attachments);
1139 -
1140 - // Clean up temporary file
1141 - if (file_exists($temp_file)) {
1142 - unlink($temp_file);
1143 - }
1144 -
1145 - return $result;
1146 -}
1147 -
1148 -
1149 -
1150 546 public function mxchat_handle_save_email_and_response() {
1151 547 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1152 548 //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1153 549
1154 - nocache_headers();
1155 -
1156 550 // Validate nonce
1157 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
551 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1158 552 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1159 553 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1160 554 wp_die();
1161 555 }
@@ -1165,9 +559,9 @@
1165 559 $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1166 560
1167 561 //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
1168 562
1169 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
563 + if (empty($session_id) || empty($email)) {
1170 564 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1171 565 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1172 566 wp_die();
1173 567 }
@@ -1184,15 +578,15 @@
1184 578 }
1185 579
1186 580 // 1) Always store email in wp_options
1187 581 $email_option_key = "mxchat_email_{$session_id}";
1188 - update_option($email_option_key, $email, 'no');
582 + update_option($email_option_key, $email);
1189 583 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1190 584
1191 585 // Store name in wp_options if provided
1192 586 if (!empty($name)) {
1193 587 $name_option_key = "mxchat_name_{$session_id}";
1194 - update_option($name_option_key, $name, 'no');
588 + update_option($name_option_key, $name);
1195 589 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1196 590 }
1197 591
1198 592 // 2) (Optional) Also store in DB if a row already exists
@@ -1236,17 +630,15 @@
1236 630
1237 631 public function mxchat_check_email_provided() {
1238 632 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1239 633
1240 - nocache_headers();
1241 -
1242 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
634 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1243 635 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1244 636 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1245 637 }
1246 638
1247 639 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1248 - if (empty($session_id) || $session_id === 'null') {
640 + if (empty($session_id)) {
1249 641 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1250 642 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1251 643 }
1252 644
@@ -1303,42 +695,15 @@
1303 695 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1304 696 }
1305 697 }
1306 698
1307 -/**
1308 - * Send error response in appropriate format based on streaming mode
1309 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1310 - *
1311 - * @param string $error_message The error message to display
1312 - * @param string $error_code Optional error code for debugging
1313 - */
1314 -private function send_error_response($error_message, $error_code = 'api_error') {
1315 - if ($this->is_streaming) {
1316 - echo "data: " . json_encode([
1317 - 'error' => true,
1318 - 'error_message' => $error_message,
1319 - 'error_code' => $error_code,
1320 - 'text' => $error_message,
1321 - 'message' => $error_message
1322 - ]) . "\n\n";
1323 - echo "data: [DONE]\n\n";
1324 - flush();
1325 - } else {
1326 - wp_send_json_error([
1327 - 'error_message' => $error_message,
1328 - 'error_code' => $error_code
1329 - ]);
1330 - }
1331 - wp_die();
1332 -}
1333 -
1334 699 public function mxchat_handle_chat_request() {
1335 700 global $wpdb;
1336 701
1337 702 // Debug: Log incoming bot_id
1338 703 $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1339 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1340 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
704 + error_log("=== MXCHAT DEBUG: Starting chat request ===");
705 + error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1341 706
1342 707 // Get bot-specific options
1343 708 $bot_options = $this->get_bot_options($bot_id);
1344 709 $current_options = !empty($bot_options) ? $bot_options : $this->options;
@@ -1343,19 +708,29 @@
1343 708 $bot_options = $this->get_bot_options($bot_id);
1344 709 $current_options = !empty($bot_options) ? $bot_options : $this->options;
1345 710
1346 711 // Check if this is a streaming request
1347 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1348 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1349 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1350 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
712 + $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
713 + isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on';
714 +
715 + // Set streaming headers if needed
716 + if ($is_streaming) {
717 + // Disable output buffering
718 + while (ob_get_level()) {
719 + ob_end_flush(); // Changed from ob_end_clean()
720 + }
721 +
722 + // Set headers for SSE
723 + header('Content-Type: text/event-stream');
724 + header('Cache-Control: no-cache');
725 + header('Connection: keep-alive');
726 + header('X-Accel-Buffering: no');
727 +
728 + // Add these new lines:
729 + ob_implicit_flush(true);
730 + flush();
731 + }
1351 732
1352 - // ADDED: Store streaming state in class property for use in private methods
1353 - $this->is_streaming = $is_streaming;
1354 -
1355 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1356 - // This allows actions/forms to return JSON responses without header conflicts
1357 -
1358 733 // Check if MX Chat Moderation is active
1359 734 if (class_exists('MX_Chat_Moderation')) {
1360 735 // Get user email and IP
1361 736 $user_email = '';
@@ -1420,31 +795,13 @@
1420 795
1421 796 // Rest of your existing code...
1422 797 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1423 798
1424 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1425 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1426 - // the frontend FormData.append() to stringify a null session_id into the literal
1427 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1428 - // ghost sessions that group every visitor's first message under one row.
1429 - if ($session_id === 'null' || $session_id === 'undefined') {
1430 - $session_id = '';
1431 - }
1432 -
1433 799 if (empty($session_id)) {
1434 800 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1435 801 wp_die();
1436 802 }
1437 803
1438 - // Update session owner if it changed (e.g. IP changed due to network switch)
1439 - // The session ID itself is the authentication — if the client has it, they own it
1440 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1441 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1442 -
1443 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1444 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1445 - }
1446 -
1447 804 // Validate and sanitize the incoming message
1448 805 if (empty($_POST['message'])) {
1449 806 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1450 807 wp_die();
@@ -1820,8 +1177,38 @@
1820 1177 }
1821 1178 }
1822 1179 }
1823 1180
1181 + // Check if there's an active recommendation flow session
1182 + $flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
1183 + if (!empty($flow_state) && isset($flow_state['flow_id'])) {
1184 + // Create a dummy intent object that matches the original intent
1185 + $dummy_intent = new stdClass();
1186 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
1187 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
1188 +
1189 + // Call the recommendation flow handler directly
1190 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
1191 +
1192 + // If the handler returned a response, send it
1193 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
1194 + // Save the bot's response to the chat history
1195 + if (!empty($response_data['text'])) {
1196 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
1197 + }
1198 + if (!empty($response_data['html'])) {
1199 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
1200 + }
1201 +
1202 + if ($testing_data !== null) {
1203 + $response_data['testing_data'] = $testing_data;
1204 + }
1205 +
1206 + // Send the response
1207 + wp_send_json($response_data);
1208 + wp_die();
1209 + }
1210 + }
1824 1211
1825 1212 // Step 2: Detect intent and handle intent-based responses
1826 1213 $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1827 1214
@@ -1840,46 +1227,58 @@
1840 1227 'text' => $intent_result['text'] ?? '',
1841 1228 'html' => $intent_result['html'] ?? '',
1842 1229 'session_id' => $session_id
1843 1230 ];
1844 -
1845 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1846 - if (isset($intent_result['chat_mode'])) {
1847 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1848 - }
1849 -
1231 +
1850 1232 if ($testing_data !== null) {
1851 1233 $response_data['testing_data'] = $testing_data;
1852 1234 }
1853 -
1235 +
1236 + // Clear streaming headers if they were set
1237 + if ($is_streaming) {
1238 + header_remove('Content-Type');
1239 + header_remove('Cache-Control');
1240 + header_remove('Connection');
1241 + header_remove('X-Accel-Buffering');
1242 + header('Content-Type: application/json');
1243 + }
1244 +
1854 1245 wp_send_json($response_data);
1855 1246 wp_die();
1856 1247 } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1857 1248 // Intent returned true and set fallbackResponse
1858 -
1859 - // SAVE TO TRANSCRIPT
1249 +
1250 + // SAVE TO TRANSCRIPT FIRST
1860 1251 if (!empty($this->fallbackResponse['text'])) {
1861 1252 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1862 1253 }
1863 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1864 1254 if (!empty($this->fallbackResponse['html'])) {
1865 1255 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1866 1256 }
1867 -
1257 +
1868 1258 $response_data = [
1869 1259 'text' => $this->fallbackResponse['text'] ?? '',
1870 1260 'html' => $this->fallbackResponse['html'] ?? '',
1871 1261 'session_id' => $session_id
1872 1262 ];
1873 -
1263 +
1874 1264 if (isset($this->fallbackResponse['chat_mode'])) {
1875 1265 $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1876 1266 }
1877 -
1267 +
1878 1268 if ($testing_data !== null) {
1879 1269 $response_data['testing_data'] = $testing_data;
1880 1270 }
1881 -
1271 +
1272 + // Clear streaming headers if they were set
1273 + if ($is_streaming) {
1274 + header_remove('Content-Type');
1275 + header_remove('Cache-Control');
1276 + header_remove('Connection');
1277 + header_remove('X-Accel-Buffering');
1278 + header('Content-Type: application/json');
1279 + }
1280 +
1882 1281 wp_send_json($response_data);
1883 1282 wp_die();
1884 1283 }
1885 1284 }
@@ -1884,13 +1283,11 @@
1884 1283 }
1885 1284 }
1886 1285
1887 1286 // If we get here, no intent matched OR the intent didn't provide a usable response
1888 -
1287 +
1889 1288 // Step 4: Generate AI response
1890 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1891 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
1892 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
1289 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
1893 1290 $this->mxchat_increment_chat_count();
1894 1291
1895 1292 // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1896 1293 $api_key = $current_options['api_key'] ?? $this->options['api_key'];
@@ -1899,50 +1296,22 @@
1899 1296 // Check if the embedding generation returned an error
1900 1297 if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1901 1298 $error_message = $user_message_embedding['error'];
1902 1299 $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
1903 -
1904 - // FIXED: Send error in appropriate format based on streaming mode
1905 - if ($is_streaming) {
1906 - echo "data: " . json_encode([
1907 - 'error' => true,
1908 - 'error_message' => $error_message,
1909 - 'error_code' => $error_code,
1910 - 'text' => $error_message,
1911 - 'message' => $error_message
1912 - ]) . "\n\n";
1913 - echo "data: [DONE]\n\n";
1914 - flush();
1915 - } else {
1916 - wp_send_json_error([
1917 - 'error_message' => $error_message,
1918 - 'error_code' => $error_code
1919 - ]);
1920 - }
1300 +
1301 + wp_send_json_error([
1302 + 'error_message' => $error_message,
1303 + 'error_code' => $error_code
1304 + ]);
1921 1305 wp_die();
1922 1306 }
1923 -
1307 +
1924 1308 // Check if the embedding is valid
1925 1309 if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
1926 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
1927 -
1928 - // FIXED: Send error in appropriate format based on streaming mode
1929 - if ($is_streaming) {
1930 - echo "data: " . json_encode([
1931 - 'error' => true,
1932 - 'error_message' => $error_message,
1933 - 'error_code' => 'invalid_embedding',
1934 - 'text' => $error_message,
1935 - 'message' => $error_message
1936 - ]) . "\n\n";
1937 - echo "data: [DONE]\n\n";
1938 - flush();
1939 - } else {
1940 - wp_send_json_error([
1941 - 'error_message' => $error_message,
1942 - 'error_code' => 'invalid_embedding'
1943 - ]);
1944 - }
1310 + wp_send_json_error([
1311 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1312 + 'error_code' => 'invalid_embedding'
1313 + ]);
1945 1314 wp_die();
1946 1315 }
1947 1316
1948 1317 // Build context with both knowledge base and PDF content if available
@@ -1968,71 +1337,26 @@
1968 1337 $context_content .= "Page Content: " . $page_context['content'] . "\n";
1969 1338 $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
1970 1339 }
1971 1340
1972 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
1973 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
1974 -
1975 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
1976 - // Use fresh options to ensure we get the latest setting value
1977 - $fresh_options = get_option('mxchat_options', []);
1978 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
1341 + // Get relevant content from knowledge base - PASS BOT_ID
1342 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id);
1979 1343
1980 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
1981 - if ($citation_links_enabled && !empty($system_instructions)) {
1982 - preg_match_all(
1983 - '#\bhttps?://[^\s<>"\']+#i',
1984 - $system_instructions,
1985 - $system_instruction_urls
1986 - );
1987 -
1988 - if (!empty($system_instruction_urls[0])) {
1989 - // Merge with existing valid URLs
1990 - $this->current_valid_urls = array_merge(
1991 - $this->current_valid_urls,
1992 - $system_instruction_urls[0]
1993 - );
1994 - // Remove duplicates
1995 - $this->current_valid_urls = array_unique($this->current_valid_urls);
1996 -
1997 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
1998 - }
1344 + // ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
1345 + if ($testing_data !== null && $this->last_similarity_analysis !== null) {
1346 + // Update testing data with the REAL similarity analysis
1347 + $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
1348 + $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1349 + $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
1999 1350 }
1351 + // ===== END SIMILARITY DATA CAPTURE =====
2000 1352
2001 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2002 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2003 - // Update testing data with the REAL similarity analysis
2004 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2005 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2006 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2007 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2008 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2009 -}
2010 -// ===== END SIMILARITY DATA CAPTURE =====
2011 -
2012 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2013 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2014 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2015 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2016 -}
2017 -
2018 1353 if (!empty($relevant_content)) {
2019 1354 $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2020 1355 } else {
2021 1356 $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2022 1357 }
2023 -
2024 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2025 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2026 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2027 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2028 - foreach ($this->current_valid_urls as $url) {
2029 - $context_content .= "- " . $url . "\n";
2030 - }
2031 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2032 - $context_content .= "===== END APPROVED URLS =====\n\n";
2033 - }
2034 -
1358 +
2035 1359 // Check for and include PDF content
2036 1360 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2037 1361 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2038 1362 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2064,9 +1388,9 @@
2064 1388
2065 1389 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2066 1390
2067 1391 // Extract model from current options for bot-specific model support
2068 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
1392 + $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-4o';
2069 1393
2070 1394 $response = $this->mxchat_generate_response(
2071 1395 $context_content,
2072 1396 $current_options['api_key'] ?? $this->options['api_key'],
@@ -2073,14 +1397,13 @@
2073 1397 $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2074 1398 $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2075 1399 $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2076 1400 $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2077 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2078 1401 $conversation_history,
2079 1402 $is_streaming,
2080 1403 $session_id,
2081 1404 $testing_data,
2082 - $selected_model
1405 + $selected_model // ADD THIS LINE
2083 1406 );
2084 1407
2085 1408 // Handle streaming vs non-streaming responses
2086 1409 if ($is_streaming) {
@@ -2088,27 +1411,11 @@
2088 1411 if ($response === true) {
2089 1412 wp_die();
2090 1413 }
2091 1414 // If we get here, streaming fell back to regular response, continue
2092 - // But if there's an error, we need to send it as SSE format since headers are already set
2093 - if (is_array($response) && isset($response['error'])) {
2094 - $error_message = $response['error'];
2095 - $error_code = $response['error_code'] ?? 'api_error';
2096 - // Send error in SSE format that the client JS can handle
2097 - echo "data: " . json_encode([
2098 - 'error' => true,
2099 - 'error_message' => $error_message,
2100 - 'error_code' => $error_code,
2101 - 'text' => $error_message, // Also include as text for fallback handling
2102 - 'message' => $error_message
2103 - ]) . "\n\n";
2104 - echo "data: [DONE]\n\n";
2105 - flush();
2106 - wp_die();
2107 - }
2108 1415 }
2109 -
2110 - // Check if the response is an error array (non-streaming mode)
1416 +
1417 + // Check if the response is an error array
2111 1418 if (is_array($response) && isset($response['error'])) {
2112 1419 wp_send_json_error([
2113 1420 'error_message' => $response['error'],
2114 1421 'error_code' => $response['error_code'] ?? 'api_error'
@@ -2115,51 +1422,11 @@
2115 1422 ]);
2116 1423 wp_die();
2117 1424 }
2118 1425
2119 - // DEBUG: Check what we have
2120 - //error_log("=== BEFORE URL VALIDATION ===");
2121 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2122 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2123 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2124 -
2125 - // If we get here, the response is valid text - now validate URLs
2126 - if (!empty($this->current_valid_urls)) {
2127 - //error_log("CALLING validate_and_clean_urls");
2128 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2129 - } else {
2130 - //error_log("SKIPPING validation - current_valid_urls is empty");
2131 - }
2132 - // ===== END URL VALIDATION =====
1426 + // If we get here, the response is valid text
1427 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2133 1428
2134 - // Prepare RAG context data for storage (only include documents used for context)
2135 - $rag_context_for_storage = null;
2136 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2137 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2138 -
2139 - if ($has_rag_data || $has_action_data) {
2140 - $rag_context_for_storage = [];
2141 -
2142 - // Add RAG/source data if available
2143 - if ($has_rag_data) {
2144 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2145 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2146 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2147 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2148 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2149 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2150 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2151 - }
2152 -
2153 - // Add action analysis data if available
2154 - if ($has_action_data) {
2155 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2156 - }
2157 - }
2158 -
2159 - // Save the cleaned response with RAG context
2160 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2161 -
2162 1429 // Step 5: Save additional content if available
2163 1430 if (!empty($this->productCardHtml)) {
2164 1431 $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2165 1432 }
@@ -2168,13 +1435,8 @@
2168 1435 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2169 1436 }
2170 1437
2171 1438 // Step 6: Return the response
2172 - // DEBUG: Check if newlines exist in the response
2173 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2174 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2175 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2176 -
2177 1439 $response_data = [
2178 1440 'text' => $response,
2179 1441 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2180 1442 'session_id' => $session_id
@@ -2179,18 +1441,8 @@
2179 1441 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2180 1442 'session_id' => $session_id
2181 1443 ];
2182 1444
2183 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2184 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2185 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2186 - }
2187 -
2188 - // Also pass it as a top-level field so JS can show a better error message to admins
2189 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2190 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2191 - }
2192 -
2193 1445 // Always add testing data for admins (no toggle needed)
2194 1446 if ($testing_data !== null) {
2195 1447 $response_data['testing_data'] = $testing_data;
2196 1448 }
@@ -2198,8 +1450,9 @@
2198 1450 wp_send_json($response_data);
2199 1451 wp_die();
2200 1452 }
2201 1453
1454 +
2202 1455 /**
2203 1456 * Get bot-specific options for multi-bot functionality
2204 1457 * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2205 1458 */
@@ -2204,12 +1457,12 @@
2204 1457 * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2205 1458 */
2206 1459 // Also debug the bot options retrieval
2207 1460 private function get_bot_options($bot_id = 'default') {
2208 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
1461 + error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2209 1462
2210 1463 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2211 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
1464 + error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2212 1465 return array();
2213 1466 }
2214 1467
2215 1468 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
@@ -2214,11 +1467,11 @@
2214 1467
2215 1468 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2216 1469
2217 1470 if (!empty($bot_options)) {
2218 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
1471 + error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2219 1472 if (isset($bot_options['similarity_threshold'])) {
2220 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
1473 + error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2221 1474 }
2222 1475 }
2223 1476
2224 1477 return is_array($bot_options) ? $bot_options : array();
@@ -2229,13 +1482,13 @@
2229 1482 * Used in the knowledge retrieval functions
2230 1483 */
2231 1484 // Also add debugging to your get_bot_pinecone_config function
2232 1485 private function get_bot_pinecone_config($bot_id = 'default') {
2233 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
1486 + error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2234 1487
2235 1488 // If default bot or multi-bot add-on not active, use default Pinecone config
2236 1489 if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2237 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
1490 + error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2238 1491 $addon_options = get_option('mxchat_pinecone_addon_options', array());
2239 1492 $config = array(
2240 1493 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2241 1494 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
@@ -2241,24 +1494,24 @@
2241 1494 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2242 1495 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2243 1496 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2244 1497 );
2245 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
1498 + error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2246 1499 return $config;
2247 1500 }
2248 1501
2249 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
1502 + error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2250 1503
2251 1504 // Hook for multi-bot add-on to provide bot-specific Pinecone config
2252 1505 $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2253 1506
2254 1507 if (!empty($bot_pinecone_config)) {
2255 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2256 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2257 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2258 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
1508 + error_log("MXCHAT DEBUG: Got bot-specific config from filter");
1509 + error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
1510 + error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
1511 + error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2259 1512 } else {
2260 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
1513 + error_log("MXCHAT DEBUG: Filter returned empty config!");
2261 1514 }
2262 1515
2263 1516 return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2264 1517 }
@@ -2268,63 +1521,35 @@
2268 1521 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2269 1522 global $wpdb;
2270 1523 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2271 1524
2272 - // Get the current bot_id
1525 + // NEW: Get the current bot_id
2273 1526 $current_bot_id = $this->get_current_bot_id($session_id);
2274 1527
2275 1528 // Generate the user embedding
2276 1529 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2277 -
1530 +
2278 1531 // Check if embedding generation returned an error
2279 1532 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2280 1533 $error_message = $user_embedding['error'];
2281 1534 $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2282 -
2283 - // FIXED: Send error in appropriate format based on streaming mode
2284 - if ($this->is_streaming) {
2285 - echo "data: " . json_encode([
2286 - 'error' => true,
2287 - 'error_message' => $error_message,
2288 - 'error_code' => $error_code,
2289 - 'text' => $error_message,
2290 - 'message' => $error_message
2291 - ]) . "\n\n";
2292 - echo "data: [DONE]\n\n";
2293 - flush();
2294 - } else {
2295 - wp_send_json_error([
2296 - 'error_message' => $error_message,
2297 - 'error_code' => $error_code
2298 - ]);
2299 - }
1535 +
1536 + wp_send_json_error([
1537 + 'error_message' => $error_message,
1538 + 'error_code' => $error_code
1539 + ]);
2300 1540 wp_die();
2301 1541 }
2302 -
1542 +
2303 1543 // Check if embedding is valid
2304 1544 if (!is_array($user_embedding) || empty($user_embedding)) {
2305 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2306 -
2307 - // FIXED: Send error in appropriate format based on streaming mode
2308 - if ($this->is_streaming) {
2309 - echo "data: " . json_encode([
2310 - 'error' => true,
2311 - 'error_message' => $error_message,
2312 - 'error_code' => 'invalid_embedding',
2313 - 'text' => $error_message,
2314 - 'message' => $error_message
2315 - ]) . "\n\n";
2316 - echo "data: [DONE]\n\n";
2317 - flush();
2318 - } else {
2319 - wp_send_json_error([
2320 - 'error_message' => $error_message,
2321 - 'error_code' => 'invalid_embedding'
2322 - ]);
2323 - }
1545 + wp_send_json_error([
1546 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1547 + 'error_code' => 'invalid_embedding'
1548 + ]);
2324 1549 wp_die();
2325 1550 }
2326 -
1551 +
2327 1552 // Fetch intents from the database
2328 1553 $table_name = $wpdb->prefix . 'mxchat_intents';
2329 1554 if ($chat_mode === 'agent') {
2330 1555 $query = $wpdb->prepare(
@@ -2334,29 +1559,19 @@
2334 1559 $intents = $wpdb->get_results($query);
2335 1560 } else {
2336 1561 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2337 1562 }
2338 -
1563 +
2339 1564 if (empty($intents)) {
2340 1565 return false;
2341 1566 }
2342 -
2343 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2344 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2345 - $phrases_by_intent = [];
2346 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2347 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2348 - foreach ($all_phrases as $p) {
2349 - $phrases_by_intent[$p->intent_id][] = $p;
2350 - }
2351 - }
2352 -
1567 +
2353 1568 $highest_similarity = -INF;
2354 1569 $matched_intent = null;
2355 -
1570 +
2356 1571 // Array to store action analysis for testing panel
2357 1572 $action_analysis = [];
2358 -
1573 +
2359 1574 foreach ($intents as $intent) {
2360 1575 // Additional check for enabled state
2361 1576 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2362 1577 if (!$is_enabled) {
@@ -2361,56 +1576,26 @@
2361 1576 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2362 1577 if (!$is_enabled) {
2363 1578 continue;
2364 1579 }
2365 -
2366 - // Check if this action is enabled for the current bot
1580 +
1581 + // NEW: Check if this action is enabled for the current bot
2367 1582 if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2368 1583 continue;
2369 1584 }
2370 -
2371 - $best_similarity = -INF;
2372 - $matched_phrase_text = '';
2373 -
2374 - // Check legacy embedding vector (existing behavior)
1585 +
2375 1586 $intent_embedding_serialized = $intent->embedding_vector;
2376 1587 $intent_embedding = $intent_embedding_serialized
2377 1588 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2378 1589 : null;
2379 -
2380 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2381 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2382 - if ($legacy_similarity > $best_similarity) {
2383 - $best_similarity = $legacy_similarity;
2384 - $matched_phrase_text = 'legacy';
2385 - }
2386 - }
2387 -
2388 - // Check individual phrase vectors
2389 - if (isset($phrases_by_intent[$intent->id])) {
2390 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2391 - $phrase_embedding = $phrase_row->embedding_vector
2392 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2393 - : null;
2394 - if (!is_array($phrase_embedding)) {
2395 - continue;
2396 - }
2397 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2398 - if ($phrase_similarity > $best_similarity) {
2399 - $best_similarity = $phrase_similarity;
2400 - $matched_phrase_text = $phrase_row->phrase;
2401 - }
2402 - }
2403 - }
2404 -
2405 - // Skip if no valid embedding was found at all
2406 - if ($best_similarity === -INF) {
1590 +
1591 + if (!is_array($intent_embedding)) {
2407 1592 continue;
2408 1593 }
2409 -
2410 - $similarity = $best_similarity;
1594 +
1595 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2411 1596 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2412 -
1597 +
2413 1598 // Store action analysis data for testing panel
2414 1599 $action_analysis[] = [
2415 1600 'intent_label' => $intent->intent_label,
2416 1601 'callback_function' => $intent->callback_function,
@@ -2418,12 +1603,11 @@
2418 1603 'similarity_percentage' => round($similarity * 100, 2),
2419 1604 'threshold' => $intent_threshold,
2420 1605 'threshold_percentage' => round($intent_threshold * 100, 2),
2421 1606 'above_threshold' => $similarity >= $intent_threshold,
2422 - 'matched_phrase' => $matched_phrase_text,
2423 1607 'triggered' => false // Will be updated below if this intent is triggered
2424 1608 ];
2425 -
1609 +
2426 1610 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2427 1611 $highest_similarity = $similarity;
2428 1612 $matched_intent = $intent;
2429 1613 }
@@ -2494,22 +1678,16 @@
2494 1678 // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2495 1679 if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2496 1680 return true;
2497 1681 }
2498 -
1682 +
2499 1683 $enabled_bots = json_decode($intent->enabled_bots, true);
2500 -
1684 +
2501 1685 // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2502 1686 if (!is_array($enabled_bots) || empty($enabled_bots)) {
2503 1687 return true;
2504 1688 }
2505 -
2506 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2507 - // default-bot actions are testable from the admin panel
2508 - if ($bot_id === 'testing') {
2509 - $bot_id = 'default';
2510 - }
2511 -
1689 +
2512 1690 // Check if the current bot is in the enabled bots list
2513 1691 return in_array($bot_id, $enabled_bots);
2514 1692 }
2515 1693
@@ -2547,23 +1725,18 @@
2547 1725 }
2548 1726
2549 1727 public function mxchat_generate_image($message, $user_id, $session_id) {
2550 1728 //error_log("Starting image generation for message: " . $message);
2551 -
2552 - // Prepare a prompt for OpenAI image generation
1729 +
1730 + // Prepare a prompt for DALL-E
2553 1731 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2554 -
2555 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2556 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2557 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2558 - $image_response = $this->mxchat_generate_custom_image($prompt);
2559 - } else {
2560 - // Use the existing OpenAI API key
2561 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2562 - // Call OpenAI GPT Image to generate an image
2563 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2564 - }
2565 1732
1733 + // Use the existing OpenAI API key
1734 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1735 +
1736 + // Call DALL-E to generate an image
1737 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1738 +
2566 1739 // Check if the response contains an image URL
2567 1740 if (isset($image_response['imageUrl'])) {
2568 1741 $image_url = esc_url_raw($image_response['imageUrl']);
2569 1742
@@ -2606,103 +1779,24 @@
2606 1779 // Return the response directly instead of relying on the property
2607 1780 return $this->fallbackResponse;
2608 1781 }
2609 1782 }
2610 -
2611 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2612 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2613 -
2614 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2615 - if (empty($gemini_api_key)) {
2616 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2617 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2618 - return ['text' => $response_text, 'html' => '', 'images' => []];
2619 - }
2620 -
2621 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2622 -
2623 - if (isset($image_response['imageUrl'])) {
2624 - $image_url = esc_url_raw($image_response['imageUrl']);
2625 -
2626 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2627 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2628 -
2629 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2630 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2631 -
2632 - $this->fallbackResponse = [
2633 - 'text' => $response_text,
2634 - 'html' => $response_html,
2635 - 'images' => [$image_url]
2636 - ];
2637 -
2638 - return $this->fallbackResponse;
2639 - } else {
2640 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2641 -
2642 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2643 -
2644 - $this->fallbackResponse = [
2645 - 'text' => $response_text,
2646 - 'html' => '',
2647 - 'images' => []
2648 - ];
2649 -
2650 - return $this->fallbackResponse;
2651 - }
2652 -}
2653 -
2654 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2655 - $extension = ($mime_type === 'image/jpeg') ? 'jpg' : 'png';
2656 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2657 - $decoded = base64_decode($base64_data);
2658 -
2659 - if ($decoded === false) {
2660 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2661 - }
2662 -
2663 - $upload = wp_upload_bits($filename, null, $decoded);
2664 -
2665 - if (!empty($upload['error'])) {
2666 - return new \WP_Error('upload_failed', $upload['error']);
2667 - }
2668 -
2669 - $attach_id = wp_insert_attachment([
2670 - 'post_mime_type' => $mime_type,
2671 - 'post_title' => $prefix,
2672 - 'post_content' => '',
2673 - 'post_status' => 'inherit',
2674 - ], $upload['file']);
2675 -
2676 - if (is_wp_error($attach_id)) {
2677 - return $attach_id;
2678 - }
2679 -
2680 - require_once ABSPATH . 'wp-admin/includes/image.php';
2681 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2682 - wp_update_attachment_metadata($attach_id, $metadata);
2683 -
2684 - return esc_url_raw(wp_get_attachment_url($attach_id));
2685 -}
2686 -
2687 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1783 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2688 1784 $api_url = 'https://api.openai.com/v1/images/generations';
2689 1785 $body = json_encode([
2690 - 'prompt' => sanitize_text_field($prompt),
2691 - 'n' => 1,
2692 - 'size' => '1024x1024',
2693 - 'quality' => 'medium',
2694 - 'output_format' => 'png',
2695 - 'model' => sanitize_text_field($model),
1786 + 'prompt' => sanitize_text_field($prompt),
1787 + 'n' => 1,
1788 + 'size' => '1024x1024',
1789 + 'model' => sanitize_text_field($model),
2696 1790 ]);
2697 1791
2698 1792 $args = [
2699 - 'body' => $body,
1793 + 'body' => $body,
2700 1794 'headers' => [
2701 - 'Content-Type' => 'application/json',
1795 + 'Content-Type' => 'application/json',
2702 1796 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2703 1797 ],
2704 - 'method' => 'POST',
1798 + 'method' => 'POST',
2705 1799 'timeout' => absint($timeout),
2706 1800 ];
2707 1801
2708 1802 $response = wp_remote_post($api_url, $args);
@@ -2707,114 +1801,23 @@
2707 1801
2708 1802 $response = wp_remote_post($api_url, $args);
2709 1803
2710 1804 if (is_wp_error($response)) {
1805 + //error_log("DALL-E request failed: " . $response->get_error_message());
2711 1806 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2712 1807 }
2713 1808
2714 1809 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2715 1810
2716 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2717 - if ($b64) {
2718 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2719 - if (is_wp_error($saved_url)) {
2720 - return ['error' => $saved_url->get_error_message()];
2721 - }
2722 - return ['imageUrl' => $saved_url];
1811 + if (isset($response_body['data'][0]['url'])) {
1812 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2723 1813 } else {
1814 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2724 1815 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2725 1816 }
2726 1817 }
2727 1818
2728 1819 /**
2729 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2730 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2731 - */
2732 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2733 - $cfg = $this->mxchat_resolve_custom_provider();
2734 - if (empty($cfg['base_url'])) {
2735 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2736 - }
2737 - $url = $cfg['base_url'] . '/images/generations';
2738 - if (!empty($cfg['api_version'])) {
2739 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2740 - }
2741 - $body = wp_json_encode([
2742 - 'prompt' => sanitize_text_field($prompt),
2743 - 'n' => 1,
2744 - 'size' => '1024x1024',
2745 - 'model' => $cfg['model'],
2746 - ]);
2747 - $response = wp_remote_post($url, [
2748 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2749 - 'body' => $body,
2750 - 'method' => 'POST',
2751 - 'timeout' => absint($timeout),
2752 - ]);
2753 - if (is_wp_error($response)) {
2754 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2755 - }
2756 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2757 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2758 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2759 - if ($b64) {
2760 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2761 - if (is_wp_error($saved)) {
2762 - return ['error' => $saved->get_error_message()];
2763 - }
2764 - return ['imageUrl' => $saved];
2765 - }
2766 - $remote_url = $resp['data'][0]['url'] ?? null;
2767 - if ($remote_url) {
2768 - return ['imageUrl' => esc_url_raw($remote_url)];
2769 - }
2770 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2771 - return ['error' => esc_html($err_msg)];
2772 -}
2773 -
2774 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2775 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2776 -
2777 - $body = json_encode([
2778 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2779 - 'parameters' => [
2780 - 'sampleCount' => 1,
2781 - 'aspectRatio' => '1:1',
2782 - ],
2783 - ]);
2784 -
2785 - $args = [
2786 - 'body' => $body,
2787 - 'headers' => [
2788 - 'Content-Type' => 'application/json',
2789 - 'x-goog-api-key' => sanitize_text_field($api_key),
2790 - ],
2791 - 'method' => 'POST',
2792 - 'timeout' => absint($timeout),
2793 - ];
2794 -
2795 - $response = wp_remote_post($api_url, $args);
2796 -
2797 - if (is_wp_error($response)) {
2798 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2799 - }
2800 -
2801 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2802 -
2803 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2804 - if ($b64) {
2805 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2806 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2807 - if (is_wp_error($saved_url)) {
2808 - return ['error' => $saved_url->get_error_message()];
2809 - }
2810 - return ['imageUrl' => $saved_url];
2811 - } else {
2812 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2813 - }
2814 -}
2815 -
2816 -/**
2817 1820 * Handle web search requests.
2818 1821 *
2819 1822 * Sends the refined search query to the Brave Search API and uses the
2820 1823 * results to generate a conversational response with the AI model.
@@ -2862,10 +1865,10 @@
2862 1865 $transient_key = 'mxchat_search_' . md5($refined_search_query);
2863 1866 $results = get_transient($transient_key);
2864 1867
2865 1868 if (false === $results) {
2866 - // SECURITY FIX: Changed to wp_safe_remote_get
2867 - $response = wp_safe_remote_get(
1869 + // Fetch new results from the Brave Search API
1870 + $response = wp_remote_get(
2868 1871 $api_url,
2869 1872 array(
2870 1873 'headers' => array(
2871 1874 'Accept' => 'application/json',
@@ -3004,10 +2007,9 @@
3004 2007 ],
3005 2008 'timeout' => 10,
3006 2009 ];
3007 2010
3008 - // SECURITY FIX: Changed to wp_safe_remote_get
3009 - $response = wp_safe_remote_get($api_url, $args);
2011 + $response = wp_remote_get($api_url, $args);
3010 2012
3011 2013 if (is_wp_error($response)) {
3012 2014 return array(
3013 2015 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
@@ -3077,22 +2079,17 @@
3077 2079 * @return string The refined search query
3078 2080 */
3079 2081 public function mxchat_interpret_search_query($user_query) {
3080 2082 $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3081 -
2083 +
3082 2084 // Get options and determine the selected model
3083 2085 $options = $this->options ?? get_option('mxchat_options');
3084 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
3085 -
3086 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3087 - if ($selected_model === 'custom-provider') {
3088 - return $this->interpret_query_with_custom($user_query, $system_prompt);
3089 - }
3090 -
2086 + $selected_model = isset($options['model']) ? $options['model'] : 'gpt-4o';
2087 +
3091 2088 // Extract model prefix to determine the provider
3092 2089 $model_parts = explode('-', $selected_model);
3093 2090 $provider = strtolower($model_parts[0]);
3094 -
2091 +
3095 2092 // Determine which API key to use based on the provider
3096 2093 switch ($provider) {
3097 2094 case 'gemini':
3098 2095 $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
@@ -3133,60 +2130,11 @@
3133 2130 }
3134 2131 }
3135 2132
3136 2133 /**
3137 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3138 - * Uses the same base URL + auth scheme as the chat dispatcher.
3139 - */
3140 -private function interpret_query_with_custom($user_query, $system_prompt) {
3141 - $cfg = $this->mxchat_resolve_custom_provider();
3142 - if (empty($cfg['base_url'])) {
3143 - return sanitize_text_field($user_query);
3144 - }
3145 - $args = [
3146 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3147 - 'body' => wp_json_encode([
3148 - 'model' => $cfg['model'],
3149 - 'messages' => [
3150 - ['role' => 'system', 'content' => $system_prompt],
3151 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3152 - ],
3153 - 'temperature' => 0.2,
3154 - 'max_tokens' => 20,
3155 - ]),
3156 - 'method' => 'POST',
3157 - 'timeout' => 15,
3158 - ];
3159 - $response = wp_remote_post($cfg['chat_url'], $args);
3160 - if (is_wp_error($response)) {
3161 - return sanitize_text_field($user_query);
3162 - }
3163 - $body = json_decode(wp_remote_retrieve_body($response), true);
3164 - return isset($body['choices'][0]['message']['content'])
3165 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3166 - : sanitize_text_field($user_query);
3167 -}
3168 -
3169 -/**
3170 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3171 - * into the assoc-array form wp_remote_post expects.
3172 - */
3173 -private function mxchat_custom_provider_assoc_headers($cfg) {
3174 - $headers = ['Content-Type' => 'application/json'];
3175 - if (!empty($cfg['api_key'])) {
3176 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3177 - $headers['api-key'] = $cfg['api_key'];
3178 - } else {
3179 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3180 - }
3181 - }
3182 - return $headers;
3183 -}
3184 -
3185 -/**
3186 2134 * Interpret query using OpenAI models
3187 2135 */
3188 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
2136 +private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-4o') {
3189 2137 $url = 'https://api.openai.com/v1/chat/completions';
3190 2138 $args = [
3191 2139 'headers' => [
3192 2140 'Authorization' => 'Bearer ' . $api_key,
@@ -3216,34 +2164,13 @@
3216 2164 : sanitize_text_field($user_query);
3217 2165 }
3218 2166
3219 2167 /**
3220 - * Anthropic deprecated temperature/top_p/top_k starting with Opus 4.7 —
3221 - * add new reasoning-Opus model ids here. (We don't send top_p/top_k in any
3222 - * Claude body, so the list only needs to gate temperature stripping.)
3223 - */
3224 -private function mxchat_claude_omits_temperature($model) {
3225 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8');
3226 - return in_array($model, $no_temp, true);
3227 -}
3228 -
3229 -/**
3230 2168 * Interpret query using Claude models
3231 2169 */
3232 2170 private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3233 2171 $url = 'https://api.anthropic.com/v1/messages';
3234 -
3235 - $payload = [
3236 - 'model' => $model,
3237 - 'system' => $system_prompt,
3238 - 'messages' => [
3239 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3240 - ],
3241 - 'max_tokens' => 20,
3242 - 'temperature' => 0.2,
3243 - ];
3244 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3245 -
2172 +
3246 2173 $args = [
3247 2174 'headers' => [
3248 2175 'Content-Type' => 'application/json',
3249 2176 'x-api-key' => $api_key,
@@ -3248,9 +2175,17 @@
3248 2175 'Content-Type' => 'application/json',
3249 2176 'x-api-key' => $api_key,
3250 2177 'anthropic-version' => '2023-06-01',
3251 2178 ],
3252 - 'body' => wp_json_encode($payload),
2179 + 'body' => wp_json_encode([
2180 + 'model' => $model,
2181 + 'system' => $system_prompt,
2182 + 'messages' => [
2183 + ['role' => 'user', 'content' => sanitize_text_field($user_query)]
2184 + ],
2185 + 'max_tokens' => 20,
2186 + 'temperature' => 0.2,
2187 + ]),
3253 2188 'method' => 'POST',
3254 2189 'timeout' => 15,
3255 2190 ];
3256 2191
@@ -3270,16 +2205,13 @@
3270 2205 /**
3271 2206 * Interpret query using Gemini models
3272 2207 */
3273 2208 private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3274 - if ($model === 'gemini-3-pro-preview') {
3275 - $model = 'gemini-3.1-pro-preview';
3276 - }
3277 - // Use v1beta for preview models, v1 for stable models
3278 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3279 -
3280 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
2209 + // Strip "gemini-" prefix for the API
2210 + $model_version = str_replace('gemini-', '', $model);
3281 2211
2212 + $url = "https://generativelanguage.googleapis.com/v1/models/$model_version:generateContent?key=" . urlencode($api_key);
2213 +
3282 2214 $args = [
3283 2215 'headers' => [
3284 2216 'Content-Type' => 'application/json',
3285 2217 ],
@@ -3475,9 +2407,9 @@
3475 2407 }
3476 2408
3477 2409
3478 2410 /**
3479 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
2411 + * Enhanced fetch_and_split_pdf_pages with detailed debugging
3480 2412 */
3481 2413 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3482 2414 // CLEAR DEBUG LOGGING
3483 2415 //error_log("=== MXCHAT PDF PROCESSING START ===");
@@ -3532,19 +2464,10 @@
3532 2464 // (I'll include the key parts with debug logging)
3533 2465
3534 2466 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3535 2467 //error_log("Downloading PDF from URL...");
3536 -
3537 - // SECURITY FIX: Validate URL before processing
3538 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3539 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3540 - return false;
3541 - }
3542 -
3543 2468 $temp_file = wp_tempnam($pdf_source);
3544 -
3545 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3546 - $response = wp_safe_remote_get($pdf_source, [
2469 + $response = wp_remote_get($pdf_source, [
3547 2470 'timeout' => 60,
3548 2471 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3549 2472 ]);
3550 2473
@@ -3553,14 +2476,9 @@
3553 2476 //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3554 2477 return false;
3555 2478 }
3556 2479
3557 - global $wp_filesystem;
3558 - if (empty($wp_filesystem)) {
3559 - require_once ABSPATH . 'wp-admin/includes/file.php';
3560 - WP_Filesystem();
3561 - }
3562 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
2480 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
3563 2481 //error_log("✅ PDF downloaded successfully");
3564 2482 } else {
3565 2483 $temp_file = $pdf_source;
3566 2484 //error_log("Using local PDF file: " . $temp_file);
@@ -3567,9 +2485,8 @@
3567 2485 }
3568 2486
3569 2487 // Parse PDF
3570 2488 //error_log("Parsing PDF with basic parser...");
3571 - mxchat_load_pdf_parser();
3572 2489 $parser = new \Smalot\PdfParser\Parser();
3573 2490 $pdf = $parser->parseFile($temp_file);
3574 2491 $pages = $pdf->getPages();
3575 2492
@@ -3632,33 +2549,8 @@
3632 2549 return false;
3633 2550 }
3634 2551 }
3635 2552
3636 -
3637 -/**
3638 - * Validate PDF URL for security
3639 - * Prevents SSRF attacks by blocking dangerous URLs
3640 - */
3641 -
3642 -private function mxchat_is_safe_pdf_url($url) {
3643 - // Use WordPress core function for comprehensive validation
3644 - // This blocks localhost, private IPs, and reserved IP ranges
3645 - $validated_url = wp_http_validate_url($url);
3646 -
3647 - if ($validated_url === false) {
3648 - return false;
3649 - }
3650 -
3651 - // Additional check: only allow HTTP/HTTPS schemes
3652 - $parsed = parse_url($url);
3653 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3654 - return false;
3655 - }
3656 -
3657 - return true;
3658 -}
3659 -
3660 -
3661 2553 private function mxchat_clean_text($text) {
3662 2554 // Remove excessive whitespace
3663 2555 $text = preg_replace('/\s+/', ' ', $text);
3664 2556
@@ -3697,14 +2589,11 @@
3697 2589 }
3698 2590
3699 2591 return [];
3700 2592 }
3701 -
3702 -
2593 +// Add this to your class
3703 2594 public function handle_pdf_upload() {
3704 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3705 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3706 - }
2595 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3707 2596
3708 2597 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3709 2598 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3710 2599 return;
@@ -3709,29 +2598,12 @@
3709 2598 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3710 2599 return;
3711 2600 }
3712 2601
3713 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3714 - $options = get_option('mxchat_options', array());
3715 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3716 -
3717 - if ($show_pdf_button !== 'on') {
3718 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3719 - return;
3720 - }
3721 -
3722 2602 $file = $_FILES['pdf_file'];
3723 2603 $session_id = sanitize_text_field($_POST['session_id']);
3724 2604 $original_filename = sanitize_text_field($file['name']);
3725 2605
3726 - // Update session owner if it changed (e.g. IP changed due to network switch)
3727 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3728 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3729 -
3730 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3731 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3732 - }
3733 -
3734 2606 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3735 2607 if ($file_type['type'] !== 'application/pdf') {
3736 2608 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3737 2609 return;
@@ -3737,12 +2609,9 @@
3737 2609 return;
3738 2610 }
3739 2611
3740 2612 $upload_dir = wp_upload_dir();
3741 -
3742 - // SECURITY FIX: Generate random filename without exposing session_id
3743 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3744 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2613 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3745 2614 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3746 2615
3747 2616 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3748 2617 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3773,9 +2642,8 @@
3773 2642 return;
3774 2643 }
3775 2644
3776 2645 if (!empty($embeddings)) {
3777 - // Store the mapping between session and the random filename
3778 2646 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3779 2647 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3780 2648 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3781 2649 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3796,11 +2664,9 @@
3796 2664 wp_send_json_error($error_message);
3797 2665 return;
3798 2666 }
3799 2667 public function handle_pdf_remove() {
3800 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3801 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3802 - }
2668 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3803 2669
3804 2670 if (empty($_POST['session_id'])) {
3805 2671 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
3806 2672 wp_die();
@@ -3821,8 +2687,10 @@
3821 2687 wp_die();
3822 2688 }
3823 2689
3824 2690
2691 +
2692 +
3825 2693 function mxchat_fetch_new_messages() {
3826 2694 $session_id = sanitize_text_field($_POST['session_id']);
3827 2695 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3828 2696 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -3835,31 +2703,14 @@
3835 2703 }
3836 2704
3837 2705 $history = get_option("mxchat_history_{$session_id}", []);
3838 2706
3839 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
3840 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
3841 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
3842 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
3843 -
3844 2707 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
3845 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
3846 -
3847 2708 // If persistence is enabled, show all new messages
3848 2709 if ($persistence_enabled) {
3849 - $has_id = !empty($message['id']);
3850 - $is_agent = $message['role'] === 'agent';
3851 -
3852 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
3853 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
3854 - $is_newer = true;
3855 - } else {
3856 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
3857 - }
3858 -
3859 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
3860 -
3861 - return $has_id && $is_newer && $is_agent;
2710 + return !empty($message['id']) &&
2711 + strcmp($message['id'], $last_seen_id) > 0 &&
2712 + $message['role'] === 'agent';
3862 2713 }
3863 2714
3864 2715 // If persistence is disabled, only show messages after initial timestamp
3865 2716 return !empty($message['id']) &&
@@ -3866,16 +2717,12 @@
3866 2717 $message['role'] === 'agent' &&
3867 2718 $message['timestamp'] > $initial_timestamp;
3868 2719 });
3869 2720
3870 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2721 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
3871 2722
3872 - // Include current chat mode so frontend can detect agent→AI transitions
3873 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3874 -
3875 2723 wp_send_json_success([
3876 - 'new_messages' => array_values($new_messages),
3877 - 'chat_mode' => $chat_mode
2724 + 'new_messages' => array_values($new_messages)
3878 2725 ]);
3879 2726 wp_die();
3880 2727 }
3881 2728 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
@@ -4160,393 +3007,9 @@
4160 3007
4161 3008 //error_log("[DEBUG] Generated channel name: {$channel_name}");
4162 3009 return $channel_name;
4163 3010 }
4164 -
4165 -/**
4166 - * Telegram Live Agent Handover
4167 - * Creates a forum topic in the Telegram group and notifies agents
4168 - */
4169 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4170 - // Check if Telegram agents are available
4171 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4172 - if ($telegram_available !== 'on') {
4173 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4174 - $this->fallbackResponse = [
4175 - 'text' => $away_message,
4176 - 'html' => '',
4177 - 'images' => [],
4178 - 'chat_mode' => 'ai'
4179 - ];
4180 - wp_send_json([
4181 - 'text' => $away_message,
4182 - 'html' => '',
4183 - 'chat_mode' => 'ai',
4184 - 'session_id' => $session_id
4185 - ]);
4186 - wp_die();
4187 - }
4188 -
4189 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4190 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4191 -
4192 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
4193 - return false;
4194 - }
4195 -
4196 - // Check if topic already exists for this session
4197 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4198 -
4199 - if (empty($topic_id)) {
4200 - // Generate topic name
4201 - $topic_name = $this->generate_telegram_topic_name($session_id);
4202 -
4203 - // Random icon color (Telegram forum topic colors)
4204 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4205 - $icon_color = $icon_colors[array_rand($icon_colors)];
4206 -
4207 - // Create forum topic
4208 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4209 - 'headers' => ['Content-Type' => 'application/json'],
4210 - 'body' => json_encode([
4211 - 'chat_id' => $telegram_group_id,
4212 - 'name' => $topic_name,
4213 - 'icon_color' => $icon_color
4214 - ])
4215 - ]);
4216 -
4217 - if (!is_wp_error($response)) {
4218 - $response_body = wp_remote_retrieve_body($response);
4219 - $response_data = json_decode($response_body, true);
4220 -
4221 - if (isset($response_data['ok']) && $response_data['ok']) {
4222 - $topic_id = $response_data['result']['message_thread_id'];
4223 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4224 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4225 - }
4226 - }
4227 -
4228 - if (empty($topic_id)) {
4229 - return false; // Failed to create topic
4230 - }
4231 - }
4232 -
4233 - // Get recent chat history
4234 - $history = get_option("mxchat_history_{$session_id}", []);
4235 - $recent_history = array_slice($history, -5);
4236 -
4237 - // Format conversation context for Telegram (HTML format)
4238 - $conversation_context = "";
4239 - if (!empty($recent_history)) {
4240 - $conversation_context = "<b>Recent Conversation:</b>\n";
4241 - foreach ($recent_history as $hist_message) {
4242 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4243 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4244 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4245 - }
4246 - $conversation_context .= "\n";
4247 - }
4248 -
4249 - // Get user info
4250 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4251 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4252 -
4253 - // Update session mode
4254 - update_option("mxchat_mode_{$session_id}", 'agent');
4255 -
4256 - // Send initial message to topic
4257 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4258 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4259 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4260 - $topic_message .= "<b>User:</b> {$user_name}\n";
4261 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4262 -
4263 - if (!empty($conversation_context)) {
4264 - $topic_message .= $conversation_context;
4265 - }
4266 -
4267 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4268 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4269 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4270 -
4271 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4272 - 'headers' => ['Content-Type' => 'application/json'],
4273 - 'body' => json_encode([
4274 - 'chat_id' => $telegram_group_id,
4275 - 'message_thread_id' => $topic_id,
4276 - 'text' => $topic_message,
4277 - 'parse_mode' => 'HTML'
4278 - ])
4279 - ]);
4280 -
4281 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
4282 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4283 -
4284 - $this->fallbackResponse = [
4285 - 'text' => $success_message,
4286 - 'html' => '',
4287 - 'images' => [],
4288 - 'chat_mode' => 'agent'
4289 - ];
4290 -
4291 - wp_send_json([
4292 - 'success' => true,
4293 - 'text' => $success_message,
4294 - 'html' => '',
4295 - 'chat_mode' => 'agent',
4296 - 'session_id' => $session_id,
4297 - 'fallbackResponse' => $this->fallbackResponse
4298 - ]);
4299 - wp_die();
4300 -}
4301 -
4302 -/**
4303 - * Generate topic name for Telegram forum
4304 - */
4305 -private function generate_telegram_topic_name($session_id) {
4306 - $name = null;
4307 - $email = null;
4308 -
4309 - // Check logged in user
4310 - if (is_user_logged_in()) {
4311 - $current_user = wp_get_current_user();
4312 - if (!empty($current_user->display_name)) {
4313 - $name = $current_user->display_name;
4314 - }
4315 - if (!empty($current_user->user_email)) {
4316 - $email = $current_user->user_email;
4317 - }
4318 - }
4319 -
4320 - // Check session data
4321 - if (empty($name)) {
4322 - $name = get_option("mxchat_name_{$session_id}");
4323 - }
4324 - if (empty($email)) {
4325 - $email = get_option("mxchat_email_{$session_id}");
4326 - }
4327 -
4328 - // Generate topic name
4329 - $session_suffix = substr($session_id, -6);
4330 -
4331 - if (!empty($name)) {
4332 - // Clean name for topic (max 128 chars in Telegram)
4333 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4334 - $clean_name = trim($clean_name);
4335 - if (strlen($clean_name) > 50) {
4336 - $clean_name = substr($clean_name, 0, 50);
4337 - }
4338 - return "Chat - {$clean_name} ({$session_suffix})";
4339 - } elseif (!empty($email)) {
4340 - // Use email prefix
4341 - $email_prefix = explode('@', $email)[0];
4342 - if (strlen($email_prefix) > 30) {
4343 - $email_prefix = substr($email_prefix, 0, 30);
4344 - }
4345 - return "Chat - {$email_prefix} ({$session_suffix})";
4346 - }
4347 -
4348 - return "Chat - {$session_suffix}";
4349 -}
4350 -
4351 -/**
4352 - * Send user message to Telegram agent
4353 - */
4354 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4355 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4356 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4357 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4358 -
4359 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4360 - return false;
4361 - }
4362 -
4363 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4364 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4365 -
4366 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4367 - 'headers' => ['Content-Type' => 'application/json'],
4368 - 'body' => json_encode([
4369 - 'chat_id' => $group_id,
4370 - 'message_thread_id' => $topic_id,
4371 - 'text' => $user_message,
4372 - 'parse_mode' => 'HTML'
4373 - ])
4374 - ]);
4375 -
4376 - return !is_wp_error($response);
4377 -}
4378 -
4379 -/**
4380 - * Handle incoming Telegram webhook
4381 - */
4382 -public function handle_telegram_webhook(WP_REST_Request $request) {
4383 - $body = $request->get_body();
4384 - $data = json_decode($body, true);
4385 -
4386 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4387 -
4388 - // Handle message events from forum topics
4389 - if (isset($data['message'])) {
4390 - $message_data = $data['message'];
4391 -
4392 - // Skip if not from a forum topic
4393 - if (!isset($message_data['message_thread_id'])) {
4394 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4395 - return new WP_REST_Response(['ok' => true]);
4396 - }
4397 -
4398 - // Skip bot messages
4399 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4400 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4401 - return new WP_REST_Response(['ok' => true]);
4402 - }
4403 -
4404 - $chat_id = $message_data['chat']['id'] ?? '';
4405 - $topic_id = $message_data['message_thread_id'];
4406 - $message_text = $message_data['text'] ?? '';
4407 - $message_id = $message_data['message_id'] ?? '';
4408 - $from = $message_data['from'] ?? [];
4409 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4410 - if (empty($agent_name)) {
4411 - $agent_name = $from['username'] ?? 'Agent';
4412 - }
4413 -
4414 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4415 -
4416 - // Skip empty messages
4417 - if (empty($message_text)) {
4418 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4419 - return new WP_REST_Response(['ok' => true]);
4420 - }
4421 -
4422 - // Find session ID by topic ID - cast to string for comparison
4423 - global $wpdb;
4424 - $topic_id_str = strval($topic_id);
4425 - $session_option = $wpdb->get_var(
4426 - $wpdb->prepare(
4427 - "SELECT option_name FROM {$wpdb->options}
4428 - WHERE option_name LIKE %s
4429 - AND option_value = %s",
4430 - 'mxchat_telegram_topic_%',
4431 - $topic_id_str
4432 - )
4433 - );
4434 -
4435 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4436 -
4437 - if ($session_option) {
4438 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4439 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4440 -
4441 - // Verify the group ID matches
4442 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4443 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4444 -
4445 - if (strval($stored_group_id) != strval($chat_id)) {
4446 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4447 - return new WP_REST_Response(['ok' => true]);
4448 - }
4449 -
4450 - // Check for closure commands
4451 - $lower_text = strtolower(trim($message_text));
4452 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4453 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4454 - // End the live agent session
4455 - update_option("mxchat_mode_{$session_id}", 'ai');
4456 -
4457 - // Save disconnect message
4458 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4459 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4460 -
4461 - // Notify in Telegram
4462 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4463 - if (!empty($telegram_bot_token)) {
4464 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4465 - 'headers' => ['Content-Type' => 'application/json'],
4466 - 'body' => json_encode([
4467 - 'chat_id' => $chat_id,
4468 - 'message_thread_id' => $topic_id,
4469 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4470 - 'parse_mode' => 'HTML'
4471 - ])
4472 - ]);
4473 -
4474 - // Optionally close the topic
4475 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4476 - 'headers' => ['Content-Type' => 'application/json'],
4477 - 'body' => json_encode([
4478 - 'chat_id' => $chat_id,
4479 - 'message_thread_id' => $topic_id
4480 - ])
4481 - ]);
4482 - }
4483 -
4484 - return new WP_REST_Response(['ok' => true]);
4485 - }
4486 -
4487 - // Deduplicate messages
4488 - $message_key = md5($session_id . $message_id . $message_text);
4489 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4490 -
4491 - if (in_array($message_key, $processed_messages)) {
4492 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4493 - return new WP_REST_Response(['ok' => true]);
4494 - }
4495 -
4496 - $processed_messages[] = $message_key;
4497 - if (count($processed_messages) > 50) {
4498 - $processed_messages = array_slice($processed_messages, -50);
4499 - }
4500 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4501 -
4502 - // Save the agent message - format with agent name prefix for proper parsing
4503 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4504 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4505 -
4506 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4507 -
4508 - // Verify the message was saved to history
4509 - $history = get_option("mxchat_history_{$session_id}", []);
4510 - $last_message = end($history);
4511 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4512 -
4513 - // Send confirmation back to Telegram
4514 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4515 - if (!empty($telegram_bot_token)) {
4516 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4517 - if (!get_transient($confirm_key)) {
4518 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4519 - 'headers' => ['Content-Type' => 'application/json'],
4520 - 'body' => json_encode([
4521 - 'chat_id' => $chat_id,
4522 - 'message_thread_id' => $topic_id,
4523 - 'text' => "✅ <i>Message sent to user</i>",
4524 - 'parse_mode' => 'HTML',
4525 - 'reply_to_message_id' => $message_id
4526 - ])
4527 - ]);
4528 - set_transient($confirm_key, true, 300);
4529 - }
4530 - }
4531 - } else {
4532 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4533 - }
4534 - } else {
4535 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4536 - }
4537 -
4538 - return new WP_REST_Response(['ok' => true]);
4539 -}
4540 -
4541 3011 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4542 - // Check if this is a Telegram agent session
4543 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4544 - if (!empty($telegram_topic_id)) {
4545 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4546 - }
4547 -
4548 - // Otherwise, try Slack
4549 3012 $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4550 3013 $channel_id = get_option("mxchat_channel_{$session_id}", '');
4551 3014
4552 3015 if (empty($slack_bot_token) || empty($channel_id)) {
@@ -4772,33 +3235,33 @@
4772 3235
4773 3236 $channel_id = $event['channel'];
4774 3237 $message_text = $event['text'] ?? '';
4775 3238 $message_ts = $event['ts'] ?? '';
4776 -
3239 +
4777 3240 // Find session ID by looking for matching channel
4778 3241 global $wpdb;
4779 3242 $session_option = $wpdb->get_var(
4780 3243 $wpdb->prepare(
4781 - "SELECT option_name FROM {$wpdb->options}
4782 - WHERE option_name LIKE 'mxchat_channel_%'
3244 + "SELECT option_name FROM {$wpdb->options}
3245 + WHERE option_name LIKE 'mxchat_channel_%'
4783 3246 AND option_value = %s",
4784 3247 $channel_id
4785 3248 )
4786 3249 );
4787 -
3250 +
4788 3251 if ($session_option) {
4789 3252 $session_id = str_replace('mxchat_channel_', '', $session_option);
4790 -
3253 +
4791 3254 // Create a unique key for this specific message
4792 3255 $message_key = md5($session_id . $message_ts . $message_text);
4793 3256 $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4794 -
3257 +
4795 3258 // Check if we've already processed this exact message
4796 3259 if (in_array($message_key, $processed_messages)) {
4797 3260 //error_log("Duplicate message detected for session $session_id");
4798 3261 return new WP_REST_Response(['ok' => true]);
4799 3262 }
4800 -
3263 +
4801 3264 // Add to processed messages
4802 3265 $processed_messages[] = $message_key;
4803 3266 // Keep only last 50 messages per session
4804 3267 if (count($processed_messages) > 50) {
@@ -4804,46 +3267,14 @@
4804 3267 if (count($processed_messages) > 50) {
4805 3268 $processed_messages = array_slice($processed_messages, -50);
4806 3269 }
4807 3270 set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4808 -
4809 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4810 -
4811 - // Handle agent ending the chat — transfer back to AI
4812 - // Format: "!endchat" or "!endchat <custom message to user>"
4813 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
4814 - update_option("mxchat_mode_{$session_id}", 'ai');
4815 -
4816 - // Extract custom message after !endchat, or use empty string
4817 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
4818 -
4819 - // Send the agent's custom farewell message if provided
4820 - if (!empty($custom_message)) {
4821 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
4822 - }
4823 -
4824 - // Confirm in Slack channel
4825 - if (!empty($slack_bot_token)) {
4826 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4827 - 'headers' => [
4828 - 'Content-Type' => 'application/json',
4829 - 'Authorization' => 'Bearer ' . $slack_bot_token
4830 - ],
4831 - 'body' => json_encode([
4832 - 'channel' => $channel_id,
4833 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
4834 - 'mrkdwn' => true
4835 - ])
4836 - ]);
4837 - }
4838 -
4839 - return new WP_REST_Response(['ok' => true]);
4840 - }
4841 -
3271 +
4842 3272 // Save the agent message
4843 3273 $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4844 -
3274 +
4845 3275 // Send confirmation back to Slack (only once)
3276 + $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4846 3277 if (!empty($slack_bot_token)) {
4847 3278 // Use a transient to prevent duplicate confirmations
4848 3279 $confirm_key = 'mxchat_confirm_' . $message_key;
4849 3280 if (!get_transient($confirm_key)) {
@@ -4853,9 +3284,9 @@
4853 3284 'Authorization' => 'Bearer ' . $slack_bot_token
4854 3285 ],
4855 3286 'body' => json_encode([
4856 3287 'channel' => $channel_id,
4857 - 'text' => "✅ _Message sent to user_",
3288 + 'text' => "✅ _Message sent to user_",
4858 3289 'thread_ts' => $event['ts'] // Reply in thread
4859 3290 ])
4860 3291 ]);
4861 3292 // Set transient to prevent duplicate confirmations
@@ -4895,15 +3326,9 @@
4895 3326 try {
4896 3327 // Get options and selected model
4897 3328 $options = get_option('mxchat_options');
4898 3329 $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4899 -
4900 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
4901 - // Off by default so existing sites see byte-identical behavior.
4902 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
4903 - return $this->mxchat_generate_embedding_custom($text);
4904 - }
4905 -
3330 +
4906 3331 // Determine endpoint and API key based on model
4907 3332 if (strpos($selected_model, 'voyage') === 0) {
4908 3333 $endpoint = 'https://api.voyageai.com/v1/embeddings';
4909 3334 $api_key = $options['voyage_api_key'] ?? '';
@@ -5100,97 +3525,26 @@
5100 3525 }
5101 3526 }
5102 3527
5103 3528
5104 -/**
5105 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5106 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5107 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5108 - */
5109 -private function mxchat_generate_embedding_custom($text) {
5110 - if (empty($text)) {
5111 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5112 - }
5113 - $cfg = $this->mxchat_resolve_custom_provider();
5114 - if (empty($cfg['base_url'])) {
5115 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5116 - }
5117 -
5118 - $options = get_option('mxchat_options');
5119 - $embed_url = $cfg['base_url'] . '/embeddings';
5120 - if (!empty($cfg['api_version'])) {
5121 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5122 - }
5123 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5124 - ? trim((string) $options['custom_provider_embedding_model'])
5125 - : $cfg['model'];
5126 -
5127 - $response = wp_remote_post($embed_url, [
5128 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5129 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5130 - 'timeout' => 60,
5131 - ]);
5132 - if (is_wp_error($response)) {
5133 - return [
5134 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5135 - 'error_code' => 'embedding_custom_connection_error',
5136 - ];
5137 - }
5138 - $status = wp_remote_retrieve_response_code($response);
5139 - $body = json_decode(wp_remote_retrieve_body($response), true);
5140 - if ($status !== 200) {
5141 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5142 - return [
5143 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5144 - 'error_code' => 'embedding_custom_api_error',
5145 - 'status_code' => $status,
5146 - ];
5147 - }
5148 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5149 - return $body['data'][0]['embedding'];
5150 - }
5151 - return [
5152 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5153 - 'error_code' => 'embedding_custom_invalid_response',
5154 - ];
5155 -}
5156 -
5157 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5158 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5159 -
5160 - // Check for OpenAI Vector Store first (takes priority when enabled)
5161 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5162 -
5163 - if ($bot_vectorstore_config['use_vectorstore']) {
5164 - // Get current model to verify it's an OpenAI model
5165 - $bot_options = $this->get_bot_options($bot_id);
5166 - $mxchat_options = get_option('mxchat_options', array());
5167 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5168 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5169 -
5170 - if ($this->is_openai_chat_model($selected_model)) {
5171 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5172 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5173 - } else {
5174 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5175 - }
5176 - }
5177 -
3529 +private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default') {
3530 + error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
3531 +
5178 3532 // Get bot-specific Pinecone configuration
5179 3533 $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5180 -
3534 +
5181 3535 // Debug: Log the Pinecone configuration
5182 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5183 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5184 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5185 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5186 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5187 -
3536 + error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
3537 + error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
3538 + error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
3539 + error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
3540 + error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
3541 +
5188 3542 // Determine whether to use Pinecone based on bot configuration
5189 3543 $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
3544 +
3545 + error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5190 3546
5191 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5192 -
5193 3547 if ($use_pinecone) {
5194 3548 return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
5195 3549 } else {
5196 3550 return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
@@ -5199,483 +3553,233 @@
5199 3553
5200 3554 private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
5201 3555 global $wpdb;
5202 3556 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3557 + $cache_key = 'mxchat_system_prompt_embeddings_' . $bot_id; // Bot-specific cache key
3558 + $batch_size = 500;
3559 +
5203 3560 // Initialize similarity analysis storage
5204 3561 $this->last_similarity_analysis = [
5205 3562 'knowledge_base_type' => 'WordPress Database',
5206 - 'bot_id' => $bot_id,
3563 + 'bot_id' => $bot_id, // Track which bot is being used
5207 3564 'top_matches' => [],
5208 3565 'threshold_used' => 0,
5209 3566 'total_checked' => 0
5210 3567 ];
5211 3568
5212 - // NEW: Initialize valid URLs array
5213 - $valid_urls = [];
5214 -
5215 - // Get bot-specific options for similarity threshold
3569 + // Get bot-specific options for similarity threshold
5216 3570 $bot_options = $this->get_bot_options($bot_id);
5217 3571 $current_options = !empty($bot_options) ? $bot_options : $this->options;
5218 3572
5219 - // Get knowledge manager instance for role checking
5220 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3573 + // Retrieve embeddings from cache or database
3574 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3575 + if ($embeddings === false) {
3576 + // Cache miss - load embeddings from database WITH CONTENT and ROLE RESTRICTION for testing
3577 + $embeddings = [];
3578 + $offset = 0;
5221 3579
5222 - // Get base similarity threshold from bot options or default options
5223 - $similarity_threshold = isset($current_options['similarity_threshold'])
5224 - ? ((int) $current_options['similarity_threshold']) / 100
5225 - : 0.35;
5226 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3580 + do {
3581 + // Add bot_id filter if not default and if bot_metadata column exists
3582 + $bot_filter = '';
3583 + if ($bot_id !== 'default') {
3584 + // Check if bot_metadata column exists
3585 + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
3586 + if ($column_exists) {
3587 + $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
3588 + }
3589 + }
5227 3590
5228 - // Precompute bot_filter once, outside the streaming loop
5229 - $bot_filter = '';
5230 - if ($bot_id !== 'default') {
5231 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5232 - if ($column_exists) {
5233 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5234 - }
5235 - }
3591 + $query = $wpdb->prepare(
3592 + "SELECT id, embedding_vector, article_content, source_url, role_restriction
3593 + FROM {$system_prompt_table}
3594 + WHERE 1=1 {$bot_filter}
3595 + LIMIT %d OFFSET %d",
3596 + $batch_size,
3597 + $offset
3598 + );
5236 3599
5237 - // ===== STREAMING TOP-K PASS =====
5238 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5239 - // - top 10 by raw similarity (for the testing/debug display panel)
5240 - // - candidates above threshold with access (capped) for context assembly
5241 - // This bounds peak memory regardless of knowledge base size and avoids loading
5242 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5243 - $batch_size = 250;
5244 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5245 - $top_display = [];
5246 - $candidates = [];
5247 - $total_checked = 0;
5248 - $offset = 0;
3600 + $batch = $wpdb->get_results($query);
3601 + if (empty($batch)) {
3602 + break;
3603 + }
5249 3604
5250 - do {
5251 - $batch = $wpdb->get_results($wpdb->prepare(
5252 - "SELECT id, embedding_vector, source_url, role_restriction
5253 - FROM {$system_prompt_table}
5254 - WHERE 1=1 {$bot_filter}
5255 - LIMIT %d OFFSET %d",
5256 - $batch_size,
5257 - $offset
5258 - ));
3605 + $embeddings = array_merge($embeddings, $batch);
3606 + $offset += $batch_size;
3607 + unset($batch);
3608 + } while (true);
5259 3609
5260 - if (empty($batch)) {
5261 - break;
3610 + if (empty($embeddings)) {
3611 + return '';
5262 3612 }
3613 +
3614 + // Cache embeddings for future use (but note: this now includes content and role restrictions)
3615 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3616 + }
5263 3617
5264 - foreach ($batch as $row) {
5265 - $database_embedding = $row->embedding_vector
5266 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5267 - : null;
3618 + // Get knowledge manager instance for role checking
3619 + $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5268 3620
5269 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5270 - unset($database_embedding);
5271 - continue;
5272 - }
5273 -
3621 + // Get base similarity threshold from bot options or default options
3622 + $similarity_threshold = isset($current_options['similarity_threshold'])
3623 + ? ((int) $current_options['similarity_threshold']) / 100
3624 + : 0.35;
3625 +
3626 + $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3627 +
3628 + // Calculate similarities and build results array
3629 + $all_similarities = [];
3630 + $relevant_results = [];
3631 +
3632 + foreach ($embeddings as $embedding) {
3633 + $database_embedding = $embedding->embedding_vector
3634 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3635 + : null;
3636 +
3637 + if (is_array($database_embedding) && is_array($user_embedding)) {
5274 3638 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5275 - unset($database_embedding);
5276 -
5277 - $role_restriction = $row->role_restriction ?? 'public';
3639 +
3640 + // Check role access
3641 + $role_restriction = $embedding->role_restriction ?? 'public';
5278 3642 $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5279 - $source_url = $row->source_url ?? '';
5280 -
5281 - // Maintain top 10 display buffer (insert-if-beats-worst)
5282 - if (count($top_display) < 10) {
5283 - $top_display[] = [
5284 - 'id' => $row->id,
5285 - 'similarity' => $similarity,
5286 - 'source_url' => $source_url,
5287 - 'role_restriction' => $role_restriction,
5288 - 'has_access' => $has_access,
5289 - ];
5290 - usort($top_display, function ($a, $b) {
5291 - return $b['similarity'] <=> $a['similarity'];
5292 - });
5293 - } elseif ($similarity > $top_display[9]['similarity']) {
5294 - $top_display[9] = [
5295 - 'id' => $row->id,
5296 - 'similarity' => $similarity,
5297 - 'source_url' => $source_url,
5298 - 'role_restriction' => $role_restriction,
5299 - 'has_access' => $has_access,
5300 - ];
5301 - usort($top_display, function ($a, $b) {
5302 - return $b['similarity'] <=> $a['similarity'];
5303 - });
3643 +
3644 + // Store ALL similarities for testing (top 10)
3645 + $source_display = '';
3646 + if (!empty($embedding->source_url) && $embedding->source_url !== '#') {
3647 + $source_display = $embedding->source_url;
3648 + } else {
3649 + $content_preview = strip_tags($embedding->article_content ?? '');
3650 + $content_preview = preg_replace('/\s+/', ' ', $content_preview);
3651 + $source_display = substr(trim($content_preview), 0, 50) . '...';
5304 3652 }
5305 -
5306 - // Track candidates for context assembly (above threshold + has access)
3653 +
3654 + $all_similarities[] = [
3655 + 'document_id' => $embedding->id,
3656 + 'similarity' => $similarity,
3657 + 'similarity_percentage' => round($similarity * 100, 2),
3658 + 'above_threshold' => $similarity >= $similarity_threshold,
3659 + 'source_display' => $source_display,
3660 + 'content_preview' => substr(strip_tags($embedding->article_content ?? ''), 0, 100) . '...',
3661 + 'used_for_context' => false, // Initialize as false, we'll update this later
3662 + 'role_restriction' => $role_restriction, // Include role info for testing
3663 + 'has_access' => $has_access, // Include access info for testing
3664 + 'filtered_out' => !$has_access // Mark if filtered out by role
3665 + ];
3666 +
3667 + // Only consider results above threshold AND with access for actual content retrieval
5307 3668 if ($similarity >= $similarity_threshold && $has_access) {
5308 - $candidates[] = [
5309 - 'id' => $row->id,
5310 - 'similarity' => $similarity,
5311 - 'source_url' => $source_url,
3669 + $relevant_results[] = [
3670 + 'id' => $embedding->id,
3671 + 'similarity' => $similarity
5312 3672 ];
5313 3673 }
5314 -
5315 - $total_checked++;
5316 3674 }
5317 -
5318 - unset($batch);
5319 -
5320 - // Trim candidates periodically to cap memory during long scans
5321 - if (count($candidates) > $max_candidates) {
5322 - usort($candidates, function ($a, $b) {
5323 - return $b['similarity'] <=> $a['similarity'];
5324 - });
5325 - $candidates = array_slice($candidates, 0, $max_candidates);
5326 - }
5327 -
5328 - $offset += $batch_size;
5329 - } while (true);
5330 -
5331 - if ($total_checked === 0) {
5332 - $this->current_valid_urls = [];
5333 - return '';
3675 +
3676 + unset($database_embedding);
5334 3677 }
5335 3678
5336 - // Final candidates sort (best first)
5337 - if (count($candidates) > 1) {
5338 - usort($candidates, function ($a, $b) {
5339 - return $b['similarity'] <=> $a['similarity'];
5340 - });
5341 - }
5342 -
5343 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5344 - // Gather unique IDs we actually need (top_display + candidates) and pull
5345 - // article_content in bounded IN() batches. This avoids loading content for
5346 - // every row during the similarity scan.
5347 - $needed_ids = [];
5348 - foreach ($top_display as $item) {
5349 - $needed_ids[$item['id']] = true;
5350 - }
5351 - foreach ($candidates as $item) {
5352 - $needed_ids[$item['id']] = true;
5353 - }
5354 - $needed_ids = array_keys($needed_ids);
5355 -
5356 - $content_map = [];
5357 - if (!empty($needed_ids)) {
5358 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5359 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5360 - $rows = $wpdb->get_results($wpdb->prepare(
5361 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5362 - ...$chunk_ids
5363 - ));
5364 - foreach ($rows as $r) {
5365 - $content_map[$r->id] = $r->article_content;
5366 - }
5367 - unset($rows);
5368 - }
5369 - }
5370 -
5371 - // Build the all_similarities display array from the top 10
5372 - $all_similarities = [];
5373 - foreach ($top_display as $item) {
5374 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5375 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5376 - $is_chunk = $parsed_for_display['is_chunked'];
5377 - $chunk_meta = $parsed_for_display['metadata'];
5378 -
5379 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5380 - $source_display = $item['source_url'];
5381 - } else {
5382 - $content_preview = strip_tags($article_content_for_parse);
5383 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5384 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5385 - }
5386 -
5387 - $all_similarities[] = [
5388 - 'document_id' => $item['id'],
5389 - 'similarity' => $item['similarity'],
5390 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5391 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5392 - 'source_display' => $source_display,
5393 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5394 - 'used_for_context' => false,
5395 - 'role_restriction' => $item['role_restriction'],
5396 - 'has_access' => $item['has_access'],
5397 - 'filtered_out' => !$item['has_access'],
5398 - 'is_chunk' => $is_chunk,
5399 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5400 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5401 - ];
5402 - }
5403 -
5404 - // Build url_groups from candidates for chunk reassembly
5405 - $url_groups = array();
5406 - foreach ($candidates as $cand) {
5407 - $article_content = $content_map[$cand['id']] ?? '';
5408 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5409 - $is_chunked = $parsed['is_chunked'];
5410 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5411 - $text_content = $parsed['text'];
5412 -
5413 - $source_url = $cand['source_url'];
5414 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5415 -
5416 - if (!isset($url_groups[$group_key])) {
5417 - $url_groups[$group_key] = array(
5418 - 'source_url' => $source_url,
5419 - 'best_score' => 0,
5420 - 'is_chunked' => $is_chunked,
5421 - 'chunks' => array(),
5422 - 'single_text' => '',
5423 - 'single_id' => null
5424 - );
5425 - }
5426 -
5427 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5428 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5429 - }
5430 -
5431 - if ($is_chunked) {
5432 - $url_groups[$group_key]['is_chunked'] = true;
5433 - $url_groups[$group_key]['chunks'][] = array(
5434 - 'id' => $cand['id'],
5435 - 'score' => $cand['similarity'],
5436 - 'chunk_index' => $chunk_index,
5437 - 'text' => $text_content
5438 - );
5439 - } else {
5440 - $url_groups[$group_key]['single_text'] = $text_content;
5441 - $url_groups[$group_key]['single_id'] = $cand['id'];
5442 - }
5443 - }
5444 -
5445 3679 // Sort ALL similarities for testing display (highest first)
5446 3680 usort($all_similarities, function ($a, $b) {
5447 3681 return $b['similarity'] <=> $a['similarity'];
5448 3682 });
5449 -
5450 - // Sort URL groups by best score (highest first)
5451 - uasort($url_groups, function($a, $b) {
5452 - return $b['best_score'] <=> $a['best_score'];
3683 +
3684 + // Sort relevant results by similarity (highest first)
3685 + usort($relevant_results, function ($a, $b) {
3686 + return $b['similarity'] <=> $a['similarity'];
5453 3687 });
5454 -
5455 - // Get RAG sources limit from options (default 6, min 3, max 10)
5456 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5457 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5458 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5459 -
5460 - // Take top N unique URLs based on user setting
5461 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5462 -
5463 - // Track which document IDs are used for context
3688 +
3689 + // Get top 5 results for actual content (standard approach)
3690 + $top_results = array_slice($relevant_results, 0, 5);
3691 +
3692 + // NOW mark which documents are actually used for context
5464 3693 $used_document_ids = [];
5465 - foreach ($top_urls as $group) {
5466 - if ($group['is_chunked']) {
5467 - foreach ($group['chunks'] as $chunk) {
5468 - $used_document_ids[] = $chunk['id'];
5469 - }
5470 - } elseif ($group['single_id']) {
5471 - $used_document_ids[] = $group['single_id'];
5472 - }
3694 + foreach ($top_results as $result) {
3695 + $used_document_ids[] = $result['id'];
5473 3696 }
5474 -
3697 +
5475 3698 // Update the all_similarities array to mark which were actually used
5476 3699 foreach ($all_similarities as &$similarity_item) {
5477 3700 $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5478 3701 }
5479 -
5480 - // Store top 10 for testing panel
3702 +
3703 + // Store top 10 for testing panel (now with correct used_for_context flags and role info)
5481 3704 $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5482 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5483 -
3705 + $this->last_similarity_analysis['total_checked'] = count($embeddings);
3706 +
3707 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " top matches for testing");
3708 +
5484 3709 // Initialize final content
5485 3710 $content = '';
5486 - $matches_used = 0;
5487 - $total_chunks_used = 0;
5488 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5489 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5490 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5491 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5492 -
5493 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5494 - // Use fresh options to ensure we get the latest setting value
5495 - $fresh_options = get_option('mxchat_options', []);
5496 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5497 -
5498 - // Build content from top sources
5499 - foreach ($top_urls as $group_key => $group) {
5500 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5501 -
5502 - // Stop if we've hit the total chunk limit
5503 - if ($total_chunks_used >= $max_total_chunks) {
5504 - break;
3711 +
3712 + // Track document IDs to avoid duplicates
3713 + $added_document_ids = [];
3714 +
3715 + // Fetch and format content for each selected result
3716 + foreach ($top_results as $index => $result) {
3717 + if (in_array($result['id'], $added_document_ids)) {
3718 + continue;
5505 3719 }
5506 -
5507 - $full_text = '';
5508 - $chunks_in_this_source = 1; // Default for non-chunked content
5509 -
5510 - if ($group['is_chunked']) {
5511 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5512 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5513 -
5514 - // Fetch chunks for this URL with limit
5515 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5516 -
5517 - // If fetching all chunks fails, fall back to matched chunks
5518 - if (empty($full_text)) {
5519 - // Sort matched chunks by index and concatenate
5520 - usort($group['chunks'], function($a, $b) {
5521 - return $a['chunk_index'] <=> $b['chunk_index'];
5522 - });
5523 -
5524 - $chunk_texts = array();
5525 - $chunks_in_this_source = 0;
5526 - foreach ($group['chunks'] as $chunk) {
5527 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5528 - break;
5529 - }
5530 - $chunk_texts[] = $chunk['text'];
5531 - $chunks_in_this_source++;
3720 +
3721 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3722 + $added_document_ids[] = $result['id'];
3723 +
3724 + $content .= "## Reference " . ($index + 1) . " ##\n";
3725 + $content .= $chunk_content . "\n\n";
3726 +
3727 + // PDF surrounding pages logic (unchanged)
3728 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
3729 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
3730 + "SELECT id, article_content, role_restriction FROM {$system_prompt_table}
3731 + WHERE id IN (
3732 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
3733 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
3734 + )",
3735 + $result['id'],
3736 + $result['id']
3737 + ));
3738 +
3739 + // Check role access for surrounding content too
3740 + if (!empty($surrounding_content[0])) {
3741 + $surrounding_role = $surrounding_content[0]->role_restriction ?? 'public';
3742 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3743 + $content .= "## Related Content ##\n";
3744 + $content .= $surrounding_content[0]->article_content . "\n\n";
3745 + $added_document_ids[] = $surrounding_content[0]->id;
5532 3746 }
5533 - $full_text = implode("\n\n", $chunk_texts);
5534 3747 }
5535 - } else {
5536 - $full_text = $group['single_text'];
5537 - $chunks_in_this_source = 1;
5538 - }
5539 -
5540 - if (!empty($full_text)) {
5541 - // Strip URLs from content if citation links are disabled
5542 - if (!$citation_links_enabled) {
5543 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5544 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5545 - }
5546 -
5547 - // Use numbered reference for URL-based entries, plain info label for manual entries
5548 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5549 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5550 - $matches_used++;
5551 - $content .= "## Reference " . $matches_used . " ##\n";
5552 - $content .= $full_text . "\n\n";
5553 -
5554 - // Only include citation URLs if citation links are enabled
5555 - if ($citation_links_enabled) {
5556 - $valid_urls[] = $source_url;
5557 - $content .= "URL: " . $source_url . "\n\n";
3748 +
3749 + if (!empty($surrounding_content[1])) {
3750 + $surrounding_role = $surrounding_content[1]->role_restriction ?? 'public';
3751 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3752 + $content .= "## Related Content ##\n";
3753 + $content .= $surrounding_content[1]->article_content . "\n\n";
3754 + $added_document_ids[] = $surrounding_content[1]->id;
5558 3755 }
5559 - } else {
5560 - // Manual entry — no reference number, no citation
5561 - $content .= "## Information ##\n";
5562 - $content .= $full_text . "\n\n";
5563 3756 }
5564 -
5565 - // Extract any URLs from the text content itself (only if citation links enabled)
5566 - if ($citation_links_enabled) {
5567 - preg_match_all(
5568 - '#\bhttps?://[^\s<>"\']+#i',
5569 - $full_text,
5570 - $content_urls
5571 - );
5572 - if (!empty($content_urls[0])) {
5573 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5574 - }
5575 - }
5576 -
5577 - $total_chunks_used += $chunks_in_this_source;
5578 3757 }
5579 3758 }
5580 -
5581 - // NEW: Store unique valid URLs for validation
5582 - $this->current_valid_urls = array_unique($valid_urls);
5583 -
5584 - // Store sources and chunks counts for testing/transcript display
5585 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5586 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5587 -
5588 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5589 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5590 -
3759 +
5591 3760 // Add response guidelines
5592 - if (empty($top_urls)) {
3761 + if (empty($top_results)) {
5593 3762 $content = "No reference information was found for this query.\n\n";
5594 3763 } else {
5595 - // Build response guidelines based on citation links setting
5596 3764 $content .= "\n## Response Guidelines ##\n" .
5597 3765 "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5598 3766 "Be conversational and friendly, but never mention your knowledge base or training data. " .
5599 3767 "If you don't have specific information or are uncertain about any details, it's always " .
5600 3768 "better to honestly say you don't know rather than making up or guessing at answers. " .
5601 - "When information is incomplete, let them know you are unsure.\n\n";
5602 -
5603 - // Only add hyperlink instructions if citation links are enabled
5604 - if ($citation_links_enabled) {
5605 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5606 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5607 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5608 - } else {
5609 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5610 - "Simply provide helpful answers based on the reference information without citing sources.";
5611 - }
3769 + "When information is incomplete, let them know you are unsure.";
5612 3770 }
5613 3771
5614 3772 return trim($content);
5615 3773 }
5616 3774
5617 -/**
5618 - * Fetch and reassemble chunks for a URL from WordPress database
5619 - *
5620 - * @param string $source_url The source URL to fetch chunks for
5621 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5622 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5623 - * @return string Reassembled content from chunks
5624 - */
5625 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5626 - global $wpdb;
5627 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5628 -
5629 - // Fetch all rows with this source_url
5630 - $rows = $wpdb->get_results($wpdb->prepare(
5631 - "SELECT article_content FROM {$table}
5632 - WHERE source_url = %s
5633 - ORDER BY id ASC",
5634 - $source_url
5635 - ));
5636 -
5637 - if (empty($rows)) {
5638 - $chunk_count = 0;
5639 - return '';
5640 - }
5641 -
5642 - // Parse and sort chunks by index
5643 - $chunks = array();
5644 - foreach ($rows as $row) {
5645 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5646 -
5647 - if ($parsed['is_chunked']) {
5648 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5649 - $chunks[$chunk_index] = $parsed['text'];
5650 - } else {
5651 - // Non-chunked content - just return it
5652 - $chunks[] = $parsed['text'];
5653 - }
5654 - }
5655 -
5656 - // Sort by chunk index
5657 - ksort($chunks);
5658 -
5659 - // Apply chunk limit if specified
5660 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5661 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5662 - }
5663 -
5664 - // Store actual chunk count
5665 - $chunk_count = count($chunks);
5666 -
5667 - // Reassemble content
5668 - return implode("\n\n", $chunks);
5669 -}
5670 -
5671 3775 private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5672 3776 global $wpdb;
5673 3777
5674 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5675 - //error_log(" - bot_id: " . $bot_id);
5676 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5677 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
3778 + error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
3779 + error_log(" - bot_id: " . $bot_id);
3780 + error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
3781 + error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5678 3782
5679 3783 // Use bot-specific config or fall back to default
5680 3784 if ($bot_config === null) {
5681 3785 $bot_config = $this->get_bot_pinecone_config($bot_id);
@@ -5684,12 +3788,12 @@
5684 3788 $api_key = $bot_config['api_key'] ?? '';
5685 3789 $host = $bot_config['host'] ?? '';
5686 3790 $namespace = $bot_config['namespace'] ?? '';
5687 3791
5688 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5689 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5690 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5691 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
3792 + error_log("MXCHAT DEBUG: Pinecone query parameters:");
3793 + error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
3794 + error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
3795 + error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5692 3796
5693 3797 // Initialize similarity analysis storage
5694 3798 $this->last_similarity_analysis = [
5695 3799 'knowledge_base_type' => 'Pinecone',
@@ -5699,17 +3803,12 @@
5699 3803 'threshold_used' => 0,
5700 3804 'total_checked' => 0
5701 3805 ];
5702 3806
5703 - // NEW: Initialize valid URLs array
5704 - $valid_urls = [];
5705 -
5706 3807 if (empty($host) || empty($api_key)) {
5707 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5708 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5709 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5710 - // Store empty array for valid URLs since we can't proceed
5711 - $this->current_valid_urls = [];
3808 + error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
3809 + error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
3810 + error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5712 3811 return '';
5713 3812 }
5714 3813
5715 3814 // Get knowledge manager instance for role checking
@@ -5729,9 +3828,9 @@
5729 3828 $api_endpoint = "https://{$host}/query";
5730 3829
5731 3830 $request_body = array(
5732 3831 'vector' => $user_embedding,
5733 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
3832 + 'topK' => 20, // Request more to get good testing data
5734 3833 'includeMetadata' => true,
5735 3834 'includeValues' => true
5736 3835 );
5737 3836
@@ -5739,11 +3838,11 @@
5739 3838 if (!empty($namespace)) {
5740 3839 $request_body['namespace'] = $namespace;
5741 3840 }
5742 3841
5743 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5744 - //error_log(" - Endpoint: " . $api_endpoint);
5745 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
3842 + error_log("MXCHAT DEBUG: About to call Pinecone API");
3843 + error_log(" - Endpoint: " . $api_endpoint);
3844 + error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5746 3845
5747 3846 $response = wp_remote_post($api_endpoint, array(
5748 3847 'headers' => array(
5749 3848 'Api-Key' => $api_key,
@@ -5754,61 +3853,53 @@
5754 3853 'timeout' => 30
5755 3854 ));
5756 3855
5757 3856 if (is_wp_error($response)) {
5758 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5759 - // Store empty array for valid URLs
5760 - $this->current_valid_urls = [];
3857 + error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5761 3858 return '';
5762 3859 }
5763 3860
5764 3861 $response_code = wp_remote_retrieve_response_code($response);
5765 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
3862 + error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5766 3863
5767 3864 if ($response_code !== 200) {
5768 3865 $response_body = wp_remote_retrieve_body($response);
5769 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5770 - // Store empty array for valid URLs
5771 - $this->current_valid_urls = [];
3866 + error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5772 3867 return '';
5773 3868 }
5774 3869
5775 3870 // ADD DETAILED DEBUG SECTION HERE
5776 3871 $response_body = wp_remote_retrieve_body($response);
5777 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
3872 + error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5778 3873
5779 3874 $results = json_decode($response_body, true);
5780 3875
5781 3876 if (json_last_error() !== JSON_ERROR_NONE) {
5782 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5783 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5784 - // Store empty array for valid URLs
5785 - $this->current_valid_urls = [];
3877 + error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
3878 + error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5786 3879 return '';
5787 3880 }
5788 3881
5789 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5790 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5791 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
3882 + error_log("MXCHAT DEBUG: Pinecone response structure:");
3883 + error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
3884 + error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5792 3885
5793 3886 if (empty($results['matches'])) {
5794 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5795 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5796 - // Store empty array for valid URLs
5797 - $this->current_valid_urls = [];
3887 + error_log("MXCHAT DEBUG: No matches found in Pinecone response");
3888 + error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5798 3889 return '';
5799 3890 }
5800 3891
5801 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
3892 + error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
5802 3893
5803 3894 // Log first match details for debugging
5804 3895 if (!empty($results['matches'][0])) {
5805 3896 $first_match = $results['matches'][0];
5806 - //error_log("MXCHAT DEBUG: First match details:");
5807 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
5808 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
3897 + error_log("MXCHAT DEBUG: First match details:");
3898 + error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
3899 + error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5809 3900 if (isset($first_match['metadata'])) {
5810 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
3901 + error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5811 3902 }
5812 3903 }
5813 3904
5814 3905 // Initialize the final content
@@ -5814,182 +3905,45 @@
5814 3905 // Initialize the final content
5815 3906 $content = '';
5816 3907 $matches_used = 0;
5817 3908 $matches_used_for_context = [];
5818 - $total_chunks_used = 0;
5819 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5820 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5821 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5822 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5823 -
5824 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5825 - // Use fresh options to ensure we get the latest setting value
5826 - $fresh_options = get_option('mxchat_options', []);
5827 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5828 -
5829 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
5830 - $url_groups = array();
5831 -
3909 +
3910 + // Process each match for actual content generation (lazy role checking)
5832 3911 foreach ($results['matches'] as $index => $match) {
5833 3912 // Skip if similarity is below threshold
5834 3913 if ($match['score'] < $similarity_threshold) {
5835 3914 continue;
5836 3915 }
5837 -
5838 - $metadata = $match['metadata'] ?? array();
5839 - $source_url = $metadata['source_url'] ?? '';
5840 - $match_id = $match['id'] ?? '';
5841 -
5842 - // LAZY ROLE CHECK: Only check role for content we're actually considering
5843 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
5844 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5845 -
5846 - // Skip if user doesn't have access
5847 - if (!$has_access) {
5848 - continue;
5849 - }
5850 -
5851 - // Use a unique key for manual entries without a source URL
5852 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
5853 -
5854 - // Group by source URL (or unique key for manual entries)
5855 - if (!isset($url_groups[$group_key])) {
5856 - $url_groups[$group_key] = array(
5857 - 'source_url' => $source_url,
5858 - 'best_score' => 0,
5859 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
5860 - 'chunks' => array(),
5861 - 'single_text' => ''
5862 - );
5863 - }
5864 -
5865 - // Track best score for this group
5866 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
5867 - $url_groups[$group_key]['best_score'] = $match['score'];
5868 - }
5869 -
5870 - // Store chunk info or single text
5871 - if ($url_groups[$group_key]['is_chunked']) {
5872 - $url_groups[$group_key]['chunks'][] = array(
5873 - 'id' => $match_id,
5874 - 'score' => $match['score'],
5875 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
5876 - 'text' => $metadata['text'] ?? ''
5877 - );
5878 - } else {
5879 - // Non-chunked content - just store the text
5880 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
5881 - $url_groups[$group_key]['single_id'] = $match_id;
5882 - }
5883 - }
5884 -
5885 - // Sort URL groups by best score (highest first)
5886 - uasort($url_groups, function($a, $b) {
5887 - return $b['best_score'] <=> $a['best_score'];
5888 - });
5889 -
5890 - // Get RAG sources limit from options (default 6, min 3, max 10)
5891 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5892 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5893 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5894 -
5895 - // Take top N unique URLs based on user setting
5896 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5897 -
5898 - // Track which match IDs are actually used for context
5899 - foreach ($top_urls as $group) {
5900 - if ($group['is_chunked']) {
5901 - foreach ($group['chunks'] as $chunk) {
5902 - $matches_used_for_context[] = $chunk['id'];
5903 - }
5904 - } elseif (!empty($group['single_id'])) {
5905 - $matches_used_for_context[] = $group['single_id'];
5906 - }
5907 - }
5908 -
5909 - // Build content from top sources
5910 - foreach ($top_urls as $group_key => $group) {
5911 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5912 -
5913 - // Stop if we've hit the total chunk limit
5914 - if ($total_chunks_used >= $max_total_chunks) {
3916 +
3917 + // Limit to top 5 matches above threshold
3918 + if ($matches_used >= 5) {
5915 3919 break;
5916 3920 }
5917 -
5918 - $full_text = '';
5919 - $chunks_in_this_source = 1; // Default for non-chunked content
5920 -
5921 - if ($group['is_chunked']) {
5922 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5923 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5924 -
5925 - // Fetch chunks for this URL with limit
5926 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
5927 -
5928 - // If fetching all chunks fails, fall back to matched chunks
5929 - if (empty($full_text)) {
5930 - // Sort matched chunks by index and concatenate
5931 - usort($group['chunks'], function($a, $b) {
5932 - return $a['chunk_index'] <=> $b['chunk_index'];
5933 - });
5934 -
5935 - $chunk_texts = array();
5936 - $chunks_in_this_source = 0;
5937 - foreach ($group['chunks'] as $chunk) {
5938 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5939 - break;
5940 - }
5941 - $chunk_texts[] = $chunk['text'];
5942 - $chunks_in_this_source++;
5943 - }
5944 - $full_text = implode("\n\n", $chunk_texts);
3921 +
3922 + if (!empty($match['metadata']['text'])) {
3923 + // LAZY ROLE CHECK: Only check role for content we're actually considering
3924 + $match_id = $match['id'] ?? '';
3925 + $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
3926 + $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
3927 +
3928 + // Skip if user doesn't have access
3929 + if (!$has_access) {
3930 + continue;
5945 3931 }
5946 - } else {
5947 - $full_text = $group['single_text'];
5948 - $chunks_in_this_source = 1;
5949 - }
5950 -
5951 - if (!empty($full_text)) {
5952 - // Strip URLs from content if citation links are disabled
5953 - if (!$citation_links_enabled) {
5954 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5955 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
3932 +
3933 + // User has access - add to content
3934 + $content .= "## Reference " . ($matches_used + 1) . " ##\n";
3935 + $content .= $match['metadata']['text'] . "\n\n";
3936 +
3937 + if (!empty($match['metadata']['source_url'])) {
3938 + $content .= "URL: " . $match['metadata']['source_url'] . "\n\n";
5956 3939 }
5957 -
5958 - // Use numbered reference for URL-based entries, plain info label for manual entries
5959 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5960 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5961 - $matches_used++;
5962 - $content .= "## Reference " . $matches_used . " ##\n";
5963 - $content .= $full_text . "\n\n";
5964 -
5965 - // Only include citation URLs if citation links are enabled
5966 - if ($citation_links_enabled) {
5967 - $valid_urls[] = $source_url;
5968 - $content .= "URL: " . $source_url . "\n\n";
5969 - }
5970 - } else {
5971 - // Manual entry — no reference number, no citation
5972 - $content .= "## Information ##\n";
5973 - $content .= $full_text . "\n\n";
5974 - }
5975 -
5976 - // Extract any URLs from the text content itself (only if citation links enabled)
5977 - if ($citation_links_enabled) {
5978 - preg_match_all(
5979 - '#\bhttps?://[^\s<>"\']+#i',
5980 - $full_text,
5981 - $content_urls
5982 - );
5983 - if (!empty($content_urls[0])) {
5984 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5985 - }
5986 - }
5987 -
5988 - $total_chunks_used += $chunks_in_this_source;
3940 +
3941 + $matches_used_for_context[] = $match['id'] ?? $index;
3942 + $matches_used++;
5989 3943 }
5990 3944 }
5991 -
3945 +
5992 3946 // Process ALL matches for testing data (top 10) - with role checking for testing display
5993 3947 $all_matches = [];
5994 3948 foreach ($results['matches'] as $index => $match) {
5995 3949 if ($index >= 10) break; // Limit to top 10 for testing
@@ -6009,19 +3963,9 @@
6009 3963 $source_display = substr(trim($content_preview), 0, 50) . '...';
6010 3964 }
6011 3965
6012 3966 $match_id_for_display = $match['id'] ?? $index;
6013 -
6014 - // Check for chunk metadata in Pinecone
6015 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6016 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6017 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6018 -
6019 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6020 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6021 - $is_chunk = true;
6022 - }
6023 -
3967 +
6024 3968 $all_matches[] = [
6025 3969 'document_id' => $match_id_for_display,
6026 3970 'similarity' => $match['score'],
6027 3971 'similarity_percentage' => round($match['score'] * 100, 2),
@@ -6030,12 +3974,9 @@
6030 3974 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6031 3975 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6032 3976 'role_restriction' => $role_restriction,
6033 3977 'has_access' => $has_access,
6034 - 'filtered_out' => !$has_access,
6035 - 'is_chunk' => $is_chunk,
6036 - 'chunk_index' => $chunk_index,
6037 - 'total_chunks' => $total_chunks
3978 + 'filtered_out' => !$has_access
6038 3979 ];
6039 3980 }
6040 3981
6041 3982 // Store for testing panel
@@ -6040,43 +3981,25 @@
6040 3981
6041 3982 // Store for testing panel
6042 3983 $this->last_similarity_analysis['top_matches'] = $all_matches;
6043 3984 $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6044 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6045 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6046 -
6047 - // NEW: Store unique valid URLs for validation
6048 - $this->current_valid_urls = array_unique($valid_urls);
6049 -
6050 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6051 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6052 -
3985 +
6053 3986 // Add response guidelines
6054 3987 if ($matches_used === 0) {
6055 3988 $content = "No reference information was found for this query.\n\n";
6056 3989 } else {
6057 - // Build response guidelines based on citation links setting
6058 3990 $content .= "\n## Response Guidelines ##\n" .
6059 3991 "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6060 3992 "Be conversational and friendly, but never mention your knowledge base or training data. " .
6061 3993 "If you don't have specific information or are uncertain about any details, it's always " .
6062 3994 "better to honestly say you don't know rather than making up or guessing at answers. " .
6063 - "When information is incomplete, let them know you are unsure.\n\n";
6064 -
6065 - // Only add hyperlink instructions if citation links are enabled
6066 - if ($citation_links_enabled) {
6067 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6068 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6069 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6070 - } else {
6071 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6072 - "Simply provide helpful answers based on the reference information without citing sources.";
6073 - }
3995 + "When information is incomplete, let them know you are unsure.";
6074 3996 }
6075 -
3997 +
6076 3998 return trim($content);
6077 3999 }
6078 4000
4001 +
6079 4002 /**
6080 4003 * Get role restriction for a single vector (with caching)
6081 4004 */
6082 4005 private function get_single_vector_role($vector_id, $metadata = array()) {
@@ -6113,518 +4036,12 @@
6113 4036 }
6114 4037
6115 4038 // Cache individual role for 1 hour
6116 4039 wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6117 -
4040 +
6118 4041 return $role_restriction;
6119 4042 }
6120 4043
6121 -/**
6122 - * Fetch and reassemble all chunks for a URL from Pinecone
6123 - *
6124 - * @param string $source_url The source URL to fetch chunks for
6125 - * @param array $bot_config Bot-specific Pinecone configuration
6126 - * @return string Reassembled content from all chunks
6127 - */
6128 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6129 - $api_key = $bot_config['api_key'] ?? '';
6130 - $host = $bot_config['host'] ?? '';
6131 - $namespace = $bot_config['namespace'] ?? '';
6132 -
6133 - if (empty($host) || empty($api_key)) {
6134 - $chunk_count = 0;
6135 - return '';
6136 - }
6137 -
6138 - $base_hash = md5($source_url);
6139 -
6140 - // Use Pinecone list API to find all chunk vectors with this prefix
6141 - $list_url = "https://{$host}/vectors/list";
6142 -
6143 - // Limit to max_chunks if specified, otherwise fetch up to 100
6144 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6145 -
6146 - $list_body = array(
6147 - 'prefix' => $base_hash . '_chunk_',
6148 - 'limit' => $fetch_limit
6149 - );
6150 -
6151 - if (!empty($namespace)) {
6152 - $list_body['namespace'] = $namespace;
6153 - }
6154 -
6155 - $list_response = wp_remote_post($list_url, array(
6156 - 'headers' => array(
6157 - 'Api-Key' => $api_key,
6158 - 'accept' => 'application/json',
6159 - 'content-type' => 'application/json'
6160 - ),
6161 - 'body' => wp_json_encode($list_body),
6162 - 'timeout' => 30
6163 - ));
6164 -
6165 - if (is_wp_error($list_response)) {
6166 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6167 - return '';
6168 - }
6169 -
6170 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6171 -
6172 - if (empty($list_data['vectors'])) {
6173 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6174 - return '';
6175 - }
6176 -
6177 - // Extract vector IDs
6178 - $vector_ids = array();
6179 - foreach ($list_data['vectors'] as $vector) {
6180 - if (isset($vector['id'])) {
6181 - $vector_ids[] = $vector['id'];
6182 - }
6183 - }
6184 -
6185 - if (empty($vector_ids)) {
6186 - return '';
6187 - }
6188 -
6189 - // Fetch all chunk content
6190 - $fetch_url = "https://{$host}/vectors/fetch";
6191 -
6192 - $fetch_body = array(
6193 - 'ids' => $vector_ids
6194 - );
6195 -
6196 - if (!empty($namespace)) {
6197 - $fetch_body['namespace'] = $namespace;
6198 - }
6199 -
6200 - $fetch_response = wp_remote_post($fetch_url, array(
6201 - 'headers' => array(
6202 - 'Api-Key' => $api_key,
6203 - 'accept' => 'application/json',
6204 - 'content-type' => 'application/json'
6205 - ),
6206 - 'body' => wp_json_encode($fetch_body),
6207 - 'timeout' => 30
6208 - ));
6209 -
6210 - if (is_wp_error($fetch_response)) {
6211 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6212 - return '';
6213 - }
6214 -
6215 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6216 -
6217 - if (empty($fetch_data['vectors'])) {
6218 - return '';
6219 - }
6220 -
6221 - // Sort chunks by index and reassemble
6222 - $chunks = array();
6223 - foreach ($fetch_data['vectors'] as $id => $vector) {
6224 - $metadata = $vector['metadata'] ?? array();
6225 - $chunk_index = $metadata['chunk_index'] ?? 0;
6226 - $text = $metadata['text'] ?? '';
6227 -
6228 - // Store chunk with its index
6229 - $chunks[$chunk_index] = $text;
6230 - }
6231 -
6232 - // Sort by chunk index
6233 - ksort($chunks);
6234 -
6235 - // Apply chunk limit if specified
6236 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6237 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6238 - }
6239 -
6240 - // Store actual chunk count
6241 - $chunk_count = count($chunks);
6242 -
6243 - // Reassemble content
6244 - return implode("\n\n", $chunks);
6245 -}
6246 -
6247 -/**
6248 - * Search for relevant content using OpenAI Vector Store (File Search)
6249 - *
6250 - * @param string $user_query The user's query text
6251 - * @param string $bot_id The bot ID
6252 - * @param array $vectorstore_config Vector Store configuration
6253 - * @return string Formatted context string with references
6254 - */
6255 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6256 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6257 - //error_log(" - bot_id: " . $bot_id);
6258 - //error_log(" - user_query length: " . strlen($user_query));
6259 -
6260 - // Get OpenAI API key
6261 - $mxchat_options = get_option('mxchat_options', array());
6262 - $api_key = $mxchat_options['api_key'] ?? '';
6263 -
6264 - // Reset vectorstore error tracking
6265 - $this->last_vectorstore_error = null;
6266 -
6267 - if (empty($api_key)) {
6268 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6269 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6270 - $this->current_valid_urls = [];
6271 - return '';
6272 - }
6273 -
6274 - // Get Vector Store configuration
6275 - if (empty($vectorstore_config)) {
6276 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6277 - }
6278 -
6279 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6280 - $max_results = $vectorstore_config['max_results'] ?? 5;
6281 -
6282 - if (empty($vectorstore_ids_string)) {
6283 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6284 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6285 - $this->current_valid_urls = [];
6286 - return '';
6287 - }
6288 -
6289 - // Parse Vector Store IDs
6290 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6291 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6292 -
6293 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6294 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6295 -
6296 - // Initialize similarity analysis storage
6297 - $this->last_similarity_analysis = [
6298 - 'knowledge_base_type' => 'OpenAI Vector Store',
6299 - 'bot_id' => $bot_id,
6300 - 'vectorstore_ids' => $vectorstore_ids,
6301 - 'top_matches' => [],
6302 - 'threshold_used' => 0,
6303 - 'total_checked' => 0
6304 - ];
6305 -
6306 - $valid_urls = [];
6307 -
6308 - // Get the selected model
6309 - $bot_options = $this->get_bot_options($bot_id);
6310 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6311 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6312 -
6313 - // Verify it's an OpenAI model
6314 - if (!$this->is_openai_chat_model($selected_model)) {
6315 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6316 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6317 - $this->current_valid_urls = [];
6318 - return '';
6319 - }
6320 -
6321 - // Use OpenAI Responses API with file_search tool
6322 - $request_body = array(
6323 - 'model' => $selected_model,
6324 - 'input' => $user_query,
6325 - 'tools' => array(
6326 - array(
6327 - 'type' => 'file_search',
6328 - 'vector_store_ids' => $vectorstore_ids,
6329 - 'max_num_results' => intval($max_results)
6330 - )
6331 - ),
6332 - 'include' => array('output[*].file_search_call.search_results')
6333 - );
6334 -
6335 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6336 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6337 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6338 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6339 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6340 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6341 -
6342 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6343 - 'headers' => array(
6344 - 'Authorization' => 'Bearer ' . $api_key,
6345 - 'Content-Type' => 'application/json'
6346 - ),
6347 - 'body' => wp_json_encode($request_body),
6348 - 'timeout' => 60
6349 - ));
6350 -
6351 - if (is_wp_error($response)) {
6352 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6353 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6354 - $this->current_valid_urls = [];
6355 - return '';
6356 - }
6357 -
6358 - $response_code = wp_remote_retrieve_response_code($response);
6359 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6360 -
6361 - $response_body = wp_remote_retrieve_body($response);
6362 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6363 -
6364 - if ($response_code !== 200) {
6365 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6366 - $api_error_detail = '';
6367 - $decoded_error = json_decode($response_body, true);
6368 - if (isset($decoded_error['error']['message'])) {
6369 - $api_error_detail = $decoded_error['error']['message'];
6370 - }
6371 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6372 - $this->current_valid_urls = [];
6373 - return '';
6374 - }
6375 - $result = json_decode($response_body, true);
6376 -
6377 - if (json_last_error() !== JSON_ERROR_NONE) {
6378 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6379 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6380 - $this->current_valid_urls = [];
6381 - return '';
6382 - }
6383 -
6384 - // Debug: Log the structure of the result
6385 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6386 - if (isset($result['output'])) {
6387 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6388 - foreach ($result['output'] as $idx => $out) {
6389 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6390 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6391 - }
6392 - } else {
6393 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6394 - }
6395 -
6396 - // Extract file search results from the response
6397 - $content = '';
6398 - $matches_used = 0;
6399 - $all_matches = [];
6400 -
6401 - // The Responses API returns output array with tool results
6402 - if (isset($result['output']) && is_array($result['output'])) {
6403 - foreach ($result['output'] as $output_item) {
6404 - // Look for file_search_call results
6405 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6406 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6407 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6408 -
6409 - // Check for search_results in the output item directly
6410 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6411 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6412 -
6413 - if (empty($search_results)) {
6414 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6415 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6416 - }
6417 -
6418 - foreach ($search_results as $index => $search_result) {
6419 - $filename = $search_result['filename'] ?? '';
6420 - $score = $search_result['score'] ?? 0;
6421 - $text_content = '';
6422 -
6423 - // Extract text content from the result
6424 - // The text can be directly on the result OR nested under content array
6425 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6426 - // Direct text field (OpenAI's actual format)
6427 - $text_content = $search_result['text'];
6428 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6429 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6430 - // Nested content array format
6431 - foreach ($search_result['content'] as $content_item) {
6432 - if (isset($content_item['text'])) {
6433 - $text_content .= $content_item['text'] . "\n";
6434 - }
6435 - }
6436 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6437 - } else {
6438 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6439 - }
6440 -
6441 - if (!empty($text_content)) {
6442 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6443 - $content .= trim($text_content) . "\n\n";
6444 -
6445 - if (!empty($filename)) {
6446 - $content .= "Source: " . $filename . "\n\n";
6447 - }
6448 -
6449 - // Extract URLs from content
6450 - preg_match_all(
6451 - '#\bhttps?://[^\s<>"\']+#i',
6452 - $text_content,
6453 - $content_urls
6454 - );
6455 - if (!empty($content_urls[0])) {
6456 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6457 - }
6458 -
6459 - $matches_used++;
6460 - }
6461 -
6462 - // Store for similarity analysis
6463 - $all_matches[] = [
6464 - 'document_id' => $filename ?: ('result_' . $index),
6465 - 'similarity' => $score,
6466 - 'similarity_percentage' => round($score * 100, 2),
6467 - 'above_threshold' => true,
6468 - 'source_display' => $filename,
6469 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6470 - 'used_for_context' => true,
6471 - 'role_restriction' => 'public',
6472 - 'has_access' => true,
6473 - 'filtered_out' => false
6474 - ];
6475 - }
6476 - }
6477 -
6478 - // Also check for message content with annotations (citations)
6479 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6480 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6481 - foreach ($output_item['content'] as $content_block) {
6482 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6483 - foreach ($content_block['annotations'] as $annotation) {
6484 - if (isset($annotation['filename'])) {
6485 - $filename = $annotation['filename'];
6486 - $score = $annotation['score'] ?? 0;
6487 - $text_content = '';
6488 -
6489 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6490 - foreach ($annotation['content'] as $ann_content) {
6491 - if (isset($ann_content['text'])) {
6492 - $text_content .= $ann_content['text'] . "\n";
6493 - }
6494 - }
6495 - }
6496 -
6497 - if (!empty($text_content) && $matches_used < $max_results) {
6498 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6499 - $content .= trim($text_content) . "\n\n";
6500 - $content .= "Source: " . $filename . "\n\n";
6501 -
6502 - preg_match_all(
6503 - '#\bhttps?://[^\s<>"\']+#i',
6504 - $text_content,
6505 - $content_urls
6506 - );
6507 - if (!empty($content_urls[0])) {
6508 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6509 - }
6510 -
6511 - $matches_used++;
6512 -
6513 - $all_matches[] = [
6514 - 'document_id' => $filename,
6515 - 'similarity' => $score,
6516 - 'similarity_percentage' => round($score * 100, 2),
6517 - 'above_threshold' => true,
6518 - 'source_display' => $filename,
6519 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6520 - 'used_for_context' => true,
6521 - 'role_restriction' => 'public',
6522 - 'has_access' => true,
6523 - 'filtered_out' => false
6524 - ];
6525 - }
6526 - }
6527 - }
6528 - }
6529 - }
6530 - }
6531 - }
6532 - }
6533 - }
6534 -
6535 - // Store for testing panel
6536 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6537 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6538 -
6539 - // Store unique valid URLs for validation
6540 - $this->current_valid_urls = array_unique($valid_urls);
6541 -
6542 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6543 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6544 -
6545 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6546 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6547 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6548 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6549 - if ($matches_used > 0) {
6550 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6551 - }
6552 -
6553 - // Check if citation links are enabled
6554 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6555 -
6556 - // Add response guidelines
6557 - if ($matches_used === 0) {
6558 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6559 - $content = "No reference information was found for this query.\n\n";
6560 - } else {
6561 - // Build response guidelines based on citation links setting
6562 - $content .= "\n## Response Guidelines ##\n" .
6563 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6564 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6565 - "If you don't have specific information or are uncertain about any details, it's always " .
6566 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6567 - "When information is incomplete, let them know you are unsure.\n\n";
6568 -
6569 - // Only add hyperlink instructions if citation links are enabled
6570 - if ($citation_links_enabled) {
6571 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6572 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6573 - } else {
6574 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6575 - "Simply provide helpful answers based on the reference information without citing sources.";
6576 - }
6577 - }
6578 -
6579 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6580 -
6581 - return trim($content);
6582 -}
6583 -
6584 -/**
6585 - * Check if the given model is an OpenAI chat model
6586 - *
6587 - * @param string $model The model ID
6588 - * @return bool True if it's an OpenAI model
6589 - */
6590 -private function is_openai_chat_model($model) {
6591 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6592 - foreach ($openai_prefixes as $prefix) {
6593 - if (strpos($model, $prefix) === 0) {
6594 - return true;
6595 - }
6596 - }
6597 - return false;
6598 -}
6599 -
6600 -/**
6601 - * Get bot-specific Vector Store configuration
6602 - *
6603 - * @param string $bot_id The bot ID
6604 - * @return array Configuration array
6605 - */
6606 -private function get_bot_vectorstore_config($bot_id = 'default') {
6607 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6608 -
6609 - // Default global settings
6610 - $default_config = array(
6611 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6612 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6613 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6614 - );
6615 -
6616 - // Allow multi-bot plugin to override with bot-specific settings
6617 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6618 -
6619 - // Preserve max_results from global settings if not set in bot config
6620 - if (!isset($bot_config['max_results'])) {
6621 - $bot_config['max_results'] = $default_config['max_results'];
6622 - }
6623 -
6624 - return $bot_config;
6625 -}
6626 -
6627 4044 private function mxchat_find_relevant_products($user_embedding) {
6628 4045 //error_log('MXChat Vector Search: Starting product search...');
6629 4046
6630 4047 // Retrieve the add-on settings from the database
@@ -6645,75 +4062,73 @@
6645 4062 }
6646 4063 private function find_relevant_products_wordpress($user_embedding) {
6647 4064 global $wpdb;
6648 4065 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
4066 + $cache_key = 'mxchat_system_prompt_embeddings';
4067 + $batch_size = 500;
6649 4068
6650 - if (!is_array($user_embedding)) {
6651 - return '';
6652 - }
4069 + // Original WordPress database search logic
4070 + // [Previous implementation remains the same]
4071 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
4072 + if ($embeddings === false) {
4073 + $embeddings = [];
4074 + $offset = 0;
6653 4075
6654 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6655 - // results above the similarity threshold. Peak memory is bounded by
6656 - // $batch_size embedding rows plus a 3-element top list.
6657 - $batch_size = 250;
6658 - $similarity_threshold = 0.85;
6659 - $top_k = 3;
6660 - $top_results = [];
6661 - $offset = 0;
4076 + do {
4077 + $query = $wpdb->prepare(
4078 + "SELECT id, embedding_vector
4079 + FROM {$system_prompt_table}
4080 + LIMIT %d OFFSET %d",
4081 + $batch_size,
4082 + $offset
4083 + );
6662 4084
6663 - do {
6664 - $batch = $wpdb->get_results($wpdb->prepare(
6665 - "SELECT id, embedding_vector
6666 - FROM {$system_prompt_table}
6667 - LIMIT %d OFFSET %d",
6668 - $batch_size,
6669 - $offset
6670 - ));
4085 + $batch = $wpdb->get_results($query);
4086 + if (empty($batch)) {
4087 + break;
4088 + }
6671 4089
6672 - if (empty($batch)) {
6673 - break;
6674 - }
4090 + $embeddings = array_merge($embeddings, $batch);
4091 + $offset += $batch_size;
6675 4092
6676 - foreach ($batch as $row) {
6677 - $database_embedding = $row->embedding_vector
6678 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6679 - : null;
4093 + unset($batch);
6680 4094
6681 - if (!is_array($database_embedding)) {
6682 - unset($database_embedding);
6683 - continue;
6684 - }
4095 + } while (true);
6685 4096
4097 + if (empty($embeddings)) {
4098 + return '';
4099 + }
4100 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
4101 + }
4102 +
4103 + $relevant_results = [];
4104 + foreach ($embeddings as $embedding) {
4105 + $database_embedding = $embedding->embedding_vector
4106 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
4107 + : null;
4108 + if (is_array($database_embedding) && is_array($user_embedding)) {
6686 4109 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6687 - unset($database_embedding);
6688 -
6689 - if ($similarity < $similarity_threshold) {
6690 - continue;
6691 - }
6692 -
6693 - // Insert into bounded top-K (kept sorted descending)
6694 - if (count($top_results) < $top_k) {
6695 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6696 - usort($top_results, function ($a, $b) {
6697 - return $b['similarity'] <=> $a['similarity'];
6698 - });
6699 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6700 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6701 - usort($top_results, function ($a, $b) {
6702 - return $b['similarity'] <=> $a['similarity'];
6703 - });
6704 - }
4110 + $relevant_results[] = [
4111 + 'id' => $embedding->id,
4112 + 'similarity' => $similarity
4113 + ];
6705 4114 }
4115 + unset($database_embedding);
4116 + }
6706 4117
6707 - unset($batch);
6708 - $offset += $batch_size;
6709 - } while (true);
4118 + // Use fixed threshold for products
4119 + $similarity_threshold = 0.85;
6710 4120
6711 - if (empty($top_results)) {
6712 - return '';
6713 - }
4121 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
4122 + return $result['similarity'] >= $similarity_threshold;
4123 + });
4124 + usort($relevant_results, function ($a, $b) {
4125 + return $b['similarity'] <=> $a['similarity'];
4126 + });
6714 4127
4128 + $top_results = array_slice($relevant_results, 0, 5);
6715 4129 $content = '';
4130 +
6716 4131 foreach ($top_results as $result) {
6717 4132 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6718 4133 $content .= $chunk_content . "\n\n";
6719 4134 }
@@ -6822,61 +4237,23 @@
6822 4237
6823 4238 /**
6824 4239 * Get system instructions for a specific bot or default
6825 4240 * Checks for multi-bot add-on and uses bot-specific instructions if available
6826 - * Automatically strips URLs if citation links are disabled
6827 - * Replaces {visitor_name} placeholder with actual visitor name if available
6828 - *
6829 - * @param string $bot_id The bot ID to get instructions for
6830 - * @param string $session_id Optional session ID to lookup visitor name
6831 4241 */
6832 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6833 - $instructions = '';
6834 -
4242 +private function get_system_instructions($bot_id = 'default') {
6835 4243 // Check if multi-bot add-on is active
6836 4244 if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6837 4245 // Get bot-specific options from multi-bot add-on
6838 4246 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6839 -
4247 +
6840 4248 // If bot has custom system instructions, use those
6841 4249 if (!empty($bot_options['system_prompt_instructions'])) {
6842 - $instructions = $bot_options['system_prompt_instructions'];
4250 + return $bot_options['system_prompt_instructions'];
6843 4251 }
6844 4252 }
6845 -
4253 +
6846 4254 // Fall back to default system instructions
6847 - if (empty($instructions)) {
6848 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6849 - }
6850 -
6851 - // Check if citation links are disabled - if so, strip URLs from instructions
6852 - $fresh_options = get_option('mxchat_options', []);
6853 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6854 -
6855 - if (!$citation_links_enabled && !empty($instructions)) {
6856 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
6857 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
6858 - }
6859 -
6860 - // Replace {visitor_name} placeholder with actual visitor name if available
6861 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
6862 - $name_option_key = "mxchat_name_{$session_id}";
6863 - $visitor_name = get_option($name_option_key, '');
6864 -
6865 - if (!empty($visitor_name)) {
6866 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
6867 - } else {
6868 - // Remove placeholder if no name is available
6869 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
6870 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
6871 - }
6872 - }
6873 -
6874 - // Allow developers to filter system instructions and process shortcodes
6875 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
6876 - $instructions = do_shortcode($instructions);
6877 -
6878 - return $instructions;
4255 + return isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6879 4256 }
6880 4257 /**
6881 4258 * Get the current bot ID from session or request context
6882 4259 */
@@ -6896,9 +4273,9 @@
6896 4273
6897 4274 // Fall back to default
6898 4275 return 'default';
6899 4276 }
6900 -private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $openrouter_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-5.1-chat-latest') {
4277 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-4o') {
6901 4278 try {
6902 4279 if (!$relevant_content) {
6903 4280 $error_response = [
6904 4281 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
@@ -6904,73 +4281,22 @@
6904 4281 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
6905 4282 'error_code' => 'no_relevant_content'
6906 4283 ];
6907 4284
4285 + // Add testing data to error response if available
6908 4286 if ($testing_data !== null) {
6909 4287 $error_response['testing_data'] = $testing_data;
4288 + //error_log("MxChat Testing: Added testing data to no_relevant_content error");
6910 4289 }
6911 4290
6912 4291 return $error_response;
6913 4292 }
6914 4293
4294 + // Ensure conversation_history is an array
6915 4295 if (!is_array($conversation_history)) {
6916 4296 $conversation_history = array();
6917 4297 }
6918 4298
6919 - // Check if this is an OpenRouter model
6920 - if ($selected_model === 'openrouter') {
6921 - // Get the actual OpenRouter model from options
6922 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
6923 -
6924 - if (empty($openrouter_selected_model)) {
6925 - $error_response = [
6926 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
6927 - 'error_code' => 'no_openrouter_model_selected'
6928 - ];
6929 - if ($testing_data !== null) {
6930 - $error_response['testing_data'] = $testing_data;
6931 - }
6932 - return $error_response;
6933 - }
6934 -
6935 - if (empty($openrouter_api_key)) {
6936 - $error_response = [
6937 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
6938 - 'error_code' => 'missing_openrouter_api_key'
6939 - ];
6940 - if ($testing_data !== null) {
6941 - $error_response['testing_data'] = $testing_data;
6942 - }
6943 - return $error_response;
6944 - }
6945 -
6946 - if ($streaming) {
6947 - return $this->mxchat_generate_response_openrouter_stream(
6948 - $openrouter_selected_model,
6949 - $openrouter_api_key,
6950 - $conversation_history,
6951 - $relevant_content,
6952 - $session_id,
6953 - $testing_data
6954 - );
6955 - } else {
6956 - $response = $this->mxchat_generate_response_openrouter(
6957 - $openrouter_selected_model,
6958 - $openrouter_api_key,
6959 - $conversation_history,
6960 - $relevant_content
6961 - );
6962 - }
6963 -
6964 - if (is_array($response) && isset($response['error'])) {
6965 - if ($testing_data !== null) {
6966 - $response['testing_data'] = $testing_data;
6967 - }
6968 - return $response;
6969 - }
6970 -
6971 - return $response;
6972 - }
6973 4299
6974 4300 // Extract model prefix to determine the provider
6975 4301 $model_parts = explode('-', $selected_model);
6976 4302 $provider = strtolower($model_parts[0]);
@@ -7013,9 +4339,9 @@
7013 4339 $claude_api_key,
7014 4340 $conversation_history,
7015 4341 $relevant_content,
7016 4342 $session_id,
7017 - $testing_data
4343 + $testing_data // Pass testing data
7018 4344 );
7019 4345 } else {
7020 4346 $response = $this->mxchat_generate_response_claude(
7021 4347 $selected_model,
@@ -7043,9 +4369,9 @@
7043 4369 $xai_api_key,
7044 4370 $conversation_history,
7045 4371 $relevant_content,
7046 4372 $session_id,
7047 - $testing_data
4373 + $testing_data // Pass testing data
7048 4374 );
7049 4375 } else {
7050 4376 $response = $this->mxchat_generate_response_xai(
7051 4377 $selected_model,
@@ -7073,9 +4399,9 @@
7073 4399 $deepseek_api_key,
7074 4400 $conversation_history,
7075 4401 $relevant_content,
7076 4402 $session_id,
7077 - $testing_data
4403 + $testing_data // Pass testing data
7078 4404 );
7079 4405 } else {
7080 4406 $response = $this->mxchat_generate_response_deepseek(
7081 4407 $selected_model,
@@ -7085,38 +4411,8 @@
7085 4411 );
7086 4412 }
7087 4413 break;
7088 4414
7089 - case 'custom':
7090 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7091 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7092 - if (empty($cp_base_url)) {
7093 - $error_response = [
7094 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7095 - 'error_code' => 'missing_custom_provider_base_url'
7096 - ];
7097 - if ($testing_data !== null) {
7098 - $error_response['testing_data'] = $testing_data;
7099 - }
7100 - return $error_response;
7101 - }
7102 - if ($streaming) {
7103 - return $this->mxchat_generate_response_custom_stream(
7104 - $selected_model,
7105 - $conversation_history,
7106 - $relevant_content,
7107 - $session_id,
7108 - $testing_data
7109 - );
7110 - } else {
7111 - $response = $this->mxchat_generate_response_custom(
7112 - $selected_model,
7113 - $conversation_history,
7114 - $relevant_content
7115 - );
7116 - }
7117 - break;
7118 -
7119 4415 case 'gpt':
7120 4416 case 'o1':
7121 4417 if (empty($api_key)) {
7122 4418 $error_response = [
@@ -7127,27 +4423,9 @@
7127 4423 $error_response['testing_data'] = $testing_data;
7128 4424 }
7129 4425 return $error_response;
7130 4426 }
7131 -
7132 - // Check if web search is enabled for this OpenAI model
7133 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7134 - // Models that don't support web search
7135 - $unsupported_web_search_models = array('gpt-4.1-nano');
7136 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7137 -
7138 - if ($web_search_enabled && $model_supports_web_search) {
7139 - // Use Responses API (required for some models, or when web search is enabled)
7140 - return $this->mxchat_generate_response_openai_web_search(
7141 - $selected_model,
7142 - $api_key,
7143 - $conversation_history,
7144 - $relevant_content,
7145 - $session_id,
7146 - $testing_data,
7147 - $streaming
7148 - );
7149 - } elseif ($streaming) {
4427 + if ($streaming) {
7150 4428 return $this->mxchat_generate_response_openai_stream(
7151 4429 $selected_model,
7152 4430 $api_key,
7153 4431 $conversation_history,
@@ -7152,9 +4430,9 @@
7152 4430 $api_key,
7153 4431 $conversation_history,
7154 4432 $relevant_content,
7155 4433 $session_id,
7156 - $testing_data
4434 + $testing_data // Pass testing data
7157 4435 );
7158 4436 } else {
7159 4437 $response = $this->mxchat_generate_response_openai(
7160 4438 $selected_model,
@@ -7165,8 +4443,9 @@
7165 4443 }
7166 4444 break;
7167 4445
7168 4446 default:
4447 + // Default to OpenAI for custom models or unrecognized prefixes
7169 4448 if (empty($api_key)) {
7170 4449 $error_response = [
7171 4450 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7172 4451 'error_code' => 'missing_openai_api_key'
@@ -7175,25 +4454,9 @@
7175 4454 $error_response['testing_data'] = $testing_data;
7176 4455 }
7177 4456 return $error_response;
7178 4457 }
7179 -
7180 - // Check if web search is enabled (default case also handles OpenAI models)
7181 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7182 - $unsupported_web_search_models = array('gpt-4.1-nano');
7183 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7184 -
7185 - if ($web_search_enabled && $model_supports_web_search) {
7186 - return $this->mxchat_generate_response_openai_web_search(
7187 - $selected_model,
7188 - $api_key,
7189 - $conversation_history,
7190 - $relevant_content,
7191 - $session_id,
7192 - $testing_data,
7193 - $streaming
7194 - );
7195 - } elseif ($streaming) {
4458 + if ($streaming) {
7196 4459 return $this->mxchat_generate_response_openai_stream(
7197 4460 $selected_model,
7198 4461 $api_key,
7199 4462 $conversation_history,
@@ -7198,9 +4461,9 @@
7198 4461 $api_key,
7199 4462 $conversation_history,
7200 4463 $relevant_content,
7201 4464 $session_id,
7202 - $testing_data
4465 + $testing_data // Pass testing data
7203 4466 );
7204 4467 } else {
7205 4468 $response = $this->mxchat_generate_response_openai(
7206 4469 $selected_model,
@@ -7211,18 +4474,24 @@
7211 4474 }
7212 4475 break;
7213 4476 }
7214 4477
4478 + // Check if the response is an error array from the provider-specific function
7215 4479 if (is_array($response) && isset($response['error'])) {
4480 + // Add testing data to error response if available
7216 4481 if ($testing_data !== null) {
7217 4482 $response['testing_data'] = $testing_data;
4483 + //error_log("MxChat Testing: Added testing data to provider error response");
7218 4484 }
7219 - return $response;
4485 + return $response; // Pass through the error with testing data
7220 4486 }
7221 4487
4488 + // For successful non-streaming responses, we don't add testing data here
4489 + // because it will be added in the main handler
7222 4490 return $response;
7223 4491
7224 4492 } catch (Exception $e) {
4493 + //error_log('MXChat Error: ' . $e->getMessage());
7225 4494 $error_response = [
7226 4495 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7227 4496 'error_code' => 'system_exception',
7228 4497 'exception_details' => $e->getMessage()
@@ -7227,24 +4496,31 @@
7227 4496 'error_code' => 'system_exception',
7228 4497 'exception_details' => $e->getMessage()
7229 4498 ];
7230 4499
4500 + // Add testing data to exception response if available
7231 4501 if ($testing_data !== null) {
7232 4502 $error_response['testing_data'] = $testing_data;
4503 + //error_log("MxChat Testing: Added testing data to exception response");
7233 4504 }
7234 4505
7235 4506 return $error_response;
7236 4507 }
7237 4508 }
7238 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4509 +
4510 +private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7239 4511 try {
7240 4512 $bot_id = $this->get_current_bot_id($session_id);
7241 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7242 4513
4514 + // Get system prompt instructions using centralized function
4515 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
4516 +
4517 + // Ensure conversation_history is an array
7243 4518 if (!is_array($conversation_history)) {
7244 4519 $conversation_history = array();
7245 4520 }
7246 4521
4522 + // Format conversation history for OpenAI
7247 4523 $formatted_conversation = array();
7248 4524
7249 4525 $formatted_conversation[] = array(
7250 4526 'role' => 'system',
@@ -7256,9 +4532,9 @@
7256 4532 $role = $message['role'];
7257 4533 if ($role === 'bot' || $role === 'agent') {
7258 4534 $role = 'assistant';
7259 4535 }
7260 - if (!in_array($role, ['system', 'assistant', 'user'])) {
4536 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7261 4537 $role = 'user';
7262 4538 }
7263 4539 $formatted_conversation[] = array(
7264 4540 'role' => $role,
@@ -7266,21 +4542,18 @@
7266 4542 );
7267 4543 }
7268 4544 }
7269 4545
4546 + // Check if we can actually stream
7270 4547 if (headers_sent() || !function_exists('curl_init')) {
7271 - $regular_response = $this->mxchat_generate_response_openrouter(
4548 + // Fallback to regular response with testing data
4549 + $regular_response = $this->mxchat_generate_response_openai(
7272 4550 $selected_model,
7273 - $openrouter_api_key,
4551 + $api_key,
7274 4552 $conversation_history,
7275 4553 $relevant_content
7276 4554 );
7277 4555
7278 - // Save bot response to transcript
7279 - if (!empty($regular_response) && !empty($session_id)) {
7280 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7281 - }
7282 -
7283 4556 $response_data = [
7284 4557 'text' => $regular_response,
7285 4558 'html' => '',
7286 4559 'session_id' => $session_id
@@ -7294,8 +4567,9 @@
7294 4567 echo json_encode($response_data);
7295 4568 return true;
7296 4569 }
7297 4570
4571 + // Prepare the request body with stream: true
7298 4572 $body = json_encode([
7299 4573 'model' => $selected_model,
7300 4574 'messages' => $formatted_conversation,
7301 4575 'temperature' => 1,
@@ -7301,213 +4575,85 @@
7301 4575 'temperature' => 1,
7302 4576 'stream' => true
7303 4577 ]);
7304 4578
7305 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
7306 - // inside WRITEFUNCTION on first byte of a successful upstream.
7307 -
7308 - $captured_status_code = 0;
7309 - $captured_body_pre_stream = '';
7310 - $full_response = '';
4579 + // Use cURL for streaming support
4580 + $ch = curl_init();
4581 + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
4582 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4583 + curl_setopt($ch, CURLOPT_POST, true);
4584 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4585 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4586 + 'Content-Type: application/json',
4587 + 'Authorization: Bearer ' . $api_key
4588 + ));
4589 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4590 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4591 +
4592 + $full_response = ''; // Accumulate full response for saving
7311 4593 $stream_started = false;
7312 - $buffer = '';
7313 - $errno = 0;
7314 - $last_curl_error = '';
7315 - $http_code = 0;
7316 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7317 - $backoff_ms = array(0, 750, 2000);
7318 -
7319 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7320 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7321 - usleep($backoff_ms[$attempt] * 1000);
4594 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
4595 +
4596 + // Buffer control for real-time streaming
4597 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
4598 + // Send testing data as the first event if available
4599 + if (!$stream_started && $testing_data !== null) {
4600 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4601 + flush();
4602 + $stream_started = true;
7322 4603 }
7323 -
7324 - $captured_status_code = 0;
7325 - $captured_body_pre_stream = '';
7326 - $full_response = '';
7327 - $stream_started = false;
7328 - $buffer = '';
7329 -
7330 - $ch = curl_init();
7331 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
7332 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7333 - curl_setopt($ch, CURLOPT_POST, true);
7334 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7335 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7336 - 'Content-Type: application/json',
7337 - 'Authorization: Bearer ' . $openrouter_api_key,
7338 - 'HTTP-Referer: ' . home_url(),
7339 - 'X-Title: ' . get_bloginfo('name')
7340 - ));
7341 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7342 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7343 -
7344 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7345 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7346 - $captured_status_code = (int) $m[1];
4604 +
4605 + // CRITICAL FIX: Append new data to buffer
4606 + $buffer .= $data;
4607 +
4608 + // Process complete lines only
4609 + $lines = explode("\n", $buffer);
4610 +
4611 + // CRITICAL FIX: Keep the last incomplete line in the buffer
4612 + // The last element might be incomplete, so keep it in buffer
4613 + $buffer = array_pop($lines);
4614 +
4615 + foreach ($lines as $line) {
4616 + // Skip empty lines
4617 + if (trim($line) === '') {
4618 + continue;
7347 4619 }
7348 - return strlen($header);
7349 - });
7350 -
7351 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7352 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7353 - $captured_body_pre_stream .= $data;
7354 - return strlen($data);
4620 +
4621 + // Only process lines that start with "data: "
4622 + if (strpos($line, 'data: ') !== 0) {
4623 + continue;
7355 4624 }
7356 -
7357 - if (!$this->streaming_headers_sent) {
7358 - $this->setup_streaming_headers();
4625 +
4626 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4627 +
4628 + if (trim($json_str) === '[DONE]') {
4629 + echo "data: [DONE]\n\n";
4630 + flush();
4631 + continue;
7359 4632 }
7360 -
7361 - if (!$stream_started && $testing_data !== null) {
7362 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4633 +
4634 + // Try to decode JSON
4635 + $json = json_decode(trim($json_str), true);
4636 + if ($json && isset($json['choices'][0]['delta']['content'])) {
4637 + $content = $json['choices'][0]['delta']['content'];
4638 + $full_response .= $content; // Accumulate the full response
4639 +
4640 + // Send as SSE format
4641 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
7363 4642 flush();
7364 - $stream_started = true;
7365 4643 }
7366 -
7367 - $buffer .= $data;
7368 - $lines = explode("\n", $buffer);
7369 - $buffer = array_pop($lines);
7370 -
7371 - foreach ($lines as $line) {
7372 - if (trim($line) === '') {
7373 - continue;
7374 - }
7375 - if (strpos($line, 'data: ') !== 0) {
7376 - continue;
7377 - }
7378 -
7379 - $json_str = substr($line, 6);
7380 -
7381 - if (trim($json_str) === '[DONE]') {
7382 - echo "data: [DONE]\n\n";
7383 - flush();
7384 - continue;
7385 - }
7386 -
7387 - $json = json_decode(trim($json_str), true);
7388 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7389 - $content = $json['choices'][0]['delta']['content'];
7390 - $full_response .= $content;
7391 -
7392 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7393 - flush();
7394 - }
7395 - }
7396 -
7397 - return strlen($data);
7398 - });
7399 -
7400 - $response = curl_exec($ch);
7401 - $errno = curl_errno($ch);
7402 - $last_curl_error = curl_error($ch);
7403 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7404 - curl_close($ch);
7405 -
7406 - if (!$errno && $http_code === 200) {
7407 - break;
7408 4644 }
7409 -
7410 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7411 - $can_retry = !$this->streaming_headers_sent
7412 - && ($attempt + 1) < $max_attempts
7413 - && $is_transient;
7414 -
7415 - if (defined('WP_DEBUG') && WP_DEBUG) {
7416 - error_log(sprintf(
7417 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7418 - $attempt + 1, $max_attempts, $http_code, $errno,
7419 - $is_transient ? 'yes' : 'no',
7420 - $can_retry ? 'Retrying.' : 'Giving up.'
7421 - ));
7422 - }
7423 -
7424 - if (!$can_retry) {
7425 - break;
7426 - }
7427 - }
7428 -
7429 - if (!$errno && $http_code === 200) {
7430 - if (!empty($full_response) && !empty($session_id)) {
7431 - $rag_context_for_storage = null;
7432 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7433 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7434 -
7435 - if ($has_rag_data || $has_action_data) {
7436 - $rag_context_for_storage = [];
7437 -
7438 - if ($has_rag_data) {
7439 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7440 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7441 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7442 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7443 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7444 - }
7445 -
7446 - if ($has_action_data) {
7447 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7448 - }
7449 - }
7450 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7451 - }
7452 - return true;
7453 - }
7454 -
7455 - return $this->mxchat_stream_emit_fallback(
7456 - 'openai',
7457 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7458 - $session_id,
7459 - $testing_data
7460 - );
7461 -
7462 - } catch (Exception $e) {
7463 - return $this->mxchat_stream_emit_fallback(
7464 - 'openai',
7465 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7466 - $session_id,
7467 - $testing_data
7468 - );
7469 - }
7470 -}
7471 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7472 - try {
7473 - $bot_id = $this->get_current_bot_id($session_id);
4645 +
4646 + return strlen($data);
4647 + });
7474 4648
7475 - // Get system prompt instructions using centralized function
7476 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4649 + $response = curl_exec($ch);
4650 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7477 4651
7478 - // Ensure conversation_history is an array
7479 - if (!is_array($conversation_history)) {
7480 - $conversation_history = array();
7481 - }
7482 -
7483 - // Format conversation history for OpenAI
7484 - $formatted_conversation = array();
7485 -
7486 - $formatted_conversation[] = array(
7487 - 'role' => 'system',
7488 - 'content' => $system_prompt_instructions . " " . $relevant_content
7489 - );
7490 -
7491 - foreach ($conversation_history as $message) {
7492 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7493 - $role = $message['role'];
7494 - if ($role === 'bot' || $role === 'agent') {
7495 - $role = 'assistant';
7496 - }
7497 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7498 - $role = 'user';
7499 - }
7500 - $formatted_conversation[] = array(
7501 - 'role' => $role,
7502 - 'content' => $message['content']
7503 - );
7504 - }
7505 - }
7506 -
7507 - // Check if we can actually stream
7508 - if (headers_sent() || !function_exists('curl_init')) {
7509 - // Fallback to regular response with testing data
4652 + if (curl_errno($ch) || $http_code !== 200) {
4653 + curl_close($ch);
4654 +
4655 + // Fallback to regular response
7510 4656 $regular_response = $this->mxchat_generate_response_openai(
7511 4657 $selected_model,
7512 4658 $api_key,
7513 4659 $conversation_history,
@@ -7513,13 +4659,8 @@
7513 4659 $conversation_history,
7514 4660 $relevant_content
7515 4661 );
7516 4662
7517 - // Save bot response to transcript
7518 - if (!empty($regular_response) && !empty($session_id)) {
7519 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7520 - }
7521 -
7522 4663 $response_data = [
7523 4664 'text' => $regular_response,
7524 4665 'html' => '',
7525 4666 'session_id' => $session_id
@@ -7532,905 +4673,42 @@
7532 4673 header('Content-Type: application/json');
7533 4674 echo json_encode($response_data);
7534 4675 return true;
7535 4676 }
7536 -
7537 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
7538 - $is_gpt5_model = (
7539 - strpos($selected_model, 'gpt-5') === 0 ||
7540 - $selected_model === 'gpt-5.2' ||
7541 - $selected_model === 'gpt-5.1-2025-11-13' ||
7542 - $selected_model === 'gpt-5' ||
7543 - $selected_model === 'gpt-5-mini' ||
7544 - $selected_model === 'gpt-5-nano'
7545 - );
7546 -
7547 - // Build request body with optimal settings for fast streaming
7548 - $request_body = [
7549 - 'model' => $selected_model,
7550 - 'messages' => $formatted_conversation,
7551 - 'temperature' => 1,
7552 - 'stream' => true
7553 - ];
7554 -
7555 - // Add reasoning_effort only for GPT-5 models that support it
7556 - // These chat models don't support reasoning_effort parameter
7557 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
7558 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
7559 - // GPT-5.1 uses 'low' instead of 'minimal'
7560 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7561 - $request_body['reasoning_effort'] = 'low';
7562 - } elseif ($selected_model === 'gpt-5.5') {
7563 - $request_body['reasoning_effort'] = 'none';
7564 - } elseif ($selected_model === 'gpt-5.4') {
7565 - $request_body['reasoning_effort'] = 'none';
7566 - } else {
7567 - $request_body['reasoning_effort'] = 'minimal';
7568 - }
4677 +
4678 + curl_close($ch);
4679 +
4680 + // Save the complete response to maintain chat persistence
4681 + if (!empty($full_response) && !empty($session_id)) {
4682 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
7569 4683 }
7570 -
7571 - $body = json_encode($request_body);
7572 -
7573 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
7574 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
7575 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
7576 -
7577 - $captured_status_code = 0;
7578 - $captured_body_pre_stream = '';
7579 - $full_response = '';
7580 - $stream_started = false;
7581 - $buffer = '';
7582 - $errno = 0;
7583 - $last_curl_error = '';
7584 - $http_code = 0;
7585 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7586 - $backoff_ms = array(0, 750, 2000);
7587 -
7588 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7589 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7590 - usleep($backoff_ms[$attempt] * 1000);
7591 - }
7592 -
7593 - // Reset per-attempt capture state.
7594 - $captured_status_code = 0;
7595 - $captured_body_pre_stream = '';
7596 - $full_response = '';
7597 - $stream_started = false;
7598 - $buffer = '';
7599 -
7600 - $ch = curl_init();
7601 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
7602 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7603 - curl_setopt($ch, CURLOPT_POST, true);
7604 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7605 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7606 - 'Content-Type: application/json',
7607 - 'Authorization: Bearer ' . $api_key
7608 - ));
7609 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7610 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7611 -
7612 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
7613 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7614 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7615 - $captured_status_code = (int) $m[1];
7616 - }
7617 - return strlen($header);
7618 - });
7619 -
7620 - // Buffer control for real-time streaming
7621 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7622 - // V2 guard: if upstream returned non-200, buffer body for transient
7623 - // classification and DO NOT emit to client. Stream channel must NOT open.
7624 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7625 - $captured_body_pre_stream .= $data;
7626 - return strlen($data);
7627 - }
7628 -
7629 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
7630 - // After this point streaming_headers_sent === true → retry is structurally blocked.
7631 - if (!$this->streaming_headers_sent) {
7632 - $this->setup_streaming_headers();
7633 - }
7634 -
7635 - // Send testing data as the first event if available
7636 - if (!$stream_started && $testing_data !== null) {
7637 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7638 - flush();
7639 - $stream_started = true;
7640 - }
7641 -
7642 - // CRITICAL FIX: Append new data to buffer
7643 - $buffer .= $data;
7644 -
7645 - // Process complete lines only
7646 - $lines = explode("\n", $buffer);
7647 -
7648 - // CRITICAL FIX: Keep the last incomplete line in the buffer
7649 - $buffer = array_pop($lines);
7650 -
7651 - foreach ($lines as $line) {
7652 - if (trim($line) === '') {
7653 - continue;
7654 - }
7655 - if (strpos($line, 'data: ') !== 0) {
7656 - continue;
7657 - }
7658 -
7659 - $json_str = substr($line, 6);
7660 -
7661 - if (trim($json_str) === '[DONE]') {
7662 - echo "data: [DONE]\n\n";
7663 - flush();
7664 - continue;
7665 - }
7666 -
7667 - $json = json_decode(trim($json_str), true);
7668 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7669 - $content = $json['choices'][0]['delta']['content'];
7670 - $full_response .= $content;
7671 -
7672 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7673 - flush();
7674 - }
7675 - }
7676 -
7677 - return strlen($data);
7678 - });
7679 -
7680 - $response = curl_exec($ch);
7681 - $errno = curl_errno($ch);
7682 - $last_curl_error = curl_error($ch);
7683 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7684 - curl_close($ch);
7685 -
7686 - if (!$errno && $http_code === 200) {
7687 - break; // Happy path — WRITEFUNCTION already streamed everything.
7688 - }
7689 -
7690 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7691 - $can_retry = !$this->streaming_headers_sent
7692 - && ($attempt + 1) < $max_attempts
7693 - && $is_transient;
7694 -
7695 - if (defined('WP_DEBUG') && WP_DEBUG) {
7696 - error_log(sprintf(
7697 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7698 - $attempt + 1, $max_attempts, $http_code, $errno,
7699 - $is_transient ? 'yes' : 'no',
7700 - $can_retry ? 'Retrying.' : 'Giving up.'
7701 - ));
7702 - }
7703 -
7704 - if (!$can_retry) {
7705 - break;
7706 - }
7707 - }
7708 -
7709 - // Post-loop branch.
7710 - if (!$errno && $http_code === 200) {
7711 - // Happy path — save the complete response to maintain chat persistence.
7712 - if (!empty($full_response) && !empty($session_id)) {
7713 - $rag_context_for_storage = null;
7714 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7715 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7716 -
7717 - if ($has_rag_data || $has_action_data) {
7718 - $rag_context_for_storage = [];
7719 -
7720 - if ($has_rag_data) {
7721 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7722 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7723 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7724 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7725 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7726 - }
7727 -
7728 - if ($has_action_data) {
7729 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7730 - }
7731 - }
7732 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7733 - }
7734 -
7735 - return true;
7736 - }
7737 -
7738 - // Failure path — branch on whether SSE channel was opened.
7739 - return $this->mxchat_stream_emit_fallback(
7740 - 'openai',
7741 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7742 - $session_id,
7743 - $testing_data
7744 - );
7745 -
4684 +
4685 + return true; // Indicate streaming completed successfully
4686 +
7746 4687 } catch (Exception $e) {
7747 - return $this->mxchat_stream_emit_fallback(
7748 - 'openai',
7749 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7750 - $session_id,
7751 - $testing_data
4688 + // Fallback to regular response
4689 + $regular_response = $this->mxchat_generate_response_openai(
4690 + $selected_model,
4691 + $api_key,
4692 + $conversation_history,
4693 + $relevant_content
7752 4694 );
7753 - }
7754 -}
7755 -
7756 -/**
7757 - * Shared fallback emitter for streaming chat functions. Two outcomes:
7758 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
7759 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
7760 - * normal bot bubble. Transcript row is persisted.
7761 - * - streaming_headers_sent === false: SSE channel never opened (retries
7762 - * exhausted on initial connect). Emit a clean JSON response — the path
7763 - * the widget would normally hit if streaming wasn't even attempted.
7764 - *
7765 - * Used by all six *_stream functions after their per-attempt retry loop.
7766 - */
7767 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
7768 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
7769 -
7770 - if ($this->streaming_headers_sent) {
7771 - if ($is_error_array) {
7772 - echo "data: " . json_encode([
7773 - 'error' => true,
7774 - 'error_message' => $regular_response['error'],
7775 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7776 - 'text' => $regular_response['error'],
7777 - 'message' => $regular_response['error']
7778 - ]) . "\n\n";
7779 - echo "data: [DONE]\n\n";
7780 - flush();
7781 - return true;
4695 +
4696 + $response_data = [
4697 + 'text' => $regular_response,
4698 + 'html' => '',
4699 + 'session_id' => $session_id
4700 + ];
4701 +
4702 + if ($testing_data !== null) {
4703 + $response_data['testing_data'] = $testing_data;
7782 4704 }
7783 - $fallback_message = (string) $regular_response;
7784 - if (!empty($fallback_message) && !empty($session_id)) {
7785 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7786 - }
7787 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
7788 - echo "data: [DONE]\n\n";
7789 - flush();
7790 - return true;
7791 - }
7792 -
7793 - // SSE channel never opened — clean JSON fallback.
7794 - if ($is_error_array) {
4705 +
7795 4706 header('Content-Type: application/json');
7796 - echo json_encode(array(
7797 - 'error' => true,
7798 - 'error_message' => $regular_response['error'],
7799 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7800 - 'text' => $regular_response['error'],
7801 - 'message' => $regular_response['error'],
7802 - ));
4707 + echo json_encode($response_data);
7803 4708 return true;
7804 4709 }
7805 -
7806 - $fallback_message = (string) $regular_response;
7807 - if (!empty($fallback_message) && !empty($session_id)) {
7808 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7809 - }
7810 - $response_data = array(
7811 - 'text' => $fallback_message,
7812 - 'html' => '',
7813 - 'session_id' => $session_id,
7814 - );
7815 - if ($testing_data !== null) {
7816 - $response_data['testing_data'] = $testing_data;
7817 - }
7818 - header('Content-Type: application/json');
7819 - echo json_encode($response_data);
7820 - return true;
7821 4710 }
7822 -
7823 -/**
7824 - * Resolve custom (OpenAI-compatible) provider config from settings.
7825 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
7826 - */
7827 -private function mxchat_resolve_custom_provider() {
7828 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
7829 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
7830 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
7831 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
7832 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
7833 -
7834 - $chat_url = $base_url . '/chat/completions';
7835 - if (!empty($api_version)) {
7836 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
7837 - }
7838 -
7839 - $headers = array('Content-Type: application/json');
7840 - if (!empty($api_key)) {
7841 - if ($auth_scheme === 'api-key') {
7842 - $headers[] = 'api-key: ' . $api_key;
7843 - } else {
7844 - $headers[] = 'Authorization: Bearer ' . $api_key;
7845 - }
7846 - }
7847 -
7848 - return array(
7849 - 'base_url' => $base_url,
7850 - 'api_key' => $api_key,
7851 - 'model' => $model !== '' ? $model : 'default',
7852 - 'auth_scheme' => $auth_scheme,
7853 - 'api_version' => $api_version,
7854 - 'chat_url' => $chat_url,
7855 - 'headers' => $headers,
7856 - );
7857 -}
7858 -
7859 -/**
7860 - * Streaming chat completion against an OpenAI-compatible custom provider
7861 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
7862 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
7863 - */
7864 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7865 - try {
7866 - $cfg = $this->mxchat_resolve_custom_provider();
7867 - if (empty($cfg['base_url'])) {
7868 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7869 - }
7870 -
7871 - $bot_id = $this->get_current_bot_id($session_id);
7872 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7873 - if (!is_array($conversation_history)) {
7874 - $conversation_history = array();
7875 - }
7876 -
7877 - $formatted_conversation = array();
7878 - $formatted_conversation[] = array(
7879 - 'role' => 'system',
7880 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7881 - );
7882 - foreach ($conversation_history as $message) {
7883 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7884 - $role = $message['role'];
7885 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7886 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7887 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
7888 - }
7889 - }
7890 -
7891 - if (headers_sent() || !function_exists('curl_init')) {
7892 - // No streaming capability — fall through to non-stream wrapper
7893 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7894 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
7895 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
7896 - }
7897 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7898 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7899 - header('Content-Type: application/json');
7900 - echo json_encode($response_data);
7901 - return true;
7902 - }
7903 -
7904 - $request_body = array(
7905 - 'model' => $cfg['model'],
7906 - 'messages' => $formatted_conversation,
7907 - 'stream' => true,
7908 - );
7909 - $body = json_encode($request_body);
7910 -
7911 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
7912 -
7913 - $captured_status_code = 0;
7914 - $captured_body_pre_stream = '';
7915 - $full_response = '';
7916 - $stream_started = false;
7917 - $buffer = '';
7918 - $errno = 0;
7919 - $http_code = 0;
7920 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7921 - $backoff_ms = array(0, 750, 2000);
7922 -
7923 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7924 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7925 - usleep($backoff_ms[$attempt] * 1000);
7926 - }
7927 -
7928 - $captured_status_code = 0;
7929 - $captured_body_pre_stream = '';
7930 - $full_response = '';
7931 - $stream_started = false;
7932 - $buffer = '';
7933 -
7934 - $ch = curl_init();
7935 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
7936 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7937 - curl_setopt($ch, CURLOPT_POST, true);
7938 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7939 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
7940 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7941 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
7942 -
7943 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7944 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7945 - $captured_status_code = (int) $m[1];
7946 - }
7947 - return strlen($header);
7948 - });
7949 -
7950 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7951 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7952 - $captured_body_pre_stream .= $data;
7953 - return strlen($data);
7954 - }
7955 -
7956 - if (!$this->streaming_headers_sent) {
7957 - $this->setup_streaming_headers();
7958 - }
7959 -
7960 - if (!$stream_started && $testing_data !== null) {
7961 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
7962 - flush();
7963 - $stream_started = true;
7964 - }
7965 - $buffer .= $data;
7966 - $lines = explode("\n", $buffer);
7967 - $buffer = array_pop($lines);
7968 - foreach ($lines as $line) {
7969 - if (trim($line) === '') { continue; }
7970 - if (strpos($line, 'data: ') !== 0) { continue; }
7971 - $json_str = substr($line, 6);
7972 - if (trim($json_str) === '[DONE]') {
7973 - echo "data: [DONE]\n\n";
7974 - flush();
7975 - continue;
7976 - }
7977 - $json = json_decode(trim($json_str), true);
7978 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7979 - $content = $json['choices'][0]['delta']['content'];
7980 - $full_response .= $content;
7981 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
7982 - flush();
7983 - }
7984 - }
7985 - return strlen($data);
7986 - });
7987 -
7988 - $response = curl_exec($ch);
7989 - $errno = curl_errno($ch);
7990 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7991 - curl_close($ch);
7992 -
7993 - if (!$errno && $http_code === 200) {
7994 - break;
7995 - }
7996 -
7997 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7998 - $can_retry = !$this->streaming_headers_sent
7999 - && ($attempt + 1) < $max_attempts
8000 - && $is_transient;
8001 -
8002 - if (defined('WP_DEBUG') && WP_DEBUG) {
8003 - error_log(sprintf(
8004 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8005 - $attempt + 1, $max_attempts, $http_code, $errno,
8006 - $is_transient ? 'yes' : 'no',
8007 - $can_retry ? 'Retrying.' : 'Giving up.'
8008 - ));
8009 - }
8010 -
8011 - if (!$can_retry) {
8012 - break;
8013 - }
8014 - }
8015 -
8016 - if (!$errno && $http_code === 200) {
8017 - if (!empty($full_response) && !empty($session_id)) {
8018 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8019 - }
8020 - return true;
8021 - }
8022 -
8023 - return $this->mxchat_stream_emit_fallback(
8024 - 'openai',
8025 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8026 - $session_id,
8027 - $testing_data
8028 - );
8029 -
8030 - } catch (Exception $e) {
8031 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8032 - }
8033 -}
8034 -
8035 -/**
8036 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8037 - * Returns string content on success, array['error'=>...] on failure.
8038 - */
8039 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8040 - $cfg = $this->mxchat_resolve_custom_provider();
8041 - if (empty($cfg['base_url'])) {
8042 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8043 - }
8044 -
8045 - $bot_id = $this->get_current_bot_id(null);
8046 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8047 - if (!is_array($conversation_history)) {
8048 - $conversation_history = array();
8049 - }
8050 -
8051 - $messages = array(array(
8052 - 'role' => 'system',
8053 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8054 - ));
8055 - foreach ($conversation_history as $message) {
8056 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8057 - $role = $message['role'];
8058 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8059 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8060 - $messages[] = array('role' => $role, 'content' => $message['content']);
8061 - }
8062 - }
8063 -
8064 - $headers_assoc = array('Content-Type' => 'application/json');
8065 - if (!empty($cfg['api_key'])) {
8066 - if ($cfg['auth_scheme'] === 'api-key') {
8067 - $headers_assoc['api-key'] = $cfg['api_key'];
8068 - } else {
8069 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8070 - }
8071 - }
8072 -
8073 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8074 - 'headers' => $headers_assoc,
8075 - 'body' => wp_json_encode(array(
8076 - 'model' => $cfg['model'],
8077 - 'messages' => $messages,
8078 - )),
8079 - 'timeout' => 120,
8080 - ), 'openai');
8081 -
8082 - if (is_wp_error($response)) {
8083 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8084 - }
8085 - $code = (int) wp_remote_retrieve_response_code($response);
8086 - if ($code < 200 || $code >= 300) {
8087 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8088 - }
8089 - $body = json_decode(wp_remote_retrieve_body($response), true);
8090 - if (isset($body['choices'][0]['message']['content'])) {
8091 - return (string) $body['choices'][0]['message']['content'];
8092 - }
8093 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8094 -}
8095 -
8096 -/**
8097 - * Generate response using OpenAI Responses API with web search tool
8098 - * This uses the newer Responses API which supports web search functionality
8099 - */
8100 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8101 - try {
8102 - $bot_id = $this->get_current_bot_id($session_id);
8103 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8104 -
8105 - if (!is_array($conversation_history)) {
8106 - $conversation_history = array();
8107 - }
8108 -
8109 - // Build the input for Responses API
8110 - // The Responses API uses a different format - we need to construct the input properly
8111 - $input_parts = [];
8112 -
8113 - // Add system instructions as context
8114 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8115 -
8116 - // Build conversation as input items for Responses API
8117 - foreach ($conversation_history as $message) {
8118 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8119 - $role = $message['role'];
8120 - if ($role === 'bot' || $role === 'agent') {
8121 - $role = 'assistant';
8122 - }
8123 - if (!in_array($role, ['assistant', 'user'])) {
8124 - $role = 'user';
8125 - }
8126 - $input_parts[] = [
8127 - 'type' => 'message',
8128 - 'role' => $role,
8129 - 'content' => $message['content']
8130 - ];
8131 - }
8132 - }
8133 -
8134 - // Build request body for Responses API
8135 - $request_body = [
8136 - 'model' => $selected_model,
8137 - 'input' => $input_parts,
8138 - 'instructions' => $system_context,
8139 - 'stream' => $streaming
8140 - ];
8141 -
8142 - // Only add web search tool if web search is enabled in settings
8143 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8144 - if ($web_search_enabled) {
8145 - $request_body['tools'] = [
8146 - ['type' => 'web_search']
8147 - ];
8148 - }
8149 -
8150 - // Add reasoning effort for supported models
8151 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8152 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8153 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8154 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8155 - $request_body['reasoning'] = ['effort' => 'low'];
8156 - } elseif ($selected_model === 'gpt-5.5') {
8157 - $request_body['reasoning'] = ['effort' => 'low'];
8158 - } elseif ($selected_model === 'gpt-5.4') {
8159 - $request_body['reasoning'] = ['effort' => 'low'];
8160 - }
8161 - }
8162 -
8163 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8164 -
8165 - if ($streaming) {
8166 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8167 - } else {
8168 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8169 - }
8170 -
8171 - } catch (Exception $e) {
8172 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8173 - return [
8174 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8175 - 'error_code' => 'web_search_exception'
8176 - ];
8177 - }
8178 -}
8179 -
8180 -/**
8181 - * Handle non-streaming web search response
8182 - */
8183 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8184 - $request_body['stream'] = false;
8185 -
8186 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8187 - 'headers' => array(
8188 - 'Authorization' => 'Bearer ' . $api_key,
8189 - 'Content-Type' => 'application/json'
8190 - ),
8191 - 'body' => json_encode($request_body),
8192 - 'timeout' => 90
8193 - ), 'openai');
8194 -
8195 - if (is_wp_error($response)) {
8196 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8197 - return [
8198 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8199 - 'error_code' => 'web_search_connection_error'
8200 - ];
8201 - }
8202 -
8203 - $response_code = wp_remote_retrieve_response_code($response);
8204 - $response_body = wp_remote_retrieve_body($response);
8205 -
8206 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8207 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8208 -
8209 - if ($response_code !== 200) {
8210 - $error_data = json_decode($response_body, true);
8211 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8212 - return [
8213 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8214 - 'error_code' => 'web_search_api_error'
8215 - ];
8216 - }
8217 -
8218 - $result = json_decode($response_body, true);
8219 -
8220 - if (json_last_error() !== JSON_ERROR_NONE) {
8221 - return [
8222 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8223 - 'error_code' => 'web_search_json_error'
8224 - ];
8225 - }
8226 -
8227 - // Extract the response text and citations from Responses API format
8228 - $output_text = '';
8229 - $citations = [];
8230 -
8231 - if (isset($result['output'])) {
8232 - foreach ($result['output'] as $output_item) {
8233 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8234 - foreach ($output_item['content'] as $content_item) {
8235 - if ($content_item['type'] === 'output_text') {
8236 - $output_text .= $content_item['text'];
8237 -
8238 - // Extract citations/annotations
8239 - if (isset($content_item['annotations'])) {
8240 - foreach ($content_item['annotations'] as $annotation) {
8241 - if ($annotation['type'] === 'url_citation') {
8242 - $citations[] = [
8243 - 'url' => $annotation['url'],
8244 - 'title' => $annotation['title'] ?? ''
8245 - ];
8246 - }
8247 - }
8248 - }
8249 - }
8250 - }
8251 - }
8252 - }
8253 - }
8254 -
8255 - // If we have citations, append them to the response
8256 - if (!empty($citations)) {
8257 - $output_text .= "\n\n**Sources:**\n";
8258 - $seen_urls = [];
8259 - foreach ($citations as $citation) {
8260 - if (!in_array($citation['url'], $seen_urls)) {
8261 - $seen_urls[] = $citation['url'];
8262 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8263 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8264 - }
8265 - }
8266 - }
8267 -
8268 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8269 - // which includes rag_context for the "sources" link in transcripts.
8270 -
8271 - return $output_text;
8272 -}
8273 -
8274 -/**
8275 - * Handle streaming web search response using Responses API
8276 - */
8277 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8278 - $request_body['stream'] = true;
8279 -
8280 - // Check if we can stream
8281 - if (headers_sent() || !function_exists('curl_init')) {
8282 - // Fallback to non-streaming
8283 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8284 - }
8285 -
8286 - // Setup streaming headers
8287 - $this->setup_streaming_headers();
8288 -
8289 - $ch = curl_init();
8290 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8291 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8292 - curl_setopt($ch, CURLOPT_POST, true);
8293 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8294 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8295 - 'Content-Type: application/json',
8296 - 'Authorization: Bearer ' . $api_key
8297 - ));
8298 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8299 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8300 -
8301 - $full_response = '';
8302 - $stream_started = false;
8303 - $buffer = '';
8304 - $citations = [];
8305 -
8306 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
8307 - // Send testing data as first event if available
8308 - if (!$stream_started && $testing_data !== null) {
8309 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8310 - flush();
8311 - $stream_started = true;
8312 - }
8313 -
8314 - $buffer .= $data;
8315 - $lines = explode("\n", $buffer);
8316 - $buffer = array_pop($lines);
8317 -
8318 - foreach ($lines as $line) {
8319 - if (trim($line) === '') continue;
8320 - if (strpos($line, 'data: ') !== 0) continue;
8321 -
8322 - $json_str = substr($line, 6);
8323 -
8324 - if (trim($json_str) === '[DONE]') {
8325 - // Append citations if we have any
8326 - if (!empty($citations)) {
8327 - $citation_text = "\n\n**Sources:**\n";
8328 - $seen_urls = [];
8329 - foreach ($citations as $citation) {
8330 - if (!in_array($citation['url'], $seen_urls)) {
8331 - $seen_urls[] = $citation['url'];
8332 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8333 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8334 - }
8335 - }
8336 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
8337 - $full_response .= $citation_text;
8338 - flush();
8339 - }
8340 - echo "data: [DONE]\n\n";
8341 - flush();
8342 - continue;
8343 - }
8344 -
8345 - $json = json_decode(trim($json_str), true);
8346 - if (!$json) continue;
8347 -
8348 - // Handle Responses API streaming events
8349 - // The format is different from Chat Completions
8350 - if (isset($json['type'])) {
8351 - switch ($json['type']) {
8352 - case 'response.output_text.delta':
8353 - // Text content delta
8354 - if (isset($json['delta'])) {
8355 - $content = $json['delta'];
8356 - $full_response .= $content;
8357 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8358 - flush();
8359 - }
8360 - break;
8361 -
8362 - case 'response.output_item.done':
8363 - // Check for citations in completed items
8364 - if (isset($json['item']['content'])) {
8365 - foreach ($json['item']['content'] as $content_item) {
8366 - if (isset($content_item['annotations'])) {
8367 - foreach ($content_item['annotations'] as $annotation) {
8368 - if ($annotation['type'] === 'url_citation') {
8369 - $citations[] = [
8370 - 'url' => $annotation['url'],
8371 - 'title' => $annotation['title'] ?? ''
8372 - ];
8373 - }
8374 - }
8375 - }
8376 - }
8377 - }
8378 - break;
8379 - }
8380 - }
8381 - }
8382 -
8383 - return strlen($data);
8384 - });
8385 -
8386 - $response = curl_exec($ch);
8387 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8388 -
8389 - if (curl_errno($ch) || $http_code !== 200) {
8390 - $curl_error = curl_error($ch);
8391 - curl_close($ch);
8392 -
8393 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
8394 -
8395 - return $this->mxchat_stream_emit_fallback(
8396 - 'web_search',
8397 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
8398 - $session_id,
8399 - $testing_data
8400 - );
8401 - }
8402 -
8403 - curl_close($ch);
8404 -
8405 - // Save the complete response with RAG context so the "sources" link
8406 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
8407 - if (!empty($full_response) && !empty($session_id)) {
8408 - $rag_context_for_storage = null;
8409 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8410 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8411 -
8412 - if ($has_rag_data || $has_action_data) {
8413 - $rag_context_for_storage = [];
8414 -
8415 - if ($has_rag_data) {
8416 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8417 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8418 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8419 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8420 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8421 - }
8422 -
8423 - if ($has_action_data) {
8424 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8425 - }
8426 - }
8427 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8428 - }
8429 -
8430 - return true;
8431 -}
8432 -
8433 4711 private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8434 4712 try {
8435 4713 // Get bot ID from session or request
8436 4714 $bot_id = $this->get_current_bot_id($session_id);
@@ -8435,9 +4713,9 @@
8435 4713 // Get bot ID from session or request
8436 4714 $bot_id = $this->get_current_bot_id($session_id);
8437 4715
8438 4716 // Get system prompt instructions using centralized function
8439 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4717 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
8440 4718 // Ensure conversation_history is an array
8441 4719 if (!is_array($conversation_history)) {
8442 4720 $conversation_history = array();
8443 4721 }
@@ -8469,9 +4747,9 @@
8469 4747 'content' => $relevant_content
8470 4748 ];
8471 4749
8472 4750 // Prepare the request body with stream: true
8473 - $payload = [
4751 + $body = json_encode([
8474 4752 'model' => $selected_model,
8475 4753 'messages' => $conversation_history,
8476 4754 'max_tokens' => 1000,
8477 4755 'temperature' => 0.8,
@@ -8476,11 +4754,9 @@
8476 4754 'max_tokens' => 1000,
8477 4755 'temperature' => 0.8,
8478 4756 'system' => $system_prompt_instructions,
8479 4757 'stream' => true
8480 - ];
8481 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
8482 - $body = json_encode($payload);
4758 + ]);
8483 4759
8484 4760 // Check if we can actually stream (headers not sent, etc.)
8485 4761 if (headers_sent() || !function_exists('curl_init')) {
8486 4762 // Fallback to regular response with testing data
@@ -8491,13 +4767,8 @@
8491 4767 array_slice($conversation_history, 0, -1), // Remove the added content
8492 4768 $relevant_content
8493 4769 );
8494 4770
8495 - // Save bot response to transcript
8496 - if (!empty($regular_response) && !empty($session_id)) {
8497 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8498 - }
8499 -
8500 4771 // Return as JSON with testing data
8501 4772 $response_data = [
8502 4773 'text' => $regular_response,
8503 4774 'html' => '',
@@ -8516,187 +4787,163 @@
8516 4787 echo json_encode($response_data);
8517 4788 return true; // Indicate we handled the response
8518 4789 }
8519 4790
8520 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
4791 + // Use cURL for streaming support
4792 + $ch = curl_init();
4793 + curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
4794 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4795 + curl_setopt($ch, CURLOPT_POST, true);
4796 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4797 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4798 + 'Content-Type: application/json',
4799 + 'x-api-key: ' . $claude_api_key,
4800 + 'anthropic-version: 2023-06-01'
4801 + ));
4802 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4803 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8521 4804
8522 - $captured_status_code = 0;
8523 - $captured_body_pre_stream = '';
8524 - $full_response = '';
4805 + $full_response = ''; // Accumulate full response for saving
8525 4806 $stream_started = false;
8526 - $buffer = '';
8527 - $errno = 0;
8528 - $http_code = 0;
8529 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8530 - $backoff_ms = array(0, 750, 2000);
4807 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
8531 4808
8532 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8533 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8534 - usleep($backoff_ms[$attempt] * 1000);
4809 + // Buffer control for real-time streaming
4810 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
4811 + // Send testing data as the first event if available
4812 + if (!$stream_started && $testing_data !== null) {
4813 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4814 + flush();
4815 + $stream_started = true;
4816 + //error_log("MxChat Testing: Sent testing data in Claude stream");
8535 4817 }
4818 +
4819 + // CRITICAL FIX: Append new data to buffer
4820 + $buffer .= $data;
4821 +
4822 + // Process complete lines only
4823 + $lines = explode("\n", $buffer);
4824 +
4825 + // CRITICAL FIX: Keep the last incomplete line in the buffer
4826 + // The last element might be incomplete, so keep it in buffer
4827 + $buffer = array_pop($lines);
8536 4828
8537 - $captured_status_code = 0;
8538 - $captured_body_pre_stream = '';
8539 - $full_response = '';
8540 - $stream_started = false;
8541 - $buffer = '';
8542 -
8543 - $ch = curl_init();
8544 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
8545 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8546 - curl_setopt($ch, CURLOPT_POST, true);
8547 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8548 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8549 - 'Content-Type: application/json',
8550 - 'x-api-key: ' . $claude_api_key,
8551 - 'anthropic-version: 2023-06-01'
8552 - ));
8553 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8554 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8555 -
8556 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8557 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8558 - $captured_status_code = (int) $m[1];
4829 + foreach ($lines as $line) {
4830 + if (trim($line) === '') {
4831 + continue;
8559 4832 }
8560 - return strlen($header);
8561 - });
8562 4833
8563 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8564 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8565 - $captured_body_pre_stream .= $data;
8566 - return strlen($data);
4834 + // Claude uses event: and data: format
4835 + if (strpos($line, 'event: ') === 0) {
4836 + // Store the event type for the next data line
4837 + continue;
8567 4838 }
8568 4839
8569 - if (!$this->streaming_headers_sent) {
8570 - $this->setup_streaming_headers();
8571 - }
4840 + if (strpos($line, 'data: ') === 0) {
4841 + $json_str = substr($line, 6); // Remove 'data: ' prefix
8572 4842
8573 - if (!$stream_started && $testing_data !== null) {
8574 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8575 - flush();
8576 - $stream_started = true;
8577 - }
8578 -
8579 - $buffer .= $data;
8580 - $lines = explode("\n", $buffer);
8581 - $buffer = array_pop($lines);
8582 -
8583 - foreach ($lines as $line) {
8584 - if (trim($line) === '') {
4843 + $json = json_decode(trim($json_str), true);
4844 + if (json_last_error() !== JSON_ERROR_NONE) {
8585 4845 continue;
8586 4846 }
8587 4847
8588 - if (strpos($line, 'event: ') === 0) {
8589 - continue;
8590 - }
4848 + // Handle different event types
4849 + if (isset($json['type'])) {
4850 + switch ($json['type']) {
4851 + case 'content_block_delta':
4852 + if (isset($json['delta']['text'])) {
4853 + $content = $json['delta']['text'];
4854 + $full_response .= $content; // Accumulate
4855 + // Send as SSE format compatible with your frontend
4856 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
4857 + flush();
4858 + }
4859 + break;
8591 4860
8592 - if (strpos($line, 'data: ') === 0) {
8593 - $json_str = substr($line, 6);
4861 + case 'message_stop':
4862 + echo "data: [DONE]\n\n";
4863 + flush();
4864 + break;
8594 4865
8595 - $json = json_decode(trim($json_str), true);
8596 - if (json_last_error() !== JSON_ERROR_NONE) {
8597 - continue;
4866 + case 'error':
4867 + echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
4868 + flush();
4869 + break;
8598 4870 }
8599 -
8600 - if (isset($json['type'])) {
8601 - switch ($json['type']) {
8602 - case 'content_block_delta':
8603 - if (isset($json['delta']['text'])) {
8604 - $content = $json['delta']['text'];
8605 - $full_response .= $content;
8606 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8607 - flush();
8608 - }
8609 - break;
8610 -
8611 - case 'message_stop':
8612 - echo "data: [DONE]\n\n";
8613 - flush();
8614 - break;
8615 -
8616 - case 'error':
8617 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8618 - flush();
8619 - break;
8620 - }
8621 - }
8622 4871 }
8623 4872 }
8624 -
8625 - return strlen($data);
8626 - });
8627 -
8628 - $response = curl_exec($ch);
8629 - $errno = curl_errno($ch);
8630 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8631 - curl_close($ch);
8632 -
8633 - if (!$errno && $http_code === 200) {
8634 - break;
8635 4873 }
8636 4874
8637 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
8638 - $can_retry = !$this->streaming_headers_sent
8639 - && ($attempt + 1) < $max_attempts
8640 - && $is_transient;
4875 + return strlen($data);
4876 + });
8641 4877
8642 - if (defined('WP_DEBUG') && WP_DEBUG) {
8643 - error_log(sprintf(
8644 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8645 - $attempt + 1, $max_attempts, $http_code, $errno,
8646 - $is_transient ? 'yes' : 'no',
8647 - $can_retry ? 'Retrying.' : 'Giving up.'
8648 - ));
8649 - }
4878 + $response = curl_exec($ch);
4879 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8650 4880
8651 - if (!$can_retry) {
8652 - break;
8653 - }
4881 + if (curl_errno($ch)) {
4882 + curl_close($ch);
4883 + throw new Exception('cURL Error: ' . curl_error($ch));
8654 4884 }
8655 4885
8656 - if ($errno || $http_code !== 200) {
8657 - return $this->mxchat_stream_emit_fallback(
8658 - 'anthropic',
8659 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content),
8660 - $session_id,
8661 - $testing_data
4886 + curl_close($ch);
4887 +
4888 + if ($http_code !== 200) {
4889 + // Fallback to regular response
4890 + //error_log("MxChat: Claude streaming failed with HTTP $http_code, falling back");
4891 + $regular_response = $this->mxchat_generate_response_claude(
4892 + $selected_model,
4893 + $claude_api_key,
4894 + array_slice($conversation_history, 0, -1), // Remove the added content
4895 + $relevant_content
8662 4896 );
4897 +
4898 + $response_data = [
4899 + 'text' => $regular_response,
4900 + 'html' => '',
4901 + 'session_id' => $session_id
4902 + ];
4903 +
4904 + if ($testing_data !== null) {
4905 + $response_data['testing_data'] = $testing_data;
4906 + //error_log("MxChat Testing: Added testing data to Claude error fallback");
4907 + }
4908 +
4909 + header('Content-Type: application/json');
4910 + echo json_encode($response_data);
4911 + return true;
8663 4912 }
8664 4913
8665 4914 // Save the complete response to maintain chat persistence
8666 4915 if (!empty($full_response) && !empty($session_id)) {
8667 - // Prepare RAG context for streaming response
8668 - $rag_context_for_storage = null;
8669 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8670 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8671 -
8672 - if ($has_rag_data || $has_action_data) {
8673 - $rag_context_for_storage = [];
8674 -
8675 - if ($has_rag_data) {
8676 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8677 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8678 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8679 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8680 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8681 - }
8682 -
8683 - if ($has_action_data) {
8684 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8685 - }
8686 - }
8687 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
4916 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8688 4917 }
8689 4918
8690 4919 return true; // Indicate streaming completed successfully
8691 4920
8692 4921 } catch (Exception $e) {
8693 - return $this->mxchat_stream_emit_fallback(
8694 - 'anthropic',
8695 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content),
8696 - $session_id,
8697 - $testing_data
4922 + //error_log("MxChat Claude streaming exception: " . $e->getMessage());
4923 +
4924 + // Fallback to regular response on exception
4925 + $regular_response = $this->mxchat_generate_response_claude(
4926 + $selected_model,
4927 + $claude_api_key,
4928 + $conversation_history,
4929 + $relevant_content
8698 4930 );
4931 +
4932 + $response_data = [
4933 + 'text' => $regular_response,
4934 + 'html' => '',
4935 + 'session_id' => $session_id
4936 + ];
4937 +
4938 + if ($testing_data !== null) {
4939 + $response_data['testing_data'] = $testing_data;
4940 + //error_log("MxChat Testing: Added testing data to Claude exception fallback");
4941 + }
4942 +
4943 + header('Content-Type: application/json');
4944 + echo json_encode($response_data);
4945 + return true;
8699 4946 }
8700 4947 }
8701 4948 private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8702 4949 try {
@@ -8703,9 +4950,9 @@
8703 4950 // Get bot ID from session or request
8704 4951 $bot_id = $this->get_current_bot_id($session_id);
8705 4952
8706 4953 // Get system prompt instructions using centralized function
8707 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4954 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
8708 4955
8709 4956 // Ensure conversation_history is an array
8710 4957 if (!is_array($conversation_history)) {
8711 4958 $conversation_history = array();
@@ -8745,13 +4992,8 @@
8745 4992 $conversation_history,
8746 4993 $relevant_content
8747 4994 );
8748 4995
8749 - // Save bot response to transcript
8750 - if (!empty($regular_response) && !empty($session_id)) {
8751 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8752 - }
8753 -
8754 4996 $response_data = [
8755 4997 'text' => $regular_response,
8756 4998 'html' => '',
8757 4999 'session_id' => $session_id
@@ -8774,169 +5016,143 @@
8774 5016 'temperature' => 0.8,
8775 5017 'stream' => true
8776 5018 ]);
8777 5019
8778 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8779 -
8780 - $captured_status_code = 0;
8781 - $captured_body_pre_stream = '';
8782 - $full_response = '';
5020 + // Use cURL for streaming support
5021 + $ch = curl_init();
5022 + curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
5023 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
5024 + curl_setopt($ch, CURLOPT_POST, true);
5025 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
5026 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5027 + 'Content-Type: application/json',
5028 + 'Authorization: Bearer ' . $xai_api_key
5029 + ));
5030 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
5031 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
5032 +
5033 + $full_response = ''; // Accumulate full response for saving
8783 5034 $stream_started = false;
8784 - $buffer = '';
8785 - $errno = 0;
8786 - $http_code = 0;
8787 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8788 - $backoff_ms = array(0, 750, 2000);
8789 -
8790 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8791 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8792 - usleep($backoff_ms[$attempt] * 1000);
5035 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
5036 +
5037 + // Buffer control for real-time streaming
5038 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
5039 + // Send testing data as the first event if available
5040 + if (!$stream_started && $testing_data !== null) {
5041 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5042 + flush();
5043 + $stream_started = true;
5044 + //error_log("MxChat Testing: Sent testing data in X.AI stream");
8793 5045 }
8794 -
8795 - $captured_status_code = 0;
8796 - $captured_body_pre_stream = '';
8797 - $full_response = '';
8798 - $stream_started = false;
8799 - $buffer = '';
8800 -
8801 - $ch = curl_init();
8802 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
8803 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8804 - curl_setopt($ch, CURLOPT_POST, true);
8805 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8806 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8807 - 'Content-Type: application/json',
8808 - 'Authorization: Bearer ' . $xai_api_key
8809 - ));
8810 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8811 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8812 -
8813 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8814 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8815 - $captured_status_code = (int) $m[1];
5046 +
5047 + // CRITICAL FIX: Append new data to buffer
5048 + $buffer .= $data;
5049 +
5050 + // Process complete lines only
5051 + $lines = explode("\n", $buffer);
5052 +
5053 + // CRITICAL FIX: Keep the last incomplete line in the buffer
5054 + // The last element might be incomplete, so keep it in buffer
5055 + $buffer = array_pop($lines);
5056 +
5057 + foreach ($lines as $line) {
5058 + // Skip empty lines
5059 + if (trim($line) === '') {
5060 + continue;
8816 5061 }
8817 - return strlen($header);
8818 - });
8819 -
8820 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8821 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8822 - $captured_body_pre_stream .= $data;
8823 - return strlen($data);
5062 +
5063 + // Only process lines that start with "data: "
5064 + if (strpos($line, 'data: ') !== 0) {
5065 + continue;
8824 5066 }
8825 -
8826 - if (!$this->streaming_headers_sent) {
8827 - $this->setup_streaming_headers();
5067 +
5068 + $json_str = substr($line, 6); // Remove 'data: ' prefix
5069 +
5070 + if (trim($json_str) === '[DONE]') {
5071 + echo "data: [DONE]\n\n";
5072 + flush();
5073 + continue;
8828 5074 }
8829 -
8830 - if (!$stream_started && $testing_data !== null) {
8831 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5075 +
5076 + // Try to decode JSON
5077 + $json = json_decode(trim($json_str), true);
5078 + if ($json && isset($json['choices'][0]['delta']['content'])) {
5079 + $content = $json['choices'][0]['delta']['content'];
5080 + $full_response .= $content; // Accumulate
5081 + // Send as SSE format
5082 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
8832 5083 flush();
8833 - $stream_started = true;
8834 5084 }
8835 -
8836 - $buffer .= $data;
8837 - $lines = explode("\n", $buffer);
8838 - $buffer = array_pop($lines);
8839 -
8840 - foreach ($lines as $line) {
8841 - if (trim($line) === '') {
8842 - continue;
8843 - }
8844 - if (strpos($line, 'data: ') !== 0) {
8845 - continue;
8846 - }
8847 -
8848 - $json_str = substr($line, 6);
8849 -
8850 - if (trim($json_str) === '[DONE]') {
8851 - echo "data: [DONE]\n\n";
8852 - flush();
8853 - continue;
8854 - }
8855 -
8856 - $json = json_decode(trim($json_str), true);
8857 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8858 - $content = $json['choices'][0]['delta']['content'];
8859 - $full_response .= $content;
8860 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8861 - flush();
8862 - }
8863 - }
8864 -
8865 - return strlen($data);
8866 - });
8867 -
8868 - $response = curl_exec($ch);
8869 - $errno = curl_errno($ch);
8870 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
5085 + }
5086 +
5087 + return strlen($data);
5088 + });
5089 +
5090 + $response = curl_exec($ch);
5091 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5092 +
5093 + if (curl_errno($ch) || $http_code !== 200) {
8871 5094 curl_close($ch);
8872 -
8873 - if (!$errno && $http_code === 200) {
8874 - break;
5095 +
5096 + // Fallback to regular response
5097 + //error_log("MxChat: X.AI streaming failed, falling back");
5098 + $regular_response = $this->mxchat_generate_response_xai(
5099 + $selected_model,
5100 + $xai_api_key,
5101 + $conversation_history,
5102 + $relevant_content
5103 + );
5104 +
5105 + $response_data = [
5106 + 'text' => $regular_response,
5107 + 'html' => '',
5108 + 'session_id' => $session_id
5109 + ];
5110 +
5111 + if ($testing_data !== null) {
5112 + $response_data['testing_data'] = $testing_data;
5113 + //error_log("MxChat Testing: Added testing data to X.AI error fallback");
8875 5114 }
8876 -
8877 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
8878 - $can_retry = !$this->streaming_headers_sent
8879 - && ($attempt + 1) < $max_attempts
8880 - && $is_transient;
8881 -
8882 - if (defined('WP_DEBUG') && WP_DEBUG) {
8883 - error_log(sprintf(
8884 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8885 - $attempt + 1, $max_attempts, $http_code, $errno,
8886 - $is_transient ? 'yes' : 'no',
8887 - $can_retry ? 'Retrying.' : 'Giving up.'
8888 - ));
8889 - }
8890 -
8891 - if (!$can_retry) {
8892 - break;
8893 - }
5115 +
5116 + header('Content-Type: application/json');
5117 + echo json_encode($response_data);
5118 + return true;
8894 5119 }
8895 -
8896 - if ($errno || $http_code !== 200) {
8897 - return $this->mxchat_stream_emit_fallback(
8898 - 'xai',
8899 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
8900 - $session_id,
8901 - $testing_data
8902 - );
8903 - }
8904 -
5120 +
5121 + curl_close($ch);
5122 +
8905 5123 // Save the complete response to maintain chat persistence
8906 5124 if (!empty($full_response) && !empty($session_id)) {
8907 - // Prepare RAG context for streaming response
8908 - $rag_context_for_storage = null;
8909 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8910 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8911 -
8912 - if ($has_rag_data || $has_action_data) {
8913 - $rag_context_for_storage = [];
8914 -
8915 - if ($has_rag_data) {
8916 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8917 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8918 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8919 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8920 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8921 - }
8922 -
8923 - if ($has_action_data) {
8924 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8925 - }
8926 - }
8927 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
5125 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8928 5126 }
8929 -
5127 +
8930 5128 return true; // Indicate streaming completed successfully
8931 -
5129 +
8932 5130 } catch (Exception $e) {
8933 - return $this->mxchat_stream_emit_fallback(
8934 - 'xai',
8935 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
8936 - $session_id,
8937 - $testing_data
5131 + //error_log("MxChat X.AI streaming exception: " . $e->getMessage());
5132 +
5133 + // Fallback to regular response
5134 + $regular_response = $this->mxchat_generate_response_xai(
5135 + $selected_model,
5136 + $xai_api_key,
5137 + $conversation_history,
5138 + $relevant_content
8938 5139 );
5140 +
5141 + $response_data = [
5142 + 'text' => $regular_response,
5143 + 'html' => '',
5144 + 'session_id' => $session_id
5145 + ];
5146 +
5147 + if ($testing_data !== null) {
5148 + $response_data['testing_data'] = $testing_data;
5149 + //error_log("MxChat Testing: Added testing data to X.AI exception fallback");
5150 + }
5151 +
5152 + header('Content-Type: application/json');
5153 + echo json_encode($response_data);
5154 + return true;
8939 5155 }
8940 5156 }
8941 5157 private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8942 5158 try {
@@ -8943,9 +5159,9 @@
8943 5159 // Get bot ID from session or request
8944 5160 $bot_id = $this->get_current_bot_id($session_id);
8945 5161
8946 5162 // Get system prompt instructions using centralized function
8947 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5163 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
8948 5164
8949 5165 // Ensure conversation_history is an array
8950 5166 if (!is_array($conversation_history)) {
8951 5167 $conversation_history = array();
@@ -8985,13 +5201,8 @@
8985 5201 $conversation_history,
8986 5202 $relevant_content
8987 5203 );
8988 5204
8989 - // Save bot response to transcript
8990 - if (!empty($regular_response) && !empty($session_id)) {
8991 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8992 - }
8993 -
8994 5205 $response_data = [
8995 5206 'text' => $regular_response,
8996 5207 'html' => '',
8997 5208 'session_id' => $session_id
@@ -9014,276 +5225,170 @@
9014 5225 'temperature' => 0.8,
9015 5226 'stream' => true
9016 5227 ]);
9017 5228
9018 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9019 -
9020 - $captured_status_code = 0;
9021 - $captured_body_pre_stream = '';
9022 - $full_response = '';
5229 + // Use cURL for streaming support
5230 + $ch = curl_init();
5231 + curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
5232 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
5233 + curl_setopt($ch, CURLOPT_POST, true);
5234 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
5235 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5236 + 'Content-Type: application/json',
5237 + 'Authorization: Bearer ' . $deepseek_api_key
5238 + ));
5239 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
5240 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
5241 +
5242 + $full_response = ''; // Accumulate full response for saving
9023 5243 $stream_started = false;
9024 - $buffer = '';
9025 - $errno = 0;
9026 - $http_code = 0;
9027 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9028 - $backoff_ms = array(0, 750, 2000);
9029 -
9030 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9031 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9032 - usleep($backoff_ms[$attempt] * 1000);
5244 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
5245 +
5246 + // Buffer control for real-time streaming
5247 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
5248 + // Send testing data as the first event if available
5249 + if (!$stream_started && $testing_data !== null) {
5250 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5251 + flush();
5252 + $stream_started = true;
5253 + //error_log("MxChat Testing: Sent testing data in DeepSeek stream");
9033 5254 }
9034 -
9035 - $captured_status_code = 0;
9036 - $captured_body_pre_stream = '';
9037 - $full_response = '';
9038 - $stream_started = false;
9039 - $buffer = '';
9040 -
9041 - $ch = curl_init();
9042 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9043 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9044 - curl_setopt($ch, CURLOPT_POST, true);
9045 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9046 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9047 - 'Content-Type: application/json',
9048 - 'Authorization: Bearer ' . $deepseek_api_key
9049 - ));
9050 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9051 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9052 -
9053 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9054 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9055 - $captured_status_code = (int) $m[1];
5255 +
5256 + // CRITICAL FIX: Append new data to buffer
5257 + $buffer .= $data;
5258 +
5259 + // Process complete lines only
5260 + $lines = explode("\n", $buffer);
5261 +
5262 + // CRITICAL FIX: Keep the last incomplete line in the buffer
5263 + // The last element might be incomplete, so keep it in buffer
5264 + $buffer = array_pop($lines);
5265 +
5266 + foreach ($lines as $line) {
5267 + // Skip empty lines
5268 + if (trim($line) === '') {
5269 + continue;
9056 5270 }
9057 - return strlen($header);
9058 - });
9059 -
9060 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9061 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9062 - $captured_body_pre_stream .= $data;
9063 - return strlen($data);
5271 +
5272 + // Only process lines that start with "data: "
5273 + if (strpos($line, 'data: ') !== 0) {
5274 + continue;
9064 5275 }
9065 -
9066 - if (!$this->streaming_headers_sent) {
9067 - $this->setup_streaming_headers();
5276 +
5277 + $json_str = substr($line, 6); // Remove 'data: ' prefix
5278 +
5279 + if (trim($json_str) === '[DONE]') {
5280 + echo "data: [DONE]\n\n";
5281 + flush();
5282 + continue;
9068 5283 }
9069 -
9070 - if (!$stream_started && $testing_data !== null) {
9071 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5284 +
5285 + // Try to decode JSON
5286 + $json = json_decode(trim($json_str), true);
5287 + if ($json && isset($json['choices'][0]['delta']['content'])) {
5288 + $content = $json['choices'][0]['delta']['content'];
5289 + $full_response .= $content; // Accumulate the full response
5290 +
5291 + // Send as SSE format
5292 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
9072 5293 flush();
9073 - $stream_started = true;
9074 5294 }
9075 -
9076 - $buffer .= $data;
9077 - $lines = explode("\n", $buffer);
9078 - $buffer = array_pop($lines);
9079 -
9080 - foreach ($lines as $line) {
9081 - if (trim($line) === '') {
9082 - continue;
9083 - }
9084 - if (strpos($line, 'data: ') !== 0) {
9085 - continue;
9086 - }
9087 -
9088 - $json_str = substr($line, 6);
9089 -
9090 - if (trim($json_str) === '[DONE]') {
9091 - echo "data: [DONE]\n\n";
9092 - flush();
9093 - continue;
9094 - }
9095 -
9096 - $json = json_decode(trim($json_str), true);
9097 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9098 - $content = $json['choices'][0]['delta']['content'];
9099 - $full_response .= $content;
9100 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9101 - flush();
9102 - }
5295 + }
5296 +
5297 + return strlen($data);
5298 + });
5299 +
5300 + $response = curl_exec($ch);
5301 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5302 +
5303 + if (curl_errno($ch) || $http_code !== 200) {
5304 + $curl_error = curl_error($ch);
5305 + curl_close($ch);
5306 +
5307 + // Log the specific error for debugging
5308 + //error_log("MxChat: DeepSeek streaming failed - HTTP: $http_code, cURL: $curl_error");
5309 +
5310 + // Fallback to regular response
5311 + $regular_response = $this->mxchat_generate_response_deepseek(
5312 + $selected_model,
5313 + $deepseek_api_key,
5314 + $conversation_history,
5315 + $relevant_content
5316 + );
5317 +
5318 + // Handle error response from regular function
5319 + if (is_array($regular_response) && isset($regular_response['error'])) {
5320 + if ($testing_data !== null) {
5321 + $regular_response['testing_data'] = $testing_data;
9103 5322 }
9104 -
9105 - return strlen($data);
9106 - });
9107 -
9108 - $response = curl_exec($ch);
9109 - $errno = curl_errno($ch);
9110 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9111 - curl_close($ch);
9112 -
9113 - if (!$errno && $http_code === 200) {
9114 - break;
5323 + header('Content-Type: application/json');
5324 + echo json_encode($regular_response);
5325 + return true;
9115 5326 }
9116 -
9117 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9118 - $can_retry = !$this->streaming_headers_sent
9119 - && ($attempt + 1) < $max_attempts
9120 - && $is_transient;
9121 -
9122 - if (defined('WP_DEBUG') && WP_DEBUG) {
9123 - error_log(sprintf(
9124 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9125 - $attempt + 1, $max_attempts, $http_code, $errno,
9126 - $is_transient ? 'yes' : 'no',
9127 - $can_retry ? 'Retrying.' : 'Giving up.'
9128 - ));
5327 +
5328 + $response_data = [
5329 + 'text' => $regular_response,
5330 + 'html' => '',
5331 + 'session_id' => $session_id
5332 + ];
5333 +
5334 + if ($testing_data !== null) {
5335 + $response_data['testing_data'] = $testing_data;
5336 + //error_log("MxChat Testing: Added testing data to DeepSeek error fallback");
9129 5337 }
9130 -
9131 - if (!$can_retry) {
9132 - break;
9133 - }
5338 +
5339 + header('Content-Type: application/json');
5340 + echo json_encode($response_data);
5341 + return true;
9134 5342 }
9135 -
9136 - if ($errno || $http_code !== 200) {
9137 - return $this->mxchat_stream_emit_fallback(
9138 - 'openai',
9139 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9140 - $session_id,
9141 - $testing_data
9142 - );
9143 - }
9144 -
5343 +
5344 + curl_close($ch);
5345 +
9145 5346 // Save the complete response to maintain chat persistence
9146 5347 if (!empty($full_response) && !empty($session_id)) {
9147 - // Prepare RAG context for streaming response
9148 - $rag_context_for_storage = null;
9149 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9150 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9151 -
9152 - if ($has_rag_data || $has_action_data) {
9153 - $rag_context_for_storage = [];
9154 -
9155 - if ($has_rag_data) {
9156 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9157 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9158 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9159 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9160 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9161 - }
9162 -
9163 - if ($has_action_data) {
9164 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9165 - }
9166 - }
9167 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
5348 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9168 5349 }
9169 -
5350 +
9170 5351 return true; // Indicate streaming completed successfully
9171 -
5352 +
9172 5353 } catch (Exception $e) {
9173 - return $this->mxchat_stream_emit_fallback(
9174 - 'openai',
9175 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9176 - $session_id,
9177 - $testing_data
5354 + //error_log("MxChat DeepSeek streaming exception: " . $e->getMessage());
5355 +
5356 + // Fallback to regular response
5357 + $regular_response = $this->mxchat_generate_response_deepseek(
5358 + $selected_model,
5359 + $deepseek_api_key,
5360 + $conversation_history,
5361 + $relevant_content
9178 5362 );
9179 - }
9180 -}
9181 -
9182 -
9183 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content) {
9184 - try {
9185 - if (!is_array($conversation_history)) {
9186 - $conversation_history = array();
9187 - }
9188 -
9189 - $bot_id = $this->get_current_bot_id('');
9190 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9191 5363
9192 - $formatted_conversation = array();
9193 -
9194 - $formatted_conversation[] = array(
9195 - 'role' => 'system',
9196 - 'content' => $system_prompt_instructions . " " . $relevant_content
9197 - );
9198 -
9199 - foreach ($conversation_history as $message) {
9200 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9201 - $role = $message['role'];
9202 -
9203 - if ($role === 'bot' || $role === 'agent') {
9204 - $role = 'assistant';
9205 - }
9206 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9207 - $role = 'user';
9208 - }
9209 -
9210 - $formatted_conversation[] = array(
9211 - 'role' => $role,
9212 - 'content' => $message['content']
9213 - );
5364 + // Handle error response from regular function
5365 + if (is_array($regular_response) && isset($regular_response['error'])) {
5366 + if ($testing_data !== null) {
5367 + $regular_response['testing_data'] = $testing_data;
9214 5368 }
5369 + header('Content-Type: application/json');
5370 + echo json_encode($regular_response);
5371 + return true;
9215 5372 }
9216 -
9217 - $body = json_encode([
9218 - 'model' => $selected_model,
9219 - 'messages' => $formatted_conversation,
9220 - 'temperature' => 1,
9221 - ]);
9222 -
9223 - $args = [
9224 - 'body' => $body,
9225 - 'headers' => [
9226 - 'Content-Type' => 'application/json',
9227 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9228 - 'HTTP-Referer' => home_url(),
9229 - 'X-Title' => get_bloginfo('name'),
9230 - ],
9231 - 'timeout' => 60,
9232 - 'redirection' => 5,
9233 - 'blocking' => true,
9234 - 'httpversion' => '1.0',
9235 - 'sslverify' => true,
5373 +
5374 + $response_data = [
5375 + 'text' => $regular_response,
5376 + 'html' => '',
5377 + 'session_id' => $session_id
9236 5378 ];
9237 -
9238 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9239 -
9240 - if (is_wp_error($response)) {
9241 - $error_message = $response->get_error_message();
9242 - return [
9243 - 'error' => esc_html__('Connection error when contacting OpenRouter: ', 'mxchat') . esc_html($error_message),
9244 - 'error_code' => 'openrouter_connection_error',
9245 - 'provider' => 'openrouter'
9246 - ];
5379 +
5380 + if ($testing_data !== null) {
5381 + $response_data['testing_data'] = $testing_data;
5382 + //error_log("MxChat Testing: Added testing data to DeepSeek exception fallback");
9247 5383 }
9248 -
9249 - $status_code = wp_remote_retrieve_response_code($response);
9250 - if ($status_code !== 200) {
9251 - $response_body = wp_remote_retrieve_body($response);
9252 - $decoded_response = json_decode($response_body, true);
9253 -
9254 - $error_message = isset($decoded_response['error']['message'])
9255 - ? $decoded_response['error']['message']
9256 - : 'HTTP Error ' . $status_code;
9257 -
9258 - return [
9259 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9260 - 'error_code' => 'openrouter_api_error',
9261 - 'provider' => 'openrouter',
9262 - 'status_code' => $status_code
9263 - ];
9264 - }
9265 -
9266 - $response_body = wp_remote_retrieve_body($response);
9267 - $decoded_response = json_decode($response_body, true);
9268 -
9269 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9270 - return trim($decoded_response['choices'][0]['message']['content']);
9271 - } else {
9272 - return [
9273 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9274 - 'error_code' => 'openrouter_response_format_error',
9275 - 'provider' => 'openrouter'
9276 - ];
9277 - }
9278 - } catch (Exception $e) {
9279 - return [
9280 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9281 - 'error_code' => 'openrouter_exception',
9282 - 'provider' => 'openrouter'
9283 - ];
5384 +
5385 + header('Content-Type: application/json');
5386 + echo json_encode($response_data);
5387 + return true;
9284 5388 }
9285 5389 }
5390 +
9286 5391 private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
9287 5392
9288 5393 // Get bot ID from session or request
9289 5394 $bot_id = $this->get_current_bot_id($session_id);
@@ -9288,9 +5393,9 @@
9288 5393 // Get bot ID from session or request
9289 5394 $bot_id = $this->get_current_bot_id($session_id);
9290 5395
9291 5396 // Get system prompt instructions using centralized function
9292 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5397 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9293 5398
9294 5399 // Clean and validate conversation history
9295 5400 foreach ($conversation_history as &$message) {
9296 5401 // Convert bot and agent roles to assistant
@@ -9318,17 +5423,15 @@
9318 5423 'content' => $relevant_content
9319 5424 ];
9320 5425
9321 5426 // Build request body
9322 - $payload = [
5427 + $body = json_encode([
9323 5428 'model' => $selected_model,
9324 5429 'max_tokens' => 1000,
9325 5430 'temperature' => 0.8,
9326 5431 'messages' => $conversation_history,
9327 5432 'system' => $system_prompt_instructions
9328 - ];
9329 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9330 - $body = json_encode($payload);
5433 + ]);
9331 5434
9332 5435 // Set up API request
9333 5436 $args = [
9334 5437 'body' => $body,
@@ -9344,9 +5447,9 @@
9344 5447 'sslverify' => true,
9345 5448 ];
9346 5449
9347 5450 // Make API request
9348 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
5451 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
9349 5452
9350 5453 // Check for WordPress errors
9351 5454 if (is_wp_error($response)) {
9352 5455 //error_log("Claude API request error: " . $response->get_error_message());
@@ -9396,12 +5499,12 @@
9396 5499 $conversation_history = array();
9397 5500 }
9398 5501
9399 5502 // Get bot ID from session or request
9400 - $bot_id = $this->get_current_bot_id('');
5503 + $bot_id = $this->get_current_bot_id($session_id);
9401 5504
9402 5505 // Get system prompt instructions using centralized function
9403 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5506 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9404 5507
9405 5508 // Create a new array for the formatted conversation
9406 5509 $formatted_conversation = array();
9407 5510
@@ -9430,44 +5533,15 @@
9430 5533 );
9431 5534 }
9432 5535 }
9433 5536
9434 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
9435 - $is_gpt5_model = (
9436 - strpos($selected_model, 'gpt-5') === 0 ||
9437 - $selected_model === 'gpt-5.2' ||
9438 - $selected_model === 'gpt-5.1-2025-11-13' ||
9439 - $selected_model === 'gpt-5' ||
9440 - $selected_model === 'gpt-5-mini' ||
9441 - $selected_model === 'gpt-5-nano'
9442 - );
9443 -
9444 - // Build request body with optimal settings for fast responses
9445 - $request_body = [
5537 + $body = json_encode([
9446 5538 'model' => $selected_model,
9447 5539 'messages' => $formatted_conversation,
9448 5540 'temperature' => 1,
9449 5541 'stream' => false
9450 - ];
5542 + ]);
9451 5543
9452 - // Add reasoning_effort only for GPT-5 models that support it
9453 - // These chat models don't support reasoning_effort parameter
9454 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
9455 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
9456 - // GPT-5.1 uses 'low' instead of 'minimal'
9457 - if ($selected_model === 'gpt-5.1-2025-11-13') {
9458 - $request_body['reasoning_effort'] = 'low';
9459 - } elseif ($selected_model === 'gpt-5.5') {
9460 - $request_body['reasoning_effort'] = 'none';
9461 - } elseif ($selected_model === 'gpt-5.4') {
9462 - $request_body['reasoning_effort'] = 'none';
9463 - } else {
9464 - $request_body['reasoning_effort'] = 'minimal';
9465 - }
9466 - }
9467 -
9468 - $body = json_encode($request_body);
9469 -
9470 5544 $args = [
9471 5545 'body' => $body,
9472 5546 'headers' => [
9473 5547 'Content-Type' => 'application/json',
@@ -9479,12 +5553,13 @@
9479 5553 'httpversion' => '1.0',
9480 5554 'sslverify' => true,
9481 5555 ];
9482 5556
9483 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
5557 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
9484 5558
9485 5559 if (is_wp_error($response)) {
9486 5560 $error_message = $response->get_error_message();
5561 + //error_log('OpenAI API Error: ' . $error_message);
9487 5562 return [
9488 5563 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9489 5564 'error_code' => 'openai_connection_error',
9490 5565 'provider' => 'openai'
@@ -9503,8 +5578,10 @@
9503 5578 $error_type = isset($decoded_response['error']['type'])
9504 5579 ? $decoded_response['error']['type']
9505 5580 : 'unknown';
9506 5581
5582 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
5583 +
9507 5584 // Handle specific error types
9508 5585 switch ($error_type) {
9509 5586 case 'invalid_request_error':
9510 5587 if (strpos($error_message, 'API key') !== false) {
@@ -9552,8 +5629,9 @@
9552 5629
9553 5630 if (isset($decoded_response['choices'][0]['message']['content'])) {
9554 5631 return trim($decoded_response['choices'][0]['message']['content']);
9555 5632 } else {
5633 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
9556 5634 return [
9557 5635 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9558 5636 'error_code' => 'openai_response_format_error',
9559 5637 'provider' => 'openai'
@@ -9559,8 +5637,9 @@
9559 5637 'provider' => 'openai'
9560 5638 ];
9561 5639 }
9562 5640 } catch (Exception $e) {
5641 + //error_log('OpenAI Exception: ' . $e->getMessage());
9563 5642 return [
9564 5643 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9565 5644 'error_code' => 'openai_exception',
9566 5645 'provider' => 'openai'
@@ -9566,9 +5645,8 @@
9566 5645 'provider' => 'openai'
9567 5646 ];
9568 5647 }
9569 5648 }
9570 -
9571 5649 private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9572 5650 try {
9573 5651 // Get bot ID from session or request
9574 5652 $bot_id = $this->get_current_bot_id($session_id);
@@ -9573,9 +5651,9 @@
9573 5651 // Get bot ID from session or request
9574 5652 $bot_id = $this->get_current_bot_id($session_id);
9575 5653
9576 5654 // Get system prompt instructions using centralized function
9577 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5655 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9578 5656
9579 5657 // Add system prompt to relevant content
9580 5658 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9581 5659
@@ -9625,9 +5703,9 @@
9625 5703 'sslverify' => true,
9626 5704 ];
9627 5705
9628 5706 // Make the API request
9629 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
5707 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
9630 5708
9631 5709 // Process the response
9632 5710 if (is_wp_error($response)) {
9633 5711 $error_message = $response->get_error_message();
@@ -9772,9 +5850,9 @@
9772 5850 // Get bot ID from session or request
9773 5851 $bot_id = $this->get_current_bot_id($session_id);
9774 5852
9775 5853 // Get system prompt instructions using centralized function
9776 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5854 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9777 5855
9778 5856 // Create a new array for the formatted conversation
9779 5857 $formatted_conversation = array();
9780 5858
@@ -9823,9 +5901,9 @@
9823 5901 'httpversion' => '1.0',
9824 5902 'sslverify' => true,
9825 5903 ];
9826 5904
9827 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
5905 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
9828 5906
9829 5907 if (is_wp_error($response)) {
9830 5908 $error_message = $response->get_error_message();
9831 5909 //error_log('DeepSeek API Error: ' . $error_message);
@@ -9927,18 +6005,13 @@
9927 6005 ];
9928 6006 }
9929 6007 }
9930 6008 private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
9931 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
9932 - // Auto-rescue existing installs whose saved model is the dead ID.
9933 - if ($selected_model === 'gemini-3-pro-preview') {
9934 - $selected_model = 'gemini-3.1-pro-preview';
9935 - }
9936 6009 // Get bot ID from session or request
9937 6010 $bot_id = $this->get_current_bot_id($session_id);
9938 6011
9939 6012 // Get system prompt instructions using centralized function
9940 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
6013 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9941 6014
9942 6015 // Add system prompt to relevant content
9943 6016 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9944 6017
@@ -10034,11 +6107,9 @@
10034 6107 ]
10035 6108 ]);
10036 6109
10037 6110 // Prepare the API endpoint
10038 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10039 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10040 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
6111 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10041 6112
10042 6113 // Set up the API request
10043 6114 $args = [
10044 6115 'body' => $body,
@@ -10052,10 +6123,10 @@
10052 6123 'sslverify' => true,
10053 6124 ];
10054 6125
10055 6126 // Make the API request
10056 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10057 -
6127 + $response = wp_remote_post($api_endpoint, $args);
6128 +
10058 6129 // Process the response
10059 6130 if (is_wp_error($response)) {
10060 6131 return "Sorry, there was an error processing your request: " . $response->get_error_message();
10061 6132 }
@@ -10080,9 +6151,9 @@
10080 6151
10081 6152
10082 6153 public function test_streaming_request() {
10083 6154 $options = get_option('mxchat_options', []);
10084 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
6155 + $model = $options['model'] ?? 'gpt-4o';
10085 6156
10086 6157 // Detect provider from model prefix
10087 6158 $provider = strtolower(explode('-', $model)[0]);
10088 6159
@@ -10265,60 +6336,35 @@
10265 6336 }
10266 6337
10267 6338
10268 6339 public function mxchat_enqueue_scripts_styles() {
10269 - // Fetch options from the database first to check loading strategy
10270 - $this->options = get_option('mxchat_options');
10271 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10272 -
10273 - // Always enqueue CSS immediately
6340 + // Define version numbers for the styles and scripts
6341 + $chat_style_version = '2.4.6';
6342 + $chat_script_version = '2.4.6';
6343 + // Enqueue the script
6344 + wp_enqueue_script(
6345 + 'mxchat-chat-js',
6346 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
6347 + array('jquery'),
6348 + $chat_script_version,
6349 + true
6350 + );
6351 + // Enqueue the CSS
10274 6352 wp_enqueue_style(
10275 6353 'mxchat-chat-css',
10276 6354 plugin_dir_url(__FILE__) . '../css/chat-style.css',
10277 6355 array(),
10278 - MXCHAT_VERSION
6356 + $chat_style_version
10279 6357 );
10280 -
10281 - // Handle script loading based on strategy
10282 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10283 - // Enqueue the script normally
10284 - wp_enqueue_script(
10285 - 'mxchat-chat-js',
10286 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
10287 - array('jquery'),
10288 - MXCHAT_VERSION,
10289 - true
10290 - );
10291 -
10292 - // Add defer attribute if strategy is 'defer'
10293 - if ($loading_strategy === 'defer') {
10294 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
10295 - }
10296 - } else {
10297 - // For delay or interaction-based loading, we'll use a custom loader
10298 - // Don't enqueue the main script - we'll load it dynamically
10299 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
10300 - }
10301 -
6358 + // Fetch options from the database
6359 + $this->options = get_option('mxchat_options');
10302 6360 $prompts_options = get_option('mxchat_prompts_options', array());
10303 -
10304 - // Check if AI theme is active - if so, skip inline colors in JavaScript
10305 - $theme_options = get_option('mxchat_theme_options', array());
10306 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10307 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10308 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10309 -
6361 +
10310 6362 // Prepare settings for JavaScript
10311 6363 $style_settings = array(
10312 6364 'ajax_url' => admin_url('admin-ajax.php'),
10313 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
10314 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
10315 - // as a one-shot fallback for the first interaction on a fresh page load
10316 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
10317 - // but the widget refetches before each subsequent send.
10318 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10319 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10320 - 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest',
6365 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
6366 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-4o',
10321 6367 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
10322 6368 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10323 6369 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10324 6370 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
@@ -10345,175 +6391,15 @@
10345 6391 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10346 6392 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
10347 6393 'initial_email_state' => null, // Also fixed this undefined variable
10348 6394 'skip_email_check' => true,
10349 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10350 - 'skip_inline_colors' => $skip_inline_colors,
10351 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10352 - 'print_button_enabled' => $this->options['print_button_enabled'] ?? 'on',
10353 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
10354 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
10355 - 'satisfaction_rating_enabled' => apply_filters(
10356 - 'mxchat_satisfaction_rating_enabled',
10357 - ($this->options['satisfaction_rating_enabled'] ?? 'off') === 'on'
10358 - ),
10359 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($this->options['satisfaction_rating_idle_seconds'] ?? 60))),
10360 - 'satisfaction_rating_copy' => array(
10361 - 'question' => !empty($this->options['satisfaction_rating_question']) ? esc_html($this->options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
10362 - 'helpful' => esc_html__('Helpful', 'mxchat'),
10363 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
10364 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
10365 - 'thanks' => !empty($this->options['satisfaction_rating_thanks']) ? esc_html($this->options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
10366 - 'placeholder' => !empty($this->options['satisfaction_rating_placeholder']) ? esc_html($this->options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
10367 - 'send' => esc_html__('Send', 'mxchat'),
10368 - 'skip' => esc_html__('Skip', 'mxchat'),
10369 - 'saved' => !empty($this->options['satisfaction_rating_saved']) ? esc_html($this->options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
10370 - ),
6395 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
10371 6396 );
10372 -
10373 - // For normal/defer loading, use wp_localize_script
10374 - // For delayed loading, we store settings in a transient to be output inline
10375 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10376 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10377 - } else {
10378 - // Store settings for the delayed loader to use
10379 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
10380 - }
6397 + // Pass the settings to the script
6398 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10381 6399 }
10382 6400
10383 -/**
10384 - * Output the delayed script loader for performance optimization
10385 - */
10386 -public function mxchat_output_delayed_script_loader() {
10387 - $this->options = get_option('mxchat_options');
10388 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10389 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
10390 6401
10391 - // Get the stored settings
10392 - $prompts_options = get_option('mxchat_prompts_options', array());
10393 - $theme_options = get_option('mxchat_theme_options', array());
10394 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10395 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10396 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10397 -
10398 - $style_settings = array(
10399 - 'ajax_url' => admin_url('admin-ajax.php'),
10400 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
10401 - // before each send. This inline value is a one-shot fallback for the first interaction.
10402 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10403 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10404 - 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest',
10405 - 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
10406 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10407 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10408 - 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
10409 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10410 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10411 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10412 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10413 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10414 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10415 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10416 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10417 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10418 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10419 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10420 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10421 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10422 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10423 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10424 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10425 - 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
10426 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10427 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10428 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10429 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10430 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
10431 - 'initial_email_state' => null,
10432 - 'skip_email_check' => true,
10433 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10434 - 'skip_inline_colors' => $skip_inline_colors,
10435 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10436 - 'print_button_enabled' => $this->options['print_button_enabled'] ?? 'on',
10437 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
10438 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
10439 - 'satisfaction_rating_enabled' => apply_filters(
10440 - 'mxchat_satisfaction_rating_enabled',
10441 - ($this->options['satisfaction_rating_enabled'] ?? 'off') === 'on'
10442 - ),
10443 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($this->options['satisfaction_rating_idle_seconds'] ?? 60))),
10444 - 'satisfaction_rating_copy' => array(
10445 - 'question' => !empty($this->options['satisfaction_rating_question']) ? esc_html($this->options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
10446 - 'helpful' => esc_html__('Helpful', 'mxchat'),
10447 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
10448 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
10449 - 'thanks' => !empty($this->options['satisfaction_rating_thanks']) ? esc_html($this->options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
10450 - 'placeholder' => !empty($this->options['satisfaction_rating_placeholder']) ? esc_html($this->options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
10451 - 'send' => esc_html__('Send', 'mxchat'),
10452 - 'skip' => esc_html__('Skip', 'mxchat'),
10453 - 'saved' => !empty($this->options['satisfaction_rating_saved']) ? esc_html($this->options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
10454 - ),
10455 - );
10456 -
10457 - // Determine delay time based on strategy
10458 - $delay_ms = 0;
10459 - switch ($loading_strategy) {
10460 - case 'delay_1s':
10461 - $delay_ms = 1000;
10462 - break;
10463 - case 'delay_3s':
10464 - $delay_ms = 3000;
10465 - break;
10466 - case 'delay_5s':
10467 - $delay_ms = 5000;
10468 - break;
10469 - }
10470 -
10471 - ?>
10472 - <script type="text/javascript">
10473 - (function() {
10474 - var mxchatLoaded = false;
10475 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
10476 - window.mxchatChat = mxchatChat;
10477 -
10478 - function loadMxChatScript() {
10479 - if (mxchatLoaded) return;
10480 - mxchatLoaded = true;
10481 -
10482 - function appendChatScript() {
10483 - var script = document.createElement('script');
10484 - script.src = <?php echo wp_json_encode($script_url); ?>;
10485 - script.type = 'text/javascript';
10486 - document.body.appendChild(script);
10487 - }
10488 -
10489 - if (typeof jQuery !== 'undefined') {
10490 - appendChatScript();
10491 - } else {
10492 - var jq = document.createElement('script');
10493 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
10494 - jq.onload = appendChatScript;
10495 - document.body.appendChild(jq);
10496 - }
10497 - }
10498 -
10499 - <?php if ($loading_strategy === 'on_interaction'): ?>
10500 - // Load on user interaction
10501 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
10502 - events.forEach(function(evt) {
10503 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
10504 - });
10505 - // Fallback: load after 8 seconds if no interaction
10506 - setTimeout(loadMxChatScript, 8000);
10507 - <?php else: ?>
10508 - // Load after specified delay
10509 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
10510 - <?php endif; ?>
10511 - })();
10512 - </script>
10513 - <?php
10514 -}
10515 -
10516 6402 /**
10517 6403 * Setup the cron jobs for rate limits with guard against multiple calls
10518 6404 */
10519 6405 public function setup_rate_limit_cron_jobs() {
@@ -10672,57 +6558,9 @@
10672 6558 $current_options = !empty($bot_options) ? $bot_options : $this->options;
10673 6559
10674 6560 // Use bot-specific rate limits if available, otherwise fall back to default
10675 6561 $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
10676 -
10677 - // -------------------------------------------------------------------
10678 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
10679 - // it acts as a hard ceiling across all users + all roles. Default is
10680 - // 'unlimited' so existing installs are unchanged. Counter key drops
10681 - // both <role> and <user_id> segments — single pool per bot.
10682 - // -------------------------------------------------------------------
10683 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
10684 - ? $current_options['rate_limits_global']
10685 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
10686 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
10687 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
10688 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
10689 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10690 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
10691 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
10692 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
10693 - if ((int) $global_data['count'] === 0) {
10694 - $global_data['timestamp'] = time();
10695 - update_option($global_option, $global_data);
10696 - }
10697 - $now = time();
10698 - $ts = (int) $global_data['timestamp'];
10699 - $reset = false;
10700 - switch ($global_timeframe) {
10701 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
10702 - case 'daily': $reset = ($now - $ts) >= 86400; break;
10703 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
10704 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
10705 - }
10706 - if ($reset) {
10707 - $global_data = ['count' => 0, 'timestamp' => $now];
10708 - update_option($global_option, $global_data);
10709 - }
10710 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
10711 - $global_msg = !empty($global_cfg['message'])
10712 - ? $global_cfg['message']
10713 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
10714 - return [
10715 - 'error' => true,
10716 - 'message' => $this->process_rate_limit_message_html($global_msg),
10717 - ];
10718 - }
10719 - // Reserve the slot for this request. Per-role check below also increments
10720 - // its own counter — that is intentional, both ceilings apply independently.
10721 - $global_data['count']++;
10722 - update_option($global_option, $global_data);
10723 - }
10724 -
6562 +
10725 6563 // Determine user role or if logged out
10726 6564 if (is_user_logged_in()) {
10727 6565 $user = wp_get_current_user();
10728 6566 $user_id = $user->ID;
@@ -11096,11 +6934,8 @@
11096 6934
11097 6935 /**
11098 6936 * AJAX handler to get system information for testing panel
11099 6937 */
11100 -/**
11101 - * AJAX handler to get system information for testing panel
11102 - */
11103 6938 public function mxchat_get_system_info() {
11104 6939 // Verify nonce for security
11105 6940 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11106 6941 wp_send_json_error(['message' => 'Invalid nonce']);
@@ -11117,25 +6952,11 @@
11117 6952 $system_prompt = isset($this->options['system_prompt_instructions'])
11118 6953 ? $this->options['system_prompt_instructions']
11119 6954 : 'No system prompt configured';
11120 6955
11121 - // Get selected model
11122 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
6956 + // Get selected model - FIXED: Use $this->options instead of $current_options
6957 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
11123 6958
11124 - // Check if OpenRouter is being used
11125 - $is_openrouter = ($selected_model === 'openrouter');
11126 - $openrouter_model = '';
11127 -
11128 - if ($is_openrouter) {
11129 - // Get the actual OpenRouter model that's selected
11130 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11131 - ? $this->options['openrouter_selected_model']
11132 - : 'No OpenRouter model selected';
11133 -
11134 - // Update selected_model display to show both
11135 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11136 - }
11137 -
11138 6959 // Get API key status (just check if they exist, don't expose the keys)
11139 6960 $api_status = [];
11140 6961 $api_status['openai'] = !empty($this->options['api_key']);
11141 6962 $api_status['claude'] = !empty($this->options['claude_api_key']);
@@ -11141,15 +6962,12 @@
11141 6962 $api_status['claude'] = !empty($this->options['claude_api_key']);
11142 6963 $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11143 6964 $api_status['xai'] = !empty($this->options['xai_api_key']);
11144 6965 $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11145 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11146 6966
11147 6967 wp_send_json_success([
11148 6968 'system_prompt' => $system_prompt,
11149 6969 'selected_model' => $selected_model,
11150 - 'is_openrouter' => $is_openrouter,
11151 - 'openrouter_model' => $openrouter_model,
11152 6970 'api_status' => $api_status
11153 6971 ]);
11154 6972 }
11155 6973
@@ -11188,42 +7006,24 @@
11188 7006 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11189 7007 wp_send_json_error(['message' => 'Invalid nonce']);
11190 7008 return;
11191 7009 }
11192 -
7010 +
11193 7011 // Only allow admin users
11194 7012 if (!current_user_can('administrator')) {
11195 7013 wp_send_json_error(['message' => 'Unauthorized']);
11196 7014 return;
11197 7015 }
11198 -
11199 - // Check OpenAI Vector Store first (takes priority)
11200 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11201 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11202 -
11203 - if ($use_vectorstore) {
11204 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11205 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11206 -
11207 - $kb_info = [
11208 - 'type' => 'OpenAI Vector Store',
11209 - 'status' => 'Active',
11210 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11211 - ];
11212 -
11213 - wp_send_json_success($kb_info);
11214 - return;
11215 - }
11216 -
7016 +
11217 7017 // Check Pinecone vs WordPress
11218 7018 $addon_options = get_option('mxchat_pinecone_addon_options', array());
11219 7019 $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11220 -
7020 +
11221 7021 $kb_info = [
11222 7022 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11223 7023 'status' => 'Active'
11224 7024 ];
11225 -
7025 +
11226 7026 // Get document count
11227 7027 if ($use_pinecone) {
11228 7028 $kb_info['documents'] = 'Connected to Pinecone';
11229 7029 $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
@@ -11233,9 +7033,9 @@
11233 7033 $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11234 7034 $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11235 7035 $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11236 7036 }
11237 -
7037 +
11238 7038 wp_send_json_success($kb_info);
11239 7039 }
11240 7040
11241 7041 /**
@@ -11317,13 +7117,9 @@
11317 7117 // Clear any other session-specific transients
11318 7118 delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
11319 7119 delete_transient("mxchat_include_pdf_in_context_{$session_id}");
11320 7120 delete_transient("mxchat_include_word_in_context_{$session_id}");
11321 -
11322 - // Clear form addon state (pending forms and submitted forms)
11323 - delete_option("mxchat_pending_form_{$session_id}");
11324 - delete_option("mxchat_submitted_forms_{$session_id}");
11325 -
7121 +
11326 7122 //error_log("MxChat: Cleared all data for session: {$session_id}");
11327 7123 }
11328 7124
11329 7125 /**
@@ -11398,9 +7194,9 @@
11398 7194 * Track URL clicks from chatbot responses
11399 7195 */
11400 7196 public function mxchat_track_url_click() {
11401 7197 // Verify nonce for security
11402 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7198 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11403 7199 wp_send_json_error(['message' => 'Invalid nonce']);
11404 7200 wp_die();
11405 7201 }
11406 7202
@@ -11451,9 +7247,9 @@
11451 7247 * Track the originating page where chat was started
11452 7248 */
11453 7249 public function mxchat_track_originating_page() {
11454 7250 // Verify nonce
11455 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7251 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11456 7252 wp_send_json_error(['message' => 'Invalid nonce']);
11457 7253 wp_die();
11458 7254 }
11459 7255
@@ -11498,169 +7294,8 @@
11498 7294 wp_send_json_success(['message' => 'Originating page tracked']);
11499 7295 wp_die();
11500 7296 }
11501 7297
11502 -/**
11503 - * Validate and clean URLs from AI response
11504 - * Removes any URLs that aren't in the knowledge base
11505 - *
11506 - * @param string $response_text The AI-generated response
11507 - * @param array $valid_urls Array of URLs from the knowledge base
11508 - * @return string Cleaned response with invalid URLs removed/flagged
11509 - */
11510 -private function validate_and_clean_urls($response_text, $valid_urls) {
11511 - // DEBUG: Log what we're working with
11512 - //error_log("=== MxChat URL Validation Debug ===");
11513 - //error_log("Valid URLs count: " . count($valid_urls));
11514 - //error_log("Valid URLs: " . print_r($valid_urls, true));
11515 - //error_log("Response text length: " . strlen($response_text));
11516 - //error_log("Response text preview: " . substr($response_text, 0, 500));
11517 -
11518 - // If no valid URLs provided or empty response, return as-is
11519 - if (empty($valid_urls) || empty($response_text)) {
11520 - //error_log("Validation skipped - empty valid_urls or response");
11521 - return $response_text;
11522 - }
11523 -
11524 - // Extract all URLs from the AI response
11525 - // This regex matches http:// and https:// URLs
11526 - preg_match_all(
11527 - '#\bhttps?://[^\s<>"\')\]]+#i',
11528 - $response_text,
11529 - $matches
11530 - );
11531 -
11532 - // If no URLs found in response, return as-is
11533 - if (empty($matches[0])) {
11534 - //error_log("No URLs found in response");
11535 - return $response_text;
11536 - }
11537 -
11538 - $found_urls = $matches[0];
11539 - $cleaned_response = $response_text;
11540 - $removed_count = 0;
11541 -
11542 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
11543 - $normalized_valid_urls = array_map(function($url) {
11544 - // Remove trailing slash
11545 - $url = rtrim($url, '/');
11546 - // Remove URL fragments (#section)
11547 - $url = preg_replace('/#.*$/', '', $url);
11548 - // Remove trailing punctuation that might have been captured
11549 - $url = rtrim($url, '.,;:!?');
11550 - return $url;
11551 - }, $valid_urls);
11552 -
11553 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
11554 -
11555 - foreach ($found_urls as $found_url) {
11556 - // Clean up the found URL (remove trailing punctuation that might have been captured)
11557 - $clean_found_url = rtrim($found_url, '.,;:!?)');
11558 -
11559 - // DEBUG: Log each URL being checked
11560 - //error_log("Checking found URL: " . $found_url);
11561 -
11562 - // Normalize for comparison
11563 - $normalized_found = rtrim($clean_found_url, '/');
11564 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
11565 -
11566 - //error_log("Normalized found URL: " . $normalized_found);
11567 -
11568 - // Check if this URL exists in our valid URLs list
11569 - $is_valid = false;
11570 -
11571 - //error_log("Starting validation checks for: " . $normalized_found);
11572 -
11573 - // First, try exact match
11574 - if (in_array($normalized_found, $normalized_valid_urls)) {
11575 - $is_valid = true;
11576 - //error_log("EXACT MATCH FOUND");
11577 - } else {
11578 - //error_log("No exact match, checking variations...");
11579 - // If no exact match, check if it's a variation (with query params, etc.)
11580 - foreach ($normalized_valid_urls as $valid_url) {
11581 - //error_log(" Comparing against valid URL: " . $valid_url);
11582 -
11583 - // Check if the found URL starts with a valid URL (handles query params)
11584 - if (strpos($normalized_found, $valid_url) === 0) {
11585 - // Check what comes after the valid URL
11586 - $remainder = substr($normalized_found, strlen($valid_url));
11587 -
11588 - // Only valid if:
11589 - // 1. Exact match (remainder is empty)
11590 - // 2. Query params (starts with ?)
11591 - // 3. Fragment (starts with #)
11592 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
11593 - $is_valid = true;
11594 - //error_log(" MATCH: Found URL is valid variation of base URL");
11595 - break;
11596 - } else {
11597 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
11598 - }
11599 - }
11600 - // Also check the reverse (in case valid URL has query params)
11601 - if (strpos($valid_url, $normalized_found) === 0) {
11602 - $is_valid = true;
11603 - //error_log(" MATCH: Valid URL starts with found URL");
11604 - break;
11605 - }
11606 - }
11607 -
11608 - if (!$is_valid) {
11609 - //error_log("NO MATCH FOUND - URL should be removed");
11610 - }
11611 - }
11612 -
11613 - // If URL is not valid, remove it from the response
11614 - if (!$is_valid) {
11615 - // Log the removal for debugging
11616 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
11617 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
11618 -
11619 - $removed_count++;
11620 -
11621 - // Check if URL is part of a markdown link: [text](url)
11622 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
11623 - if (preg_match($markdown_pattern, $cleaned_response)) {
11624 - //error_log("Found markdown link, removing but keeping text");
11625 - // Remove the markdown link but keep the text
11626 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
11627 - }
11628 - // Check if URL is part of an HTML link: <a href="url">text</a>
11629 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
11630 - //error_log("Found HTML link, removing but keeping text");
11631 - // Remove the HTML link but keep the text
11632 - $link_text = $link_match[1];
11633 - $cleaned_response = preg_replace(
11634 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
11635 - $link_text,
11636 - $cleaned_response
11637 - );
11638 - }
11639 - // Otherwise just remove the bare URL
11640 - else {
11641 - //error_log("Removing bare URL");
11642 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
11643 - }
11644 - }
11645 - }
11646 -
11647 - // Log summary if any URLs were removed
11648 - if ($removed_count > 0) {
11649 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
11650 - } else {
11651 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
11652 - }
11653 -
11654 - // Clean up any double spaces or awkward punctuation left behind
11655 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
11656 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
11657 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
11658 -
11659 - //error_log("Final cleaned response: " . $cleaned_response);
11660 -
11661 - return trim($cleaned_response);
11662 -}
11663 7298
11664 7299 /**
11665 7300 * AJAX handler to get current chat mode for a session
11666 7301 */
@@ -11665,9 +7300,9 @@
11665 7300 * AJAX handler to get current chat mode for a session
11666 7301 */
11667 7302 public function mxchat_get_current_chat_mode() {
11668 7303 // Verify nonce for security
11669 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7304 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11670 7305 wp_send_json_error(['message' => 'Invalid nonce']);
11671 7306 wp_die();
11672 7307 }
11673 7308