PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.3.3
MxChat – AI Chatbot & Content Generation for WordPress v2.3.3
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 +1509 -7132 3.2.92.3.3 View file →
@@ -10,247 +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 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
19 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
20 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
21 14
22 -/**
23 - * Setup streaming headers - call this right before actually streaming
24 - * This delays header setup to allow actions/forms to return JSON responses
25 - */
26 -/**
27 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
28 - *
29 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
30 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
31 - * specific "overloaded" / "rate limit" body string. Returns immediately on
32 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
33 - *
34 - * Drop-in replacement for wp_remote_post — returns the same shape
35 - * (WP_Error or response array) so the caller's existing error-handling
36 - * code path is unchanged.
37 - *
38 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
39 - * paths (the *_response_openai / *_response_claude / etc functions).
40 - * For the *_stream variants, the cURL initial-connect happens inside a
41 - * read-chunks loop — retrying there safely (without re-emitting partial
42 - * stream chunks to the client) is a separate problem. Streaming paths
43 - * are NOT wrapped in this build; tracked as a follow-on.
44 - *
45 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
46 - * (default true). When false, behavior is identical to plain wp_remote_post.
47 - */
48 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
49 - $opts = is_array($this->options ?? null) ? $this->options : array();
50 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
51 - (string) $opts['auto_retry_on_transient_error'] !== '0';
52 15
53 - if (!$enabled) {
54 - return wp_remote_post($url, $args);
55 - }
56 -
57 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
58 - $last_response = null;
59 -
60 - foreach ($backoffs as $i => $delay_ms) {
61 - if ($delay_ms > 0) {
62 - usleep($delay_ms * 1000);
63 - }
64 - $response = wp_remote_post($url, $args);
65 - $last_response = $response;
66 -
67 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
68 - return $response;
69 - }
70 -
71 - if (defined('WP_DEBUG') && WP_DEBUG) {
72 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
73 - : (int) wp_remote_retrieve_response_code($response);
74 - error_log(sprintf(
75 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
76 - $provider_hint ?: 'unknown',
77 - $i + 1,
78 - $code_for_log,
79 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
80 - ));
81 - }
82 - }
83 -
84 - return $last_response;
85 -}
86 -
87 16 /**
88 - * Returns true if a wp_remote_post response represents a TRANSIENT
89 - * provider error worth retrying. Conservative — only retries on signals
90 - * that are very likely to clear within a few seconds.
91 - *
92 - * Transient signals:
93 - * - WP_Error with timeout / connection / dns / ssl
94 - * - HTTP 429, 502, 503, 504
95 - * - Provider-specific overload bodies (gemini "overloaded", openai
96 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
97 - *
98 - * NOT transient (return false — fail-fast):
99 - * - 200/2xx (success)
100 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
101 - * budget; the user needs to fix something)
102 - * - Any other 4xx (assume permanent unless explicitly listed above)
103 - * - 5xx other than the four listed above (e.g. 500 generic server error
104 - * is often a malformed request on our side, not a transient outage)
105 - */
106 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
107 - if (is_wp_error($response)) {
108 - $code = $response->get_error_code();
109 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
110 - || stripos((string) $response->get_error_message(), 'timed out') !== false
111 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
112 - }
113 -
114 - $status = (int) wp_remote_retrieve_response_code($response);
115 - if (in_array($status, array(429, 502, 503, 504), true)) {
116 - return true;
117 - }
118 - if ($status >= 200 && $status < 300) {
119 - return false;
120 - }
121 - // Permanent 4xx that should fail fast — even with no body.
122 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
123 - return false;
124 - }
125 -
126 - // Provider-specific body inspection for the cases where the upstream
127 - // returns 200 with an error envelope (gemini does this for overload).
128 - $body = (string) wp_remote_retrieve_body($response);
129 - if ($body === '') {
130 - return false;
131 - }
132 - $lower = strtolower($body);
133 - $hint = strtolower((string) $provider_hint);
134 -
135 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
136 - || strpos($lower, 'high demand') !== false
137 - || strpos($lower, 'model is overloaded') !== false)) {
138 - return true;
139 - }
140 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
141 - || strpos($lower, '"type":"server_error"') !== false
142 - || strpos($lower, '"code":"server_error"') !== false)) {
143 - return true;
144 - }
145 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
146 - || strpos($lower, 'overloaded_error') !== false)) {
147 - return true;
148 - }
149 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
150 - return true;
151 - }
152 -
153 - return false;
154 -}
155 -
156 -/**
157 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
158 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
159 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
160 - * collect status separately from a plain wp_remote_post array shape, so the
161 - * non-streaming helper above can't be called directly. This delegate keeps
162 - * the classification rules identical across both paths.
163 - */
164 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
165 - if ($curl_errno) {
166 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
167 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
168 - return in_array($curl_errno, array(
169 - CURLE_OPERATION_TIMEDOUT,
170 - CURLE_COULDNT_CONNECT,
171 - CURLE_COULDNT_RESOLVE_HOST,
172 - CURLE_SSL_CONNECT_ERROR,
173 - CURLE_GOT_NOTHING,
174 - CURLE_SEND_ERROR,
175 - CURLE_RECV_ERROR,
176 - ), true);
177 - }
178 -
179 - $status = (int) $http_code;
180 - if (in_array($status, array(429, 502, 503, 504), true)) {
181 - return true;
182 - }
183 - if ($status >= 200 && $status < 300) {
184 - return false;
185 - }
186 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
187 - return false;
188 - }
189 -
190 - $body = (string) $body;
191 - if ($body === '') {
192 - return false;
193 - }
194 - $lower = strtolower($body);
195 - $hint = strtolower((string) $provider_hint);
196 -
197 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
198 - || strpos($lower, 'high demand') !== false
199 - || strpos($lower, 'model is overloaded') !== false)) {
200 - return true;
201 - }
202 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
203 - || strpos($lower, '"type":"server_error"') !== false
204 - || strpos($lower, '"code":"server_error"') !== false)) {
205 - return true;
206 - }
207 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
208 - || strpos($lower, 'overloaded_error') !== false)) {
209 - return true;
210 - }
211 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
212 - return true;
213 - }
214 -
215 - return false;
216 -}
217 -
218 -/**
219 - * Whether transient-error auto-retry is enabled in admin settings.
220 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
221 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
222 - */
223 -private function mxchat_retry_enabled() {
224 - $opts = is_array($this->options ?? null) ? $this->options : array();
225 - return !isset($opts['auto_retry_on_transient_error']) ||
226 - (string) $opts['auto_retry_on_transient_error'] !== '0';
227 -}
228 -
229 -private function setup_streaming_headers() {
230 - if ($this->streaming_headers_sent || headers_sent()) {
231 - return false;
232 - }
233 -
234 - // Disable output buffering
235 - while (ob_get_level()) {
236 - ob_end_flush();
237 - }
238 -
239 - // Set headers for SSE
240 - header('Content-Type: text/event-stream');
241 - header('Cache-Control: no-cache');
242 - header('Connection: keep-alive');
243 - header('X-Accel-Buffering: no');
244 -
245 - ob_implicit_flush(true);
246 - flush();
247 -
248 - $this->streaming_headers_sent = true;
249 - return true;
250 -}
251 -
252 -/**
253 17 * Class constructor
254 18 */
255 19 public function __construct() {
256 20 $this->options = get_option('mxchat_options');
@@ -308,109 +72,21 @@
308 72 add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
309 73 add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
310 74 add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
311 75 add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
312 - // Add to your existing constructor, in the section with other AJAX actions:
313 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
314 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
315 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
316 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
317 - // Add chat mode checking actions
318 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
319 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
320 76
321 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
322 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
323 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
77 +add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
324 78
325 - // Auto-email transcript action
326 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
327 79
328 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
329 -
330 -
331 80 }
332 81
333 -/**
334 - * Return a fresh nonce so cached pages can replace the stale one.
335 - * With `with_settings`, also returns the current behavior-gate settings so
336 - * the widget can correct stale inline-localized values (plan-32db95).
337 - */
338 -public function mxchat_refresh_nonce() {
339 - nocache_headers();
340 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
341 - if (!empty($_REQUEST['with_settings'])) {
342 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
343 - }
344 - wp_send_json_success($payload);
345 -}
346 -
347 -/**
348 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
349 - *
350 - * Every widget setting ships inline in page HTML via wp_localize_script, so
351 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
352 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
353 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
354 - * caches PHP can reach; this payload covers the rest — the widget requests
355 - * it on first open (via the nonce-refresh endpoints) and merges it over
356 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
357 - * nonce uses.
358 - *
359 - * Behavior gates + labels ONLY — colors stay inline because they're also
360 - * server-inline-styled, and a runtime swap would visibly flash.
361 - *
362 - * Both wp_localize_script blocks merge this exact array, so the inline and
363 - * refreshed payloads cannot drift.
364 - *
365 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
366 - * instead of trusting the instance copy.
367 - * @return array
368 - */
369 -public function get_dynamic_widget_settings($fresh = false) {
370 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
371 - if (!is_array($options)) {
372 - $options = array();
373 - }
374 - return array(
375 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
376 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
377 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
378 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
379 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
380 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
381 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
382 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
383 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
384 - // scalars to string, and (string) false === '' — which the widget's
385 - // old gate read as enabled (plan-4bba64). The filter keeps its
386 - // boolean contract; only the emitted value is stringified.
387 - 'satisfaction_rating_enabled' => apply_filters(
388 - 'mxchat_satisfaction_rating_enabled',
389 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
390 - ) ? 'on' : 'off',
391 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
392 - 'satisfaction_rating_copy' => array(
393 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
394 - 'helpful' => esc_html__('Helpful', 'mxchat'),
395 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
396 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
397 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
398 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
399 - 'send' => esc_html__('Send', 'mxchat'),
400 - 'skip' => esc_html__('Skip', 'mxchat'),
401 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
402 - ),
403 - );
404 -}
405 -
406 82 // In your core plugin's check_actions_for_addons method:
407 83 public function check_actions_for_addons($default, $message, $user_id, $session_id) {
408 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
84 + error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
409 85
410 86 $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
411 87
412 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
88 + error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
413 89
414 90 return $result;
415 91 }
416 92
@@ -426,22 +102,8 @@
426 102 wp_die();
427 103 }
428 104
429 105 $session_id = sanitize_text_field($_POST['session_id']);
430 -
431 - // SECURITY FIX: Verify session ownership before retrieving data
432 - // If IP/user changed, signal frontend to reset session instead of blocking
433 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
434 -
435 - // Check if this session has an owner recorded
436 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
437 -
438 - // Update session owner if it changed (e.g. IP changed due to network switch)
439 - // The session ID itself is the authentication — if the client has it, they own it
440 - if (!$session_owner || $session_owner !== $current_user_identifier) {
441 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
442 - }
443 -
444 106 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
445 107 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
446 108
447 109 if (empty($history)) {
@@ -458,25 +120,11 @@
458 120 'chat_mode' => $chat_mode
459 121 ]);
460 122 wp_die();
461 123 }
462 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
124 +
125 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
463 126 $history = get_option("mxchat_history_{$session_id}", []);
464 -
465 - // Check persistence setting - when OFF, only include messages from current page load
466 - $options = get_option('mxchat_options', []);
467 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
468 -
469 - // Filter history when persistence is OFF to match what the user sees
470 - if (!$persistence_enabled && $session_start_timestamp > 0) {
471 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
472 - // Include messages from this page load onwards
473 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
474 - });
475 - // Re-index array after filtering
476 - $history = array_values($history);
477 - }
478 -
479 127 $formatted_history = [];
480 128
481 129 // Adjusted for code-heavy conversations
482 130 $max_tokens = 120000; // Context window size
@@ -550,17 +198,8 @@
550 198
551 199 public function register_routes() {
552 200 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
553 201
554 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
555 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
556 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
557 - register_rest_route('mxchat/v1', '/nonce', [
558 - 'methods' => 'GET',
559 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
560 - 'permission_callback' => '__return_true',
561 - ]);
562 -
563 202 register_rest_route('mxchat/v1', '/stream', [
564 203 'methods' => 'GET',
565 204 'callback' => [$this, 'mxchat_stream_events'],
566 205 'permission_callback' => [$this, 'verify_chat_session'],
@@ -583,105 +222,12 @@
583 222 'callback' => [$this, 'handle_slack_messages'],
584 223 'permission_callback' => [$this, 'verify_slack_request'],
585 224 ]);
586 225
587 - // Telegram webhook endpoint
588 - register_rest_route('mxchat/v1', '/telegram-webhook', [
589 - 'methods' => 'POST',
590 - 'callback' => [$this, 'handle_telegram_webhook'],
591 - 'permission_callback' => [$this, 'verify_telegram_request'],
592 - ]);
593 -
594 226 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
595 227 }
596 228
597 229 /**
598 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
599 - * caches it for the session and includes it on every chat-send / stream-send /
600 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
601 - * we eliminate the entire class of "first-message Access denied" failures that
602 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
603 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
604 - * lives in the HTML body.
605 - *
606 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
607 - * client browser can't be used to flood the nonce-issuance path.
608 - *
609 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
610 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
611 - * backwards-compat window so cached pages still in users' browsers don't break
612 - * mid-session.
613 - *
614 - * @since 3.2.7
615 - */
616 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
617 - $ip = '';
618 - if (!empty($_SERVER['REMOTE_ADDR'])) {
619 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
620 - }
621 - if ($ip !== '') {
622 - // Best-effort rate limit. WP transients with sub-second TTL are racy
623 - // (parallel bursts can squeak through before set_transient completes);
624 - // we use 2s to make the gate slightly more reliable. Real production
625 - // rate-limiting at sub-second granularity needs Redis or DB row locks
626 - // — out of scope for this endpoint, which is already cheap.
627 - $key = 'mxchat_nonce_rl_' . md5($ip);
628 - if (get_transient($key)) {
629 - return new WP_REST_Response(array(
630 - 'error' => 'rate_limited',
631 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
632 - ), 429);
633 - }
634 - set_transient($key, 1, 2);
635 - }
636 -
637 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
638 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
639 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
640 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
641 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
642 - if ( ! is_user_logged_in() ) {
643 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
644 - if ( $maybe_uid ) {
645 - wp_set_current_user( $maybe_uid );
646 - }
647 - }
648 -
649 - $payload = array(
650 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
651 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
652 - );
653 -
654 - // plan-32db95: the widget's first-open refresh asks for current behavior
655 - // settings in the same round-trip, so stale inline-localized values on
656 - // cached pages get corrected without a second request. All values in
657 - // this payload already ship in public page HTML — nothing sensitive.
658 - if ($request->get_param('with_settings')) {
659 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
660 - }
661 -
662 - return new WP_REST_Response($payload, 200);
663 -}
664 -
665 -/**
666 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
667 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
668 - * action (inline-localized in older cached HTML). The legacy acceptance is
669 - * a 30-day backwards-compat window — to be removed in a follow-up release
670 - * after 2026-06-27.
671 - *
672 - * @param string $posted_nonce
673 - * @return bool
674 - */
675 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
676 - if (!is_string($posted_nonce) || $posted_nonce === '') {
677 - return false;
678 - }
679 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
680 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
681 -}
682 -
683 -/**
684 230 * Verify valid chat session
685 231 */
686 232 public function verify_chat_session($request) {
687 233 $session_id = $request->get_param('session_id');
@@ -717,11 +263,10 @@
717 263 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
718 264 return false;
719 265 }
720 266
721 - // Get raw request body from the WP_REST_Request object
722 - // (php://input may already be consumed by WordPress at this point)
723 - $request_body = $request->get_body();
267 + // Get raw request body
268 + $request_body = file_get_contents('php://input');
724 269
725 270 // Create the signature base string
726 271 $sig_basestring = "v0:{$timestamp}:{$request_body}";
727 272
@@ -731,42 +276,8 @@
731 276 // Compare signatures
732 277 return hash_equals($my_signature, $slack_signature);
733 278 }
734 279
735 -/**
736 - * Verify request is coming from Telegram.
737 - *
738 - * @param WP_REST_Request $request
739 - * @return bool True if valid, false otherwise.
740 - */
741 -public function verify_telegram_request($request) {
742 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
743 -
744 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
745 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
746 -
747 - if (empty($secret_token)) {
748 - // If no secret is configured, allow the request (for initial setup)
749 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
750 - return true;
751 - }
752 -
753 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
754 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
755 -
756 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
757 -
758 - if (empty($request_token)) {
759 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
760 - return false;
761 - }
762 -
763 - // Timing-safe comparison
764 - $result = hash_equals($secret_token, $request_token);
765 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
766 - return $result;
767 -}
768 -
769 280 public function mxchat_stream_events(WP_REST_Request $request) {
770 281 header('Content-Type: text/event-stream');
771 282 header('Cache-Control: no-cache');
772 283 header('Connection: keep-alive');
@@ -800,9 +311,9 @@
800 311
801 312
802 313
803 314
804 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
315 +private function mxchat_save_chat_message($session_id, $role, $message) {
805 316 global $wpdb;
806 317 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
807 318 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
808 319
@@ -813,27 +324,10 @@
813 324 "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
814 325 $session_id
815 326 ));
816 327 $is_new_session = ($existing_messages == 0);
817 -
818 - // Log for debugging
819 - if ($is_new_session) {
820 - //error_log("[DEBUG] This is a NEW session - first message");
821 - }
822 328 }
823 329
824 - // SECURITY FIX: Set session ownership for new sessions
825 - if ($is_new_session && $role === 'user') {
826 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
827 - $session_owner_key = "mxchat_session_owner_{$session_id}";
828 -
829 - // Only set ownership if not already set
830 - if (!get_option($session_owner_key)) {
831 - update_option($session_owner_key, $current_user_identifier, 'no');
832 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
833 - }
834 - }
835 -
836 330 // 1) Extract agent name if present
837 331 $agent_name = '';
838 332 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
839 333 $agent_name = $matches[1];
@@ -843,57 +337,35 @@
843 337 update_option($session_meta_key, $agent_name);
844 338 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
845 339 }
846 340 }
847 -
848 341 // 2) Generate unique message_id
849 342 $message_id = uniqid();
850 343 //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
851 -
852 344 // 3) Determine user_id
853 345 $user_id = is_user_logged_in() ? get_current_user_id() : 0;
854 -
855 346 // 4) Determine user_identifier
856 347 $user_identifier = $agent_name
857 348 ? $agent_name
858 349 : MxChat_User::mxchat_get_user_identifier();
859 -
860 350 // 5) Determine displayed_name
861 351 $user_email = MxChat_User::mxchat_get_user_email();
862 352 $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
863 -
864 353 // 6) Check for a saved email in wp_options
865 354 $email_option_key = "mxchat_email_{$session_id}";
866 355 $saved_email = get_option($email_option_key);
867 356 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
868 -
869 - // Check for a saved name in wp_options
870 - $name_option_key = "mxchat_name_{$session_id}";
871 - $saved_name = get_option($name_option_key);
872 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
873 -
874 - // If found, update DB user_email and user_name
875 - if ($saved_email || $saved_name) {
876 - $update_data = [];
877 - if ($saved_email) {
878 - $update_data['user_email'] = $saved_email;
879 - }
880 - if ($saved_name) {
881 - $update_data['user_name'] = $saved_name;
882 - }
883 -
884 - if (!empty($update_data)) {
885 - $update_res = $wpdb->update(
886 - $table_name,
887 - $update_data,
888 - ['session_id' => $session_id],
889 - array_fill(0, count($update_data), '%s'),
890 - ['%s']
891 - );
892 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
893 - }
357 + // If found, update DB user_email
358 + if ($saved_email) {
359 + $update_res = $wpdb->update(
360 + $table_name,
361 + ['user_email' => $saved_email],
362 + ['session_id' => $session_id],
363 + ['%s'],
364 + ['%s']
365 + );
366 + //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email update for session_id {$session_id}. update_res: {$update_res}");
894 367 }
895 -
896 368 // 7) Save to session history in wp_options
897 369 $history_key = "mxchat_history_{$session_id}";
898 370 $history = get_option($history_key, []);
899 371 $history[] = [
@@ -904,90 +376,18 @@
904 376 'agent_name' => $displayed_name,
905 377 ];
906 378 update_option($history_key, $history, 'no');
907 379 //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
908 -
909 380 // 8) Save the message to DB (INSERT)
910 381 $insert_data = [
911 382 'user_id' => $user_id,
912 383 'user_identifier'=> $user_identifier,
913 384 'user_email' => $saved_email ?: $user_email,
914 - 'user_name' => $saved_name ?: '', // Add name to insert data
915 385 'session_id' => $session_id,
916 386 'role' => $role,
917 387 'message' => $message,
918 388 'timestamp' => current_time('mysql', 1),
919 389 ];
920 -
921 - // IMPROVED: Handle originating page data
922 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
923 -
924 - if ($columns_exist) {
925 - if ($is_new_session && $role === 'user') {
926 - // For the first user message, set originating page data
927 -
928 - // First check if we have it from the parameter
929 - if ($originating_page && !empty($originating_page['url'])) {
930 - $insert_data['originating_page_url'] = $originating_page['url'];
931 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
932 -
933 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
934 - }
935 - // Otherwise check if it's stored in the instance property
936 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
937 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
938 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
939 -
940 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
941 -
942 - // Clear after using (= null, not unset(): unset() undeclares the property
943 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
944 - $this->pending_originating_page = null;
945 - }
946 - // Fallback to HTTP_REFERER if nothing else is available
947 - else if (isset($_SERVER['HTTP_REFERER'])) {
948 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
949 - $insert_data['originating_page_url'] = $referer_url;
950 -
951 - // Generate title from URL
952 - $parsed_url = parse_url($referer_url);
953 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
954 -
955 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
956 - $insert_data['originating_page_title'] = 'Homepage';
957 - } else {
958 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
959 - $insert_data['originating_page_title'] = ucwords(trim($title));
960 - }
961 -
962 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
963 - }
964 -
965 - // Store for this session so all messages have the same originating page
966 - if (!empty($insert_data['originating_page_url'])) {
967 - update_option("mxchat_originating_page_{$session_id}", [
968 - 'url' => $insert_data['originating_page_url'],
969 - 'title' => $insert_data['originating_page_title']
970 - ], 'no');
971 - }
972 - } else {
973 - // For subsequent messages in the session, use the stored originating page
974 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
975 - if ($stored_originating && !empty($stored_originating['url'])) {
976 - $insert_data['originating_page_url'] = $stored_originating['url'];
977 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
978 - }
979 - }
980 - }
981 -
982 - // Add RAG context if provided (for bot messages)
983 - if ($rag_context !== null && $role === 'bot') {
984 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
985 - if ($rag_context_column_exists) {
986 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
987 - }
988 - }
989 -
990 390 $wpdb->insert($table_name, $insert_data);
991 391 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
992 392
993 393 // 9) Send notification email if this is the first user message in a new session
@@ -998,17 +398,11 @@
998 398 'ip' => $_SERVER['REMOTE_ADDR']
999 399 ));
1000 400 }
1001 401
1002 - // 10) Schedule delayed transcript email if enabled and message is from user
1003 - if ($wpdb->insert_id && $role === 'user') {
1004 - $this->schedule_delayed_transcript_email($session_id);
1005 - }
1006 -
1007 402 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1008 403 return $message_id;
1009 404 }
1010 -
1011 405 private function send_new_chat_notification($session_id, $user_info = array()) {
1012 406 $options = get_option('mxchat_transcripts_options');
1013 407
1014 408 // Check if notifications are enabled
@@ -1051,202 +445,13 @@
1051 445 // Send email
1052 446 return wp_mail($to, $subject, $message);
1053 447 }
1054 448
1055 -/**
1056 - * Schedule delayed transcript email for a session
1057 - * Reschedules if a new user message is received
1058 - */
1059 -private function schedule_delayed_transcript_email($session_id) {
1060 - $options = get_option('mxchat_transcripts_options');
1061 -
1062 - // Check if auto-email is enabled
1063 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1064 - return;
1065 - }
1066 -
1067 - // Get notification email
1068 - $email = !empty($options['mxchat_notification_email']) ?
1069 - $options['mxchat_notification_email'] :
1070 - get_option('admin_email');
1071 -
1072 - if (!is_email($email)) {
1073 - return;
1074 - }
1075 -
1076 - // Get delay in minutes (default 30)
1077 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1078 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1079 -
1080 - // Clear any existing scheduled event for this session
1081 - $hook = 'mxchat_send_delayed_transcript';
1082 - $args = array($session_id);
1083 - $timestamp = wp_next_scheduled($hook, $args);
1084 -
1085 - if ($timestamp) {
1086 - wp_unschedule_event($timestamp, $hook, $args);
1087 - }
1088 -
1089 - // Schedule new event
1090 - $schedule_time = time() + ($delay_minutes * 60);
1091 - wp_schedule_single_event($schedule_time, $hook, $args);
1092 -}
1093 -
1094 -/**
1095 - * Check if chat messages contain contact information (email or phone number)
1096 - *
1097 - * @param array $messages Array of message objects with 'message' property
1098 - * @param object|null $session_data Session data object with user_email property
1099 - * @return bool True if contact info found, false otherwise
1100 - */
1101 -private function chat_contains_contact_info($messages, $session_data = null) {
1102 - // Check if session already has a stored email
1103 - if ($session_data && !empty($session_data->user_email)) {
1104 - return true;
1105 - }
1106 -
1107 - // Email regex pattern
1108 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1109 -
1110 - // Phone number patterns (covers various formats including international, WhatsApp style)
1111 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1112 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1113 -
1114 - // Only check user messages (not assistant responses)
1115 - foreach ($messages as $msg) {
1116 - if ($msg->role !== 'user') {
1117 - continue;
1118 - }
1119 -
1120 - $message_text = $msg->message;
1121 -
1122 - // Check for email
1123 - if (preg_match($email_pattern, $message_text)) {
1124 - return true;
1125 - }
1126 -
1127 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1128 - if (preg_match($phone_pattern, $message_text, $matches)) {
1129 - // Count actual digits to avoid matching short numbers
1130 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1131 - if (strlen($digits_only) >= 7) {
1132 - return true;
1133 - }
1134 - }
1135 - }
1136 -
1137 - return false;
1138 -}
1139 -
1140 -/**
1141 - * Send the delayed transcript email with .txt attachment
1142 - */
1143 -public function mxchat_send_delayed_transcript($session_id) {
1144 - global $wpdb;
1145 -
1146 - $options = get_option('mxchat_transcripts_options');
1147 -
1148 - // Get notification email
1149 - $to = !empty($options['mxchat_notification_email']) ?
1150 - $options['mxchat_notification_email'] :
1151 - get_option('admin_email');
1152 -
1153 - if (!is_email($to)) {
1154 - return false;
1155 - }
1156 -
1157 - // Get all messages for this session
1158 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1159 - $messages = $wpdb->get_results($wpdb->prepare(
1160 - "SELECT role, message, timestamp FROM {$table_name}
1161 - WHERE session_id = %s
1162 - ORDER BY timestamp ASC",
1163 - $session_id
1164 - ));
1165 -
1166 - if (empty($messages)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get session metadata
1171 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1172 - $session_data = $wpdb->get_row($wpdb->prepare(
1173 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1174 - $session_id
1175 - ));
1176 -
1177 - // Check if contact info is required and if it's present
1178 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1179 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1180 - // Contact info required but not found - skip sending
1181 - return false;
1182 - }
1183 -
1184 - // Build transcript content
1185 - $transcript_content = "Chat Transcript\n";
1186 - $transcript_content .= "================\n\n";
1187 - $transcript_content .= "Session ID: " . $session_id . "\n";
1188 -
1189 - if ($session_data) {
1190 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1191 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1192 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1193 - }
1194 -
1195 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1196 -
1197 - // Add messages
1198 - foreach ($messages as $msg) {
1199 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1200 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1201 - $transcript_content .= $msg->message . "\n\n";
1202 - }
1203 -
1204 - // Create temporary file for attachment using WP_Filesystem
1205 - $upload_dir = wp_upload_dir();
1206 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1207 - global $wp_filesystem;
1208 - if (empty($wp_filesystem)) {
1209 - require_once ABSPATH . 'wp-admin/includes/file.php';
1210 - WP_Filesystem();
1211 - }
1212 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1213 -
1214 - // Prepare email
1215 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1216 -
1217 - $message = "Please find attached the full chat transcript.\n\n";
1218 - $message .= "Session ID: {$session_id}\n";
1219 -
1220 - if ($session_data) {
1221 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1222 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1223 - }
1224 -
1225 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1226 -
1227 - // Send email with attachment
1228 - $attachments = array($temp_file);
1229 - $result = wp_mail($to, $subject, $message, '', $attachments);
1230 -
1231 - // Clean up temporary file
1232 - if (file_exists($temp_file)) {
1233 - unlink($temp_file);
1234 - }
1235 -
1236 - return $result;
1237 -}
1238 -
1239 -
1240 -
1241 449 public function mxchat_handle_save_email_and_response() {
1242 450 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1243 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1244 451
1245 - nocache_headers();
1246 -
1247 452 // Validate nonce
1248 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
453 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1249 454 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1250 455 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1251 456 wp_die();
1252 457 }
@@ -1252,41 +457,22 @@
1252 457 }
1253 458
1254 459 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1255 460 $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1256 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1257 461
1258 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
462 + //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}");
1259 463
1260 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
464 + if (empty($session_id) || empty($email)) {
1261 465 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1262 466 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1263 467 wp_die();
1264 468 }
1265 469
1266 - // Validate name if provided (check if name field is enabled and name is required)
1267 - $options = get_option('mxchat_options', []);
1268 - $name_field_enabled = isset($options['enable_name_field']) &&
1269 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1270 -
1271 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1272 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1273 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1274 - wp_die();
1275 - }
470 + // 1) Always store in wp_options
471 + $option_key = "mxchat_email_{$session_id}";
472 + update_option($option_key, $email);
473 + //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$option_key} => {$email}");
1276 474
1277 - // 1) Always store email in wp_options
1278 - $email_option_key = "mxchat_email_{$session_id}";
1279 - update_option($email_option_key, $email, 'no');
1280 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1281 -
1282 - // Store name in wp_options if provided
1283 - if (!empty($name)) {
1284 - $name_option_key = "mxchat_name_{$session_id}";
1285 - update_option($name_option_key, $name, 'no');
1286 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1287 - }
1288 -
1289 475 // 2) (Optional) Also store in DB if a row already exists
1290 476 global $wpdb;
1291 477 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1292 478
@@ -1296,30 +482,21 @@
1296 482
1297 483 //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1298 484
1299 485 if ($session_count) {
1300 - // Update both user_email and user_name if row(s) exist
1301 - if (!empty($name)) {
1302 - $update_sql = $wpdb->prepare(
1303 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1304 - $email,
1305 - $name,
1306 - $session_id
1307 - );
1308 - } else {
1309 - $update_sql = $wpdb->prepare(
1310 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1311 - $email,
1312 - $session_id
1313 - );
1314 - }
486 + // Update user_email if row(s) exist
487 + $update_sql = $wpdb->prepare(
488 + "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
489 + $email,
490 + $session_id
491 + );
1315 492 $wpdb->query($update_sql);
1316 493 //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1317 494 } else {
1318 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
495 + //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email is only in wp_options.");
1319 496 }
1320 497
1321 - // Provide success response (same as original)
498 + // Provide success response
1322 499 $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1323 500 //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1324 501 wp_send_json_success(['message' => $bot_message]);
1325 502 wp_die();
@@ -1327,17 +504,15 @@
1327 504
1328 505 public function mxchat_check_email_provided() {
1329 506 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1330 507
1331 - nocache_headers();
1332 -
1333 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
508 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1334 509 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1335 510 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1336 511 }
1337 512
1338 513 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1339 - if (empty($session_id) || $session_id === 'null') {
514 + if (empty($session_id)) {
1340 515 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1341 516 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1342 517 }
1343 518
@@ -1344,109 +519,49 @@
1344 519 // Check if the user is logged in
1345 520 if (is_user_logged_in()) {
1346 521 $current_user = wp_get_current_user();
1347 522 //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1348 -
1349 - // Get user's display name for logged in users
1350 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1351 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1352 -
1353 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1354 - if (!empty($user_name)) {
1355 - $response_data['name'] = $user_name;
1356 - }
1357 -
1358 - wp_send_json_success($response_data);
523 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
1359 524 }
1360 525
1361 - // Check if name field is required
1362 - $options = get_option('mxchat_options', []);
1363 - $name_field_enabled = isset($options['enable_name_field']) &&
1364 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
526 + $option_key = "mxchat_email_{$session_id}";
527 + $stored_email = get_option($option_key, '');
1365 528
1366 - $email_option_key = "mxchat_email_{$session_id}";
1367 - $stored_email = get_option($email_option_key, '');
1368 -
1369 - // Check for stored name
1370 - $name_option_key = "mxchat_name_{$session_id}";
1371 - $stored_name = get_option($name_option_key, '');
529 + //error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
1372 530
1373 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1374 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1375 -
1376 - // Check if we have email and name (if name is required)
1377 - $has_required_info = !empty($stored_email);
1378 -
1379 - if ($name_field_enabled) {
1380 - $has_required_info = $has_required_info && !empty($stored_name);
1381 - }
1382 -
1383 - if ($has_required_info) {
1384 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1385 -
1386 - $response_data = ['email' => $stored_email];
1387 - if (!empty($stored_name)) {
1388 - $response_data['name'] = $stored_name;
1389 - }
1390 -
1391 - wp_send_json_success($response_data);
531 + if (!empty($stored_email)) {
532 + //error_log("[DEBUG] mxchat_check_email_provided -> Email found, returning success");
533 + wp_send_json_success(['email' => $stored_email]);
1392 534 } else {
1393 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
535 + //error_log("[DEBUG] mxchat_check_email_provided -> No email found, returning error");
1394 536 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1395 537 }
1396 538 }
1397 539
1398 -/**
1399 - * Send error response in appropriate format based on streaming mode
1400 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1401 - *
1402 - * @param string $error_message The error message to display
1403 - * @param string $error_code Optional error code for debugging
1404 - */
1405 -private function send_error_response($error_message, $error_code = 'api_error') {
1406 - if ($this->is_streaming) {
1407 - echo "data: " . json_encode([
1408 - 'error' => true,
1409 - 'error_message' => $error_message,
1410 - 'error_code' => $error_code,
1411 - 'text' => $error_message,
1412 - 'message' => $error_message
1413 - ]) . "\n\n";
1414 - echo "data: [DONE]\n\n";
1415 - flush();
1416 - } else {
1417 - wp_send_json_error([
1418 - 'error_message' => $error_message,
1419 - 'error_code' => $error_code
1420 - ]);
1421 - }
1422 - wp_die();
1423 -}
1424 -
1425 540 public function mxchat_handle_chat_request() {
1426 541 global $wpdb;
1427 542
1428 - // Debug: Log incoming bot_id
1429 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1430 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1431 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
543 + // NEW: Check if this is a streaming request
544 + $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat';
1432 545
1433 - // Get bot-specific options
1434 - $bot_options = $this->get_bot_options($bot_id);
1435 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
546 + // NEW: Set streaming headers if needed
547 + if ($is_streaming) {
548 + // Disable output buffering
549 + while (ob_get_level()) {
550 + ob_end_flush(); // Changed from ob_end_clean()
551 + }
552 +
553 + // Set headers for SSE
554 + header('Content-Type: text/event-stream');
555 + header('Cache-Control: no-cache');
556 + header('Connection: keep-alive');
557 + header('X-Accel-Buffering: no');
558 +
559 + // Add these new lines:
560 + ob_implicit_flush(true);
561 + flush();
562 + }
1436 563
1437 - // Check if this is a streaming request
1438 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1439 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1440 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1441 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1442 -
1443 - // ADDED: Store streaming state in class property for use in private methods
1444 - $this->is_streaming = $is_streaming;
1445 -
1446 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1447 - // This allows actions/forms to return JSON responses without header conflicts
1448 -
1449 564 // Check if MX Chat Moderation is active
1450 565 if (class_exists('MX_Chat_Moderation')) {
1451 566 // Get user email and IP
1452 567 $user_email = '';
@@ -1511,911 +626,598 @@
1511 626
1512 627 // Rest of your existing code...
1513 628 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1514 629
1515 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1516 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1517 - // the frontend FormData.append() to stringify a null session_id into the literal
1518 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1519 - // ghost sessions that group every visitor's first message under one row.
1520 - if ($session_id === 'null' || $session_id === 'undefined') {
1521 - $session_id = '';
1522 - }
1523 -
1524 630 if (empty($session_id)) {
1525 631 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1526 632 wp_die();
1527 633 }
1528 634
1529 - // Update session owner if it changed (e.g. IP changed due to network switch)
1530 - // The session ID itself is the authentication — if the client has it, they own it
1531 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1532 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1533 -
1534 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1535 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1536 - }
1537 -
1538 635 // Validate and sanitize the incoming message
1539 636 if (empty($_POST['message'])) {
1540 637 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1541 638 wp_die();
1542 639 }
1543 -
1544 -
1545 - // Track originating page for first message in session
1546 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1547 640
1548 - // Check if originating page columns exist
1549 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1550 -
1551 - if ($columns_exist) {
1552 - // Check if this session already has messages
1553 - $message_count = $wpdb->get_var($wpdb->prepare(
1554 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1555 - $session_id
1556 - ));
641 + // NEW: Get page context if provided
642 + $page_context = null;
643 + if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
644 + $page_context_raw = stripslashes($_POST['page_context']);
645 + $page_context = json_decode($page_context_raw, true);
1557 646
1558 - // If this is the first message in the session
1559 - if ($message_count == 0) {
1560 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1561 - $originating_url = '';
1562 - $originating_title = '';
647 + // Validate page context structure
648 + if (is_array($page_context) &&
649 + isset($page_context['url']) &&
650 + isset($page_context['title']) &&
651 + isset($page_context['content'])) {
1563 652
1564 - // Try to get from POST data first (sent by JavaScript)
1565 - if (isset($_POST['current_page_url'])) {
1566 - $originating_url = esc_url_raw($_POST['current_page_url']);
1567 - $originating_title = isset($_POST['current_page_title'])
1568 - ? sanitize_text_field($_POST['current_page_title'])
1569 - : '';
1570 - }
1571 - // Fallback to HTTP_REFERER if not provided by JavaScript
1572 - else if (isset($_SERVER['HTTP_REFERER'])) {
1573 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1574 - }
1575 -
1576 - // Generate title if we have URL but no title
1577 - if ($originating_url && empty($originating_title)) {
1578 - $parsed_url = parse_url($originating_url);
1579 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1580 -
1581 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1582 - $originating_title = 'Homepage';
1583 - } else {
1584 - // Clean up the path to make a readable title
1585 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1586 - $originating_title = ucwords(trim($originating_title));
1587 - }
1588 - }
1589 -
1590 - // Store for later use when saving the message
1591 - $this->pending_originating_page = [
1592 - 'url' => $originating_url,
1593 - 'title' => $originating_title
1594 - ];
653 + // Sanitize page context
654 + $page_context['url'] = esc_url_raw($page_context['url']);
655 + $page_context['title'] = sanitize_text_field($page_context['title']);
656 + $page_context['content'] = wp_kses_post($page_context['content']);
657 + } else {
658 + $page_context = null;
1595 659 }
1596 660 }
1597 -
1598 -
1599 661
1600 - // Get page context if provided
1601 - $page_context = null;
1602 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1603 - $page_context_raw = stripslashes($_POST['page_context']);
1604 - $page_context = json_decode($page_context_raw, true);
1605 -
1606 - // Validate page context structure
1607 - if (is_array($page_context) &&
1608 - isset($page_context['url']) &&
1609 - isset($page_context['title']) &&
1610 - isset($page_context['content'])) {
1611 -
1612 - // Sanitize page context
1613 - $page_context['url'] = esc_url_raw($page_context['url']);
1614 - $page_context['title'] = sanitize_text_field($page_context['title']);
1615 - $page_context['content'] = wp_kses_post($page_context['content']);
1616 - } else {
1617 - $page_context = null;
1618 - }
1619 - }
662 + // Modify the message sanitization to preserve PHP tags in code blocks
663 + $allowed_tags = [
664 + 'pre' => [],
665 + 'code' => ['class' => true],
666 + 'span' => ['class' => true],
667 + 'div' => ['class' => true],
668 + ];
1620 669
1621 - // Modify the message sanitization to preserve PHP tags in code blocks
1622 - $allowed_tags = [
1623 - 'pre' => [],
1624 - 'code' => ['class' => true],
1625 - 'span' => ['class' => true],
1626 - 'div' => ['class' => true],
1627 - ];
670 + // First preserve code blocks
671 + $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
672 + return htmlspecialchars_decode($matches[0]);
673 + }, $_POST['message']);
1628 674
1629 - // First preserve code blocks
1630 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1631 - return htmlspecialchars_decode($matches[0]);
1632 - }, $_POST['message']);
675 + // Then apply sanitization
676 + $message = wp_kses($message, $allowed_tags);
1633 677
1634 - // Then apply sanitization
1635 - $message = wp_kses($message, $allowed_tags);
678 + // Preserve code blocks from markdown conversion
679 + $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
680 + $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1636 681
1637 - // Preserve code blocks from markdown conversion
1638 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1639 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1640 -
1641 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1642 - // Always initialize testing data for admins (no toggle needed)
1643 - $testing_data = null;
1644 - if (current_user_can('administrator')) {
1645 - // For vision messages, use the original user message for the query display
1646 - $query_for_testing = $message;
1647 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1648 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1649 - }
1650 -
1651 - $testing_data = [
1652 - 'query' => $query_for_testing,
1653 - 'timestamp' => time(),
1654 - 'top_matches' => [],
1655 - 'action_matches' => [], // Initialize action matches array
1656 - 'page_context' => $page_context, // Include page context in testing data
1657 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1658 - 'bot_id' => $bot_id // Include bot ID in testing data
1659 - ];
1660 -
1661 - // Get similarity threshold from bot options or default options
1662 - $similarity_threshold = isset($current_options['similarity_threshold'])
1663 - ? ((int) $current_options['similarity_threshold']) / 100
1664 - : 0.35;
1665 -
1666 - $testing_data['similarity_threshold'] = $similarity_threshold;
1667 -
1668 - // Determine knowledge base type using bot-specific config
1669 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1670 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1671 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
682 +// ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
683 + // Always initialize testing data for admins (no toggle needed)
684 + $testing_data = null;
685 + if (current_user_can('administrator')) {
686 + // For vision messages, use the original user message for the query display
687 + $query_for_testing = $message;
688 + if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
689 + $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1672 690 }
1673 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
691 +
692 + $testing_data = [
693 + 'query' => $query_for_testing,
694 + 'timestamp' => time(),
695 + 'top_matches' => [],
696 + 'action_matches' => [], // NEW: Initialize action matches array
697 + 'page_context' => $page_context, // NEW: Include page context in testing data
698 + 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed']
699 + ];
700 +
701 + // Get similarity threshold
702 + $similarity_threshold = isset($this->options['similarity_threshold'])
703 + ? ((int) $this->options['similarity_threshold']) / 100
704 + : 0.75;
705 +
706 + $testing_data['similarity_threshold'] = $similarity_threshold;
707 +
708 + // Determine knowledge base type
709 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
710 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
711 + $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
712 + }
713 + // ===== END SIMPLIFIED TESTING INITIALIZATION =====
1674 714
1675 - // Add debug before and after:
1676 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1677 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1678 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
715 +// Add debug before and after:
716 +error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
717 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
718 +error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
1679 719
1680 720
1681 - // If the pre-processing returned a result (not the original message), use it directly
1682 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1683 - // Save the AI response
1684 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1685 -
1686 - // Save HTML content if provided
1687 - if (!empty($pre_processed_result['html'])) {
1688 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1689 - }
1690 -
1691 - // Add testing data if admin
1692 - $response_data = [
1693 - 'text' => $pre_processed_result['text'],
1694 - 'html' => $pre_processed_result['html'] ?? '',
1695 - 'session_id' => $session_id
1696 - ];
1697 -
1698 - if ($testing_data !== null) {
1699 - $response_data['testing_data'] = $testing_data;
1700 - }
1701 -
1702 - wp_send_json($response_data);
1703 - wp_die();
721 + // If the pre-processing returned a result (not the original message), use it directly
722 + if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
723 + // Save the AI response
724 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
725 +
726 + // Save HTML content if provided
727 + if (!empty($pre_processed_result['html'])) {
728 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1704 729 }
730 +
731 + // Add testing data if admin
732 + $response_data = [
733 + 'text' => $pre_processed_result['text'],
734 + 'html' => $pre_processed_result['html'] ?? '',
735 + 'session_id' => $session_id
736 + ];
737 +
738 + if ($testing_data !== null) {
739 + $response_data['testing_data'] = $testing_data;
740 + }
741 +
742 + wp_send_json($response_data);
743 + wp_die();
744 + }
1705 745
1706 - // Save the user's message - handle vision processed messages differently
1707 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1708 - // For vision messages, save the original user message with image indicator
1709 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1710 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1711 - $image_count = intval($_POST['vision_images_count']);
1712 - $original_message .= " [{$image_count} image(s)]";
1713 - }
1714 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1715 - } else {
1716 - // Regular message - save as normal
1717 - $this->mxchat_save_chat_message($session_id, 'user', $message);
746 + // Save the user's message - handle vision processed messages differently
747 + if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
748 + // For vision messages, save the original user message with image indicator
749 + $original_message = sanitize_textarea_field($_POST['original_user_message']);
750 + if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
751 + $image_count = intval($_POST['vision_images_count']);
752 + $original_message .= " [{$image_count} image(s)]";
1718 753 }
754 + $this->mxchat_save_chat_message($session_id, 'user', $original_message);
755 + } else {
756 + // Regular message - save as normal
757 + $this->mxchat_save_chat_message($session_id, 'user', $message);
758 + }
1719 759
1720 -
760 + // Check if the message is an email address
1721 761 if (is_email($message)) {
1722 - // Add the email to Loops
1723 - $this->add_email_to_loops($message);
1724 -
1725 - // Get the user's success message instruction using current_options
1726 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1727 -
1728 - // Set instruction for AI using the user's success message
1729 - $this->current_action_instruction = $user_success_message;
1730 -
1731 - // Clear the email capture transient since we got the email
1732 - delete_transient('mxchat_email_capture_' . $user_id);
762 + // Add the email to Loops
763 + $this->add_email_to_loops($message);
764 +
765 + // Send success response
766 + $response_message = $this->options['email_capture_response'] ??
767 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
768 +
769 + // Clear streaming headers if they were set
770 + if ($is_streaming) {
771 + header_remove('Content-Type');
772 + header_remove('Cache-Control');
773 + header_remove('Connection');
774 + header_remove('X-Accel-Buffering');
775 + header('Content-Type: application/json');
1733 776 }
777 +
778 + $email_response = [
779 + 'success' => true,
780 + 'status' => 'email_captured',
781 + 'message' => $response_message
782 + ];
1734 783
1735 - // Check if we're in an email capture flow but user hasn't provided email yet
1736 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1737 - // Check if the message contains an email (not the whole message being an email)
1738 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1739 - $extracted_email = $matches[0];
1740 -
1741 - // Add the extracted email to Loops
1742 - $this->add_email_to_loops($extracted_email);
1743 -
1744 - // Get the user's success message instruction using current_options
1745 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1746 -
1747 - // Set instruction for AI using the user's success message
1748 - $this->current_action_instruction = $user_success_message;
1749 -
1750 - // Clear the email capture transient since we got the email
1751 - delete_transient('mxchat_email_capture_' . $user_id);
1752 - }
1753 - // If no email found but we're in capture mode, remind them
1754 - else {
1755 - // Get the original instruction to remind them using current_options
1756 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1757 - $this->current_action_instruction = $original_instruction;
1758 - }
784 + if ($testing_data !== null) {
785 + $email_response['testing_data'] = $testing_data;
1759 786 }
787 +
788 + wp_send_json($email_response);
789 + wp_die();
790 + }
1760 791
1761 - $intent_info = '';
792 + $intent_info = '';
1762 793
1763 - // Check chat mode
1764 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
794 + // Check chat mode
795 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1765 796
1766 - // Handle agent mode
1767 797 // Handle agent mode
1768 - if ($chat_mode === 'agent') {
1769 - // First, check for switch intent before doing anything else
1770 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
798 +// Handle agent mode
799 + if ($chat_mode === 'agent') {
800 + // First, check for switch intent before doing anything else
801 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1771 802
1772 - // Capture action analysis for testing panel after intent check
1773 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1774 - $testing_data['action_matches'] = $this->last_action_analysis;
803 + // NEW: Capture action analysis for testing panel after intent check
804 + if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
805 + $testing_data['action_matches'] = $this->last_action_analysis;
806 + }
807 +
808 + // If we matched an intent and it's the switch intent, handle it
809 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
810 + // Update chat mode first
811 + update_option("mxchat_mode_{$session_id}", 'ai');
812 +
813 + // Clear any existing PDF context to start fresh
814 + $this->clear_pdf_transients($session_id);
815 +
816 + // Prepare clean switch response
817 + $response_data = [
818 + 'text' => $this->fallbackResponse['text'],
819 + 'html' => '',
820 + 'session_id' => $session_id,
821 + 'chat_mode' => 'ai'
822 + ];
823 +
824 + if ($testing_data !== null) {
825 + $response_data['testing_data'] = $testing_data;
1775 826 }
1776 -
1777 - // Around line 506, in the agent mode handling section:
1778 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1779 - // Update chat mode first
1780 - update_option("mxchat_mode_{$session_id}", 'ai');
1781 -
1782 - // Clear any existing PDF context to start fresh
1783 - $this->clear_pdf_transients($session_id);
1784 -
1785 - // Prepare clean switch response with explicit chat_mode
1786 - $response_data = [
1787 - 'text' => $this->fallbackResponse['text'],
1788 - 'html' => $this->fallbackResponse['html'] ?? '',
1789 - 'session_id' => $session_id,
1790 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
827 +
828 + // Save the mode switch message
829 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
830 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
831 +
832 + // Send response and exit
833 + wp_send_json($response_data);
834 + wp_die();
835 + } elseif (!$intent_matched) {
836 + // No intent matched, handle live agent message
837 + try {
838 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
839 +
840 + $agent_response = [
841 + 'status' => 'waiting_for_agent',
842 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1791 843 ];
1792 -
844 +
1793 845 if ($testing_data !== null) {
1794 - $response_data['testing_data'] = $testing_data;
846 + $agent_response['testing_data'] = $testing_data;
1795 847 }
1796 -
1797 - // Save the mode switch message
1798 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1799 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1800 -
1801 - // Send response and exit
1802 - wp_send_json($response_data);
1803 - wp_die();
1804 - } elseif (!$intent_matched) {
1805 - // No intent matched, handle live agent message
1806 - try {
1807 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1808 848
1809 - $agent_response = [
1810 - 'status' => 'waiting_for_agent',
1811 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1812 - ];
1813 -
1814 - if ($testing_data !== null) {
1815 - $agent_response['testing_data'] = $testing_data;
1816 - }
1817 -
1818 - wp_send_json_success($agent_response);
1819 - } catch (\Exception $e) {
1820 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1821 - }
1822 - wp_die();
849 + wp_send_json_success($agent_response);
850 + } catch (\Exception $e) {
851 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1823 852 }
853 + wp_die();
1824 854 }
855 + }
1825 856
1826 - // Step 1: Check for new PDF URL in the message
1827 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1828 - $new_pdf_url = $matches[0];
857 + // Step 1: Check for new PDF URL in the message
858 + if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
859 + $new_pdf_url = $matches[0];
1829 860
1830 - // Check if this is likely a PDF-related request
1831 - $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1832 - $is_pdf_request = false;
861 + // Check if this is likely a PDF-related request
862 + $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
863 + $is_pdf_request = false;
1833 864
1834 - foreach ($pdf_keywords as $keyword) {
1835 - if (stripos($message, $keyword) !== false) {
1836 - $is_pdf_request = true;
1837 - break;
1838 - }
865 + foreach ($pdf_keywords as $keyword) {
866 + if (stripos($message, $keyword) !== false) {
867 + $is_pdf_request = true;
868 + break;
1839 869 }
870 + }
1840 871
1841 - // If it looks like a PDF request or we're waiting for a PDF URL
1842 - if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1843 - // Validate HTTPS
1844 - if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1845 - // Extract filename from URL
1846 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
872 + // If it looks like a PDF request or we're waiting for a PDF URL
873 + if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
874 + // Validate HTTPS
875 + if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
876 + // Extract filename from URL
877 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1847 878
1848 - // Clear previous PDF transients
1849 - $this->clear_pdf_transients($session_id);
879 + // Clear previous PDF transients
880 + $this->clear_pdf_transients($session_id);
1850 881
1851 - // Process new PDF using current_options
1852 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
1853 - $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
882 + // Process new PDF
883 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
884 + $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1854 885
1855 - if ($embeddings === 'too_many_pages') {
1856 - $error_text = sprintf(
1857 - $current_options['pdf_intent_error_text'] ??
1858 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1859 - $max_pages
1860 - );
1861 - $this->fallbackResponse['text'] = $error_text;
1862 - } elseif ($embeddings) {
1863 - // Store new PDF information
1864 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
886 + if ($embeddings === 'too_many_pages') {
887 + $error_text = sprintf(
888 + $this->options['pdf_intent_error_text'] ??
889 + esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
890 + $max_pages
891 + );
892 + $this->fallbackResponse['text'] = $error_text;
893 + } elseif ($embeddings) {
894 + // Store new PDF information
895 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1865 896
1866 - // If the filename is generic, create a more descriptive one
1867 - if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1868 - strpos($pdf_filename, '.php') !== false) {
1869 - $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1870 - }
897 + // If the filename is generic, create a more descriptive one
898 + if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
899 + strpos($pdf_filename, '.php') !== false) {
900 + $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
901 + }
1871 902
1872 - set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1873 - set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1874 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1875 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
903 + set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
904 + set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
905 + set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
906 + set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1876 907
1877 - $success_text = $current_options['pdf_intent_success_text'] ??
1878 - esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
908 + $success_text = $this->options['pdf_intent_success_text'] ??
909 + esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1879 910
1880 - $pdf_response = [
1881 - 'success' => true,
1882 - 'message' => $success_text,
1883 - 'data' => [
1884 - 'filename' => $pdf_filename
1885 - ]
1886 - ];
1887 -
1888 - if ($testing_data !== null) {
1889 - $pdf_response['testing_data'] = $testing_data;
1890 - }
1891 -
1892 - wp_send_json($pdf_response);
1893 - wp_die();
1894 - } else {
1895 - $error_text = $current_options['pdf_intent_error_text'] ??
1896 - esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1897 - $this->fallbackResponse['text'] = $error_text;
1898 - }
1899 -
1900 - $pdf_error_response = [
1901 - 'success' => false,
1902 - 'message' => $this->fallbackResponse['text']
911 + $pdf_response = [
912 + 'success' => true,
913 + 'message' => $success_text,
914 + 'data' => [
915 + 'filename' => $pdf_filename
916 + ]
1903 917 ];
1904 918
1905 919 if ($testing_data !== null) {
1906 - $pdf_error_response['testing_data'] = $testing_data;
920 + $pdf_response['testing_data'] = $testing_data;
1907 921 }
1908 922
1909 - wp_send_json($pdf_error_response);
923 + wp_send_json($pdf_response);
1910 924 wp_die();
925 + } else {
926 + $error_text = $this->options['pdf_intent_error_text'] ??
927 + esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
928 + $this->fallbackResponse['text'] = $error_text;
1911 929 }
1912 - }
1913 - }
1914 930
1915 -
1916 - // Step 2: Detect intent and handle intent-based responses
1917 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1918 -
1919 - // Capture action analysis for testing panel after intent check
1920 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1921 - $testing_data['action_matches'] = $this->last_action_analysis;
1922 - }
1923 -
1924 - // Step 3: Handle the intent result appropriately
1925 - if ($intent_result !== false) {
1926 - // Intent was matched - ALWAYS send as JSON response, never streaming
1927 -
1928 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1929 - // Intent returned a direct response array
1930 - $response_data = [
1931 - 'text' => $intent_result['text'] ?? '',
1932 - 'html' => $intent_result['html'] ?? '',
1933 - 'session_id' => $session_id
931 + $pdf_error_response = [
932 + 'success' => false,
933 + 'message' => $this->fallbackResponse['text']
1934 934 ];
1935 -
1936 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1937 - if (isset($intent_result['chat_mode'])) {
1938 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1939 - }
1940 -
935 +
1941 936 if ($testing_data !== null) {
1942 - $response_data['testing_data'] = $testing_data;
937 + $pdf_error_response['testing_data'] = $testing_data;
1943 938 }
1944 939
1945 - wp_send_json($response_data);
940 + wp_send_json($pdf_error_response);
1946 941 wp_die();
1947 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1948 - // Intent returned true and set fallbackResponse
1949 -
1950 - // SAVE TO TRANSCRIPT
1951 - if (!empty($this->fallbackResponse['text'])) {
1952 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1953 - }
1954 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1955 - if (!empty($this->fallbackResponse['html'])) {
1956 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1957 - }
1958 -
1959 - $response_data = [
1960 - 'text' => $this->fallbackResponse['text'] ?? '',
1961 - 'html' => $this->fallbackResponse['html'] ?? '',
1962 - 'session_id' => $session_id
1963 - ];
1964 -
1965 - if (isset($this->fallbackResponse['chat_mode'])) {
1966 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1967 - }
1968 -
1969 - if ($testing_data !== null) {
1970 - $response_data['testing_data'] = $testing_data;
1971 - }
1972 -
1973 - wp_send_json($response_data);
1974 - wp_die();
1975 942 }
1976 943 }
944 + }
1977 945
1978 - // If we get here, no intent matched OR the intent didn't provide a usable response
1979 -
1980 - // Step 4: Generate AI response
1981 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1982 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
1983 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
1984 - $this->mxchat_increment_chat_count();
946 + // Check if there's an active recommendation flow session
947 + $flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
948 + if (!empty($flow_state) && isset($flow_state['flow_id'])) {
949 + // Create a dummy intent object that matches the original intent
950 + $dummy_intent = new stdClass();
951 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
952 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
1985 953
1986 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1987 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
1988 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
954 + // Call the recommendation flow handler directly
955 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
1989 956
1990 - // Check if the embedding generation returned an error
1991 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1992 - $error_message = $user_message_embedding['error'];
1993 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
1994 -
1995 - // FIXED: Send error in appropriate format based on streaming mode
1996 - if ($is_streaming) {
1997 - echo "data: " . json_encode([
1998 - 'error' => true,
1999 - 'error_message' => $error_message,
2000 - 'error_code' => $error_code,
2001 - 'text' => $error_message,
2002 - 'message' => $error_message
2003 - ]) . "\n\n";
2004 - echo "data: [DONE]\n\n";
2005 - flush();
2006 - } else {
2007 - wp_send_json_error([
2008 - 'error_message' => $error_message,
2009 - 'error_code' => $error_code
2010 - ]);
957 + // If the handler returned a response, send it
958 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
959 + // Save the bot's response to the chat history
960 + if (!empty($response_data['text'])) {
961 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
2011 962 }
963 + if (!empty($response_data['html'])) {
964 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
965 + }
966 +
967 + if ($testing_data !== null) {
968 + $response_data['testing_data'] = $testing_data;
969 + }
970 +
971 + // Send the response
972 + wp_send_json($response_data);
2012 973 wp_die();
2013 974 }
975 + }
2014 976
2015 - // Check if the embedding is valid
2016 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2017 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
977 + // Step 2: Detect intent and handle intent-based responses
978 + $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
2018 979
2019 - // FIXED: Send error in appropriate format based on streaming mode
980 + // NEW: Capture action analysis for testing panel after intent check
981 + if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
982 + $testing_data['action_matches'] = $this->last_action_analysis;
983 + }
984 +
985 + // Step 3: Handle the intent result appropriately
986 + if ($intent_result !== false) {
987 + // Intent was matched - ALWAYS send as JSON response, never streaming
988 +
989 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
990 + // Intent returned a direct response array
991 + $response_data = [
992 + 'text' => $intent_result['text'] ?? '',
993 + 'html' => $intent_result['html'] ?? '',
994 + 'session_id' => $session_id
995 + ];
996 +
997 + if ($testing_data !== null) {
998 + $response_data['testing_data'] = $testing_data;
999 + }
1000 +
1001 + // Clear streaming headers if they were set
2020 1002 if ($is_streaming) {
2021 - echo "data: " . json_encode([
2022 - 'error' => true,
2023 - 'error_message' => $error_message,
2024 - 'error_code' => 'invalid_embedding',
2025 - 'text' => $error_message,
2026 - 'message' => $error_message
2027 - ]) . "\n\n";
2028 - echo "data: [DONE]\n\n";
2029 - flush();
2030 - } else {
2031 - wp_send_json_error([
2032 - 'error_message' => $error_message,
2033 - 'error_code' => 'invalid_embedding'
2034 - ]);
1003 + header_remove('Content-Type');
1004 + header_remove('Cache-Control');
1005 + header_remove('Connection');
1006 + header_remove('X-Accel-Buffering');
1007 + header('Content-Type: application/json');
2035 1008 }
1009 +
1010 + wp_send_json($response_data);
2036 1011 wp_die();
2037 - }
2038 -
2039 - // Build context with both knowledge base and PDF content if available
2040 - $context_content = "User asked: '{$message}'\n\n";
2041 -
2042 - // Add action instruction if present (add this right after the above line)
2043 - if (!empty($this->current_action_instruction)) {
2044 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2045 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2046 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2047 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
1012 + }
1013 + else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1014 + // Intent returned true and set fallbackResponse
1015 + $response_data = [
1016 + 'text' => $this->fallbackResponse['text'] ?? '',
1017 + 'html' => $this->fallbackResponse['html'] ?? '',
1018 + 'session_id' => $session_id
1019 + ];
2048 1020
2049 - // Clear the instruction after using it
2050 - $this->current_action_instruction = null;
1021 + if ($testing_data !== null) {
1022 + $response_data['testing_data'] = $testing_data;
1023 + }
1024 +
1025 + // Clear streaming headers if they were set
1026 + if ($is_streaming) {
1027 + header_remove('Content-Type');
1028 + header_remove('Cache-Control');
1029 + header_remove('Connection');
1030 + header_remove('X-Accel-Buffering');
1031 + header('Content-Type: application/json');
1032 + }
1033 +
1034 + wp_send_json($response_data);
1035 + wp_die();
2051 1036 }
1037 + }
2052 1038
2053 -
2054 - // Add page context if available and contextual awareness is enabled using current_options
2055 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2056 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2057 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2058 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2059 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2060 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2061 - }
2062 -
2063 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2064 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
1039 + // If we get here, no intent matched OR the intent didn't provide a usable response
1040 +
1041 + // Step 4: Generate AI response
1042 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
1043 + $this->mxchat_increment_chat_count();
1044 +
1045 + // Generate embedding for the user's query
1046 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1047 +
1048 + // Check if the embedding generation returned an error
1049 + if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1050 + $error_message = $user_message_embedding['error'];
1051 + $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
2065 1052
2066 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2067 - // Use fresh options to ensure we get the latest setting value
2068 - $fresh_options = get_option('mxchat_options', []);
2069 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
1053 + wp_send_json_error([
1054 + 'error_message' => $error_message,
1055 + 'error_code' => $error_code
1056 + ]);
1057 + wp_die();
1058 + }
1059 +
1060 + // Check if the embedding is valid
1061 + if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
1062 + wp_send_json_error([
1063 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1064 + 'error_code' => 'invalid_embedding'
1065 + ]);
1066 + wp_die();
1067 + }
2070 1068
2071 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2072 - if ($citation_links_enabled && !empty($system_instructions)) {
2073 - preg_match_all(
2074 - '#\bhttps?://[^\s<>"\']+#i',
2075 - $system_instructions,
2076 - $system_instruction_urls
2077 - );
1069 + // Build context with both knowledge base and PDF content if available
1070 + $context_content = "User asked: '{$message}'\n\n";
2078 1071
2079 - if (!empty($system_instruction_urls[0])) {
2080 - // Merge with existing valid URLs
2081 - $this->current_valid_urls = array_merge(
2082 - $this->current_valid_urls,
2083 - $system_instruction_urls[0]
2084 - );
2085 - // Remove duplicates
2086 - $this->current_valid_urls = array_unique($this->current_valid_urls);
1072 + // NEW: Add page context if available and contextual awareness is enabled
1073 + if ($page_context && isset($this->options['contextual_awareness_toggle']) && $this->options['contextual_awareness_toggle'] === 'on') {
1074 + $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
1075 + $context_content .= "Page URL: " . $page_context['url'] . "\n";
1076 + $context_content .= "Page Title: " . $page_context['title'] . "\n";
1077 + $context_content .= "Page Content: " . $page_context['content'] . "\n";
1078 + $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
1079 + }
2087 1080
2088 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2089 - }
2090 - }
2091 -
2092 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2093 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2094 - // Update testing data with the REAL similarity analysis
2095 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2096 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2097 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2098 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2099 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2100 -}
2101 -// ===== END SIMILARITY DATA CAPTURE =====
1081 + // Get relevant content from knowledge base - THIS IS WHERE THE SIMILARITY ANALYSIS HAPPENS
1082 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
1083 +
1084 + // ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
1085 + if ($testing_data !== null && $this->last_similarity_analysis !== null) {
1086 + // Update testing data with the REAL similarity analysis
1087 + $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
1088 + $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1089 + $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
1090 + }
1091 + // ===== END SIMILARITY DATA CAPTURE =====
1092 +
1093 + if (!empty($relevant_content)) {
1094 + $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
1095 + } else {
1096 + $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
1097 + }
2102 1098
2103 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2104 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2105 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2106 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2107 -}
2108 -
2109 - if (!empty($relevant_content)) {
2110 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2111 - } else {
2112 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2113 - }
2114 -
2115 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2116 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2117 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2118 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2119 - foreach ($this->current_valid_urls as $url) {
2120 - $context_content .= "- " . $url . "\n";
1099 + // Check for and include PDF content
1100 + $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
1101 + $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
1102 + $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
1103 + if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
1104 + $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
1105 + if (!empty($relevant_pdf_pages)) {
1106 + $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
1107 + foreach ($relevant_pdf_pages as $page_data) {
1108 + $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2121 1109 }
2122 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2123 - $context_content .= "===== END APPROVED URLS =====\n\n";
1110 + $context_content .= "\n";
2124 1111 }
2125 -
2126 - // Check for and include PDF content
2127 - $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2128 - $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2129 - $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
2130 - if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
2131 - $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
2132 - if (!empty($relevant_pdf_pages)) {
2133 - $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
2134 - foreach ($relevant_pdf_pages as $page_data) {
2135 - $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2136 - }
2137 - $context_content .= "\n";
2138 - }
2139 - }
1112 + }
2140 1113
2141 - // Check for and include Word content
2142 - $word_url = get_transient('mxchat_word_url_' . $session_id);
2143 - $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
2144 - $word_filename = get_transient('mxchat_word_filename_' . $session_id);
2145 - if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
2146 - $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
2147 - if (!empty($relevant_word_chunks)) {
2148 - $context_content .= "Relevant content from Word document '{$word_filename}':\n";
2149 - foreach ($relevant_word_chunks as $chunk_data) {
2150 - $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
2151 - }
2152 - $context_content .= "\n";
1114 + // Check for and include Word content
1115 + $word_url = get_transient('mxchat_word_url_' . $session_id);
1116 + $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
1117 + $word_filename = get_transient('mxchat_word_filename_' . $session_id);
1118 + if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
1119 + $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
1120 + if (!empty($relevant_word_chunks)) {
1121 + $context_content .= "Relevant content from Word document '{$word_filename}':\n";
1122 + foreach ($relevant_word_chunks as $chunk_data) {
1123 + $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
2153 1124 }
1125 + $context_content .= "\n";
2154 1126 }
2155 -
2156 - $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
1127 + }
1128 +
1129 + $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2157 1130
2158 - // Extract model from current options for bot-specific model support
2159 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2160 -
2161 - $response = $this->mxchat_generate_response(
2162 - $context_content,
2163 - $current_options['api_key'] ?? $this->options['api_key'],
2164 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2165 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2166 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2167 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2168 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2169 - $conversation_history,
2170 - $is_streaming,
2171 - $session_id,
2172 - $testing_data,
2173 - $selected_model
2174 - );
2175 -
2176 - // Handle streaming vs non-streaming responses
2177 - if ($is_streaming) {
2178 - // Check if streaming actually happened or if it fell back to regular response
2179 - if ($response === true) {
2180 - wp_die();
2181 - }
2182 - // If we get here, streaming fell back to regular response, continue
2183 - // But if there's an error, we need to send it as SSE format since headers are already set
2184 - if (is_array($response) && isset($response['error'])) {
2185 - $error_message = $response['error'];
2186 - $error_code = $response['error_code'] ?? 'api_error';
2187 - // Send error in SSE format that the client JS can handle
2188 - echo "data: " . json_encode([
2189 - 'error' => true,
2190 - 'error_message' => $error_message,
2191 - 'error_code' => $error_code,
2192 - 'text' => $error_message, // Also include as text for fallback handling
2193 - 'message' => $error_message
2194 - ]) . "\n\n";
2195 - echo "data: [DONE]\n\n";
2196 - flush();
2197 - wp_die();
2198 - }
2199 - }
2200 -
2201 - // Check if the response is an error array (non-streaming mode)
2202 - if (is_array($response) && isset($response['error'])) {
2203 - wp_send_json_error([
2204 - 'error_message' => $response['error'],
2205 - 'error_code' => $response['error_code'] ?? 'api_error'
2206 - ]);
1131 + // Generate response
1132 + $response = $this->mxchat_generate_response(
1133 + $context_content,
1134 + $this->options['api_key'],
1135 + $this->options['xai_api_key'],
1136 + $this->options['claude_api_key'],
1137 + $this->options['deepseek_api_key'],
1138 + $this->options['gemini_api_key'],
1139 + $conversation_history,
1140 + $is_streaming,
1141 + $session_id,
1142 + $testing_data
1143 + );
1144 +
1145 + // Handle streaming vs non-streaming responses
1146 + if ($is_streaming) {
1147 + // Check if streaming actually happened or if it fell back to regular response
1148 + if ($response === true) {
2207 1149 wp_die();
2208 1150 }
1151 + // If we get here, streaming fell back to regular response, continue
1152 + }
2209 1153
2210 - // DEBUG: Check what we have
2211 - //error_log("=== BEFORE URL VALIDATION ===");
2212 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2213 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2214 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2215 -
2216 - // If we get here, the response is valid text - now validate URLs
2217 - if (!empty($this->current_valid_urls)) {
2218 - //error_log("CALLING validate_and_clean_urls");
2219 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2220 - } else {
2221 - //error_log("SKIPPING validation - current_valid_urls is empty");
2222 - }
2223 - // ===== END URL VALIDATION =====
2224 -
2225 - // Prepare RAG context data for storage (only include documents used for context)
2226 - $rag_context_for_storage = null;
2227 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2228 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2229 -
2230 - if ($has_rag_data || $has_action_data) {
2231 - $rag_context_for_storage = [];
2232 -
2233 - // Add RAG/source data if available
2234 - if ($has_rag_data) {
2235 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2236 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2237 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2238 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2239 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2240 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2241 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2242 - }
2243 -
2244 - // Add action analysis data if available
2245 - if ($has_action_data) {
2246 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2247 - }
2248 - }
2249 -
2250 - // Save the cleaned response with RAG context
2251 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2252 -
2253 - // Step 5: Save additional content if available
2254 - if (!empty($this->productCardHtml)) {
2255 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2256 - }
2257 -
2258 - if (!empty($this->fallbackResponse['html'])) {
2259 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2260 - }
2261 -
2262 - // Step 6: Return the response
2263 - // DEBUG: Check if newlines exist in the response
2264 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2265 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2266 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2267 -
2268 - $response_data = [
2269 - 'text' => $response,
2270 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2271 - 'session_id' => $session_id
2272 - ];
2273 -
2274 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2275 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2276 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2277 - }
2278 -
2279 - // Also pass it as a top-level field so JS can show a better error message to admins
2280 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2281 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2282 - }
2283 -
2284 - // Always add testing data for admins (no toggle needed)
2285 - if ($testing_data !== null) {
2286 - $response_data['testing_data'] = $testing_data;
2287 - }
2288 -
2289 - wp_send_json($response_data);
1154 + // Check if the response is an error array
1155 + if (is_array($response) && isset($response['error'])) {
1156 + wp_send_json_error([
1157 + 'error_message' => $response['error'],
1158 + 'error_code' => $response['error_code'] ?? 'api_error'
1159 + ]);
2290 1160 wp_die();
2291 -}
2292 -
2293 -/**
2294 - * Get bot-specific options for multi-bot functionality
2295 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2296 - */
2297 -// Also debug the bot options retrieval
2298 -private function get_bot_options($bot_id = 'default') {
2299 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2300 -
2301 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2302 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2303 - return array();
2304 1161 }
2305 1162
2306 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2307 -
2308 - if (!empty($bot_options)) {
2309 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2310 - if (isset($bot_options['similarity_threshold'])) {
2311 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2312 - }
1163 + // If we get here, the response is valid text
1164 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1165 +
1166 + // Step 5: Save additional content if available
1167 + if (!empty($this->productCardHtml)) {
1168 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2313 1169 }
2314 -
2315 - return is_array($bot_options) ? $bot_options : array();
2316 -}
2317 1170
2318 -/**
2319 - * Get bot-specific Pinecone configuration
2320 - * Used in the knowledge retrieval functions
2321 - */
2322 -// Also add debugging to your get_bot_pinecone_config function
2323 -private function get_bot_pinecone_config($bot_id = 'default') {
2324 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2325 -
2326 - // If default bot or multi-bot add-on not active, use default Pinecone config
2327 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2328 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2329 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2330 - $config = array(
2331 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2332 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2333 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2334 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2335 - );
2336 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2337 - return $config;
1171 + if (!empty($this->fallbackResponse['html'])) {
1172 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2338 1173 }
2339 -
2340 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2341 -
2342 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2343 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2344 -
2345 - if (!empty($bot_pinecone_config)) {
2346 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2347 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2348 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2349 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2350 - } else {
2351 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
1174 +
1175 + // Step 6: Return the response
1176 + $response_data = [
1177 + 'text' => $response,
1178 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1179 + 'session_id' => $session_id
1180 + ];
1181 +
1182 + // Always add testing data for admins (no toggle needed)
1183 + if ($testing_data !== null) {
1184 + $response_data['testing_data'] = $testing_data;
2352 1185 }
2353 -
2354 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
1186 +
1187 + wp_send_json($response_data);
1188 + wp_die();
2355 1189 }
2356 1190
2357 -
2358 1191 // Updated function to check intents and invoke the callback function
2359 1192 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2360 1193 global $wpdb;
2361 1194 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2362 1195
2363 - // Get the current bot_id
2364 - $current_bot_id = $this->get_current_bot_id($session_id);
2365 -
2366 1196 // Generate the user embedding
2367 1197 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2368 -
1198 +
2369 1199 // Check if embedding generation returned an error
2370 1200 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2371 1201 $error_message = $user_embedding['error'];
2372 1202 $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2373 -
2374 - // FIXED: Send error in appropriate format based on streaming mode
2375 - if ($this->is_streaming) {
2376 - echo "data: " . json_encode([
2377 - 'error' => true,
2378 - 'error_message' => $error_message,
2379 - 'error_code' => $error_code,
2380 - 'text' => $error_message,
2381 - 'message' => $error_message
2382 - ]) . "\n\n";
2383 - echo "data: [DONE]\n\n";
2384 - flush();
2385 - } else {
2386 - wp_send_json_error([
2387 - 'error_message' => $error_message,
2388 - 'error_code' => $error_code
2389 - ]);
2390 - }
1203 +
1204 + wp_send_json_error([
1205 + 'error_message' => $error_message,
1206 + 'error_code' => $error_code
1207 + ]);
2391 1208 wp_die();
2392 1209 }
2393 -
1210 +
2394 1211 // Check if embedding is valid
2395 1212 if (!is_array($user_embedding) || empty($user_embedding)) {
2396 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2397 -
2398 - // FIXED: Send error in appropriate format based on streaming mode
2399 - if ($this->is_streaming) {
2400 - echo "data: " . json_encode([
2401 - 'error' => true,
2402 - 'error_message' => $error_message,
2403 - 'error_code' => 'invalid_embedding',
2404 - 'text' => $error_message,
2405 - 'message' => $error_message
2406 - ]) . "\n\n";
2407 - echo "data: [DONE]\n\n";
2408 - flush();
2409 - } else {
2410 - wp_send_json_error([
2411 - 'error_message' => $error_message,
2412 - 'error_code' => 'invalid_embedding'
2413 - ]);
2414 - }
1213 + wp_send_json_error([
1214 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1215 + 'error_code' => 'invalid_embedding'
1216 + ]);
2415 1217 wp_die();
2416 1218 }
2417 -
1219 +
2418 1220 // Fetch intents from the database
2419 1221 $table_name = $wpdb->prefix . 'mxchat_intents';
2420 1222 if ($chat_mode === 'agent') {
2421 1223 $query = $wpdb->prepare(
@@ -2425,29 +1227,19 @@
2425 1227 $intents = $wpdb->get_results($query);
2426 1228 } else {
2427 1229 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2428 1230 }
2429 -
1231 +
2430 1232 if (empty($intents)) {
2431 1233 return false;
2432 1234 }
2433 -
2434 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2435 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2436 - $phrases_by_intent = [];
2437 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2438 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2439 - foreach ($all_phrases as $p) {
2440 - $phrases_by_intent[$p->intent_id][] = $p;
2441 - }
2442 - }
2443 -
1235 +
2444 1236 $highest_similarity = -INF;
2445 1237 $matched_intent = null;
2446 -
2447 - // Array to store action analysis for testing panel
1238 +
1239 + // NEW: Array to store action analysis for testing panel
2448 1240 $action_analysis = [];
2449 -
1241 +
2450 1242 foreach ($intents as $intent) {
2451 1243 // Additional check for enabled state
2452 1244 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 1245 if (!$is_enabled) {
@@ -2452,57 +1244,22 @@
2452 1244 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 1245 if (!$is_enabled) {
2454 1246 continue;
2455 1247 }
2456 -
2457 - // Check if this action is enabled for the current bot
2458 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2459 - continue;
2460 - }
2461 -
2462 - $best_similarity = -INF;
2463 - $matched_phrase_text = '';
2464 -
2465 - // Check legacy embedding vector (existing behavior)
1248 +
2466 1249 $intent_embedding_serialized = $intent->embedding_vector;
2467 1250 $intent_embedding = $intent_embedding_serialized
2468 1251 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2469 1252 : null;
2470 -
2471 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2472 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2473 - if ($legacy_similarity > $best_similarity) {
2474 - $best_similarity = $legacy_similarity;
2475 - $matched_phrase_text = 'legacy';
2476 - }
2477 - }
2478 -
2479 - // Check individual phrase vectors
2480 - if (isset($phrases_by_intent[$intent->id])) {
2481 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2482 - $phrase_embedding = $phrase_row->embedding_vector
2483 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2484 - : null;
2485 - if (!is_array($phrase_embedding)) {
2486 - continue;
2487 - }
2488 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2489 - if ($phrase_similarity > $best_similarity) {
2490 - $best_similarity = $phrase_similarity;
2491 - $matched_phrase_text = $phrase_row->phrase;
2492 - }
2493 - }
2494 - }
2495 -
2496 - // Skip if no valid embedding was found at all
2497 - if ($best_similarity === -INF) {
1253 +
1254 + if (!is_array($intent_embedding)) {
2498 1255 continue;
2499 1256 }
2500 -
2501 - $similarity = $best_similarity;
1257 +
1258 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2502 1259 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2503 -
2504 - // Store action analysis data for testing panel
1260 +
1261 + // NEW: Store action analysis data for testing panel
2505 1262 $action_analysis[] = [
2506 1263 'intent_label' => $intent->intent_label,
2507 1264 'callback_function' => $intent->callback_function,
2508 1265 'similarity' => round($similarity, 4),
@@ -2509,12 +1266,11 @@
2509 1266 'similarity_percentage' => round($similarity * 100, 2),
2510 1267 'threshold' => $intent_threshold,
2511 1268 'threshold_percentage' => round($intent_threshold * 100, 2),
2512 1269 'above_threshold' => $similarity >= $intent_threshold,
2513 - 'matched_phrase' => $matched_phrase_text,
2514 1270 'triggered' => false // Will be updated below if this intent is triggered
2515 1271 ];
2516 -
1272 +
2517 1273 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2518 1274 $highest_similarity = $similarity;
2519 1275 $matched_intent = $intent;
2520 1276 }
@@ -2519,9 +1275,9 @@
2519 1275 $matched_intent = $intent;
2520 1276 }
2521 1277 }
2522 1278
2523 - // Mark the triggered action if any
1279 + // NEW: Mark the triggered action if any
2524 1280 if ($matched_intent) {
2525 1281 foreach ($action_analysis as &$action) {
2526 1282 if ($action['intent_label'] === $matched_intent->intent_label) {
2527 1283 $action['triggered'] = true;
@@ -2529,9 +1285,9 @@
2529 1285 }
2530 1286 }
2531 1287 }
2532 1288
2533 - // Sort actions by similarity (highest first) and store for testing panel
1289 + // NEW: Sort actions by similarity (highest first) and store for testing panel
2534 1290 usort($action_analysis, function($a, $b) {
2535 1291 return $b['similarity'] <=> $a['similarity'];
2536 1292 });
2537 1293
@@ -2537,9 +1293,8 @@
2537 1293
2538 1294 // Store action analysis for testing panel capture
2539 1295 $this->last_action_analysis = $action_analysis;
2540 1296
2541 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2542 1297 if ($matched_intent) {
2543 1298 // If the callback is a method on this instance (core callback), call it directly
2544 1299 if (method_exists($this, $matched_intent->callback_function)) {
2545 1300 $callback_result = call_user_func(
@@ -2553,9 +1308,9 @@
2553 1308 } else {
2554 1309 // Otherwise, use apply_filters for add-on callbacks
2555 1310 $callback_result = apply_filters(
2556 1311 $matched_intent->callback_function,
2557 - false,
1312 + false, // default return value
2558 1313 $message,
2559 1314 $user_id,
2560 1315 $session_id,
2561 1316 $matched_intent
@@ -2561,18 +1316,11 @@
2561 1316 $matched_intent
2562 1317 );
2563 1318 }
2564 1319
2565 - // Handle the callback result properly
2566 1320 if ($callback_result !== false) {
2567 - // If callback returned an array with chat_mode, use it directly
2568 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2569 - $this->fallbackResponse = $callback_result;
2570 - return $callback_result; // Return the full array
2571 - } else {
2572 - $this->fallbackResponse = $callback_result;
2573 - return true;
2574 - }
1321 + $this->fallbackResponse = $callback_result;
1322 + return true;
2575 1323 }
2576 1324 }
2577 1325
2578 1326 return false;
@@ -2577,34 +1325,8 @@
2577 1325
2578 1326 return false;
2579 1327 }
2580 1328
2581 -/**
2582 - * Check if an action is enabled for a specific bot
2583 - */
2584 -private function is_action_enabled_for_bot($intent, $bot_id) {
2585 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2586 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2587 - return true;
2588 - }
2589 -
2590 - $enabled_bots = json_decode($intent->enabled_bots, true);
2591 -
2592 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2593 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2594 - return true;
2595 - }
2596 -
2597 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2598 - // default-bot actions are testable from the admin panel
2599 - if ($bot_id === 'testing') {
2600 - $bot_id = 'default';
2601 - }
2602 -
2603 - // Check if the current bot is in the enabled bots list
2604 - return in_array($bot_id, $enabled_bots);
2605 -}
2606 -
2607 1329 // Helper function to clear PDF and Word document related transients
2608 1330 private function clear_pdf_transients($session_id) {
2609 1331 // PDF transients
2610 1332 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2623,38 +1345,37 @@
2623 1345
2624 1346
2625 1347 //verified good
2626 1348 public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2627 - // Get the user's original instruction/message
2628 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2629 -
2630 - // Set instruction for AI - just pass along what the user wanted to say
2631 - $this->current_action_instruction = $user_instruction;
2632 -
2633 - // Set the transient to track email capture flow
1349 + // Log the message safely
1350 + //error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
1351 +
1352 + // Initiate email capture flow
1353 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
2634 1354 set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
1355 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2635 1356
2636 - // Return false to let the AI generate the response
2637 - return false;
1357 + // FIXED: Return response data instead of sending JSON directly
1358 + // This allows the main chat handler to add testing data before sending
1359 + return [
1360 + 'text' => $response,
1361 + 'html' => '',
1362 + 'session_id' => $session_id
1363 + ];
2638 1364 }
2639 1365
2640 1366 public function mxchat_generate_image($message, $user_id, $session_id) {
2641 1367 //error_log("Starting image generation for message: " . $message);
2642 -
2643 - // Prepare a prompt for OpenAI image generation
1368 +
1369 + // Prepare a prompt for DALL-E
2644 1370 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2645 -
2646 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2647 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2648 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2649 - $image_response = $this->mxchat_generate_custom_image($prompt);
2650 - } else {
2651 - // Use the existing OpenAI API key
2652 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2653 - // Call OpenAI GPT Image to generate an image
2654 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2655 - }
2656 1371
1372 + // Use the existing OpenAI API key
1373 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1374 +
1375 + // Call DALL-E to generate an image
1376 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1377 +
2657 1378 // Check if the response contains an image URL
2658 1379 if (isset($image_response['imageUrl'])) {
2659 1380 $image_url = esc_url_raw($image_response['imageUrl']);
2660 1381
@@ -2697,103 +1418,24 @@
2697 1418 // Return the response directly instead of relying on the property
2698 1419 return $this->fallbackResponse;
2699 1420 }
2700 1421 }
2701 -
2702 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2703 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2704 -
2705 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2706 - if (empty($gemini_api_key)) {
2707 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2708 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2709 - return ['text' => $response_text, 'html' => '', 'images' => []];
2710 - }
2711 -
2712 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2713 -
2714 - if (isset($image_response['imageUrl'])) {
2715 - $image_url = esc_url_raw($image_response['imageUrl']);
2716 -
2717 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2718 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2719 -
2720 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2721 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2722 -
2723 - $this->fallbackResponse = [
2724 - 'text' => $response_text,
2725 - 'html' => $response_html,
2726 - 'images' => [$image_url]
2727 - ];
2728 -
2729 - return $this->fallbackResponse;
2730 - } else {
2731 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2732 -
2733 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2734 -
2735 - $this->fallbackResponse = [
2736 - 'text' => $response_text,
2737 - 'html' => '',
2738 - 'images' => []
2739 - ];
2740 -
2741 - return $this->fallbackResponse;
2742 - }
2743 -}
2744 -
2745 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2746 - $extension = ($mime_type === 'image/jpeg') ? 'jpg' : 'png';
2747 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2748 - $decoded = base64_decode($base64_data);
2749 -
2750 - if ($decoded === false) {
2751 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2752 - }
2753 -
2754 - $upload = wp_upload_bits($filename, null, $decoded);
2755 -
2756 - if (!empty($upload['error'])) {
2757 - return new \WP_Error('upload_failed', $upload['error']);
2758 - }
2759 -
2760 - $attach_id = wp_insert_attachment([
2761 - 'post_mime_type' => $mime_type,
2762 - 'post_title' => $prefix,
2763 - 'post_content' => '',
2764 - 'post_status' => 'inherit',
2765 - ], $upload['file']);
2766 -
2767 - if (is_wp_error($attach_id)) {
2768 - return $attach_id;
2769 - }
2770 -
2771 - require_once ABSPATH . 'wp-admin/includes/image.php';
2772 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2773 - wp_update_attachment_metadata($attach_id, $metadata);
2774 -
2775 - return esc_url_raw(wp_get_attachment_url($attach_id));
2776 -}
2777 -
2778 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1422 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2779 1423 $api_url = 'https://api.openai.com/v1/images/generations';
2780 1424 $body = json_encode([
2781 - 'prompt' => sanitize_text_field($prompt),
2782 - 'n' => 1,
2783 - 'size' => '1024x1024',
2784 - 'quality' => 'medium',
2785 - 'output_format' => 'png',
2786 - 'model' => sanitize_text_field($model),
1425 + 'prompt' => sanitize_text_field($prompt),
1426 + 'n' => 1,
1427 + 'size' => '1024x1024',
1428 + 'model' => sanitize_text_field($model),
2787 1429 ]);
2788 1430
2789 1431 $args = [
2790 - 'body' => $body,
1432 + 'body' => $body,
2791 1433 'headers' => [
2792 - 'Content-Type' => 'application/json',
1434 + 'Content-Type' => 'application/json',
2793 1435 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2794 1436 ],
2795 - 'method' => 'POST',
1437 + 'method' => 'POST',
2796 1438 'timeout' => absint($timeout),
2797 1439 ];
2798 1440
2799 1441 $response = wp_remote_post($api_url, $args);
@@ -2798,114 +1440,23 @@
2798 1440
2799 1441 $response = wp_remote_post($api_url, $args);
2800 1442
2801 1443 if (is_wp_error($response)) {
1444 + //error_log("DALL-E request failed: " . $response->get_error_message());
2802 1445 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2803 1446 }
2804 1447
2805 1448 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2806 1449
2807 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2808 - if ($b64) {
2809 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2810 - if (is_wp_error($saved_url)) {
2811 - return ['error' => $saved_url->get_error_message()];
2812 - }
2813 - return ['imageUrl' => $saved_url];
1450 + if (isset($response_body['data'][0]['url'])) {
1451 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2814 1452 } else {
1453 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2815 1454 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2816 1455 }
2817 1456 }
2818 1457
2819 1458 /**
2820 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2821 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2822 - */
2823 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2824 - $cfg = $this->mxchat_resolve_custom_provider();
2825 - if (empty($cfg['base_url'])) {
2826 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2827 - }
2828 - $url = $cfg['base_url'] . '/images/generations';
2829 - if (!empty($cfg['api_version'])) {
2830 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2831 - }
2832 - $body = wp_json_encode([
2833 - 'prompt' => sanitize_text_field($prompt),
2834 - 'n' => 1,
2835 - 'size' => '1024x1024',
2836 - 'model' => $cfg['model'],
2837 - ]);
2838 - $response = wp_remote_post($url, [
2839 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2840 - 'body' => $body,
2841 - 'method' => 'POST',
2842 - 'timeout' => absint($timeout),
2843 - ]);
2844 - if (is_wp_error($response)) {
2845 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2846 - }
2847 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2848 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2849 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2850 - if ($b64) {
2851 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2852 - if (is_wp_error($saved)) {
2853 - return ['error' => $saved->get_error_message()];
2854 - }
2855 - return ['imageUrl' => $saved];
2856 - }
2857 - $remote_url = $resp['data'][0]['url'] ?? null;
2858 - if ($remote_url) {
2859 - return ['imageUrl' => esc_url_raw($remote_url)];
2860 - }
2861 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2862 - return ['error' => esc_html($err_msg)];
2863 -}
2864 -
2865 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2866 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2867 -
2868 - $body = json_encode([
2869 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2870 - 'parameters' => [
2871 - 'sampleCount' => 1,
2872 - 'aspectRatio' => '1:1',
2873 - ],
2874 - ]);
2875 -
2876 - $args = [
2877 - 'body' => $body,
2878 - 'headers' => [
2879 - 'Content-Type' => 'application/json',
2880 - 'x-goog-api-key' => sanitize_text_field($api_key),
2881 - ],
2882 - 'method' => 'POST',
2883 - 'timeout' => absint($timeout),
2884 - ];
2885 -
2886 - $response = wp_remote_post($api_url, $args);
2887 -
2888 - if (is_wp_error($response)) {
2889 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2890 - }
2891 -
2892 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2893 -
2894 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2895 - if ($b64) {
2896 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2897 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2898 - if (is_wp_error($saved_url)) {
2899 - return ['error' => $saved_url->get_error_message()];
2900 - }
2901 - return ['imageUrl' => $saved_url];
2902 - } else {
2903 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2904 - }
2905 -}
2906 -
2907 -/**
2908 1459 * Handle web search requests.
2909 1460 *
2910 1461 * Sends the refined search query to the Brave Search API and uses the
2911 1462 * results to generate a conversational response with the AI model.
@@ -2953,10 +1504,10 @@
2953 1504 $transient_key = 'mxchat_search_' . md5($refined_search_query);
2954 1505 $results = get_transient($transient_key);
2955 1506
2956 1507 if (false === $results) {
2957 - // SECURITY FIX: Changed to wp_safe_remote_get
2958 - $response = wp_safe_remote_get(
1508 + // Fetch new results from the Brave Search API
1509 + $response = wp_remote_get(
2959 1510 $api_url,
2960 1511 array(
2961 1512 'headers' => array(
2962 1513 'Accept' => 'application/json',
@@ -3095,10 +1646,9 @@
3095 1646 ],
3096 1647 'timeout' => 10,
3097 1648 ];
3098 1649
3099 - // SECURITY FIX: Changed to wp_safe_remote_get
3100 - $response = wp_safe_remote_get($api_url, $args);
1650 + $response = wp_remote_get($api_url, $args);
3101 1651
3102 1652 if (is_wp_error($response)) {
3103 1653 return array(
3104 1654 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
@@ -3168,22 +1718,17 @@
3168 1718 * @return string The refined search query
3169 1719 */
3170 1720 public function mxchat_interpret_search_query($user_query) {
3171 1721 $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');
3172 -
1722 +
3173 1723 // Get options and determine the selected model
3174 1724 $options = $this->options ?? get_option('mxchat_options');
3175 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
3176 -
3177 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3178 - if ($selected_model === 'custom-provider') {
3179 - return $this->interpret_query_with_custom($user_query, $system_prompt);
3180 - }
3181 -
1725 + $selected_model = isset($options['model']) ? $options['model'] : 'gpt-4o';
1726 +
3182 1727 // Extract model prefix to determine the provider
3183 1728 $model_parts = explode('-', $selected_model);
3184 1729 $provider = strtolower($model_parts[0]);
3185 -
1730 +
3186 1731 // Determine which API key to use based on the provider
3187 1732 switch ($provider) {
3188 1733 case 'gemini':
3189 1734 $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
@@ -3224,60 +1769,11 @@
3224 1769 }
3225 1770 }
3226 1771
3227 1772 /**
3228 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3229 - * Uses the same base URL + auth scheme as the chat dispatcher.
3230 - */
3231 -private function interpret_query_with_custom($user_query, $system_prompt) {
3232 - $cfg = $this->mxchat_resolve_custom_provider();
3233 - if (empty($cfg['base_url'])) {
3234 - return sanitize_text_field($user_query);
3235 - }
3236 - $args = [
3237 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3238 - 'body' => wp_json_encode([
3239 - 'model' => $cfg['model'],
3240 - 'messages' => [
3241 - ['role' => 'system', 'content' => $system_prompt],
3242 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3243 - ],
3244 - 'temperature' => 0.2,
3245 - 'max_tokens' => 20,
3246 - ]),
3247 - 'method' => 'POST',
3248 - 'timeout' => 15,
3249 - ];
3250 - $response = wp_remote_post($cfg['chat_url'], $args);
3251 - if (is_wp_error($response)) {
3252 - return sanitize_text_field($user_query);
3253 - }
3254 - $body = json_decode(wp_remote_retrieve_body($response), true);
3255 - return isset($body['choices'][0]['message']['content'])
3256 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3257 - : sanitize_text_field($user_query);
3258 -}
3259 -
3260 -/**
3261 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3262 - * into the assoc-array form wp_remote_post expects.
3263 - */
3264 -private function mxchat_custom_provider_assoc_headers($cfg) {
3265 - $headers = ['Content-Type' => 'application/json'];
3266 - if (!empty($cfg['api_key'])) {
3267 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3268 - $headers['api-key'] = $cfg['api_key'];
3269 - } else {
3270 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3271 - }
3272 - }
3273 - return $headers;
3274 -}
3275 -
3276 -/**
3277 1773 * Interpret query using OpenAI models
3278 1774 */
3279 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
1775 +private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-4o') {
3280 1776 $url = 'https://api.openai.com/v1/chat/completions';
3281 1777 $args = [
3282 1778 'headers' => [
3283 1779 'Authorization' => 'Bearer ' . $api_key,
@@ -3307,36 +1803,13 @@
3307 1803 : sanitize_text_field($user_query);
3308 1804 }
3309 1805
3310 1806 /**
3311 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3312 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3313 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3314 - * stripping. We never send a `thinking` param either, which is required for
3315 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3316 - */
3317 -private function mxchat_claude_omits_temperature($model) {
3318 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3319 - return in_array($model, $no_temp, true);
3320 -}
3321 -
3322 -/**
3323 1807 * Interpret query using Claude models
3324 1808 */
3325 1809 private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3326 1810 $url = 'https://api.anthropic.com/v1/messages';
3327 -
3328 - $payload = [
3329 - 'model' => $model,
3330 - 'system' => $system_prompt,
3331 - 'messages' => [
3332 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3333 - ],
3334 - 'max_tokens' => 20,
3335 - 'temperature' => 0.2,
3336 - ];
3337 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3338 -
1811 +
3339 1812 $args = [
3340 1813 'headers' => [
3341 1814 'Content-Type' => 'application/json',
3342 1815 'x-api-key' => $api_key,
@@ -3341,9 +1814,17 @@
3341 1814 'Content-Type' => 'application/json',
3342 1815 'x-api-key' => $api_key,
3343 1816 'anthropic-version' => '2023-06-01',
3344 1817 ],
3345 - 'body' => wp_json_encode($payload),
1818 + 'body' => wp_json_encode([
1819 + 'model' => $model,
1820 + 'system' => $system_prompt,
1821 + 'messages' => [
1822 + ['role' => 'user', 'content' => sanitize_text_field($user_query)]
1823 + ],
1824 + 'max_tokens' => 20,
1825 + 'temperature' => 0.2,
1826 + ]),
3346 1827 'method' => 'POST',
3347 1828 'timeout' => 15,
3348 1829 ];
3349 1830
@@ -3352,16 +1833,12 @@
3352 1833 return sanitize_text_field($user_query);
3353 1834 }
3354 1835
3355 1836 $body = json_decode(wp_remote_retrieve_body($response), true);
3356 - // claude-fable-5 prepends a thinking block to content — take the first
3357 - // TEXT block, not content[0].
3358 - foreach ((array) ($body['content'] ?? array()) as $block) {
3359 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3360 - return sanitize_text_field(trim($block['text']));
3361 - }
1837 + if (!empty($body['content'][0]['text'])) {
1838 + return sanitize_text_field(trim($body['content'][0]['text']));
3362 1839 }
3363 -
1840 +
3364 1841 return sanitize_text_field($user_query);
3365 1842 }
3366 1843
3367 1844 /**
@@ -3367,16 +1844,13 @@
3367 1844 /**
3368 1845 * Interpret query using Gemini models
3369 1846 */
3370 1847 private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3371 - if ($model === 'gemini-3-pro-preview') {
3372 - $model = 'gemini-3.1-pro-preview';
3373 - }
3374 - // Use v1beta for preview models, v1 for stable models
3375 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3376 -
3377 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
1848 + // Strip "gemini-" prefix for the API
1849 + $model_version = str_replace('gemini-', '', $model);
3378 1850
1851 + $url = "https://generativelanguage.googleapis.com/v1/models/$model_version:generateContent?key=" . urlencode($api_key);
1852 +
3379 1853 $args = [
3380 1854 'headers' => [
3381 1855 'Content-Type' => 'application/json',
3382 1856 ],
@@ -3572,55 +2046,55 @@
3572 2046 }
3573 2047
3574 2048
3575 2049 /**
3576 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
2050 + * Enhanced fetch_and_split_pdf_pages with detailed debugging
3577 2051 */
3578 2052 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3579 2053 // CLEAR DEBUG LOGGING
3580 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3581 - //error_log("PDF Source: " . $pdf_source);
3582 - //error_log("Max Pages: " . $max_pages);
3583 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
2054 + error_log("=== MXCHAT PDF PROCESSING START ===");
2055 + error_log("PDF Source: " . $pdf_source);
2056 + error_log("Max Pages: " . $max_pages);
2057 + error_log("Session ID: " . ($this->session_id ?? 'not set'));
3584 2058
3585 2059 // Check if Advanced Claude Toolbar is available and enabled
3586 2060 $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3587 2061 $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3588 2062
3589 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3590 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
2063 + error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
2064 + error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3591 2065
3592 2066 if ($claude_available && $claude_enabled) {
3593 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
2067 + error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3594 2068
3595 2069 // Attempt Claude processing first
3596 2070 $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3597 2071
3598 2072 if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3599 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3600 - //error_log("Claude returned " . count($claude_result) . " processed pages");
2073 + error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
2074 + error_log("Claude returned " . count($claude_result) . " processed pages");
3601 2075
3602 2076 // Log first page details for verification
3603 2077 if (isset($claude_result[0])) {
3604 2078 $first_page = $claude_result[0];
3605 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3606 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3607 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
2079 + error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
2080 + error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
2081 + error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3608 2082 }
3609 2083
3610 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
2084 + error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3611 2085 return $claude_result;
3612 2086 } else {
3613 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3614 - //error_log("Claude result type: " . gettype($claude_result));
2087 + error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
2088 + error_log("Claude result type: " . gettype($claude_result));
3615 2089 if (is_array($claude_result)) {
3616 - //error_log("Claude result count: " . count($claude_result));
2090 + error_log("Claude result count: " . count($claude_result));
3617 2091 }
3618 2092 }
3619 2093 }
3620 2094
3621 2095 // Fallback to basic processing
3622 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
2096 + error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3623 2097
3624 2098 $upload_dir = wp_upload_dir();
3625 2099 $temp_file = null;
3626 2100
@@ -3628,20 +2102,11 @@
3628 2102 // Your existing basic processing code here...
3629 2103 // (I'll include the key parts with debug logging)
3630 2104
3631 2105 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3632 - //error_log("Downloading PDF from URL...");
3633 -
3634 - // SECURITY FIX: Validate URL before processing
3635 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3636 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3637 - return false;
3638 - }
3639 -
2106 + error_log("Downloading PDF from URL...");
3640 2107 $temp_file = wp_tempnam($pdf_source);
3641 -
3642 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3643 - $response = wp_safe_remote_get($pdf_source, [
2108 + $response = wp_remote_get($pdf_source, [
3644 2109 'timeout' => 60,
3645 2110 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3646 2111 ]);
3647 2112
@@ -3646,35 +2111,29 @@
3646 2111 ]);
3647 2112
3648 2113 if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3649 2114 $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3650 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
2115 + error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3651 2116 return false;
3652 2117 }
3653 2118
3654 - global $wp_filesystem;
3655 - if (empty($wp_filesystem)) {
3656 - require_once ABSPATH . 'wp-admin/includes/file.php';
3657 - WP_Filesystem();
3658 - }
3659 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3660 - //error_log("✅ PDF downloaded successfully");
2119 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
2120 + error_log("✅ PDF downloaded successfully");
3661 2121 } else {
3662 2122 $temp_file = $pdf_source;
3663 - //error_log("Using local PDF file: " . $temp_file);
2123 + error_log("Using local PDF file: " . $temp_file);
3664 2124 }
3665 2125
3666 2126 // Parse PDF
3667 - //error_log("Parsing PDF with basic parser...");
3668 - mxchat_load_pdf_parser();
2127 + error_log("Parsing PDF with basic parser...");
3669 2128 $parser = new \Smalot\PdfParser\Parser();
3670 2129 $pdf = $parser->parseFile($temp_file);
3671 2130 $pages = $pdf->getPages();
3672 2131
3673 - //error_log("PDF contains " . count($pages) . " pages");
2132 + error_log("PDF contains " . count($pages) . " pages");
3674 2133
3675 2134 if (count($pages) > $max_pages) {
3676 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
2135 + error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3677 2136 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3678 2137 unlink($temp_file);
3679 2138 }
3680 2139 return 'too_many_pages';
@@ -3686,9 +2145,9 @@
3686 2145 foreach ($pages as $page_number => $page) {
3687 2146 $text = $page->getText();
3688 2147
3689 2148 if (empty(trim($text))) {
3690 - //error_log("Skipping empty page: " . ($page_number + 1));
2149 + error_log("Skipping empty page: " . ($page_number + 1));
3691 2150 continue;
3692 2151 }
3693 2152
3694 2153 $text = $this->mxchat_clean_text($text);
@@ -3709,9 +2168,9 @@
3709 2168 $processed_pages++;
3710 2169 }
3711 2170 }
3712 2171
3713 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
2172 + error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3714 2173
3715 2174 // Cleanup
3716 2175 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3717 2176 unlink($temp_file);
@@ -3716,46 +2175,21 @@
3716 2175 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3717 2176 unlink($temp_file);
3718 2177 }
3719 2178
3720 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
2179 + error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
3721 2180 return $embeddings;
3722 2181
3723 2182 } catch (\Exception $e) {
3724 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
2183 + error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
3725 2184 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3726 2185 unlink($temp_file);
3727 2186 }
3728 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
2187 + error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3729 2188 return false;
3730 2189 }
3731 2190 }
3732 2191
3733 -
3734 -/**
3735 - * Validate PDF URL for security
3736 - * Prevents SSRF attacks by blocking dangerous URLs
3737 - */
3738 -
3739 -private function mxchat_is_safe_pdf_url($url) {
3740 - // Use WordPress core function for comprehensive validation
3741 - // This blocks localhost, private IPs, and reserved IP ranges
3742 - $validated_url = wp_http_validate_url($url);
3743 -
3744 - if ($validated_url === false) {
3745 - return false;
3746 - }
3747 -
3748 - // Additional check: only allow HTTP/HTTPS schemes
3749 - $parsed = parse_url($url);
3750 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3751 - return false;
3752 - }
3753 -
3754 - return true;
3755 -}
3756 -
3757 -
3758 2192 private function mxchat_clean_text($text) {
3759 2193 // Remove excessive whitespace
3760 2194 $text = preg_replace('/\s+/', ' ', $text);
3761 2195
@@ -3794,14 +2228,11 @@
3794 2228 }
3795 2229
3796 2230 return [];
3797 2231 }
3798 -
3799 -
2232 +// Add this to your class
3800 2233 public function handle_pdf_upload() {
3801 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3802 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3803 - }
2234 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3804 2235
3805 2236 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3806 2237 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 2238 return;
@@ -3806,29 +2237,12 @@
3806 2237 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 2238 return;
3808 2239 }
3809 2240
3810 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3811 - $options = get_option('mxchat_options', array());
3812 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3813 -
3814 - if ($show_pdf_button !== 'on') {
3815 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3816 - return;
3817 - }
3818 -
3819 2241 $file = $_FILES['pdf_file'];
3820 2242 $session_id = sanitize_text_field($_POST['session_id']);
3821 2243 $original_filename = sanitize_text_field($file['name']);
3822 2244
3823 - // Update session owner if it changed (e.g. IP changed due to network switch)
3824 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3825 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3826 -
3827 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3828 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3829 - }
3830 -
3831 2245 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3832 2246 if ($file_type['type'] !== 'application/pdf') {
3833 2247 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3834 2248 return;
@@ -3834,12 +2248,9 @@
3834 2248 return;
3835 2249 }
3836 2250
3837 2251 $upload_dir = wp_upload_dir();
3838 -
3839 - // SECURITY FIX: Generate random filename without exposing session_id
3840 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3841 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2252 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3842 2253 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3843 2254
3844 2255 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3845 2256 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3870,9 +2281,8 @@
3870 2281 return;
3871 2282 }
3872 2283
3873 2284 if (!empty($embeddings)) {
3874 - // Store the mapping between session and the random filename
3875 2285 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3876 2286 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3877 2287 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3878 2288 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3893,11 +2303,9 @@
3893 2303 wp_send_json_error($error_message);
3894 2304 return;
3895 2305 }
3896 2306 public function handle_pdf_remove() {
3897 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3898 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3899 - }
2307 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3900 2308
3901 2309 if (empty($_POST['session_id'])) {
3902 2310 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
3903 2311 wp_die();
@@ -3918,8 +2326,10 @@
3918 2326 wp_die();
3919 2327 }
3920 2328
3921 2329
2330 +
2331 +
3922 2332 function mxchat_fetch_new_messages() {
3923 2333 $session_id = sanitize_text_field($_POST['session_id']);
3924 2334 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3925 2335 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -3932,31 +2342,14 @@
3932 2342 }
3933 2343
3934 2344 $history = get_option("mxchat_history_{$session_id}", []);
3935 2345
3936 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
3937 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
3938 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
3939 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
3940 -
3941 2346 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
3942 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
3943 -
3944 2347 // If persistence is enabled, show all new messages
3945 2348 if ($persistence_enabled) {
3946 - $has_id = !empty($message['id']);
3947 - $is_agent = $message['role'] === 'agent';
3948 -
3949 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
3950 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
3951 - $is_newer = true;
3952 - } else {
3953 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
3954 - }
3955 -
3956 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
3957 -
3958 - return $has_id && $is_newer && $is_agent;
2349 + return !empty($message['id']) &&
2350 + strcmp($message['id'], $last_seen_id) > 0 &&
2351 + $message['role'] === 'agent';
3959 2352 }
3960 2353
3961 2354 // If persistence is disabled, only show messages after initial timestamp
3962 2355 return !empty($message['id']) &&
@@ -3963,16 +2356,12 @@
3963 2356 $message['role'] === 'agent' &&
3964 2357 $message['timestamp'] > $initial_timestamp;
3965 2358 });
3966 2359
3967 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2360 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
3968 2361
3969 - // Include current chat mode so frontend can detect agent→AI transitions
3970 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3971 -
3972 2362 wp_send_json_success([
3973 - 'new_messages' => array_values($new_messages),
3974 - 'chat_mode' => $chat_mode
2363 + 'new_messages' => array_values($new_messages)
3975 2364 ]);
3976 2365 wp_die();
3977 2366 }
3978 2367 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
@@ -4005,9 +2394,9 @@
4005 2394 $channel_id = get_option("mxchat_channel_{$session_id}", '');
4006 2395
4007 2396 if (empty($channel_id)) {
4008 2397 // Create new channel with session ID as name
4009 - $channel_name = $this->generate_channel_name($session_id);
2398 + $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4010 2399
4011 2400 //error_log("Attempting to create channel: $channel_name");
4012 2401
4013 2402 $response = wp_remote_post('https://slack.com/api/conversations.create', [
@@ -4143,507 +2532,9 @@
4143 2532 'fallbackResponse' => $this->fallbackResponse
4144 2533 ]);
4145 2534 wp_die();
4146 2535 }
4147 -
4148 -private function generate_channel_name($session_id) {
4149 - $email = null;
4150 - $name = null;
4151 -
4152 - // 1. First priority: Check if user is logged in and get their info
4153 - if (is_user_logged_in()) {
4154 - $current_user = wp_get_current_user();
4155 - if (!empty($current_user->user_email)) {
4156 - $email = $current_user->user_email;
4157 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4158 - }
4159 - if (!empty($current_user->display_name)) {
4160 - $name = $current_user->display_name;
4161 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4162 - }
4163 - }
4164 -
4165 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4166 - if (empty($email)) {
4167 - $email_option_key = "mxchat_email_{$session_id}";
4168 - $saved_email = get_option($email_option_key);
4169 - if (!empty($saved_email)) {
4170 - $email = $saved_email;
4171 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4172 - }
4173 - }
4174 -
4175 - if (empty($name)) {
4176 - $name_option_key = "mxchat_name_{$session_id}";
4177 - $saved_name = get_option($name_option_key);
4178 - if (!empty($saved_name)) {
4179 - $name = $saved_name;
4180 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4181 - }
4182 - }
4183 -
4184 - // 3. Third priority: Check existing chat transcript for email/name
4185 - if (empty($email) || empty($name)) {
4186 - global $wpdb;
4187 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4188 - $existing_data = $wpdb->get_row($wpdb->prepare(
4189 - "SELECT user_email, user_name FROM $table_name WHERE session_id = %s AND (user_email IS NOT NULL OR user_name IS NOT NULL) LIMIT 1",
4190 - $session_id
4191 - ));
4192 -
4193 - if ($existing_data) {
4194 - if (empty($email) && !empty($existing_data->user_email)) {
4195 - $email = $existing_data->user_email;
4196 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4197 - }
4198 - if (empty($name) && !empty($existing_data->user_name)) {
4199 - $name = $existing_data->user_name;
4200 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4201 - }
4202 - }
4203 - }
4204 -
4205 - // 4. Generate channel name based on priority: Name > Email > Session ID
4206 - $channel_name = '';
4207 -
4208 - if (!empty($name)) {
4209 - // Convert name to valid Slack channel name
4210 - $base_name = strtolower(trim($name));
4211 - // Replace spaces and invalid characters
4212 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4213 - $base_name = preg_replace('/\s+/', '-', $base_name);
4214 - $base_name = trim($base_name, '-');
4215 -
4216 - // Get last 4 characters of session ID for uniqueness
4217 - $session_suffix = substr($session_id, -4);
4218 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4219 -
4220 - // Slack channel names have a 21 character limit
4221 - if (strlen($channel_name) > 21) {
4222 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4223 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4224 - $truncated_name = substr($base_name, 0, $available_space);
4225 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4226 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4227 - }
4228 -
4229 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4230 -
4231 - } elseif (!empty($email)) {
4232 - // Convert email to valid Slack channel name (your existing logic)
4233 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4234 - // Remove any remaining invalid characters
4235 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4236 - // Ensure it doesn't end with a hyphen
4237 - $channel_name = rtrim($channel_name, '-');
4238 - // Slack channel names have a 21 character limit, so truncate if needed
4239 - if (strlen($channel_name) > 21) {
4240 - $channel_name = substr($channel_name, 0, 21);
4241 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4242 - }
4243 -
4244 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4245 -
4246 - } else {
4247 - // Fallback to session ID if no name or email found
4248 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4249 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4250 - }
4251 -
4252 - // Final validation - ensure channel name meets Slack requirements
4253 - if (strlen($channel_name) > 21) {
4254 - $channel_name = substr($channel_name, 0, 21);
4255 - $channel_name = rtrim($channel_name, '-');
4256 - }
4257 -
4258 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4259 - return $channel_name;
4260 -}
4261 -
4262 -/**
4263 - * Telegram Live Agent Handover
4264 - * Creates a forum topic in the Telegram group and notifies agents
4265 - */
4266 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4267 - // Check if Telegram agents are available
4268 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4269 - if ($telegram_available !== 'on') {
4270 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4271 - $this->fallbackResponse = [
4272 - 'text' => $away_message,
4273 - 'html' => '',
4274 - 'images' => [],
4275 - 'chat_mode' => 'ai'
4276 - ];
4277 - wp_send_json([
4278 - 'text' => $away_message,
4279 - 'html' => '',
4280 - 'chat_mode' => 'ai',
4281 - 'session_id' => $session_id
4282 - ]);
4283 - wp_die();
4284 - }
4285 -
4286 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4287 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4288 -
4289 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
4290 - return false;
4291 - }
4292 -
4293 - // Check if topic already exists for this session
4294 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4295 -
4296 - if (empty($topic_id)) {
4297 - // Generate topic name
4298 - $topic_name = $this->generate_telegram_topic_name($session_id);
4299 -
4300 - // Random icon color (Telegram forum topic colors)
4301 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4302 - $icon_color = $icon_colors[array_rand($icon_colors)];
4303 -
4304 - // Create forum topic
4305 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4306 - 'headers' => ['Content-Type' => 'application/json'],
4307 - 'body' => json_encode([
4308 - 'chat_id' => $telegram_group_id,
4309 - 'name' => $topic_name,
4310 - 'icon_color' => $icon_color
4311 - ])
4312 - ]);
4313 -
4314 - if (!is_wp_error($response)) {
4315 - $response_body = wp_remote_retrieve_body($response);
4316 - $response_data = json_decode($response_body, true);
4317 -
4318 - if (isset($response_data['ok']) && $response_data['ok']) {
4319 - $topic_id = $response_data['result']['message_thread_id'];
4320 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4321 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4322 - }
4323 - }
4324 -
4325 - if (empty($topic_id)) {
4326 - return false; // Failed to create topic
4327 - }
4328 - }
4329 -
4330 - // Get recent chat history
4331 - $history = get_option("mxchat_history_{$session_id}", []);
4332 - $recent_history = array_slice($history, -5);
4333 -
4334 - // Format conversation context for Telegram (HTML format)
4335 - $conversation_context = "";
4336 - if (!empty($recent_history)) {
4337 - $conversation_context = "<b>Recent Conversation:</b>\n";
4338 - foreach ($recent_history as $hist_message) {
4339 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4340 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4341 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4342 - }
4343 - $conversation_context .= "\n";
4344 - }
4345 -
4346 - // Get user info
4347 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4348 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4349 -
4350 - // Update session mode
4351 - update_option("mxchat_mode_{$session_id}", 'agent');
4352 -
4353 - // Send initial message to topic
4354 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4355 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4356 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4357 - $topic_message .= "<b>User:</b> {$user_name}\n";
4358 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4359 -
4360 - if (!empty($conversation_context)) {
4361 - $topic_message .= $conversation_context;
4362 - }
4363 -
4364 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4365 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4366 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4367 -
4368 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4369 - 'headers' => ['Content-Type' => 'application/json'],
4370 - 'body' => json_encode([
4371 - 'chat_id' => $telegram_group_id,
4372 - 'message_thread_id' => $topic_id,
4373 - 'text' => $topic_message,
4374 - 'parse_mode' => 'HTML'
4375 - ])
4376 - ]);
4377 -
4378 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
4379 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4380 -
4381 - $this->fallbackResponse = [
4382 - 'text' => $success_message,
4383 - 'html' => '',
4384 - 'images' => [],
4385 - 'chat_mode' => 'agent'
4386 - ];
4387 -
4388 - wp_send_json([
4389 - 'success' => true,
4390 - 'text' => $success_message,
4391 - 'html' => '',
4392 - 'chat_mode' => 'agent',
4393 - 'session_id' => $session_id,
4394 - 'fallbackResponse' => $this->fallbackResponse
4395 - ]);
4396 - wp_die();
4397 -}
4398 -
4399 -/**
4400 - * Generate topic name for Telegram forum
4401 - */
4402 -private function generate_telegram_topic_name($session_id) {
4403 - $name = null;
4404 - $email = null;
4405 -
4406 - // Check logged in user
4407 - if (is_user_logged_in()) {
4408 - $current_user = wp_get_current_user();
4409 - if (!empty($current_user->display_name)) {
4410 - $name = $current_user->display_name;
4411 - }
4412 - if (!empty($current_user->user_email)) {
4413 - $email = $current_user->user_email;
4414 - }
4415 - }
4416 -
4417 - // Check session data
4418 - if (empty($name)) {
4419 - $name = get_option("mxchat_name_{$session_id}");
4420 - }
4421 - if (empty($email)) {
4422 - $email = get_option("mxchat_email_{$session_id}");
4423 - }
4424 -
4425 - // Generate topic name
4426 - $session_suffix = substr($session_id, -6);
4427 -
4428 - if (!empty($name)) {
4429 - // Clean name for topic (max 128 chars in Telegram)
4430 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4431 - $clean_name = trim($clean_name);
4432 - if (strlen($clean_name) > 50) {
4433 - $clean_name = substr($clean_name, 0, 50);
4434 - }
4435 - return "Chat - {$clean_name} ({$session_suffix})";
4436 - } elseif (!empty($email)) {
4437 - // Use email prefix
4438 - $email_prefix = explode('@', $email)[0];
4439 - if (strlen($email_prefix) > 30) {
4440 - $email_prefix = substr($email_prefix, 0, 30);
4441 - }
4442 - return "Chat - {$email_prefix} ({$session_suffix})";
4443 - }
4444 -
4445 - return "Chat - {$session_suffix}";
4446 -}
4447 -
4448 -/**
4449 - * Send user message to Telegram agent
4450 - */
4451 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4452 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4453 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4454 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4455 -
4456 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4457 - return false;
4458 - }
4459 -
4460 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4461 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4462 -
4463 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4464 - 'headers' => ['Content-Type' => 'application/json'],
4465 - 'body' => json_encode([
4466 - 'chat_id' => $group_id,
4467 - 'message_thread_id' => $topic_id,
4468 - 'text' => $user_message,
4469 - 'parse_mode' => 'HTML'
4470 - ])
4471 - ]);
4472 -
4473 - return !is_wp_error($response);
4474 -}
4475 -
4476 -/**
4477 - * Handle incoming Telegram webhook
4478 - */
4479 -public function handle_telegram_webhook(WP_REST_Request $request) {
4480 - $body = $request->get_body();
4481 - $data = json_decode($body, true);
4482 -
4483 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4484 -
4485 - // Handle message events from forum topics
4486 - if (isset($data['message'])) {
4487 - $message_data = $data['message'];
4488 -
4489 - // Skip if not from a forum topic
4490 - if (!isset($message_data['message_thread_id'])) {
4491 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4492 - return new WP_REST_Response(['ok' => true]);
4493 - }
4494 -
4495 - // Skip bot messages
4496 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4497 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4498 - return new WP_REST_Response(['ok' => true]);
4499 - }
4500 -
4501 - $chat_id = $message_data['chat']['id'] ?? '';
4502 - $topic_id = $message_data['message_thread_id'];
4503 - $message_text = $message_data['text'] ?? '';
4504 - $message_id = $message_data['message_id'] ?? '';
4505 - $from = $message_data['from'] ?? [];
4506 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4507 - if (empty($agent_name)) {
4508 - $agent_name = $from['username'] ?? 'Agent';
4509 - }
4510 -
4511 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4512 -
4513 - // Skip empty messages
4514 - if (empty($message_text)) {
4515 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4516 - return new WP_REST_Response(['ok' => true]);
4517 - }
4518 -
4519 - // Find session ID by topic ID - cast to string for comparison
4520 - global $wpdb;
4521 - $topic_id_str = strval($topic_id);
4522 - $session_option = $wpdb->get_var(
4523 - $wpdb->prepare(
4524 - "SELECT option_name FROM {$wpdb->options}
4525 - WHERE option_name LIKE %s
4526 - AND option_value = %s",
4527 - 'mxchat_telegram_topic_%',
4528 - $topic_id_str
4529 - )
4530 - );
4531 -
4532 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4533 -
4534 - if ($session_option) {
4535 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4536 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4537 -
4538 - // Verify the group ID matches
4539 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4540 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4541 -
4542 - if (strval($stored_group_id) != strval($chat_id)) {
4543 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4544 - return new WP_REST_Response(['ok' => true]);
4545 - }
4546 -
4547 - // Check for closure commands
4548 - $lower_text = strtolower(trim($message_text));
4549 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4550 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4551 - // End the live agent session
4552 - update_option("mxchat_mode_{$session_id}", 'ai');
4553 -
4554 - // Save disconnect message
4555 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4556 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4557 -
4558 - // Notify in Telegram
4559 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4560 - if (!empty($telegram_bot_token)) {
4561 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4562 - 'headers' => ['Content-Type' => 'application/json'],
4563 - 'body' => json_encode([
4564 - 'chat_id' => $chat_id,
4565 - 'message_thread_id' => $topic_id,
4566 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4567 - 'parse_mode' => 'HTML'
4568 - ])
4569 - ]);
4570 -
4571 - // Optionally close the topic
4572 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4573 - 'headers' => ['Content-Type' => 'application/json'],
4574 - 'body' => json_encode([
4575 - 'chat_id' => $chat_id,
4576 - 'message_thread_id' => $topic_id
4577 - ])
4578 - ]);
4579 - }
4580 -
4581 - return new WP_REST_Response(['ok' => true]);
4582 - }
4583 -
4584 - // Deduplicate messages
4585 - $message_key = md5($session_id . $message_id . $message_text);
4586 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4587 -
4588 - if (in_array($message_key, $processed_messages)) {
4589 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4590 - return new WP_REST_Response(['ok' => true]);
4591 - }
4592 -
4593 - $processed_messages[] = $message_key;
4594 - if (count($processed_messages) > 50) {
4595 - $processed_messages = array_slice($processed_messages, -50);
4596 - }
4597 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4598 -
4599 - // Save the agent message - format with agent name prefix for proper parsing
4600 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4601 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4602 -
4603 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4604 -
4605 - // Verify the message was saved to history
4606 - $history = get_option("mxchat_history_{$session_id}", []);
4607 - $last_message = end($history);
4608 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4609 -
4610 - // Send confirmation back to Telegram
4611 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4612 - if (!empty($telegram_bot_token)) {
4613 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4614 - if (!get_transient($confirm_key)) {
4615 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4616 - 'headers' => ['Content-Type' => 'application/json'],
4617 - 'body' => json_encode([
4618 - 'chat_id' => $chat_id,
4619 - 'message_thread_id' => $topic_id,
4620 - 'text' => "✅ <i>Message sent to user</i>",
4621 - 'parse_mode' => 'HTML',
4622 - 'reply_to_message_id' => $message_id
4623 - ])
4624 - ]);
4625 - set_transient($confirm_key, true, 300);
4626 - }
4627 - }
4628 - } else {
4629 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4630 - }
4631 - } else {
4632 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4633 - }
4634 -
4635 - return new WP_REST_Response(['ok' => true]);
4636 -}
4637 -
4638 2536 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4639 - // Check if this is a Telegram agent session
4640 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4641 - if (!empty($telegram_topic_id)) {
4642 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4643 - }
4644 -
4645 - // Otherwise, try Slack
4646 2537 $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4647 2538 $channel_id = get_option("mxchat_channel_{$session_id}", '');
4648 2539
4649 2540 if (empty($slack_bot_token) || empty($channel_id)) {
@@ -4800,26 +2691,22 @@
4800 2691 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4801 2692 ], 200);
4802 2693 }
4803 2694 public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4804 - // Update mode to AI
2695 + //error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
2696 +
2697 + // Just update mode to AI
4805 2698 update_option("mxchat_mode_{$session_id}", 'ai');
4806 -
4807 - // Clear any existing PDF context to start fresh
4808 - $this->clear_pdf_transients($session_id);
4809 -
4810 - // Set the response with explicit chat_mode
4811 - $this->fallbackResponse = [
4812 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4813 - 'html' => '',
4814 - 'images' => [],
4815 - 'chat_mode' => 'ai' // Ensure this is set
4816 - ];
4817 -
4818 - // Return the complete response array instead of just true
4819 - return $this->fallbackResponse;
2699 +
2700 + // Initialize states
2701 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2702 + $this->productCardHtml = '';
2703 +
2704 + // Set the response message
2705 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
2706 +
2707 + return true; // Intent was handled
4820 2708 }
4821 -
4822 2709 public function handle_slack_messages(WP_REST_Request $request) {
4823 2710 // Log the incoming request for debugging
4824 2711 //error_log('Slack events request received: ' . $request->get_body());
4825 2712
@@ -4869,33 +2756,33 @@
4869 2756
4870 2757 $channel_id = $event['channel'];
4871 2758 $message_text = $event['text'] ?? '';
4872 2759 $message_ts = $event['ts'] ?? '';
4873 -
2760 +
4874 2761 // Find session ID by looking for matching channel
4875 2762 global $wpdb;
4876 2763 $session_option = $wpdb->get_var(
4877 2764 $wpdb->prepare(
4878 - "SELECT option_name FROM {$wpdb->options}
4879 - WHERE option_name LIKE 'mxchat_channel_%'
2765 + "SELECT option_name FROM {$wpdb->options}
2766 + WHERE option_name LIKE 'mxchat_channel_%'
4880 2767 AND option_value = %s",
4881 2768 $channel_id
4882 2769 )
4883 2770 );
4884 -
2771 +
4885 2772 if ($session_option) {
4886 2773 $session_id = str_replace('mxchat_channel_', '', $session_option);
4887 -
2774 +
4888 2775 // Create a unique key for this specific message
4889 2776 $message_key = md5($session_id . $message_ts . $message_text);
4890 2777 $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4891 -
2778 +
4892 2779 // Check if we've already processed this exact message
4893 2780 if (in_array($message_key, $processed_messages)) {
4894 2781 //error_log("Duplicate message detected for session $session_id");
4895 2782 return new WP_REST_Response(['ok' => true]);
4896 2783 }
4897 -
2784 +
4898 2785 // Add to processed messages
4899 2786 $processed_messages[] = $message_key;
4900 2787 // Keep only last 50 messages per session
4901 2788 if (count($processed_messages) > 50) {
@@ -4901,46 +2788,14 @@
4901 2788 if (count($processed_messages) > 50) {
4902 2789 $processed_messages = array_slice($processed_messages, -50);
4903 2790 }
4904 2791 set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4905 -
4906 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4907 -
4908 - // Handle agent ending the chat — transfer back to AI
4909 - // Format: "!endchat" or "!endchat <custom message to user>"
4910 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
4911 - update_option("mxchat_mode_{$session_id}", 'ai');
4912 -
4913 - // Extract custom message after !endchat, or use empty string
4914 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
4915 -
4916 - // Send the agent's custom farewell message if provided
4917 - if (!empty($custom_message)) {
4918 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
4919 - }
4920 -
4921 - // Confirm in Slack channel
4922 - if (!empty($slack_bot_token)) {
4923 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4924 - 'headers' => [
4925 - 'Content-Type' => 'application/json',
4926 - 'Authorization' => 'Bearer ' . $slack_bot_token
4927 - ],
4928 - 'body' => json_encode([
4929 - 'channel' => $channel_id,
4930 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
4931 - 'mrkdwn' => true
4932 - ])
4933 - ]);
4934 - }
4935 -
4936 - return new WP_REST_Response(['ok' => true]);
4937 - }
4938 -
2792 +
4939 2793 // Save the agent message
4940 2794 $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4941 -
2795 +
4942 2796 // Send confirmation back to Slack (only once)
2797 + $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4943 2798 if (!empty($slack_bot_token)) {
4944 2799 // Use a transient to prevent duplicate confirmations
4945 2800 $confirm_key = 'mxchat_confirm_' . $message_key;
4946 2801 if (!get_transient($confirm_key)) {
@@ -4950,9 +2805,9 @@
4950 2805 'Authorization' => 'Bearer ' . $slack_bot_token
4951 2806 ],
4952 2807 'body' => json_encode([
4953 2808 'channel' => $channel_id,
4954 - 'text' => "✅ _Message sent to user_",
2809 + 'text' => "✅ _Message sent to user_",
4955 2810 'thread_ts' => $event['ts'] // Reply in thread
4956 2811 ])
4957 2812 ]);
4958 2813 // Set transient to prevent duplicate confirmations
@@ -4992,15 +2847,9 @@
4992 2847 try {
4993 2848 // Get options and selected model
4994 2849 $options = get_option('mxchat_options');
4995 2850 $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4996 -
4997 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
4998 - // Off by default so existing sites see byte-identical behavior.
4999 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5000 - return $this->mxchat_generate_embedding_custom($text);
5001 - }
5002 -
2851 +
5003 2852 // Determine endpoint and API key based on model
5004 2853 if (strpos($selected_model, 'voyage') === 0) {
5005 2854 $endpoint = 'https://api.voyageai.com/v1/embeddings';
5006 2855 $api_key = $options['voyage_api_key'] ?? '';
@@ -5195,653 +3044,254 @@
5195 3044 'error_code' => 'embedding_exception'
5196 3045 ];
5197 3046 }
5198 3047 }
3048 +private function mxchat_find_relevant_content($user_embedding) {
3049 + //error_log('MXChat Vector Search: Starting content search...');
5199 3050
3051 + // Retrieve the add-on settings from the database.
3052 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5200 3053
5201 -/**
5202 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5203 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5204 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5205 - */
5206 -private function mxchat_generate_embedding_custom($text) {
5207 - if (empty($text)) {
5208 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5209 - }
5210 - $cfg = $this->mxchat_resolve_custom_provider();
5211 - if (empty($cfg['base_url'])) {
5212 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5213 - }
3054 + // Determine whether Pinecone is enabled.
3055 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5214 3056
5215 - $options = get_option('mxchat_options');
5216 - $embed_url = $cfg['base_url'] . '/embeddings';
5217 - if (!empty($cfg['api_version'])) {
5218 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5219 - }
5220 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5221 - ? trim((string) $options['custom_provider_embedding_model'])
5222 - : $cfg['model'];
3057 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
5223 3058
5224 - $response = wp_remote_post($embed_url, [
5225 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5226 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5227 - 'timeout' => 60,
5228 - ]);
5229 - if (is_wp_error($response)) {
5230 - return [
5231 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5232 - 'error_code' => 'embedding_custom_connection_error',
5233 - ];
5234 - }
5235 - $status = wp_remote_retrieve_response_code($response);
5236 - $body = json_decode(wp_remote_retrieve_body($response), true);
5237 - if ($status !== 200) {
5238 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5239 - return [
5240 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5241 - 'error_code' => 'embedding_custom_api_error',
5242 - 'status_code' => $status,
5243 - ];
5244 - }
5245 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5246 - return $body['data'][0]['embedding'];
5247 - }
5248 - return [
5249 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5250 - 'error_code' => 'embedding_custom_invalid_response',
5251 - ];
5252 -}
5253 -
5254 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5255 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5256 -
5257 - // Check for OpenAI Vector Store first (takes priority when enabled)
5258 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5259 -
5260 - if ($bot_vectorstore_config['use_vectorstore']) {
5261 - // Get current model to verify it's an OpenAI model
5262 - $bot_options = $this->get_bot_options($bot_id);
5263 - $mxchat_options = get_option('mxchat_options', array());
5264 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5265 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5266 -
5267 - if ($this->is_openai_chat_model($selected_model)) {
5268 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5269 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5270 - } else {
5271 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5272 - }
5273 - }
5274 -
5275 - // Get bot-specific Pinecone configuration
5276 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5277 -
5278 - // Debug: Log the Pinecone configuration
5279 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5280 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5281 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5282 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5283 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5284 -
5285 - // Determine whether to use Pinecone based on bot configuration
5286 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5287 -
5288 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5289 -
5290 - if ($use_pinecone) {
5291 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
3059 + if ($use_pinecone === 1) {
3060 + //error_log('MXChat Vector Search: Using Pinecone database');
3061 + return $this->find_relevant_content_pinecone($user_embedding);
5292 3062 } else {
5293 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
3063 + //error_log('MXChat Vector Search: Using WordPress database');
3064 + return $this->find_relevant_content_wordpress($user_embedding);
5294 3065 }
5295 3066 }
5296 3067
5297 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
3068 +private function find_relevant_content_wordpress($user_embedding) {
5298 3069 global $wpdb;
5299 3070 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3071 + $cache_key = 'mxchat_system_prompt_embeddings';
3072 + $batch_size = 500;
3073 +
5300 3074 // Initialize similarity analysis storage
5301 3075 $this->last_similarity_analysis = [
5302 3076 'knowledge_base_type' => 'WordPress Database',
5303 - 'bot_id' => $bot_id,
5304 3077 'top_matches' => [],
5305 3078 'threshold_used' => 0,
5306 3079 'total_checked' => 0
5307 3080 ];
5308 3081
5309 - // NEW: Initialize valid URLs array
5310 - $valid_urls = [];
3082 + // Retrieve embeddings from cache or database
3083 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3084 + if ($embeddings === false) {
3085 + // Cache miss - load embeddings from database WITH CONTENT for testing
3086 + $embeddings = [];
3087 + $offset = 0;
5311 3088
5312 - // Get bot-specific options for similarity threshold
5313 - $bot_options = $this->get_bot_options($bot_id);
5314 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
3089 + do {
3090 + $query = $wpdb->prepare(
3091 + "SELECT id, embedding_vector, article_content, source_url
3092 + FROM {$system_prompt_table}
3093 + LIMIT %d OFFSET %d",
3094 + $batch_size,
3095 + $offset
3096 + );
5315 3097
5316 - // Get knowledge manager instance for role checking
5317 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3098 + $batch = $wpdb->get_results($query);
3099 + if (empty($batch)) {
3100 + break;
3101 + }
5318 3102
5319 - // Get base similarity threshold from bot options or default options
5320 - $similarity_threshold = isset($current_options['similarity_threshold'])
5321 - ? ((int) $current_options['similarity_threshold']) / 100
5322 - : 0.35;
5323 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3103 + $embeddings = array_merge($embeddings, $batch);
3104 + $offset += $batch_size;
3105 + unset($batch);
3106 + } while (true);
5324 3107
5325 - // Precompute bot_filter once, outside the streaming loop
5326 - $bot_filter = '';
5327 - if ($bot_id !== 'default') {
5328 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5329 - if ($column_exists) {
5330 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
3108 + if (empty($embeddings)) {
3109 + return '';
5331 3110 }
3111 +
3112 + // Cache embeddings for future use (but note: this now includes content)
3113 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
5332 3114 }
5333 3115
5334 - // ===== STREAMING TOP-K PASS =====
5335 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5336 - // - top 10 by raw similarity (for the testing/debug display panel)
5337 - // - candidates above threshold with access (capped) for context assembly
5338 - // This bounds peak memory regardless of knowledge base size and avoids loading
5339 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5340 - $batch_size = 250;
5341 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5342 - $top_display = [];
5343 - $candidates = [];
5344 - $total_checked = 0;
5345 - $offset = 0;
5346 -
5347 - do {
5348 - $batch = $wpdb->get_results($wpdb->prepare(
5349 - "SELECT id, embedding_vector, source_url, role_restriction
5350 - FROM {$system_prompt_table}
5351 - WHERE 1=1 {$bot_filter}
5352 - LIMIT %d OFFSET %d",
5353 - $batch_size,
5354 - $offset
5355 - ));
5356 -
5357 - if (empty($batch)) {
5358 - break;
5359 - }
5360 -
5361 - foreach ($batch as $row) {
5362 - $database_embedding = $row->embedding_vector
5363 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5364 - : null;
5365 -
5366 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5367 - unset($database_embedding);
5368 - continue;
5369 - }
5370 -
3116 + // Get configuration options
3117 + $main_options = get_option('mxchat_options', []);
3118 +
3119 + // Get base similarity threshold (default 75%)
3120 + $similarity_threshold = isset($main_options['similarity_threshold'])
3121 + ? ((int) $main_options['similarity_threshold']) / 100
3122 + : 0.75;
3123 +
3124 + $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3125 +
3126 + // Calculate similarities and build results array
3127 + $all_similarities = [];
3128 + $relevant_results = [];
3129 +
3130 + foreach ($embeddings as $embedding) {
3131 + $database_embedding = $embedding->embedding_vector
3132 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3133 + : null;
3134 +
3135 + if (is_array($database_embedding) && is_array($user_embedding)) {
5371 3136 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5372 - unset($database_embedding);
5373 -
5374 - $role_restriction = $row->role_restriction ?? 'public';
5375 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5376 - $source_url = $row->source_url ?? '';
5377 -
5378 - // Maintain top 10 display buffer (insert-if-beats-worst)
5379 - if (count($top_display) < 10) {
5380 - $top_display[] = [
5381 - 'id' => $row->id,
5382 - 'similarity' => $similarity,
5383 - 'source_url' => $source_url,
5384 - 'role_restriction' => $role_restriction,
5385 - 'has_access' => $has_access,
5386 - ];
5387 - usort($top_display, function ($a, $b) {
5388 - return $b['similarity'] <=> $a['similarity'];
5389 - });
5390 - } elseif ($similarity > $top_display[9]['similarity']) {
5391 - $top_display[9] = [
5392 - 'id' => $row->id,
5393 - 'similarity' => $similarity,
5394 - 'source_url' => $source_url,
5395 - 'role_restriction' => $role_restriction,
5396 - 'has_access' => $has_access,
5397 - ];
5398 - usort($top_display, function ($a, $b) {
5399 - return $b['similarity'] <=> $a['similarity'];
5400 - });
3137 +
3138 + // Store ALL similarities for testing (top 10)
3139 + $source_display = '';
3140 + if (!empty($embedding->source_url) && $embedding->source_url !== '#') {
3141 + $source_display = $embedding->source_url;
3142 + } else {
3143 + $content_preview = strip_tags($embedding->article_content ?? '');
3144 + $content_preview = preg_replace('/\s+/', ' ', $content_preview);
3145 + $source_display = substr(trim($content_preview), 0, 50) . '...';
5401 3146 }
5402 -
5403 - // Track candidates for context assembly (above threshold + has access)
5404 - if ($similarity >= $similarity_threshold && $has_access) {
5405 - $candidates[] = [
5406 - 'id' => $row->id,
5407 - 'similarity' => $similarity,
5408 - 'source_url' => $source_url,
3147 +
3148 + $all_similarities[] = [
3149 + 'document_id' => $embedding->id,
3150 + 'similarity' => $similarity,
3151 + 'similarity_percentage' => round($similarity * 100, 2),
3152 + 'above_threshold' => $similarity >= $similarity_threshold,
3153 + 'source_display' => $source_display,
3154 + 'content_preview' => substr(strip_tags($embedding->article_content ?? ''), 0, 100) . '...',
3155 + 'used_for_context' => false // Initialize as false, we'll update this later
3156 + ];
3157 +
3158 + // Only consider results above threshold for actual content retrieval
3159 + if ($similarity >= $similarity_threshold) {
3160 + $relevant_results[] = [
3161 + 'id' => $embedding->id,
3162 + 'similarity' => $similarity
5409 3163 ];
5410 3164 }
5411 -
5412 - $total_checked++;
5413 3165 }
5414 -
5415 - unset($batch);
5416 -
5417 - // Trim candidates periodically to cap memory during long scans
5418 - if (count($candidates) > $max_candidates) {
5419 - usort($candidates, function ($a, $b) {
5420 - return $b['similarity'] <=> $a['similarity'];
5421 - });
5422 - $candidates = array_slice($candidates, 0, $max_candidates);
5423 - }
5424 -
5425 - $offset += $batch_size;
5426 - } while (true);
5427 -
5428 - if ($total_checked === 0) {
5429 - $this->current_valid_urls = [];
5430 - return '';
3166 +
3167 + unset($database_embedding);
5431 3168 }
5432 3169
5433 - // Final candidates sort (best first)
5434 - if (count($candidates) > 1) {
5435 - usort($candidates, function ($a, $b) {
5436 - return $b['similarity'] <=> $a['similarity'];
5437 - });
5438 - }
5439 -
5440 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5441 - // Gather unique IDs we actually need (top_display + candidates) and pull
5442 - // article_content in bounded IN() batches. This avoids loading content for
5443 - // every row during the similarity scan.
5444 - $needed_ids = [];
5445 - foreach ($top_display as $item) {
5446 - $needed_ids[$item['id']] = true;
5447 - }
5448 - foreach ($candidates as $item) {
5449 - $needed_ids[$item['id']] = true;
5450 - }
5451 - $needed_ids = array_keys($needed_ids);
5452 -
5453 - $content_map = [];
5454 - if (!empty($needed_ids)) {
5455 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5456 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5457 - $rows = $wpdb->get_results($wpdb->prepare(
5458 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5459 - ...$chunk_ids
5460 - ));
5461 - foreach ($rows as $r) {
5462 - $content_map[$r->id] = $r->article_content;
5463 - }
5464 - unset($rows);
5465 - }
5466 - }
5467 -
5468 - // Build the all_similarities display array from the top 10
5469 - $all_similarities = [];
5470 - foreach ($top_display as $item) {
5471 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5472 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5473 - $is_chunk = $parsed_for_display['is_chunked'];
5474 - $chunk_meta = $parsed_for_display['metadata'];
5475 -
5476 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5477 - $source_display = $item['source_url'];
5478 - } else {
5479 - $content_preview = strip_tags($article_content_for_parse);
5480 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5481 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5482 - }
5483 -
5484 - $all_similarities[] = [
5485 - 'document_id' => $item['id'],
5486 - 'similarity' => $item['similarity'],
5487 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5488 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5489 - 'source_display' => $source_display,
5490 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5491 - 'used_for_context' => false,
5492 - 'role_restriction' => $item['role_restriction'],
5493 - 'has_access' => $item['has_access'],
5494 - 'filtered_out' => !$item['has_access'],
5495 - 'is_chunk' => $is_chunk,
5496 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5497 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5498 - ];
5499 - }
5500 -
5501 - // Build url_groups from candidates for chunk reassembly
5502 - $url_groups = array();
5503 - foreach ($candidates as $cand) {
5504 - $article_content = $content_map[$cand['id']] ?? '';
5505 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5506 - $is_chunked = $parsed['is_chunked'];
5507 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5508 - $text_content = $parsed['text'];
5509 -
5510 - $source_url = $cand['source_url'];
5511 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5512 -
5513 - if (!isset($url_groups[$group_key])) {
5514 - $url_groups[$group_key] = array(
5515 - 'source_url' => $source_url,
5516 - 'best_score' => 0,
5517 - 'is_chunked' => $is_chunked,
5518 - 'chunks' => array(),
5519 - 'single_text' => '',
5520 - 'single_id' => null
5521 - );
5522 - }
5523 -
5524 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5525 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5526 - }
5527 -
5528 - if ($is_chunked) {
5529 - $url_groups[$group_key]['is_chunked'] = true;
5530 - $url_groups[$group_key]['chunks'][] = array(
5531 - 'id' => $cand['id'],
5532 - 'score' => $cand['similarity'],
5533 - 'chunk_index' => $chunk_index,
5534 - 'text' => $text_content
5535 - );
5536 - } else {
5537 - $url_groups[$group_key]['single_text'] = $text_content;
5538 - $url_groups[$group_key]['single_id'] = $cand['id'];
5539 - }
5540 - }
5541 -
5542 3170 // Sort ALL similarities for testing display (highest first)
5543 3171 usort($all_similarities, function ($a, $b) {
5544 3172 return $b['similarity'] <=> $a['similarity'];
5545 3173 });
5546 -
5547 - // Sort URL groups by best score (highest first)
5548 - uasort($url_groups, function($a, $b) {
5549 - return $b['best_score'] <=> $a['best_score'];
3174 +
3175 + // Sort relevant results by similarity (highest first)
3176 + usort($relevant_results, function ($a, $b) {
3177 + return $b['similarity'] <=> $a['similarity'];
5550 3178 });
5551 -
5552 - // Get RAG sources limit from options (default 6, min 3, max 10)
5553 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5554 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5555 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5556 -
5557 - // Take top N unique URLs based on user setting
5558 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5559 -
5560 - // Track which document IDs are used for context
3179 +
3180 + // Get top 5 results for actual content (standard approach)
3181 + $top_results = array_slice($relevant_results, 0, 5);
3182 +
3183 + // NOW mark which documents are actually used for context
5561 3184 $used_document_ids = [];
5562 - foreach ($top_urls as $group) {
5563 - if ($group['is_chunked']) {
5564 - foreach ($group['chunks'] as $chunk) {
5565 - $used_document_ids[] = $chunk['id'];
5566 - }
5567 - } elseif ($group['single_id']) {
5568 - $used_document_ids[] = $group['single_id'];
5569 - }
3185 + foreach ($top_results as $result) {
3186 + $used_document_ids[] = $result['id'];
5570 3187 }
5571 -
3188 +
5572 3189 // Update the all_similarities array to mark which were actually used
5573 3190 foreach ($all_similarities as &$similarity_item) {
5574 3191 $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5575 3192 }
5576 -
5577 - // Store top 10 for testing panel
3193 +
3194 + // Store top 10 for testing panel (now with correct used_for_context flags)
5578 3195 $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5579 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5580 -
3196 + $this->last_similarity_analysis['total_checked'] = count($embeddings);
3197 +
3198 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " top matches for testing");
3199 +
5581 3200 // Initialize final content
5582 3201 $content = '';
5583 - $matches_used = 0;
5584 - $total_chunks_used = 0;
5585 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5586 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5587 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5588 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5589 -
5590 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5591 - // Use fresh options to ensure we get the latest setting value
5592 - $fresh_options = get_option('mxchat_options', []);
5593 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5594 -
5595 - // Build content from top sources
5596 - foreach ($top_urls as $group_key => $group) {
5597 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5598 -
5599 - // Stop if we've hit the total chunk limit
5600 - if ($total_chunks_used >= $max_total_chunks) {
5601 - break;
3202 +
3203 + // Track document IDs to avoid duplicates
3204 + $added_document_ids = [];
3205 +
3206 + // Fetch and format content for each selected result
3207 + foreach ($top_results as $index => $result) {
3208 + if (in_array($result['id'], $added_document_ids)) {
3209 + continue;
5602 3210 }
5603 -
5604 - $full_text = '';
5605 - $chunks_in_this_source = 1; // Default for non-chunked content
5606 -
5607 - if ($group['is_chunked']) {
5608 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5609 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5610 -
5611 - // Fetch chunks for this URL with limit
5612 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5613 -
5614 - // If fetching all chunks fails, fall back to matched chunks
5615 - if (empty($full_text)) {
5616 - // Sort matched chunks by index and concatenate
5617 - usort($group['chunks'], function($a, $b) {
5618 - return $a['chunk_index'] <=> $b['chunk_index'];
5619 - });
5620 -
5621 - $chunk_texts = array();
5622 - $chunks_in_this_source = 0;
5623 - foreach ($group['chunks'] as $chunk) {
5624 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5625 - break;
5626 - }
5627 - $chunk_texts[] = $chunk['text'];
5628 - $chunks_in_this_source++;
5629 - }
5630 - $full_text = implode("\n\n", $chunk_texts);
3211 +
3212 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3213 + $added_document_ids[] = $result['id'];
3214 +
3215 + $content .= "## Reference " . ($index + 1) . " ##\n";
3216 + $content .= $chunk_content . "\n\n";
3217 +
3218 + // PDF surrounding pages logic (unchanged)
3219 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
3220 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
3221 + "SELECT id, article_content FROM {$system_prompt_table}
3222 + WHERE id IN (
3223 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
3224 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
3225 + )",
3226 + $result['id'],
3227 + $result['id']
3228 + ));
3229 +
3230 + if (!empty($surrounding_content[0])) {
3231 + $content .= "## Related Content ##\n";
3232 + $content .= $surrounding_content[0]->article_content . "\n\n";
3233 + $added_document_ids[] = $surrounding_content[0]->id;
5631 3234 }
5632 - } else {
5633 - $full_text = $group['single_text'];
5634 - $chunks_in_this_source = 1;
5635 - }
5636 -
5637 - if (!empty($full_text)) {
5638 - // Strip URLs from content if citation links are disabled
5639 - if (!$citation_links_enabled) {
5640 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5641 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
3235 +
3236 + if (!empty($surrounding_content[1])) {
3237 + $content .= "## Related Content ##\n";
3238 + $content .= $surrounding_content[1]->article_content . "\n\n";
3239 + $added_document_ids[] = $surrounding_content[1]->id;
5642 3240 }
5643 -
5644 - // Use numbered reference for URL-based entries, plain info label for manual entries
5645 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5646 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5647 - $matches_used++;
5648 - $content .= "## Reference " . $matches_used . " ##\n";
5649 - $content .= $full_text . "\n\n";
5650 -
5651 - // Only include citation URLs if citation links are enabled
5652 - if ($citation_links_enabled) {
5653 - $valid_urls[] = $source_url;
5654 - $content .= "URL: " . $source_url . "\n\n";
5655 - }
5656 - } else {
5657 - // Manual entry — no reference number, no citation
5658 - $content .= "## Information ##\n";
5659 - $content .= $full_text . "\n\n";
5660 - }
5661 -
5662 - // Extract any URLs from the text content itself (only if citation links enabled)
5663 - if ($citation_links_enabled) {
5664 - preg_match_all(
5665 - '#\bhttps?://[^\s<>"\']+#i',
5666 - $full_text,
5667 - $content_urls
5668 - );
5669 - if (!empty($content_urls[0])) {
5670 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5671 - }
5672 - }
5673 -
5674 - $total_chunks_used += $chunks_in_this_source;
5675 3241 }
5676 3242 }
5677 -
5678 - // NEW: Store unique valid URLs for validation
5679 - $this->current_valid_urls = array_unique($valid_urls);
5680 -
5681 - // Store sources and chunks counts for testing/transcript display
5682 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5683 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5684 -
5685 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5686 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5687 -
5688 - // Add response guidelines
5689 - if (empty($top_urls)) {
5690 - $content = "No reference information was found for this query.\n\n";
5691 - } else {
5692 - // Build response guidelines based on citation links setting
5693 - $content .= "\n## Response Guidelines ##\n" .
5694 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5695 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5696 - "If you don't have specific information or are uncertain about any details, it's always " .
5697 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5698 - "When information is incomplete, let them know you are unsure.\n\n";
5699 -
5700 - // Only add hyperlink instructions if citation links are enabled
5701 - if ($citation_links_enabled) {
5702 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5703 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5704 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
3243 +
3244 + // Add response guidelines
3245 + if (empty($top_results)) {
3246 + $content = "No reference information was found for this query.\n\n";
5705 3247 } else {
5706 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5707 - "Simply provide helpful answers based on the reference information without citing sources.";
3248 + $content .= "\n## Response Guidelines ##\n" .
3249 + "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
3250 + "Be conversational and friendly, but never mention your knowledge base or training data. " .
3251 + "If you don't have specific information or are uncertain about any details, it's always " .
3252 + "better to honestly say you don't know rather than making up or guessing at answers. " .
3253 + "When information is incomplete, let them know you are unsure.";
5708 3254 }
5709 - }
5710 -
3255 +
5711 3256 return trim($content);
5712 3257 }
5713 3258
5714 -/**
5715 - * Fetch and reassemble chunks for a URL from WordPress database
5716 - *
5717 - * @param string $source_url The source URL to fetch chunks for
5718 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5719 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5720 - * @return string Reassembled content from chunks
5721 - */
5722 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5723 - global $wpdb;
5724 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5725 -
5726 - // Fetch all rows with this source_url
5727 - $rows = $wpdb->get_results($wpdb->prepare(
5728 - "SELECT article_content FROM {$table}
5729 - WHERE source_url = %s
5730 - ORDER BY id ASC",
5731 - $source_url
5732 - ));
5733 -
5734 - if (empty($rows)) {
5735 - $chunk_count = 0;
5736 - return '';
5737 - }
5738 -
5739 - // Parse and sort chunks by index
5740 - $chunks = array();
5741 - foreach ($rows as $row) {
5742 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5743 -
5744 - if ($parsed['is_chunked']) {
5745 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5746 - $chunks[$chunk_index] = $parsed['text'];
5747 - } else {
5748 - // Non-chunked content - just return it
5749 - $chunks[] = $parsed['text'];
5750 - }
5751 - }
5752 -
5753 - // Sort by chunk index
5754 - ksort($chunks);
5755 -
5756 - // Apply chunk limit if specified
5757 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5758 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5759 - }
5760 -
5761 - // Store actual chunk count
5762 - $chunk_count = count($chunks);
5763 -
5764 - // Reassemble content
5765 - return implode("\n\n", $chunks);
5766 -}
5767 -
5768 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5769 - global $wpdb;
3259 +private function find_relevant_content_pinecone($user_embedding) {
3260 + $options = get_option('mxchat_pinecone_addon_options', array());
3261 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
3262 + $host = $options['mxchat_pinecone_host'] ?? '';
5770 3263
5771 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5772 - //error_log(" - bot_id: " . $bot_id);
5773 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5774 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5775 -
5776 - // Use bot-specific config or fall back to default
5777 - if ($bot_config === null) {
5778 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5779 - }
5780 -
5781 - $api_key = $bot_config['api_key'] ?? '';
5782 - $host = $bot_config['host'] ?? '';
5783 - $namespace = $bot_config['namespace'] ?? '';
5784 -
5785 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5786 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5787 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5788 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5789 -
5790 3264 // Initialize similarity analysis storage
5791 3265 $this->last_similarity_analysis = [
5792 3266 'knowledge_base_type' => 'Pinecone',
5793 - 'bot_id' => $bot_id,
5794 - 'namespace' => $namespace,
5795 3267 'top_matches' => [],
5796 3268 'threshold_used' => 0,
5797 3269 'total_checked' => 0
5798 3270 ];
5799 3271
5800 - // NEW: Initialize valid URLs array
5801 - $valid_urls = [];
5802 -
5803 3272 if (empty($host) || empty($api_key)) {
5804 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5805 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5806 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5807 - // Store empty array for valid URLs since we can't proceed
5808 - $this->current_valid_urls = [];
5809 3273 return '';
5810 3274 }
5811 3275
5812 - // Get knowledge manager instance for role checking
5813 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3276 + // Get the similarity threshold from the main options
3277 + $main_options = get_option('mxchat_options', []);
3278 + $similarity_threshold = isset($main_options['similarity_threshold'])
3279 + ? ((int) $main_options['similarity_threshold']) / 100
3280 + : 0.75;
5814 3281
5815 - // Get the similarity threshold from the bot options or main options
5816 - $bot_options = $this->get_bot_options($bot_id);
5817 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
5818 -
5819 - $similarity_threshold = isset($current_options['similarity_threshold'])
5820 - ? ((int) $current_options['similarity_threshold']) / 100
5821 - : 0.35;
5822 -
5823 3282 $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5824 3283
5825 - // Prepare the query request for Pinecone
3284 + // Prepare the query request for Pinecone (request more for testing)
5826 3285 $api_endpoint = "https://{$host}/query";
5827 3286
5828 3287 $request_body = array(
5829 3288 'vector' => $user_embedding,
5830 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
3289 + 'topK' => 20, // Request more to get good testing data
5831 3290 'includeMetadata' => true,
5832 3291 'includeValues' => true
5833 3292 );
5834 3293
5835 - // Add namespace if specified for this bot
5836 - if (!empty($namespace)) {
5837 - $request_body['namespace'] = $namespace;
5838 - }
5839 -
5840 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5841 - //error_log(" - Endpoint: " . $api_endpoint);
5842 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5843 -
5844 3294 $response = wp_remote_post($api_endpoint, array(
5845 3295 'headers' => array(
5846 3296 'Api-Key' => $api_key,
5847 3297 'accept' => 'application/json',
@@ -5851,253 +3301,47 @@
5851 3301 'timeout' => 30
5852 3302 ));
5853 3303
5854 3304 if (is_wp_error($response)) {
5855 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5856 - // Store empty array for valid URLs
5857 - $this->current_valid_urls = [];
5858 3305 return '';
5859 3306 }
5860 3307
5861 3308 $response_code = wp_remote_retrieve_response_code($response);
5862 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5863 -
5864 3309 if ($response_code !== 200) {
5865 - $response_body = wp_remote_retrieve_body($response);
5866 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5867 - // Store empty array for valid URLs
5868 - $this->current_valid_urls = [];
5869 3310 return '';
5870 3311 }
5871 3312
5872 - // ADD DETAILED DEBUG SECTION HERE
5873 - $response_body = wp_remote_retrieve_body($response);
5874 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5875 -
5876 - $results = json_decode($response_body, true);
5877 -
5878 - if (json_last_error() !== JSON_ERROR_NONE) {
5879 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5880 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5881 - // Store empty array for valid URLs
5882 - $this->current_valid_urls = [];
5883 - return '';
5884 - }
5885 -
5886 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5887 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5888 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5889 -
3313 + $results = json_decode(wp_remote_retrieve_body($response), true);
5890 3314 if (empty($results['matches'])) {
5891 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5892 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5893 - // Store empty array for valid URLs
5894 - $this->current_valid_urls = [];
5895 3315 return '';
5896 3316 }
5897 3317
5898 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
3318 + // First, determine which matches will actually be used for content
3319 + $matches_used_for_context = [];
3320 + $matches_used = 0;
5899 3321
5900 - // Log first match details for debugging
5901 - if (!empty($results['matches'][0])) {
5902 - $first_match = $results['matches'][0];
5903 - //error_log("MXCHAT DEBUG: First match details:");
5904 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
5905 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5906 - if (isset($first_match['metadata'])) {
5907 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5908 - }
5909 - }
5910 -
5911 - // Initialize the final content
5912 - $content = '';
5913 - $matches_used = 0;
5914 - $matches_used_for_context = [];
5915 - $total_chunks_used = 0;
5916 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5917 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5918 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5919 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5920 -
5921 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5922 - // Use fresh options to ensure we get the latest setting value
5923 - $fresh_options = get_option('mxchat_options', []);
5924 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5925 -
5926 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
5927 - $url_groups = array();
5928 -
5929 3322 foreach ($results['matches'] as $index => $match) {
5930 3323 // Skip if similarity is below threshold
5931 3324 if ($match['score'] < $similarity_threshold) {
5932 3325 continue;
5933 3326 }
5934 -
5935 - $metadata = $match['metadata'] ?? array();
5936 - $source_url = $metadata['source_url'] ?? '';
5937 - $match_id = $match['id'] ?? '';
5938 -
5939 - // LAZY ROLE CHECK: Only check role for content we're actually considering
5940 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
5941 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5942 -
5943 - // Skip if user doesn't have access
5944 - if (!$has_access) {
5945 - continue;
5946 - }
5947 -
5948 - // Use a unique key for manual entries without a source URL
5949 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
5950 -
5951 - // Group by source URL (or unique key for manual entries)
5952 - if (!isset($url_groups[$group_key])) {
5953 - $url_groups[$group_key] = array(
5954 - 'source_url' => $source_url,
5955 - 'best_score' => 0,
5956 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
5957 - 'chunks' => array(),
5958 - 'single_text' => ''
5959 - );
5960 - }
5961 -
5962 - // Track best score for this group
5963 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
5964 - $url_groups[$group_key]['best_score'] = $match['score'];
5965 - }
5966 -
5967 - // Store chunk info or single text
5968 - if ($url_groups[$group_key]['is_chunked']) {
5969 - $url_groups[$group_key]['chunks'][] = array(
5970 - 'id' => $match_id,
5971 - 'score' => $match['score'],
5972 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
5973 - 'text' => $metadata['text'] ?? ''
5974 - );
5975 - } else {
5976 - // Non-chunked content - just store the text
5977 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
5978 - $url_groups[$group_key]['single_id'] = $match_id;
5979 - }
5980 - }
5981 -
5982 - // Sort URL groups by best score (highest first)
5983 - uasort($url_groups, function($a, $b) {
5984 - return $b['best_score'] <=> $a['best_score'];
5985 - });
5986 -
5987 - // Get RAG sources limit from options (default 6, min 3, max 10)
5988 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5989 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5990 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5991 -
5992 - // Take top N unique URLs based on user setting
5993 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5994 -
5995 - // Track which match IDs are actually used for context
5996 - foreach ($top_urls as $group) {
5997 - if ($group['is_chunked']) {
5998 - foreach ($group['chunks'] as $chunk) {
5999 - $matches_used_for_context[] = $chunk['id'];
6000 - }
6001 - } elseif (!empty($group['single_id'])) {
6002 - $matches_used_for_context[] = $group['single_id'];
6003 - }
6004 - }
6005 -
6006 - // Build content from top sources
6007 - foreach ($top_urls as $group_key => $group) {
6008 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6009 -
6010 - // Stop if we've hit the total chunk limit
6011 - if ($total_chunks_used >= $max_total_chunks) {
3327 +
3328 + // Limit to top 5 matches above threshold
3329 + if ($matches_used >= 5) {
6012 3330 break;
6013 3331 }
6014 -
6015 - $full_text = '';
6016 - $chunks_in_this_source = 1; // Default for non-chunked content
6017 -
6018 - if ($group['is_chunked']) {
6019 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6020 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6021 -
6022 - // Fetch chunks for this URL with limit
6023 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6024 -
6025 - // If fetching all chunks fails, fall back to matched chunks
6026 - if (empty($full_text)) {
6027 - // Sort matched chunks by index and concatenate
6028 - usort($group['chunks'], function($a, $b) {
6029 - return $a['chunk_index'] <=> $b['chunk_index'];
6030 - });
6031 -
6032 - $chunk_texts = array();
6033 - $chunks_in_this_source = 0;
6034 - foreach ($group['chunks'] as $chunk) {
6035 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6036 - break;
6037 - }
6038 - $chunk_texts[] = $chunk['text'];
6039 - $chunks_in_this_source++;
6040 - }
6041 - $full_text = implode("\n\n", $chunk_texts);
6042 - }
6043 - } else {
6044 - $full_text = $group['single_text'];
6045 - $chunks_in_this_source = 1;
3332 +
3333 + if (!empty($match['metadata']['text'])) {
3334 + $matches_used_for_context[] = $match['id'] ?? $index;
3335 + $matches_used++;
6046 3336 }
6047 -
6048 - if (!empty($full_text)) {
6049 - // Strip URLs from content if citation links are disabled
6050 - if (!$citation_links_enabled) {
6051 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6052 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6053 - }
6054 -
6055 - // Use numbered reference for URL-based entries, plain info label for manual entries
6056 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6057 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6058 - $matches_used++;
6059 - $content .= "## Reference " . $matches_used . " ##\n";
6060 - $content .= $full_text . "\n\n";
6061 -
6062 - // Only include citation URLs if citation links are enabled
6063 - if ($citation_links_enabled) {
6064 - $valid_urls[] = $source_url;
6065 - $content .= "URL: " . $source_url . "\n\n";
6066 - }
6067 - } else {
6068 - // Manual entry — no reference number, no citation
6069 - $content .= "## Information ##\n";
6070 - $content .= $full_text . "\n\n";
6071 - }
6072 -
6073 - // Extract any URLs from the text content itself (only if citation links enabled)
6074 - if ($citation_links_enabled) {
6075 - preg_match_all(
6076 - '#\bhttps?://[^\s<>"\']+#i',
6077 - $full_text,
6078 - $content_urls
6079 - );
6080 - if (!empty($content_urls[0])) {
6081 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6082 - }
6083 - }
6084 -
6085 - $total_chunks_used += $chunks_in_this_source;
6086 - }
6087 3337 }
6088 -
6089 - // Process ALL matches for testing data (top 10) - with role checking for testing display
3338 +
3339 + // Process ALL matches for testing data (top 10)
6090 3340 $all_matches = [];
6091 3341 foreach ($results['matches'] as $index => $match) {
6092 3342 if ($index >= 10) break; // Limit to top 10 for testing
6093 3343
6094 - $match_id = $match['id'] ?? '';
6095 -
6096 - // Check role access for testing display (use cache if available)
6097 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6098 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6099 -
6100 3344 $source_display = '';
6101 3345 if (!empty($match['metadata']['source_url'])) {
6102 3346 $source_display = $match['metadata']['source_url'];
6103 3347 } else {
@@ -6105,34 +3349,18 @@
6105 3349 $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6106 3350 $source_display = substr(trim($content_preview), 0, 50) . '...';
6107 3351 }
6108 3352
6109 - $match_id_for_display = $match['id'] ?? $index;
6110 -
6111 - // Check for chunk metadata in Pinecone
6112 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6113 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6114 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6115 -
6116 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6117 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6118 - $is_chunk = true;
6119 - }
6120 -
3353 + $match_id = $match['id'] ?? $index;
3354 +
6121 3355 $all_matches[] = [
6122 - 'document_id' => $match_id_for_display,
3356 + 'document_id' => $match_id,
6123 3357 'similarity' => $match['score'],
6124 3358 'similarity_percentage' => round($match['score'] * 100, 2),
6125 3359 'above_threshold' => $match['score'] >= $similarity_threshold,
6126 3360 'source_display' => $source_display,
6127 3361 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6128 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6129 - 'role_restriction' => $role_restriction,
6130 - 'has_access' => $has_access,
6131 - 'filtered_out' => !$has_access,
6132 - 'is_chunk' => $is_chunk,
6133 - 'chunk_index' => $chunk_index,
6134 - 'total_chunks' => $total_chunks
3362 + 'used_for_context' => in_array($match_id, $matches_used_for_context) // Correct usage flag
6135 3363 ];
6136 3364 }
6137 3365
6138 3366 // Store for testing panel
@@ -6137,591 +3365,54 @@
6137 3365
6138 3366 // Store for testing panel
6139 3367 $this->last_similarity_analysis['top_matches'] = $all_matches;
6140 3368 $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6141 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6142 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6143 -
6144 - // NEW: Store unique valid URLs for validation
6145 - $this->current_valid_urls = array_unique($valid_urls);
6146 -
6147 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6148 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6149 -
6150 - // Add response guidelines
6151 - if ($matches_used === 0) {
6152 - $content = "No reference information was found for this query.\n\n";
6153 - } else {
6154 - // Build response guidelines based on citation links setting
6155 - $content .= "\n## Response Guidelines ##\n" .
6156 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6157 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6158 - "If you don't have specific information or are uncertain about any details, it's always " .
6159 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6160 - "When information is incomplete, let them know you are unsure.\n\n";
6161 -
6162 - // Only add hyperlink instructions if citation links are enabled
6163 - if ($citation_links_enabled) {
6164 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6165 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6166 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6167 - } else {
6168 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6169 - "Simply provide helpful answers based on the reference information without citing sources.";
6170 - }
6171 - }
6172 -
6173 - return trim($content);
6174 -}
6175 -
6176 -/**
6177 - * Get role restriction for a single vector (with caching)
6178 - */
6179 -private function get_single_vector_role($vector_id, $metadata = array()) {
6180 - global $wpdb;
6181 3369
6182 - if (empty($vector_id)) {
6183 - return 'public';
6184 - }
3370 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " Pinecone matches for testing");
6185 3371
6186 - // Check cache first
6187 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6188 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
3372 + // Initialize the final content
3373 + $content = '';
3374 + $matches_used = 0;
6189 3375
6190 - if ($cached_role !== false) {
6191 - return $cached_role;
6192 - }
6193 -
6194 - $role_restriction = 'public';
6195 -
6196 - // First try Pinecone metadata
6197 - if (!empty($metadata['role_restriction'])) {
6198 - $role_restriction = $metadata['role_restriction'];
6199 - } else {
6200 - // Check WordPress table for user-modified roles
6201 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6202 - $stored_role = $wpdb->get_var($wpdb->prepare(
6203 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6204 - $vector_id
6205 - ));
3376 + // Process each match for actual content (this is the real content generation)
3377 + foreach ($results['matches'] as $index => $match) {
3378 + // Skip if similarity is below threshold
3379 + if ($match['score'] < $similarity_threshold) {
3380 + continue;
3381 + }
6206 3382
6207 - if ($stored_role) {
6208 - $role_restriction = $stored_role;
3383 + // Limit to top 5 matches above threshold
3384 + if ($matches_used >= 5) {
3385 + break;
6209 3386 }
6210 - }
6211 -
6212 - // Cache individual role for 1 hour
6213 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6214 -
6215 - return $role_restriction;
6216 -}
6217 -
6218 -/**
6219 - * Fetch and reassemble all chunks for a URL from Pinecone
6220 - *
6221 - * @param string $source_url The source URL to fetch chunks for
6222 - * @param array $bot_config Bot-specific Pinecone configuration
6223 - * @return string Reassembled content from all chunks
6224 - */
6225 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6226 - $api_key = $bot_config['api_key'] ?? '';
6227 - $host = $bot_config['host'] ?? '';
6228 - $namespace = $bot_config['namespace'] ?? '';
6229 -
6230 - if (empty($host) || empty($api_key)) {
6231 - $chunk_count = 0;
6232 - return '';
6233 - }
6234 -
6235 - $base_hash = md5($source_url);
6236 -
6237 - // Use Pinecone list API to find all chunk vectors with this prefix
6238 - $list_url = "https://{$host}/vectors/list";
6239 -
6240 - // Limit to max_chunks if specified, otherwise fetch up to 100
6241 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6242 -
6243 - $list_body = array(
6244 - 'prefix' => $base_hash . '_chunk_',
6245 - 'limit' => $fetch_limit
6246 - );
6247 -
6248 - if (!empty($namespace)) {
6249 - $list_body['namespace'] = $namespace;
6250 - }
6251 -
6252 - $list_response = wp_remote_post($list_url, array(
6253 - 'headers' => array(
6254 - 'Api-Key' => $api_key,
6255 - 'accept' => 'application/json',
6256 - 'content-type' => 'application/json'
6257 - ),
6258 - 'body' => wp_json_encode($list_body),
6259 - 'timeout' => 30
6260 - ));
6261 -
6262 - if (is_wp_error($list_response)) {
6263 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6264 - return '';
6265 - }
6266 -
6267 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6268 -
6269 - if (empty($list_data['vectors'])) {
6270 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6271 - return '';
6272 - }
6273 -
6274 - // Extract vector IDs
6275 - $vector_ids = array();
6276 - foreach ($list_data['vectors'] as $vector) {
6277 - if (isset($vector['id'])) {
6278 - $vector_ids[] = $vector['id'];
6279 - }
6280 - }
6281 -
6282 - if (empty($vector_ids)) {
6283 - return '';
6284 - }
6285 -
6286 - // Fetch all chunk content
6287 - $fetch_url = "https://{$host}/vectors/fetch";
6288 -
6289 - $fetch_body = array(
6290 - 'ids' => $vector_ids
6291 - );
6292 -
6293 - if (!empty($namespace)) {
6294 - $fetch_body['namespace'] = $namespace;
6295 - }
6296 -
6297 - $fetch_response = wp_remote_post($fetch_url, array(
6298 - 'headers' => array(
6299 - 'Api-Key' => $api_key,
6300 - 'accept' => 'application/json',
6301 - 'content-type' => 'application/json'
6302 - ),
6303 - 'body' => wp_json_encode($fetch_body),
6304 - 'timeout' => 30
6305 - ));
6306 -
6307 - if (is_wp_error($fetch_response)) {
6308 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6309 - return '';
6310 - }
6311 -
6312 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6313 -
6314 - if (empty($fetch_data['vectors'])) {
6315 - return '';
6316 - }
6317 -
6318 - // Sort chunks by index and reassemble
6319 - $chunks = array();
6320 - foreach ($fetch_data['vectors'] as $id => $vector) {
6321 - $metadata = $vector['metadata'] ?? array();
6322 - $chunk_index = $metadata['chunk_index'] ?? 0;
6323 - $text = $metadata['text'] ?? '';
6324 -
6325 - // Store chunk with its index
6326 - $chunks[$chunk_index] = $text;
6327 - }
6328 -
6329 - // Sort by chunk index
6330 - ksort($chunks);
6331 -
6332 - // Apply chunk limit if specified
6333 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6334 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6335 - }
6336 -
6337 - // Store actual chunk count
6338 - $chunk_count = count($chunks);
6339 -
6340 - // Reassemble content
6341 - return implode("\n\n", $chunks);
6342 -}
6343 -
6344 -/**
6345 - * Search for relevant content using OpenAI Vector Store (File Search)
6346 - *
6347 - * @param string $user_query The user's query text
6348 - * @param string $bot_id The bot ID
6349 - * @param array $vectorstore_config Vector Store configuration
6350 - * @return string Formatted context string with references
6351 - */
6352 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6353 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6354 - //error_log(" - bot_id: " . $bot_id);
6355 - //error_log(" - user_query length: " . strlen($user_query));
6356 -
6357 - // Get OpenAI API key
6358 - $mxchat_options = get_option('mxchat_options', array());
6359 - $api_key = $mxchat_options['api_key'] ?? '';
6360 -
6361 - // Reset vectorstore error tracking
6362 - $this->last_vectorstore_error = null;
6363 -
6364 - if (empty($api_key)) {
6365 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6366 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6367 - $this->current_valid_urls = [];
6368 - return '';
6369 - }
6370 -
6371 - // Get Vector Store configuration
6372 - if (empty($vectorstore_config)) {
6373 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6374 - }
6375 -
6376 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6377 - $max_results = $vectorstore_config['max_results'] ?? 5;
6378 -
6379 - if (empty($vectorstore_ids_string)) {
6380 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6381 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6382 - $this->current_valid_urls = [];
6383 - return '';
6384 - }
6385 -
6386 - // Parse Vector Store IDs
6387 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6388 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6389 -
6390 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6391 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6392 -
6393 - // Initialize similarity analysis storage
6394 - $this->last_similarity_analysis = [
6395 - 'knowledge_base_type' => 'OpenAI Vector Store',
6396 - 'bot_id' => $bot_id,
6397 - 'vectorstore_ids' => $vectorstore_ids,
6398 - 'top_matches' => [],
6399 - 'threshold_used' => 0,
6400 - 'total_checked' => 0
6401 - ];
6402 -
6403 - $valid_urls = [];
6404 -
6405 - // Get the selected model
6406 - $bot_options = $this->get_bot_options($bot_id);
6407 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6408 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6409 -
6410 - // Verify it's an OpenAI model
6411 - if (!$this->is_openai_chat_model($selected_model)) {
6412 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6413 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6414 - $this->current_valid_urls = [];
6415 - return '';
6416 - }
6417 -
6418 - // Use OpenAI Responses API with file_search tool
6419 - $request_body = array(
6420 - 'model' => $selected_model,
6421 - 'input' => $user_query,
6422 - 'tools' => array(
6423 - array(
6424 - 'type' => 'file_search',
6425 - 'vector_store_ids' => $vectorstore_ids,
6426 - 'max_num_results' => intval($max_results)
6427 - )
6428 - ),
6429 - 'include' => array('output[*].file_search_call.search_results')
6430 - );
6431 -
6432 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6433 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6434 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6435 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6436 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6437 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6438 -
6439 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6440 - 'headers' => array(
6441 - 'Authorization' => 'Bearer ' . $api_key,
6442 - 'Content-Type' => 'application/json'
6443 - ),
6444 - 'body' => wp_json_encode($request_body),
6445 - 'timeout' => 60
6446 - ));
6447 -
6448 - if (is_wp_error($response)) {
6449 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6450 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6451 - $this->current_valid_urls = [];
6452 - return '';
6453 - }
6454 -
6455 - $response_code = wp_remote_retrieve_response_code($response);
6456 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6457 -
6458 - $response_body = wp_remote_retrieve_body($response);
6459 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6460 -
6461 - if ($response_code !== 200) {
6462 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6463 - $api_error_detail = '';
6464 - $decoded_error = json_decode($response_body, true);
6465 - if (isset($decoded_error['error']['message'])) {
6466 - $api_error_detail = $decoded_error['error']['message'];
6467 - }
6468 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6469 - $this->current_valid_urls = [];
6470 - return '';
6471 - }
6472 - $result = json_decode($response_body, true);
6473 -
6474 - if (json_last_error() !== JSON_ERROR_NONE) {
6475 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6476 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6477 - $this->current_valid_urls = [];
6478 - return '';
6479 - }
6480 -
6481 - // Debug: Log the structure of the result
6482 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6483 - if (isset($result['output'])) {
6484 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6485 - foreach ($result['output'] as $idx => $out) {
6486 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6487 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6488 - }
6489 - } else {
6490 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6491 - }
6492 -
6493 - // Extract file search results from the response
6494 - $content = '';
6495 - $matches_used = 0;
6496 - $all_matches = [];
6497 -
6498 - // The Responses API returns output array with tool results
6499 - if (isset($result['output']) && is_array($result['output'])) {
6500 - foreach ($result['output'] as $output_item) {
6501 - // Look for file_search_call results
6502 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6503 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6504 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6505 -
6506 - // Check for search_results in the output item directly
6507 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6508 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6509 -
6510 - if (empty($search_results)) {
6511 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6512 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6513 - }
6514 -
6515 - foreach ($search_results as $index => $search_result) {
6516 - $filename = $search_result['filename'] ?? '';
6517 - $score = $search_result['score'] ?? 0;
6518 - $text_content = '';
6519 -
6520 - // Extract text content from the result
6521 - // The text can be directly on the result OR nested under content array
6522 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6523 - // Direct text field (OpenAI's actual format)
6524 - $text_content = $search_result['text'];
6525 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6526 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6527 - // Nested content array format
6528 - foreach ($search_result['content'] as $content_item) {
6529 - if (isset($content_item['text'])) {
6530 - $text_content .= $content_item['text'] . "\n";
6531 - }
6532 - }
6533 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6534 - } else {
6535 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6536 - }
6537 -
6538 - if (!empty($text_content)) {
6539 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6540 - $content .= trim($text_content) . "\n\n";
6541 -
6542 - if (!empty($filename)) {
6543 - $content .= "Source: " . $filename . "\n\n";
6544 - }
6545 -
6546 - // Extract URLs from content
6547 - preg_match_all(
6548 - '#\bhttps?://[^\s<>"\']+#i',
6549 - $text_content,
6550 - $content_urls
6551 - );
6552 - if (!empty($content_urls[0])) {
6553 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6554 - }
6555 -
6556 - $matches_used++;
6557 - }
6558 -
6559 - // Store for similarity analysis
6560 - $all_matches[] = [
6561 - 'document_id' => $filename ?: ('result_' . $index),
6562 - 'similarity' => $score,
6563 - 'similarity_percentage' => round($score * 100, 2),
6564 - 'above_threshold' => true,
6565 - 'source_display' => $filename,
6566 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6567 - 'used_for_context' => true,
6568 - 'role_restriction' => 'public',
6569 - 'has_access' => true,
6570 - 'filtered_out' => false
6571 - ];
6572 - }
3387 +
3388 + if (!empty($match['metadata']['text'])) {
3389 + $content .= "## Reference " . ($matches_used + 1) . " ##\n";
3390 + $content .= $match['metadata']['text'] . "\n\n";
3391 +
3392 + if (!empty($match['metadata']['source_url'])) {
3393 + $content .= "URL: " . $match['metadata']['source_url'] . "\n\n";
6573 3394 }
6574 -
6575 - // Also check for message content with annotations (citations)
6576 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6577 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6578 - foreach ($output_item['content'] as $content_block) {
6579 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6580 - foreach ($content_block['annotations'] as $annotation) {
6581 - if (isset($annotation['filename'])) {
6582 - $filename = $annotation['filename'];
6583 - $score = $annotation['score'] ?? 0;
6584 - $text_content = '';
6585 -
6586 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6587 - foreach ($annotation['content'] as $ann_content) {
6588 - if (isset($ann_content['text'])) {
6589 - $text_content .= $ann_content['text'] . "\n";
6590 - }
6591 - }
6592 - }
6593 -
6594 - if (!empty($text_content) && $matches_used < $max_results) {
6595 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6596 - $content .= trim($text_content) . "\n\n";
6597 - $content .= "Source: " . $filename . "\n\n";
6598 -
6599 - preg_match_all(
6600 - '#\bhttps?://[^\s<>"\']+#i',
6601 - $text_content,
6602 - $content_urls
6603 - );
6604 - if (!empty($content_urls[0])) {
6605 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6606 - }
6607 -
6608 - $matches_used++;
6609 -
6610 - $all_matches[] = [
6611 - 'document_id' => $filename,
6612 - 'similarity' => $score,
6613 - 'similarity_percentage' => round($score * 100, 2),
6614 - 'above_threshold' => true,
6615 - 'source_display' => $filename,
6616 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6617 - 'used_for_context' => true,
6618 - 'role_restriction' => 'public',
6619 - 'has_access' => true,
6620 - 'filtered_out' => false
6621 - ];
6622 - }
6623 - }
6624 - }
6625 - }
6626 - }
6627 - }
6628 - }
3395 +
3396 + $matches_used++;
6629 3397 }
6630 3398 }
6631 -
6632 - // Store for testing panel
6633 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6634 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6635 -
6636 - // Store unique valid URLs for validation
6637 - $this->current_valid_urls = array_unique($valid_urls);
6638 -
6639 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6640 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6641 -
6642 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6643 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6644 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6645 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6646 - if ($matches_used > 0) {
6647 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6648 - }
6649 -
6650 - // Check if citation links are enabled
6651 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6652 -
3399 +
6653 3400 // Add response guidelines
6654 3401 if ($matches_used === 0) {
6655 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6656 3402 $content = "No reference information was found for this query.\n\n";
6657 3403 } else {
6658 - // Build response guidelines based on citation links setting
6659 - $content .= "\n## Response Guidelines ##\n" .
6660 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6661 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6662 - "If you don't have specific information or are uncertain about any details, it's always " .
6663 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6664 - "When information is incomplete, let them know you are unsure.\n\n";
6665 -
6666 - // Only add hyperlink instructions if citation links are enabled
6667 - if ($citation_links_enabled) {
6668 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6669 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6670 - } else {
6671 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6672 - "Simply provide helpful answers based on the reference information without citing sources.";
6673 - }
3404 + $content .= "\n## Response Guidelines ##\n" .
3405 + "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
3406 + "Be conversational and friendly, but never mention your knowledge base or training data. " .
3407 + "If you don't have specific information or are uncertain about any details, it's always " .
3408 + "better to honestly say you don't know rather than making up or guessing at answers. " .
3409 + "When information is incomplete, let them know you are unsure.";
6674 3410 }
6675 -
6676 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6677 -
3411 +
6678 3412 return trim($content);
6679 3413 }
6680 3414
6681 -/**
6682 - * Check if the given model is an OpenAI chat model
6683 - *
6684 - * @param string $model The model ID
6685 - * @return bool True if it's an OpenAI model
6686 - */
6687 -private function is_openai_chat_model($model) {
6688 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6689 - foreach ($openai_prefixes as $prefix) {
6690 - if (strpos($model, $prefix) === 0) {
6691 - return true;
6692 - }
6693 - }
6694 - return false;
6695 -}
6696 -
6697 -/**
6698 - * Get bot-specific Vector Store configuration
6699 - *
6700 - * @param string $bot_id The bot ID
6701 - * @return array Configuration array
6702 - */
6703 -private function get_bot_vectorstore_config($bot_id = 'default') {
6704 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6705 -
6706 - // Default global settings
6707 - $default_config = array(
6708 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6709 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6710 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6711 - );
6712 -
6713 - // Allow multi-bot plugin to override with bot-specific settings
6714 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6715 -
6716 - // Preserve max_results from global settings if not set in bot config
6717 - if (!isset($bot_config['max_results'])) {
6718 - $bot_config['max_results'] = $default_config['max_results'];
6719 - }
6720 -
6721 - return $bot_config;
6722 -}
6723 -
6724 3415 private function mxchat_find_relevant_products($user_embedding) {
6725 3416 //error_log('MXChat Vector Search: Starting product search...');
6726 3417
6727 3418 // Retrieve the add-on settings from the database
@@ -6742,75 +3433,73 @@
6742 3433 }
6743 3434 private function find_relevant_products_wordpress($user_embedding) {
6744 3435 global $wpdb;
6745 3436 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3437 + $cache_key = 'mxchat_system_prompt_embeddings';
3438 + $batch_size = 500;
6746 3439
6747 - if (!is_array($user_embedding)) {
6748 - return '';
6749 - }
3440 + // Original WordPress database search logic
3441 + // [Previous implementation remains the same]
3442 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3443 + if ($embeddings === false) {
3444 + $embeddings = [];
3445 + $offset = 0;
6750 3446
6751 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6752 - // results above the similarity threshold. Peak memory is bounded by
6753 - // $batch_size embedding rows plus a 3-element top list.
6754 - $batch_size = 250;
6755 - $similarity_threshold = 0.85;
6756 - $top_k = 3;
6757 - $top_results = [];
6758 - $offset = 0;
3447 + do {
3448 + $query = $wpdb->prepare(
3449 + "SELECT id, embedding_vector
3450 + FROM {$system_prompt_table}
3451 + LIMIT %d OFFSET %d",
3452 + $batch_size,
3453 + $offset
3454 + );
6759 3455
6760 - do {
6761 - $batch = $wpdb->get_results($wpdb->prepare(
6762 - "SELECT id, embedding_vector
6763 - FROM {$system_prompt_table}
6764 - LIMIT %d OFFSET %d",
6765 - $batch_size,
6766 - $offset
6767 - ));
3456 + $batch = $wpdb->get_results($query);
3457 + if (empty($batch)) {
3458 + break;
3459 + }
6768 3460
6769 - if (empty($batch)) {
6770 - break;
6771 - }
3461 + $embeddings = array_merge($embeddings, $batch);
3462 + $offset += $batch_size;
6772 3463
6773 - foreach ($batch as $row) {
6774 - $database_embedding = $row->embedding_vector
6775 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6776 - : null;
3464 + unset($batch);
6777 3465
6778 - if (!is_array($database_embedding)) {
6779 - unset($database_embedding);
6780 - continue;
6781 - }
3466 + } while (true);
6782 3467
3468 + if (empty($embeddings)) {
3469 + return '';
3470 + }
3471 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3472 + }
3473 +
3474 + $relevant_results = [];
3475 + foreach ($embeddings as $embedding) {
3476 + $database_embedding = $embedding->embedding_vector
3477 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3478 + : null;
3479 + if (is_array($database_embedding) && is_array($user_embedding)) {
6783 3480 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6784 - unset($database_embedding);
6785 -
6786 - if ($similarity < $similarity_threshold) {
6787 - continue;
6788 - }
6789 -
6790 - // Insert into bounded top-K (kept sorted descending)
6791 - if (count($top_results) < $top_k) {
6792 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6793 - usort($top_results, function ($a, $b) {
6794 - return $b['similarity'] <=> $a['similarity'];
6795 - });
6796 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6797 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6798 - usort($top_results, function ($a, $b) {
6799 - return $b['similarity'] <=> $a['similarity'];
6800 - });
6801 - }
3481 + $relevant_results[] = [
3482 + 'id' => $embedding->id,
3483 + 'similarity' => $similarity
3484 + ];
6802 3485 }
3486 + unset($database_embedding);
3487 + }
6803 3488
6804 - unset($batch);
6805 - $offset += $batch_size;
6806 - } while (true);
3489 + // Use fixed threshold for products
3490 + $similarity_threshold = 0.85;
6807 3491
6808 - if (empty($top_results)) {
6809 - return '';
6810 - }
3492 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3493 + return $result['similarity'] >= $similarity_threshold;
3494 + });
3495 + usort($relevant_results, function ($a, $b) {
3496 + return $b['similarity'] <=> $a['similarity'];
3497 + });
6811 3498
3499 + $top_results = array_slice($relevant_results, 0, 5);
6812 3500 $content = '';
3501 +
6813 3502 foreach ($top_results as $result) {
6814 3503 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6815 3504 $content .= $chunk_content . "\n\n";
6816 3505 }
@@ -6816,10 +3505,8 @@
6816 3505 }
6817 3506
6818 3507 return trim($content);
6819 3508 }
6820 -
6821 -
6822 3509 private function find_relevant_products_pinecone($user_embedding) {
6823 3510 //error_log('Starting Pinecone product search...');
6824 3511
6825 3512 $options = get_option('mxchat_pinecone_addon_options', array());
@@ -6894,10 +3581,8 @@
6894 3581 }
6895 3582
6896 3583 return trim($content);
6897 3584 }
6898 -
6899 -
6900 3585 private function fetch_content_with_product_links($most_relevant_id) {
6901 3586 global $wpdb;
6902 3587 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6903 3588
@@ -6917,85 +3602,13 @@
6917 3602 return null;
6918 3603 }
6919 3604
6920 3605 /**
6921 - * Get system instructions for a specific bot or default
6922 - * Checks for multi-bot add-on and uses bot-specific instructions if available
6923 - * Automatically strips URLs if citation links are disabled
6924 - * Replaces {visitor_name} placeholder with actual visitor name if available
6925 - *
6926 - * @param string $bot_id The bot ID to get instructions for
6927 - * @param string $session_id Optional session ID to lookup visitor name
3606 + * Modified streaming functions to include testing data
6928 3607 */
6929 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6930 - $instructions = '';
6931 3608
6932 - // Check if multi-bot add-on is active
6933 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6934 - // Get bot-specific options from multi-bot add-on
6935 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6936 -
6937 - // If bot has custom system instructions, use those
6938 - if (!empty($bot_options['system_prompt_instructions'])) {
6939 - $instructions = $bot_options['system_prompt_instructions'];
6940 - }
6941 - }
6942 -
6943 - // Fall back to default system instructions
6944 - if (empty($instructions)) {
6945 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6946 - }
6947 -
6948 - // Check if citation links are disabled - if so, strip URLs from instructions
6949 - $fresh_options = get_option('mxchat_options', []);
6950 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6951 -
6952 - if (!$citation_links_enabled && !empty($instructions)) {
6953 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
6954 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
6955 - }
6956 -
6957 - // Replace {visitor_name} placeholder with actual visitor name if available
6958 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
6959 - $name_option_key = "mxchat_name_{$session_id}";
6960 - $visitor_name = get_option($name_option_key, '');
6961 -
6962 - if (!empty($visitor_name)) {
6963 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
6964 - } else {
6965 - // Remove placeholder if no name is available
6966 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
6967 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
6968 - }
6969 - }
6970 -
6971 - // Allow developers to filter system instructions and process shortcodes
6972 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
6973 - $instructions = do_shortcode($instructions);
6974 -
6975 - return $instructions;
6976 -}
6977 -/**
6978 - * Get the current bot ID from session or request context
6979 - */
6980 -private function get_current_bot_id($session_id = '') {
6981 - // First, check if bot_id is passed in the current request
6982 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
6983 - return sanitize_key($_POST['bot_id']);
6984 - }
6985 -
6986 - // If not in POST, try to get it from session data
6987 - if (!empty($session_id)) {
6988 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
6989 - if (!empty($bot_id)) {
6990 - return $bot_id;
6991 - }
6992 - }
6993 -
6994 - // Fall back to default
6995 - return 'default';
6996 -}
6997 -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') {
3609 +// 1. Update the main handler to pass testing data to streaming functions
3610 +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) {
6998 3611 try {
6999 3612 if (!$relevant_content) {
7000 3613 $error_response = [
7001 3614 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
@@ -7001,74 +3614,25 @@
7001 3614 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7002 3615 'error_code' => 'no_relevant_content'
7003 3616 ];
7004 3617
3618 + // Add testing data to error response if available
7005 3619 if ($testing_data !== null) {
7006 3620 $error_response['testing_data'] = $testing_data;
3621 + //error_log("MxChat Testing: Added testing data to no_relevant_content error");
7007 3622 }
7008 3623
7009 3624 return $error_response;
7010 3625 }
7011 3626
3627 + // Ensure conversation_history is an array
7012 3628 if (!is_array($conversation_history)) {
7013 3629 $conversation_history = array();
7014 3630 }
7015 3631
7016 - // Check if this is an OpenRouter model
7017 - if ($selected_model === 'openrouter') {
7018 - // Get the actual OpenRouter model from options
7019 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7020 -
7021 - if (empty($openrouter_selected_model)) {
7022 - $error_response = [
7023 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7024 - 'error_code' => 'no_openrouter_model_selected'
7025 - ];
7026 - if ($testing_data !== null) {
7027 - $error_response['testing_data'] = $testing_data;
7028 - }
7029 - return $error_response;
7030 - }
7031 -
7032 - if (empty($openrouter_api_key)) {
7033 - $error_response = [
7034 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7035 - 'error_code' => 'missing_openrouter_api_key'
7036 - ];
7037 - if ($testing_data !== null) {
7038 - $error_response['testing_data'] = $testing_data;
7039 - }
7040 - return $error_response;
7041 - }
7042 -
7043 - if ($streaming) {
7044 - return $this->mxchat_generate_response_openrouter_stream(
7045 - $openrouter_selected_model,
7046 - $openrouter_api_key,
7047 - $conversation_history,
7048 - $relevant_content,
7049 - $session_id,
7050 - $testing_data
7051 - );
7052 - } else {
7053 - $response = $this->mxchat_generate_response_openrouter(
7054 - $openrouter_selected_model,
7055 - $openrouter_api_key,
7056 - $conversation_history,
7057 - $relevant_content
7058 - );
7059 - }
7060 -
7061 - if (is_array($response) && isset($response['error'])) {
7062 - if ($testing_data !== null) {
7063 - $response['testing_data'] = $testing_data;
7064 - }
7065 - return $response;
7066 - }
7067 -
7068 - return $response;
7069 - }
7070 -
3632 + // Get selected model with default fallback
3633 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
3634 +
7071 3635 // Extract model prefix to determine the provider
7072 3636 $model_parts = explode('-', $selected_model);
7073 3637 $provider = strtolower($model_parts[0]);
7074 3638
@@ -7110,9 +3674,9 @@
7110 3674 $claude_api_key,
7111 3675 $conversation_history,
7112 3676 $relevant_content,
7113 3677 $session_id,
7114 - $testing_data
3678 + $testing_data // Pass testing data
7115 3679 );
7116 3680 } else {
7117 3681 $response = $this->mxchat_generate_response_claude(
7118 3682 $selected_model,
@@ -7140,9 +3704,9 @@
7140 3704 $xai_api_key,
7141 3705 $conversation_history,
7142 3706 $relevant_content,
7143 3707 $session_id,
7144 - $testing_data
3708 + $testing_data // Pass testing data
7145 3709 );
7146 3710 } else {
7147 3711 $response = $this->mxchat_generate_response_xai(
7148 3712 $selected_model,
@@ -7163,57 +3727,16 @@
7163 3727 $error_response['testing_data'] = $testing_data;
7164 3728 }
7165 3729 return $error_response;
7166 3730 }
7167 - if ($streaming) {
7168 - return $this->mxchat_generate_response_deepseek_stream(
7169 - $selected_model,
7170 - $deepseek_api_key,
7171 - $conversation_history,
7172 - $relevant_content,
7173 - $session_id,
7174 - $testing_data
7175 - );
7176 - } else {
7177 - $response = $this->mxchat_generate_response_deepseek(
7178 - $selected_model,
7179 - $deepseek_api_key,
7180 - $conversation_history,
7181 - $relevant_content
7182 - );
7183 - }
3731 + $response = $this->mxchat_generate_response_deepseek(
3732 + $selected_model,
3733 + $deepseek_api_key,
3734 + $conversation_history,
3735 + $relevant_content
3736 + );
7184 3737 break;
7185 3738
7186 - case 'custom':
7187 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7188 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7189 - if (empty($cp_base_url)) {
7190 - $error_response = [
7191 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7192 - 'error_code' => 'missing_custom_provider_base_url'
7193 - ];
7194 - if ($testing_data !== null) {
7195 - $error_response['testing_data'] = $testing_data;
7196 - }
7197 - return $error_response;
7198 - }
7199 - if ($streaming) {
7200 - return $this->mxchat_generate_response_custom_stream(
7201 - $selected_model,
7202 - $conversation_history,
7203 - $relevant_content,
7204 - $session_id,
7205 - $testing_data
7206 - );
7207 - } else {
7208 - $response = $this->mxchat_generate_response_custom(
7209 - $selected_model,
7210 - $conversation_history,
7211 - $relevant_content
7212 - );
7213 - }
7214 - break;
7215 -
7216 3739 case 'gpt':
7217 3740 case 'o1':
7218 3741 if (empty($api_key)) {
7219 3742 $error_response = [
@@ -7224,27 +3747,9 @@
7224 3747 $error_response['testing_data'] = $testing_data;
7225 3748 }
7226 3749 return $error_response;
7227 3750 }
7228 -
7229 - // Check if web search is enabled for this OpenAI model
7230 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7231 - // Models that don't support web search
7232 - $unsupported_web_search_models = array('gpt-4.1-nano');
7233 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7234 -
7235 - if ($web_search_enabled && $model_supports_web_search) {
7236 - // Use Responses API (required for some models, or when web search is enabled)
7237 - return $this->mxchat_generate_response_openai_web_search(
7238 - $selected_model,
7239 - $api_key,
7240 - $conversation_history,
7241 - $relevant_content,
7242 - $session_id,
7243 - $testing_data,
7244 - $streaming
7245 - );
7246 - } elseif ($streaming) {
3751 + if ($streaming) {
7247 3752 return $this->mxchat_generate_response_openai_stream(
7248 3753 $selected_model,
7249 3754 $api_key,
7250 3755 $conversation_history,
@@ -7249,9 +3754,9 @@
7249 3754 $api_key,
7250 3755 $conversation_history,
7251 3756 $relevant_content,
7252 3757 $session_id,
7253 - $testing_data
3758 + $testing_data // Pass testing data
7254 3759 );
7255 3760 } else {
7256 3761 $response = $this->mxchat_generate_response_openai(
7257 3762 $selected_model,
@@ -7262,8 +3767,9 @@
7262 3767 }
7263 3768 break;
7264 3769
7265 3770 default:
3771 + // Default to OpenAI for custom models or unrecognized prefixes
7266 3772 if (empty($api_key)) {
7267 3773 $error_response = [
7268 3774 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7269 3775 'error_code' => 'missing_openai_api_key'
@@ -7272,25 +3778,9 @@
7272 3778 $error_response['testing_data'] = $testing_data;
7273 3779 }
7274 3780 return $error_response;
7275 3781 }
7276 -
7277 - // Check if web search is enabled (default case also handles OpenAI models)
7278 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7279 - $unsupported_web_search_models = array('gpt-4.1-nano');
7280 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7281 -
7282 - if ($web_search_enabled && $model_supports_web_search) {
7283 - return $this->mxchat_generate_response_openai_web_search(
7284 - $selected_model,
7285 - $api_key,
7286 - $conversation_history,
7287 - $relevant_content,
7288 - $session_id,
7289 - $testing_data,
7290 - $streaming
7291 - );
7292 - } elseif ($streaming) {
3782 + if ($streaming) {
7293 3783 return $this->mxchat_generate_response_openai_stream(
7294 3784 $selected_model,
7295 3785 $api_key,
7296 3786 $conversation_history,
@@ -7295,9 +3785,9 @@
7295 3785 $api_key,
7296 3786 $conversation_history,
7297 3787 $relevant_content,
7298 3788 $session_id,
7299 - $testing_data
3789 + $testing_data // Pass testing data
7300 3790 );
7301 3791 } else {
7302 3792 $response = $this->mxchat_generate_response_openai(
7303 3793 $selected_model,
@@ -7308,18 +3798,24 @@
7308 3798 }
7309 3799 break;
7310 3800 }
7311 3801
3802 + // Check if the response is an error array from the provider-specific function
7312 3803 if (is_array($response) && isset($response['error'])) {
3804 + // Add testing data to error response if available
7313 3805 if ($testing_data !== null) {
7314 3806 $response['testing_data'] = $testing_data;
3807 + //error_log("MxChat Testing: Added testing data to provider error response");
7315 3808 }
7316 - return $response;
3809 + return $response; // Pass through the error with testing data
7317 3810 }
7318 3811
3812 + // For successful non-streaming responses, we don't add testing data here
3813 + // because it will be added in the main handler
7319 3814 return $response;
7320 3815
7321 3816 } catch (Exception $e) {
3817 + //error_log('MXChat Error: ' . $e->getMessage());
7322 3818 $error_response = [
7323 3819 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7324 3820 'error_code' => 'system_exception',
7325 3821 'exception_details' => $e->getMessage()
@@ -7324,1217 +3820,27 @@
7324 3820 'error_code' => 'system_exception',
7325 3821 'exception_details' => $e->getMessage()
7326 3822 ];
7327 3823
3824 + // Add testing data to exception response if available
7328 3825 if ($testing_data !== null) {
7329 3826 $error_response['testing_data'] = $testing_data;
3827 + //error_log("MxChat Testing: Added testing data to exception response");
7330 3828 }
7331 3829
7332 3830 return $error_response;
7333 3831 }
7334 3832 }
7335 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7336 - try {
7337 - $bot_id = $this->get_current_bot_id($session_id);
7338 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7339 -
7340 - if (!is_array($conversation_history)) {
7341 - $conversation_history = array();
7342 - }
7343 3833
7344 - $formatted_conversation = array();
7345 -
7346 - $formatted_conversation[] = array(
7347 - 'role' => 'system',
7348 - 'content' => $system_prompt_instructions . " " . $relevant_content
7349 - );
7350 -
7351 - foreach ($conversation_history as $message) {
7352 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7353 - $role = $message['role'];
7354 - if ($role === 'bot' || $role === 'agent') {
7355 - $role = 'assistant';
7356 - }
7357 - if (!in_array($role, ['system', 'assistant', 'user'])) {
7358 - $role = 'user';
7359 - }
7360 - $formatted_conversation[] = array(
7361 - 'role' => $role,
7362 - 'content' => $message['content']
7363 - );
7364 - }
7365 - }
7366 -
7367 - if (headers_sent() || !function_exists('curl_init')) {
7368 - $regular_response = $this->mxchat_generate_response_openrouter(
7369 - $selected_model,
7370 - $openrouter_api_key,
7371 - $conversation_history,
7372 - $relevant_content
7373 - );
7374 -
7375 - // Save bot response to transcript
7376 - if (!empty($regular_response) && !empty($session_id)) {
7377 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7378 - }
7379 -
7380 - $response_data = [
7381 - 'text' => $regular_response,
7382 - 'html' => '',
7383 - 'session_id' => $session_id
7384 - ];
7385 -
7386 - if ($testing_data !== null) {
7387 - $response_data['testing_data'] = $testing_data;
7388 - }
7389 -
7390 - header('Content-Type: application/json');
7391 - echo json_encode($response_data);
7392 - return true;
7393 - }
7394 -
7395 - $body = json_encode([
7396 - 'model' => $selected_model,
7397 - 'messages' => $formatted_conversation,
7398 - 'temperature' => 1,
7399 - 'stream' => true
7400 - ]);
7401 -
7402 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
7403 - // inside WRITEFUNCTION on first byte of a successful upstream.
7404 -
7405 - $captured_status_code = 0;
7406 - $captured_body_pre_stream = '';
7407 - $full_response = '';
7408 - $stream_started = false;
7409 - $buffer = '';
7410 - $errno = 0;
7411 - $last_curl_error = '';
7412 - $http_code = 0;
7413 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7414 - $backoff_ms = array(0, 750, 2000);
7415 -
7416 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7417 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7418 - usleep($backoff_ms[$attempt] * 1000);
7419 - }
7420 -
7421 - $captured_status_code = 0;
7422 - $captured_body_pre_stream = '';
7423 - $full_response = '';
7424 - $stream_started = false;
7425 - $buffer = '';
7426 -
7427 - $ch = curl_init();
7428 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
7429 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7430 - curl_setopt($ch, CURLOPT_POST, true);
7431 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7432 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7433 - 'Content-Type: application/json',
7434 - 'Authorization: Bearer ' . $openrouter_api_key,
7435 - 'HTTP-Referer: ' . home_url(),
7436 - 'X-Title: ' . get_bloginfo('name')
7437 - ));
7438 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7439 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7440 -
7441 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7442 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7443 - $captured_status_code = (int) $m[1];
7444 - }
7445 - return strlen($header);
7446 - });
7447 -
7448 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7449 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7450 - $captured_body_pre_stream .= $data;
7451 - return strlen($data);
7452 - }
7453 -
7454 - if (!$this->streaming_headers_sent) {
7455 - $this->setup_streaming_headers();
7456 - }
7457 -
7458 - if (!$stream_started && $testing_data !== null) {
7459 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7460 - flush();
7461 - $stream_started = true;
7462 - }
7463 -
7464 - $buffer .= $data;
7465 - $lines = explode("\n", $buffer);
7466 - $buffer = array_pop($lines);
7467 -
7468 - foreach ($lines as $line) {
7469 - if (trim($line) === '') {
7470 - continue;
7471 - }
7472 - if (strpos($line, 'data: ') !== 0) {
7473 - continue;
7474 - }
7475 -
7476 - $json_str = substr($line, 6);
7477 -
7478 - if (trim($json_str) === '[DONE]') {
7479 - echo "data: [DONE]\n\n";
7480 - flush();
7481 - continue;
7482 - }
7483 -
7484 - $json = json_decode(trim($json_str), true);
7485 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7486 - $content = $json['choices'][0]['delta']['content'];
7487 - $full_response .= $content;
7488 -
7489 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7490 - flush();
7491 - }
7492 - }
7493 -
7494 - return strlen($data);
7495 - });
7496 -
7497 - $response = curl_exec($ch);
7498 - $errno = curl_errno($ch);
7499 - $last_curl_error = curl_error($ch);
7500 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7501 - curl_close($ch);
7502 -
7503 - if (!$errno && $http_code === 200) {
7504 - break;
7505 - }
7506 -
7507 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7508 - $can_retry = !$this->streaming_headers_sent
7509 - && ($attempt + 1) < $max_attempts
7510 - && $is_transient;
7511 -
7512 - if (defined('WP_DEBUG') && WP_DEBUG) {
7513 - error_log(sprintf(
7514 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7515 - $attempt + 1, $max_attempts, $http_code, $errno,
7516 - $is_transient ? 'yes' : 'no',
7517 - $can_retry ? 'Retrying.' : 'Giving up.'
7518 - ));
7519 - }
7520 -
7521 - if (!$can_retry) {
7522 - break;
7523 - }
7524 - }
7525 -
7526 - if (!$errno && $http_code === 200) {
7527 - if (!empty($full_response) && !empty($session_id)) {
7528 - $rag_context_for_storage = null;
7529 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7530 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7531 -
7532 - if ($has_rag_data || $has_action_data) {
7533 - $rag_context_for_storage = [];
7534 -
7535 - if ($has_rag_data) {
7536 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7537 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7538 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7539 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7540 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7541 - }
7542 -
7543 - if ($has_action_data) {
7544 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7545 - }
7546 - }
7547 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7548 - }
7549 - return true;
7550 - }
7551 -
7552 - return $this->mxchat_stream_emit_fallback(
7553 - 'openai',
7554 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7555 - $session_id,
7556 - $testing_data
7557 - );
7558 -
7559 - } catch (Exception $e) {
7560 - return $this->mxchat_stream_emit_fallback(
7561 - 'openai',
7562 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7563 - $session_id,
7564 - $testing_data
7565 - );
7566 - }
7567 -}
7568 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
3834 +// 2. Update Claude streaming function
3835 +private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7569 3836 try {
7570 - $bot_id = $this->get_current_bot_id($session_id);
3837 + // Enable implicit flushing for real-time streaming
3838 + ob_implicit_flush(true);
7571 3839
7572 - // Get system prompt instructions using centralized function
7573 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7574 -
7575 - // Ensure conversation_history is an array
7576 - if (!is_array($conversation_history)) {
7577 - $conversation_history = array();
7578 - }
3840 + // Get system prompt instructions from options
3841 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7579 3842
7580 - // Format conversation history for OpenAI
7581 - $formatted_conversation = array();
7582 -
7583 - $formatted_conversation[] = array(
7584 - 'role' => 'system',
7585 - 'content' => $system_prompt_instructions . " " . $relevant_content
7586 - );
7587 -
7588 - foreach ($conversation_history as $message) {
7589 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7590 - $role = $message['role'];
7591 - if ($role === 'bot' || $role === 'agent') {
7592 - $role = 'assistant';
7593 - }
7594 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7595 - $role = 'user';
7596 - }
7597 - $formatted_conversation[] = array(
7598 - 'role' => $role,
7599 - 'content' => $message['content']
7600 - );
7601 - }
7602 - }
7603 -
7604 - // Check if we can actually stream
7605 - if (headers_sent() || !function_exists('curl_init')) {
7606 - // Fallback to regular response with testing data
7607 - $regular_response = $this->mxchat_generate_response_openai(
7608 - $selected_model,
7609 - $api_key,
7610 - $conversation_history,
7611 - $relevant_content
7612 - );
7613 -
7614 - // Save bot response to transcript
7615 - if (!empty($regular_response) && !empty($session_id)) {
7616 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7617 - }
7618 -
7619 - $response_data = [
7620 - 'text' => $regular_response,
7621 - 'html' => '',
7622 - 'session_id' => $session_id
7623 - ];
7624 -
7625 - if ($testing_data !== null) {
7626 - $response_data['testing_data'] = $testing_data;
7627 - }
7628 -
7629 - header('Content-Type: application/json');
7630 - echo json_encode($response_data);
7631 - return true;
7632 - }
7633 -
7634 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
7635 - $is_gpt5_model = (
7636 - strpos($selected_model, 'gpt-5') === 0 ||
7637 - $selected_model === 'gpt-5.2' ||
7638 - $selected_model === 'gpt-5.1-2025-11-13' ||
7639 - $selected_model === 'gpt-5' ||
7640 - $selected_model === 'gpt-5-mini' ||
7641 - $selected_model === 'gpt-5-nano'
7642 - );
7643 -
7644 - // Build request body with optimal settings for fast streaming
7645 - $request_body = [
7646 - 'model' => $selected_model,
7647 - 'messages' => $formatted_conversation,
7648 - 'temperature' => 1,
7649 - 'stream' => true
7650 - ];
7651 -
7652 - // Add reasoning_effort only for GPT-5 models that support it
7653 - // These chat models don't support reasoning_effort parameter
7654 - $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');
7655 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
7656 - // GPT-5.1 uses 'low' instead of 'minimal'
7657 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7658 - $request_body['reasoning_effort'] = 'low';
7659 - } elseif ($selected_model === 'gpt-5.5') {
7660 - $request_body['reasoning_effort'] = 'none';
7661 - } elseif ($selected_model === 'gpt-5.4') {
7662 - $request_body['reasoning_effort'] = 'none';
7663 - } else {
7664 - $request_body['reasoning_effort'] = 'minimal';
7665 - }
7666 - }
7667 -
7668 - $body = json_encode($request_body);
7669 -
7670 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
7671 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
7672 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
7673 -
7674 - $captured_status_code = 0;
7675 - $captured_body_pre_stream = '';
7676 - $full_response = '';
7677 - $stream_started = false;
7678 - $buffer = '';
7679 - $errno = 0;
7680 - $last_curl_error = '';
7681 - $http_code = 0;
7682 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7683 - $backoff_ms = array(0, 750, 2000);
7684 -
7685 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7686 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7687 - usleep($backoff_ms[$attempt] * 1000);
7688 - }
7689 -
7690 - // Reset per-attempt capture state.
7691 - $captured_status_code = 0;
7692 - $captured_body_pre_stream = '';
7693 - $full_response = '';
7694 - $stream_started = false;
7695 - $buffer = '';
7696 -
7697 - $ch = curl_init();
7698 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
7699 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7700 - curl_setopt($ch, CURLOPT_POST, true);
7701 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7702 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7703 - 'Content-Type: application/json',
7704 - 'Authorization: Bearer ' . $api_key
7705 - ));
7706 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7707 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7708 -
7709 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
7710 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7711 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7712 - $captured_status_code = (int) $m[1];
7713 - }
7714 - return strlen($header);
7715 - });
7716 -
7717 - // Buffer control for real-time streaming
7718 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7719 - // V2 guard: if upstream returned non-200, buffer body for transient
7720 - // classification and DO NOT emit to client. Stream channel must NOT open.
7721 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7722 - $captured_body_pre_stream .= $data;
7723 - return strlen($data);
7724 - }
7725 -
7726 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
7727 - // After this point streaming_headers_sent === true → retry is structurally blocked.
7728 - if (!$this->streaming_headers_sent) {
7729 - $this->setup_streaming_headers();
7730 - }
7731 -
7732 - // Send testing data as the first event if available
7733 - if (!$stream_started && $testing_data !== null) {
7734 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7735 - flush();
7736 - $stream_started = true;
7737 - }
7738 -
7739 - // CRITICAL FIX: Append new data to buffer
7740 - $buffer .= $data;
7741 -
7742 - // Process complete lines only
7743 - $lines = explode("\n", $buffer);
7744 -
7745 - // CRITICAL FIX: Keep the last incomplete line in the buffer
7746 - $buffer = array_pop($lines);
7747 -
7748 - foreach ($lines as $line) {
7749 - if (trim($line) === '') {
7750 - continue;
7751 - }
7752 - if (strpos($line, 'data: ') !== 0) {
7753 - continue;
7754 - }
7755 -
7756 - $json_str = substr($line, 6);
7757 -
7758 - if (trim($json_str) === '[DONE]') {
7759 - echo "data: [DONE]\n\n";
7760 - flush();
7761 - continue;
7762 - }
7763 -
7764 - $json = json_decode(trim($json_str), true);
7765 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7766 - $content = $json['choices'][0]['delta']['content'];
7767 - $full_response .= $content;
7768 -
7769 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7770 - flush();
7771 - }
7772 - }
7773 -
7774 - return strlen($data);
7775 - });
7776 -
7777 - $response = curl_exec($ch);
7778 - $errno = curl_errno($ch);
7779 - $last_curl_error = curl_error($ch);
7780 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7781 - curl_close($ch);
7782 -
7783 - if (!$errno && $http_code === 200) {
7784 - break; // Happy path — WRITEFUNCTION already streamed everything.
7785 - }
7786 -
7787 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7788 - $can_retry = !$this->streaming_headers_sent
7789 - && ($attempt + 1) < $max_attempts
7790 - && $is_transient;
7791 -
7792 - if (defined('WP_DEBUG') && WP_DEBUG) {
7793 - error_log(sprintf(
7794 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7795 - $attempt + 1, $max_attempts, $http_code, $errno,
7796 - $is_transient ? 'yes' : 'no',
7797 - $can_retry ? 'Retrying.' : 'Giving up.'
7798 - ));
7799 - }
7800 -
7801 - if (!$can_retry) {
7802 - break;
7803 - }
7804 - }
7805 -
7806 - // Post-loop branch.
7807 - if (!$errno && $http_code === 200) {
7808 - // Happy path — save the complete response to maintain chat persistence.
7809 - if (!empty($full_response) && !empty($session_id)) {
7810 - $rag_context_for_storage = null;
7811 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7812 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7813 -
7814 - if ($has_rag_data || $has_action_data) {
7815 - $rag_context_for_storage = [];
7816 -
7817 - if ($has_rag_data) {
7818 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7819 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7820 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7821 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7822 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7823 - }
7824 -
7825 - if ($has_action_data) {
7826 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7827 - }
7828 - }
7829 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7830 - }
7831 -
7832 - return true;
7833 - }
7834 -
7835 - // Failure path — branch on whether SSE channel was opened.
7836 - return $this->mxchat_stream_emit_fallback(
7837 - 'openai',
7838 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7839 - $session_id,
7840 - $testing_data
7841 - );
7842 -
7843 - } catch (Exception $e) {
7844 - return $this->mxchat_stream_emit_fallback(
7845 - 'openai',
7846 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7847 - $session_id,
7848 - $testing_data
7849 - );
7850 - }
7851 -}
7852 -
7853 -/**
7854 - * Shared fallback emitter for streaming chat functions. Two outcomes:
7855 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
7856 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
7857 - * normal bot bubble. Transcript row is persisted.
7858 - * - streaming_headers_sent === false: SSE channel never opened (retries
7859 - * exhausted on initial connect). Emit a clean JSON response — the path
7860 - * the widget would normally hit if streaming wasn't even attempted.
7861 - *
7862 - * Used by all six *_stream functions after their per-attempt retry loop.
7863 - */
7864 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
7865 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
7866 -
7867 - if ($this->streaming_headers_sent) {
7868 - if ($is_error_array) {
7869 - echo "data: " . json_encode([
7870 - 'error' => true,
7871 - 'error_message' => $regular_response['error'],
7872 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7873 - 'text' => $regular_response['error'],
7874 - 'message' => $regular_response['error']
7875 - ]) . "\n\n";
7876 - echo "data: [DONE]\n\n";
7877 - flush();
7878 - return true;
7879 - }
7880 - $fallback_message = (string) $regular_response;
7881 - if (!empty($fallback_message) && !empty($session_id)) {
7882 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7883 - }
7884 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
7885 - echo "data: [DONE]\n\n";
7886 - flush();
7887 - return true;
7888 - }
7889 -
7890 - // SSE channel never opened — clean JSON fallback.
7891 - if ($is_error_array) {
7892 - header('Content-Type: application/json');
7893 - echo json_encode(array(
7894 - 'error' => true,
7895 - 'error_message' => $regular_response['error'],
7896 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7897 - 'text' => $regular_response['error'],
7898 - 'message' => $regular_response['error'],
7899 - ));
7900 - return true;
7901 - }
7902 -
7903 - $fallback_message = (string) $regular_response;
7904 - if (!empty($fallback_message) && !empty($session_id)) {
7905 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7906 - }
7907 - $response_data = array(
7908 - 'text' => $fallback_message,
7909 - 'html' => '',
7910 - 'session_id' => $session_id,
7911 - );
7912 - if ($testing_data !== null) {
7913 - $response_data['testing_data'] = $testing_data;
7914 - }
7915 - header('Content-Type: application/json');
7916 - echo json_encode($response_data);
7917 - return true;
7918 -}
7919 -
7920 -/**
7921 - * Resolve custom (OpenAI-compatible) provider config from settings.
7922 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
7923 - */
7924 -private function mxchat_resolve_custom_provider() {
7925 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
7926 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
7927 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
7928 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
7929 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
7930 -
7931 - $chat_url = $base_url . '/chat/completions';
7932 - if (!empty($api_version)) {
7933 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
7934 - }
7935 -
7936 - $headers = array('Content-Type: application/json');
7937 - if (!empty($api_key)) {
7938 - if ($auth_scheme === 'api-key') {
7939 - $headers[] = 'api-key: ' . $api_key;
7940 - } else {
7941 - $headers[] = 'Authorization: Bearer ' . $api_key;
7942 - }
7943 - }
7944 -
7945 - return array(
7946 - 'base_url' => $base_url,
7947 - 'api_key' => $api_key,
7948 - 'model' => $model !== '' ? $model : 'default',
7949 - 'auth_scheme' => $auth_scheme,
7950 - 'api_version' => $api_version,
7951 - 'chat_url' => $chat_url,
7952 - 'headers' => $headers,
7953 - );
7954 -}
7955 -
7956 -/**
7957 - * Streaming chat completion against an OpenAI-compatible custom provider
7958 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
7959 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
7960 - */
7961 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7962 - try {
7963 - $cfg = $this->mxchat_resolve_custom_provider();
7964 - if (empty($cfg['base_url'])) {
7965 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7966 - }
7967 -
7968 - $bot_id = $this->get_current_bot_id($session_id);
7969 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7970 - if (!is_array($conversation_history)) {
7971 - $conversation_history = array();
7972 - }
7973 -
7974 - $formatted_conversation = array();
7975 - $formatted_conversation[] = array(
7976 - 'role' => 'system',
7977 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7978 - );
7979 - foreach ($conversation_history as $message) {
7980 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7981 - $role = $message['role'];
7982 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7983 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7984 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
7985 - }
7986 - }
7987 -
7988 - if (headers_sent() || !function_exists('curl_init')) {
7989 - // No streaming capability — fall through to non-stream wrapper
7990 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7991 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
7992 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
7993 - }
7994 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7995 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7996 - header('Content-Type: application/json');
7997 - echo json_encode($response_data);
7998 - return true;
7999 - }
8000 -
8001 - $request_body = array(
8002 - 'model' => $cfg['model'],
8003 - 'messages' => $formatted_conversation,
8004 - 'stream' => true,
8005 - );
8006 - $body = json_encode($request_body);
8007 -
8008 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8009 -
8010 - $captured_status_code = 0;
8011 - $captured_body_pre_stream = '';
8012 - $full_response = '';
8013 - $stream_started = false;
8014 - $buffer = '';
8015 - $errno = 0;
8016 - $http_code = 0;
8017 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8018 - $backoff_ms = array(0, 750, 2000);
8019 -
8020 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8021 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8022 - usleep($backoff_ms[$attempt] * 1000);
8023 - }
8024 -
8025 - $captured_status_code = 0;
8026 - $captured_body_pre_stream = '';
8027 - $full_response = '';
8028 - $stream_started = false;
8029 - $buffer = '';
8030 -
8031 - $ch = curl_init();
8032 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8033 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8034 - curl_setopt($ch, CURLOPT_POST, true);
8035 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8036 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8037 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8038 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8039 -
8040 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8041 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8042 - $captured_status_code = (int) $m[1];
8043 - }
8044 - return strlen($header);
8045 - });
8046 -
8047 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8048 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8049 - $captured_body_pre_stream .= $data;
8050 - return strlen($data);
8051 - }
8052 -
8053 - if (!$this->streaming_headers_sent) {
8054 - $this->setup_streaming_headers();
8055 - }
8056 -
8057 - if (!$stream_started && $testing_data !== null) {
8058 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8059 - flush();
8060 - $stream_started = true;
8061 - }
8062 - $buffer .= $data;
8063 - $lines = explode("\n", $buffer);
8064 - $buffer = array_pop($lines);
8065 - foreach ($lines as $line) {
8066 - if (trim($line) === '') { continue; }
8067 - if (strpos($line, 'data: ') !== 0) { continue; }
8068 - $json_str = substr($line, 6);
8069 - if (trim($json_str) === '[DONE]') {
8070 - echo "data: [DONE]\n\n";
8071 - flush();
8072 - continue;
8073 - }
8074 - $json = json_decode(trim($json_str), true);
8075 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8076 - $content = $json['choices'][0]['delta']['content'];
8077 - $full_response .= $content;
8078 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8079 - flush();
8080 - }
8081 - }
8082 - return strlen($data);
8083 - });
8084 -
8085 - $response = curl_exec($ch);
8086 - $errno = curl_errno($ch);
8087 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8088 - curl_close($ch);
8089 -
8090 - if (!$errno && $http_code === 200) {
8091 - break;
8092 - }
8093 -
8094 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8095 - $can_retry = !$this->streaming_headers_sent
8096 - && ($attempt + 1) < $max_attempts
8097 - && $is_transient;
8098 -
8099 - if (defined('WP_DEBUG') && WP_DEBUG) {
8100 - error_log(sprintf(
8101 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8102 - $attempt + 1, $max_attempts, $http_code, $errno,
8103 - $is_transient ? 'yes' : 'no',
8104 - $can_retry ? 'Retrying.' : 'Giving up.'
8105 - ));
8106 - }
8107 -
8108 - if (!$can_retry) {
8109 - break;
8110 - }
8111 - }
8112 -
8113 - if (!$errno && $http_code === 200) {
8114 - if (!empty($full_response) && !empty($session_id)) {
8115 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8116 - }
8117 - return true;
8118 - }
8119 -
8120 - return $this->mxchat_stream_emit_fallback(
8121 - 'openai',
8122 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8123 - $session_id,
8124 - $testing_data
8125 - );
8126 -
8127 - } catch (Exception $e) {
8128 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8129 - }
8130 -}
8131 -
8132 -/**
8133 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8134 - * Returns string content on success, array['error'=>...] on failure.
8135 - */
8136 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8137 - $cfg = $this->mxchat_resolve_custom_provider();
8138 - if (empty($cfg['base_url'])) {
8139 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8140 - }
8141 -
8142 - $bot_id = $this->get_current_bot_id(null);
8143 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8144 - if (!is_array($conversation_history)) {
8145 - $conversation_history = array();
8146 - }
8147 -
8148 - $messages = array(array(
8149 - 'role' => 'system',
8150 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8151 - ));
8152 - foreach ($conversation_history as $message) {
8153 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8154 - $role = $message['role'];
8155 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8156 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8157 - $messages[] = array('role' => $role, 'content' => $message['content']);
8158 - }
8159 - }
8160 -
8161 - $headers_assoc = array('Content-Type' => 'application/json');
8162 - if (!empty($cfg['api_key'])) {
8163 - if ($cfg['auth_scheme'] === 'api-key') {
8164 - $headers_assoc['api-key'] = $cfg['api_key'];
8165 - } else {
8166 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8167 - }
8168 - }
8169 -
8170 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8171 - 'headers' => $headers_assoc,
8172 - 'body' => wp_json_encode(array(
8173 - 'model' => $cfg['model'],
8174 - 'messages' => $messages,
8175 - )),
8176 - 'timeout' => 120,
8177 - ), 'openai');
8178 -
8179 - if (is_wp_error($response)) {
8180 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8181 - }
8182 - $code = (int) wp_remote_retrieve_response_code($response);
8183 - if ($code < 200 || $code >= 300) {
8184 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8185 - }
8186 - $body = json_decode(wp_remote_retrieve_body($response), true);
8187 - if (isset($body['choices'][0]['message']['content'])) {
8188 - return (string) $body['choices'][0]['message']['content'];
8189 - }
8190 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8191 -}
8192 -
8193 -/**
8194 - * Generate response using OpenAI Responses API with web search tool
8195 - * This uses the newer Responses API which supports web search functionality
8196 - */
8197 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8198 - try {
8199 - $bot_id = $this->get_current_bot_id($session_id);
8200 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8201 -
8202 - if (!is_array($conversation_history)) {
8203 - $conversation_history = array();
8204 - }
8205 -
8206 - // Build the input for Responses API
8207 - // The Responses API uses a different format - we need to construct the input properly
8208 - $input_parts = [];
8209 -
8210 - // Add system instructions as context
8211 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8212 -
8213 - // Build conversation as input items for Responses API
8214 - foreach ($conversation_history as $message) {
8215 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8216 - $role = $message['role'];
8217 - if ($role === 'bot' || $role === 'agent') {
8218 - $role = 'assistant';
8219 - }
8220 - if (!in_array($role, ['assistant', 'user'])) {
8221 - $role = 'user';
8222 - }
8223 - $input_parts[] = [
8224 - 'type' => 'message',
8225 - 'role' => $role,
8226 - 'content' => $message['content']
8227 - ];
8228 - }
8229 - }
8230 -
8231 - // Build request body for Responses API
8232 - $request_body = [
8233 - 'model' => $selected_model,
8234 - 'input' => $input_parts,
8235 - 'instructions' => $system_context,
8236 - 'stream' => $streaming
8237 - ];
8238 -
8239 - // Only add web search tool if web search is enabled in settings
8240 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8241 - if ($web_search_enabled) {
8242 - $request_body['tools'] = [
8243 - ['type' => 'web_search']
8244 - ];
8245 - }
8246 -
8247 - // Add reasoning effort for supported models
8248 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8249 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8250 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8251 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8252 - $request_body['reasoning'] = ['effort' => 'low'];
8253 - } elseif ($selected_model === 'gpt-5.5') {
8254 - $request_body['reasoning'] = ['effort' => 'low'];
8255 - } elseif ($selected_model === 'gpt-5.4') {
8256 - $request_body['reasoning'] = ['effort' => 'low'];
8257 - }
8258 - }
8259 -
8260 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8261 -
8262 - if ($streaming) {
8263 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8264 - } else {
8265 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8266 - }
8267 -
8268 - } catch (Exception $e) {
8269 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8270 - return [
8271 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8272 - 'error_code' => 'web_search_exception'
8273 - ];
8274 - }
8275 -}
8276 -
8277 -/**
8278 - * Handle non-streaming web search response
8279 - */
8280 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8281 - $request_body['stream'] = false;
8282 -
8283 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8284 - 'headers' => array(
8285 - 'Authorization' => 'Bearer ' . $api_key,
8286 - 'Content-Type' => 'application/json'
8287 - ),
8288 - 'body' => json_encode($request_body),
8289 - 'timeout' => 90
8290 - ), 'openai');
8291 -
8292 - if (is_wp_error($response)) {
8293 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8294 - return [
8295 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8296 - 'error_code' => 'web_search_connection_error'
8297 - ];
8298 - }
8299 -
8300 - $response_code = wp_remote_retrieve_response_code($response);
8301 - $response_body = wp_remote_retrieve_body($response);
8302 -
8303 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8304 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8305 -
8306 - if ($response_code !== 200) {
8307 - $error_data = json_decode($response_body, true);
8308 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8309 - return [
8310 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8311 - 'error_code' => 'web_search_api_error'
8312 - ];
8313 - }
8314 -
8315 - $result = json_decode($response_body, true);
8316 -
8317 - if (json_last_error() !== JSON_ERROR_NONE) {
8318 - return [
8319 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8320 - 'error_code' => 'web_search_json_error'
8321 - ];
8322 - }
8323 -
8324 - // Extract the response text and citations from Responses API format
8325 - $output_text = '';
8326 - $citations = [];
8327 -
8328 - if (isset($result['output'])) {
8329 - foreach ($result['output'] as $output_item) {
8330 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8331 - foreach ($output_item['content'] as $content_item) {
8332 - if ($content_item['type'] === 'output_text') {
8333 - $output_text .= $content_item['text'];
8334 -
8335 - // Extract citations/annotations
8336 - if (isset($content_item['annotations'])) {
8337 - foreach ($content_item['annotations'] as $annotation) {
8338 - if ($annotation['type'] === 'url_citation') {
8339 - $citations[] = [
8340 - 'url' => $annotation['url'],
8341 - 'title' => $annotation['title'] ?? ''
8342 - ];
8343 - }
8344 - }
8345 - }
8346 - }
8347 - }
8348 - }
8349 - }
8350 - }
8351 -
8352 - // If we have citations, append them to the response
8353 - if (!empty($citations)) {
8354 - $output_text .= "\n\n**Sources:**\n";
8355 - $seen_urls = [];
8356 - foreach ($citations as $citation) {
8357 - if (!in_array($citation['url'], $seen_urls)) {
8358 - $seen_urls[] = $citation['url'];
8359 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8360 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8361 - }
8362 - }
8363 - }
8364 -
8365 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8366 - // which includes rag_context for the "sources" link in transcripts.
8367 -
8368 - return $output_text;
8369 -}
8370 -
8371 -/**
8372 - * Handle streaming web search response using Responses API
8373 - */
8374 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8375 - $request_body['stream'] = true;
8376 -
8377 - // Check if we can stream
8378 - if (headers_sent() || !function_exists('curl_init')) {
8379 - // Fallback to non-streaming
8380 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8381 - }
8382 -
8383 - // Setup streaming headers
8384 - $this->setup_streaming_headers();
8385 -
8386 - $ch = curl_init();
8387 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8388 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8389 - curl_setopt($ch, CURLOPT_POST, true);
8390 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8391 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8392 - 'Content-Type: application/json',
8393 - 'Authorization: Bearer ' . $api_key
8394 - ));
8395 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8396 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8397 -
8398 - $full_response = '';
8399 - $stream_started = false;
8400 - $buffer = '';
8401 - $citations = [];
8402 -
8403 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
8404 - // Send testing data as first event if available
8405 - if (!$stream_started && $testing_data !== null) {
8406 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8407 - flush();
8408 - $stream_started = true;
8409 - }
8410 -
8411 - $buffer .= $data;
8412 - $lines = explode("\n", $buffer);
8413 - $buffer = array_pop($lines);
8414 -
8415 - foreach ($lines as $line) {
8416 - if (trim($line) === '') continue;
8417 - if (strpos($line, 'data: ') !== 0) continue;
8418 -
8419 - $json_str = substr($line, 6);
8420 -
8421 - if (trim($json_str) === '[DONE]') {
8422 - // Append citations if we have any
8423 - if (!empty($citations)) {
8424 - $citation_text = "\n\n**Sources:**\n";
8425 - $seen_urls = [];
8426 - foreach ($citations as $citation) {
8427 - if (!in_array($citation['url'], $seen_urls)) {
8428 - $seen_urls[] = $citation['url'];
8429 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8430 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8431 - }
8432 - }
8433 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
8434 - $full_response .= $citation_text;
8435 - flush();
8436 - }
8437 - echo "data: [DONE]\n\n";
8438 - flush();
8439 - continue;
8440 - }
8441 -
8442 - $json = json_decode(trim($json_str), true);
8443 - if (!$json) continue;
8444 -
8445 - // Handle Responses API streaming events
8446 - // The format is different from Chat Completions
8447 - if (isset($json['type'])) {
8448 - switch ($json['type']) {
8449 - case 'response.output_text.delta':
8450 - // Text content delta
8451 - if (isset($json['delta'])) {
8452 - $content = $json['delta'];
8453 - $full_response .= $content;
8454 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8455 - flush();
8456 - }
8457 - break;
8458 -
8459 - case 'response.output_item.done':
8460 - // Check for citations in completed items
8461 - if (isset($json['item']['content'])) {
8462 - foreach ($json['item']['content'] as $content_item) {
8463 - if (isset($content_item['annotations'])) {
8464 - foreach ($content_item['annotations'] as $annotation) {
8465 - if ($annotation['type'] === 'url_citation') {
8466 - $citations[] = [
8467 - 'url' => $annotation['url'],
8468 - 'title' => $annotation['title'] ?? ''
8469 - ];
8470 - }
8471 - }
8472 - }
8473 - }
8474 - }
8475 - break;
8476 - }
8477 - }
8478 - }
8479 -
8480 - return strlen($data);
8481 - });
8482 -
8483 - $response = curl_exec($ch);
8484 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8485 -
8486 - if (curl_errno($ch) || $http_code !== 200) {
8487 - $curl_error = curl_error($ch);
8488 - curl_close($ch);
8489 -
8490 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
8491 -
8492 - return $this->mxchat_stream_emit_fallback(
8493 - 'web_search',
8494 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
8495 - $session_id,
8496 - $testing_data
8497 - );
8498 - }
8499 -
8500 - curl_close($ch);
8501 -
8502 - // Save the complete response with RAG context so the "sources" link
8503 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
8504 - if (!empty($full_response) && !empty($session_id)) {
8505 - $rag_context_for_storage = null;
8506 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8507 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8508 -
8509 - if ($has_rag_data || $has_action_data) {
8510 - $rag_context_for_storage = [];
8511 -
8512 - if ($has_rag_data) {
8513 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8514 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8515 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8516 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8517 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8518 - }
8519 -
8520 - if ($has_action_data) {
8521 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8522 - }
8523 - }
8524 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8525 - }
8526 -
8527 - return true;
8528 -}
8529 -
8530 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8531 - try {
8532 - // Get bot ID from session or request
8533 - $bot_id = $this->get_current_bot_id($session_id);
8534 -
8535 - // Get system prompt instructions using centralized function
8536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8537 3843 // Ensure conversation_history is an array
8538 3844 if (!is_array($conversation_history)) {
8539 3845 $conversation_history = array();
8540 3846 }
@@ -8566,9 +3872,9 @@
8566 3872 'content' => $relevant_content
8567 3873 ];
8568 3874
8569 3875 // Prepare the request body with stream: true
8570 - $payload = [
3876 + $body = json_encode([
8571 3877 'model' => $selected_model,
8572 3878 'messages' => $conversation_history,
8573 3879 'max_tokens' => 1000,
8574 3880 'temperature' => 0.8,
@@ -8573,11 +3879,9 @@
8573 3879 'max_tokens' => 1000,
8574 3880 'temperature' => 0.8,
8575 3881 'system' => $system_prompt_instructions,
8576 3882 'stream' => true
8577 - ];
8578 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
8579 - $body = json_encode($payload);
3883 + ]);
8580 3884
8581 3885 // Check if we can actually stream (headers not sent, etc.)
8582 3886 if (headers_sent() || !function_exists('curl_init')) {
8583 3887 // Fallback to regular response with testing data
@@ -8588,13 +3892,8 @@
8588 3892 array_slice($conversation_history, 0, -1), // Remove the added content
8589 3893 $relevant_content
8590 3894 );
8591 3895
8592 - // Save bot response to transcript
8593 - if (!empty($regular_response) && !empty($session_id)) {
8594 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8595 - }
8596 -
8597 3896 // Return as JSON with testing data
8598 3897 $response_data = [
8599 3898 'text' => $regular_response,
8600 3899 'html' => '',
@@ -8613,196 +3912,187 @@
8613 3912 echo json_encode($response_data);
8614 3913 return true; // Indicate we handled the response
8615 3914 }
8616 3915
8617 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
3916 + // Use cURL for streaming support
3917 + $ch = curl_init();
3918 + curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
3919 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
3920 + curl_setopt($ch, CURLOPT_POST, true);
3921 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
3922 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
3923 + 'Content-Type: application/json',
3924 + 'x-api-key: ' . $claude_api_key,
3925 + 'anthropic-version: 2023-06-01'
3926 + ));
3927 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
3928 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
3929 +
3930 + // Add buffer control options for better streaming
3931 + curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // Smaller buffer for faster streaming
3932 + curl_setopt($ch, CURLOPT_NOPROGRESS, false);
8618 3933
8619 - $captured_status_code = 0;
8620 - $captured_body_pre_stream = '';
8621 - $full_response = '';
3934 + $full_response = ''; // Accumulate full response for saving
8622 3935 $stream_started = false;
8623 - $buffer = '';
8624 - $errno = 0;
8625 - $http_code = 0;
8626 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8627 - $backoff_ms = array(0, 750, 2000);
8628 3936
8629 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8630 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8631 - usleep($backoff_ms[$attempt] * 1000);
3937 + // Buffer control for real-time streaming
3938 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
3939 + // Flush any existing output buffers
3940 + if (ob_get_level()) {
3941 + ob_flush();
8632 3942 }
8633 -
8634 - $captured_status_code = 0;
8635 - $captured_body_pre_stream = '';
8636 - $full_response = '';
8637 - $stream_started = false;
8638 - $buffer = '';
8639 -
8640 - $ch = curl_init();
8641 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
8642 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8643 - curl_setopt($ch, CURLOPT_POST, true);
8644 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8645 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8646 - 'Content-Type: application/json',
8647 - 'x-api-key: ' . $claude_api_key,
8648 - 'anthropic-version: 2023-06-01'
8649 - ));
8650 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8651 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8652 -
8653 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8654 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8655 - $captured_status_code = (int) $m[1];
3943 +
3944 + // Send testing data as the first event if available
3945 + if (!$stream_started && $testing_data !== null) {
3946 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
3947 + flush();
3948 + if (function_exists('fastcgi_finish_request')) {
3949 + fastcgi_finish_request();
8656 3950 }
8657 - return strlen($header);
8658 - });
3951 + $stream_started = true;
3952 + //error_log("MxChat Testing: Sent testing data in Claude stream");
3953 + }
3954 +
3955 + // Process each chunk of data
3956 + $lines = explode("\n", $data);
8659 3957
8660 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8661 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8662 - $captured_body_pre_stream .= $data;
8663 - return strlen($data);
3958 + foreach ($lines as $line) {
3959 + if (trim($line) === '') {
3960 + continue;
8664 3961 }
8665 3962
8666 - if (!$this->streaming_headers_sent) {
8667 - $this->setup_streaming_headers();
3963 + // Claude uses event: and data: format
3964 + if (strpos($line, 'event: ') === 0) {
3965 + // Store the event type for the next data line
3966 + continue;
8668 3967 }
8669 3968
8670 - if (!$stream_started && $testing_data !== null) {
8671 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8672 - flush();
8673 - $stream_started = true;
8674 - }
3969 + if (strpos($line, 'data: ') === 0) {
3970 + $json_str = substr($line, 6); // Remove 'data: ' prefix
8675 3971
8676 - $buffer .= $data;
8677 - $lines = explode("\n", $buffer);
8678 - $buffer = array_pop($lines);
8679 -
8680 - foreach ($lines as $line) {
8681 - if (trim($line) === '') {
3972 + $json = json_decode($json_str, true);
3973 + if (json_last_error() !== JSON_ERROR_NONE) {
8682 3974 continue;
8683 3975 }
8684 3976
8685 - if (strpos($line, 'event: ') === 0) {
8686 - continue;
8687 - }
8688 -
8689 - if (strpos($line, 'data: ') === 0) {
8690 - $json_str = substr($line, 6);
8691 -
8692 - $json = json_decode(trim($json_str), true);
8693 - if (json_last_error() !== JSON_ERROR_NONE) {
8694 - continue;
8695 - }
8696 -
8697 - if (isset($json['type'])) {
8698 - switch ($json['type']) {
8699 - case 'content_block_delta':
8700 - if (isset($json['delta']['text'])) {
8701 - $content = $json['delta']['text'];
8702 - $full_response .= $content;
8703 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8704 - flush();
3977 + // Handle different event types
3978 + if (isset($json['type'])) {
3979 + switch ($json['type']) {
3980 + case 'content_block_delta':
3981 + if (isset($json['delta']['text'])) {
3982 + $content = $json['delta']['text'];
3983 + $full_response .= $content; // Accumulate
3984 + // Send as SSE format compatible with your frontend
3985 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
3986 + flush();
3987 + if (function_exists('fastcgi_finish_request')) {
3988 + fastcgi_finish_request();
8705 3989 }
8706 - break;
3990 + }
3991 + break;
8707 3992
8708 - case 'message_stop':
8709 - echo "data: [DONE]\n\n";
8710 - flush();
8711 - break;
3993 + case 'message_stop':
3994 + echo "data: [DONE]\n\n";
3995 + flush();
3996 + if (function_exists('fastcgi_finish_request')) {
3997 + fastcgi_finish_request();
3998 + }
3999 + break;
8712 4000
8713 - case 'error':
8714 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8715 - flush();
8716 - break;
8717 - }
4001 + case 'error':
4002 + echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
4003 + flush();
4004 + if (function_exists('fastcgi_finish_request')) {
4005 + fastcgi_finish_request();
4006 + }
4007 + break;
8718 4008 }
8719 4009 }
8720 4010 }
8721 -
8722 - return strlen($data);
8723 - });
8724 -
8725 - $response = curl_exec($ch);
8726 - $errno = curl_errno($ch);
8727 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8728 - curl_close($ch);
8729 -
8730 - if (!$errno && $http_code === 200) {
8731 - break;
8732 4011 }
8733 4012
8734 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
8735 - $can_retry = !$this->streaming_headers_sent
8736 - && ($attempt + 1) < $max_attempts
8737 - && $is_transient;
4013 + return strlen($data);
4014 + });
8738 4015
8739 - if (defined('WP_DEBUG') && WP_DEBUG) {
8740 - error_log(sprintf(
8741 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8742 - $attempt + 1, $max_attempts, $http_code, $errno,
8743 - $is_transient ? 'yes' : 'no',
8744 - $can_retry ? 'Retrying.' : 'Giving up.'
8745 - ));
8746 - }
4016 + $response = curl_exec($ch);
4017 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8747 4018
8748 - if (!$can_retry) {
8749 - break;
8750 - }
4019 + if (curl_errno($ch)) {
4020 + curl_close($ch);
4021 + throw new Exception('cURL Error: ' . curl_error($ch));
8751 4022 }
8752 4023
8753 - if ($errno || $http_code !== 200) {
8754 - return $this->mxchat_stream_emit_fallback(
8755 - 'anthropic',
8756 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content),
8757 - $session_id,
8758 - $testing_data
4024 + curl_close($ch);
4025 +
4026 + if ($http_code !== 200) {
4027 + // Fallback to regular response
4028 + //error_log("MxChat: Claude streaming failed with HTTP $http_code, falling back");
4029 + $regular_response = $this->mxchat_generate_response_claude(
4030 + $selected_model,
4031 + $claude_api_key,
4032 + array_slice($conversation_history, 0, -1), // Remove the added content
4033 + $relevant_content
8759 4034 );
4035 +
4036 + $response_data = [
4037 + 'text' => $regular_response,
4038 + 'html' => '',
4039 + 'session_id' => $session_id
4040 + ];
4041 +
4042 + if ($testing_data !== null) {
4043 + $response_data['testing_data'] = $testing_data;
4044 + //error_log("MxChat Testing: Added testing data to Claude error fallback");
4045 + }
4046 +
4047 + header('Content-Type: application/json');
4048 + echo json_encode($response_data);
4049 + return true;
8760 4050 }
8761 4051
8762 4052 // Save the complete response to maintain chat persistence
8763 4053 if (!empty($full_response) && !empty($session_id)) {
8764 - // Prepare RAG context for streaming response
8765 - $rag_context_for_storage = null;
8766 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8767 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8768 -
8769 - if ($has_rag_data || $has_action_data) {
8770 - $rag_context_for_storage = [];
8771 -
8772 - if ($has_rag_data) {
8773 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8774 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8775 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8776 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8777 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8778 - }
8779 -
8780 - if ($has_action_data) {
8781 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8782 - }
8783 - }
8784 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
4054 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8785 4055 }
8786 4056
8787 4057 return true; // Indicate streaming completed successfully
8788 4058
8789 4059 } catch (Exception $e) {
8790 - return $this->mxchat_stream_emit_fallback(
8791 - 'anthropic',
8792 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content),
8793 - $session_id,
8794 - $testing_data
4060 + //error_log("MxChat Claude streaming exception: " . $e->getMessage());
4061 +
4062 + // Fallback to regular response on exception
4063 + $regular_response = $this->mxchat_generate_response_claude(
4064 + $selected_model,
4065 + $claude_api_key,
4066 + $conversation_history,
4067 + $relevant_content
8795 4068 );
4069 +
4070 + $response_data = [
4071 + 'text' => $regular_response,
4072 + 'html' => '',
4073 + 'session_id' => $session_id
4074 + ];
4075 +
4076 + if ($testing_data !== null) {
4077 + $response_data['testing_data'] = $testing_data;
4078 + //error_log("MxChat Testing: Added testing data to Claude exception fallback");
4079 + }
4080 +
4081 + header('Content-Type: application/json');
4082 + echo json_encode($response_data);
4083 + return true;
8796 4084 }
8797 4085 }
8798 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4086 +
4087 +// 3. Update OpenAI streaming function similarly
4088 +private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8799 4089 try {
8800 - // Get bot ID from session or request
8801 - $bot_id = $this->get_current_bot_id($session_id);
4090 + // Enable implicit flushing for real-time streaming
4091 + ob_implicit_flush(true);
8802 4092
8803 - // Get system prompt instructions using centralized function
8804 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4093 + // Get system prompt instructions from options
4094 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8805 4095
8806 4096 // Ensure conversation_history is an array
8807 4097 if (!is_array($conversation_history)) {
8808 4098 $conversation_history = array();
@@ -8807,9 +4097,9 @@
8807 4097 if (!is_array($conversation_history)) {
8808 4098 $conversation_history = array();
8809 4099 }
8810 4100
8811 - // Format conversation history for X.AI (same as OpenAI format)
4101 + // Format conversation history for OpenAI
8812 4102 $formatted_conversation = array();
8813 4103
8814 4104 $formatted_conversation[] = array(
8815 4105 'role' => 'system',
@@ -8834,21 +4124,16 @@
8834 4124
8835 4125 // Check if we can actually stream
8836 4126 if (headers_sent() || !function_exists('curl_init')) {
8837 4127 // Fallback to regular response with testing data
8838 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
8839 - $regular_response = $this->mxchat_generate_response_xai(
4128 + //error_log("MxChat: OpenAI streaming not possible, falling back to regular response");
4129 + $regular_response = $this->mxchat_generate_response_openai(
8840 4130 $selected_model,
8841 - $xai_api_key,
4131 + $api_key,
8842 4132 $conversation_history,
8843 4133 $relevant_content
8844 4134 );
8845 4135
8846 - // Save bot response to transcript
8847 - if (!empty($regular_response) && !empty($session_id)) {
8848 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8849 - }
8850 -
8851 4136 $response_data = [
8852 4137 'text' => $regular_response,
8853 4138 'html' => '',
8854 4139 'session_id' => $session_id
@@ -8855,9 +4140,9 @@
8855 4140 ];
8856 4141
8857 4142 if ($testing_data !== null) {
8858 4143 $response_data['testing_data'] = $testing_data;
8859 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
4144 + //error_log("MxChat Testing: Added testing data to OpenAI fallback response");
8860 4145 }
8861 4146
8862 4147 header('Content-Type: application/json');
8863 4148 echo json_encode($response_data);
@@ -8871,224 +4156,96 @@
8871 4156 'temperature' => 0.8,
8872 4157 'stream' => true
8873 4158 ]);
8874 4159
8875 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8876 -
8877 - $captured_status_code = 0;
8878 - $captured_body_pre_stream = '';
8879 - $full_response = '';
4160 + // Use cURL for streaming support
4161 + $ch = curl_init();
4162 + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
4163 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4164 + curl_setopt($ch, CURLOPT_POST, true);
4165 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4166 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4167 + 'Content-Type: application/json',
4168 + 'Authorization: Bearer ' . $api_key
4169 + ));
4170 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4171 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4172 +
4173 + // Add buffer control options for better streaming
4174 + curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // Smaller buffer for faster streaming
4175 + curl_setopt($ch, CURLOPT_NOPROGRESS, false);
4176 +
4177 + $full_response = ''; // Accumulate full response for saving
8880 4178 $stream_started = false;
8881 - $buffer = '';
8882 - $errno = 0;
8883 - $http_code = 0;
8884 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8885 - $backoff_ms = array(0, 750, 2000);
8886 -
8887 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8888 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8889 - usleep($backoff_ms[$attempt] * 1000);
4179 +
4180 + // Buffer control for real-time streaming
4181 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4182 + // Flush any existing output buffers
4183 + if (ob_get_level()) {
4184 + ob_flush();
8890 4185 }
8891 -
8892 - $captured_status_code = 0;
8893 - $captured_body_pre_stream = '';
8894 - $full_response = '';
8895 - $stream_started = false;
8896 - $buffer = '';
8897 -
8898 - $ch = curl_init();
8899 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
8900 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8901 - curl_setopt($ch, CURLOPT_POST, true);
8902 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8903 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8904 - 'Content-Type: application/json',
8905 - 'Authorization: Bearer ' . $xai_api_key
8906 - ));
8907 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8908 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8909 -
8910 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8911 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8912 - $captured_status_code = (int) $m[1];
4186 +
4187 + // Send testing data as the first event if available
4188 + if (!$stream_started && $testing_data !== null) {
4189 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4190 + flush();
4191 + if (function_exists('fastcgi_finish_request')) {
4192 + fastcgi_finish_request();
8913 4193 }
8914 - return strlen($header);
8915 - });
8916 -
8917 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8918 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8919 - $captured_body_pre_stream .= $data;
8920 - return strlen($data);
4194 + $stream_started = true;
4195 + //error_log("MxChat Testing: Sent testing data in OpenAI stream");
4196 + }
4197 +
4198 + // Process each chunk of data
4199 + $lines = explode("\n", $data);
4200 +
4201 + foreach ($lines as $line) {
4202 + if (trim($line) === '' || strpos($line, 'data: ') !== 0) {
4203 + continue;
8921 4204 }
8922 -
8923 - if (!$this->streaming_headers_sent) {
8924 - $this->setup_streaming_headers();
4205 +
4206 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4207 +
4208 + if ($json_str === '[DONE]') {
4209 + echo "data: [DONE]\n\n";
4210 + flush();
4211 + if (function_exists('fastcgi_finish_request')) {
4212 + fastcgi_finish_request();
4213 + }
4214 + continue;
8925 4215 }
8926 -
8927 - if (!$stream_started && $testing_data !== null) {
8928 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4216 +
4217 + $json = json_decode($json_str, true);
4218 + if (isset($json['choices'][0]['delta']['content'])) {
4219 + $content = $json['choices'][0]['delta']['content'];
4220 + $full_response .= $content; // Accumulate
4221 + // Send as SSE format
4222 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
8929 4223 flush();
8930 - $stream_started = true;
8931 - }
8932 -
8933 - $buffer .= $data;
8934 - $lines = explode("\n", $buffer);
8935 - $buffer = array_pop($lines);
8936 -
8937 - foreach ($lines as $line) {
8938 - if (trim($line) === '') {
8939 - continue;
4224 + if (function_exists('fastcgi_finish_request')) {
4225 + fastcgi_finish_request();
8940 4226 }
8941 - if (strpos($line, 'data: ') !== 0) {
8942 - continue;
8943 - }
8944 -
8945 - $json_str = substr($line, 6);
8946 -
8947 - if (trim($json_str) === '[DONE]') {
8948 - echo "data: [DONE]\n\n";
8949 - flush();
8950 - continue;
8951 - }
8952 -
8953 - $json = json_decode(trim($json_str), true);
8954 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8955 - $content = $json['choices'][0]['delta']['content'];
8956 - $full_response .= $content;
8957 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8958 - flush();
8959 - }
8960 4227 }
8961 -
8962 - return strlen($data);
8963 - });
8964 -
8965 - $response = curl_exec($ch);
8966 - $errno = curl_errno($ch);
8967 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8968 - curl_close($ch);
8969 -
8970 - if (!$errno && $http_code === 200) {
8971 - break;
8972 4228 }
8973 -
8974 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
8975 - $can_retry = !$this->streaming_headers_sent
8976 - && ($attempt + 1) < $max_attempts
8977 - && $is_transient;
8978 -
8979 - if (defined('WP_DEBUG') && WP_DEBUG) {
8980 - error_log(sprintf(
8981 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8982 - $attempt + 1, $max_attempts, $http_code, $errno,
8983 - $is_transient ? 'yes' : 'no',
8984 - $can_retry ? 'Retrying.' : 'Giving up.'
8985 - ));
8986 - }
8987 -
8988 - if (!$can_retry) {
8989 - break;
8990 - }
8991 - }
8992 -
8993 - if ($errno || $http_code !== 200) {
8994 - return $this->mxchat_stream_emit_fallback(
8995 - 'xai',
8996 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
8997 - $session_id,
8998 - $testing_data
8999 - );
9000 - }
9001 -
9002 - // Save the complete response to maintain chat persistence
9003 - if (!empty($full_response) && !empty($session_id)) {
9004 - // Prepare RAG context for streaming response
9005 - $rag_context_for_storage = null;
9006 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9007 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9008 -
9009 - if ($has_rag_data || $has_action_data) {
9010 - $rag_context_for_storage = [];
9011 -
9012 - if ($has_rag_data) {
9013 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9014 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9015 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9016 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9017 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9018 - }
9019 -
9020 - if ($has_action_data) {
9021 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9022 - }
9023 - }
9024 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9025 - }
9026 -
9027 - return true; // Indicate streaming completed successfully
9028 -
9029 - } catch (Exception $e) {
9030 - return $this->mxchat_stream_emit_fallback(
9031 - 'xai',
9032 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9033 - $session_id,
9034 - $testing_data
9035 - );
9036 - }
9037 -}
9038 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9039 - try {
9040 - // Get bot ID from session or request
9041 - $bot_id = $this->get_current_bot_id($session_id);
4229 +
4230 + return strlen($data);
4231 + });
9042 4232
9043 - // Get system prompt instructions using centralized function
9044 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4233 + $response = curl_exec($ch);
4234 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9045 4235
9046 - // Ensure conversation_history is an array
9047 - if (!is_array($conversation_history)) {
9048 - $conversation_history = array();
9049 - }
9050 -
9051 - // Format conversation history for DeepSeek
9052 - $formatted_conversation = array();
9053 -
9054 - $formatted_conversation[] = array(
9055 - 'role' => 'system',
9056 - 'content' => $system_prompt_instructions . " " . $relevant_content
9057 - );
9058 -
9059 - foreach ($conversation_history as $message) {
9060 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9061 - $role = $message['role'];
9062 - if ($role === 'bot' || $role === 'agent') {
9063 - $role = 'assistant';
9064 - }
9065 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9066 - $role = 'user';
9067 - }
9068 - $formatted_conversation[] = array(
9069 - 'role' => $role,
9070 - 'content' => $message['content']
9071 - );
9072 - }
9073 - }
9074 -
9075 - // Check if we can actually stream
9076 - if (headers_sent() || !function_exists('curl_init')) {
9077 - // Fallback to regular response with testing data
9078 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9079 - $regular_response = $this->mxchat_generate_response_deepseek(
4236 + if (curl_errno($ch) || $http_code !== 200) {
4237 + curl_close($ch);
4238 +
4239 + // Fallback to regular response
4240 + //error_log("MxChat: OpenAI streaming failed, falling back");
4241 + $regular_response = $this->mxchat_generate_response_openai(
9080 4242 $selected_model,
9081 - $deepseek_api_key,
4243 + $api_key,
9082 4244 $conversation_history,
9083 4245 $relevant_content
9084 4246 );
9085 4247
9086 - // Save bot response to transcript
9087 - if (!empty($regular_response) && !empty($session_id)) {
9088 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9089 - }
9090 -
9091 4248 $response_data = [
9092 4249 'text' => $regular_response,
9093 4250 'html' => '',
9094 4251 'session_id' => $session_id
@@ -9095,9 +4252,9 @@
9095 4252 ];
9096 4253
9097 4254 if ($testing_data !== null) {
9098 4255 $response_data['testing_data'] = $testing_data;
9099 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
4256 + //error_log("MxChat Testing: Added testing data to OpenAI error fallback");
9100 4257 }
9101 4258
9102 4259 header('Content-Type: application/json');
9103 4260 echo json_encode($response_data);
@@ -9102,293 +4259,50 @@
9102 4259 header('Content-Type: application/json');
9103 4260 echo json_encode($response_data);
9104 4261 return true;
9105 4262 }
9106 -
9107 - // Prepare the request body with stream: true
9108 - $body = json_encode([
9109 - 'model' => $selected_model,
9110 - 'messages' => $formatted_conversation,
9111 - 'temperature' => 0.8,
9112 - 'stream' => true
9113 - ]);
9114 -
9115 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9116 -
9117 - $captured_status_code = 0;
9118 - $captured_body_pre_stream = '';
9119 - $full_response = '';
9120 - $stream_started = false;
9121 - $buffer = '';
9122 - $errno = 0;
9123 - $http_code = 0;
9124 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9125 - $backoff_ms = array(0, 750, 2000);
9126 -
9127 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9128 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9129 - usleep($backoff_ms[$attempt] * 1000);
9130 - }
9131 -
9132 - $captured_status_code = 0;
9133 - $captured_body_pre_stream = '';
9134 - $full_response = '';
9135 - $stream_started = false;
9136 - $buffer = '';
9137 -
9138 - $ch = curl_init();
9139 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9140 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9141 - curl_setopt($ch, CURLOPT_POST, true);
9142 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9143 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9144 - 'Content-Type: application/json',
9145 - 'Authorization: Bearer ' . $deepseek_api_key
9146 - ));
9147 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9148 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9149 -
9150 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9151 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9152 - $captured_status_code = (int) $m[1];
9153 - }
9154 - return strlen($header);
9155 - });
9156 -
9157 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9158 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9159 - $captured_body_pre_stream .= $data;
9160 - return strlen($data);
9161 - }
9162 -
9163 - if (!$this->streaming_headers_sent) {
9164 - $this->setup_streaming_headers();
9165 - }
9166 -
9167 - if (!$stream_started && $testing_data !== null) {
9168 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9169 - flush();
9170 - $stream_started = true;
9171 - }
9172 -
9173 - $buffer .= $data;
9174 - $lines = explode("\n", $buffer);
9175 - $buffer = array_pop($lines);
9176 -
9177 - foreach ($lines as $line) {
9178 - if (trim($line) === '') {
9179 - continue;
9180 - }
9181 - if (strpos($line, 'data: ') !== 0) {
9182 - continue;
9183 - }
9184 -
9185 - $json_str = substr($line, 6);
9186 -
9187 - if (trim($json_str) === '[DONE]') {
9188 - echo "data: [DONE]\n\n";
9189 - flush();
9190 - continue;
9191 - }
9192 -
9193 - $json = json_decode(trim($json_str), true);
9194 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9195 - $content = $json['choices'][0]['delta']['content'];
9196 - $full_response .= $content;
9197 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9198 - flush();
9199 - }
9200 - }
9201 -
9202 - return strlen($data);
9203 - });
9204 -
9205 - $response = curl_exec($ch);
9206 - $errno = curl_errno($ch);
9207 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9208 - curl_close($ch);
9209 -
9210 - if (!$errno && $http_code === 200) {
9211 - break;
9212 - }
9213 -
9214 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9215 - $can_retry = !$this->streaming_headers_sent
9216 - && ($attempt + 1) < $max_attempts
9217 - && $is_transient;
9218 -
9219 - if (defined('WP_DEBUG') && WP_DEBUG) {
9220 - error_log(sprintf(
9221 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9222 - $attempt + 1, $max_attempts, $http_code, $errno,
9223 - $is_transient ? 'yes' : 'no',
9224 - $can_retry ? 'Retrying.' : 'Giving up.'
9225 - ));
9226 - }
9227 -
9228 - if (!$can_retry) {
9229 - break;
9230 - }
9231 - }
9232 -
9233 - if ($errno || $http_code !== 200) {
9234 - return $this->mxchat_stream_emit_fallback(
9235 - 'openai',
9236 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9237 - $session_id,
9238 - $testing_data
9239 - );
9240 - }
9241 -
4263 +
4264 + curl_close($ch);
4265 +
9242 4266 // Save the complete response to maintain chat persistence
9243 4267 if (!empty($full_response) && !empty($session_id)) {
9244 - // Prepare RAG context for streaming response
9245 - $rag_context_for_storage = null;
9246 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9247 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9248 -
9249 - if ($has_rag_data || $has_action_data) {
9250 - $rag_context_for_storage = [];
9251 -
9252 - if ($has_rag_data) {
9253 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9254 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9255 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9256 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9257 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9258 - }
9259 -
9260 - if ($has_action_data) {
9261 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9262 - }
9263 - }
9264 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
4268 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9265 4269 }
9266 -
4270 +
9267 4271 return true; // Indicate streaming completed successfully
9268 -
4272 +
9269 4273 } catch (Exception $e) {
9270 - return $this->mxchat_stream_emit_fallback(
9271 - 'openai',
9272 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9273 - $session_id,
9274 - $testing_data
4274 + //error_log("MxChat OpenAI streaming exception: " . $e->getMessage());
4275 +
4276 + // Fallback to regular response
4277 + $regular_response = $this->mxchat_generate_response_openai(
4278 + $selected_model,
4279 + $api_key,
4280 + $conversation_history,
4281 + $relevant_content
9275 4282 );
9276 - }
9277 -}
9278 -
9279 -
9280 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content) {
9281 - try {
9282 - if (!is_array($conversation_history)) {
9283 - $conversation_history = array();
9284 - }
9285 -
9286 - $bot_id = $this->get_current_bot_id('');
9287 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9288 4283
9289 - $formatted_conversation = array();
9290 -
9291 - $formatted_conversation[] = array(
9292 - 'role' => 'system',
9293 - 'content' => $system_prompt_instructions . " " . $relevant_content
9294 - );
9295 -
9296 - foreach ($conversation_history as $message) {
9297 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9298 - $role = $message['role'];
9299 -
9300 - if ($role === 'bot' || $role === 'agent') {
9301 - $role = 'assistant';
9302 - }
9303 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9304 - $role = 'user';
9305 - }
9306 -
9307 - $formatted_conversation[] = array(
9308 - 'role' => $role,
9309 - 'content' => $message['content']
9310 - );
9311 - }
9312 - }
9313 -
9314 - $body = json_encode([
9315 - 'model' => $selected_model,
9316 - 'messages' => $formatted_conversation,
9317 - 'temperature' => 1,
9318 - ]);
9319 -
9320 - $args = [
9321 - 'body' => $body,
9322 - 'headers' => [
9323 - 'Content-Type' => 'application/json',
9324 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9325 - 'HTTP-Referer' => home_url(),
9326 - 'X-Title' => get_bloginfo('name'),
9327 - ],
9328 - 'timeout' => 60,
9329 - 'redirection' => 5,
9330 - 'blocking' => true,
9331 - 'httpversion' => '1.0',
9332 - 'sslverify' => true,
4284 + $response_data = [
4285 + 'text' => $regular_response,
4286 + 'html' => '',
4287 + 'session_id' => $session_id
9333 4288 ];
9334 -
9335 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9336 -
9337 - if (is_wp_error($response)) {
9338 - $error_message = $response->get_error_message();
9339 - return [
9340 - 'error' => esc_html__('Connection error when contacting OpenRouter: ', 'mxchat') . esc_html($error_message),
9341 - 'error_code' => 'openrouter_connection_error',
9342 - 'provider' => 'openrouter'
9343 - ];
4289 +
4290 + if ($testing_data !== null) {
4291 + $response_data['testing_data'] = $testing_data;
4292 + //error_log("MxChat Testing: Added testing data to OpenAI exception fallback");
9344 4293 }
9345 -
9346 - $status_code = wp_remote_retrieve_response_code($response);
9347 - if ($status_code !== 200) {
9348 - $response_body = wp_remote_retrieve_body($response);
9349 - $decoded_response = json_decode($response_body, true);
9350 -
9351 - $error_message = isset($decoded_response['error']['message'])
9352 - ? $decoded_response['error']['message']
9353 - : 'HTTP Error ' . $status_code;
9354 -
9355 - return [
9356 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9357 - 'error_code' => 'openrouter_api_error',
9358 - 'provider' => 'openrouter',
9359 - 'status_code' => $status_code
9360 - ];
9361 - }
9362 -
9363 - $response_body = wp_remote_retrieve_body($response);
9364 - $decoded_response = json_decode($response_body, true);
9365 -
9366 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9367 - return trim($decoded_response['choices'][0]['message']['content']);
9368 - } else {
9369 - return [
9370 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9371 - 'error_code' => 'openrouter_response_format_error',
9372 - 'provider' => 'openrouter'
9373 - ];
9374 - }
9375 - } catch (Exception $e) {
9376 - return [
9377 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9378 - 'error_code' => 'openrouter_exception',
9379 - 'provider' => 'openrouter'
9380 - ];
4294 +
4295 + header('Content-Type: application/json');
4296 + echo json_encode($response_data);
4297 + return true;
9381 4298 }
9382 4299 }
4300 +
9383 4301 private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
9384 -
9385 - // Get bot ID from session or request
9386 - $bot_id = $this->get_current_bot_id($session_id);
9387 -
9388 - // Get system prompt instructions using centralized function
9389 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9390 -
4302 + // Get system prompt instructions from options
4303 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4304 +
9391 4305 // Clean and validate conversation history
9392 4306 foreach ($conversation_history as &$message) {
9393 4307 // Convert bot and agent roles to assistant
9394 4308 if ($message['role'] === 'bot' || $message['role'] === 'agent') {
@@ -9415,17 +4329,15 @@
9415 4329 'content' => $relevant_content
9416 4330 ];
9417 4331
9418 4332 // Build request body
9419 - $payload = [
4333 + $body = json_encode([
9420 4334 'model' => $selected_model,
9421 4335 'max_tokens' => 1000,
9422 4336 'temperature' => 0.8,
9423 4337 'messages' => $conversation_history,
9424 4338 'system' => $system_prompt_instructions
9425 - ];
9426 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9427 - $body = json_encode($payload);
4339 + ]);
9428 4340
9429 4341 // Set up API request
9430 4342 $args = [
9431 4343 'body' => $body,
@@ -9441,9 +4353,9 @@
9441 4353 'sslverify' => true,
9442 4354 ];
9443 4355
9444 4356 // Make API request
9445 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
4357 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
9446 4358
9447 4359 // Check for WordPress errors
9448 4360 if (is_wp_error($response)) {
9449 4361 //error_log("Claude API request error: " . $response->get_error_message());
@@ -9473,17 +4385,14 @@
9473 4385 //error_log("Claude API JSON decode error: " . json_last_error_msg());
9474 4386 return "Sorry, there was an error processing the API response.";
9475 4387 }
9476 4388
9477 - // Extract and validate response content. claude-fable-5 prepends a
9478 - // thinking block to content even with no thinking param — take the first
9479 - // TEXT block rather than content[0].
9480 - if (isset($response_body['content']) && is_array($response_body['content'])) {
9481 - foreach ($response_body['content'] as $block) {
9482 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
9483 - return trim($block['text']);
9484 - }
9485 - }
4389 + // Extract and validate response content
4390 + if (isset($response_body['content']) &&
4391 + is_array($response_body['content']) &&
4392 + !empty($response_body['content']) &&
4393 + isset($response_body['content'][0]['text'])) {
4394 + return trim($response_body['content'][0]['text']);
9486 4395 }
9487 4396
9488 4397 // Log unexpected response format
9489 4398 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
@@ -9495,14 +4404,11 @@
9495 4404 if (!is_array($conversation_history)) {
9496 4405 $conversation_history = array();
9497 4406 }
9498 4407
9499 - // Get bot ID from session or request
9500 - $bot_id = $this->get_current_bot_id('');
9501 -
9502 - // Get system prompt instructions using centralized function
9503 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9504 -
4408 + // Get system prompt instructions from options
4409 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4410 +
9505 4411 // Create a new array for the formatted conversation
9506 4412 $formatted_conversation = array();
9507 4413
9508 4414 // Add system message first
@@ -9530,44 +4436,15 @@
9530 4436 );
9531 4437 }
9532 4438 }
9533 4439
9534 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
9535 - $is_gpt5_model = (
9536 - strpos($selected_model, 'gpt-5') === 0 ||
9537 - $selected_model === 'gpt-5.2' ||
9538 - $selected_model === 'gpt-5.1-2025-11-13' ||
9539 - $selected_model === 'gpt-5' ||
9540 - $selected_model === 'gpt-5-mini' ||
9541 - $selected_model === 'gpt-5-nano'
9542 - );
9543 -
9544 - // Build request body with optimal settings for fast responses
9545 - $request_body = [
4440 + $body = json_encode([
9546 4441 'model' => $selected_model,
9547 4442 'messages' => $formatted_conversation,
9548 - 'temperature' => 1,
4443 + 'temperature' => 0.8,
9549 4444 'stream' => false
9550 - ];
4445 + ]);
9551 4446
9552 - // Add reasoning_effort only for GPT-5 models that support it
9553 - // These chat models don't support reasoning_effort parameter
9554 - $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');
9555 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
9556 - // GPT-5.1 uses 'low' instead of 'minimal'
9557 - if ($selected_model === 'gpt-5.1-2025-11-13') {
9558 - $request_body['reasoning_effort'] = 'low';
9559 - } elseif ($selected_model === 'gpt-5.5') {
9560 - $request_body['reasoning_effort'] = 'none';
9561 - } elseif ($selected_model === 'gpt-5.4') {
9562 - $request_body['reasoning_effort'] = 'none';
9563 - } else {
9564 - $request_body['reasoning_effort'] = 'minimal';
9565 - }
9566 - }
9567 -
9568 - $body = json_encode($request_body);
9569 -
9570 4447 $args = [
9571 4448 'body' => $body,
9572 4449 'headers' => [
9573 4450 'Content-Type' => 'application/json',
@@ -9579,12 +4456,13 @@
9579 4456 'httpversion' => '1.0',
9580 4457 'sslverify' => true,
9581 4458 ];
9582 4459
9583 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
4460 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
9584 4461
9585 4462 if (is_wp_error($response)) {
9586 4463 $error_message = $response->get_error_message();
4464 + //error_log('OpenAI API Error: ' . $error_message);
9587 4465 return [
9588 4466 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9589 4467 'error_code' => 'openai_connection_error',
9590 4468 'provider' => 'openai'
@@ -9603,8 +4481,10 @@
9603 4481 $error_type = isset($decoded_response['error']['type'])
9604 4482 ? $decoded_response['error']['type']
9605 4483 : 'unknown';
9606 4484
4485 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
4486 +
9607 4487 // Handle specific error types
9608 4488 switch ($error_type) {
9609 4489 case 'invalid_request_error':
9610 4490 if (strpos($error_message, 'API key') !== false) {
@@ -9652,8 +4532,9 @@
9652 4532
9653 4533 if (isset($decoded_response['choices'][0]['message']['content'])) {
9654 4534 return trim($decoded_response['choices'][0]['message']['content']);
9655 4535 } else {
4536 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
9656 4537 return [
9657 4538 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9658 4539 'error_code' => 'openai_response_format_error',
9659 4540 'provider' => 'openai'
@@ -9659,8 +4540,9 @@
9659 4540 'provider' => 'openai'
9660 4541 ];
9661 4542 }
9662 4543 } catch (Exception $e) {
4544 + //error_log('OpenAI Exception: ' . $e->getMessage());
9663 4545 return [
9664 4546 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9665 4547 'error_code' => 'openai_exception',
9666 4548 'provider' => 'openai'
@@ -9666,17 +4548,13 @@
9666 4548 'provider' => 'openai'
9667 4549 ];
9668 4550 }
9669 4551 }
9670 -
9671 4552 private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9672 4553 try {
9673 - // Get bot ID from session or request
9674 - $bot_id = $this->get_current_bot_id($session_id);
9675 -
9676 - // Get system prompt instructions using centralized function
9677 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9678 -
4554 + // Get system prompt instructions from options
4555 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4556 +
9679 4557 // Add system prompt to relevant content
9680 4558 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9681 4559
9682 4560 // Prepend system instructions to the conversation history
@@ -9725,9 +4603,9 @@
9725 4603 'sslverify' => true,
9726 4604 ];
9727 4605
9728 4606 // Make the API request
9729 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
4607 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
9730 4608
9731 4609 // Process the response
9732 4610 if (is_wp_error($response)) {
9733 4611 $error_message = $response->get_error_message();
@@ -9858,11 +4736,201 @@
9858 4736 'error_code' => 'xai_exception',
9859 4737 'provider' => 'xai'
9860 4738 ];
9861 4739 }
4740 +}
4741 +private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4742 + try {
4743 + // Get system prompt instructions from options
4744 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4745 +
4746 + // Ensure conversation_history is an array
4747 + if (!is_array($conversation_history)) {
4748 + $conversation_history = array();
4749 + }
9862 4750
4751 + // Format conversation history for X.AI (same as OpenAI format)
4752 + $formatted_conversation = array();
9863 4753
4754 + $formatted_conversation[] = array(
4755 + 'role' => 'system',
4756 + 'content' => $system_prompt_instructions . " " . $relevant_content
4757 + );
4758 +
4759 + foreach ($conversation_history as $message) {
4760 + if (is_array($message) && isset($message['role']) && isset($message['content'])) {
4761 + $role = $message['role'];
4762 + if ($role === 'bot' || $role === 'agent') {
4763 + $role = 'assistant';
4764 + }
4765 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
4766 + $role = 'user';
4767 + }
4768 + $formatted_conversation[] = array(
4769 + 'role' => $role,
4770 + 'content' => $message['content']
4771 + );
4772 + }
4773 + }
4774 +
4775 + // Check if we can actually stream
4776 + if (headers_sent() || !function_exists('curl_init')) {
4777 + // Fallback to regular response with testing data
4778 + //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
4779 + $regular_response = $this->mxchat_generate_response_xai(
4780 + $selected_model,
4781 + $xai_api_key,
4782 + $conversation_history,
4783 + $relevant_content
4784 + );
4785 +
4786 + $response_data = [
4787 + 'text' => $regular_response,
4788 + 'html' => '',
4789 + 'session_id' => $session_id
4790 + ];
4791 +
4792 + if ($testing_data !== null) {
4793 + $response_data['testing_data'] = $testing_data;
4794 + //error_log("MxChat Testing: Added testing data to X.AI fallback response");
4795 + }
4796 +
4797 + header('Content-Type: application/json');
4798 + echo json_encode($response_data);
4799 + return true;
4800 + }
4801 +
4802 + // Prepare the request body with stream: true
4803 + $body = json_encode([
4804 + 'model' => $selected_model,
4805 + 'messages' => $formatted_conversation,
4806 + 'temperature' => 0.8,
4807 + 'stream' => true
4808 + ]);
4809 +
4810 + // Use cURL for streaming support
4811 + $ch = curl_init();
4812 + curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
4813 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4814 + curl_setopt($ch, CURLOPT_POST, true);
4815 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4816 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4817 + 'Content-Type: application/json',
4818 + 'Authorization: Bearer ' . $xai_api_key
4819 + ));
4820 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4821 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4822 +
4823 + $full_response = ''; // Accumulate full response for saving
4824 + $stream_started = false;
4825 +
4826 + // Buffer control for real-time streaming
4827 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4828 + // Send testing data as the first event if available
4829 + if (!$stream_started && $testing_data !== null) {
4830 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4831 + flush();
4832 + $stream_started = true;
4833 + //error_log("MxChat Testing: Sent testing data in X.AI stream");
4834 + }
4835 +
4836 + // Process each chunk of data
4837 + $lines = explode("\n", $data);
4838 +
4839 + foreach ($lines as $line) {
4840 + if (trim($line) === '' || strpos($line, 'data: ') !== 0) {
4841 + continue;
4842 + }
4843 +
4844 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4845 +
4846 + if ($json_str === '[DONE]') {
4847 + echo "data: [DONE]\n\n";
4848 + flush();
4849 + continue;
4850 + }
4851 +
4852 + $json = json_decode($json_str, true);
4853 + if (isset($json['choices'][0]['delta']['content'])) {
4854 + $content = $json['choices'][0]['delta']['content'];
4855 + $full_response .= $content; // Accumulate
4856 + // Send as SSE format
4857 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
4858 + flush();
4859 + }
4860 + }
4861 +
4862 + return strlen($data);
4863 + });
4864 +
4865 + $response = curl_exec($ch);
4866 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
4867 +
4868 + if (curl_errno($ch) || $http_code !== 200) {
4869 + curl_close($ch);
4870 +
4871 + // Fallback to regular response
4872 + //error_log("MxChat: X.AI streaming failed, falling back");
4873 + $regular_response = $this->mxchat_generate_response_xai(
4874 + $selected_model,
4875 + $xai_api_key,
4876 + $conversation_history,
4877 + $relevant_content
4878 + );
4879 +
4880 + $response_data = [
4881 + 'text' => $regular_response,
4882 + 'html' => '',
4883 + 'session_id' => $session_id
4884 + ];
4885 +
4886 + if ($testing_data !== null) {
4887 + $response_data['testing_data'] = $testing_data;
4888 + //error_log("MxChat Testing: Added testing data to X.AI error fallback");
4889 + }
4890 +
4891 + header('Content-Type: application/json');
4892 + echo json_encode($response_data);
4893 + return true;
4894 + }
4895 +
4896 + curl_close($ch);
4897 +
4898 + // Save the complete response to maintain chat persistence
4899 + if (!empty($full_response) && !empty($session_id)) {
4900 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
4901 + }
4902 +
4903 + return true; // Indicate streaming completed successfully
4904 +
4905 + } catch (Exception $e) {
4906 + //error_log("MxChat X.AI streaming exception: " . $e->getMessage());
4907 +
4908 + // Fallback to regular response
4909 + $regular_response = $this->mxchat_generate_response_xai(
4910 + $selected_model,
4911 + $xai_api_key,
4912 + $conversation_history,
4913 + $relevant_content
4914 + );
4915 +
4916 + $response_data = [
4917 + 'text' => $regular_response,
4918 + 'html' => '',
4919 + 'session_id' => $session_id
4920 + ];
4921 +
4922 + if ($testing_data !== null) {
4923 + $response_data['testing_data'] = $testing_data;
4924 + //error_log("MxChat Testing: Added testing data to X.AI exception fallback");
4925 + }
4926 +
4927 + header('Content-Type: application/json');
4928 + echo json_encode($response_data);
4929 + return true;
4930 + }
9864 4931 }
4932 +
9865 4933 private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
9866 4934 try {
9867 4935 // Ensure conversation_history is an array
9868 4936 if (!is_array($conversation_history)) {
@@ -9868,14 +4936,11 @@
9868 4936 if (!is_array($conversation_history)) {
9869 4937 $conversation_history = array();
9870 4938 }
9871 4939
9872 - // Get bot ID from session or request
9873 - $bot_id = $this->get_current_bot_id($session_id);
9874 -
9875 - // Get system prompt instructions using centralized function
9876 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9877 -
4940 + // Get system prompt instructions from options
4941 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4942 +
9878 4943 // Create a new array for the formatted conversation
9879 4944 $formatted_conversation = array();
9880 4945
9881 4946 // Add system message first
@@ -9923,9 +4988,9 @@
9923 4988 'httpversion' => '1.0',
9924 4989 'sslverify' => true,
9925 4990 ];
9926 4991
9927 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
4992 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
9928 4993
9929 4994 if (is_wp_error($response)) {
9930 4995 $error_message = $response->get_error_message();
9931 4996 //error_log('DeepSeek API Error: ' . $error_message);
@@ -10026,20 +5091,13 @@
10026 5091 'provider' => 'deepseek'
10027 5092 ];
10028 5093 }
10029 5094 }
5095 +
10030 5096 private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
10031 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10032 - // Auto-rescue existing installs whose saved model is the dead ID.
10033 - if ($selected_model === 'gemini-3-pro-preview') {
10034 - $selected_model = 'gemini-3.1-pro-preview';
10035 - }
10036 - // Get bot ID from session or request
10037 - $bot_id = $this->get_current_bot_id($session_id);
10038 -
10039 - // Get system prompt instructions using centralized function
10040 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10041 -
5097 + // Get system prompt instructions from options
5098 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
5099 +
10042 5100 // Add system prompt to relevant content
10043 5101 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10044 5102
10045 5103 // Format messages for Gemini API
@@ -10134,11 +5192,9 @@
10134 5192 ]
10135 5193 ]);
10136 5194
10137 5195 // Prepare the API endpoint
10138 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10139 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10140 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
5196 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10141 5197
10142 5198 // Set up the API request
10143 5199 $args = [
10144 5200 'body' => $body,
@@ -10152,10 +5208,10 @@
10152 5208 'sslverify' => true,
10153 5209 ];
10154 5210
10155 5211 // Make the API request
10156 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10157 -
5212 + $response = wp_remote_post($api_endpoint, $args);
5213 +
10158 5214 // Process the response
10159 5215 if (is_wp_error($response)) {
10160 5216 return "Sorry, there was an error processing your request: " . $response->get_error_message();
10161 5217 }
@@ -10178,138 +5234,9 @@
10178 5234 }
10179 5235 }
10180 5236
10181 5237
10182 -public function test_streaming_request() {
10183 - $options = get_option('mxchat_options', []);
10184 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
10185 5238
10186 - // Detect provider from model prefix
10187 - $provider = strtolower(explode('-', $model)[0]);
10188 -
10189 - $sample_prompt = 'Hello! Can you stream this response back to me?';
10190 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
10191 - $headers = [];
10192 - $body = [];
10193 - $url = '';
10194 - $api_key = '';
10195 -
10196 - switch ($provider) {
10197 - case 'gpt':
10198 - case 'o1':
10199 - $api_key = $options['api_key'] ?? '';
10200 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
10201 - $url = 'https://api.openai.com/v1/chat/completions';
10202 - $headers = [
10203 - 'Content-Type: application/json',
10204 - 'Authorization: Bearer ' . $api_key
10205 - ];
10206 - $body = [
10207 - 'model' => $model,
10208 - 'messages' => $messages,
10209 - 'stream' => true
10210 - ];
10211 - break;
10212 -
10213 - case 'claude':
10214 - $api_key = $options['claude_api_key'] ?? '';
10215 - if (empty($api_key)) return '❌ Missing API key for Claude';
10216 - $url = 'https://api.anthropic.com/v1/messages';
10217 - $headers = [
10218 - 'Content-Type: application/json',
10219 - 'x-api-key: ' . $api_key,
10220 - 'anthropic-version: 2023-06-01'
10221 - ];
10222 - $body = [
10223 - 'model' => $model,
10224 - 'messages' => $messages,
10225 - 'max_tokens' => 100,
10226 - 'stream' => true
10227 - ];
10228 - break;
10229 -
10230 - case 'grok':
10231 - $api_key = $options['xai_api_key'] ?? '';
10232 - if (empty($api_key)) return '❌ Missing API key for X.AI';
10233 - $url = 'https://api.x.ai/v1/chat/completions';
10234 - $headers = [
10235 - 'Content-Type: application/json',
10236 - 'Authorization: Bearer ' . $api_key
10237 - ];
10238 - $body = [
10239 - 'model' => $model,
10240 - 'messages' => $messages,
10241 - 'stream' => true
10242 - ];
10243 - break;
10244 -
10245 - case 'deepseek':
10246 - if (empty($deepseek_api_key)) {
10247 - $error_response = [
10248 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
10249 - 'error_code' => 'missing_deepseek_api_key'
10250 - ];
10251 - if ($testing_data !== null) {
10252 - $error_response['testing_data'] = $testing_data;
10253 - }
10254 - return $error_response;
10255 - }
10256 - if ($streaming) {
10257 - return $this->mxchat_generate_response_deepseek_stream(
10258 - $selected_model,
10259 - $deepseek_api_key,
10260 - $conversation_history,
10261 - $relevant_content,
10262 - $session_id,
10263 - $testing_data // Pass testing data
10264 - );
10265 - } else {
10266 - $response = $this->mxchat_generate_response_deepseek(
10267 - $selected_model,
10268 - $deepseek_api_key,
10269 - $conversation_history,
10270 - $relevant_content
10271 - );
10272 - }
10273 - break;
10274 -
10275 - case 'gemini':
10276 - $api_key = $options['gemini_api_key'] ?? '';
10277 - if (empty($api_key)) return '❌ Missing API key for Gemini';
10278 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
10279 - $headers = ['Content-Type: application/json'];
10280 - $body = [
10281 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
10282 - 'generationConfig' => ['temperature' => 0.7]
10283 - ];
10284 - break;
10285 -
10286 - default:
10287 - return '❌ Unsupported provider: ' . $provider;
10288 - }
10289 -
10290 - // Do the actual streaming test
10291 - $ch = curl_init($url);
10292 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
10293 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
10294 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10295 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
10296 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
10297 -
10298 - $response = curl_exec($ch);
10299 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
10300 - $error = curl_error($ch);
10301 - curl_close($ch);
10302 -
10303 - if ($error) return "❌ cURL error: $error";
10304 - if ($http_code !== 200) {
10305 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
10306 - return "❌ HTTP $http_code: $error_message";
10307 - }
10308 -
10309 - return true;
10310 -}
10311 -
10312 5239 public function mxchat_dismiss_pre_chat_message() {
10313 5240 // Get and sanitize the user identifier
10314 5241 $user_id = $this->mxchat_get_user_identifier();
10315 5242 $user_id = sanitize_key($user_id);
@@ -10363,63 +5290,40 @@
10363 5290
10364 5291 return $dotProduct / ($normA * $normB);
10365 5292 }
10366 5293
10367 -
10368 5294 public function mxchat_enqueue_scripts_styles() {
10369 - // Fetch options from the database first to check loading strategy
10370 - $this->options = get_option('mxchat_options');
10371 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10372 -
10373 - // Always enqueue CSS immediately
5295 + // Define version numbers for the styles and scripts
5296 + $chat_style_version = '2.3.3';
5297 + $chat_script_version = '2.3.3';
5298 + // Enqueue the script
5299 + wp_enqueue_script(
5300 + 'mxchat-chat-js',
5301 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
5302 + array('jquery'),
5303 + $chat_script_version,
5304 + true
5305 + );
5306 + // Enqueue the CSS
10374 5307 wp_enqueue_style(
10375 5308 'mxchat-chat-css',
10376 5309 plugin_dir_url(__FILE__) . '../css/chat-style.css',
10377 5310 array(),
10378 - MXCHAT_VERSION
5311 + $chat_style_version
10379 5312 );
10380 -
10381 - // Handle script loading based on strategy
10382 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10383 - // Enqueue the script normally
10384 - wp_enqueue_script(
10385 - 'mxchat-chat-js',
10386 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
10387 - array('jquery'),
10388 - MXCHAT_VERSION,
10389 - true
10390 - );
10391 -
10392 - // Add defer attribute if strategy is 'defer'
10393 - if ($loading_strategy === 'defer') {
10394 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
10395 - }
10396 - } else {
10397 - // For delay or interaction-based loading, we'll use a custom loader
10398 - // Don't enqueue the main script - we'll load it dynamically
10399 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
10400 - }
10401 -
5313 + // Fetch options from the database
5314 + $this->options = get_option('mxchat_options');
10402 5315 $prompts_options = get_option('mxchat_prompts_options', array());
10403 -
10404 - // Check if AI theme is active - if so, skip inline colors in JavaScript
10405 - $theme_options = get_option('mxchat_theme_options', array());
10406 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10407 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10408 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10409 -
5316 +
10410 5317 // Prepare settings for JavaScript
10411 5318 $style_settings = array(
10412 5319 'ajax_url' => admin_url('admin-ajax.php'),
10413 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
10414 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
10415 - // as a one-shot fallback for the first interaction on a fresh page load
10416 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
10417 - // but the widget refetches before each subsequent send.
10418 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10419 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10420 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
5320 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
5321 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-4o',
5322 + 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
5323 + 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off', // ADD THIS LINE
10421 5324 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
5325 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
10422 5326 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10423 5327 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10424 5328 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10425 5329 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -10434,152 +5338,20 @@
10434 5338 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10435 5339 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10436 5340 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10437 5341 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
5342 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
10438 5343 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10439 5344 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10440 5345 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10441 5346 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10442 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
10443 - 'initial_email_state' => null, // Also fixed this undefined variable
10444 - 'skip_email_check' => true,
10445 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10446 - 'skip_inline_colors' => $skip_inline_colors,
10447 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
5347 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
10448 5348 );
10449 -
10450 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10451 - // print/transcript, satisfaction rating) come from the shared
10452 - // dynamic-settings method so this inline payload and the first-open
10453 - // refresh endpoint can never drift (plan-32db95).
10454 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
10455 -
10456 - // For normal/defer loading, use wp_localize_script
10457 - // For delayed loading, we store settings in a transient to be output inline
10458 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10459 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10460 - } else {
10461 - // Store settings for the delayed loader to use
10462 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
10463 - }
5349 + // Pass the settings to the script
5350 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10464 5351 }
10465 5352
10466 -/**
10467 - * Output the delayed script loader for performance optimization
10468 - */
10469 -public function mxchat_output_delayed_script_loader() {
10470 - $this->options = get_option('mxchat_options');
10471 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10472 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
10473 5353
10474 - // Get the stored settings
10475 - $prompts_options = get_option('mxchat_prompts_options', array());
10476 - $theme_options = get_option('mxchat_theme_options', array());
10477 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10478 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10479 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10480 -
10481 - $style_settings = array(
10482 - 'ajax_url' => admin_url('admin-ajax.php'),
10483 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
10484 - // before each send. This inline value is a one-shot fallback for the first interaction.
10485 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10486 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10487 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10488 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10489 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10490 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10491 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10492 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10493 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10494 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10495 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10496 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10497 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10498 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10499 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10500 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10501 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10502 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10503 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10504 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10505 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10506 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10507 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10508 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10509 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
10510 - 'initial_email_state' => null,
10511 - 'skip_email_check' => true,
10512 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10513 - 'skip_inline_colors' => $skip_inline_colors,
10514 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10515 - );
10516 -
10517 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10518 - // print/transcript, satisfaction rating) come from the shared
10519 - // dynamic-settings method so this inline payload and the first-open
10520 - // refresh endpoint can never drift (plan-32db95).
10521 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
10522 -
10523 - // Determine delay time based on strategy
10524 - $delay_ms = 0;
10525 - switch ($loading_strategy) {
10526 - case 'delay_1s':
10527 - $delay_ms = 1000;
10528 - break;
10529 - case 'delay_3s':
10530 - $delay_ms = 3000;
10531 - break;
10532 - case 'delay_5s':
10533 - $delay_ms = 5000;
10534 - break;
10535 - }
10536 -
10537 - ?>
10538 - <script type="text/javascript">
10539 - (function() {
10540 - var mxchatLoaded = false;
10541 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
10542 - window.mxchatChat = mxchatChat;
10543 -
10544 - function loadMxChatScript() {
10545 - if (mxchatLoaded) return;
10546 - mxchatLoaded = true;
10547 -
10548 - function appendChatScript() {
10549 - var script = document.createElement('script');
10550 - script.src = <?php echo wp_json_encode($script_url); ?>;
10551 - script.type = 'text/javascript';
10552 - document.body.appendChild(script);
10553 - }
10554 -
10555 - if (typeof jQuery !== 'undefined') {
10556 - appendChatScript();
10557 - } else {
10558 - var jq = document.createElement('script');
10559 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
10560 - jq.onload = appendChatScript;
10561 - document.body.appendChild(jq);
10562 - }
10563 - }
10564 -
10565 - <?php if ($loading_strategy === 'on_interaction'): ?>
10566 - // Load on user interaction
10567 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
10568 - events.forEach(function(evt) {
10569 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
10570 - });
10571 - // Fallback: load after 8 seconds if no interaction
10572 - setTimeout(loadMxChatScript, 8000);
10573 - <?php else: ?>
10574 - // Load after specified delay
10575 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
10576 - <?php endif; ?>
10577 - })();
10578 - </script>
10579 - <?php
10580 -}
10581 -
10582 5354 /**
10583 5355 * Setup the cron jobs for rate limits with guard against multiple calls
10584 5356 */
10585 5357 public function setup_rate_limit_cron_jobs() {
@@ -10595,9 +5367,9 @@
10595 5367
10596 5368 try {
10597 5369 // First, check if WordPress cron is disabled
10598 5370 if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
10599 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
5371 + error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
10600 5372 $this->setup_fallback_rate_limit_system();
10601 5373 return;
10602 5374 }
10603 5375
@@ -10602,9 +5374,9 @@
10602 5374 }
10603 5375
10604 5376 // Check if cron is already scheduled - if so, don't mess with it
10605 5377 if (wp_next_scheduled('mxchat_reset_rate_limits')) {
10606 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
5378 + error_log('MxChat: Rate limit cron already scheduled, skipping setup');
10607 5379 return;
10608 5380 }
10609 5381
10610 5382 // Clear any orphaned hooks (but don't loop indefinitely)
@@ -10632,16 +5404,16 @@
10632 5404 $initial_time = time() + 300; // Start in 5 minutes
10633 5405 $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
10634 5406
10635 5407 if ($result === false) {
10636 - //error_log('MxChat: Failed to schedule cron, using fallback system');
5408 + error_log('MxChat: Failed to schedule cron, using fallback system');
10637 5409 $this->setup_fallback_rate_limit_system();
10638 5410 } else {
10639 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
5411 + error_log('MxChat: Successfully scheduled rate limit reset cron');
10640 5412 }
10641 5413
10642 5414 } catch (Exception $e) {
10643 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
5415 + error_log('MxChat: Cron setup exception: ' . $e->getMessage());
10644 5416 $this->setup_fallback_rate_limit_system();
10645 5417 }
10646 5418 }
10647 5419
@@ -10652,9 +5424,9 @@
10652 5424 try {
10653 5425 // Method 1: Try with current time instead of future time
10654 5426 $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
10655 5427 if ($result1 !== false) {
10656 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
5428 + error_log('MxChat: Alternative method 1 (current time) succeeded');
10657 5429 return true;
10658 5430 }
10659 5431
10660 5432 // Method 2: Try with a different interval
@@ -10659,9 +5431,9 @@
10659 5431
10660 5432 // Method 2: Try with a different interval
10661 5433 $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
10662 5434 if ($result2 !== false) {
10663 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
5435 + error_log('MxChat: Alternative method 2 (daily interval) succeeded');
10664 5436 return true;
10665 5437 }
10666 5438
10667 5439 // Method 3: Try wp_schedule_single_event first, then recurring
@@ -10666,9 +5438,9 @@
10666 5438
10667 5439 // Method 3: Try wp_schedule_single_event first, then recurring
10668 5440 $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
10669 5441 if ($result3 !== false) {
10670 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
5442 + error_log('MxChat: Alternative method 3 (single event) succeeded');
10671 5443 // Schedule the next one manually in the handler
10672 5444 return true;
10673 5445 }
10674 5446
@@ -10674,9 +5446,9 @@
10674 5446
10675 5447 return false;
10676 5448
10677 5449 } catch (Exception $e) {
10678 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
5450 + error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
10679 5451 return false;
10680 5452 }
10681 5453 }
10682 5454
@@ -10692,9 +5464,9 @@
10692 5464
10693 5465 // Also set up a more frequent fallback check (every 4 hours)
10694 5466 update_option('mxchat_fallback_check_interval', 4 * 3600);
10695 5467
10696 - //error_log('MxChat: Fallback rate limit system activated');
5468 + error_log('MxChat: Fallback rate limit system activated');
10697 5469 }
10698 5470
10699 5471 /**
10700 5472 * Enhanced fallback check method
@@ -10709,9 +5481,9 @@
10709 5481 $next_check = get_option('mxchat_next_rate_limit_check', 0);
10710 5482 $check_interval = get_option('mxchat_fallback_check_interval', 3600);
10711 5483
10712 5484 if (time() >= $next_check) {
10713 - //error_log('MxChat: Running fallback rate limit cleanup');
5485 + error_log('MxChat: Running fallback rate limit cleanup');
10714 5486 $this->mxchat_reset_rate_limits();
10715 5487
10716 5488 // Schedule next check
10717 5489 update_option('mxchat_next_rate_limit_check', time() + $check_interval);
@@ -10717,9 +5489,9 @@
10717 5489 update_option('mxchat_next_rate_limit_check', time() + $check_interval);
10718 5490 }
10719 5491 }
10720 5492 /**
10721 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
5493 + * Enhanced rate limit check that includes fallback cleanup
10722 5494 */
10723 5495 public function check_rate_limit() {
10724 5496 // Check if we need to run fallback cleanup
10725 5497 $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
@@ -10729,66 +5501,11 @@
10729 5501 $this->mxchat_reset_rate_limits();
10730 5502 update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
10731 5503 }
10732 5504
10733 - // Get bot ID from current request context
10734 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
5505 + // Continue with your existing rate limit logic...
5506 + $all_options = get_option('mxchat_options', []);
10735 5507
10736 - // Get bot-specific options (includes rate limits if overridden)
10737 - $bot_options = $this->get_bot_options($bot_id);
10738 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
10739 -
10740 - // Use bot-specific rate limits if available, otherwise fall back to default
10741 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
10742 -
10743 - // -------------------------------------------------------------------
10744 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
10745 - // it acts as a hard ceiling across all users + all roles. Default is
10746 - // 'unlimited' so existing installs are unchanged. Counter key drops
10747 - // both <role> and <user_id> segments — single pool per bot.
10748 - // -------------------------------------------------------------------
10749 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
10750 - ? $current_options['rate_limits_global']
10751 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
10752 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
10753 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
10754 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
10755 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10756 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
10757 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
10758 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
10759 - if ((int) $global_data['count'] === 0) {
10760 - $global_data['timestamp'] = time();
10761 - update_option($global_option, $global_data);
10762 - }
10763 - $now = time();
10764 - $ts = (int) $global_data['timestamp'];
10765 - $reset = false;
10766 - switch ($global_timeframe) {
10767 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
10768 - case 'daily': $reset = ($now - $ts) >= 86400; break;
10769 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
10770 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
10771 - }
10772 - if ($reset) {
10773 - $global_data = ['count' => 0, 'timestamp' => $now];
10774 - update_option($global_option, $global_data);
10775 - }
10776 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
10777 - $global_msg = !empty($global_cfg['message'])
10778 - ? $global_cfg['message']
10779 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
10780 - return [
10781 - 'error' => true,
10782 - 'message' => $this->process_rate_limit_message_html($global_msg),
10783 - ];
10784 - }
10785 - // Reserve the slot for this request. Per-role check below also increments
10786 - // its own counter — that is intentional, both ceilings apply independently.
10787 - $global_data['count']++;
10788 - update_option($global_option, $global_data);
10789 - }
10790 -
10791 5508 // Determine user role or if logged out
10792 5509 if (is_user_logged_in()) {
10793 5510 $user = wp_get_current_user();
10794 5511 $user_id = $user->ID;
@@ -10808,13 +5525,13 @@
10808 5525 $user_id = $this->get_client_ip();
10809 5526 }
10810 5527
10811 5528 // Check if rate limits are configured for this role
10812 - if (!isset($rate_limits_source[$role])) {
5529 + if (!isset($all_options['rate_limits'][$role])) {
10813 5530 return true; // No limit set for this role
10814 5531 }
10815 5532
10816 - $limit = $rate_limits_source[$role]['limit'];
5533 + $limit = $all_options['rate_limits'][$role]['limit'];
10817 5534
10818 5535 // If unlimited, return true immediately
10819 5536 if ($limit === 'unlimited') {
10820 5537 return true;
@@ -10819,16 +5536,13 @@
10819 5536 if ($limit === 'unlimited') {
10820 5537 return true;
10821 5538 }
10822 5539
10823 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
5540 + // Get the option name for this user/role with safer naming
10824 5541 $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
10825 5542 $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
10826 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
5543 + $option_name = 'mxchat_chat_limit_' . $safe_role . '_' . $safe_user_id;
10827 5544
10828 - // Include bot_id in option name so each bot has separate rate limits
10829 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
10830 -
10831 5545 // Get the counter data
10832 5546 $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
10833 5547
10834 5548 // If first request or counter reset needed, set the initial timestamp
@@ -10837,10 +5551,10 @@
10837 5551 update_option($option_name, $limit_data);
10838 5552 }
10839 5553
10840 5554 // Get the timeframe
10841 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
10842 - $rate_limits_source[$role]['timeframe'] : 'daily';
5555 + $timeframe = isset($all_options['rate_limits'][$role]['timeframe']) ?
5556 + $all_options['rate_limits'][$role]['timeframe'] : 'daily';
10843 5557
10844 5558 // Check if the counter needs to be reset based on timeframe
10845 5559 $current_time = time();
10846 5560 $timestamp = $limit_data['timestamp'];
@@ -10869,10 +5583,10 @@
10869 5583
10870 5584 // Check if user has exceeded their limit
10871 5585 if ($limit_data['count'] >= intval($limit)) {
10872 5586 // Get the custom message for this role
10873 - $message = !empty($rate_limits_source[$role]['message'])
10874 - ? $rate_limits_source[$role]['message']
5587 + $message = !empty($all_options['rate_limits'][$role]['message'])
5588 + ? $all_options['rate_limits'][$role]['message']
10875 5589 : __('Rate limit exceeded. Please try again later.', 'mxchat');
10876 5590
10877 5591 // Add timeframe information to the message if placeholders exist
10878 5592 $timeframe_label = '';
@@ -10944,9 +5658,9 @@
10944 5658
10945 5659 foreach ($option_names as $option_name) {
10946 5660 // Check processing time limit
10947 5661 if ((time() - $start_time) > $max_processing_time) {
10948 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
5662 + error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
10949 5663 break;
10950 5664 }
10951 5665
10952 5666 // Parse the option name more safely
@@ -11010,12 +5724,12 @@
11010 5724
11011 5725 // Clean up any orphaned cache entries
11012 5726 wp_cache_delete('mxchat_all_chat_limits', 'options');
11013 5727
11014 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
5728 + error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11015 5729
11016 5730 } catch (Exception $e) {
11017 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
5731 + error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11018 5732 }
11019 5733 }
11020 5734
11021 5735
@@ -11162,11 +5876,8 @@
11162 5876
11163 5877 /**
11164 5878 * AJAX handler to get system information for testing panel
11165 5879 */
11166 -/**
11167 - * AJAX handler to get system information for testing panel
11168 - */
11169 5880 public function mxchat_get_system_info() {
11170 5881 // Verify nonce for security
11171 5882 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11172 5883 wp_send_json_error(['message' => 'Invalid nonce']);
@@ -11184,24 +5895,10 @@
11184 5895 ? $this->options['system_prompt_instructions']
11185 5896 : 'No system prompt configured';
11186 5897
11187 5898 // Get selected model
11188 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
5899 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
11189 5900
11190 - // Check if OpenRouter is being used
11191 - $is_openrouter = ($selected_model === 'openrouter');
11192 - $openrouter_model = '';
11193 -
11194 - if ($is_openrouter) {
11195 - // Get the actual OpenRouter model that's selected
11196 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11197 - ? $this->options['openrouter_selected_model']
11198 - : 'No OpenRouter model selected';
11199 -
11200 - // Update selected_model display to show both
11201 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11202 - }
11203 -
11204 5901 // Get API key status (just check if they exist, don't expose the keys)
11205 5902 $api_status = [];
11206 5903 $api_status['openai'] = !empty($this->options['api_key']);
11207 5904 $api_status['claude'] = !empty($this->options['claude_api_key']);
@@ -11207,15 +5904,12 @@
11207 5904 $api_status['claude'] = !empty($this->options['claude_api_key']);
11208 5905 $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11209 5906 $api_status['xai'] = !empty($this->options['xai_api_key']);
11210 5907 $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11211 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11212 5908
11213 5909 wp_send_json_success([
11214 5910 'system_prompt' => $system_prompt,
11215 5911 'selected_model' => $selected_model,
11216 - 'is_openrouter' => $is_openrouter,
11217 - 'openrouter_model' => $openrouter_model,
11218 5912 'api_status' => $api_status
11219 5913 ]);
11220 5914 }
11221 5915
@@ -11234,12 +5928,12 @@
11234 5928 wp_send_json_error(['message' => 'Unauthorized']);
11235 5929 return;
11236 5930 }
11237 5931
11238 - // Get similarity threshold from main options (default 35%)
5932 + // Get similarity threshold from main options (default 75%)
11239 5933 $similarity_threshold = isset($this->options['similarity_threshold'])
11240 5934 ? ((int) $this->options['similarity_threshold']) / 100
11241 - : 0.35;
5935 + : 0.75;
11242 5936
11243 5937 wp_send_json_success([
11244 5938 'threshold' => $similarity_threshold,
11245 5939 'threshold_percentage' => ($similarity_threshold * 100) . '%'
@@ -11254,42 +5948,24 @@
11254 5948 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11255 5949 wp_send_json_error(['message' => 'Invalid nonce']);
11256 5950 return;
11257 5951 }
11258 -
5952 +
11259 5953 // Only allow admin users
11260 5954 if (!current_user_can('administrator')) {
11261 5955 wp_send_json_error(['message' => 'Unauthorized']);
11262 5956 return;
11263 5957 }
11264 -
11265 - // Check OpenAI Vector Store first (takes priority)
11266 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11267 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11268 -
11269 - if ($use_vectorstore) {
11270 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11271 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11272 -
11273 - $kb_info = [
11274 - 'type' => 'OpenAI Vector Store',
11275 - 'status' => 'Active',
11276 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11277 - ];
11278 -
11279 - wp_send_json_success($kb_info);
11280 - return;
11281 - }
11282 -
5958 +
11283 5959 // Check Pinecone vs WordPress
11284 5960 $addon_options = get_option('mxchat_pinecone_addon_options', array());
11285 5961 $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11286 -
5962 +
11287 5963 $kb_info = [
11288 5964 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11289 5965 'status' => 'Active'
11290 5966 ];
11291 -
5967 +
11292 5968 // Get document count
11293 5969 if ($use_pinecone) {
11294 5970 $kb_info['documents'] = 'Connected to Pinecone';
11295 5971 $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
@@ -11299,9 +5975,9 @@
11299 5975 $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11300 5976 $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11301 5977 $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11302 5978 }
11303 -
5979 +
11304 5980 wp_send_json_success($kb_info);
11305 5981 }
11306 5982
11307 5983 /**
@@ -11383,13 +6059,9 @@
11383 6059 // Clear any other session-specific transients
11384 6060 delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
11385 6061 delete_transient("mxchat_include_pdf_in_context_{$session_id}");
11386 6062 delete_transient("mxchat_include_word_in_context_{$session_id}");
11387 -
11388 - // Clear form addon state (pending forms and submitted forms)
11389 - delete_option("mxchat_pending_form_{$session_id}");
11390 - delete_option("mxchat_submitted_forms_{$session_id}");
11391 -
6063 +
11392 6064 //error_log("MxChat: Cleared all data for session: {$session_id}");
11393 6065 }
11394 6066
11395 6067 /**
@@ -11424,15 +6096,15 @@
11424 6096 $testing_data = [
11425 6097 'query' => $message,
11426 6098 'timestamp' => time(),
11427 6099 'top_matches' => [],
11428 - 'action_matches' => [] // Add action matches
6100 + 'action_matches' => [] // NEW: Add action matches
11429 6101 ];
11430 6102
11431 6103 // Get similarity threshold
11432 6104 $similarity_threshold = isset($this->options['similarity_threshold'])
11433 6105 ? ((int) $this->options['similarity_threshold']) / 100
11434 - : 0.35;
6106 + : 0.75;
11435 6107
11436 6108 $testing_data['similarity_threshold'] = $similarity_threshold;
11437 6109
11438 6110 // Use the real similarity analysis if available
@@ -11447,9 +6119,9 @@
11447 6119
11448 6120 $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
11449 6121 }
11450 6122
11451 - // Include action analysis if available
6123 + // NEW: Include action analysis if available
11452 6124 if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
11453 6125 $testing_data['action_matches'] = $this->last_action_analysis;
11454 6126
11455 6127 // Clear it after capturing to avoid stale data
@@ -11457,303 +6129,8 @@
11457 6129 }
11458 6130
11459 6131 return $testing_data;
11460 6132 }
11461 -
11462 -
11463 -/**
11464 - * Track URL clicks from chatbot responses
11465 - */
11466 -public function mxchat_track_url_click() {
11467 - // Verify nonce for security
11468 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11469 - wp_send_json_error(['message' => 'Invalid nonce']);
11470 - wp_die();
11471 - }
11472 -
11473 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11474 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
11475 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
11476 -
11477 - if (empty($session_id) || empty($clicked_url)) {
11478 - wp_send_json_error(['message' => 'Missing required data']);
11479 - wp_die();
11480 - }
11481 -
11482 - global $wpdb;
11483 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11484 -
11485 - // Insert click tracking record
11486 - $wpdb->insert(
11487 - $table_name,
11488 - [
11489 - 'session_id' => $session_id,
11490 - 'clicked_url' => $clicked_url,
11491 - 'message_context' => $message_context,
11492 - 'click_timestamp' => current_time('mysql', 1),
11493 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
11494 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
11495 - ]
11496 - );
11497 -
11498 - wp_send_json_success(['message' => 'Click tracked']);
11499 - wp_die();
11500 -}
11501 -
11502 -/**
11503 - * Get URL click analytics for a session
11504 - */
11505 -public function mxchat_get_url_clicks($session_id) {
11506 - global $wpdb;
11507 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11508 -
11509 - $clicks = $wpdb->get_results($wpdb->prepare(
11510 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
11511 - $session_id
11512 - ));
11513 -
11514 - return $clicks;
11515 -}
11516 -/**
11517 - * Track the originating page where chat was started
11518 - */
11519 -public function mxchat_track_originating_page() {
11520 - // Verify nonce
11521 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11522 - wp_send_json_error(['message' => 'Invalid nonce']);
11523 - wp_die();
11524 - }
11525 -
11526 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11527 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
11528 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
11529 -
11530 - if (empty($session_id)) {
11531 - wp_send_json_error(['message' => 'Missing session ID']);
11532 - wp_die();
11533 - }
11534 -
11535 - global $wpdb;
11536 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
11537 -
11538 - // Check if we've already tracked for this session
11539 - $existing = $wpdb->get_var($wpdb->prepare(
11540 - "SELECT COUNT(*) FROM $table_name
11541 - WHERE session_id = %s
11542 - AND originating_page_url IS NOT NULL",
11543 - $session_id
11544 - ));
11545 -
11546 - if ($existing > 0) {
11547 - wp_send_json_success(['message' => 'Already tracked']);
11548 - wp_die();
11549 - }
11550 -
11551 - // Update the first message in this session with originating page info
11552 - $wpdb->query($wpdb->prepare(
11553 - "UPDATE $table_name
11554 - SET originating_page_url = %s,
11555 - originating_page_title = %s
11556 - WHERE session_id = %s
11557 - ORDER BY timestamp ASC
11558 - LIMIT 1",
11559 - $page_url,
11560 - $page_title,
11561 - $session_id
11562 - ));
11563 -
11564 - wp_send_json_success(['message' => 'Originating page tracked']);
11565 - wp_die();
11566 -}
11567 -
11568 -/**
11569 - * Validate and clean URLs from AI response
11570 - * Removes any URLs that aren't in the knowledge base
11571 - *
11572 - * @param string $response_text The AI-generated response
11573 - * @param array $valid_urls Array of URLs from the knowledge base
11574 - * @return string Cleaned response with invalid URLs removed/flagged
11575 - */
11576 -private function validate_and_clean_urls($response_text, $valid_urls) {
11577 - // DEBUG: Log what we're working with
11578 - //error_log("=== MxChat URL Validation Debug ===");
11579 - //error_log("Valid URLs count: " . count($valid_urls));
11580 - //error_log("Valid URLs: " . print_r($valid_urls, true));
11581 - //error_log("Response text length: " . strlen($response_text));
11582 - //error_log("Response text preview: " . substr($response_text, 0, 500));
11583 -
11584 - // If no valid URLs provided or empty response, return as-is
11585 - if (empty($valid_urls) || empty($response_text)) {
11586 - //error_log("Validation skipped - empty valid_urls or response");
11587 - return $response_text;
11588 - }
11589 -
11590 - // Extract all URLs from the AI response
11591 - // This regex matches http:// and https:// URLs
11592 - preg_match_all(
11593 - '#\bhttps?://[^\s<>"\')\]]+#i',
11594 - $response_text,
11595 - $matches
11596 - );
11597 -
11598 - // If no URLs found in response, return as-is
11599 - if (empty($matches[0])) {
11600 - //error_log("No URLs found in response");
11601 - return $response_text;
11602 - }
11603 -
11604 - $found_urls = $matches[0];
11605 - $cleaned_response = $response_text;
11606 - $removed_count = 0;
11607 -
11608 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
11609 - $normalized_valid_urls = array_map(function($url) {
11610 - // Remove trailing slash
11611 - $url = rtrim($url, '/');
11612 - // Remove URL fragments (#section)
11613 - $url = preg_replace('/#.*$/', '', $url);
11614 - // Remove trailing punctuation that might have been captured
11615 - $url = rtrim($url, '.,;:!?');
11616 - return $url;
11617 - }, $valid_urls);
11618 -
11619 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
11620 -
11621 - foreach ($found_urls as $found_url) {
11622 - // Clean up the found URL (remove trailing punctuation that might have been captured)
11623 - $clean_found_url = rtrim($found_url, '.,;:!?)');
11624 -
11625 - // DEBUG: Log each URL being checked
11626 - //error_log("Checking found URL: " . $found_url);
11627 -
11628 - // Normalize for comparison
11629 - $normalized_found = rtrim($clean_found_url, '/');
11630 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
11631 -
11632 - //error_log("Normalized found URL: " . $normalized_found);
11633 -
11634 - // Check if this URL exists in our valid URLs list
11635 - $is_valid = false;
11636 -
11637 - //error_log("Starting validation checks for: " . $normalized_found);
11638 -
11639 - // First, try exact match
11640 - if (in_array($normalized_found, $normalized_valid_urls)) {
11641 - $is_valid = true;
11642 - //error_log("EXACT MATCH FOUND");
11643 - } else {
11644 - //error_log("No exact match, checking variations...");
11645 - // If no exact match, check if it's a variation (with query params, etc.)
11646 - foreach ($normalized_valid_urls as $valid_url) {
11647 - //error_log(" Comparing against valid URL: " . $valid_url);
11648 -
11649 - // Check if the found URL starts with a valid URL (handles query params)
11650 - if (strpos($normalized_found, $valid_url) === 0) {
11651 - // Check what comes after the valid URL
11652 - $remainder = substr($normalized_found, strlen($valid_url));
11653 -
11654 - // Only valid if:
11655 - // 1. Exact match (remainder is empty)
11656 - // 2. Query params (starts with ?)
11657 - // 3. Fragment (starts with #)
11658 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
11659 - $is_valid = true;
11660 - //error_log(" MATCH: Found URL is valid variation of base URL");
11661 - break;
11662 - } else {
11663 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
11664 - }
11665 - }
11666 - // Also check the reverse (in case valid URL has query params)
11667 - if (strpos($valid_url, $normalized_found) === 0) {
11668 - $is_valid = true;
11669 - //error_log(" MATCH: Valid URL starts with found URL");
11670 - break;
11671 - }
11672 - }
11673 -
11674 - if (!$is_valid) {
11675 - //error_log("NO MATCH FOUND - URL should be removed");
11676 - }
11677 - }
11678 -
11679 - // If URL is not valid, remove it from the response
11680 - if (!$is_valid) {
11681 - // Log the removal for debugging
11682 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
11683 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
11684 -
11685 - $removed_count++;
11686 -
11687 - // Check if URL is part of a markdown link: [text](url)
11688 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
11689 - if (preg_match($markdown_pattern, $cleaned_response)) {
11690 - //error_log("Found markdown link, removing but keeping text");
11691 - // Remove the markdown link but keep the text
11692 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
11693 - }
11694 - // Check if URL is part of an HTML link: <a href="url">text</a>
11695 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
11696 - //error_log("Found HTML link, removing but keeping text");
11697 - // Remove the HTML link but keep the text
11698 - $link_text = $link_match[1];
11699 - $cleaned_response = preg_replace(
11700 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
11701 - $link_text,
11702 - $cleaned_response
11703 - );
11704 - }
11705 - // Otherwise just remove the bare URL
11706 - else {
11707 - //error_log("Removing bare URL");
11708 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
11709 - }
11710 - }
11711 - }
11712 -
11713 - // Log summary if any URLs were removed
11714 - if ($removed_count > 0) {
11715 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
11716 - } else {
11717 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
11718 - }
11719 -
11720 - // Clean up any double spaces or awkward punctuation left behind
11721 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
11722 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
11723 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
11724 -
11725 - //error_log("Final cleaned response: " . $cleaned_response);
11726 -
11727 - return trim($cleaned_response);
11728 -}
11729 -
11730 -/**
11731 - * AJAX handler to get current chat mode for a session
11732 - */
11733 -public function mxchat_get_current_chat_mode() {
11734 - // Verify nonce for security
11735 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11736 - wp_send_json_error(['message' => 'Invalid nonce']);
11737 - wp_die();
11738 - }
11739 -
11740 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11741 -
11742 - if (empty($session_id)) {
11743 - wp_send_json_error(['message' => 'Session ID missing']);
11744 - wp_die();
11745 - }
11746 -
11747 - // Get the current chat mode for this session
11748 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
11749 -
11750 - wp_send_json_success([
11751 - 'chat_mode' => $chat_mode
11752 - ]);
11753 - wp_die();
11754 -}
11755 -
11756 6133
11757 6134
11758 6135 }
11759 6136 ?>