PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.6
MxChat – AI Chatbot & Content Generation for WordPress v2.4.6
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-integrator.php +1097 -5528 3.2.92.4.6 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');
@@ -317,93 +81,13 @@
317 81 // Add chat mode checking actions
318 82 add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
319 83 add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
320 84
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'));
324 -
325 - // Auto-email transcript action
326 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
327 -
328 85 add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
329 86
330 87
331 88 }
332 89
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 90 // In your core plugin's check_actions_for_addons method:
407 91 public function check_actions_for_addons($default, $message, $user_id, $session_id) {
408 92 //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
409 93
@@ -426,22 +110,8 @@
426 110 wp_die();
427 111 }
428 112
429 113 $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 114 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
445 115 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
446 116
447 117 if (empty($history)) {
@@ -458,25 +128,11 @@
458 128 'chat_mode' => $chat_mode
459 129 ]);
460 130 wp_die();
461 131 }
462 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
132 +
133 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
463 134 $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 135 $formatted_history = [];
480 136
481 137 // Adjusted for code-heavy conversations
482 138 $max_tokens = 120000; // Context window size
@@ -550,17 +206,8 @@
550 206
551 207 public function register_routes() {
552 208 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
553 209
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 210 register_rest_route('mxchat/v1', '/stream', [
564 211 'methods' => 'GET',
565 212 'callback' => [$this, 'mxchat_stream_events'],
566 213 'permission_callback' => [$this, 'verify_chat_session'],
@@ -583,105 +230,12 @@
583 230 'callback' => [$this, 'handle_slack_messages'],
584 231 'permission_callback' => [$this, 'verify_slack_request'],
585 232 ]);
586 233
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 234 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
595 235 }
596 236
597 237 /**
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 238 * Verify valid chat session
685 239 */
686 240 public function verify_chat_session($request) {
687 241 $session_id = $request->get_param('session_id');
@@ -717,11 +271,10 @@
717 271 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
718 272 return false;
719 273 }
720 274
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();
275 + // Get raw request body
276 + $request_body = file_get_contents('php://input');
724 277
725 278 // Create the signature base string
726 279 $sig_basestring = "v0:{$timestamp}:{$request_body}";
727 280
@@ -730,43 +283,8 @@
730 283
731 284 // Compare signatures
732 285 return hash_equals($my_signature, $slack_signature);
733 286 }
734 -
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 287 public function mxchat_stream_events(WP_REST_Request $request) {
770 288 header('Content-Type: text/event-stream');
771 289 header('Cache-Control: no-cache');
772 290 header('Connection: keep-alive');
@@ -800,9 +318,9 @@
800 318
801 319
802 320
803 321
804 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
322 +private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null) {
805 323 global $wpdb;
806 324 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
807 325 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
808 326
@@ -820,20 +338,8 @@
820 338 //error_log("[DEBUG] This is a NEW session - first message");
821 339 }
822 340 }
823 341
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 342 // 1) Extract agent name if present
837 343 $agent_name = '';
838 344 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
839 345 $agent_name = $matches[1];
@@ -938,11 +444,10 @@
938 444 $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
939 445
940 446 //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
941 447
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;
448 + // Clear after using
449 + unset($this->pending_originating_page);
945 450 }
946 451 // Fallback to HTTP_REFERER if nothing else is available
947 452 else if (isset($_SERVER['HTTP_REFERER'])) {
948 453 $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
@@ -977,17 +482,9 @@
977 482 $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
978 483 }
979 484 }
980 485 }
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 -
486 +
990 487 $wpdb->insert($table_name, $insert_data);
991 488 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
992 489
993 490 // 9) Send notification email if this is the first user message in a new session
@@ -998,17 +495,11 @@
998 495 'ip' => $_SERVER['REMOTE_ADDR']
999 496 ));
1000 497 }
1001 498
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 499 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1008 500 return $message_id;
1009 501 }
1010 -
1011 502 private function send_new_chat_notification($session_id, $user_info = array()) {
1012 503 $options = get_option('mxchat_transcripts_options');
1013 504
1014 505 // Check if notifications are enabled
@@ -1051,202 +542,14 @@
1051 542 // Send email
1052 543 return wp_mail($to, $subject, $message);
1053 544 }
1054 545
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 546 public function mxchat_handle_save_email_and_response() {
1242 547 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1243 548 //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1244 549
1245 - nocache_headers();
1246 -
1247 550 // Validate nonce
1248 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
551 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1249 552 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1250 553 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1251 554 wp_die();
1252 555 }
@@ -1256,9 +559,9 @@
1256 559 $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1257 560
1258 561 //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
1259 562
1260 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
563 + if (empty($session_id) || empty($email)) {
1261 564 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1262 565 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1263 566 wp_die();
1264 567 }
@@ -1275,15 +578,15 @@
1275 578 }
1276 579
1277 580 // 1) Always store email in wp_options
1278 581 $email_option_key = "mxchat_email_{$session_id}";
1279 - update_option($email_option_key, $email, 'no');
582 + update_option($email_option_key, $email);
1280 583 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1281 584
1282 585 // Store name in wp_options if provided
1283 586 if (!empty($name)) {
1284 587 $name_option_key = "mxchat_name_{$session_id}";
1285 - update_option($name_option_key, $name, 'no');
588 + update_option($name_option_key, $name);
1286 589 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1287 590 }
1288 591
1289 592 // 2) (Optional) Also store in DB if a row already exists
@@ -1327,17 +630,15 @@
1327 630
1328 631 public function mxchat_check_email_provided() {
1329 632 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1330 633
1331 - nocache_headers();
1332 -
1333 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
634 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1334 635 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1335 636 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1336 637 }
1337 638
1338 639 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1339 - if (empty($session_id) || $session_id === 'null') {
640 + if (empty($session_id)) {
1340 641 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1341 642 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1342 643 }
1343 644
@@ -1394,42 +695,15 @@
1394 695 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1395 696 }
1396 697 }
1397 698
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 699 public function mxchat_handle_chat_request() {
1426 700 global $wpdb;
1427 701
1428 702 // Debug: Log incoming bot_id
1429 703 $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);
704 + error_log("=== MXCHAT DEBUG: Starting chat request ===");
705 + error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1432 706
1433 707 // Get bot-specific options
1434 708 $bot_options = $this->get_bot_options($bot_id);
1435 709 $current_options = !empty($bot_options) ? $bot_options : $this->options;
@@ -1434,19 +708,29 @@
1434 708 $bot_options = $this->get_bot_options($bot_id);
1435 709 $current_options = !empty($bot_options) ? $bot_options : $this->options;
1436 710
1437 711 // 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'));
712 + $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
713 + isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on';
714 +
715 + // Set streaming headers if needed
716 + if ($is_streaming) {
717 + // Disable output buffering
718 + while (ob_get_level()) {
719 + ob_end_flush(); // Changed from ob_end_clean()
720 + }
721 +
722 + // Set headers for SSE
723 + header('Content-Type: text/event-stream');
724 + header('Cache-Control: no-cache');
725 + header('Connection: keep-alive');
726 + header('X-Accel-Buffering: no');
727 +
728 + // Add these new lines:
729 + ob_implicit_flush(true);
730 + flush();
731 + }
1442 732
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 733 // Check if MX Chat Moderation is active
1450 734 if (class_exists('MX_Chat_Moderation')) {
1451 735 // Get user email and IP
1452 736 $user_email = '';
@@ -1511,31 +795,13 @@
1511 795
1512 796 // Rest of your existing code...
1513 797 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1514 798
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 799 if (empty($session_id)) {
1525 800 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1526 801 wp_die();
1527 802 }
1528 803
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 804 // Validate and sanitize the incoming message
1539 805 if (empty($_POST['message'])) {
1540 806 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1541 807 wp_die();
@@ -1911,8 +1177,38 @@
1911 1177 }
1912 1178 }
1913 1179 }
1914 1180
1181 + // Check if there's an active recommendation flow session
1182 + $flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
1183 + if (!empty($flow_state) && isset($flow_state['flow_id'])) {
1184 + // Create a dummy intent object that matches the original intent
1185 + $dummy_intent = new stdClass();
1186 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
1187 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
1188 +
1189 + // Call the recommendation flow handler directly
1190 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
1191 +
1192 + // If the handler returned a response, send it
1193 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
1194 + // Save the bot's response to the chat history
1195 + if (!empty($response_data['text'])) {
1196 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
1197 + }
1198 + if (!empty($response_data['html'])) {
1199 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
1200 + }
1201 +
1202 + if ($testing_data !== null) {
1203 + $response_data['testing_data'] = $testing_data;
1204 + }
1205 +
1206 + // Send the response
1207 + wp_send_json($response_data);
1208 + wp_die();
1209 + }
1210 + }
1915 1211
1916 1212 // Step 2: Detect intent and handle intent-based responses
1917 1213 $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1918 1214
@@ -1931,46 +1227,58 @@
1931 1227 'text' => $intent_result['text'] ?? '',
1932 1228 'html' => $intent_result['html'] ?? '',
1933 1229 'session_id' => $session_id
1934 1230 ];
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 -
1231 +
1941 1232 if ($testing_data !== null) {
1942 1233 $response_data['testing_data'] = $testing_data;
1943 1234 }
1944 -
1235 +
1236 + // Clear streaming headers if they were set
1237 + if ($is_streaming) {
1238 + header_remove('Content-Type');
1239 + header_remove('Cache-Control');
1240 + header_remove('Connection');
1241 + header_remove('X-Accel-Buffering');
1242 + header('Content-Type: application/json');
1243 + }
1244 +
1945 1245 wp_send_json($response_data);
1946 1246 wp_die();
1947 1247 } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1948 1248 // Intent returned true and set fallbackResponse
1949 -
1950 - // SAVE TO TRANSCRIPT
1249 +
1250 + // SAVE TO TRANSCRIPT FIRST
1951 1251 if (!empty($this->fallbackResponse['text'])) {
1952 1252 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1953 1253 }
1954 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1955 1254 if (!empty($this->fallbackResponse['html'])) {
1956 1255 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1957 1256 }
1958 -
1257 +
1959 1258 $response_data = [
1960 1259 'text' => $this->fallbackResponse['text'] ?? '',
1961 1260 'html' => $this->fallbackResponse['html'] ?? '',
1962 1261 'session_id' => $session_id
1963 1262 ];
1964 -
1263 +
1965 1264 if (isset($this->fallbackResponse['chat_mode'])) {
1966 1265 $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1967 1266 }
1968 -
1267 +
1969 1268 if ($testing_data !== null) {
1970 1269 $response_data['testing_data'] = $testing_data;
1971 1270 }
1972 -
1271 +
1272 + // Clear streaming headers if they were set
1273 + if ($is_streaming) {
1274 + header_remove('Content-Type');
1275 + header_remove('Cache-Control');
1276 + header_remove('Connection');
1277 + header_remove('X-Accel-Buffering');
1278 + header('Content-Type: application/json');
1279 + }
1280 +
1973 1281 wp_send_json($response_data);
1974 1282 wp_die();
1975 1283 }
1976 1284 }
@@ -1975,13 +1283,11 @@
1975 1283 }
1976 1284 }
1977 1285
1978 1286 // If we get here, no intent matched OR the intent didn't provide a usable response
1979 -
1287 +
1980 1288 // 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);
1289 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
1984 1290 $this->mxchat_increment_chat_count();
1985 1291
1986 1292 // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1987 1293 $api_key = $current_options['api_key'] ?? $this->options['api_key'];
@@ -1990,50 +1296,22 @@
1990 1296 // Check if the embedding generation returned an error
1991 1297 if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1992 1298 $error_message = $user_message_embedding['error'];
1993 1299 $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 - ]);
2011 - }
1300 +
1301 + wp_send_json_error([
1302 + 'error_message' => $error_message,
1303 + 'error_code' => $error_code
1304 + ]);
2012 1305 wp_die();
2013 1306 }
2014 -
1307 +
2015 1308 // Check if the embedding is valid
2016 1309 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');
2018 -
2019 - // FIXED: Send error in appropriate format based on streaming mode
2020 - 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 - ]);
2035 - }
1310 + wp_send_json_error([
1311 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1312 + 'error_code' => 'invalid_embedding'
1313 + ]);
2036 1314 wp_die();
2037 1315 }
2038 1316
2039 1317 // Build context with both knowledge base and PDF content if available
@@ -2059,71 +1337,26 @@
2059 1337 $context_content .= "Page Content: " . $page_context['content'] . "\n";
2060 1338 $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2061 1339 }
2062 1340
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);
2065 -
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;
1341 + // Get relevant content from knowledge base - PASS BOT_ID
1342 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id);
2070 1343
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 - );
2078 -
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);
2087 -
2088 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2089 - }
1344 + // ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
1345 + if ($testing_data !== null && $this->last_similarity_analysis !== null) {
1346 + // Update testing data with the REAL similarity analysis
1347 + $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
1348 + $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1349 + $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2090 1350 }
1351 + // ===== END SIMILARITY DATA CAPTURE =====
2091 1352
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 =====
2102 -
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 1353 if (!empty($relevant_content)) {
2110 1354 $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2111 1355 } else {
2112 1356 $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2113 1357 }
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";
2121 - }
2122 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2123 - $context_content .= "===== END APPROVED URLS =====\n\n";
2124 - }
2125 -
1358 +
2126 1359 // Check for and include PDF content
2127 1360 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2128 1361 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2129 1362 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2155,9 +1388,9 @@
2155 1388
2156 1389 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2157 1390
2158 1391 // 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';
1392 + $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-4o';
2160 1393
2161 1394 $response = $this->mxchat_generate_response(
2162 1395 $context_content,
2163 1396 $current_options['api_key'] ?? $this->options['api_key'],
@@ -2164,14 +1397,13 @@
2164 1397 $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2165 1398 $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2166 1399 $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2167 1400 $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2168 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2169 1401 $conversation_history,
2170 1402 $is_streaming,
2171 1403 $session_id,
2172 1404 $testing_data,
2173 - $selected_model
1405 + $selected_model // ADD THIS LINE
2174 1406 );
2175 1407
2176 1408 // Handle streaming vs non-streaming responses
2177 1409 if ($is_streaming) {
@@ -2179,27 +1411,11 @@
2179 1411 if ($response === true) {
2180 1412 wp_die();
2181 1413 }
2182 1414 // 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 1415 }
2200 -
2201 - // Check if the response is an error array (non-streaming mode)
1416 +
1417 + // Check if the response is an error array
2202 1418 if (is_array($response) && isset($response['error'])) {
2203 1419 wp_send_json_error([
2204 1420 'error_message' => $response['error'],
2205 1421 'error_code' => $response['error_code'] ?? 'api_error'
@@ -2206,51 +1422,11 @@
2206 1422 ]);
2207 1423 wp_die();
2208 1424 }
2209 1425
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 =====
1426 + // If we get here, the response is valid text
1427 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2224 1428
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 1429 // Step 5: Save additional content if available
2254 1430 if (!empty($this->productCardHtml)) {
2255 1431 $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2256 1432 }
@@ -2259,13 +1435,8 @@
2259 1435 $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2260 1436 }
2261 1437
2262 1438 // 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 1439 $response_data = [
2269 1440 'text' => $response,
2270 1441 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2271 1442 'session_id' => $session_id
@@ -2270,18 +1441,8 @@
2270 1441 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2271 1442 'session_id' => $session_id
2272 1443 ];
2273 1444
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 1445 // Always add testing data for admins (no toggle needed)
2285 1446 if ($testing_data !== null) {
2286 1447 $response_data['testing_data'] = $testing_data;
2287 1448 }
@@ -2289,8 +1450,9 @@
2289 1450 wp_send_json($response_data);
2290 1451 wp_die();
2291 1452 }
2292 1453
1454 +
2293 1455 /**
2294 1456 * Get bot-specific options for multi-bot functionality
2295 1457 * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2296 1458 */
@@ -2295,12 +1457,12 @@
2295 1457 * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2296 1458 */
2297 1459 // Also debug the bot options retrieval
2298 1460 private function get_bot_options($bot_id = 'default') {
2299 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
1461 + error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2300 1462
2301 1463 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')");
1464 + error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2303 1465 return array();
2304 1466 }
2305 1467
2306 1468 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
@@ -2305,11 +1467,11 @@
2305 1467
2306 1468 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2307 1469
2308 1470 if (!empty($bot_options)) {
2309 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
1471 + error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2310 1472 if (isset($bot_options['similarity_threshold'])) {
2311 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
1473 + error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2312 1474 }
2313 1475 }
2314 1476
2315 1477 return is_array($bot_options) ? $bot_options : array();
@@ -2320,13 +1482,13 @@
2320 1482 * Used in the knowledge retrieval functions
2321 1483 */
2322 1484 // Also add debugging to your get_bot_pinecone_config function
2323 1485 private function get_bot_pinecone_config($bot_id = 'default') {
2324 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
1486 + error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2325 1487
2326 1488 // If default bot or multi-bot add-on not active, use default Pinecone config
2327 1489 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')");
1490 + error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2329 1491 $addon_options = get_option('mxchat_pinecone_addon_options', array());
2330 1492 $config = array(
2331 1493 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2332 1494 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
@@ -2332,24 +1494,24 @@
2332 1494 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2333 1495 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2334 1496 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2335 1497 );
2336 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
1498 + error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2337 1499 return $config;
2338 1500 }
2339 1501
2340 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
1502 + error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2341 1503
2342 1504 // Hook for multi-bot add-on to provide bot-specific Pinecone config
2343 1505 $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2344 1506
2345 1507 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'));
1508 + error_log("MXCHAT DEBUG: Got bot-specific config from filter");
1509 + error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
1510 + error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
1511 + error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2350 1512 } else {
2351 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
1513 + error_log("MXCHAT DEBUG: Filter returned empty config!");
2352 1514 }
2353 1515
2354 1516 return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2355 1517 }
@@ -2359,63 +1521,35 @@
2359 1521 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2360 1522 global $wpdb;
2361 1523 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2362 1524
2363 - // Get the current bot_id
1525 + // NEW: Get the current bot_id
2364 1526 $current_bot_id = $this->get_current_bot_id($session_id);
2365 1527
2366 1528 // Generate the user embedding
2367 1529 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2368 -
1530 +
2369 1531 // Check if embedding generation returned an error
2370 1532 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2371 1533 $error_message = $user_embedding['error'];
2372 1534 $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 - }
1535 +
1536 + wp_send_json_error([
1537 + 'error_message' => $error_message,
1538 + 'error_code' => $error_code
1539 + ]);
2391 1540 wp_die();
2392 1541 }
2393 -
1542 +
2394 1543 // Check if embedding is valid
2395 1544 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 - }
1545 + wp_send_json_error([
1546 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1547 + 'error_code' => 'invalid_embedding'
1548 + ]);
2415 1549 wp_die();
2416 1550 }
2417 -
1551 +
2418 1552 // Fetch intents from the database
2419 1553 $table_name = $wpdb->prefix . 'mxchat_intents';
2420 1554 if ($chat_mode === 'agent') {
2421 1555 $query = $wpdb->prepare(
@@ -2425,29 +1559,19 @@
2425 1559 $intents = $wpdb->get_results($query);
2426 1560 } else {
2427 1561 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2428 1562 }
2429 -
1563 +
2430 1564 if (empty($intents)) {
2431 1565 return false;
2432 1566 }
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 -
1567 +
2444 1568 $highest_similarity = -INF;
2445 1569 $matched_intent = null;
2446 -
1570 +
2447 1571 // Array to store action analysis for testing panel
2448 1572 $action_analysis = [];
2449 -
1573 +
2450 1574 foreach ($intents as $intent) {
2451 1575 // Additional check for enabled state
2452 1576 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 1577 if (!$is_enabled) {
@@ -2452,56 +1576,26 @@
2452 1576 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 1577 if (!$is_enabled) {
2454 1578 continue;
2455 1579 }
2456 -
2457 - // Check if this action is enabled for the current bot
1580 +
1581 + // NEW: Check if this action is enabled for the current bot
2458 1582 if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2459 1583 continue;
2460 1584 }
2461 -
2462 - $best_similarity = -INF;
2463 - $matched_phrase_text = '';
2464 -
2465 - // Check legacy embedding vector (existing behavior)
1585 +
2466 1586 $intent_embedding_serialized = $intent->embedding_vector;
2467 1587 $intent_embedding = $intent_embedding_serialized
2468 1588 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2469 1589 : 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) {
1590 +
1591 + if (!is_array($intent_embedding)) {
2498 1592 continue;
2499 1593 }
2500 -
2501 - $similarity = $best_similarity;
1594 +
1595 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2502 1596 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2503 -
1597 +
2504 1598 // Store action analysis data for testing panel
2505 1599 $action_analysis[] = [
2506 1600 'intent_label' => $intent->intent_label,
2507 1601 'callback_function' => $intent->callback_function,
@@ -2509,12 +1603,11 @@
2509 1603 'similarity_percentage' => round($similarity * 100, 2),
2510 1604 'threshold' => $intent_threshold,
2511 1605 'threshold_percentage' => round($intent_threshold * 100, 2),
2512 1606 'above_threshold' => $similarity >= $intent_threshold,
2513 - 'matched_phrase' => $matched_phrase_text,
2514 1607 'triggered' => false // Will be updated below if this intent is triggered
2515 1608 ];
2516 -
1609 +
2517 1610 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2518 1611 $highest_similarity = $similarity;
2519 1612 $matched_intent = $intent;
2520 1613 }
@@ -2585,22 +1678,16 @@
2585 1678 // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2586 1679 if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2587 1680 return true;
2588 1681 }
2589 -
1682 +
2590 1683 $enabled_bots = json_decode($intent->enabled_bots, true);
2591 -
1684 +
2592 1685 // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2593 1686 if (!is_array($enabled_bots) || empty($enabled_bots)) {
2594 1687 return true;
2595 1688 }
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 -
1689 +
2603 1690 // Check if the current bot is in the enabled bots list
2604 1691 return in_array($bot_id, $enabled_bots);
2605 1692 }
2606 1693
@@ -2638,23 +1725,18 @@
2638 1725 }
2639 1726
2640 1727 public function mxchat_generate_image($message, $user_id, $session_id) {
2641 1728 //error_log("Starting image generation for message: " . $message);
2642 -
2643 - // Prepare a prompt for OpenAI image generation
1729 +
1730 + // Prepare a prompt for DALL-E
2644 1731 $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 1732
1733 + // Use the existing OpenAI API key
1734 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1735 +
1736 + // Call DALL-E to generate an image
1737 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1738 +
2657 1739 // Check if the response contains an image URL
2658 1740 if (isset($image_response['imageUrl'])) {
2659 1741 $image_url = esc_url_raw($image_response['imageUrl']);
2660 1742
@@ -2697,103 +1779,24 @@
2697 1779 // Return the response directly instead of relying on the property
2698 1780 return $this->fallbackResponse;
2699 1781 }
2700 1782 }
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) {
1783 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2779 1784 $api_url = 'https://api.openai.com/v1/images/generations';
2780 1785 $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),
1786 + 'prompt' => sanitize_text_field($prompt),
1787 + 'n' => 1,
1788 + 'size' => '1024x1024',
1789 + 'model' => sanitize_text_field($model),
2787 1790 ]);
2788 1791
2789 1792 $args = [
2790 - 'body' => $body,
1793 + 'body' => $body,
2791 1794 'headers' => [
2792 - 'Content-Type' => 'application/json',
1795 + 'Content-Type' => 'application/json',
2793 1796 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2794 1797 ],
2795 - 'method' => 'POST',
1798 + 'method' => 'POST',
2796 1799 'timeout' => absint($timeout),
2797 1800 ];
2798 1801
2799 1802 $response = wp_remote_post($api_url, $args);
@@ -2798,114 +1801,23 @@
2798 1801
2799 1802 $response = wp_remote_post($api_url, $args);
2800 1803
2801 1804 if (is_wp_error($response)) {
1805 + //error_log("DALL-E request failed: " . $response->get_error_message());
2802 1806 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2803 1807 }
2804 1808
2805 1809 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2806 1810
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];
1811 + if (isset($response_body['data'][0]['url'])) {
1812 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2814 1813 } else {
1814 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2815 1815 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2816 1816 }
2817 1817 }
2818 1818
2819 1819 /**
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 1820 * Handle web search requests.
2909 1821 *
2910 1822 * Sends the refined search query to the Brave Search API and uses the
2911 1823 * results to generate a conversational response with the AI model.
@@ -2953,10 +1865,10 @@
2953 1865 $transient_key = 'mxchat_search_' . md5($refined_search_query);
2954 1866 $results = get_transient($transient_key);
2955 1867
2956 1868 if (false === $results) {
2957 - // SECURITY FIX: Changed to wp_safe_remote_get
2958 - $response = wp_safe_remote_get(
1869 + // Fetch new results from the Brave Search API
1870 + $response = wp_remote_get(
2959 1871 $api_url,
2960 1872 array(
2961 1873 'headers' => array(
2962 1874 'Accept' => 'application/json',
@@ -3095,10 +2007,9 @@
3095 2007 ],
3096 2008 'timeout' => 10,
3097 2009 ];
3098 2010
3099 - // SECURITY FIX: Changed to wp_safe_remote_get
3100 - $response = wp_safe_remote_get($api_url, $args);
2011 + $response = wp_remote_get($api_url, $args);
3101 2012
3102 2013 if (is_wp_error($response)) {
3103 2014 return array(
3104 2015 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
@@ -3168,22 +2079,17 @@
3168 2079 * @return string The refined search query
3169 2080 */
3170 2081 public function mxchat_interpret_search_query($user_query) {
3171 2082 $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3172 -
2083 +
3173 2084 // Get options and determine the selected model
3174 2085 $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 -
2086 + $selected_model = isset($options['model']) ? $options['model'] : 'gpt-4o';
2087 +
3182 2088 // Extract model prefix to determine the provider
3183 2089 $model_parts = explode('-', $selected_model);
3184 2090 $provider = strtolower($model_parts[0]);
3185 -
2091 +
3186 2092 // Determine which API key to use based on the provider
3187 2093 switch ($provider) {
3188 2094 case 'gemini':
3189 2095 $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
@@ -3224,60 +2130,11 @@
3224 2130 }
3225 2131 }
3226 2132
3227 2133 /**
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 2134 * Interpret query using OpenAI models
3278 2135 */
3279 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
2136 +private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-4o') {
3280 2137 $url = 'https://api.openai.com/v1/chat/completions';
3281 2138 $args = [
3282 2139 'headers' => [
3283 2140 'Authorization' => 'Bearer ' . $api_key,
@@ -3307,36 +2164,13 @@
3307 2164 : sanitize_text_field($user_query);
3308 2165 }
3309 2166
3310 2167 /**
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 2168 * Interpret query using Claude models
3324 2169 */
3325 2170 private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3326 2171 $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 -
2172 +
3339 2173 $args = [
3340 2174 'headers' => [
3341 2175 'Content-Type' => 'application/json',
3342 2176 'x-api-key' => $api_key,
@@ -3341,9 +2175,17 @@
3341 2175 'Content-Type' => 'application/json',
3342 2176 'x-api-key' => $api_key,
3343 2177 'anthropic-version' => '2023-06-01',
3344 2178 ],
3345 - 'body' => wp_json_encode($payload),
2179 + 'body' => wp_json_encode([
2180 + 'model' => $model,
2181 + 'system' => $system_prompt,
2182 + 'messages' => [
2183 + ['role' => 'user', 'content' => sanitize_text_field($user_query)]
2184 + ],
2185 + 'max_tokens' => 20,
2186 + 'temperature' => 0.2,
2187 + ]),
3346 2188 'method' => 'POST',
3347 2189 'timeout' => 15,
3348 2190 ];
3349 2191
@@ -3352,16 +2194,12 @@
3352 2194 return sanitize_text_field($user_query);
3353 2195 }
3354 2196
3355 2197 $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 - }
2198 + if (!empty($body['content'][0]['text'])) {
2199 + return sanitize_text_field(trim($body['content'][0]['text']));
3362 2200 }
3363 -
2201 +
3364 2202 return sanitize_text_field($user_query);
3365 2203 }
3366 2204
3367 2205 /**
@@ -3367,16 +2205,13 @@
3367 2205 /**
3368 2206 * Interpret query using Gemini models
3369 2207 */
3370 2208 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);
2209 + // Strip "gemini-" prefix for the API
2210 + $model_version = str_replace('gemini-', '', $model);
3378 2211
2212 + $url = "https://generativelanguage.googleapis.com/v1/models/$model_version:generateContent?key=" . urlencode($api_key);
2213 +
3379 2214 $args = [
3380 2215 'headers' => [
3381 2216 'Content-Type' => 'application/json',
3382 2217 ],
@@ -3572,9 +2407,9 @@
3572 2407 }
3573 2408
3574 2409
3575 2410 /**
3576 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
2411 + * Enhanced fetch_and_split_pdf_pages with detailed debugging
3577 2412 */
3578 2413 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3579 2414 // CLEAR DEBUG LOGGING
3580 2415 //error_log("=== MXCHAT PDF PROCESSING START ===");
@@ -3629,19 +2464,10 @@
3629 2464 // (I'll include the key parts with debug logging)
3630 2465
3631 2466 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3632 2467 //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 -
3640 2468 $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, [
2469 + $response = wp_remote_get($pdf_source, [
3644 2470 'timeout' => 60,
3645 2471 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3646 2472 ]);
3647 2473
@@ -3650,14 +2476,9 @@
3650 2476 //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3651 2477 return false;
3652 2478 }
3653 2479
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);
2480 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
3660 2481 //error_log("✅ PDF downloaded successfully");
3661 2482 } else {
3662 2483 $temp_file = $pdf_source;
3663 2484 //error_log("Using local PDF file: " . $temp_file);
@@ -3664,9 +2485,8 @@
3664 2485 }
3665 2486
3666 2487 // Parse PDF
3667 2488 //error_log("Parsing PDF with basic parser...");
3668 - mxchat_load_pdf_parser();
3669 2489 $parser = new \Smalot\PdfParser\Parser();
3670 2490 $pdf = $parser->parseFile($temp_file);
3671 2491 $pages = $pdf->getPages();
3672 2492
@@ -3729,33 +2549,8 @@
3729 2549 return false;
3730 2550 }
3731 2551 }
3732 2552
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 2553 private function mxchat_clean_text($text) {
3759 2554 // Remove excessive whitespace
3760 2555 $text = preg_replace('/\s+/', ' ', $text);
3761 2556
@@ -3794,14 +2589,11 @@
3794 2589 }
3795 2590
3796 2591 return [];
3797 2592 }
3798 -
3799 -
2593 +// Add this to your class
3800 2594 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 - }
2595 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3804 2596
3805 2597 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3806 2598 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 2599 return;
@@ -3806,29 +2598,12 @@
3806 2598 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 2599 return;
3808 2600 }
3809 2601
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 2602 $file = $_FILES['pdf_file'];
3820 2603 $session_id = sanitize_text_field($_POST['session_id']);
3821 2604 $original_filename = sanitize_text_field($file['name']);
3822 2605
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 2606 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3832 2607 if ($file_type['type'] !== 'application/pdf') {
3833 2608 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3834 2609 return;
@@ -3834,12 +2609,9 @@
3834 2609 return;
3835 2610 }
3836 2611
3837 2612 $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';
2613 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3842 2614 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3843 2615
3844 2616 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3845 2617 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3870,9 +2642,8 @@
3870 2642 return;
3871 2643 }
3872 2644
3873 2645 if (!empty($embeddings)) {
3874 - // Store the mapping between session and the random filename
3875 2646 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3876 2647 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3877 2648 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3878 2649 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3893,11 +2664,9 @@
3893 2664 wp_send_json_error($error_message);
3894 2665 return;
3895 2666 }
3896 2667 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 - }
2668 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3900 2669
3901 2670 if (empty($_POST['session_id'])) {
3902 2671 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
3903 2672 wp_die();
@@ -3918,8 +2687,10 @@
3918 2687 wp_die();
3919 2688 }
3920 2689
3921 2690
2691 +
2692 +
3922 2693 function mxchat_fetch_new_messages() {
3923 2694 $session_id = sanitize_text_field($_POST['session_id']);
3924 2695 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3925 2696 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -3932,31 +2703,14 @@
3932 2703 }
3933 2704
3934 2705 $history = get_option("mxchat_history_{$session_id}", []);
3935 2706
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 2707 $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 2708 // If persistence is enabled, show all new messages
3945 2709 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;
2710 + return !empty($message['id']) &&
2711 + strcmp($message['id'], $last_seen_id) > 0 &&
2712 + $message['role'] === 'agent';
3959 2713 }
3960 2714
3961 2715 // If persistence is disabled, only show messages after initial timestamp
3962 2716 return !empty($message['id']) &&
@@ -3963,16 +2717,12 @@
3963 2717 $message['role'] === 'agent' &&
3964 2718 $message['timestamp'] > $initial_timestamp;
3965 2719 });
3966 2720
3967 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2721 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
3968 2722
3969 - // Include current chat mode so frontend can detect agent→AI transitions
3970 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3971 -
3972 2723 wp_send_json_success([
3973 - 'new_messages' => array_values($new_messages),
3974 - 'chat_mode' => $chat_mode
2724 + 'new_messages' => array_values($new_messages)
3975 2725 ]);
3976 2726 wp_die();
3977 2727 }
3978 2728 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
@@ -4257,393 +3007,9 @@
4257 3007
4258 3008 //error_log("[DEBUG] Generated channel name: {$channel_name}");
4259 3009 return $channel_name;
4260 3010 }
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 3011 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 3012 $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4647 3013 $channel_id = get_option("mxchat_channel_{$session_id}", '');
4648 3014
4649 3015 if (empty($slack_bot_token) || empty($channel_id)) {
@@ -4869,33 +3235,33 @@
4869 3235
4870 3236 $channel_id = $event['channel'];
4871 3237 $message_text = $event['text'] ?? '';
4872 3238 $message_ts = $event['ts'] ?? '';
4873 -
3239 +
4874 3240 // Find session ID by looking for matching channel
4875 3241 global $wpdb;
4876 3242 $session_option = $wpdb->get_var(
4877 3243 $wpdb->prepare(
4878 - "SELECT option_name FROM {$wpdb->options}
4879 - WHERE option_name LIKE 'mxchat_channel_%'
3244 + "SELECT option_name FROM {$wpdb->options}
3245 + WHERE option_name LIKE 'mxchat_channel_%'
4880 3246 AND option_value = %s",
4881 3247 $channel_id
4882 3248 )
4883 3249 );
4884 -
3250 +
4885 3251 if ($session_option) {
4886 3252 $session_id = str_replace('mxchat_channel_', '', $session_option);
4887 -
3253 +
4888 3254 // Create a unique key for this specific message
4889 3255 $message_key = md5($session_id . $message_ts . $message_text);
4890 3256 $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4891 -
3257 +
4892 3258 // Check if we've already processed this exact message
4893 3259 if (in_array($message_key, $processed_messages)) {
4894 3260 //error_log("Duplicate message detected for session $session_id");
4895 3261 return new WP_REST_Response(['ok' => true]);
4896 3262 }
4897 -
3263 +
4898 3264 // Add to processed messages
4899 3265 $processed_messages[] = $message_key;
4900 3266 // Keep only last 50 messages per session
4901 3267 if (count($processed_messages) > 50) {
@@ -4901,46 +3267,14 @@
4901 3267 if (count($processed_messages) > 50) {
4902 3268 $processed_messages = array_slice($processed_messages, -50);
4903 3269 }
4904 3270 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 -
3271 +
4939 3272 // Save the agent message
4940 3273 $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4941 -
3274 +
4942 3275 // Send confirmation back to Slack (only once)
3276 + $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4943 3277 if (!empty($slack_bot_token)) {
4944 3278 // Use a transient to prevent duplicate confirmations
4945 3279 $confirm_key = 'mxchat_confirm_' . $message_key;
4946 3280 if (!get_transient($confirm_key)) {
@@ -4950,9 +3284,9 @@
4950 3284 'Authorization' => 'Bearer ' . $slack_bot_token
4951 3285 ],
4952 3286 'body' => json_encode([
4953 3287 'channel' => $channel_id,
4954 - 'text' => "✅ _Message sent to user_",
3288 + 'text' => "✅ _Message sent to user_",
4955 3289 'thread_ts' => $event['ts'] // Reply in thread
4956 3290 ])
4957 3291 ]);
4958 3292 // Set transient to prevent duplicate confirmations
@@ -4992,15 +3326,9 @@
4992 3326 try {
4993 3327 // Get options and selected model
4994 3328 $options = get_option('mxchat_options');
4995 3329 $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 -
3330 +
5003 3331 // Determine endpoint and API key based on model
5004 3332 if (strpos($selected_model, 'voyage') === 0) {
5005 3333 $endpoint = 'https://api.voyageai.com/v1/embeddings';
5006 3334 $api_key = $options['voyage_api_key'] ?? '';
@@ -5197,97 +3525,26 @@
5197 3525 }
5198 3526 }
5199 3527
5200 3528
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 - }
5214 -
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'];
5223 -
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 -
3529 +private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default') {
3530 + error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
3531 +
5275 3532 // Get bot-specific Pinecone configuration
5276 3533 $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5277 -
3534 +
5278 3535 // 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 -
3536 + error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
3537 + error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
3538 + error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
3539 + error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
3540 + error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
3541 +
5285 3542 // Determine whether to use Pinecone based on bot configuration
5286 3543 $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
3544 +
3545 + error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5287 3546
5288 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5289 -
5290 3547 if ($use_pinecone) {
5291 3548 return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
5292 3549 } else {
5293 3550 return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
@@ -5296,483 +3553,233 @@
5296 3553
5297 3554 private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
5298 3555 global $wpdb;
5299 3556 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3557 + $cache_key = 'mxchat_system_prompt_embeddings_' . $bot_id; // Bot-specific cache key
3558 + $batch_size = 500;
3559 +
5300 3560 // Initialize similarity analysis storage
5301 3561 $this->last_similarity_analysis = [
5302 3562 'knowledge_base_type' => 'WordPress Database',
5303 - 'bot_id' => $bot_id,
3563 + 'bot_id' => $bot_id, // Track which bot is being used
5304 3564 'top_matches' => [],
5305 3565 'threshold_used' => 0,
5306 3566 'total_checked' => 0
5307 3567 ];
5308 3568
5309 - // NEW: Initialize valid URLs array
5310 - $valid_urls = [];
5311 -
5312 - // Get bot-specific options for similarity threshold
3569 + // Get bot-specific options for similarity threshold
5313 3570 $bot_options = $this->get_bot_options($bot_id);
5314 3571 $current_options = !empty($bot_options) ? $bot_options : $this->options;
5315 3572
5316 - // Get knowledge manager instance for role checking
5317 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3573 + // Retrieve embeddings from cache or database
3574 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3575 + if ($embeddings === false) {
3576 + // Cache miss - load embeddings from database WITH CONTENT and ROLE RESTRICTION for testing
3577 + $embeddings = [];
3578 + $offset = 0;
5318 3579
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;
3580 + do {
3581 + // Add bot_id filter if not default and if bot_metadata column exists
3582 + $bot_filter = '';
3583 + if ($bot_id !== 'default') {
3584 + // Check if bot_metadata column exists
3585 + $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
3586 + if ($column_exists) {
3587 + $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
3588 + }
3589 + }
5324 3590
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);
5331 - }
5332 - }
3591 + $query = $wpdb->prepare(
3592 + "SELECT id, embedding_vector, article_content, source_url, role_restriction
3593 + FROM {$system_prompt_table}
3594 + WHERE 1=1 {$bot_filter}
3595 + LIMIT %d OFFSET %d",
3596 + $batch_size,
3597 + $offset
3598 + );
5333 3599
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;
3600 + $batch = $wpdb->get_results($query);
3601 + if (empty($batch)) {
3602 + break;
3603 + }
5346 3604
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 - ));
3605 + $embeddings = array_merge($embeddings, $batch);
3606 + $offset += $batch_size;
3607 + unset($batch);
3608 + } while (true);
5356 3609
5357 - if (empty($batch)) {
5358 - break;
3610 + if (empty($embeddings)) {
3611 + return '';
5359 3612 }
3613 +
3614 + // Cache embeddings for future use (but note: this now includes content and role restrictions)
3615 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3616 + }
5360 3617
5361 - foreach ($batch as $row) {
5362 - $database_embedding = $row->embedding_vector
5363 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5364 - : null;
3618 + // Get knowledge manager instance for role checking
3619 + $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5365 3620
5366 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5367 - unset($database_embedding);
5368 - continue;
5369 - }
5370 -
3621 + // Get base similarity threshold from bot options or default options
3622 + $similarity_threshold = isset($current_options['similarity_threshold'])
3623 + ? ((int) $current_options['similarity_threshold']) / 100
3624 + : 0.35;
3625 +
3626 + $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3627 +
3628 + // Calculate similarities and build results array
3629 + $all_similarities = [];
3630 + $relevant_results = [];
3631 +
3632 + foreach ($embeddings as $embedding) {
3633 + $database_embedding = $embedding->embedding_vector
3634 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3635 + : null;
3636 +
3637 + if (is_array($database_embedding) && is_array($user_embedding)) {
5371 3638 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5372 - unset($database_embedding);
5373 -
5374 - $role_restriction = $row->role_restriction ?? 'public';
3639 +
3640 + // Check role access
3641 + $role_restriction = $embedding->role_restriction ?? 'public';
5375 3642 $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 - });
3643 +
3644 + // Store ALL similarities for testing (top 10)
3645 + $source_display = '';
3646 + if (!empty($embedding->source_url) && $embedding->source_url !== '#') {
3647 + $source_display = $embedding->source_url;
3648 + } else {
3649 + $content_preview = strip_tags($embedding->article_content ?? '');
3650 + $content_preview = preg_replace('/\s+/', ' ', $content_preview);
3651 + $source_display = substr(trim($content_preview), 0, 50) . '...';
5401 3652 }
5402 -
5403 - // Track candidates for context assembly (above threshold + has access)
3653 +
3654 + $all_similarities[] = [
3655 + 'document_id' => $embedding->id,
3656 + 'similarity' => $similarity,
3657 + 'similarity_percentage' => round($similarity * 100, 2),
3658 + 'above_threshold' => $similarity >= $similarity_threshold,
3659 + 'source_display' => $source_display,
3660 + 'content_preview' => substr(strip_tags($embedding->article_content ?? ''), 0, 100) . '...',
3661 + 'used_for_context' => false, // Initialize as false, we'll update this later
3662 + 'role_restriction' => $role_restriction, // Include role info for testing
3663 + 'has_access' => $has_access, // Include access info for testing
3664 + 'filtered_out' => !$has_access // Mark if filtered out by role
3665 + ];
3666 +
3667 + // Only consider results above threshold AND with access for actual content retrieval
5404 3668 if ($similarity >= $similarity_threshold && $has_access) {
5405 - $candidates[] = [
5406 - 'id' => $row->id,
5407 - 'similarity' => $similarity,
5408 - 'source_url' => $source_url,
3669 + $relevant_results[] = [
3670 + 'id' => $embedding->id,
3671 + 'similarity' => $similarity
5409 3672 ];
5410 3673 }
5411 -
5412 - $total_checked++;
5413 3674 }
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 '';
3675 +
3676 + unset($database_embedding);
5431 3677 }
5432 3678
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 3679 // Sort ALL similarities for testing display (highest first)
5543 3680 usort($all_similarities, function ($a, $b) {
5544 3681 return $b['similarity'] <=> $a['similarity'];
5545 3682 });
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'];
3683 +
3684 + // Sort relevant results by similarity (highest first)
3685 + usort($relevant_results, function ($a, $b) {
3686 + return $b['similarity'] <=> $a['similarity'];
5550 3687 });
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
3688 +
3689 + // Get top 5 results for actual content (standard approach)
3690 + $top_results = array_slice($relevant_results, 0, 5);
3691 +
3692 + // NOW mark which documents are actually used for context
5561 3693 $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 - }
3694 + foreach ($top_results as $result) {
3695 + $used_document_ids[] = $result['id'];
5570 3696 }
5571 -
3697 +
5572 3698 // Update the all_similarities array to mark which were actually used
5573 3699 foreach ($all_similarities as &$similarity_item) {
5574 3700 $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5575 3701 }
5576 -
5577 - // Store top 10 for testing panel
3702 +
3703 + // Store top 10 for testing panel (now with correct used_for_context flags and role info)
5578 3704 $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5579 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5580 -
3705 + $this->last_similarity_analysis['total_checked'] = count($embeddings);
3706 +
3707 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " top matches for testing");
3708 +
5581 3709 // Initialize final content
5582 3710 $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;
3711 +
3712 + // Track document IDs to avoid duplicates
3713 + $added_document_ids = [];
3714 +
3715 + // Fetch and format content for each selected result
3716 + foreach ($top_results as $index => $result) {
3717 + if (in_array($result['id'], $added_document_ids)) {
3718 + continue;
5602 3719 }
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++;
3720 +
3721 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3722 + $added_document_ids[] = $result['id'];
3723 +
3724 + $content .= "## Reference " . ($index + 1) . " ##\n";
3725 + $content .= $chunk_content . "\n\n";
3726 +
3727 + // PDF surrounding pages logic (unchanged)
3728 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
3729 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
3730 + "SELECT id, article_content, role_restriction FROM {$system_prompt_table}
3731 + WHERE id IN (
3732 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
3733 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
3734 + )",
3735 + $result['id'],
3736 + $result['id']
3737 + ));
3738 +
3739 + // Check role access for surrounding content too
3740 + if (!empty($surrounding_content[0])) {
3741 + $surrounding_role = $surrounding_content[0]->role_restriction ?? 'public';
3742 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3743 + $content .= "## Related Content ##\n";
3744 + $content .= $surrounding_content[0]->article_content . "\n\n";
3745 + $added_document_ids[] = $surrounding_content[0]->id;
5629 3746 }
5630 - $full_text = implode("\n\n", $chunk_texts);
5631 3747 }
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
5642 - }
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";
3748 +
3749 + if (!empty($surrounding_content[1])) {
3750 + $surrounding_role = $surrounding_content[1]->role_restriction ?? 'public';
3751 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3752 + $content .= "## Related Content ##\n";
3753 + $content .= $surrounding_content[1]->article_content . "\n\n";
3754 + $added_document_ids[] = $surrounding_content[1]->id;
5655 3755 }
5656 - } else {
5657 - // Manual entry — no reference number, no citation
5658 - $content .= "## Information ##\n";
5659 - $content .= $full_text . "\n\n";
5660 3756 }
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 3757 }
5676 3758 }
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 -
3759 +
5688 3760 // Add response guidelines
5689 - if (empty($top_urls)) {
3761 + if (empty($top_results)) {
5690 3762 $content = "No reference information was found for this query.\n\n";
5691 3763 } else {
5692 - // Build response guidelines based on citation links setting
5693 3764 $content .= "\n## Response Guidelines ##\n" .
5694 3765 "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5695 3766 "Be conversational and friendly, but never mention your knowledge base or training data. " .
5696 3767 "If you don't have specific information or are uncertain about any details, it's always " .
5697 3768 "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.";
5705 - } 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.";
5708 - }
3769 + "When information is incomplete, let them know you are unsure.";
5709 3770 }
5710 3771
5711 3772 return trim($content);
5712 3773 }
5713 3774
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 3775 private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5769 3776 global $wpdb;
5770 3777
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'));
3778 + error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
3779 + error_log(" - bot_id: " . $bot_id);
3780 + error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
3781 + error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5775 3782
5776 3783 // Use bot-specific config or fall back to default
5777 3784 if ($bot_config === null) {
5778 3785 $bot_config = $this->get_bot_pinecone_config($bot_id);
@@ -5781,12 +3788,12 @@
5781 3788 $api_key = $bot_config['api_key'] ?? '';
5782 3789 $host = $bot_config['host'] ?? '';
5783 3790 $namespace = $bot_config['namespace'] ?? '';
5784 3791
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));
3792 + error_log("MXCHAT DEBUG: Pinecone query parameters:");
3793 + error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
3794 + error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
3795 + error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5789 3796
5790 3797 // Initialize similarity analysis storage
5791 3798 $this->last_similarity_analysis = [
5792 3799 'knowledge_base_type' => 'Pinecone',
@@ -5796,17 +3803,12 @@
5796 3803 'threshold_used' => 0,
5797 3804 'total_checked' => 0
5798 3805 ];
5799 3806
5800 - // NEW: Initialize valid URLs array
5801 - $valid_urls = [];
5802 -
5803 3807 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 = [];
3808 + error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
3809 + error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
3810 + error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5809 3811 return '';
5810 3812 }
5811 3813
5812 3814 // Get knowledge manager instance for role checking
@@ -5826,9 +3828,9 @@
5826 3828 $api_endpoint = "https://{$host}/query";
5827 3829
5828 3830 $request_body = array(
5829 3831 'vector' => $user_embedding,
5830 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
3832 + 'topK' => 20, // Request more to get good testing data
5831 3833 'includeMetadata' => true,
5832 3834 'includeValues' => true
5833 3835 );
5834 3836
@@ -5836,11 +3838,11 @@
5836 3838 if (!empty($namespace)) {
5837 3839 $request_body['namespace'] = $namespace;
5838 3840 }
5839 3841
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'));
3842 + error_log("MXCHAT DEBUG: About to call Pinecone API");
3843 + error_log(" - Endpoint: " . $api_endpoint);
3844 + error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5843 3845
5844 3846 $response = wp_remote_post($api_endpoint, array(
5845 3847 'headers' => array(
5846 3848 'Api-Key' => $api_key,
@@ -5851,61 +3853,53 @@
5851 3853 'timeout' => 30
5852 3854 ));
5853 3855
5854 3856 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 = [];
3857 + error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5858 3858 return '';
5859 3859 }
5860 3860
5861 3861 $response_code = wp_remote_retrieve_response_code($response);
5862 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
3862 + error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5863 3863
5864 3864 if ($response_code !== 200) {
5865 3865 $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 = [];
3866 + error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5869 3867 return '';
5870 3868 }
5871 3869
5872 3870 // ADD DETAILED DEBUG SECTION HERE
5873 3871 $response_body = wp_remote_retrieve_body($response);
5874 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
3872 + error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5875 3873
5876 3874 $results = json_decode($response_body, true);
5877 3875
5878 3876 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 = [];
3877 + error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
3878 + error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5883 3879 return '';
5884 3880 }
5885 3881
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'));
3882 + error_log("MXCHAT DEBUG: Pinecone response structure:");
3883 + error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
3884 + error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5889 3885
5890 3886 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 = [];
3887 + error_log("MXCHAT DEBUG: No matches found in Pinecone response");
3888 + error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5895 3889 return '';
5896 3890 }
5897 3891
5898 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
3892 + error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
5899 3893
5900 3894 // Log first match details for debugging
5901 3895 if (!empty($results['matches'][0])) {
5902 3896 $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'));
3897 + error_log("MXCHAT DEBUG: First match details:");
3898 + error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
3899 + error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5906 3900 if (isset($first_match['metadata'])) {
5907 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
3901 + error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5908 3902 }
5909 3903 }
5910 3904
5911 3905 // Initialize the final content
@@ -5911,182 +3905,45 @@
5911 3905 // Initialize the final content
5912 3906 $content = '';
5913 3907 $matches_used = 0;
5914 3908 $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 -
3909 +
3910 + // Process each match for actual content generation (lazy role checking)
5929 3911 foreach ($results['matches'] as $index => $match) {
5930 3912 // Skip if similarity is below threshold
5931 3913 if ($match['score'] < $similarity_threshold) {
5932 3914 continue;
5933 3915 }
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) {
3916 +
3917 + // Limit to top 5 matches above threshold
3918 + if ($matches_used >= 5) {
6012 3919 break;
6013 3920 }
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);
3921 +
3922 + if (!empty($match['metadata']['text'])) {
3923 + // LAZY ROLE CHECK: Only check role for content we're actually considering
3924 + $match_id = $match['id'] ?? '';
3925 + $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
3926 + $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
3927 +
3928 + // Skip if user doesn't have access
3929 + if (!$has_access) {
3930 + continue;
6042 3931 }
6043 - } else {
6044 - $full_text = $group['single_text'];
6045 - $chunks_in_this_source = 1;
6046 - }
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
3932 +
3933 + // User has access - add to content
3934 + $content .= "## Reference " . ($matches_used + 1) . " ##\n";
3935 + $content .= $match['metadata']['text'] . "\n\n";
3936 +
3937 + if (!empty($match['metadata']['source_url'])) {
3938 + $content .= "URL: " . $match['metadata']['source_url'] . "\n\n";
6053 3939 }
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;
3940 +
3941 + $matches_used_for_context[] = $match['id'] ?? $index;
3942 + $matches_used++;
6086 3943 }
6087 3944 }
6088 -
3945 +
6089 3946 // Process ALL matches for testing data (top 10) - with role checking for testing display
6090 3947 $all_matches = [];
6091 3948 foreach ($results['matches'] as $index => $match) {
6092 3949 if ($index >= 10) break; // Limit to top 10 for testing
@@ -6106,19 +3963,9 @@
6106 3963 $source_display = substr(trim($content_preview), 0, 50) . '...';
6107 3964 }
6108 3965
6109 3966 $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 -
3967 +
6121 3968 $all_matches[] = [
6122 3969 'document_id' => $match_id_for_display,
6123 3970 'similarity' => $match['score'],
6124 3971 'similarity_percentage' => round($match['score'] * 100, 2),
@@ -6127,12 +3974,9 @@
6127 3974 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6128 3975 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6129 3976 'role_restriction' => $role_restriction,
6130 3977 'has_access' => $has_access,
6131 - 'filtered_out' => !$has_access,
6132 - 'is_chunk' => $is_chunk,
6133 - 'chunk_index' => $chunk_index,
6134 - 'total_chunks' => $total_chunks
3978 + 'filtered_out' => !$has_access
6135 3979 ];
6136 3980 }
6137 3981
6138 3982 // Store for testing panel
@@ -6137,43 +3981,25 @@
6137 3981
6138 3982 // Store for testing panel
6139 3983 $this->last_similarity_analysis['top_matches'] = $all_matches;
6140 3984 $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 -
3985 +
6150 3986 // Add response guidelines
6151 3987 if ($matches_used === 0) {
6152 3988 $content = "No reference information was found for this query.\n\n";
6153 3989 } else {
6154 - // Build response guidelines based on citation links setting
6155 3990 $content .= "\n## Response Guidelines ##\n" .
6156 3991 "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6157 3992 "Be conversational and friendly, but never mention your knowledge base or training data. " .
6158 3993 "If you don't have specific information or are uncertain about any details, it's always " .
6159 3994 "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 - }
3995 + "When information is incomplete, let them know you are unsure.";
6171 3996 }
6172 -
3997 +
6173 3998 return trim($content);
6174 3999 }
6175 4000
4001 +
6176 4002 /**
6177 4003 * Get role restriction for a single vector (with caching)
6178 4004 */
6179 4005 private function get_single_vector_role($vector_id, $metadata = array()) {
@@ -6210,518 +4036,12 @@
6210 4036 }
6211 4037
6212 4038 // Cache individual role for 1 hour
6213 4039 wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6214 -
4040 +
6215 4041 return $role_restriction;
6216 4042 }
6217 4043
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 - }
6573 - }
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 - }
6629 - }
6630 - }
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 -
6653 - // Add response guidelines
6654 - if ($matches_used === 0) {
6655 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6656 - $content = "No reference information was found for this query.\n\n";
6657 - } 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 - }
6674 - }
6675 -
6676 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6677 -
6678 - return trim($content);
6679 -}
6680 -
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 4044 private function mxchat_find_relevant_products($user_embedding) {
6725 4045 //error_log('MXChat Vector Search: Starting product search...');
6726 4046
6727 4047 // Retrieve the add-on settings from the database
@@ -6742,75 +4062,73 @@
6742 4062 }
6743 4063 private function find_relevant_products_wordpress($user_embedding) {
6744 4064 global $wpdb;
6745 4065 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
4066 + $cache_key = 'mxchat_system_prompt_embeddings';
4067 + $batch_size = 500;
6746 4068
6747 - if (!is_array($user_embedding)) {
6748 - return '';
6749 - }
4069 + // Original WordPress database search logic
4070 + // [Previous implementation remains the same]
4071 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
4072 + if ($embeddings === false) {
4073 + $embeddings = [];
4074 + $offset = 0;
6750 4075
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;
4076 + do {
4077 + $query = $wpdb->prepare(
4078 + "SELECT id, embedding_vector
4079 + FROM {$system_prompt_table}
4080 + LIMIT %d OFFSET %d",
4081 + $batch_size,
4082 + $offset
4083 + );
6759 4084
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 - ));
4085 + $batch = $wpdb->get_results($query);
4086 + if (empty($batch)) {
4087 + break;
4088 + }
6768 4089
6769 - if (empty($batch)) {
6770 - break;
6771 - }
4090 + $embeddings = array_merge($embeddings, $batch);
4091 + $offset += $batch_size;
6772 4092
6773 - foreach ($batch as $row) {
6774 - $database_embedding = $row->embedding_vector
6775 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6776 - : null;
4093 + unset($batch);
6777 4094
6778 - if (!is_array($database_embedding)) {
6779 - unset($database_embedding);
6780 - continue;
6781 - }
4095 + } while (true);
6782 4096
4097 + if (empty($embeddings)) {
4098 + return '';
4099 + }
4100 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
4101 + }
4102 +
4103 + $relevant_results = [];
4104 + foreach ($embeddings as $embedding) {
4105 + $database_embedding = $embedding->embedding_vector
4106 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
4107 + : null;
4108 + if (is_array($database_embedding) && is_array($user_embedding)) {
6783 4109 $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 - }
4110 + $relevant_results[] = [
4111 + 'id' => $embedding->id,
4112 + 'similarity' => $similarity
4113 + ];
6802 4114 }
4115 + unset($database_embedding);
4116 + }
6803 4117
6804 - unset($batch);
6805 - $offset += $batch_size;
6806 - } while (true);
4118 + // Use fixed threshold for products
4119 + $similarity_threshold = 0.85;
6807 4120
6808 - if (empty($top_results)) {
6809 - return '';
6810 - }
4121 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
4122 + return $result['similarity'] >= $similarity_threshold;
4123 + });
4124 + usort($relevant_results, function ($a, $b) {
4125 + return $b['similarity'] <=> $a['similarity'];
4126 + });
6811 4127
4128 + $top_results = array_slice($relevant_results, 0, 5);
6812 4129 $content = '';
4130 +
6813 4131 foreach ($top_results as $result) {
6814 4132 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6815 4133 $content .= $chunk_content . "\n\n";
6816 4134 }
@@ -6919,61 +4237,23 @@
6919 4237
6920 4238 /**
6921 4239 * Get system instructions for a specific bot or default
6922 4240 * 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
6928 4241 */
6929 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6930 - $instructions = '';
6931 -
4242 +private function get_system_instructions($bot_id = 'default') {
6932 4243 // Check if multi-bot add-on is active
6933 4244 if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6934 4245 // Get bot-specific options from multi-bot add-on
6935 4246 $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6936 -
4247 +
6937 4248 // If bot has custom system instructions, use those
6938 4249 if (!empty($bot_options['system_prompt_instructions'])) {
6939 - $instructions = $bot_options['system_prompt_instructions'];
4250 + return $bot_options['system_prompt_instructions'];
6940 4251 }
6941 4252 }
6942 -
4253 +
6943 4254 // 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;
4255 + return isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6976 4256 }
6977 4257 /**
6978 4258 * Get the current bot ID from session or request context
6979 4259 */
@@ -6993,9 +4273,9 @@
6993 4273
6994 4274 // Fall back to default
6995 4275 return 'default';
6996 4276 }
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') {
4277 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-4o') {
6998 4278 try {
6999 4279 if (!$relevant_content) {
7000 4280 $error_response = [
7001 4281 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
@@ -7001,73 +4281,22 @@
7001 4281 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7002 4282 'error_code' => 'no_relevant_content'
7003 4283 ];
7004 4284
4285 + // Add testing data to error response if available
7005 4286 if ($testing_data !== null) {
7006 4287 $error_response['testing_data'] = $testing_data;
4288 + //error_log("MxChat Testing: Added testing data to no_relevant_content error");
7007 4289 }
7008 4290
7009 4291 return $error_response;
7010 4292 }
7011 4293
4294 + // Ensure conversation_history is an array
7012 4295 if (!is_array($conversation_history)) {
7013 4296 $conversation_history = array();
7014 4297 }
7015 4298
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 4299
7071 4300 // Extract model prefix to determine the provider
7072 4301 $model_parts = explode('-', $selected_model);
7073 4302 $provider = strtolower($model_parts[0]);
@@ -7110,9 +4339,9 @@
7110 4339 $claude_api_key,
7111 4340 $conversation_history,
7112 4341 $relevant_content,
7113 4342 $session_id,
7114 - $testing_data
4343 + $testing_data // Pass testing data
7115 4344 );
7116 4345 } else {
7117 4346 $response = $this->mxchat_generate_response_claude(
7118 4347 $selected_model,
@@ -7140,9 +4369,9 @@
7140 4369 $xai_api_key,
7141 4370 $conversation_history,
7142 4371 $relevant_content,
7143 4372 $session_id,
7144 - $testing_data
4373 + $testing_data // Pass testing data
7145 4374 );
7146 4375 } else {
7147 4376 $response = $this->mxchat_generate_response_xai(
7148 4377 $selected_model,
@@ -7170,9 +4399,9 @@
7170 4399 $deepseek_api_key,
7171 4400 $conversation_history,
7172 4401 $relevant_content,
7173 4402 $session_id,
7174 - $testing_data
4403 + $testing_data // Pass testing data
7175 4404 );
7176 4405 } else {
7177 4406 $response = $this->mxchat_generate_response_deepseek(
7178 4407 $selected_model,
@@ -7182,38 +4411,8 @@
7182 4411 );
7183 4412 }
7184 4413 break;
7185 4414
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 4415 case 'gpt':
7217 4416 case 'o1':
7218 4417 if (empty($api_key)) {
7219 4418 $error_response = [
@@ -7224,27 +4423,9 @@
7224 4423 $error_response['testing_data'] = $testing_data;
7225 4424 }
7226 4425 return $error_response;
7227 4426 }
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) {
4427 + if ($streaming) {
7247 4428 return $this->mxchat_generate_response_openai_stream(
7248 4429 $selected_model,
7249 4430 $api_key,
7250 4431 $conversation_history,
@@ -7249,9 +4430,9 @@
7249 4430 $api_key,
7250 4431 $conversation_history,
7251 4432 $relevant_content,
7252 4433 $session_id,
7253 - $testing_data
4434 + $testing_data // Pass testing data
7254 4435 );
7255 4436 } else {
7256 4437 $response = $this->mxchat_generate_response_openai(
7257 4438 $selected_model,
@@ -7262,8 +4443,9 @@
7262 4443 }
7263 4444 break;
7264 4445
7265 4446 default:
4447 + // Default to OpenAI for custom models or unrecognized prefixes
7266 4448 if (empty($api_key)) {
7267 4449 $error_response = [
7268 4450 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7269 4451 'error_code' => 'missing_openai_api_key'
@@ -7272,25 +4454,9 @@
7272 4454 $error_response['testing_data'] = $testing_data;
7273 4455 }
7274 4456 return $error_response;
7275 4457 }
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) {
4458 + if ($streaming) {
7293 4459 return $this->mxchat_generate_response_openai_stream(
7294 4460 $selected_model,
7295 4461 $api_key,
7296 4462 $conversation_history,
@@ -7295,9 +4461,9 @@
7295 4461 $api_key,
7296 4462 $conversation_history,
7297 4463 $relevant_content,
7298 4464 $session_id,
7299 - $testing_data
4465 + $testing_data // Pass testing data
7300 4466 );
7301 4467 } else {
7302 4468 $response = $this->mxchat_generate_response_openai(
7303 4469 $selected_model,
@@ -7308,18 +4474,24 @@
7308 4474 }
7309 4475 break;
7310 4476 }
7311 4477
4478 + // Check if the response is an error array from the provider-specific function
7312 4479 if (is_array($response) && isset($response['error'])) {
4480 + // Add testing data to error response if available
7313 4481 if ($testing_data !== null) {
7314 4482 $response['testing_data'] = $testing_data;
4483 + //error_log("MxChat Testing: Added testing data to provider error response");
7315 4484 }
7316 - return $response;
4485 + return $response; // Pass through the error with testing data
7317 4486 }
7318 4487
4488 + // For successful non-streaming responses, we don't add testing data here
4489 + // because it will be added in the main handler
7319 4490 return $response;
7320 4491
7321 4492 } catch (Exception $e) {
4493 + //error_log('MXChat Error: ' . $e->getMessage());
7322 4494 $error_response = [
7323 4495 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7324 4496 'error_code' => 'system_exception',
7325 4497 'exception_details' => $e->getMessage()
@@ -7324,24 +4496,31 @@
7324 4496 'error_code' => 'system_exception',
7325 4497 'exception_details' => $e->getMessage()
7326 4498 ];
7327 4499
4500 + // Add testing data to exception response if available
7328 4501 if ($testing_data !== null) {
7329 4502 $error_response['testing_data'] = $testing_data;
4503 + //error_log("MxChat Testing: Added testing data to exception response");
7330 4504 }
7331 4505
7332 4506 return $error_response;
7333 4507 }
7334 4508 }
7335 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4509 +
4510 +private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7336 4511 try {
7337 4512 $bot_id = $this->get_current_bot_id($session_id);
7338 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7339 4513
4514 + // Get system prompt instructions using centralized function
4515 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
4516 +
4517 + // Ensure conversation_history is an array
7340 4518 if (!is_array($conversation_history)) {
7341 4519 $conversation_history = array();
7342 4520 }
7343 4521
4522 + // Format conversation history for OpenAI
7344 4523 $formatted_conversation = array();
7345 4524
7346 4525 $formatted_conversation[] = array(
7347 4526 'role' => 'system',
@@ -7353,9 +4532,9 @@
7353 4532 $role = $message['role'];
7354 4533 if ($role === 'bot' || $role === 'agent') {
7355 4534 $role = 'assistant';
7356 4535 }
7357 - if (!in_array($role, ['system', 'assistant', 'user'])) {
4536 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7358 4537 $role = 'user';
7359 4538 }
7360 4539 $formatted_conversation[] = array(
7361 4540 'role' => $role,
@@ -7363,21 +4542,18 @@
7363 4542 );
7364 4543 }
7365 4544 }
7366 4545
4546 + // Check if we can actually stream
7367 4547 if (headers_sent() || !function_exists('curl_init')) {
7368 - $regular_response = $this->mxchat_generate_response_openrouter(
4548 + // Fallback to regular response with testing data
4549 + $regular_response = $this->mxchat_generate_response_openai(
7369 4550 $selected_model,
7370 - $openrouter_api_key,
4551 + $api_key,
7371 4552 $conversation_history,
7372 4553 $relevant_content
7373 4554 );
7374 4555
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 4556 $response_data = [
7381 4557 'text' => $regular_response,
7382 4558 'html' => '',
7383 4559 'session_id' => $session_id
@@ -7391,8 +4567,9 @@
7391 4567 echo json_encode($response_data);
7392 4568 return true;
7393 4569 }
7394 4570
4571 + // Prepare the request body with stream: true
7395 4572 $body = json_encode([
7396 4573 'model' => $selected_model,
7397 4574 'messages' => $formatted_conversation,
7398 4575 'temperature' => 1,
@@ -7398,213 +4575,85 @@
7398 4575 'temperature' => 1,
7399 4576 'stream' => true
7400 4577 ]);
7401 4578
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 = '';
4579 + // Use cURL for streaming support
4580 + $ch = curl_init();
4581 + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
4582 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4583 + curl_setopt($ch, CURLOPT_POST, true);
4584 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4585 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4586 + 'Content-Type: application/json',
4587 + 'Authorization: Bearer ' . $api_key
4588 + ));
4589 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4590 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4591 +
4592 + $full_response = ''; // Accumulate full response for saving
7408 4593 $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);
4594 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
4595 +
4596 + // Buffer control for real-time streaming
4597 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
4598 + // Send testing data as the first event if available
4599 + if (!$stream_started && $testing_data !== null) {
4600 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4601 + flush();
4602 + $stream_started = true;
7419 4603 }
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];
4604 +
4605 + // CRITICAL FIX: Append new data to buffer
4606 + $buffer .= $data;
4607 +
4608 + // Process complete lines only
4609 + $lines = explode("\n", $buffer);
4610 +
4611 + // CRITICAL FIX: Keep the last incomplete line in the buffer
4612 + // The last element might be incomplete, so keep it in buffer
4613 + $buffer = array_pop($lines);
4614 +
4615 + foreach ($lines as $line) {
4616 + // Skip empty lines
4617 + if (trim($line) === '') {
4618 + continue;
7444 4619 }
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);
4620 +
4621 + // Only process lines that start with "data: "
4622 + if (strpos($line, 'data: ') !== 0) {
4623 + continue;
7452 4624 }
7453 -
7454 - if (!$this->streaming_headers_sent) {
7455 - $this->setup_streaming_headers();
4625 +
4626 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4627 +
4628 + if (trim($json_str) === '[DONE]') {
4629 + echo "data: [DONE]\n\n";
4630 + flush();
4631 + continue;
7456 4632 }
7457 -
7458 - if (!$stream_started && $testing_data !== null) {
7459 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4633 +
4634 + // Try to decode JSON
4635 + $json = json_decode(trim($json_str), true);
4636 + if ($json && isset($json['choices'][0]['delta']['content'])) {
4637 + $content = $json['choices'][0]['delta']['content'];
4638 + $full_response .= $content; // Accumulate the full response
4639 +
4640 + // Send as SSE format
4641 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
7460 4642 flush();
7461 - $stream_started = true;
7462 4643 }
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 4644 }
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) {
7569 - try {
7570 - $bot_id = $this->get_current_bot_id($session_id);
4645 +
4646 + return strlen($data);
4647 + });
7571 4648
7572 - // Get system prompt instructions using centralized function
7573 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4649 + $response = curl_exec($ch);
4650 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7574 4651
7575 - // Ensure conversation_history is an array
7576 - if (!is_array($conversation_history)) {
7577 - $conversation_history = array();
7578 - }
7579 -
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
4652 + if (curl_errno($ch) || $http_code !== 200) {
4653 + curl_close($ch);
4654 +
4655 + // Fallback to regular response
7607 4656 $regular_response = $this->mxchat_generate_response_openai(
7608 4657 $selected_model,
7609 4658 $api_key,
7610 4659 $conversation_history,
@@ -7610,13 +4659,8 @@
7610 4659 $conversation_history,
7611 4660 $relevant_content
7612 4661 );
7613 4662
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 4663 $response_data = [
7620 4664 'text' => $regular_response,
7621 4665 'html' => '',
7622 4666 'session_id' => $session_id
@@ -7629,905 +4673,42 @@
7629 4673 header('Content-Type: application/json');
7630 4674 echo json_encode($response_data);
7631 4675 return true;
7632 4676 }
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 - }
4677 +
4678 + curl_close($ch);
4679 +
4680 + // Save the complete response to maintain chat persistence
4681 + if (!empty($full_response) && !empty($session_id)) {
4682 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
7666 4683 }
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 -
4684 +
4685 + return true; // Indicate streaming completed successfully
4686 +
7843 4687 } 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
4688 + // Fallback to regular response
4689 + $regular_response = $this->mxchat_generate_response_openai(
4690 + $selected_model,
4691 + $api_key,
4692 + $conversation_history,
4693 + $relevant_content
7849 4694 );
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;
4695 +
4696 + $response_data = [
4697 + 'text' => $regular_response,
4698 + 'html' => '',
4699 + 'session_id' => $session_id
4700 + ];
4701 +
4702 + if ($testing_data !== null) {
4703 + $response_data['testing_data'] = $testing_data;
7879 4704 }
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) {
4705 +
7892 4706 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 - ));
4707 + echo json_encode($response_data);
7900 4708 return true;
7901 4709 }
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 4710 }
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 4711 private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8531 4712 try {
8532 4713 // Get bot ID from session or request
8533 4714 $bot_id = $this->get_current_bot_id($session_id);
@@ -8532,9 +4713,9 @@
8532 4713 // Get bot ID from session or request
8533 4714 $bot_id = $this->get_current_bot_id($session_id);
8534 4715
8535 4716 // Get system prompt instructions using centralized function
8536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4717 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
8537 4718 // Ensure conversation_history is an array
8538 4719 if (!is_array($conversation_history)) {
8539 4720 $conversation_history = array();
8540 4721 }
@@ -8566,9 +4747,9 @@
8566 4747 'content' => $relevant_content
8567 4748 ];
8568 4749
8569 4750 // Prepare the request body with stream: true
8570 - $payload = [
4751 + $body = json_encode([
8571 4752 'model' => $selected_model,
8572 4753 'messages' => $conversation_history,
8573 4754 'max_tokens' => 1000,
8574 4755 'temperature' => 0.8,
@@ -8573,11 +4754,9 @@
8573 4754 'max_tokens' => 1000,
8574 4755 'temperature' => 0.8,
8575 4756 'system' => $system_prompt_instructions,
8576 4757 'stream' => true
8577 - ];
8578 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
8579 - $body = json_encode($payload);
4758 + ]);
8580 4759
8581 4760 // Check if we can actually stream (headers not sent, etc.)
8582 4761 if (headers_sent() || !function_exists('curl_init')) {
8583 4762 // Fallback to regular response with testing data
@@ -8588,13 +4767,8 @@
8588 4767 array_slice($conversation_history, 0, -1), // Remove the added content
8589 4768 $relevant_content
8590 4769 );
8591 4770
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 4771 // Return as JSON with testing data
8598 4772 $response_data = [
8599 4773 'text' => $regular_response,
8600 4774 'html' => '',
@@ -8613,187 +4787,163 @@
8613 4787 echo json_encode($response_data);
8614 4788 return true; // Indicate we handled the response
8615 4789 }
8616 4790
8617 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
4791 + // Use cURL for streaming support
4792 + $ch = curl_init();
4793 + curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
4794 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4795 + curl_setopt($ch, CURLOPT_POST, true);
4796 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4797 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4798 + 'Content-Type: application/json',
4799 + 'x-api-key: ' . $claude_api_key,
4800 + 'anthropic-version: 2023-06-01'
4801 + ));
4802 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4803 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8618 4804
8619 - $captured_status_code = 0;
8620 - $captured_body_pre_stream = '';
8621 - $full_response = '';
4805 + $full_response = ''; // Accumulate full response for saving
8622 4806 $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);
4807 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
8628 4808
8629 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8630 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8631 - usleep($backoff_ms[$attempt] * 1000);
4809 + // Buffer control for real-time streaming
4810 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
4811 + // Send testing data as the first event if available
4812 + if (!$stream_started && $testing_data !== null) {
4813 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4814 + flush();
4815 + $stream_started = true;
4816 + //error_log("MxChat Testing: Sent testing data in Claude stream");
8632 4817 }
4818 +
4819 + // CRITICAL FIX: Append new data to buffer
4820 + $buffer .= $data;
4821 +
4822 + // Process complete lines only
4823 + $lines = explode("\n", $buffer);
4824 +
4825 + // CRITICAL FIX: Keep the last incomplete line in the buffer
4826 + // The last element might be incomplete, so keep it in buffer
4827 + $buffer = array_pop($lines);
8633 4828
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];
4829 + foreach ($lines as $line) {
4830 + if (trim($line) === '') {
4831 + continue;
8656 4832 }
8657 - return strlen($header);
8658 - });
8659 4833
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);
4834 + // Claude uses event: and data: format
4835 + if (strpos($line, 'event: ') === 0) {
4836 + // Store the event type for the next data line
4837 + continue;
8664 4838 }
8665 4839
8666 - if (!$this->streaming_headers_sent) {
8667 - $this->setup_streaming_headers();
8668 - }
4840 + if (strpos($line, 'data: ') === 0) {
4841 + $json_str = substr($line, 6); // Remove 'data: ' prefix
8669 4842
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 - }
8675 -
8676 - $buffer .= $data;
8677 - $lines = explode("\n", $buffer);
8678 - $buffer = array_pop($lines);
8679 -
8680 - foreach ($lines as $line) {
8681 - if (trim($line) === '') {
4843 + $json = json_decode(trim($json_str), true);
4844 + if (json_last_error() !== JSON_ERROR_NONE) {
8682 4845 continue;
8683 4846 }
8684 4847
8685 - if (strpos($line, 'event: ') === 0) {
8686 - continue;
8687 - }
4848 + // Handle different event types
4849 + if (isset($json['type'])) {
4850 + switch ($json['type']) {
4851 + case 'content_block_delta':
4852 + if (isset($json['delta']['text'])) {
4853 + $content = $json['delta']['text'];
4854 + $full_response .= $content; // Accumulate
4855 + // Send as SSE format compatible with your frontend
4856 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
4857 + flush();
4858 + }
4859 + break;
8688 4860
8689 - if (strpos($line, 'data: ') === 0) {
8690 - $json_str = substr($line, 6);
4861 + case 'message_stop':
4862 + echo "data: [DONE]\n\n";
4863 + flush();
4864 + break;
8691 4865
8692 - $json = json_decode(trim($json_str), true);
8693 - if (json_last_error() !== JSON_ERROR_NONE) {
8694 - continue;
4866 + case 'error':
4867 + echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
4868 + flush();
4869 + break;
8695 4870 }
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();
8705 - }
8706 - break;
8707 -
8708 - case 'message_stop':
8709 - echo "data: [DONE]\n\n";
8710 - flush();
8711 - break;
8712 -
8713 - case 'error':
8714 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8715 - flush();
8716 - break;
8717 - }
8718 - }
8719 4871 }
8720 4872 }
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 4873 }
8733 4874
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;
4875 + return strlen($data);
4876 + });
8738 4877
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 - }
4878 + $response = curl_exec($ch);
4879 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8747 4880
8748 - if (!$can_retry) {
8749 - break;
8750 - }
4881 + if (curl_errno($ch)) {
4882 + curl_close($ch);
4883 + throw new Exception('cURL Error: ' . curl_error($ch));
8751 4884 }
8752 4885
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
4886 + curl_close($ch);
4887 +
4888 + if ($http_code !== 200) {
4889 + // Fallback to regular response
4890 + //error_log("MxChat: Claude streaming failed with HTTP $http_code, falling back");
4891 + $regular_response = $this->mxchat_generate_response_claude(
4892 + $selected_model,
4893 + $claude_api_key,
4894 + array_slice($conversation_history, 0, -1), // Remove the added content
4895 + $relevant_content
8759 4896 );
4897 +
4898 + $response_data = [
4899 + 'text' => $regular_response,
4900 + 'html' => '',
4901 + 'session_id' => $session_id
4902 + ];
4903 +
4904 + if ($testing_data !== null) {
4905 + $response_data['testing_data'] = $testing_data;
4906 + //error_log("MxChat Testing: Added testing data to Claude error fallback");
4907 + }
4908 +
4909 + header('Content-Type: application/json');
4910 + echo json_encode($response_data);
4911 + return true;
8760 4912 }
8761 4913
8762 4914 // Save the complete response to maintain chat persistence
8763 4915 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);
4916 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8785 4917 }
8786 4918
8787 4919 return true; // Indicate streaming completed successfully
8788 4920
8789 4921 } 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
4922 + //error_log("MxChat Claude streaming exception: " . $e->getMessage());
4923 +
4924 + // Fallback to regular response on exception
4925 + $regular_response = $this->mxchat_generate_response_claude(
4926 + $selected_model,
4927 + $claude_api_key,
4928 + $conversation_history,
4929 + $relevant_content
8795 4930 );
4931 +
4932 + $response_data = [
4933 + 'text' => $regular_response,
4934 + 'html' => '',
4935 + 'session_id' => $session_id
4936 + ];
4937 +
4938 + if ($testing_data !== null) {
4939 + $response_data['testing_data'] = $testing_data;
4940 + //error_log("MxChat Testing: Added testing data to Claude exception fallback");
4941 + }
4942 +
4943 + header('Content-Type: application/json');
4944 + echo json_encode($response_data);
4945 + return true;
8796 4946 }
8797 4947 }
8798 4948 private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8799 4949 try {
@@ -8800,9 +4950,9 @@
8800 4950 // Get bot ID from session or request
8801 4951 $bot_id = $this->get_current_bot_id($session_id);
8802 4952
8803 4953 // Get system prompt instructions using centralized function
8804 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4954 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
8805 4955
8806 4956 // Ensure conversation_history is an array
8807 4957 if (!is_array($conversation_history)) {
8808 4958 $conversation_history = array();
@@ -8842,13 +4992,8 @@
8842 4992 $conversation_history,
8843 4993 $relevant_content
8844 4994 );
8845 4995
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 4996 $response_data = [
8852 4997 'text' => $regular_response,
8853 4998 'html' => '',
8854 4999 'session_id' => $session_id
@@ -8871,169 +5016,143 @@
8871 5016 'temperature' => 0.8,
8872 5017 'stream' => true
8873 5018 ]);
8874 5019
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 = '';
5020 + // Use cURL for streaming support
5021 + $ch = curl_init();
5022 + curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
5023 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
5024 + curl_setopt($ch, CURLOPT_POST, true);
5025 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
5026 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5027 + 'Content-Type: application/json',
5028 + 'Authorization: Bearer ' . $xai_api_key
5029 + ));
5030 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
5031 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
5032 +
5033 + $full_response = ''; // Accumulate full response for saving
8880 5034 $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);
5035 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
5036 +
5037 + // Buffer control for real-time streaming
5038 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
5039 + // Send testing data as the first event if available
5040 + if (!$stream_started && $testing_data !== null) {
5041 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5042 + flush();
5043 + $stream_started = true;
5044 + //error_log("MxChat Testing: Sent testing data in X.AI stream");
8890 5045 }
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];
5046 +
5047 + // CRITICAL FIX: Append new data to buffer
5048 + $buffer .= $data;
5049 +
5050 + // Process complete lines only
5051 + $lines = explode("\n", $buffer);
5052 +
5053 + // CRITICAL FIX: Keep the last incomplete line in the buffer
5054 + // The last element might be incomplete, so keep it in buffer
5055 + $buffer = array_pop($lines);
5056 +
5057 + foreach ($lines as $line) {
5058 + // Skip empty lines
5059 + if (trim($line) === '') {
5060 + continue;
8913 5061 }
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);
5062 +
5063 + // Only process lines that start with "data: "
5064 + if (strpos($line, 'data: ') !== 0) {
5065 + continue;
8921 5066 }
8922 -
8923 - if (!$this->streaming_headers_sent) {
8924 - $this->setup_streaming_headers();
5067 +
5068 + $json_str = substr($line, 6); // Remove 'data: ' prefix
5069 +
5070 + if (trim($json_str) === '[DONE]') {
5071 + echo "data: [DONE]\n\n";
5072 + flush();
5073 + continue;
8925 5074 }
8926 -
8927 - if (!$stream_started && $testing_data !== null) {
8928 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5075 +
5076 + // Try to decode JSON
5077 + $json = json_decode(trim($json_str), true);
5078 + if ($json && isset($json['choices'][0]['delta']['content'])) {
5079 + $content = $json['choices'][0]['delta']['content'];
5080 + $full_response .= $content; // Accumulate
5081 + // Send as SSE format
5082 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
8929 5083 flush();
8930 - $stream_started = true;
8931 5084 }
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;
8940 - }
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 - }
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);
5085 + }
5086 +
5087 + return strlen($data);
5088 + });
5089 +
5090 + $response = curl_exec($ch);
5091 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5092 +
5093 + if (curl_errno($ch) || $http_code !== 200) {
8968 5094 curl_close($ch);
8969 -
8970 - if (!$errno && $http_code === 200) {
8971 - break;
5095 +
5096 + // Fallback to regular response
5097 + //error_log("MxChat: X.AI streaming failed, falling back");
5098 + $regular_response = $this->mxchat_generate_response_xai(
5099 + $selected_model,
5100 + $xai_api_key,
5101 + $conversation_history,
5102 + $relevant_content
5103 + );
5104 +
5105 + $response_data = [
5106 + 'text' => $regular_response,
5107 + 'html' => '',
5108 + 'session_id' => $session_id
5109 + ];
5110 +
5111 + if ($testing_data !== null) {
5112 + $response_data['testing_data'] = $testing_data;
5113 + //error_log("MxChat Testing: Added testing data to X.AI error fallback");
8972 5114 }
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 - }
5115 +
5116 + header('Content-Type: application/json');
5117 + echo json_encode($response_data);
5118 + return true;
8991 5119 }
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 -
5120 +
5121 + curl_close($ch);
5122 +
9002 5123 // Save the complete response to maintain chat persistence
9003 5124 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);
5125 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9025 5126 }
9026 -
5127 +
9027 5128 return true; // Indicate streaming completed successfully
9028 -
5129 +
9029 5130 } 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
5131 + //error_log("MxChat X.AI streaming exception: " . $e->getMessage());
5132 +
5133 + // Fallback to regular response
5134 + $regular_response = $this->mxchat_generate_response_xai(
5135 + $selected_model,
5136 + $xai_api_key,
5137 + $conversation_history,
5138 + $relevant_content
9035 5139 );
5140 +
5141 + $response_data = [
5142 + 'text' => $regular_response,
5143 + 'html' => '',
5144 + 'session_id' => $session_id
5145 + ];
5146 +
5147 + if ($testing_data !== null) {
5148 + $response_data['testing_data'] = $testing_data;
5149 + //error_log("MxChat Testing: Added testing data to X.AI exception fallback");
5150 + }
5151 +
5152 + header('Content-Type: application/json');
5153 + echo json_encode($response_data);
5154 + return true;
9036 5155 }
9037 5156 }
9038 5157 private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9039 5158 try {
@@ -9040,9 +5159,9 @@
9040 5159 // Get bot ID from session or request
9041 5160 $bot_id = $this->get_current_bot_id($session_id);
9042 5161
9043 5162 // Get system prompt instructions using centralized function
9044 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5163 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9045 5164
9046 5165 // Ensure conversation_history is an array
9047 5166 if (!is_array($conversation_history)) {
9048 5167 $conversation_history = array();
@@ -9082,13 +5201,8 @@
9082 5201 $conversation_history,
9083 5202 $relevant_content
9084 5203 );
9085 5204
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 5205 $response_data = [
9092 5206 'text' => $regular_response,
9093 5207 'html' => '',
9094 5208 'session_id' => $session_id
@@ -9111,276 +5225,170 @@
9111 5225 'temperature' => 0.8,
9112 5226 'stream' => true
9113 5227 ]);
9114 5228
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 = '';
5229 + // Use cURL for streaming support
5230 + $ch = curl_init();
5231 + curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
5232 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
5233 + curl_setopt($ch, CURLOPT_POST, true);
5234 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
5235 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
5236 + 'Content-Type: application/json',
5237 + 'Authorization: Bearer ' . $deepseek_api_key
5238 + ));
5239 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
5240 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
5241 +
5242 + $full_response = ''; // Accumulate full response for saving
9120 5243 $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);
5244 + $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
5245 +
5246 + // Buffer control for real-time streaming
5247 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
5248 + // Send testing data as the first event if available
5249 + if (!$stream_started && $testing_data !== null) {
5250 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5251 + flush();
5252 + $stream_started = true;
5253 + //error_log("MxChat Testing: Sent testing data in DeepSeek stream");
9130 5254 }
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];
5255 +
5256 + // CRITICAL FIX: Append new data to buffer
5257 + $buffer .= $data;
5258 +
5259 + // Process complete lines only
5260 + $lines = explode("\n", $buffer);
5261 +
5262 + // CRITICAL FIX: Keep the last incomplete line in the buffer
5263 + // The last element might be incomplete, so keep it in buffer
5264 + $buffer = array_pop($lines);
5265 +
5266 + foreach ($lines as $line) {
5267 + // Skip empty lines
5268 + if (trim($line) === '') {
5269 + continue;
9153 5270 }
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);
5271 +
5272 + // Only process lines that start with "data: "
5273 + if (strpos($line, 'data: ') !== 0) {
5274 + continue;
9161 5275 }
9162 -
9163 - if (!$this->streaming_headers_sent) {
9164 - $this->setup_streaming_headers();
5276 +
5277 + $json_str = substr($line, 6); // Remove 'data: ' prefix
5278 +
5279 + if (trim($json_str) === '[DONE]') {
5280 + echo "data: [DONE]\n\n";
5281 + flush();
5282 + continue;
9165 5283 }
9166 -
9167 - if (!$stream_started && $testing_data !== null) {
9168 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
5284 +
5285 + // Try to decode JSON
5286 + $json = json_decode(trim($json_str), true);
5287 + if ($json && isset($json['choices'][0]['delta']['content'])) {
5288 + $content = $json['choices'][0]['delta']['content'];
5289 + $full_response .= $content; // Accumulate the full response
5290 +
5291 + // Send as SSE format
5292 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
9169 5293 flush();
9170 - $stream_started = true;
9171 5294 }
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 - }
5295 + }
5296 +
5297 + return strlen($data);
5298 + });
5299 +
5300 + $response = curl_exec($ch);
5301 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5302 +
5303 + if (curl_errno($ch) || $http_code !== 200) {
5304 + $curl_error = curl_error($ch);
5305 + curl_close($ch);
5306 +
5307 + // Log the specific error for debugging
5308 + //error_log("MxChat: DeepSeek streaming failed - HTTP: $http_code, cURL: $curl_error");
5309 +
5310 + // Fallback to regular response
5311 + $regular_response = $this->mxchat_generate_response_deepseek(
5312 + $selected_model,
5313 + $deepseek_api_key,
5314 + $conversation_history,
5315 + $relevant_content
5316 + );
5317 +
5318 + // Handle error response from regular function
5319 + if (is_array($regular_response) && isset($regular_response['error'])) {
5320 + if ($testing_data !== null) {
5321 + $regular_response['testing_data'] = $testing_data;
9200 5322 }
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;
5323 + header('Content-Type: application/json');
5324 + echo json_encode($regular_response);
5325 + return true;
9212 5326 }
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 - ));
5327 +
5328 + $response_data = [
5329 + 'text' => $regular_response,
5330 + 'html' => '',
5331 + 'session_id' => $session_id
5332 + ];
5333 +
5334 + if ($testing_data !== null) {
5335 + $response_data['testing_data'] = $testing_data;
5336 + //error_log("MxChat Testing: Added testing data to DeepSeek error fallback");
9226 5337 }
9227 -
9228 - if (!$can_retry) {
9229 - break;
9230 - }
5338 +
5339 + header('Content-Type: application/json');
5340 + echo json_encode($response_data);
5341 + return true;
9231 5342 }
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 -
5343 +
5344 + curl_close($ch);
5345 +
9242 5346 // Save the complete response to maintain chat persistence
9243 5347 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);
5348 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9265 5349 }
9266 -
5350 +
9267 5351 return true; // Indicate streaming completed successfully
9268 -
5352 +
9269 5353 } 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
5354 + //error_log("MxChat DeepSeek streaming exception: " . $e->getMessage());
5355 +
5356 + // Fallback to regular response
5357 + $regular_response = $this->mxchat_generate_response_deepseek(
5358 + $selected_model,
5359 + $deepseek_api_key,
5360 + $conversation_history,
5361 + $relevant_content
9275 5362 );
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 5363
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 - );
5364 + // Handle error response from regular function
5365 + if (is_array($regular_response) && isset($regular_response['error'])) {
5366 + if ($testing_data !== null) {
5367 + $regular_response['testing_data'] = $testing_data;
9311 5368 }
5369 + header('Content-Type: application/json');
5370 + echo json_encode($regular_response);
5371 + return true;
9312 5372 }
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,
5373 +
5374 + $response_data = [
5375 + 'text' => $regular_response,
5376 + 'html' => '',
5377 + 'session_id' => $session_id
9333 5378 ];
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 - ];
5379 +
5380 + if ($testing_data !== null) {
5381 + $response_data['testing_data'] = $testing_data;
5382 + //error_log("MxChat Testing: Added testing data to DeepSeek exception fallback");
9344 5383 }
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 - ];
5384 +
5385 + header('Content-Type: application/json');
5386 + echo json_encode($response_data);
5387 + return true;
9381 5388 }
9382 5389 }
5390 +
9383 5391 private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
9384 5392
9385 5393 // Get bot ID from session or request
9386 5394 $bot_id = $this->get_current_bot_id($session_id);
@@ -9385,9 +5393,9 @@
9385 5393 // Get bot ID from session or request
9386 5394 $bot_id = $this->get_current_bot_id($session_id);
9387 5395
9388 5396 // Get system prompt instructions using centralized function
9389 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5397 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9390 5398
9391 5399 // Clean and validate conversation history
9392 5400 foreach ($conversation_history as &$message) {
9393 5401 // Convert bot and agent roles to assistant
@@ -9415,17 +5423,15 @@
9415 5423 'content' => $relevant_content
9416 5424 ];
9417 5425
9418 5426 // Build request body
9419 - $payload = [
5427 + $body = json_encode([
9420 5428 'model' => $selected_model,
9421 5429 'max_tokens' => 1000,
9422 5430 'temperature' => 0.8,
9423 5431 'messages' => $conversation_history,
9424 5432 'system' => $system_prompt_instructions
9425 - ];
9426 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9427 - $body = json_encode($payload);
5433 + ]);
9428 5434
9429 5435 // Set up API request
9430 5436 $args = [
9431 5437 'body' => $body,
@@ -9441,9 +5447,9 @@
9441 5447 'sslverify' => true,
9442 5448 ];
9443 5449
9444 5450 // Make API request
9445 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
5451 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
9446 5452
9447 5453 // Check for WordPress errors
9448 5454 if (is_wp_error($response)) {
9449 5455 //error_log("Claude API request error: " . $response->get_error_message());
@@ -9473,17 +5479,14 @@
9473 5479 //error_log("Claude API JSON decode error: " . json_last_error_msg());
9474 5480 return "Sorry, there was an error processing the API response.";
9475 5481 }
9476 5482
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 - }
5483 + // Extract and validate response content
5484 + if (isset($response_body['content']) &&
5485 + is_array($response_body['content']) &&
5486 + !empty($response_body['content']) &&
5487 + isset($response_body['content'][0]['text'])) {
5488 + return trim($response_body['content'][0]['text']);
9486 5489 }
9487 5490
9488 5491 // Log unexpected response format
9489 5492 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
@@ -9496,12 +5499,12 @@
9496 5499 $conversation_history = array();
9497 5500 }
9498 5501
9499 5502 // Get bot ID from session or request
9500 - $bot_id = $this->get_current_bot_id('');
5503 + $bot_id = $this->get_current_bot_id($session_id);
9501 5504
9502 5505 // Get system prompt instructions using centralized function
9503 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5506 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9504 5507
9505 5508 // Create a new array for the formatted conversation
9506 5509 $formatted_conversation = array();
9507 5510
@@ -9530,44 +5533,15 @@
9530 5533 );
9531 5534 }
9532 5535 }
9533 5536
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 = [
5537 + $body = json_encode([
9546 5538 'model' => $selected_model,
9547 5539 'messages' => $formatted_conversation,
9548 5540 'temperature' => 1,
9549 5541 'stream' => false
9550 - ];
5542 + ]);
9551 5543
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 5544 $args = [
9571 5545 'body' => $body,
9572 5546 'headers' => [
9573 5547 'Content-Type' => 'application/json',
@@ -9579,12 +5553,13 @@
9579 5553 'httpversion' => '1.0',
9580 5554 'sslverify' => true,
9581 5555 ];
9582 5556
9583 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
5557 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
9584 5558
9585 5559 if (is_wp_error($response)) {
9586 5560 $error_message = $response->get_error_message();
5561 + //error_log('OpenAI API Error: ' . $error_message);
9587 5562 return [
9588 5563 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9589 5564 'error_code' => 'openai_connection_error',
9590 5565 'provider' => 'openai'
@@ -9603,8 +5578,10 @@
9603 5578 $error_type = isset($decoded_response['error']['type'])
9604 5579 ? $decoded_response['error']['type']
9605 5580 : 'unknown';
9606 5581
5582 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
5583 +
9607 5584 // Handle specific error types
9608 5585 switch ($error_type) {
9609 5586 case 'invalid_request_error':
9610 5587 if (strpos($error_message, 'API key') !== false) {
@@ -9652,8 +5629,9 @@
9652 5629
9653 5630 if (isset($decoded_response['choices'][0]['message']['content'])) {
9654 5631 return trim($decoded_response['choices'][0]['message']['content']);
9655 5632 } else {
5633 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
9656 5634 return [
9657 5635 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9658 5636 'error_code' => 'openai_response_format_error',
9659 5637 'provider' => 'openai'
@@ -9659,8 +5637,9 @@
9659 5637 'provider' => 'openai'
9660 5638 ];
9661 5639 }
9662 5640 } catch (Exception $e) {
5641 + //error_log('OpenAI Exception: ' . $e->getMessage());
9663 5642 return [
9664 5643 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9665 5644 'error_code' => 'openai_exception',
9666 5645 'provider' => 'openai'
@@ -9666,9 +5645,8 @@
9666 5645 'provider' => 'openai'
9667 5646 ];
9668 5647 }
9669 5648 }
9670 -
9671 5649 private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9672 5650 try {
9673 5651 // Get bot ID from session or request
9674 5652 $bot_id = $this->get_current_bot_id($session_id);
@@ -9673,9 +5651,9 @@
9673 5651 // Get bot ID from session or request
9674 5652 $bot_id = $this->get_current_bot_id($session_id);
9675 5653
9676 5654 // Get system prompt instructions using centralized function
9677 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5655 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9678 5656
9679 5657 // Add system prompt to relevant content
9680 5658 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9681 5659
@@ -9725,9 +5703,9 @@
9725 5703 'sslverify' => true,
9726 5704 ];
9727 5705
9728 5706 // Make the API request
9729 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
5707 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
9730 5708
9731 5709 // Process the response
9732 5710 if (is_wp_error($response)) {
9733 5711 $error_message = $response->get_error_message();
@@ -9872,9 +5850,9 @@
9872 5850 // Get bot ID from session or request
9873 5851 $bot_id = $this->get_current_bot_id($session_id);
9874 5852
9875 5853 // Get system prompt instructions using centralized function
9876 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
5854 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
9877 5855
9878 5856 // Create a new array for the formatted conversation
9879 5857 $formatted_conversation = array();
9880 5858
@@ -9923,9 +5901,9 @@
9923 5901 'httpversion' => '1.0',
9924 5902 'sslverify' => true,
9925 5903 ];
9926 5904
9927 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
5905 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
9928 5906
9929 5907 if (is_wp_error($response)) {
9930 5908 $error_message = $response->get_error_message();
9931 5909 //error_log('DeepSeek API Error: ' . $error_message);
@@ -10027,18 +6005,13 @@
10027 6005 ];
10028 6006 }
10029 6007 }
10030 6008 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 6009 // Get bot ID from session or request
10037 6010 $bot_id = $this->get_current_bot_id($session_id);
10038 6011
10039 6012 // Get system prompt instructions using centralized function
10040 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
6013 + $system_prompt_instructions = $this->get_system_instructions($bot_id);
10041 6014
10042 6015 // Add system prompt to relevant content
10043 6016 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10044 6017
@@ -10134,11 +6107,9 @@
10134 6107 ]
10135 6108 ]);
10136 6109
10137 6110 // 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;
6111 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10141 6112
10142 6113 // Set up the API request
10143 6114 $args = [
10144 6115 'body' => $body,
@@ -10152,10 +6123,10 @@
10152 6123 'sslverify' => true,
10153 6124 ];
10154 6125
10155 6126 // Make the API request
10156 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10157 -
6127 + $response = wp_remote_post($api_endpoint, $args);
6128 +
10158 6129 // Process the response
10159 6130 if (is_wp_error($response)) {
10160 6131 return "Sorry, there was an error processing your request: " . $response->get_error_message();
10161 6132 }
@@ -10180,9 +6151,9 @@
10180 6151
10181 6152
10182 6153 public function test_streaming_request() {
10183 6154 $options = get_option('mxchat_options', []);
10184 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
6155 + $model = $options['model'] ?? 'gpt-4o';
10185 6156
10186 6157 // Detect provider from model prefix
10187 6158 $provider = strtolower(explode('-', $model)[0]);
10188 6159
@@ -10365,61 +6336,39 @@
10365 6336 }
10366 6337
10367 6338
10368 6339 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
6340 + // Define version numbers for the styles and scripts
6341 + $chat_style_version = '2.4.6';
6342 + $chat_script_version = '2.4.6';
6343 + // Enqueue the script
6344 + wp_enqueue_script(
6345 + 'mxchat-chat-js',
6346 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
6347 + array('jquery'),
6348 + $chat_script_version,
6349 + true
6350 + );
6351 + // Enqueue the CSS
10374 6352 wp_enqueue_style(
10375 6353 'mxchat-chat-css',
10376 6354 plugin_dir_url(__FILE__) . '../css/chat-style.css',
10377 6355 array(),
10378 - MXCHAT_VERSION
6356 + $chat_style_version
10379 6357 );
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 -
6358 + // Fetch options from the database
6359 + $this->options = get_option('mxchat_options');
10402 6360 $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 -
6361 +
10410 6362 // Prepare settings for JavaScript
10411 6363 $style_settings = array(
10412 6364 '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'))),
6365 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
6366 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-4o',
6367 + 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
10420 6368 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10421 6369 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
6370 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
10422 6371 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10423 6372 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10424 6373 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10425 6374 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -10434,8 +6383,9 @@
10434 6383 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10435 6384 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10436 6385 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10437 6386 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
6387 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
10438 6388 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10439 6389 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10440 6390 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10441 6391 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
@@ -10441,145 +6391,15 @@
10441 6391 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10442 6392 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
10443 6393 'initial_email_state' => null, // Also fixed this undefined variable
10444 6394 '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(),
6395 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
10448 6396 );
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 - }
6397 + // Pass the settings to the script
6398 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10464 6399 }
10465 6400
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 6401
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 6402 /**
10583 6403 * Setup the cron jobs for rate limits with guard against multiple calls
10584 6404 */
10585 6405 public function setup_rate_limit_cron_jobs() {
@@ -10738,57 +6558,9 @@
10738 6558 $current_options = !empty($bot_options) ? $bot_options : $this->options;
10739 6559
10740 6560 // Use bot-specific rate limits if available, otherwise fall back to default
10741 6561 $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 -
6562 +
10791 6563 // Determine user role or if logged out
10792 6564 if (is_user_logged_in()) {
10793 6565 $user = wp_get_current_user();
10794 6566 $user_id = $user->ID;
@@ -11162,11 +6934,8 @@
11162 6934
11163 6935 /**
11164 6936 * AJAX handler to get system information for testing panel
11165 6937 */
11166 -/**
11167 - * AJAX handler to get system information for testing panel
11168 - */
11169 6938 public function mxchat_get_system_info() {
11170 6939 // Verify nonce for security
11171 6940 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11172 6941 wp_send_json_error(['message' => 'Invalid nonce']);
@@ -11183,25 +6952,11 @@
11183 6952 $system_prompt = isset($this->options['system_prompt_instructions'])
11184 6953 ? $this->options['system_prompt_instructions']
11185 6954 : 'No system prompt configured';
11186 6955
11187 - // Get selected model
11188 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
6956 + // Get selected model - FIXED: Use $this->options instead of $current_options
6957 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
11189 6958
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 6959 // Get API key status (just check if they exist, don't expose the keys)
11205 6960 $api_status = [];
11206 6961 $api_status['openai'] = !empty($this->options['api_key']);
11207 6962 $api_status['claude'] = !empty($this->options['claude_api_key']);
@@ -11207,15 +6962,12 @@
11207 6962 $api_status['claude'] = !empty($this->options['claude_api_key']);
11208 6963 $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11209 6964 $api_status['xai'] = !empty($this->options['xai_api_key']);
11210 6965 $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11211 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11212 6966
11213 6967 wp_send_json_success([
11214 6968 'system_prompt' => $system_prompt,
11215 6969 'selected_model' => $selected_model,
11216 - 'is_openrouter' => $is_openrouter,
11217 - 'openrouter_model' => $openrouter_model,
11218 6970 'api_status' => $api_status
11219 6971 ]);
11220 6972 }
11221 6973
@@ -11254,42 +7006,24 @@
11254 7006 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11255 7007 wp_send_json_error(['message' => 'Invalid nonce']);
11256 7008 return;
11257 7009 }
11258 -
7010 +
11259 7011 // Only allow admin users
11260 7012 if (!current_user_can('administrator')) {
11261 7013 wp_send_json_error(['message' => 'Unauthorized']);
11262 7014 return;
11263 7015 }
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 -
7016 +
11283 7017 // Check Pinecone vs WordPress
11284 7018 $addon_options = get_option('mxchat_pinecone_addon_options', array());
11285 7019 $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11286 -
7020 +
11287 7021 $kb_info = [
11288 7022 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11289 7023 'status' => 'Active'
11290 7024 ];
11291 -
7025 +
11292 7026 // Get document count
11293 7027 if ($use_pinecone) {
11294 7028 $kb_info['documents'] = 'Connected to Pinecone';
11295 7029 $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
@@ -11299,9 +7033,9 @@
11299 7033 $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11300 7034 $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11301 7035 $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11302 7036 }
11303 -
7037 +
11304 7038 wp_send_json_success($kb_info);
11305 7039 }
11306 7040
11307 7041 /**
@@ -11383,13 +7117,9 @@
11383 7117 // Clear any other session-specific transients
11384 7118 delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
11385 7119 delete_transient("mxchat_include_pdf_in_context_{$session_id}");
11386 7120 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 -
7121 +
11392 7122 //error_log("MxChat: Cleared all data for session: {$session_id}");
11393 7123 }
11394 7124
11395 7125 /**
@@ -11464,9 +7194,9 @@
11464 7194 * Track URL clicks from chatbot responses
11465 7195 */
11466 7196 public function mxchat_track_url_click() {
11467 7197 // Verify nonce for security
11468 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7198 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11469 7199 wp_send_json_error(['message' => 'Invalid nonce']);
11470 7200 wp_die();
11471 7201 }
11472 7202
@@ -11517,9 +7247,9 @@
11517 7247 * Track the originating page where chat was started
11518 7248 */
11519 7249 public function mxchat_track_originating_page() {
11520 7250 // Verify nonce
11521 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7251 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11522 7252 wp_send_json_error(['message' => 'Invalid nonce']);
11523 7253 wp_die();
11524 7254 }
11525 7255
@@ -11564,169 +7294,8 @@
11564 7294 wp_send_json_success(['message' => 'Originating page tracked']);
11565 7295 wp_die();
11566 7296 }
11567 7297
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 7298
11730 7299 /**
11731 7300 * AJAX handler to get current chat mode for a session
11732 7301 */
@@ -11731,9 +7300,9 @@
11731 7300 * AJAX handler to get current chat mode for a session
11732 7301 */
11733 7302 public function mxchat_get_current_chat_mode() {
11734 7303 // Verify nonce for security
11735 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
7304 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11736 7305 wp_send_json_error(['message' => 'Invalid nonce']);
11737 7306 wp_die();
11738 7307 }
11739 7308