PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.0
MxChat – AI Chatbot & Content Generation for WordPress v2.4.0
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 +1556 -6998 3.2.102.4.0 View file →
@@ -8,258 +8,13 @@
8 8 private $prompts_options;
9 9 private $chat_count;
10 10 private $fallbackResponse;
11 11 private $productCardHtml;
12 - // plan-mxchat-20260617-48a57a — function-calling UI payload capture. When a
13 - // model-invoked tool yields a UI element (generated image, woo product card,
14 - // image-search gallery), the FC loop stashes its html here so the FC outcome
15 - // handler can SURFACE it to the frontend the same way the intent path does,
16 - // instead of stripping it to text for the model (the bug: UI-bearing actions
17 - // rendered nothing under function calling).
18 - private $fc_ui_html = '';
19 - private $fc_ui_images = array();
20 - private $fc_ui_captured = false;
21 12 private $word_handler;
22 13 private $last_similarity_analysis = null;
23 - private $current_valid_urls = [];
24 - private $last_vectorstore_error = null;
25 - private $is_streaming = false; // ADDED: Track if current request is streaming
26 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
27 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
28 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
29 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
30 14
31 -/**
32 - * Setup streaming headers - call this right before actually streaming
33 - * This delays header setup to allow actions/forms to return JSON responses
34 - */
35 -/**
36 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
37 - *
38 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
39 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
40 - * specific "overloaded" / "rate limit" body string. Returns immediately on
41 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
42 - *
43 - * Drop-in replacement for wp_remote_post — returns the same shape
44 - * (WP_Error or response array) so the caller's existing error-handling
45 - * code path is unchanged.
46 - *
47 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
48 - * paths (the *_response_openai / *_response_claude / etc functions).
49 - * For the *_stream variants, the cURL initial-connect happens inside a
50 - * read-chunks loop — retrying there safely (without re-emitting partial
51 - * stream chunks to the client) is a separate problem. Streaming paths
52 - * are NOT wrapped in this build; tracked as a follow-on.
53 - *
54 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
55 - * (default true). When false, behavior is identical to plain wp_remote_post.
56 - */
57 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
58 - $opts = is_array($this->options ?? null) ? $this->options : array();
59 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
60 - (string) $opts['auto_retry_on_transient_error'] !== '0';
61 15
62 - if (!$enabled) {
63 - return wp_remote_post($url, $args);
64 - }
65 -
66 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
67 - $last_response = null;
68 -
69 - foreach ($backoffs as $i => $delay_ms) {
70 - if ($delay_ms > 0) {
71 - usleep($delay_ms * 1000);
72 - }
73 - $response = wp_remote_post($url, $args);
74 - $last_response = $response;
75 -
76 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
77 - return $response;
78 - }
79 -
80 - if (defined('WP_DEBUG') && WP_DEBUG) {
81 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
82 - : (int) wp_remote_retrieve_response_code($response);
83 - error_log(sprintf(
84 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
85 - $provider_hint ?: 'unknown',
86 - $i + 1,
87 - $code_for_log,
88 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
89 - ));
90 - }
91 - }
92 -
93 - return $last_response;
94 -}
95 -
96 16 /**
97 - * Returns true if a wp_remote_post response represents a TRANSIENT
98 - * provider error worth retrying. Conservative — only retries on signals
99 - * that are very likely to clear within a few seconds.
100 - *
101 - * Transient signals:
102 - * - WP_Error with timeout / connection / dns / ssl
103 - * - HTTP 429, 502, 503, 504
104 - * - Provider-specific overload bodies (gemini "overloaded", openai
105 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
106 - *
107 - * NOT transient (return false — fail-fast):
108 - * - 200/2xx (success)
109 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
110 - * budget; the user needs to fix something)
111 - * - Any other 4xx (assume permanent unless explicitly listed above)
112 - * - 5xx other than the four listed above (e.g. 500 generic server error
113 - * is often a malformed request on our side, not a transient outage)
114 - */
115 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
116 - if (is_wp_error($response)) {
117 - $code = $response->get_error_code();
118 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
119 - || stripos((string) $response->get_error_message(), 'timed out') !== false
120 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
121 - }
122 -
123 - $status = (int) wp_remote_retrieve_response_code($response);
124 - if (in_array($status, array(429, 502, 503, 504), true)) {
125 - return true;
126 - }
127 - if ($status >= 200 && $status < 300) {
128 - return false;
129 - }
130 - // Permanent 4xx that should fail fast — even with no body.
131 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
132 - return false;
133 - }
134 -
135 - // Provider-specific body inspection for the cases where the upstream
136 - // returns 200 with an error envelope (gemini does this for overload).
137 - $body = (string) wp_remote_retrieve_body($response);
138 - if ($body === '') {
139 - return false;
140 - }
141 - $lower = strtolower($body);
142 - $hint = strtolower((string) $provider_hint);
143 -
144 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
145 - || strpos($lower, 'high demand') !== false
146 - || strpos($lower, 'model is overloaded') !== false)) {
147 - return true;
148 - }
149 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
150 - || strpos($lower, '"type":"server_error"') !== false
151 - || strpos($lower, '"code":"server_error"') !== false)) {
152 - return true;
153 - }
154 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
155 - || strpos($lower, 'overloaded_error') !== false)) {
156 - return true;
157 - }
158 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
159 - return true;
160 - }
161 -
162 - return false;
163 -}
164 -
165 -/**
166 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
167 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
168 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
169 - * collect status separately from a plain wp_remote_post array shape, so the
170 - * non-streaming helper above can't be called directly. This delegate keeps
171 - * the classification rules identical across both paths.
172 - */
173 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
174 - if ($curl_errno) {
175 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
176 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
177 - return in_array($curl_errno, array(
178 - CURLE_OPERATION_TIMEDOUT,
179 - CURLE_COULDNT_CONNECT,
180 - CURLE_COULDNT_RESOLVE_HOST,
181 - CURLE_SSL_CONNECT_ERROR,
182 - CURLE_GOT_NOTHING,
183 - CURLE_SEND_ERROR,
184 - CURLE_RECV_ERROR,
185 - ), true);
186 - }
187 -
188 - $status = (int) $http_code;
189 - if (in_array($status, array(429, 502, 503, 504), true)) {
190 - return true;
191 - }
192 - if ($status >= 200 && $status < 300) {
193 - return false;
194 - }
195 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
196 - return false;
197 - }
198 -
199 - $body = (string) $body;
200 - if ($body === '') {
201 - return false;
202 - }
203 - $lower = strtolower($body);
204 - $hint = strtolower((string) $provider_hint);
205 -
206 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
207 - || strpos($lower, 'high demand') !== false
208 - || strpos($lower, 'model is overloaded') !== false)) {
209 - return true;
210 - }
211 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
212 - || strpos($lower, '"type":"server_error"') !== false
213 - || strpos($lower, '"code":"server_error"') !== false)) {
214 - return true;
215 - }
216 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
217 - || strpos($lower, 'overloaded_error') !== false)) {
218 - return true;
219 - }
220 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
221 - return true;
222 - }
223 -
224 - return false;
225 -}
226 -
227 -/**
228 - * Whether transient-error auto-retry is enabled in admin settings.
229 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
230 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
231 - */
232 -private function mxchat_retry_enabled() {
233 - $opts = is_array($this->options ?? null) ? $this->options : array();
234 - return !isset($opts['auto_retry_on_transient_error']) ||
235 - (string) $opts['auto_retry_on_transient_error'] !== '0';
236 -}
237 -
238 -private function setup_streaming_headers() {
239 - if ($this->streaming_headers_sent || headers_sent()) {
240 - return false;
241 - }
242 -
243 - // Disable output buffering
244 - while (ob_get_level()) {
245 - ob_end_flush();
246 - }
247 -
248 - // Set headers for SSE
249 - header('Content-Type: text/event-stream');
250 - header('Cache-Control: no-cache');
251 - header('Connection: keep-alive');
252 - header('X-Accel-Buffering: no');
253 -
254 - ob_implicit_flush(true);
255 - flush();
256 -
257 - $this->streaming_headers_sent = true;
258 - return true;
259 -}
260 -
261 -/**
262 17 * Class constructor
263 18 */
264 19 public function __construct() {
265 20 $this->options = get_option('mxchat_options');
@@ -326,97 +81,13 @@
326 81 // Add chat mode checking actions
327 82 add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
328 83 add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
329 84
330 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
331 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
332 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
333 -
334 - // Auto-email transcript action
335 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
336 -
337 85 add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
338 86
339 87
340 88 }
341 89
342 -/**
343 - * Return a fresh nonce so cached pages can replace the stale one.
344 - * With `with_settings`, also returns the current behavior-gate settings so
345 - * the widget can correct stale inline-localized values (plan-32db95).
346 - */
347 -public function mxchat_refresh_nonce() {
348 - nocache_headers();
349 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
350 - if (!empty($_REQUEST['with_settings'])) {
351 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
352 - }
353 - wp_send_json_success($payload);
354 -}
355 -
356 -/**
357 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
358 - *
359 - * Every widget setting ships inline in page HTML via wp_localize_script, so
360 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
361 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
362 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
363 - * caches PHP can reach; this payload covers the rest — the widget requests
364 - * it on first open (via the nonce-refresh endpoints) and merges it over
365 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
366 - * nonce uses.
367 - *
368 - * Behavior gates + labels ONLY — colors stay inline because they're also
369 - * server-inline-styled, and a runtime swap would visibly flash.
370 - *
371 - * Both wp_localize_script blocks merge this exact array, so the inline and
372 - * refreshed payloads cannot drift.
373 - *
374 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
375 - * instead of trusting the instance copy.
376 - * @return array
377 - */
378 -public function get_dynamic_widget_settings($fresh = false) {
379 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
380 - if (!is_array($options)) {
381 - $options = array();
382 - }
383 - return array(
384 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
385 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
386 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
387 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
388 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
389 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
390 - // "Start new chat" header-menu item (plan ac2e81). Default OFF.
391 - 'reset_chat_enabled' => $options['reset_chat_enabled'] ?? 'off',
392 - 'reset_chat_label' => !empty($options['reset_chat_label']) ? esc_html($options['reset_chat_label']) : esc_html__('Start new chat', 'mxchat'),
393 - 'reset_chat_confirm' => esc_html__('Start a new chat? This clears the current conversation.', 'mxchat'),
394 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
395 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
396 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
397 - // scalars to string, and (string) false === '' — which the widget's
398 - // old gate read as enabled (plan-4bba64). The filter keeps its
399 - // boolean contract; only the emitted value is stringified.
400 - 'satisfaction_rating_enabled' => apply_filters(
401 - 'mxchat_satisfaction_rating_enabled',
402 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
403 - ) ? 'on' : 'off',
404 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
405 - 'satisfaction_rating_copy' => array(
406 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
407 - 'helpful' => esc_html__('Helpful', 'mxchat'),
408 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
409 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
410 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
411 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
412 - 'send' => esc_html__('Send', 'mxchat'),
413 - 'skip' => esc_html__('Skip', 'mxchat'),
414 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
415 - ),
416 - );
417 -}
418 -
419 90 // In your core plugin's check_actions_for_addons method:
420 91 public function check_actions_for_addons($default, $message, $user_id, $session_id) {
421 92 //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
422 93
@@ -439,22 +110,8 @@
439 110 wp_die();
440 111 }
441 112
442 113 $session_id = sanitize_text_field($_POST['session_id']);
443 -
444 - // SECURITY FIX: Verify session ownership before retrieving data
445 - // If IP/user changed, signal frontend to reset session instead of blocking
446 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
447 -
448 - // Check if this session has an owner recorded
449 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
450 -
451 - // Update session owner if it changed (e.g. IP changed due to network switch)
452 - // The session ID itself is the authentication — if the client has it, they own it
453 - if (!$session_owner || $session_owner !== $current_user_identifier) {
454 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
455 - }
456 -
457 114 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
458 115 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
459 116
460 117 if (empty($history)) {
@@ -471,25 +128,11 @@
471 128 'chat_mode' => $chat_mode
472 129 ]);
473 130 wp_die();
474 131 }
475 -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) {
476 134 $history = get_option("mxchat_history_{$session_id}", []);
477 -
478 - // Check persistence setting - when OFF, only include messages from current page load
479 - $options = get_option('mxchat_options', []);
480 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
481 -
482 - // Filter history when persistence is OFF to match what the user sees
483 - if (!$persistence_enabled && $session_start_timestamp > 0) {
484 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
485 - // Include messages from this page load onwards
486 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
487 - });
488 - // Re-index array after filtering
489 - $history = array_values($history);
490 - }
491 -
492 135 $formatted_history = [];
493 136
494 137 // Adjusted for code-heavy conversations
495 138 $max_tokens = 120000; // Context window size
@@ -563,17 +206,8 @@
563 206
564 207 public function register_routes() {
565 208 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
566 209
567 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
568 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
569 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
570 - register_rest_route('mxchat/v1', '/nonce', [
571 - 'methods' => 'GET',
572 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
573 - 'permission_callback' => '__return_true',
574 - ]);
575 -
576 210 register_rest_route('mxchat/v1', '/stream', [
577 211 'methods' => 'GET',
578 212 'callback' => [$this, 'mxchat_stream_events'],
579 213 'permission_callback' => [$this, 'verify_chat_session'],
@@ -596,105 +230,12 @@
596 230 'callback' => [$this, 'handle_slack_messages'],
597 231 'permission_callback' => [$this, 'verify_slack_request'],
598 232 ]);
599 233
600 - // Telegram webhook endpoint
601 - register_rest_route('mxchat/v1', '/telegram-webhook', [
602 - 'methods' => 'POST',
603 - 'callback' => [$this, 'handle_telegram_webhook'],
604 - 'permission_callback' => [$this, 'verify_telegram_request'],
605 - ]);
606 -
607 234 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
608 235 }
609 236
610 237 /**
611 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
612 - * caches it for the session and includes it on every chat-send / stream-send /
613 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
614 - * we eliminate the entire class of "first-message Access denied" failures that
615 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
616 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
617 - * lives in the HTML body.
618 - *
619 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
620 - * client browser can't be used to flood the nonce-issuance path.
621 - *
622 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
623 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
624 - * backwards-compat window so cached pages still in users' browsers don't break
625 - * mid-session.
626 - *
627 - * @since 3.2.7
628 - */
629 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
630 - $ip = '';
631 - if (!empty($_SERVER['REMOTE_ADDR'])) {
632 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
633 - }
634 - if ($ip !== '') {
635 - // Best-effort rate limit. WP transients with sub-second TTL are racy
636 - // (parallel bursts can squeak through before set_transient completes);
637 - // we use 2s to make the gate slightly more reliable. Real production
638 - // rate-limiting at sub-second granularity needs Redis or DB row locks
639 - // — out of scope for this endpoint, which is already cheap.
640 - $key = 'mxchat_nonce_rl_' . md5($ip);
641 - if (get_transient($key)) {
642 - return new WP_REST_Response(array(
643 - 'error' => 'rate_limited',
644 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
645 - ), 429);
646 - }
647 - set_transient($key, 1, 2);
648 - }
649 -
650 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
651 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
652 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
653 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
654 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
655 - if ( ! is_user_logged_in() ) {
656 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
657 - if ( $maybe_uid ) {
658 - wp_set_current_user( $maybe_uid );
659 - }
660 - }
661 -
662 - $payload = array(
663 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
664 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
665 - );
666 -
667 - // plan-32db95: the widget's first-open refresh asks for current behavior
668 - // settings in the same round-trip, so stale inline-localized values on
669 - // cached pages get corrected without a second request. All values in
670 - // this payload already ship in public page HTML — nothing sensitive.
671 - if ($request->get_param('with_settings')) {
672 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
673 - }
674 -
675 - return new WP_REST_Response($payload, 200);
676 -}
677 -
678 -/**
679 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
680 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
681 - * action (inline-localized in older cached HTML). The legacy acceptance is
682 - * a 30-day backwards-compat window — to be removed in a follow-up release
683 - * after 2026-06-27.
684 - *
685 - * @param string $posted_nonce
686 - * @return bool
687 - */
688 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
689 - if (!is_string($posted_nonce) || $posted_nonce === '') {
690 - return false;
691 - }
692 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
693 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
694 -}
695 -
696 -/**
697 238 * Verify valid chat session
698 239 */
699 240 public function verify_chat_session($request) {
700 241 $session_id = $request->get_param('session_id');
@@ -730,11 +271,10 @@
730 271 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
731 272 return false;
732 273 }
733 274
734 - // Get raw request body from the WP_REST_Request object
735 - // (php://input may already be consumed by WordPress at this point)
736 - $request_body = $request->get_body();
275 + // Get raw request body
276 + $request_body = file_get_contents('php://input');
737 277
738 278 // Create the signature base string
739 279 $sig_basestring = "v0:{$timestamp}:{$request_body}";
740 280
@@ -743,43 +283,8 @@
743 283
744 284 // Compare signatures
745 285 return hash_equals($my_signature, $slack_signature);
746 286 }
747 -
748 -/**
749 - * Verify request is coming from Telegram.
750 - *
751 - * @param WP_REST_Request $request
752 - * @return bool True if valid, false otherwise.
753 - */
754 -public function verify_telegram_request($request) {
755 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
756 -
757 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
758 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
759 -
760 - if (empty($secret_token)) {
761 - // If no secret is configured, allow the request (for initial setup)
762 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
763 - return true;
764 - }
765 -
766 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
767 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
768 -
769 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
770 -
771 - if (empty($request_token)) {
772 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
773 - return false;
774 - }
775 -
776 - // Timing-safe comparison
777 - $result = hash_equals($secret_token, $request_token);
778 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
779 - return $result;
780 -}
781 -
782 287 public function mxchat_stream_events(WP_REST_Request $request) {
783 288 header('Content-Type: text/event-stream');
784 289 header('Cache-Control: no-cache');
785 290 header('Connection: keep-alive');
@@ -813,9 +318,9 @@
813 318
814 319
815 320
816 321
817 -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) {
818 323 global $wpdb;
819 324 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
820 325 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
821 326
@@ -827,26 +332,14 @@
827 332 $session_id
828 333 ));
829 334 $is_new_session = ($existing_messages == 0);
830 335
831 - // Log for debugging
336 + // NEW: Log for debugging
832 337 if ($is_new_session) {
833 338 //error_log("[DEBUG] This is a NEW session - first message");
834 339 }
835 340 }
836 341
837 - // SECURITY FIX: Set session ownership for new sessions
838 - if ($is_new_session && $role === 'user') {
839 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
840 - $session_owner_key = "mxchat_session_owner_{$session_id}";
841 -
842 - // Only set ownership if not already set
843 - if (!get_option($session_owner_key)) {
844 - update_option($session_owner_key, $current_user_identifier, 'no');
845 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
846 - }
847 - }
848 -
849 342 // 1) Extract agent name if present
850 343 $agent_name = '';
851 344 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
852 345 $agent_name = $matches[1];
@@ -878,9 +371,9 @@
878 371 $email_option_key = "mxchat_email_{$session_id}";
879 372 $saved_email = get_option($email_option_key);
880 373 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
881 374
882 - // Check for a saved name in wp_options
375 + // NEW: Check for a saved name in wp_options
883 376 $name_option_key = "mxchat_name_{$session_id}";
884 377 $saved_name = get_option($name_option_key);
885 378 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
886 379
@@ -923,9 +416,9 @@
923 416 $insert_data = [
924 417 'user_id' => $user_id,
925 418 'user_identifier'=> $user_identifier,
926 419 'user_email' => $saved_email ?: $user_email,
927 - 'user_name' => $saved_name ?: '', // Add name to insert data
420 + 'user_name' => $saved_name ?: '', // NEW: Add name to insert data
928 421 'session_id' => $session_id,
929 422 'role' => $role,
930 423 'message' => $message,
931 424 'timestamp' => current_time('mysql', 1),
@@ -951,11 +444,10 @@
951 444 $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
952 445
953 446 //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
954 447
955 - // Clear after using (= null, not unset(): unset() undeclares the property
956 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
957 - $this->pending_originating_page = null;
448 + // Clear after using
449 + unset($this->pending_originating_page);
958 450 }
959 451 // Fallback to HTTP_REFERER if nothing else is available
960 452 else if (isset($_SERVER['HTTP_REFERER'])) {
961 453 $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
@@ -990,17 +482,9 @@
990 482 $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
991 483 }
992 484 }
993 485 }
994 -
995 - // Add RAG context if provided (for bot messages)
996 - if ($rag_context !== null && $role === 'bot') {
997 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
998 - if ($rag_context_column_exists) {
999 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
1000 - }
1001 - }
1002 -
486 +
1003 487 $wpdb->insert($table_name, $insert_data);
1004 488 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
1005 489
1006 490 // 9) Send notification email if this is the first user message in a new session
@@ -1011,17 +495,11 @@
1011 495 'ip' => $_SERVER['REMOTE_ADDR']
1012 496 ));
1013 497 }
1014 498
1015 - // 10) Schedule delayed transcript email if enabled and message is from user
1016 - if ($wpdb->insert_id && $role === 'user') {
1017 - $this->schedule_delayed_transcript_email($session_id);
1018 - }
1019 -
1020 499 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1021 500 return $message_id;
1022 501 }
1023 -
1024 502 private function send_new_chat_notification($session_id, $user_info = array()) {
1025 503 $options = get_option('mxchat_transcripts_options');
1026 504
1027 505 // Check if notifications are enabled
@@ -1064,202 +542,14 @@
1064 542 // Send email
1065 543 return wp_mail($to, $subject, $message);
1066 544 }
1067 545
1068 -/**
1069 - * Schedule delayed transcript email for a session
1070 - * Reschedules if a new user message is received
1071 - */
1072 -private function schedule_delayed_transcript_email($session_id) {
1073 - $options = get_option('mxchat_transcripts_options');
1074 -
1075 - // Check if auto-email is enabled
1076 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1077 - return;
1078 - }
1079 -
1080 - // Get notification email
1081 - $email = !empty($options['mxchat_notification_email']) ?
1082 - $options['mxchat_notification_email'] :
1083 - get_option('admin_email');
1084 -
1085 - if (!is_email($email)) {
1086 - return;
1087 - }
1088 -
1089 - // Get delay in minutes (default 30)
1090 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1091 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1092 -
1093 - // Clear any existing scheduled event for this session
1094 - $hook = 'mxchat_send_delayed_transcript';
1095 - $args = array($session_id);
1096 - $timestamp = wp_next_scheduled($hook, $args);
1097 -
1098 - if ($timestamp) {
1099 - wp_unschedule_event($timestamp, $hook, $args);
1100 - }
1101 -
1102 - // Schedule new event
1103 - $schedule_time = time() + ($delay_minutes * 60);
1104 - wp_schedule_single_event($schedule_time, $hook, $args);
1105 -}
1106 -
1107 -/**
1108 - * Check if chat messages contain contact information (email or phone number)
1109 - *
1110 - * @param array $messages Array of message objects with 'message' property
1111 - * @param object|null $session_data Session data object with user_email property
1112 - * @return bool True if contact info found, false otherwise
1113 - */
1114 -private function chat_contains_contact_info($messages, $session_data = null) {
1115 - // Check if session already has a stored email
1116 - if ($session_data && !empty($session_data->user_email)) {
1117 - return true;
1118 - }
1119 -
1120 - // Email regex pattern
1121 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1122 -
1123 - // Phone number patterns (covers various formats including international, WhatsApp style)
1124 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1125 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1126 -
1127 - // Only check user messages (not assistant responses)
1128 - foreach ($messages as $msg) {
1129 - if ($msg->role !== 'user') {
1130 - continue;
1131 - }
1132 -
1133 - $message_text = $msg->message;
1134 -
1135 - // Check for email
1136 - if (preg_match($email_pattern, $message_text)) {
1137 - return true;
1138 - }
1139 -
1140 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1141 - if (preg_match($phone_pattern, $message_text, $matches)) {
1142 - // Count actual digits to avoid matching short numbers
1143 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1144 - if (strlen($digits_only) >= 7) {
1145 - return true;
1146 - }
1147 - }
1148 - }
1149 -
1150 - return false;
1151 -}
1152 -
1153 -/**
1154 - * Send the delayed transcript email with .txt attachment
1155 - */
1156 -public function mxchat_send_delayed_transcript($session_id) {
1157 - global $wpdb;
1158 -
1159 - $options = get_option('mxchat_transcripts_options');
1160 -
1161 - // Get notification email
1162 - $to = !empty($options['mxchat_notification_email']) ?
1163 - $options['mxchat_notification_email'] :
1164 - get_option('admin_email');
1165 -
1166 - if (!is_email($to)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get all messages for this session
1171 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1172 - $messages = $wpdb->get_results($wpdb->prepare(
1173 - "SELECT role, message, timestamp FROM {$table_name}
1174 - WHERE session_id = %s
1175 - ORDER BY timestamp ASC",
1176 - $session_id
1177 - ));
1178 -
1179 - if (empty($messages)) {
1180 - return false;
1181 - }
1182 -
1183 - // Get session metadata
1184 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1185 - $session_data = $wpdb->get_row($wpdb->prepare(
1186 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1187 - $session_id
1188 - ));
1189 -
1190 - // Check if contact info is required and if it's present
1191 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1192 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1193 - // Contact info required but not found - skip sending
1194 - return false;
1195 - }
1196 -
1197 - // Build transcript content
1198 - $transcript_content = "Chat Transcript\n";
1199 - $transcript_content .= "================\n\n";
1200 - $transcript_content .= "Session ID: " . $session_id . "\n";
1201 -
1202 - if ($session_data) {
1203 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1204 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1205 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1206 - }
1207 -
1208 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1209 -
1210 - // Add messages
1211 - foreach ($messages as $msg) {
1212 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1213 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1214 - $transcript_content .= $msg->message . "\n\n";
1215 - }
1216 -
1217 - // Create temporary file for attachment using WP_Filesystem
1218 - $upload_dir = wp_upload_dir();
1219 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1220 - global $wp_filesystem;
1221 - if (empty($wp_filesystem)) {
1222 - require_once ABSPATH . 'wp-admin/includes/file.php';
1223 - WP_Filesystem();
1224 - }
1225 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1226 -
1227 - // Prepare email
1228 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1229 -
1230 - $message = "Please find attached the full chat transcript.\n\n";
1231 - $message .= "Session ID: {$session_id}\n";
1232 -
1233 - if ($session_data) {
1234 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1235 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1236 - }
1237 -
1238 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1239 -
1240 - // Send email with attachment
1241 - $attachments = array($temp_file);
1242 - $result = wp_mail($to, $subject, $message, '', $attachments);
1243 -
1244 - // Clean up temporary file
1245 - if (file_exists($temp_file)) {
1246 - unlink($temp_file);
1247 - }
1248 -
1249 - return $result;
1250 -}
1251 -
1252 -
1253 -
1254 546 public function mxchat_handle_save_email_and_response() {
1255 547 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1256 548 //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1257 549
1258 - nocache_headers();
1259 -
1260 550 // Validate nonce
1261 - 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')) {
1262 552 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1263 553 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1264 554 wp_die();
1265 555 }
@@ -1269,15 +559,15 @@
1269 559 $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1270 560
1271 561 //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
1272 562
1273 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
563 + if (empty($session_id) || empty($email)) {
1274 564 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1275 565 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1276 566 wp_die();
1277 567 }
1278 568
1279 - // Validate name if provided (check if name field is enabled and name is required)
569 + // NEW: Validate name if provided (check if name field is enabled and name is required)
1280 570 $options = get_option('mxchat_options', []);
1281 571 $name_field_enabled = isset($options['enable_name_field']) &&
1282 572 ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1283 573
@@ -1288,15 +578,15 @@
1288 578 }
1289 579
1290 580 // 1) Always store email in wp_options
1291 581 $email_option_key = "mxchat_email_{$session_id}";
1292 - update_option($email_option_key, $email, 'no');
582 + update_option($email_option_key, $email);
1293 583 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1294 584
1295 - // Store name in wp_options if provided
585 + // NEW: Store name in wp_options if provided
1296 586 if (!empty($name)) {
1297 587 $name_option_key = "mxchat_name_{$session_id}";
1298 - update_option($name_option_key, $name, 'no');
588 + update_option($name_option_key, $name);
1299 589 //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1300 590 }
1301 591
1302 592 // 2) (Optional) Also store in DB if a row already exists
@@ -1309,9 +599,9 @@
1309 599
1310 600 //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1311 601
1312 602 if ($session_count) {
1313 - // Update both user_email and user_name if row(s) exist
603 + // NEW: Update both user_email and user_name if row(s) exist
1314 604 if (!empty($name)) {
1315 605 $update_sql = $wpdb->prepare(
1316 606 "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1317 607 $email,
@@ -1340,17 +630,15 @@
1340 630
1341 631 public function mxchat_check_email_provided() {
1342 632 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1343 633
1344 - nocache_headers();
1345 -
1346 - 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')) {
1347 635 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1348 636 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1349 637 }
1350 638
1351 639 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1352 - if (empty($session_id) || $session_id === 'null') {
640 + if (empty($session_id)) {
1353 641 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1354 642 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1355 643 }
1356 644
@@ -1358,9 +646,9 @@
1358 646 if (is_user_logged_in()) {
1359 647 $current_user = wp_get_current_user();
1360 648 //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1361 649
1362 - // Get user's display name for logged in users
650 + // NEW: Get user's display name for logged in users
1363 651 $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1364 652 (!empty($current_user->first_name) ? $current_user->first_name : '');
1365 653
1366 654 $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
@@ -1370,9 +658,9 @@
1370 658
1371 659 wp_send_json_success($response_data);
1372 660 }
1373 661
1374 - // Check if name field is required
662 + // NEW: Check if name field is required
1375 663 $options = get_option('mxchat_options', []);
1376 664 $name_field_enabled = isset($options['enable_name_field']) &&
1377 665 ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1378 666
@@ -1378,9 +666,9 @@
1378 666
1379 667 $email_option_key = "mxchat_email_{$session_id}";
1380 668 $stored_email = get_option($email_option_key, '');
1381 669
1382 - // Check for stored name
670 + // NEW: Check for stored name
1383 671 $name_option_key = "mxchat_name_{$session_id}";
1384 672 $stored_name = get_option($name_option_key, '');
1385 673
1386 674 //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
@@ -1385,9 +673,9 @@
1385 673
1386 674 //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1387 675 //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1388 676
1389 - // Check if we have email and name (if name is required)
677 + // NEW: Check if we have email and name (if name is required)
1390 678 $has_required_info = !empty($stored_email);
1391 679
1392 680 if ($name_field_enabled) {
1393 681 $has_required_info = $has_required_info && !empty($stored_name);
@@ -1407,59 +695,32 @@
1407 695 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1408 696 }
1409 697 }
1410 698
1411 -/**
1412 - * Send error response in appropriate format based on streaming mode
1413 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1414 - *
1415 - * @param string $error_message The error message to display
1416 - * @param string $error_code Optional error code for debugging
1417 - */
1418 -private function send_error_response($error_message, $error_code = 'api_error') {
1419 - if ($this->is_streaming) {
1420 - echo "data: " . json_encode([
1421 - 'error' => true,
1422 - 'error_message' => $error_message,
1423 - 'error_code' => $error_code,
1424 - 'text' => $error_message,
1425 - 'message' => $error_message
1426 - ]) . "\n\n";
1427 - echo "data: [DONE]\n\n";
1428 - flush();
1429 - } else {
1430 - wp_send_json_error([
1431 - 'error_message' => $error_message,
1432 - 'error_code' => $error_code
1433 - ]);
1434 - }
1435 - wp_die();
1436 -}
1437 -
1438 699 public function mxchat_handle_chat_request() {
1439 700 global $wpdb;
1440 701
1441 - // Debug: Log incoming bot_id
1442 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1443 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1444 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
702 + // NEW: Check if this is a streaming request
703 + $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat';
1445 704
1446 - // Get bot-specific options
1447 - $bot_options = $this->get_bot_options($bot_id);
1448 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
705 + // NEW: Set streaming headers if needed
706 + if ($is_streaming) {
707 + // Disable output buffering
708 + while (ob_get_level()) {
709 + ob_end_flush(); // Changed from ob_end_clean()
710 + }
711 +
712 + // Set headers for SSE
713 + header('Content-Type: text/event-stream');
714 + header('Cache-Control: no-cache');
715 + header('Connection: keep-alive');
716 + header('X-Accel-Buffering: no');
717 +
718 + // Add these new lines:
719 + ob_implicit_flush(true);
720 + flush();
721 + }
1449 722
1450 - // Check if this is a streaming request
1451 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1452 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1453 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1454 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1455 -
1456 - // ADDED: Store streaming state in class property for use in private methods
1457 - $this->is_streaming = $is_streaming;
1458 -
1459 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1460 - // This allows actions/forms to return JSON responses without header conflicts
1461 -
1462 723 // Check if MX Chat Moderation is active
1463 724 if (class_exists('MX_Chat_Moderation')) {
1464 725 // Get user email and IP
1465 726 $user_email = '';
@@ -1496,12 +757,8 @@
1496 757 }
1497 758
1498 759 $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1499 760 $this->productCardHtml = '';
1500 - // Reset the per-turn function-calling UI capture (plan 48a57a).
1501 - $this->fc_ui_html = '';
1502 - $this->fc_ui_images = array();
1503 - $this->fc_ui_captured = false;
1504 761
1505 762 // Get the actual WordPress user ID if logged in
1506 763 $is_logged_in = is_user_logged_in();
1507 764 if ($is_logged_in) {
@@ -1528,31 +785,13 @@
1528 785
1529 786 // Rest of your existing code...
1530 787 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1531 788
1532 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1533 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1534 - // the frontend FormData.append() to stringify a null session_id into the literal
1535 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1536 - // ghost sessions that group every visitor's first message under one row.
1537 - if ($session_id === 'null' || $session_id === 'undefined') {
1538 - $session_id = '';
1539 - }
1540 -
1541 789 if (empty($session_id)) {
1542 790 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1543 791 wp_die();
1544 792 }
1545 793
1546 - // Update session owner if it changed (e.g. IP changed due to network switch)
1547 - // The session ID itself is the authentication — if the client has it, they own it
1548 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1549 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1550 -
1551 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1552 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1553 - }
1554 -
1555 794 // Validate and sanitize the incoming message
1556 795 if (empty($_POST['message'])) {
1557 796 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1558 797 wp_die();
@@ -1558,191 +797,209 @@
1558 797 wp_die();
1559 798 }
1560 799
1561 800
1562 - // Track originating page for first message in session
1563 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
801 + // NEW: Track originating page for first message in session
802 +$table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1564 803
1565 - // Check if originating page columns exist
1566 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
804 +// Check if originating page columns exist
805 +$columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1567 806
1568 - if ($columns_exist) {
1569 - // Check if this session already has messages
1570 - $message_count = $wpdb->get_var($wpdb->prepare(
1571 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1572 - $session_id
1573 - ));
807 +if ($columns_exist) {
808 + // Check if this session already has messages
809 + $message_count = $wpdb->get_var($wpdb->prepare(
810 + "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
811 + $session_id
812 + ));
813 +
814 + // If this is the first message in the session
815 + if ($message_count == 0) {
816 + // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
817 + $originating_url = '';
818 + $originating_title = '';
1574 819
1575 - // If this is the first message in the session
1576 - if ($message_count == 0) {
1577 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1578 - $originating_url = '';
1579 - $originating_title = '';
820 + // Try to get from POST data first (sent by JavaScript)
821 + if (isset($_POST['current_page_url'])) {
822 + $originating_url = esc_url_raw($_POST['current_page_url']);
823 + $originating_title = isset($_POST['current_page_title'])
824 + ? sanitize_text_field($_POST['current_page_title'])
825 + : '';
826 + }
827 + // Fallback to HTTP_REFERER if not provided by JavaScript
828 + else if (isset($_SERVER['HTTP_REFERER'])) {
829 + $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
830 + }
831 +
832 + // Generate title if we have URL but no title
833 + if ($originating_url && empty($originating_title)) {
834 + $parsed_url = parse_url($originating_url);
835 + $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1580 836
1581 - // Try to get from POST data first (sent by JavaScript)
1582 - if (isset($_POST['current_page_url'])) {
1583 - $originating_url = esc_url_raw($_POST['current_page_url']);
1584 - $originating_title = isset($_POST['current_page_title'])
1585 - ? sanitize_text_field($_POST['current_page_title'])
1586 - : '';
837 + if (empty($path) || $path === 'index.php' || $path === 'index.html') {
838 + $originating_title = 'Homepage';
839 + } else {
840 + // Clean up the path to make a readable title
841 + $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
842 + $originating_title = ucwords(trim($originating_title));
1587 843 }
1588 - // Fallback to HTTP_REFERER if not provided by JavaScript
1589 - else if (isset($_SERVER['HTTP_REFERER'])) {
1590 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1591 - }
1592 -
1593 - // Generate title if we have URL but no title
1594 - if ($originating_url && empty($originating_title)) {
1595 - $parsed_url = parse_url($originating_url);
1596 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1597 -
1598 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1599 - $originating_title = 'Homepage';
1600 - } else {
1601 - // Clean up the path to make a readable title
1602 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1603 - $originating_title = ucwords(trim($originating_title));
1604 - }
1605 - }
1606 -
1607 - // Store for later use when saving the message
1608 - $this->pending_originating_page = [
1609 - 'url' => $originating_url,
1610 - 'title' => $originating_title
1611 - ];
1612 844 }
845 +
846 + // Store for later use when saving the message
847 + $this->pending_originating_page = [
848 + 'url' => $originating_url,
849 + 'title' => $originating_title
850 + ];
1613 851 }
852 +}
853 +
854 +
855 +
856 + // NEW: Get page context if provided
857 + $page_context = null;
858 + if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
859 + $page_context_raw = stripslashes($_POST['page_context']);
860 + $page_context = json_decode($page_context_raw, true);
1614 861
1615 -
1616 -
1617 - // Get page context if provided
1618 - $page_context = null;
1619 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1620 - $page_context_raw = stripslashes($_POST['page_context']);
1621 - $page_context = json_decode($page_context_raw, true);
862 + // Validate page context structure
863 + if (is_array($page_context) &&
864 + isset($page_context['url']) &&
865 + isset($page_context['title']) &&
866 + isset($page_context['content'])) {
1622 867
1623 - // Validate page context structure
1624 - if (is_array($page_context) &&
1625 - isset($page_context['url']) &&
1626 - isset($page_context['title']) &&
1627 - isset($page_context['content'])) {
1628 -
1629 - // Sanitize page context
1630 - $page_context['url'] = esc_url_raw($page_context['url']);
1631 - $page_context['title'] = sanitize_text_field($page_context['title']);
1632 - $page_context['content'] = wp_kses_post($page_context['content']);
1633 - } else {
1634 - $page_context = null;
1635 - }
868 + // Sanitize page context
869 + $page_context['url'] = esc_url_raw($page_context['url']);
870 + $page_context['title'] = sanitize_text_field($page_context['title']);
871 + $page_context['content'] = wp_kses_post($page_context['content']);
872 + } else {
873 + $page_context = null;
1636 874 }
875 + }
1637 876
1638 - // Modify the message sanitization to preserve PHP tags in code blocks
1639 - $allowed_tags = [
1640 - 'pre' => [],
1641 - 'code' => ['class' => true],
1642 - 'span' => ['class' => true],
1643 - 'div' => ['class' => true],
1644 - ];
877 + // Modify the message sanitization to preserve PHP tags in code blocks
878 + $allowed_tags = [
879 + 'pre' => [],
880 + 'code' => ['class' => true],
881 + 'span' => ['class' => true],
882 + 'div' => ['class' => true],
883 + ];
1645 884
1646 - // First preserve code blocks
1647 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1648 - return htmlspecialchars_decode($matches[0]);
1649 - }, $_POST['message']);
885 + // First preserve code blocks
886 + $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
887 + return htmlspecialchars_decode($matches[0]);
888 + }, $_POST['message']);
1650 889
1651 - // Then apply sanitization
1652 - $message = wp_kses($message, $allowed_tags);
890 + // Then apply sanitization
891 + $message = wp_kses($message, $allowed_tags);
1653 892
1654 - // Preserve code blocks from markdown conversion
1655 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1656 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
893 + // Preserve code blocks from markdown conversion
894 + $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
895 + $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1657 896
1658 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1659 - // Always initialize testing data for admins (no toggle needed)
1660 - $testing_data = null;
1661 - if (current_user_can('administrator')) {
1662 - // For vision messages, use the original user message for the query display
1663 - $query_for_testing = $message;
1664 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1665 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1666 - }
1667 -
1668 - $testing_data = [
1669 - 'query' => $query_for_testing,
1670 - 'timestamp' => time(),
1671 - 'top_matches' => [],
1672 - 'action_matches' => [], // Initialize action matches array
1673 - 'page_context' => $page_context, // Include page context in testing data
1674 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1675 - 'bot_id' => $bot_id // Include bot ID in testing data
1676 - ];
1677 -
1678 - // Get similarity threshold from bot options or default options
1679 - $similarity_threshold = isset($current_options['similarity_threshold'])
1680 - ? ((int) $current_options['similarity_threshold']) / 100
1681 - : 0.35;
1682 -
1683 - $testing_data['similarity_threshold'] = $similarity_threshold;
1684 -
1685 - // Determine knowledge base type using bot-specific config
1686 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1687 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1688 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
897 +// ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
898 + // Always initialize testing data for admins (no toggle needed)
899 + $testing_data = null;
900 + if (current_user_can('administrator')) {
901 + // For vision messages, use the original user message for the query display
902 + $query_for_testing = $message;
903 + if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
904 + $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1689 905 }
1690 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
906 +
907 + $testing_data = [
908 + 'query' => $query_for_testing,
909 + 'timestamp' => time(),
910 + 'top_matches' => [],
911 + 'action_matches' => [], // NEW: Initialize action matches array
912 + 'page_context' => $page_context, // NEW: Include page context in testing data
913 + 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed']
914 + ];
915 +
916 + // Get similarity threshold
917 + $similarity_threshold = isset($this->options['similarity_threshold'])
918 + ? ((int) $this->options['similarity_threshold']) / 100
919 + : 0.75;
920 +
921 + $testing_data['similarity_threshold'] = $similarity_threshold;
922 +
923 + // Determine knowledge base type
924 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
925 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
926 + $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
927 + }
928 + // ===== END SIMPLIFIED TESTING INITIALIZATION =====
1691 929
1692 - // Add debug before and after:
1693 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1694 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1695 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
930 +// Add debug before and after:
931 +//error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
932 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
933 +//error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
1696 934
1697 935
1698 - // If the pre-processing returned a result (not the original message), use it directly
1699 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1700 - // Save the AI response
1701 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1702 -
1703 - // Save HTML content if provided
1704 - if (!empty($pre_processed_result['html'])) {
1705 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1706 - }
1707 -
1708 - // Add testing data if admin
1709 - $response_data = [
1710 - 'text' => $pre_processed_result['text'],
1711 - 'html' => $pre_processed_result['html'] ?? '',
1712 - 'session_id' => $session_id
1713 - ];
1714 -
1715 - if ($testing_data !== null) {
1716 - $response_data['testing_data'] = $testing_data;
1717 - }
1718 -
1719 - wp_send_json($response_data);
1720 - wp_die();
936 + // If the pre-processing returned a result (not the original message), use it directly
937 + if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
938 + // Save the AI response
939 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
940 +
941 + // Save HTML content if provided
942 + if (!empty($pre_processed_result['html'])) {
943 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1721 944 }
945 +
946 + // Add testing data if admin
947 + $response_data = [
948 + 'text' => $pre_processed_result['text'],
949 + 'html' => $pre_processed_result['html'] ?? '',
950 + 'session_id' => $session_id
951 + ];
952 +
953 + if ($testing_data !== null) {
954 + $response_data['testing_data'] = $testing_data;
955 + }
956 +
957 + wp_send_json($response_data);
958 + wp_die();
959 + }
1722 960
1723 - // Save the user's message - handle vision processed messages differently
1724 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1725 - // For vision messages, save the original user message with image indicator
1726 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1727 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1728 - $image_count = intval($_POST['vision_images_count']);
1729 - $original_message .= " [{$image_count} image(s)]";
1730 - }
1731 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1732 - } else {
1733 - // Regular message - save as normal
1734 - $this->mxchat_save_chat_message($session_id, 'user', $message);
961 + // Save the user's message - handle vision processed messages differently
962 + if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
963 + // For vision messages, save the original user message with image indicator
964 + $original_message = sanitize_textarea_field($_POST['original_user_message']);
965 + if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
966 + $image_count = intval($_POST['vision_images_count']);
967 + $original_message .= " [{$image_count} image(s)]";
1735 968 }
969 + $this->mxchat_save_chat_message($session_id, 'user', $original_message);
970 + } else {
971 + // Regular message - save as normal
972 + $this->mxchat_save_chat_message($session_id, 'user', $message);
973 + }
1736 974
975 +
976 +if (is_email($message)) {
977 + // Add the email to Loops
978 + $this->add_email_to_loops($message);
1737 979
1738 - if (is_email($message)) {
1739 - // Add the email to Loops
1740 - $this->add_email_to_loops($message);
980 + // Get the user's success message instruction
981 + $user_success_message = $this->options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
982 +
983 + // Set instruction for AI using the user's success message
984 + $this->current_action_instruction = $user_success_message;
985 +
986 + // Clear the email capture transient since we got the email
987 + delete_transient('mxchat_email_capture_' . $user_id);
988 + }
989 +
990 + // NEW: Check if we're in an email capture flow but user hasn't provided email yet
991 + elseif (get_transient('mxchat_email_capture_' . $user_id)) {
992 + // Check if the message contains an email (not the whole message being an email)
993 + if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
994 + $extracted_email = $matches[0];
1741 995
1742 - // Get the user's success message instruction using current_options
1743 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
996 + // Add the extracted email to Loops
997 + $this->add_email_to_loops($extracted_email);
1744 998
999 + // Get the user's success message instruction
1000 + $user_success_message = $this->options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1001 +
1745 1002 // Set instruction for AI using the user's success message
1746 1003 $this->current_action_instruction = $user_success_message;
1747 1004
1748 1005 // Clear the email capture transient since we got the email
@@ -1747,750 +1004,468 @@
1747 1004
1748 1005 // Clear the email capture transient since we got the email
1749 1006 delete_transient('mxchat_email_capture_' . $user_id);
1750 1007 }
1751 -
1752 - // Check if we're in an email capture flow but user hasn't provided email yet
1753 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1754 - // Check if the message contains an email (not the whole message being an email)
1755 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1756 - $extracted_email = $matches[0];
1757 -
1758 - // Add the extracted email to Loops
1759 - $this->add_email_to_loops($extracted_email);
1760 -
1761 - // Get the user's success message instruction using current_options
1762 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1763 -
1764 - // Set instruction for AI using the user's success message
1765 - $this->current_action_instruction = $user_success_message;
1766 -
1767 - // Clear the email capture transient since we got the email
1768 - delete_transient('mxchat_email_capture_' . $user_id);
1769 - }
1770 - // If no email found but we're in capture mode, remind them
1771 - else {
1772 - // Get the original instruction to remind them using current_options
1773 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1774 - $this->current_action_instruction = $original_instruction;
1775 - }
1008 + // If no email found but we're in capture mode, remind them
1009 + else {
1010 + // Get the original instruction to remind them
1011 + $original_instruction = $this->options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1012 + $this->current_action_instruction = $original_instruction;
1776 1013 }
1014 + }
1777 1015
1778 - $intent_info = '';
1016 + $intent_info = '';
1779 1017
1780 - // Check chat mode
1781 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1018 + // Check chat mode
1019 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1782 1020
1783 - // Handle agent mode
1784 1021 // Handle agent mode
1785 - if ($chat_mode === 'agent') {
1786 - // First, check for switch intent before doing anything else
1787 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1022 +// Handle agent mode
1023 + if ($chat_mode === 'agent') {
1024 + // First, check for switch intent before doing anything else
1025 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1788 1026
1789 - // Capture action analysis for testing panel after intent check
1790 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1791 - $testing_data['action_matches'] = $this->last_action_analysis;
1027 + // NEW: Capture action analysis for testing panel after intent check
1028 + if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1029 + $testing_data['action_matches'] = $this->last_action_analysis;
1030 + }
1031 +
1032 + // Around line 506, in the agent mode handling section:
1033 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1034 + // Update chat mode first
1035 + update_option("mxchat_mode_{$session_id}", 'ai');
1036 +
1037 + // Clear any existing PDF context to start fresh
1038 + $this->clear_pdf_transients($session_id);
1039 +
1040 + // Prepare clean switch response with explicit chat_mode
1041 + $response_data = [
1042 + 'text' => $this->fallbackResponse['text'],
1043 + 'html' => $this->fallbackResponse['html'] ?? '',
1044 + 'session_id' => $session_id,
1045 + 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1046 + ];
1047 +
1048 + if ($testing_data !== null) {
1049 + $response_data['testing_data'] = $testing_data;
1792 1050 }
1793 -
1794 - // Around line 506, in the agent mode handling section:
1795 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1796 - // Update chat mode first
1797 - update_option("mxchat_mode_{$session_id}", 'ai');
1798 -
1799 - // Clear any existing PDF context to start fresh
1800 - $this->clear_pdf_transients($session_id);
1801 -
1802 - // Prepare clean switch response with explicit chat_mode
1803 - $response_data = [
1804 - 'text' => $this->fallbackResponse['text'],
1805 - 'html' => $this->fallbackResponse['html'] ?? '',
1806 - 'session_id' => $session_id,
1807 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1051 +
1052 + // Save the mode switch message
1053 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1054 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1055 +
1056 + // Send response and exit
1057 + wp_send_json($response_data);
1058 + wp_die();
1059 + } elseif (!$intent_matched) {
1060 + // No intent matched, handle live agent message
1061 + try {
1062 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1063 +
1064 + $agent_response = [
1065 + 'status' => 'waiting_for_agent',
1066 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1808 1067 ];
1809 -
1068 +
1810 1069 if ($testing_data !== null) {
1811 - $response_data['testing_data'] = $testing_data;
1070 + $agent_response['testing_data'] = $testing_data;
1812 1071 }
1813 -
1814 - // Save the mode switch message
1815 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1816 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1817 -
1818 - // Send response and exit
1819 - wp_send_json($response_data);
1820 - wp_die();
1821 - } elseif (!$intent_matched) {
1822 - // No intent matched, handle live agent message
1823 - try {
1824 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1825 1072
1826 - $agent_response = [
1827 - 'status' => 'waiting_for_agent',
1828 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1829 - ];
1830 -
1831 - if ($testing_data !== null) {
1832 - $agent_response['testing_data'] = $testing_data;
1833 - }
1834 -
1835 - wp_send_json_success($agent_response);
1836 - } catch (\Exception $e) {
1837 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1838 - }
1839 - wp_die();
1073 + wp_send_json_success($agent_response);
1074 + } catch (\Exception $e) {
1075 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1840 1076 }
1077 + wp_die();
1841 1078 }
1079 + }
1842 1080
1843 - // Step 1: Check for new PDF URL in the message
1844 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1845 - $new_pdf_url = $matches[0];
1081 + // Step 1: Check for new PDF URL in the message
1082 + if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1083 + $new_pdf_url = $matches[0];
1846 1084
1847 - // Check if this is likely a PDF-related request
1848 - $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1849 - $is_pdf_request = false;
1085 + // Check if this is likely a PDF-related request
1086 + $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1087 + $is_pdf_request = false;
1850 1088
1851 - foreach ($pdf_keywords as $keyword) {
1852 - if (stripos($message, $keyword) !== false) {
1853 - $is_pdf_request = true;
1854 - break;
1855 - }
1089 + foreach ($pdf_keywords as $keyword) {
1090 + if (stripos($message, $keyword) !== false) {
1091 + $is_pdf_request = true;
1092 + break;
1856 1093 }
1094 + }
1857 1095
1858 - // If it looks like a PDF request or we're waiting for a PDF URL
1859 - if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1860 - // Validate HTTPS
1861 - if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1862 - // Extract filename from URL
1863 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1096 + // If it looks like a PDF request or we're waiting for a PDF URL
1097 + if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1098 + // Validate HTTPS
1099 + if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1100 + // Extract filename from URL
1101 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1864 1102
1865 - // Clear previous PDF transients
1866 - $this->clear_pdf_transients($session_id);
1103 + // Clear previous PDF transients
1104 + $this->clear_pdf_transients($session_id);
1867 1105
1868 - // Process new PDF using current_options
1869 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
1870 - $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1106 + // Process new PDF
1107 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
1108 + $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1871 1109
1872 - if ($embeddings === 'too_many_pages') {
1873 - $error_text = sprintf(
1874 - $current_options['pdf_intent_error_text'] ??
1875 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1876 - $max_pages
1877 - );
1878 - $this->fallbackResponse['text'] = $error_text;
1879 - } elseif ($embeddings) {
1880 - // Store new PDF information
1881 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1110 + if ($embeddings === 'too_many_pages') {
1111 + $error_text = sprintf(
1112 + $this->options['pdf_intent_error_text'] ??
1113 + esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1114 + $max_pages
1115 + );
1116 + $this->fallbackResponse['text'] = $error_text;
1117 + } elseif ($embeddings) {
1118 + // Store new PDF information
1119 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1882 1120
1883 - // If the filename is generic, create a more descriptive one
1884 - if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1885 - strpos($pdf_filename, '.php') !== false) {
1886 - $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1887 - }
1121 + // If the filename is generic, create a more descriptive one
1122 + if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1123 + strpos($pdf_filename, '.php') !== false) {
1124 + $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1125 + }
1888 1126
1889 - set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1890 - set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1891 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1892 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1127 + set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1128 + set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1129 + set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1130 + set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1893 1131
1894 - $success_text = $current_options['pdf_intent_success_text'] ??
1895 - esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1132 + $success_text = $this->options['pdf_intent_success_text'] ??
1133 + esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1896 1134
1897 - $pdf_response = [
1898 - 'success' => true,
1899 - 'message' => $success_text,
1900 - 'data' => [
1901 - 'filename' => $pdf_filename
1902 - ]
1903 - ];
1904 -
1905 - if ($testing_data !== null) {
1906 - $pdf_response['testing_data'] = $testing_data;
1907 - }
1908 -
1909 - wp_send_json($pdf_response);
1910 - wp_die();
1911 - } else {
1912 - $error_text = $current_options['pdf_intent_error_text'] ??
1913 - esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1914 - $this->fallbackResponse['text'] = $error_text;
1915 - }
1916 -
1917 - $pdf_error_response = [
1918 - 'success' => false,
1919 - 'message' => $this->fallbackResponse['text']
1135 + $pdf_response = [
1136 + 'success' => true,
1137 + 'message' => $success_text,
1138 + 'data' => [
1139 + 'filename' => $pdf_filename
1140 + ]
1920 1141 ];
1921 1142
1922 1143 if ($testing_data !== null) {
1923 - $pdf_error_response['testing_data'] = $testing_data;
1144 + $pdf_response['testing_data'] = $testing_data;
1924 1145 }
1925 1146
1926 - wp_send_json($pdf_error_response);
1147 + wp_send_json($pdf_response);
1927 1148 wp_die();
1149 + } else {
1150 + $error_text = $this->options['pdf_intent_error_text'] ??
1151 + esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1152 + $this->fallbackResponse['text'] = $error_text;
1928 1153 }
1929 - }
1930 - }
1931 1154
1932 -
1933 - // Step 2: Detect intent and handle intent-based responses
1934 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1935 -
1936 - // Capture action analysis for testing panel after intent check
1937 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1938 - $testing_data['action_matches'] = $this->last_action_analysis;
1939 - }
1940 -
1941 - // Step 3: Handle the intent result appropriately
1942 - if ($intent_result !== false) {
1943 - // Intent was matched - ALWAYS send as JSON response, never streaming
1944 -
1945 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1946 - // Intent returned a direct response array
1947 - $response_data = [
1948 - 'text' => $intent_result['text'] ?? '',
1949 - 'html' => $intent_result['html'] ?? '',
1950 - 'session_id' => $session_id
1155 + $pdf_error_response = [
1156 + 'success' => false,
1157 + 'message' => $this->fallbackResponse['text']
1951 1158 ];
1952 -
1953 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1954 - if (isset($intent_result['chat_mode'])) {
1955 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1956 - }
1957 -
1159 +
1958 1160 if ($testing_data !== null) {
1959 - $response_data['testing_data'] = $testing_data;
1161 + $pdf_error_response['testing_data'] = $testing_data;
1960 1162 }
1961 1163
1962 - wp_send_json($response_data);
1164 + wp_send_json($pdf_error_response);
1963 1165 wp_die();
1964 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1965 - // Intent returned true and set fallbackResponse
1966 -
1967 - // SAVE TO TRANSCRIPT
1968 - if (!empty($this->fallbackResponse['text'])) {
1969 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1970 - }
1971 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1972 - if (!empty($this->fallbackResponse['html'])) {
1973 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1974 - }
1975 -
1976 - $response_data = [
1977 - 'text' => $this->fallbackResponse['text'] ?? '',
1978 - 'html' => $this->fallbackResponse['html'] ?? '',
1979 - 'session_id' => $session_id
1980 - ];
1981 -
1982 - if (isset($this->fallbackResponse['chat_mode'])) {
1983 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1984 - }
1985 -
1986 - if ($testing_data !== null) {
1987 - $response_data['testing_data'] = $testing_data;
1988 - }
1989 -
1990 - wp_send_json($response_data);
1991 - wp_die();
1992 1166 }
1993 1167 }
1168 + }
1994 1169
1995 - // If we get here, no intent matched OR the intent didn't provide a usable response
1996 -
1997 - // Step 4: Generate AI response
1998 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1999 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
2000 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
2001 - $this->mxchat_increment_chat_count();
1170 + // Check if there's an active recommendation flow session
1171 + $flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
1172 + if (!empty($flow_state) && isset($flow_state['flow_id'])) {
1173 + // Create a dummy intent object that matches the original intent
1174 + $dummy_intent = new stdClass();
1175 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
1176 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
2002 1177
2003 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
2004 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
2005 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
1178 + // Call the recommendation flow handler directly
1179 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
2006 1180
2007 - // Check if the embedding generation returned an error
2008 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
2009 - $error_message = $user_message_embedding['error'];
2010 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
2011 -
2012 - // FIXED: Send error in appropriate format based on streaming mode
2013 - if ($is_streaming) {
2014 - echo "data: " . json_encode([
2015 - 'error' => true,
2016 - 'error_message' => $error_message,
2017 - 'error_code' => $error_code,
2018 - 'text' => $error_message,
2019 - 'message' => $error_message
2020 - ]) . "\n\n";
2021 - echo "data: [DONE]\n\n";
2022 - flush();
2023 - } else {
2024 - wp_send_json_error([
2025 - 'error_message' => $error_message,
2026 - 'error_code' => $error_code
2027 - ]);
1181 + // If the handler returned a response, send it
1182 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
1183 + // Save the bot's response to the chat history
1184 + if (!empty($response_data['text'])) {
1185 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
2028 1186 }
1187 + if (!empty($response_data['html'])) {
1188 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
1189 + }
1190 +
1191 + if ($testing_data !== null) {
1192 + $response_data['testing_data'] = $testing_data;
1193 + }
1194 +
1195 + // Send the response
1196 + wp_send_json($response_data);
2029 1197 wp_die();
2030 1198 }
1199 + }
2031 1200
2032 - // Check if the embedding is valid
2033 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2034 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
1201 + // Step 2: Detect intent and handle intent-based responses
1202 + $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
2035 1203
2036 - // FIXED: Send error in appropriate format based on streaming mode
1204 + // NEW: Capture action analysis for testing panel after intent check
1205 + if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1206 + $testing_data['action_matches'] = $this->last_action_analysis;
1207 + }
1208 +
1209 + // Step 3: Handle the intent result appropriately
1210 + if ($intent_result !== false) {
1211 + // Intent was matched - ALWAYS send as JSON response, never streaming
1212 +
1213 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1214 + // Intent returned a direct response array
1215 + $response_data = [
1216 + 'text' => $intent_result['text'] ?? '',
1217 + 'html' => $intent_result['html'] ?? '',
1218 + 'session_id' => $session_id
1219 + ];
1220 +
1221 + if ($testing_data !== null) {
1222 + $response_data['testing_data'] = $testing_data;
1223 + }
1224 +
1225 + // Clear streaming headers if they were set
2037 1226 if ($is_streaming) {
2038 - echo "data: " . json_encode([
2039 - 'error' => true,
2040 - 'error_message' => $error_message,
2041 - 'error_code' => 'invalid_embedding',
2042 - 'text' => $error_message,
2043 - 'message' => $error_message
2044 - ]) . "\n\n";
2045 - echo "data: [DONE]\n\n";
2046 - flush();
2047 - } else {
2048 - wp_send_json_error([
2049 - 'error_message' => $error_message,
2050 - 'error_code' => 'invalid_embedding'
2051 - ]);
1227 + header_remove('Content-Type');
1228 + header_remove('Cache-Control');
1229 + header_remove('Connection');
1230 + header_remove('X-Accel-Buffering');
1231 + header('Content-Type: application/json');
2052 1232 }
1233 +
1234 + wp_send_json($response_data);
2053 1235 wp_die();
2054 - }
2055 -
2056 - // Build context with both knowledge base and PDF content if available
2057 - $context_content = "User asked: '{$message}'\n\n";
2058 -
2059 - // Add action instruction if present (add this right after the above line)
2060 - if (!empty($this->current_action_instruction)) {
2061 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2062 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2063 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2064 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
1236 + } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1237 + // Intent returned true and set fallbackResponse
2065 1238
2066 - // Clear the instruction after using it
2067 - $this->current_action_instruction = null;
2068 - }
2069 -
2070 -
2071 - // Add page context if available and contextual awareness is enabled using current_options
2072 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2073 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2074 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2075 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2076 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2077 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2078 - }
2079 -
2080 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2081 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2082 -
2083 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2084 - // Use fresh options to ensure we get the latest setting value
2085 - $fresh_options = get_option('mxchat_options', []);
2086 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2087 -
2088 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2089 - if ($citation_links_enabled && !empty($system_instructions)) {
2090 - preg_match_all(
2091 - '#\bhttps?://[^\s<>"\']+#i',
2092 - $system_instructions,
2093 - $system_instruction_urls
2094 - );
2095 -
2096 - if (!empty($system_instruction_urls[0])) {
2097 - // Merge with existing valid URLs
2098 - $this->current_valid_urls = array_merge(
2099 - $this->current_valid_urls,
2100 - $system_instruction_urls[0]
2101 - );
2102 - // Remove duplicates
2103 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2104 -
2105 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
1239 + // SAVE TO TRANSCRIPT FIRST
1240 + if (!empty($this->fallbackResponse['text'])) {
1241 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
2106 1242 }
2107 - }
2108 -
2109 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2110 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2111 - // Update testing data with the REAL similarity analysis
2112 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2113 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2114 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2115 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2116 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2117 -}
2118 -// ===== END SIMILARITY DATA CAPTURE =====
2119 -
2120 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2121 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2122 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2123 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2124 -}
2125 -
2126 - if (!empty($relevant_content)) {
2127 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2128 - } else {
2129 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2130 - }
2131 -
2132 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2133 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2134 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2135 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2136 - foreach ($this->current_valid_urls as $url) {
2137 - $context_content .= "- " . $url . "\n";
1243 + if (!empty($this->fallbackResponse['html'])) {
1244 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2138 1245 }
2139 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2140 - $context_content .= "===== END APPROVED URLS =====\n\n";
2141 - }
2142 -
2143 - // Check for and include PDF content
2144 - $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2145 - $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2146 - $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
2147 - if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
2148 - $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
2149 - if (!empty($relevant_pdf_pages)) {
2150 - $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
2151 - foreach ($relevant_pdf_pages as $page_data) {
2152 - $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2153 - }
2154 - $context_content .= "\n";
1246 +
1247 + $response_data = [
1248 + 'text' => $this->fallbackResponse['text'] ?? '',
1249 + 'html' => $this->fallbackResponse['html'] ?? '',
1250 + 'session_id' => $session_id
1251 + ];
1252 +
1253 + if (isset($this->fallbackResponse['chat_mode'])) {
1254 + $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
2155 1255 }
2156 - }
2157 -
2158 - // Check for and include Word content
2159 - $word_url = get_transient('mxchat_word_url_' . $session_id);
2160 - $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
2161 - $word_filename = get_transient('mxchat_word_filename_' . $session_id);
2162 - if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
2163 - $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
2164 - if (!empty($relevant_word_chunks)) {
2165 - $context_content .= "Relevant content from Word document '{$word_filename}':\n";
2166 - foreach ($relevant_word_chunks as $chunk_data) {
2167 - $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
2168 - }
2169 - $context_content .= "\n";
1256 +
1257 + if ($testing_data !== null) {
1258 + $response_data['testing_data'] = $testing_data;
2170 1259 }
2171 - }
2172 -
2173 - $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2174 -
2175 - // Extract model from current options for bot-specific model support
2176 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2177 -
2178 - // ===== Native function-calling fallback (plan-mxchat-20260617-a41dee) =====
2179 - // Intents already missed (we're past the intent router). If function
2180 - // calling is enabled and the active model is tool-capable, let the model
2181 - // SELECT and run registered callbacks as tools — independent of intents,
2182 - // works with zero Actions. The tool round is buffered; the final answer is
2183 - // emitted via the SAME envelopes the normal path uses. Default-off, so
2184 - // existing installs never enter this branch.
2185 - if ($this->mxchat_fc_should_run($selected_model)) {
2186 - $fc_outcome = $this->mxchat_fc_attempt(
2187 - $message,
2188 - $context_content,
2189 - $conversation_history,
2190 - $selected_model,
2191 - $current_options,
2192 - $session_id,
2193 - $user_id
2194 - );
2195 - if (is_array($fc_outcome) && !empty($fc_outcome['handled'])) {
2196 - $fc_text = isset($fc_outcome['text']) ? $fc_outcome['text'] : '';
2197 - if (!empty($this->current_valid_urls)) {
2198 - $fc_text = $this->validate_and_clean_urls($fc_text, $this->current_valid_urls);
2199 - }
2200 - // plan-mxchat-20260617-48a57a — surface any UI element a tool
2201 - // produced (generated image / product card / image gallery) so the
2202 - // widget RENDERS it, instead of emitting only the model's text.
2203 - // The html was already saved to the transcript in
2204 - // mxchat_fc_execute_tool (or by the callback itself for self-saving
2205 - // core tools), so we persist ONLY the model's caption text here.
2206 - $fc_html = isset($this->fc_ui_html) ? $this->fc_ui_html : '';
2207 -
2208 - if ($fc_text !== '') {
2209 - $this->mxchat_save_chat_message($session_id, 'bot', $fc_text, null, null);
2210 - }
2211 -
2212 - if ($is_streaming) {
2213 - // The frontend SSE reader routes any event carrying text/html
2214 - // to handleNonStreamResponse(), which renders text + html in a
2215 - // single bot message — so emit one complete event (mirrors the
2216 - // intent path's text/html envelope).
2217 - $sse = array('session_id' => $session_id);
2218 - if ($fc_text !== '') $sse['text'] = $fc_text;
2219 - if ($fc_html !== '') $sse['html'] = $fc_html;
2220 - if ($fc_text === '' && $fc_html === '') $sse['text'] = $this->mxchat_fc_giveup_text();
2221 - echo "data: " . wp_json_encode($sse) . "\n\n";
2222 - echo "data: [DONE]\n\n";
2223 - flush();
2224 - } else {
2225 - $fc_response_data = array('text' => $fc_text, 'html' => $fc_html, 'session_id' => $session_id);
2226 - if ($testing_data !== null) {
2227 - $fc_response_data['testing_data'] = $testing_data;
2228 - }
2229 - wp_send_json($fc_response_data);
2230 - }
2231 - wp_die();
1260 +
1261 + // Clear streaming headers if they were set
1262 + if ($is_streaming) {
1263 + header_remove('Content-Type');
1264 + header_remove('Cache-Control');
1265 + header_remove('Connection');
1266 + header_remove('X-Accel-Buffering');
1267 + header('Content-Type: application/json');
2232 1268 }
1269 +
1270 + wp_send_json($response_data);
1271 + wp_die();
2233 1272 }
2234 - // ===== end function-calling fallback =====
1273 + }
2235 1274
2236 - $response = $this->mxchat_generate_response(
2237 - $context_content,
2238 - $current_options['api_key'] ?? $this->options['api_key'],
2239 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2240 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2241 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2242 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2243 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2244 - $conversation_history,
2245 - $is_streaming,
2246 - $session_id,
2247 - $testing_data,
2248 - $selected_model
2249 - );
1275 + // If we get here, no intent matched OR the intent didn't provide a usable response
1276 +
1277 + // Step 4: Generate AI response
1278 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
1279 + $this->mxchat_increment_chat_count();
1280 +
1281 + // Generate embedding for the user's query
1282 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1283 +
1284 + // Check if the embedding generation returned an error
1285 + if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1286 + $error_message = $user_message_embedding['error'];
1287 + $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
2250 1288
2251 - // Handle streaming vs non-streaming responses
2252 - if ($is_streaming) {
2253 - // Check if streaming actually happened or if it fell back to regular response
2254 - if ($response === true) {
2255 - wp_die();
2256 - }
2257 - // If we get here, streaming fell back to regular response, continue
2258 - // But if there's an error, we need to send it as SSE format since headers are already set
2259 - if (is_array($response) && isset($response['error'])) {
2260 - $error_message = $response['error'];
2261 - $error_code = $response['error_code'] ?? 'api_error';
2262 - // Send error in SSE format that the client JS can handle
2263 - echo "data: " . json_encode([
2264 - 'error' => true,
2265 - 'error_message' => $error_message,
2266 - 'error_code' => $error_code,
2267 - 'text' => $error_message, // Also include as text for fallback handling
2268 - 'message' => $error_message
2269 - ]) . "\n\n";
2270 - echo "data: [DONE]\n\n";
2271 - flush();
2272 - wp_die();
2273 - }
2274 - }
1289 + wp_send_json_error([
1290 + 'error_message' => $error_message,
1291 + 'error_code' => $error_code
1292 + ]);
1293 + wp_die();
1294 + }
1295 +
1296 + // Check if the embedding is valid
1297 + if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
1298 + wp_send_json_error([
1299 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1300 + 'error_code' => 'invalid_embedding'
1301 + ]);
1302 + wp_die();
1303 + }
2275 1304
2276 - // Check if the response is an error array (non-streaming mode)
2277 - if (is_array($response) && isset($response['error'])) {
2278 - wp_send_json_error([
2279 - 'error_message' => $response['error'],
2280 - 'error_code' => $response['error_code'] ?? 'api_error'
2281 - ]);
2282 - wp_die();
2283 - }
1305 + // Build context with both knowledge base and PDF content if available
1306 + $context_content = "User asked: '{$message}'\n\n";
1307 +
1308 + // NEW: Add action instruction if present (add this right after the above line)
1309 + if (!empty($this->current_action_instruction)) {
1310 + $context_content .= "===== SPECIAL INSTRUCTION =====\n";
1311 + $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
1312 + $context_content .= "Respond naturally and conversationally while following this instruction.\n";
1313 + $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2284 1314
2285 - // DEBUG: Check what we have
2286 - //error_log("=== BEFORE URL VALIDATION ===");
2287 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2288 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2289 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2290 -
2291 - // If we get here, the response is valid text - now validate URLs
2292 - if (!empty($this->current_valid_urls)) {
2293 - //error_log("CALLING validate_and_clean_urls");
2294 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2295 - } else {
2296 - //error_log("SKIPPING validation - current_valid_urls is empty");
2297 - }
2298 - // ===== END URL VALIDATION =====
1315 + // Clear the instruction after using it
1316 + $this->current_action_instruction = null;
1317 + }
2299 1318
2300 - // Prepare RAG context data for storage (only include documents used for context)
2301 - $rag_context_for_storage = null;
2302 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2303 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2304 1319
2305 - if ($has_rag_data || $has_action_data) {
2306 - $rag_context_for_storage = [];
1320 + // NEW: Add page context if available and contextual awareness is enabled
1321 + if ($page_context && isset($this->options['contextual_awareness_toggle']) && $this->options['contextual_awareness_toggle'] === 'on') {
1322 + $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
1323 + $context_content .= "Page URL: " . $page_context['url'] . "\n";
1324 + $context_content .= "Page Title: " . $page_context['title'] . "\n";
1325 + $context_content .= "Page Content: " . $page_context['content'] . "\n";
1326 + $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
1327 + }
2307 1328
2308 - // Add RAG/source data if available
2309 - if ($has_rag_data) {
2310 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2311 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2312 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2313 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2314 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2315 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2316 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2317 - }
1329 + // Get relevant content from knowledge base - THIS IS WHERE THE SIMILARITY ANALYSIS HAPPENS
1330 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
1331 +
1332 + // ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
1333 + if ($testing_data !== null && $this->last_similarity_analysis !== null) {
1334 + // Update testing data with the REAL similarity analysis
1335 + $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
1336 + $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1337 + $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
1338 + }
1339 + // ===== END SIMILARITY DATA CAPTURE =====
1340 +
1341 + if (!empty($relevant_content)) {
1342 + $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
1343 + } else {
1344 + $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
1345 + }
2318 1346
2319 - // Add action analysis data if available
2320 - if ($has_action_data) {
2321 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
1347 + // Check for and include PDF content
1348 + $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
1349 + $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
1350 + $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
1351 + if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
1352 + $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
1353 + if (!empty($relevant_pdf_pages)) {
1354 + $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
1355 + foreach ($relevant_pdf_pages as $page_data) {
1356 + $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2322 1357 }
1358 + $context_content .= "\n";
2323 1359 }
1360 + }
2324 1361
2325 - // Save the cleaned response with RAG context
2326 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2327 -
2328 - // Step 5: Save additional content if available
2329 - if (!empty($this->productCardHtml)) {
2330 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
1362 + // Check for and include Word content
1363 + $word_url = get_transient('mxchat_word_url_' . $session_id);
1364 + $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
1365 + $word_filename = get_transient('mxchat_word_filename_' . $session_id);
1366 + if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
1367 + $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
1368 + if (!empty($relevant_word_chunks)) {
1369 + $context_content .= "Relevant content from Word document '{$word_filename}':\n";
1370 + foreach ($relevant_word_chunks as $chunk_data) {
1371 + $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
1372 + }
1373 + $context_content .= "\n";
2331 1374 }
1375 + }
1376 +
1377 + $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2332 1378
2333 - if (!empty($this->fallbackResponse['html'])) {
2334 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1379 + // Generate response
1380 + $response = $this->mxchat_generate_response(
1381 + $context_content,
1382 + $this->options['api_key'],
1383 + $this->options['xai_api_key'],
1384 + $this->options['claude_api_key'],
1385 + $this->options['deepseek_api_key'],
1386 + $this->options['gemini_api_key'],
1387 + $conversation_history,
1388 + $is_streaming,
1389 + $session_id,
1390 + $testing_data
1391 + );
1392 +
1393 + // Handle streaming vs non-streaming responses
1394 + if ($is_streaming) {
1395 + // Check if streaming actually happened or if it fell back to regular response
1396 + if ($response === true) {
1397 + wp_die();
2335 1398 }
2336 -
2337 - // Step 6: Return the response
2338 - // DEBUG: Check if newlines exist in the response
2339 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2340 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2341 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2342 -
2343 - $response_data = [
2344 - 'text' => $response,
2345 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2346 - 'session_id' => $session_id
2347 - ];
2348 -
2349 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2350 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2351 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2352 - }
2353 -
2354 - // Also pass it as a top-level field so JS can show a better error message to admins
2355 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2356 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2357 - }
2358 -
2359 - // Always add testing data for admins (no toggle needed)
2360 - if ($testing_data !== null) {
2361 - $response_data['testing_data'] = $testing_data;
2362 - }
2363 -
2364 - wp_send_json($response_data);
1399 + // If we get here, streaming fell back to regular response, continue
1400 + }
1401 +
1402 + // Check if the response is an error array
1403 + if (is_array($response) && isset($response['error'])) {
1404 + wp_send_json_error([
1405 + 'error_message' => $response['error'],
1406 + 'error_code' => $response['error_code'] ?? 'api_error'
1407 + ]);
2365 1408 wp_die();
2366 -}
2367 -
2368 -/**
2369 - * Get bot-specific options for multi-bot functionality
2370 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2371 - */
2372 -// Also debug the bot options retrieval
2373 -private function get_bot_options($bot_id = 'default') {
2374 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2375 -
2376 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2377 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2378 - return array();
2379 1409 }
2380 1410
2381 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2382 -
2383 - if (!empty($bot_options)) {
2384 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2385 - if (isset($bot_options['similarity_threshold'])) {
2386 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2387 - }
1411 + // If we get here, the response is valid text
1412 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1413 +
1414 + // Step 5: Save additional content if available
1415 + if (!empty($this->productCardHtml)) {
1416 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2388 1417 }
2389 -
2390 - return is_array($bot_options) ? $bot_options : array();
2391 -}
2392 1418
2393 -/**
2394 - * Get bot-specific Pinecone configuration
2395 - * Used in the knowledge retrieval functions
2396 - */
2397 -// Also add debugging to your get_bot_pinecone_config function
2398 -private function get_bot_pinecone_config($bot_id = 'default') {
2399 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2400 -
2401 - // If default bot or multi-bot add-on not active, use default Pinecone config
2402 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2403 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2404 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2405 - $config = array(
2406 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2407 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2408 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2409 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2410 - );
2411 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2412 - return $config;
1419 + if (!empty($this->fallbackResponse['html'])) {
1420 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2413 1421 }
2414 -
2415 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2416 -
2417 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2418 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2419 -
2420 - if (!empty($bot_pinecone_config)) {
2421 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2422 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2423 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2424 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2425 - } else {
2426 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
1422 +
1423 + // Step 6: Return the response
1424 + $response_data = [
1425 + 'text' => $response,
1426 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1427 + 'session_id' => $session_id
1428 + ];
1429 +
1430 + // Always add testing data for admins (no toggle needed)
1431 + if ($testing_data !== null) {
1432 + $response_data['testing_data'] = $testing_data;
2427 1433 }
2428 -
2429 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
1434 +
1435 + wp_send_json($response_data);
1436 + wp_die();
2430 1437 }
2431 1438
2432 -
2433 1439 // Updated function to check intents and invoke the callback function
2434 1440 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2435 1441 global $wpdb;
2436 1442 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2437 1443
2438 - // Get the current bot_id
2439 - $current_bot_id = $this->get_current_bot_id($session_id);
2440 -
2441 1444 // Generate the user embedding
2442 1445 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2443 -
1446 +
2444 1447 // Check if embedding generation returned an error
2445 1448 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2446 1449 $error_message = $user_embedding['error'];
2447 1450 $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2448 -
2449 - // FIXED: Send error in appropriate format based on streaming mode
2450 - if ($this->is_streaming) {
2451 - echo "data: " . json_encode([
2452 - 'error' => true,
2453 - 'error_message' => $error_message,
2454 - 'error_code' => $error_code,
2455 - 'text' => $error_message,
2456 - 'message' => $error_message
2457 - ]) . "\n\n";
2458 - echo "data: [DONE]\n\n";
2459 - flush();
2460 - } else {
2461 - wp_send_json_error([
2462 - 'error_message' => $error_message,
2463 - 'error_code' => $error_code
2464 - ]);
2465 - }
1451 +
1452 + wp_send_json_error([
1453 + 'error_message' => $error_message,
1454 + 'error_code' => $error_code
1455 + ]);
2466 1456 wp_die();
2467 1457 }
2468 -
1458 +
2469 1459 // Check if embedding is valid
2470 1460 if (!is_array($user_embedding) || empty($user_embedding)) {
2471 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2472 -
2473 - // FIXED: Send error in appropriate format based on streaming mode
2474 - if ($this->is_streaming) {
2475 - echo "data: " . json_encode([
2476 - 'error' => true,
2477 - 'error_message' => $error_message,
2478 - 'error_code' => 'invalid_embedding',
2479 - 'text' => $error_message,
2480 - 'message' => $error_message
2481 - ]) . "\n\n";
2482 - echo "data: [DONE]\n\n";
2483 - flush();
2484 - } else {
2485 - wp_send_json_error([
2486 - 'error_message' => $error_message,
2487 - 'error_code' => 'invalid_embedding'
2488 - ]);
2489 - }
1461 + wp_send_json_error([
1462 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1463 + 'error_code' => 'invalid_embedding'
1464 + ]);
2490 1465 wp_die();
2491 1466 }
2492 -
1467 +
2493 1468 // Fetch intents from the database
2494 1469 $table_name = $wpdb->prefix . 'mxchat_intents';
2495 1470 if ($chat_mode === 'agent') {
2496 1471 $query = $wpdb->prepare(
@@ -2500,29 +1475,19 @@
2500 1475 $intents = $wpdb->get_results($query);
2501 1476 } else {
2502 1477 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2503 1478 }
2504 -
1479 +
2505 1480 if (empty($intents)) {
2506 1481 return false;
2507 1482 }
2508 -
2509 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2510 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2511 - $phrases_by_intent = [];
2512 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2513 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2514 - foreach ($all_phrases as $p) {
2515 - $phrases_by_intent[$p->intent_id][] = $p;
2516 - }
2517 - }
2518 -
1483 +
2519 1484 $highest_similarity = -INF;
2520 1485 $matched_intent = null;
2521 -
2522 - // Array to store action analysis for testing panel
1486 +
1487 + // NEW: Array to store action analysis for testing panel
2523 1488 $action_analysis = [];
2524 -
1489 +
2525 1490 foreach ($intents as $intent) {
2526 1491 // Additional check for enabled state
2527 1492 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2528 1493 if (!$is_enabled) {
@@ -2527,57 +1492,22 @@
2527 1492 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2528 1493 if (!$is_enabled) {
2529 1494 continue;
2530 1495 }
2531 -
2532 - // Check if this action is enabled for the current bot
2533 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2534 - continue;
2535 - }
2536 -
2537 - $best_similarity = -INF;
2538 - $matched_phrase_text = '';
2539 -
2540 - // Check legacy embedding vector (existing behavior)
1496 +
2541 1497 $intent_embedding_serialized = $intent->embedding_vector;
2542 1498 $intent_embedding = $intent_embedding_serialized
2543 1499 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2544 1500 : null;
2545 -
2546 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2547 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2548 - if ($legacy_similarity > $best_similarity) {
2549 - $best_similarity = $legacy_similarity;
2550 - $matched_phrase_text = 'legacy';
2551 - }
2552 - }
2553 -
2554 - // Check individual phrase vectors
2555 - if (isset($phrases_by_intent[$intent->id])) {
2556 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2557 - $phrase_embedding = $phrase_row->embedding_vector
2558 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2559 - : null;
2560 - if (!is_array($phrase_embedding)) {
2561 - continue;
2562 - }
2563 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2564 - if ($phrase_similarity > $best_similarity) {
2565 - $best_similarity = $phrase_similarity;
2566 - $matched_phrase_text = $phrase_row->phrase;
2567 - }
2568 - }
2569 - }
2570 -
2571 - // Skip if no valid embedding was found at all
2572 - if ($best_similarity === -INF) {
1501 +
1502 + if (!is_array($intent_embedding)) {
2573 1503 continue;
2574 1504 }
2575 -
2576 - $similarity = $best_similarity;
1505 +
1506 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2577 1507 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2578 -
2579 - // Store action analysis data for testing panel
1508 +
1509 + // NEW: Store action analysis data for testing panel
2580 1510 $action_analysis[] = [
2581 1511 'intent_label' => $intent->intent_label,
2582 1512 'callback_function' => $intent->callback_function,
2583 1513 'similarity' => round($similarity, 4),
@@ -2584,12 +1514,11 @@
2584 1514 'similarity_percentage' => round($similarity * 100, 2),
2585 1515 'threshold' => $intent_threshold,
2586 1516 'threshold_percentage' => round($intent_threshold * 100, 2),
2587 1517 'above_threshold' => $similarity >= $intent_threshold,
2588 - 'matched_phrase' => $matched_phrase_text,
2589 1518 'triggered' => false // Will be updated below if this intent is triggered
2590 1519 ];
2591 -
1520 +
2592 1521 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2593 1522 $highest_similarity = $similarity;
2594 1523 $matched_intent = $intent;
2595 1524 }
@@ -2594,9 +1523,9 @@
2594 1523 $matched_intent = $intent;
2595 1524 }
2596 1525 }
2597 1526
2598 - // Mark the triggered action if any
1527 + // NEW: Mark the triggered action if any
2599 1528 if ($matched_intent) {
2600 1529 foreach ($action_analysis as &$action) {
2601 1530 if ($action['intent_label'] === $matched_intent->intent_label) {
2602 1531 $action['triggered'] = true;
@@ -2604,9 +1533,9 @@
2604 1533 }
2605 1534 }
2606 1535 }
2607 1536
2608 - // Sort actions by similarity (highest first) and store for testing panel
1537 + // NEW: Sort actions by similarity (highest first) and store for testing panel
2609 1538 usort($action_analysis, function($a, $b) {
2610 1539 return $b['similarity'] <=> $a['similarity'];
2611 1540 });
2612 1541
@@ -2613,73 +1542,47 @@
2613 1542 // Store action analysis for testing panel capture
2614 1543 $this->last_action_analysis = $action_analysis;
2615 1544
2616 1545 // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2617 - if ($matched_intent) {
2618 - // If the callback is a method on this instance (core callback), call it directly
2619 - if (method_exists($this, $matched_intent->callback_function)) {
2620 - $callback_result = call_user_func(
2621 - [$this, $matched_intent->callback_function],
2622 - $message,
2623 - $user_id,
2624 - $session_id,
2625 - $matched_intent,
2626 - $user_context ?? null
2627 - );
1546 +if ($matched_intent) {
1547 + // If the callback is a method on this instance (core callback), call it directly
1548 + if (method_exists($this, $matched_intent->callback_function)) {
1549 + $callback_result = call_user_func(
1550 + [$this, $matched_intent->callback_function],
1551 + $message,
1552 + $user_id,
1553 + $session_id,
1554 + $matched_intent,
1555 + $user_context ?? null
1556 + );
1557 + } else {
1558 + // Otherwise, use apply_filters for add-on callbacks
1559 + $callback_result = apply_filters(
1560 + $matched_intent->callback_function,
1561 + false,
1562 + $message,
1563 + $user_id,
1564 + $session_id,
1565 + $matched_intent
1566 + );
1567 + }
1568 +
1569 + // Handle the callback result properly
1570 + if ($callback_result !== false) {
1571 + // If callback returned an array with chat_mode, use it directly
1572 + if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
1573 + $this->fallbackResponse = $callback_result;
1574 + return $callback_result; // Return the full array
2628 1575 } else {
2629 - // Otherwise, use apply_filters for add-on callbacks
2630 - $callback_result = apply_filters(
2631 - $matched_intent->callback_function,
2632 - false,
2633 - $message,
2634 - $user_id,
2635 - $session_id,
2636 - $matched_intent
2637 - );
1576 + $this->fallbackResponse = $callback_result;
1577 + return true;
2638 1578 }
2639 -
2640 - // Handle the callback result properly
2641 - if ($callback_result !== false) {
2642 - // If callback returned an array with chat_mode, use it directly
2643 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2644 - $this->fallbackResponse = $callback_result;
2645 - return $callback_result; // Return the full array
2646 - } else {
2647 - $this->fallbackResponse = $callback_result;
2648 - return true;
2649 - }
2650 - }
2651 1579 }
1580 +}
2652 1581
2653 1582 return false;
2654 1583 }
2655 1584
2656 -/**
2657 - * Check if an action is enabled for a specific bot
2658 - */
2659 -private function is_action_enabled_for_bot($intent, $bot_id) {
2660 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2661 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2662 - return true;
2663 - }
2664 -
2665 - $enabled_bots = json_decode($intent->enabled_bots, true);
2666 -
2667 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2668 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2669 - return true;
2670 - }
2671 -
2672 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2673 - // default-bot actions are testable from the admin panel
2674 - if ($bot_id === 'testing') {
2675 - $bot_id = 'default';
2676 - }
2677 -
2678 - // Check if the current bot is in the enabled bots list
2679 - return in_array($bot_id, $enabled_bots);
2680 -}
2681 -
2682 1585 // Helper function to clear PDF and Word document related transients
2683 1586 private function clear_pdf_transients($session_id) {
2684 1587 // PDF transients
2685 1588 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2713,23 +1616,18 @@
2713 1616 }
2714 1617
2715 1618 public function mxchat_generate_image($message, $user_id, $session_id) {
2716 1619 //error_log("Starting image generation for message: " . $message);
2717 -
2718 - // Prepare a prompt for OpenAI image generation
1620 +
1621 + // Prepare a prompt for DALL-E
2719 1622 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2720 -
2721 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2722 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2723 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2724 - $image_response = $this->mxchat_generate_custom_image($prompt);
2725 - } else {
2726 - // Use the existing OpenAI API key
2727 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2728 - // Call OpenAI GPT Image to generate an image
2729 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2730 - }
2731 1623
1624 + // Use the existing OpenAI API key
1625 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1626 +
1627 + // Call DALL-E to generate an image
1628 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1629 +
2732 1630 // Check if the response contains an image URL
2733 1631 if (isset($image_response['imageUrl'])) {
2734 1632 $image_url = esc_url_raw($image_response['imageUrl']);
2735 1633
@@ -2772,125 +1670,24 @@
2772 1670 // Return the response directly instead of relying on the property
2773 1671 return $this->fallbackResponse;
2774 1672 }
2775 1673 }
2776 -
2777 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2778 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2779 -
2780 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2781 - if (empty($gemini_api_key)) {
2782 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2783 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2784 - return ['text' => $response_text, 'html' => '', 'images' => []];
2785 - }
2786 -
2787 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2788 -
2789 - if (isset($image_response['imageUrl'])) {
2790 - $image_url = esc_url_raw($image_response['imageUrl']);
2791 -
2792 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2793 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2794 -
2795 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2796 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2797 -
2798 - $this->fallbackResponse = [
2799 - 'text' => $response_text,
2800 - 'html' => $response_html,
2801 - 'images' => [$image_url]
2802 - ];
2803 -
2804 - return $this->fallbackResponse;
2805 - } else {
2806 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2807 -
2808 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2809 -
2810 - $this->fallbackResponse = [
2811 - 'text' => $response_text,
2812 - 'html' => '',
2813 - 'images' => []
2814 - ];
2815 -
2816 - return $this->fallbackResponse;
2817 - }
2818 -}
2819 -
2820 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2821 - // Map the real mime type to a matching file extension so the saved file's
2822 - // extension always agrees with its bytes. A mismatch (e.g. Imagen returning
2823 - // webp bytes that were written into a ".png" file) makes the browser refuse
2824 - // to render the image even though the file saved successfully and the bot
2825 - // reported success — that was the Gemini/Imagen "image never renders" bug.
2826 - // OpenAI + custom-provider paths pass 'image/png' explicitly, so they are
2827 - // unaffected; this only matters for providers that return another type.
2828 - $mime_to_ext = [
2829 - 'image/jpeg' => 'jpg',
2830 - 'image/jpg' => 'jpg',
2831 - 'image/png' => 'png',
2832 - 'image/webp' => 'webp',
2833 - 'image/gif' => 'gif',
2834 - ];
2835 - $mime_type = strtolower(trim((string) $mime_type));
2836 - if (isset($mime_to_ext[$mime_type])) {
2837 - $extension = $mime_to_ext[$mime_type];
2838 - } else {
2839 - // Unknown/unsupported type: fall back to png and normalize the stored
2840 - // mime so the attachment record and the file extension stay consistent.
2841 - $extension = 'png';
2842 - $mime_type = 'image/png';
2843 - }
2844 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2845 - $decoded = base64_decode($base64_data);
2846 -
2847 - if ($decoded === false) {
2848 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2849 - }
2850 -
2851 - $upload = wp_upload_bits($filename, null, $decoded);
2852 -
2853 - if (!empty($upload['error'])) {
2854 - return new \WP_Error('upload_failed', $upload['error']);
2855 - }
2856 -
2857 - $attach_id = wp_insert_attachment([
2858 - 'post_mime_type' => $mime_type,
2859 - 'post_title' => $prefix,
2860 - 'post_content' => '',
2861 - 'post_status' => 'inherit',
2862 - ], $upload['file']);
2863 -
2864 - if (is_wp_error($attach_id)) {
2865 - return $attach_id;
2866 - }
2867 -
2868 - require_once ABSPATH . 'wp-admin/includes/image.php';
2869 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2870 - wp_update_attachment_metadata($attach_id, $metadata);
2871 -
2872 - return esc_url_raw(wp_get_attachment_url($attach_id));
2873 -}
2874 -
2875 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1674 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2876 1675 $api_url = 'https://api.openai.com/v1/images/generations';
2877 1676 $body = json_encode([
2878 - 'prompt' => sanitize_text_field($prompt),
2879 - 'n' => 1,
2880 - 'size' => '1024x1024',
2881 - 'quality' => 'medium',
2882 - 'output_format' => 'png',
2883 - 'model' => sanitize_text_field($model),
1677 + 'prompt' => sanitize_text_field($prompt),
1678 + 'n' => 1,
1679 + 'size' => '1024x1024',
1680 + 'model' => sanitize_text_field($model),
2884 1681 ]);
2885 1682
2886 1683 $args = [
2887 - 'body' => $body,
1684 + 'body' => $body,
2888 1685 'headers' => [
2889 - 'Content-Type' => 'application/json',
1686 + 'Content-Type' => 'application/json',
2890 1687 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2891 1688 ],
2892 - 'method' => 'POST',
1689 + 'method' => 'POST',
2893 1690 'timeout' => absint($timeout),
2894 1691 ];
2895 1692
2896 1693 $response = wp_remote_post($api_url, $args);
@@ -2895,114 +1692,23 @@
2895 1692
2896 1693 $response = wp_remote_post($api_url, $args);
2897 1694
2898 1695 if (is_wp_error($response)) {
1696 + //error_log("DALL-E request failed: " . $response->get_error_message());
2899 1697 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2900 1698 }
2901 1699
2902 1700 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2903 1701
2904 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2905 - if ($b64) {
2906 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2907 - if (is_wp_error($saved_url)) {
2908 - return ['error' => $saved_url->get_error_message()];
2909 - }
2910 - return ['imageUrl' => $saved_url];
1702 + if (isset($response_body['data'][0]['url'])) {
1703 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2911 1704 } else {
1705 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2912 1706 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2913 1707 }
2914 1708 }
2915 1709
2916 1710 /**
2917 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2918 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2919 - */
2920 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2921 - $cfg = $this->mxchat_resolve_custom_provider();
2922 - if (empty($cfg['base_url'])) {
2923 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2924 - }
2925 - $url = $cfg['base_url'] . '/images/generations';
2926 - if (!empty($cfg['api_version'])) {
2927 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2928 - }
2929 - $body = wp_json_encode([
2930 - 'prompt' => sanitize_text_field($prompt),
2931 - 'n' => 1,
2932 - 'size' => '1024x1024',
2933 - 'model' => $cfg['model'],
2934 - ]);
2935 - $response = wp_remote_post($url, [
2936 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2937 - 'body' => $body,
2938 - 'method' => 'POST',
2939 - 'timeout' => absint($timeout),
2940 - ]);
2941 - if (is_wp_error($response)) {
2942 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2943 - }
2944 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2945 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2946 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2947 - if ($b64) {
2948 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2949 - if (is_wp_error($saved)) {
2950 - return ['error' => $saved->get_error_message()];
2951 - }
2952 - return ['imageUrl' => $saved];
2953 - }
2954 - $remote_url = $resp['data'][0]['url'] ?? null;
2955 - if ($remote_url) {
2956 - return ['imageUrl' => esc_url_raw($remote_url)];
2957 - }
2958 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2959 - return ['error' => esc_html($err_msg)];
2960 -}
2961 -
2962 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2963 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2964 -
2965 - $body = json_encode([
2966 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2967 - 'parameters' => [
2968 - 'sampleCount' => 1,
2969 - 'aspectRatio' => '1:1',
2970 - ],
2971 - ]);
2972 -
2973 - $args = [
2974 - 'body' => $body,
2975 - 'headers' => [
2976 - 'Content-Type' => 'application/json',
2977 - 'x-goog-api-key' => sanitize_text_field($api_key),
2978 - ],
2979 - 'method' => 'POST',
2980 - 'timeout' => absint($timeout),
2981 - ];
2982 -
2983 - $response = wp_remote_post($api_url, $args);
2984 -
2985 - if (is_wp_error($response)) {
2986 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2987 - }
2988 -
2989 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2990 -
2991 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2992 - if ($b64) {
2993 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2994 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2995 - if (is_wp_error($saved_url)) {
2996 - return ['error' => $saved_url->get_error_message()];
2997 - }
2998 - return ['imageUrl' => $saved_url];
2999 - } else {
3000 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3001 - }
3002 -}
3003 -
3004 -/**
3005 1711 * Handle web search requests.
3006 1712 *
3007 1713 * Sends the refined search query to the Brave Search API and uses the
3008 1714 * results to generate a conversational response with the AI model.
@@ -3050,10 +1756,10 @@
3050 1756 $transient_key = 'mxchat_search_' . md5($refined_search_query);
3051 1757 $results = get_transient($transient_key);
3052 1758
3053 1759 if (false === $results) {
3054 - // SECURITY FIX: Changed to wp_safe_remote_get
3055 - $response = wp_safe_remote_get(
1760 + // Fetch new results from the Brave Search API
1761 + $response = wp_remote_get(
3056 1762 $api_url,
3057 1763 array(
3058 1764 'headers' => array(
3059 1765 'Accept' => 'application/json',
@@ -3192,10 +1898,9 @@
3192 1898 ],
3193 1899 'timeout' => 10,
3194 1900 ];
3195 1901
3196 - // SECURITY FIX: Changed to wp_safe_remote_get
3197 - $response = wp_safe_remote_get($api_url, $args);
1902 + $response = wp_remote_get($api_url, $args);
3198 1903
3199 1904 if (is_wp_error($response)) {
3200 1905 return array(
3201 1906 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
@@ -3265,22 +1970,17 @@
3265 1970 * @return string The refined search query
3266 1971 */
3267 1972 public function mxchat_interpret_search_query($user_query) {
3268 1973 $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');
3269 -
1974 +
3270 1975 // Get options and determine the selected model
3271 1976 $options = $this->options ?? get_option('mxchat_options');
3272 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
3273 -
3274 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3275 - if ($selected_model === 'custom-provider') {
3276 - return $this->interpret_query_with_custom($user_query, $system_prompt);
3277 - }
3278 -
1977 + $selected_model = isset($options['model']) ? $options['model'] : 'gpt-4o';
1978 +
3279 1979 // Extract model prefix to determine the provider
3280 1980 $model_parts = explode('-', $selected_model);
3281 1981 $provider = strtolower($model_parts[0]);
3282 -
1982 +
3283 1983 // Determine which API key to use based on the provider
3284 1984 switch ($provider) {
3285 1985 case 'gemini':
3286 1986 $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
@@ -3321,60 +2021,11 @@
3321 2021 }
3322 2022 }
3323 2023
3324 2024 /**
3325 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3326 - * Uses the same base URL + auth scheme as the chat dispatcher.
3327 - */
3328 -private function interpret_query_with_custom($user_query, $system_prompt) {
3329 - $cfg = $this->mxchat_resolve_custom_provider();
3330 - if (empty($cfg['base_url'])) {
3331 - return sanitize_text_field($user_query);
3332 - }
3333 - $args = [
3334 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3335 - 'body' => wp_json_encode([
3336 - 'model' => $cfg['model'],
3337 - 'messages' => [
3338 - ['role' => 'system', 'content' => $system_prompt],
3339 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3340 - ],
3341 - 'temperature' => 0.2,
3342 - 'max_tokens' => 20,
3343 - ]),
3344 - 'method' => 'POST',
3345 - 'timeout' => 15,
3346 - ];
3347 - $response = wp_remote_post($cfg['chat_url'], $args);
3348 - if (is_wp_error($response)) {
3349 - return sanitize_text_field($user_query);
3350 - }
3351 - $body = json_decode(wp_remote_retrieve_body($response), true);
3352 - return isset($body['choices'][0]['message']['content'])
3353 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3354 - : sanitize_text_field($user_query);
3355 -}
3356 -
3357 -/**
3358 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3359 - * into the assoc-array form wp_remote_post expects.
3360 - */
3361 -private function mxchat_custom_provider_assoc_headers($cfg) {
3362 - $headers = ['Content-Type' => 'application/json'];
3363 - if (!empty($cfg['api_key'])) {
3364 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3365 - $headers['api-key'] = $cfg['api_key'];
3366 - } else {
3367 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3368 - }
3369 - }
3370 - return $headers;
3371 -}
3372 -
3373 -/**
3374 2025 * Interpret query using OpenAI models
3375 2026 */
3376 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
2027 +private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-4o') {
3377 2028 $url = 'https://api.openai.com/v1/chat/completions';
3378 2029 $args = [
3379 2030 'headers' => [
3380 2031 'Authorization' => 'Bearer ' . $api_key,
@@ -3404,40 +2055,13 @@
3404 2055 : sanitize_text_field($user_query);
3405 2056 }
3406 2057
3407 2058 /**
3408 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3409 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3410 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3411 - * stripping. We never send a `thinking` param either, which is required for
3412 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3413 - */
3414 -private function mxchat_claude_omits_temperature($model) {
3415 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3416 - return in_array($model, $no_temp, true);
3417 -}
3418 -
3419 -/**
3420 2059 * Interpret query using Claude models
3421 2060 */
3422 2061 private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3423 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3424 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3425 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3426 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3427 2062 $url = 'https://api.anthropic.com/v1/messages';
3428 -
3429 - $payload = [
3430 - 'model' => $model,
3431 - 'system' => $system_prompt,
3432 - 'messages' => [
3433 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3434 - ],
3435 - 'max_tokens' => 20,
3436 - 'temperature' => 0.2,
3437 - ];
3438 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3439 -
2063 +
3440 2064 $args = [
3441 2065 'headers' => [
3442 2066 'Content-Type' => 'application/json',
3443 2067 'x-api-key' => $api_key,
@@ -3442,9 +2066,17 @@
3442 2066 'Content-Type' => 'application/json',
3443 2067 'x-api-key' => $api_key,
3444 2068 'anthropic-version' => '2023-06-01',
3445 2069 ],
3446 - 'body' => wp_json_encode($payload),
2070 + 'body' => wp_json_encode([
2071 + 'model' => $model,
2072 + 'system' => $system_prompt,
2073 + 'messages' => [
2074 + ['role' => 'user', 'content' => sanitize_text_field($user_query)]
2075 + ],
2076 + 'max_tokens' => 20,
2077 + 'temperature' => 0.2,
2078 + ]),
3447 2079 'method' => 'POST',
3448 2080 'timeout' => 15,
3449 2081 ];
3450 2082
@@ -3453,16 +2085,12 @@
3453 2085 return sanitize_text_field($user_query);
3454 2086 }
3455 2087
3456 2088 $body = json_decode(wp_remote_retrieve_body($response), true);
3457 - // claude-fable-5 prepends a thinking block to content — take the first
3458 - // TEXT block, not content[0].
3459 - foreach ((array) ($body['content'] ?? array()) as $block) {
3460 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3461 - return sanitize_text_field(trim($block['text']));
3462 - }
2089 + if (!empty($body['content'][0]['text'])) {
2090 + return sanitize_text_field(trim($body['content'][0]['text']));
3463 2091 }
3464 -
2092 +
3465 2093 return sanitize_text_field($user_query);
3466 2094 }
3467 2095
3468 2096 /**
@@ -3468,16 +2096,13 @@
3468 2096 /**
3469 2097 * Interpret query using Gemini models
3470 2098 */
3471 2099 private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3472 - if ($model === 'gemini-3-pro-preview') {
3473 - $model = 'gemini-3.1-pro-preview';
3474 - }
3475 - // Use v1beta for preview models, v1 for stable models
3476 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3477 -
3478 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
2100 + // Strip "gemini-" prefix for the API
2101 + $model_version = str_replace('gemini-', '', $model);
3479 2102
2103 + $url = "https://generativelanguage.googleapis.com/v1/models/$model_version:generateContent?key=" . urlencode($api_key);
2104 +
3480 2105 $args = [
3481 2106 'headers' => [
3482 2107 'Content-Type' => 'application/json',
3483 2108 ],
@@ -3673,9 +2298,9 @@
3673 2298 }
3674 2299
3675 2300
3676 2301 /**
3677 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
2302 + * Enhanced fetch_and_split_pdf_pages with detailed debugging
3678 2303 */
3679 2304 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3680 2305 // CLEAR DEBUG LOGGING
3681 2306 //error_log("=== MXCHAT PDF PROCESSING START ===");
@@ -3730,19 +2355,10 @@
3730 2355 // (I'll include the key parts with debug logging)
3731 2356
3732 2357 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3733 2358 //error_log("Downloading PDF from URL...");
3734 -
3735 - // SECURITY FIX: Validate URL before processing
3736 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3737 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3738 - return false;
3739 - }
3740 -
3741 2359 $temp_file = wp_tempnam($pdf_source);
3742 -
3743 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3744 - $response = wp_safe_remote_get($pdf_source, [
2360 + $response = wp_remote_get($pdf_source, [
3745 2361 'timeout' => 60,
3746 2362 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3747 2363 ]);
3748 2364
@@ -3751,14 +2367,9 @@
3751 2367 //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3752 2368 return false;
3753 2369 }
3754 2370
3755 - global $wp_filesystem;
3756 - if (empty($wp_filesystem)) {
3757 - require_once ABSPATH . 'wp-admin/includes/file.php';
3758 - WP_Filesystem();
3759 - }
3760 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
2371 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
3761 2372 //error_log("✅ PDF downloaded successfully");
3762 2373 } else {
3763 2374 $temp_file = $pdf_source;
3764 2375 //error_log("Using local PDF file: " . $temp_file);
@@ -3765,9 +2376,8 @@
3765 2376 }
3766 2377
3767 2378 // Parse PDF
3768 2379 //error_log("Parsing PDF with basic parser...");
3769 - mxchat_load_pdf_parser();
3770 2380 $parser = new \Smalot\PdfParser\Parser();
3771 2381 $pdf = $parser->parseFile($temp_file);
3772 2382 $pages = $pdf->getPages();
3773 2383
@@ -3830,33 +2440,8 @@
3830 2440 return false;
3831 2441 }
3832 2442 }
3833 2443
3834 -
3835 -/**
3836 - * Validate PDF URL for security
3837 - * Prevents SSRF attacks by blocking dangerous URLs
3838 - */
3839 -
3840 -private function mxchat_is_safe_pdf_url($url) {
3841 - // Use WordPress core function for comprehensive validation
3842 - // This blocks localhost, private IPs, and reserved IP ranges
3843 - $validated_url = wp_http_validate_url($url);
3844 -
3845 - if ($validated_url === false) {
3846 - return false;
3847 - }
3848 -
3849 - // Additional check: only allow HTTP/HTTPS schemes
3850 - $parsed = parse_url($url);
3851 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3852 - return false;
3853 - }
3854 -
3855 - return true;
3856 -}
3857 -
3858 -
3859 2444 private function mxchat_clean_text($text) {
3860 2445 // Remove excessive whitespace
3861 2446 $text = preg_replace('/\s+/', ' ', $text);
3862 2447
@@ -3895,14 +2480,11 @@
3895 2480 }
3896 2481
3897 2482 return [];
3898 2483 }
3899 -
3900 -
2484 +// Add this to your class
3901 2485 public function handle_pdf_upload() {
3902 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3903 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3904 - }
2486 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3905 2487
3906 2488 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3907 2489 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3908 2490 return;
@@ -3907,29 +2489,12 @@
3907 2489 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3908 2490 return;
3909 2491 }
3910 2492
3911 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3912 - $options = get_option('mxchat_options', array());
3913 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3914 -
3915 - if ($show_pdf_button !== 'on') {
3916 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3917 - return;
3918 - }
3919 -
3920 2493 $file = $_FILES['pdf_file'];
3921 2494 $session_id = sanitize_text_field($_POST['session_id']);
3922 2495 $original_filename = sanitize_text_field($file['name']);
3923 2496
3924 - // Update session owner if it changed (e.g. IP changed due to network switch)
3925 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3926 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3927 -
3928 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3929 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3930 - }
3931 -
3932 2497 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3933 2498 if ($file_type['type'] !== 'application/pdf') {
3934 2499 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3935 2500 return;
@@ -3935,12 +2500,9 @@
3935 2500 return;
3936 2501 }
3937 2502
3938 2503 $upload_dir = wp_upload_dir();
3939 -
3940 - // SECURITY FIX: Generate random filename without exposing session_id
3941 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3942 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2504 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3943 2505 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3944 2506
3945 2507 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3946 2508 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3971,9 +2533,8 @@
3971 2533 return;
3972 2534 }
3973 2535
3974 2536 if (!empty($embeddings)) {
3975 - // Store the mapping between session and the random filename
3976 2537 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3977 2538 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3978 2539 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3979 2540 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3994,11 +2555,9 @@
3994 2555 wp_send_json_error($error_message);
3995 2556 return;
3996 2557 }
3997 2558 public function handle_pdf_remove() {
3998 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3999 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4000 - }
2559 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
4001 2560
4002 2561 if (empty($_POST['session_id'])) {
4003 2562 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
4004 2563 wp_die();
@@ -4019,8 +2578,10 @@
4019 2578 wp_die();
4020 2579 }
4021 2580
4022 2581
2582 +
2583 +
4023 2584 function mxchat_fetch_new_messages() {
4024 2585 $session_id = sanitize_text_field($_POST['session_id']);
4025 2586 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
4026 2587 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -4033,31 +2594,14 @@
4033 2594 }
4034 2595
4035 2596 $history = get_option("mxchat_history_{$session_id}", []);
4036 2597
4037 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
4038 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
4039 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
4040 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
4041 -
4042 2598 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
4043 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
4044 -
4045 2599 // If persistence is enabled, show all new messages
4046 2600 if ($persistence_enabled) {
4047 - $has_id = !empty($message['id']);
4048 - $is_agent = $message['role'] === 'agent';
4049 -
4050 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
4051 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
4052 - $is_newer = true;
4053 - } else {
4054 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
4055 - }
4056 -
4057 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
4058 -
4059 - return $has_id && $is_newer && $is_agent;
2601 + return !empty($message['id']) &&
2602 + strcmp($message['id'], $last_seen_id) > 0 &&
2603 + $message['role'] === 'agent';
4060 2604 }
4061 2605
4062 2606 // If persistence is disabled, only show messages after initial timestamp
4063 2607 return !empty($message['id']) &&
@@ -4064,16 +2608,12 @@
4064 2608 $message['role'] === 'agent' &&
4065 2609 $message['timestamp'] > $initial_timestamp;
4066 2610 });
4067 2611
4068 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2612 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
4069 2613
4070 - // Include current chat mode so frontend can detect agent→AI transitions
4071 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
4072 -
4073 2614 wp_send_json_success([
4074 - 'new_messages' => array_values($new_messages),
4075 - 'chat_mode' => $chat_mode
2615 + 'new_messages' => array_values($new_messages)
4076 2616 ]);
4077 2617 wp_die();
4078 2618 }
4079 2619 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
@@ -4358,393 +2898,9 @@
4358 2898
4359 2899 //error_log("[DEBUG] Generated channel name: {$channel_name}");
4360 2900 return $channel_name;
4361 2901 }
4362 -
4363 -/**
4364 - * Telegram Live Agent Handover
4365 - * Creates a forum topic in the Telegram group and notifies agents
4366 - */
4367 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4368 - // Check if Telegram agents are available
4369 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4370 - if ($telegram_available !== 'on') {
4371 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4372 - $this->fallbackResponse = [
4373 - 'text' => $away_message,
4374 - 'html' => '',
4375 - 'images' => [],
4376 - 'chat_mode' => 'ai'
4377 - ];
4378 - wp_send_json([
4379 - 'text' => $away_message,
4380 - 'html' => '',
4381 - 'chat_mode' => 'ai',
4382 - 'session_id' => $session_id
4383 - ]);
4384 - wp_die();
4385 - }
4386 -
4387 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4388 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4389 -
4390 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
4391 - return false;
4392 - }
4393 -
4394 - // Check if topic already exists for this session
4395 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4396 -
4397 - if (empty($topic_id)) {
4398 - // Generate topic name
4399 - $topic_name = $this->generate_telegram_topic_name($session_id);
4400 -
4401 - // Random icon color (Telegram forum topic colors)
4402 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4403 - $icon_color = $icon_colors[array_rand($icon_colors)];
4404 -
4405 - // Create forum topic
4406 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4407 - 'headers' => ['Content-Type' => 'application/json'],
4408 - 'body' => json_encode([
4409 - 'chat_id' => $telegram_group_id,
4410 - 'name' => $topic_name,
4411 - 'icon_color' => $icon_color
4412 - ])
4413 - ]);
4414 -
4415 - if (!is_wp_error($response)) {
4416 - $response_body = wp_remote_retrieve_body($response);
4417 - $response_data = json_decode($response_body, true);
4418 -
4419 - if (isset($response_data['ok']) && $response_data['ok']) {
4420 - $topic_id = $response_data['result']['message_thread_id'];
4421 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4422 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4423 - }
4424 - }
4425 -
4426 - if (empty($topic_id)) {
4427 - return false; // Failed to create topic
4428 - }
4429 - }
4430 -
4431 - // Get recent chat history
4432 - $history = get_option("mxchat_history_{$session_id}", []);
4433 - $recent_history = array_slice($history, -5);
4434 -
4435 - // Format conversation context for Telegram (HTML format)
4436 - $conversation_context = "";
4437 - if (!empty($recent_history)) {
4438 - $conversation_context = "<b>Recent Conversation:</b>\n";
4439 - foreach ($recent_history as $hist_message) {
4440 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4441 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4442 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4443 - }
4444 - $conversation_context .= "\n";
4445 - }
4446 -
4447 - // Get user info
4448 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4449 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4450 -
4451 - // Update session mode
4452 - update_option("mxchat_mode_{$session_id}", 'agent');
4453 -
4454 - // Send initial message to topic
4455 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4456 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4457 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4458 - $topic_message .= "<b>User:</b> {$user_name}\n";
4459 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4460 -
4461 - if (!empty($conversation_context)) {
4462 - $topic_message .= $conversation_context;
4463 - }
4464 -
4465 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4466 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4467 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4468 -
4469 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4470 - 'headers' => ['Content-Type' => 'application/json'],
4471 - 'body' => json_encode([
4472 - 'chat_id' => $telegram_group_id,
4473 - 'message_thread_id' => $topic_id,
4474 - 'text' => $topic_message,
4475 - 'parse_mode' => 'HTML'
4476 - ])
4477 - ]);
4478 -
4479 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
4480 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4481 -
4482 - $this->fallbackResponse = [
4483 - 'text' => $success_message,
4484 - 'html' => '',
4485 - 'images' => [],
4486 - 'chat_mode' => 'agent'
4487 - ];
4488 -
4489 - wp_send_json([
4490 - 'success' => true,
4491 - 'text' => $success_message,
4492 - 'html' => '',
4493 - 'chat_mode' => 'agent',
4494 - 'session_id' => $session_id,
4495 - 'fallbackResponse' => $this->fallbackResponse
4496 - ]);
4497 - wp_die();
4498 -}
4499 -
4500 -/**
4501 - * Generate topic name for Telegram forum
4502 - */
4503 -private function generate_telegram_topic_name($session_id) {
4504 - $name = null;
4505 - $email = null;
4506 -
4507 - // Check logged in user
4508 - if (is_user_logged_in()) {
4509 - $current_user = wp_get_current_user();
4510 - if (!empty($current_user->display_name)) {
4511 - $name = $current_user->display_name;
4512 - }
4513 - if (!empty($current_user->user_email)) {
4514 - $email = $current_user->user_email;
4515 - }
4516 - }
4517 -
4518 - // Check session data
4519 - if (empty($name)) {
4520 - $name = get_option("mxchat_name_{$session_id}");
4521 - }
4522 - if (empty($email)) {
4523 - $email = get_option("mxchat_email_{$session_id}");
4524 - }
4525 -
4526 - // Generate topic name
4527 - $session_suffix = substr($session_id, -6);
4528 -
4529 - if (!empty($name)) {
4530 - // Clean name for topic (max 128 chars in Telegram)
4531 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4532 - $clean_name = trim($clean_name);
4533 - if (strlen($clean_name) > 50) {
4534 - $clean_name = substr($clean_name, 0, 50);
4535 - }
4536 - return "Chat - {$clean_name} ({$session_suffix})";
4537 - } elseif (!empty($email)) {
4538 - // Use email prefix
4539 - $email_prefix = explode('@', $email)[0];
4540 - if (strlen($email_prefix) > 30) {
4541 - $email_prefix = substr($email_prefix, 0, 30);
4542 - }
4543 - return "Chat - {$email_prefix} ({$session_suffix})";
4544 - }
4545 -
4546 - return "Chat - {$session_suffix}";
4547 -}
4548 -
4549 -/**
4550 - * Send user message to Telegram agent
4551 - */
4552 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4553 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4554 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4555 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4556 -
4557 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4558 - return false;
4559 - }
4560 -
4561 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4562 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4563 -
4564 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4565 - 'headers' => ['Content-Type' => 'application/json'],
4566 - 'body' => json_encode([
4567 - 'chat_id' => $group_id,
4568 - 'message_thread_id' => $topic_id,
4569 - 'text' => $user_message,
4570 - 'parse_mode' => 'HTML'
4571 - ])
4572 - ]);
4573 -
4574 - return !is_wp_error($response);
4575 -}
4576 -
4577 -/**
4578 - * Handle incoming Telegram webhook
4579 - */
4580 -public function handle_telegram_webhook(WP_REST_Request $request) {
4581 - $body = $request->get_body();
4582 - $data = json_decode($body, true);
4583 -
4584 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4585 -
4586 - // Handle message events from forum topics
4587 - if (isset($data['message'])) {
4588 - $message_data = $data['message'];
4589 -
4590 - // Skip if not from a forum topic
4591 - if (!isset($message_data['message_thread_id'])) {
4592 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4593 - return new WP_REST_Response(['ok' => true]);
4594 - }
4595 -
4596 - // Skip bot messages
4597 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4598 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4599 - return new WP_REST_Response(['ok' => true]);
4600 - }
4601 -
4602 - $chat_id = $message_data['chat']['id'] ?? '';
4603 - $topic_id = $message_data['message_thread_id'];
4604 - $message_text = $message_data['text'] ?? '';
4605 - $message_id = $message_data['message_id'] ?? '';
4606 - $from = $message_data['from'] ?? [];
4607 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4608 - if (empty($agent_name)) {
4609 - $agent_name = $from['username'] ?? 'Agent';
4610 - }
4611 -
4612 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4613 -
4614 - // Skip empty messages
4615 - if (empty($message_text)) {
4616 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4617 - return new WP_REST_Response(['ok' => true]);
4618 - }
4619 -
4620 - // Find session ID by topic ID - cast to string for comparison
4621 - global $wpdb;
4622 - $topic_id_str = strval($topic_id);
4623 - $session_option = $wpdb->get_var(
4624 - $wpdb->prepare(
4625 - "SELECT option_name FROM {$wpdb->options}
4626 - WHERE option_name LIKE %s
4627 - AND option_value = %s",
4628 - 'mxchat_telegram_topic_%',
4629 - $topic_id_str
4630 - )
4631 - );
4632 -
4633 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4634 -
4635 - if ($session_option) {
4636 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4637 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4638 -
4639 - // Verify the group ID matches
4640 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4641 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4642 -
4643 - if (strval($stored_group_id) != strval($chat_id)) {
4644 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4645 - return new WP_REST_Response(['ok' => true]);
4646 - }
4647 -
4648 - // Check for closure commands
4649 - $lower_text = strtolower(trim($message_text));
4650 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4651 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4652 - // End the live agent session
4653 - update_option("mxchat_mode_{$session_id}", 'ai');
4654 -
4655 - // Save disconnect message
4656 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4657 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4658 -
4659 - // Notify in Telegram
4660 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4661 - if (!empty($telegram_bot_token)) {
4662 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4663 - 'headers' => ['Content-Type' => 'application/json'],
4664 - 'body' => json_encode([
4665 - 'chat_id' => $chat_id,
4666 - 'message_thread_id' => $topic_id,
4667 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4668 - 'parse_mode' => 'HTML'
4669 - ])
4670 - ]);
4671 -
4672 - // Optionally close the topic
4673 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4674 - 'headers' => ['Content-Type' => 'application/json'],
4675 - 'body' => json_encode([
4676 - 'chat_id' => $chat_id,
4677 - 'message_thread_id' => $topic_id
4678 - ])
4679 - ]);
4680 - }
4681 -
4682 - return new WP_REST_Response(['ok' => true]);
4683 - }
4684 -
4685 - // Deduplicate messages
4686 - $message_key = md5($session_id . $message_id . $message_text);
4687 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4688 -
4689 - if (in_array($message_key, $processed_messages)) {
4690 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4691 - return new WP_REST_Response(['ok' => true]);
4692 - }
4693 -
4694 - $processed_messages[] = $message_key;
4695 - if (count($processed_messages) > 50) {
4696 - $processed_messages = array_slice($processed_messages, -50);
4697 - }
4698 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4699 -
4700 - // Save the agent message - format with agent name prefix for proper parsing
4701 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4702 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4703 -
4704 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4705 -
4706 - // Verify the message was saved to history
4707 - $history = get_option("mxchat_history_{$session_id}", []);
4708 - $last_message = end($history);
4709 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4710 -
4711 - // Send confirmation back to Telegram
4712 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4713 - if (!empty($telegram_bot_token)) {
4714 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4715 - if (!get_transient($confirm_key)) {
4716 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4717 - 'headers' => ['Content-Type' => 'application/json'],
4718 - 'body' => json_encode([
4719 - 'chat_id' => $chat_id,
4720 - 'message_thread_id' => $topic_id,
4721 - 'text' => "✅ <i>Message sent to user</i>",
4722 - 'parse_mode' => 'HTML',
4723 - 'reply_to_message_id' => $message_id
4724 - ])
4725 - ]);
4726 - set_transient($confirm_key, true, 300);
4727 - }
4728 - }
4729 - } else {
4730 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4731 - }
4732 - } else {
4733 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4734 - }
4735 -
4736 - return new WP_REST_Response(['ok' => true]);
4737 -}
4738 -
4739 2902 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4740 - // Check if this is a Telegram agent session
4741 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4742 - if (!empty($telegram_topic_id)) {
4743 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4744 - }
4745 -
4746 - // Otherwise, try Slack
4747 2903 $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4748 2904 $channel_id = get_option("mxchat_channel_{$session_id}", '');
4749 2905
4750 2906 if (empty($slack_bot_token) || empty($channel_id)) {
@@ -4970,33 +3126,33 @@
4970 3126
4971 3127 $channel_id = $event['channel'];
4972 3128 $message_text = $event['text'] ?? '';
4973 3129 $message_ts = $event['ts'] ?? '';
4974 -
3130 +
4975 3131 // Find session ID by looking for matching channel
4976 3132 global $wpdb;
4977 3133 $session_option = $wpdb->get_var(
4978 3134 $wpdb->prepare(
4979 - "SELECT option_name FROM {$wpdb->options}
4980 - WHERE option_name LIKE 'mxchat_channel_%'
3135 + "SELECT option_name FROM {$wpdb->options}
3136 + WHERE option_name LIKE 'mxchat_channel_%'
4981 3137 AND option_value = %s",
4982 3138 $channel_id
4983 3139 )
4984 3140 );
4985 -
3141 +
4986 3142 if ($session_option) {
4987 3143 $session_id = str_replace('mxchat_channel_', '', $session_option);
4988 -
3144 +
4989 3145 // Create a unique key for this specific message
4990 3146 $message_key = md5($session_id . $message_ts . $message_text);
4991 3147 $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4992 -
3148 +
4993 3149 // Check if we've already processed this exact message
4994 3150 if (in_array($message_key, $processed_messages)) {
4995 3151 //error_log("Duplicate message detected for session $session_id");
4996 3152 return new WP_REST_Response(['ok' => true]);
4997 3153 }
4998 -
3154 +
4999 3155 // Add to processed messages
5000 3156 $processed_messages[] = $message_key;
5001 3157 // Keep only last 50 messages per session
5002 3158 if (count($processed_messages) > 50) {
@@ -5002,46 +3158,14 @@
5002 3158 if (count($processed_messages) > 50) {
5003 3159 $processed_messages = array_slice($processed_messages, -50);
5004 3160 }
5005 3161 set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
5006 -
5007 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5008 -
5009 - // Handle agent ending the chat — transfer back to AI
5010 - // Format: "!endchat" or "!endchat <custom message to user>"
5011 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
5012 - update_option("mxchat_mode_{$session_id}", 'ai');
5013 -
5014 - // Extract custom message after !endchat, or use empty string
5015 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
5016 -
5017 - // Send the agent's custom farewell message if provided
5018 - if (!empty($custom_message)) {
5019 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
5020 - }
5021 -
5022 - // Confirm in Slack channel
5023 - if (!empty($slack_bot_token)) {
5024 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5025 - 'headers' => [
5026 - 'Content-Type' => 'application/json',
5027 - 'Authorization' => 'Bearer ' . $slack_bot_token
5028 - ],
5029 - 'body' => json_encode([
5030 - 'channel' => $channel_id,
5031 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
5032 - 'mrkdwn' => true
5033 - ])
5034 - ]);
5035 - }
5036 -
5037 - return new WP_REST_Response(['ok' => true]);
5038 - }
5039 -
3162 +
5040 3163 // Save the agent message
5041 3164 $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
5042 -
3165 +
5043 3166 // Send confirmation back to Slack (only once)
3167 + $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5044 3168 if (!empty($slack_bot_token)) {
5045 3169 // Use a transient to prevent duplicate confirmations
5046 3170 $confirm_key = 'mxchat_confirm_' . $message_key;
5047 3171 if (!get_transient($confirm_key)) {
@@ -5051,9 +3175,9 @@
5051 3175 'Authorization' => 'Bearer ' . $slack_bot_token
5052 3176 ],
5053 3177 'body' => json_encode([
5054 3178 'channel' => $channel_id,
5055 - 'text' => "✅ _Message sent to user_",
3179 + 'text' => "✅ _Message sent to user_",
5056 3180 'thread_ts' => $event['ts'] // Reply in thread
5057 3181 ])
5058 3182 ]);
5059 3183 // Set transient to prevent duplicate confirmations
@@ -5093,15 +3217,9 @@
5093 3217 try {
5094 3218 // Get options and selected model
5095 3219 $options = get_option('mxchat_options');
5096 3220 $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5097 -
5098 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
5099 - // Off by default so existing sites see byte-identical behavior.
5100 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5101 - return $this->mxchat_generate_embedding_custom($text);
5102 - }
5103 -
3221 +
5104 3222 // Determine endpoint and API key based on model
5105 3223 if (strpos($selected_model, 'voyage') === 0) {
5106 3224 $endpoint = 'https://api.voyageai.com/v1/embeddings';
5107 3225 $api_key = $options['voyage_api_key'] ?? '';
@@ -5297,617 +3415,251 @@
5297 3415 ];
5298 3416 }
5299 3417 }
5300 3418
3419 +private function mxchat_find_relevant_content($user_embedding) {
3420 + //error_log('MXChat Vector Search: Starting content search...');
5301 3421
5302 -/**
5303 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5304 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5305 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5306 - */
5307 -private function mxchat_generate_embedding_custom($text) {
5308 - if (empty($text)) {
5309 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5310 - }
5311 - $cfg = $this->mxchat_resolve_custom_provider();
5312 - if (empty($cfg['base_url'])) {
5313 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5314 - }
3422 + // Retrieve the add-on settings from the database.
3423 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5315 3424
5316 - $options = get_option('mxchat_options');
5317 - $embed_url = $cfg['base_url'] . '/embeddings';
5318 - if (!empty($cfg['api_version'])) {
5319 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5320 - }
5321 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5322 - ? trim((string) $options['custom_provider_embedding_model'])
5323 - : $cfg['model'];
3425 + // Determine whether Pinecone is enabled.
3426 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5324 3427
5325 - $response = wp_remote_post($embed_url, [
5326 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5327 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5328 - 'timeout' => 60,
5329 - ]);
5330 - if (is_wp_error($response)) {
5331 - return [
5332 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5333 - 'error_code' => 'embedding_custom_connection_error',
5334 - ];
5335 - }
5336 - $status = wp_remote_retrieve_response_code($response);
5337 - $body = json_decode(wp_remote_retrieve_body($response), true);
5338 - if ($status !== 200) {
5339 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5340 - return [
5341 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5342 - 'error_code' => 'embedding_custom_api_error',
5343 - 'status_code' => $status,
5344 - ];
5345 - }
5346 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5347 - return $body['data'][0]['embedding'];
5348 - }
5349 - return [
5350 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5351 - 'error_code' => 'embedding_custom_invalid_response',
5352 - ];
5353 -}
3428 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
5354 3429
5355 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5356 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5357 -
5358 - // Check for OpenAI Vector Store first (takes priority when enabled)
5359 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5360 -
5361 - if ($bot_vectorstore_config['use_vectorstore']) {
5362 - // Get current model to verify it's an OpenAI model
5363 - $bot_options = $this->get_bot_options($bot_id);
5364 - $mxchat_options = get_option('mxchat_options', array());
5365 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5366 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5367 -
5368 - if ($this->is_openai_chat_model($selected_model)) {
5369 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5370 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5371 - } else {
5372 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5373 - }
5374 - }
5375 -
5376 - // Get bot-specific Pinecone configuration
5377 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5378 -
5379 - // Debug: Log the Pinecone configuration
5380 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5381 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5382 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5383 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5384 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5385 -
5386 - // Determine whether to use Pinecone based on bot configuration
5387 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5388 -
5389 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5390 -
5391 - if ($use_pinecone) {
5392 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
3430 + if ($use_pinecone === 1) {
3431 + //error_log('MXChat Vector Search: Using Pinecone database');
3432 + return $this->find_relevant_content_pinecone($user_embedding);
5393 3433 } else {
5394 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
3434 + //error_log('MXChat Vector Search: Using WordPress database');
3435 + return $this->find_relevant_content_wordpress($user_embedding);
5395 3436 }
5396 3437 }
5397 3438
5398 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
3439 +private function find_relevant_content_wordpress($user_embedding) {
5399 3440 global $wpdb;
5400 3441 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3442 + $cache_key = 'mxchat_system_prompt_embeddings';
3443 + $batch_size = 500;
3444 +
5401 3445 // Initialize similarity analysis storage
5402 3446 $this->last_similarity_analysis = [
5403 3447 'knowledge_base_type' => 'WordPress Database',
5404 - 'bot_id' => $bot_id,
5405 3448 'top_matches' => [],
5406 3449 'threshold_used' => 0,
5407 3450 'total_checked' => 0
5408 3451 ];
5409 3452
5410 - // NEW: Initialize valid URLs array
5411 - $valid_urls = [];
3453 + // Retrieve embeddings from cache or database
3454 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3455 + if ($embeddings === false) {
3456 + // Cache miss - load embeddings from database WITH CONTENT and ROLE RESTRICTION for testing
3457 + $embeddings = [];
3458 + $offset = 0;
5412 3459
5413 - // Get bot-specific options for similarity threshold
5414 - $bot_options = $this->get_bot_options($bot_id);
5415 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
3460 + do {
3461 + $query = $wpdb->prepare(
3462 + "SELECT id, embedding_vector, article_content, source_url, role_restriction
3463 + FROM {$system_prompt_table}
3464 + LIMIT %d OFFSET %d",
3465 + $batch_size,
3466 + $offset
3467 + );
5416 3468
5417 - // Get knowledge manager instance for role checking
5418 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
3469 + $batch = $wpdb->get_results($query);
3470 + if (empty($batch)) {
3471 + break;
3472 + }
5419 3473
5420 - // Get base similarity threshold from bot options or default options
5421 - $similarity_threshold = isset($current_options['similarity_threshold'])
5422 - ? ((int) $current_options['similarity_threshold']) / 100
5423 - : 0.35;
5424 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3474 + $embeddings = array_merge($embeddings, $batch);
3475 + $offset += $batch_size;
3476 + unset($batch);
3477 + } while (true);
5425 3478
5426 - // Precompute bot_filter once, outside the streaming loop
5427 - $bot_filter = '';
5428 - if ($bot_id !== 'default') {
5429 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5430 - if ($column_exists) {
5431 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
3479 + if (empty($embeddings)) {
3480 + return '';
5432 3481 }
3482 +
3483 + // Cache embeddings for future use (but note: this now includes content and role restrictions)
3484 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
5433 3485 }
5434 3486
5435 - // ===== STREAMING TOP-K PASS =====
5436 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5437 - // - top 10 by raw similarity (for the testing/debug display panel)
5438 - // - candidates above threshold with access (capped) for context assembly
5439 - // This bounds peak memory regardless of knowledge base size and avoids loading
5440 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5441 - $batch_size = 250;
5442 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5443 - $top_display = [];
5444 - $candidates = [];
5445 - $total_checked = 0;
5446 - $offset = 0;
3487 + // NEW: Get knowledge manager instance for role checking
3488 + $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5447 3489
5448 - do {
5449 - $batch = $wpdb->get_results($wpdb->prepare(
5450 - "SELECT id, embedding_vector, source_url, role_restriction
5451 - FROM {$system_prompt_table}
5452 - WHERE 1=1 {$bot_filter}
5453 - LIMIT %d OFFSET %d",
5454 - $batch_size,
5455 - $offset
5456 - ));
5457 -
5458 - if (empty($batch)) {
5459 - break;
5460 - }
5461 -
5462 - foreach ($batch as $row) {
5463 - $database_embedding = $row->embedding_vector
5464 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5465 - : null;
5466 -
5467 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5468 - unset($database_embedding);
5469 - continue;
5470 - }
5471 -
3490 + // Get configuration options
3491 + $main_options = get_option('mxchat_options', []);
3492 +
3493 + // Get base similarity threshold (default 75%)
3494 + $similarity_threshold = isset($main_options['similarity_threshold'])
3495 + ? ((int) $main_options['similarity_threshold']) / 100
3496 + : 0.75;
3497 +
3498 + $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
3499 +
3500 + // Calculate similarities and build results array
3501 + $all_similarities = [];
3502 + $relevant_results = [];
3503 +
3504 + foreach ($embeddings as $embedding) {
3505 + $database_embedding = $embedding->embedding_vector
3506 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3507 + : null;
3508 +
3509 + if (is_array($database_embedding) && is_array($user_embedding)) {
5472 3510 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5473 - unset($database_embedding);
5474 -
5475 - $role_restriction = $row->role_restriction ?? 'public';
3511 +
3512 + // NEW: Check role access
3513 + $role_restriction = $embedding->role_restriction ?? 'public';
5476 3514 $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5477 - $source_url = $row->source_url ?? '';
5478 -
5479 - // Maintain top 10 display buffer (insert-if-beats-worst)
5480 - if (count($top_display) < 10) {
5481 - $top_display[] = [
5482 - 'id' => $row->id,
5483 - 'similarity' => $similarity,
5484 - 'source_url' => $source_url,
5485 - 'role_restriction' => $role_restriction,
5486 - 'has_access' => $has_access,
5487 - ];
5488 - usort($top_display, function ($a, $b) {
5489 - return $b['similarity'] <=> $a['similarity'];
5490 - });
5491 - } elseif ($similarity > $top_display[9]['similarity']) {
5492 - $top_display[9] = [
5493 - 'id' => $row->id,
5494 - 'similarity' => $similarity,
5495 - 'source_url' => $source_url,
5496 - 'role_restriction' => $role_restriction,
5497 - 'has_access' => $has_access,
5498 - ];
5499 - usort($top_display, function ($a, $b) {
5500 - return $b['similarity'] <=> $a['similarity'];
5501 - });
3515 +
3516 + // Store ALL similarities for testing (top 10)
3517 + $source_display = '';
3518 + if (!empty($embedding->source_url) && $embedding->source_url !== '#') {
3519 + $source_display = $embedding->source_url;
3520 + } else {
3521 + $content_preview = strip_tags($embedding->article_content ?? '');
3522 + $content_preview = preg_replace('/\s+/', ' ', $content_preview);
3523 + $source_display = substr(trim($content_preview), 0, 50) . '...';
5502 3524 }
5503 -
5504 - // Track candidates for context assembly (above threshold + has access)
3525 +
3526 + $all_similarities[] = [
3527 + 'document_id' => $embedding->id,
3528 + 'similarity' => $similarity,
3529 + 'similarity_percentage' => round($similarity * 100, 2),
3530 + 'above_threshold' => $similarity >= $similarity_threshold,
3531 + 'source_display' => $source_display,
3532 + 'content_preview' => substr(strip_tags($embedding->article_content ?? ''), 0, 100) . '...',
3533 + 'used_for_context' => false, // Initialize as false, we'll update this later
3534 + 'role_restriction' => $role_restriction, // NEW: Include role info for testing
3535 + 'has_access' => $has_access, // NEW: Include access info for testing
3536 + 'filtered_out' => !$has_access // NEW: Mark if filtered out by role
3537 + ];
3538 +
3539 + // Only consider results above threshold AND with access for actual content retrieval
5505 3540 if ($similarity >= $similarity_threshold && $has_access) {
5506 - $candidates[] = [
5507 - 'id' => $row->id,
5508 - 'similarity' => $similarity,
5509 - 'source_url' => $source_url,
3541 + $relevant_results[] = [
3542 + 'id' => $embedding->id,
3543 + 'similarity' => $similarity
5510 3544 ];
5511 3545 }
5512 -
5513 - $total_checked++;
5514 3546 }
5515 -
5516 - unset($batch);
5517 -
5518 - // Trim candidates periodically to cap memory during long scans
5519 - if (count($candidates) > $max_candidates) {
5520 - usort($candidates, function ($a, $b) {
5521 - return $b['similarity'] <=> $a['similarity'];
5522 - });
5523 - $candidates = array_slice($candidates, 0, $max_candidates);
5524 - }
5525 -
5526 - $offset += $batch_size;
5527 - } while (true);
5528 -
5529 - if ($total_checked === 0) {
5530 - $this->current_valid_urls = [];
5531 - return '';
3547 +
3548 + unset($database_embedding);
5532 3549 }
5533 3550
5534 - // Final candidates sort (best first)
5535 - if (count($candidates) > 1) {
5536 - usort($candidates, function ($a, $b) {
5537 - return $b['similarity'] <=> $a['similarity'];
5538 - });
5539 - }
5540 -
5541 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5542 - // Gather unique IDs we actually need (top_display + candidates) and pull
5543 - // article_content in bounded IN() batches. This avoids loading content for
5544 - // every row during the similarity scan.
5545 - $needed_ids = [];
5546 - foreach ($top_display as $item) {
5547 - $needed_ids[$item['id']] = true;
5548 - }
5549 - foreach ($candidates as $item) {
5550 - $needed_ids[$item['id']] = true;
5551 - }
5552 - $needed_ids = array_keys($needed_ids);
5553 -
5554 - $content_map = [];
5555 - if (!empty($needed_ids)) {
5556 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5557 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5558 - $rows = $wpdb->get_results($wpdb->prepare(
5559 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5560 - ...$chunk_ids
5561 - ));
5562 - foreach ($rows as $r) {
5563 - $content_map[$r->id] = $r->article_content;
5564 - }
5565 - unset($rows);
5566 - }
5567 - }
5568 -
5569 - // Build the all_similarities display array from the top 10
5570 - $all_similarities = [];
5571 - foreach ($top_display as $item) {
5572 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5573 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5574 - $is_chunk = $parsed_for_display['is_chunked'];
5575 - $chunk_meta = $parsed_for_display['metadata'];
5576 -
5577 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5578 - $source_display = $item['source_url'];
5579 - } else {
5580 - $content_preview = strip_tags($article_content_for_parse);
5581 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5582 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5583 - }
5584 -
5585 - $all_similarities[] = [
5586 - 'document_id' => $item['id'],
5587 - 'similarity' => $item['similarity'],
5588 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5589 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5590 - 'source_display' => $source_display,
5591 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5592 - 'used_for_context' => false,
5593 - 'role_restriction' => $item['role_restriction'],
5594 - 'has_access' => $item['has_access'],
5595 - 'filtered_out' => !$item['has_access'],
5596 - 'is_chunk' => $is_chunk,
5597 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5598 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5599 - ];
5600 - }
5601 -
5602 - // Build url_groups from candidates for chunk reassembly
5603 - $url_groups = array();
5604 - foreach ($candidates as $cand) {
5605 - $article_content = $content_map[$cand['id']] ?? '';
5606 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5607 - $is_chunked = $parsed['is_chunked'];
5608 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5609 - $text_content = $parsed['text'];
5610 -
5611 - $source_url = $cand['source_url'];
5612 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5613 -
5614 - if (!isset($url_groups[$group_key])) {
5615 - $url_groups[$group_key] = array(
5616 - 'source_url' => $source_url,
5617 - 'best_score' => 0,
5618 - 'is_chunked' => $is_chunked,
5619 - 'chunks' => array(),
5620 - 'single_text' => '',
5621 - 'single_id' => null
5622 - );
5623 - }
5624 -
5625 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5626 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5627 - }
5628 -
5629 - if ($is_chunked) {
5630 - $url_groups[$group_key]['is_chunked'] = true;
5631 - $url_groups[$group_key]['chunks'][] = array(
5632 - 'id' => $cand['id'],
5633 - 'score' => $cand['similarity'],
5634 - 'chunk_index' => $chunk_index,
5635 - 'text' => $text_content
5636 - );
5637 - } else {
5638 - $url_groups[$group_key]['single_text'] = $text_content;
5639 - $url_groups[$group_key]['single_id'] = $cand['id'];
5640 - }
5641 - }
5642 -
5643 3551 // Sort ALL similarities for testing display (highest first)
5644 3552 usort($all_similarities, function ($a, $b) {
5645 3553 return $b['similarity'] <=> $a['similarity'];
5646 3554 });
5647 -
5648 - // Sort URL groups by best score (highest first)
5649 - uasort($url_groups, function($a, $b) {
5650 - return $b['best_score'] <=> $a['best_score'];
3555 +
3556 + // Sort relevant results by similarity (highest first)
3557 + usort($relevant_results, function ($a, $b) {
3558 + return $b['similarity'] <=> $a['similarity'];
5651 3559 });
5652 -
5653 - // Get RAG sources limit from options (default 6, min 3, max 10)
5654 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5655 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5656 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5657 -
5658 - // Take top N unique URLs based on user setting
5659 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5660 -
5661 - // Track which document IDs are used for context
3560 +
3561 + // Get top 5 results for actual content (standard approach)
3562 + $top_results = array_slice($relevant_results, 0, 5);
3563 +
3564 + // NOW mark which documents are actually used for context
5662 3565 $used_document_ids = [];
5663 - foreach ($top_urls as $group) {
5664 - if ($group['is_chunked']) {
5665 - foreach ($group['chunks'] as $chunk) {
5666 - $used_document_ids[] = $chunk['id'];
5667 - }
5668 - } elseif ($group['single_id']) {
5669 - $used_document_ids[] = $group['single_id'];
5670 - }
3566 + foreach ($top_results as $result) {
3567 + $used_document_ids[] = $result['id'];
5671 3568 }
5672 -
3569 +
5673 3570 // Update the all_similarities array to mark which were actually used
5674 3571 foreach ($all_similarities as &$similarity_item) {
5675 3572 $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5676 3573 }
5677 -
5678 - // Store top 10 for testing panel
3574 +
3575 + // Store top 10 for testing panel (now with correct used_for_context flags and role info)
5679 3576 $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5680 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5681 -
3577 + $this->last_similarity_analysis['total_checked'] = count($embeddings);
3578 +
3579 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " top matches for testing");
3580 +
5682 3581 // Initialize final content
5683 3582 $content = '';
5684 - $matches_used = 0;
5685 - $total_chunks_used = 0;
5686 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5687 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5688 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5689 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5690 -
5691 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5692 - // Use fresh options to ensure we get the latest setting value
5693 - $fresh_options = get_option('mxchat_options', []);
5694 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5695 -
5696 - // Build content from top sources
5697 - foreach ($top_urls as $group_key => $group) {
5698 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5699 -
5700 - // Stop if we've hit the total chunk limit
5701 - if ($total_chunks_used >= $max_total_chunks) {
5702 - break;
3583 +
3584 + // Track document IDs to avoid duplicates
3585 + $added_document_ids = [];
3586 +
3587 + // Fetch and format content for each selected result
3588 + foreach ($top_results as $index => $result) {
3589 + if (in_array($result['id'], $added_document_ids)) {
3590 + continue;
5703 3591 }
5704 -
5705 - $full_text = '';
5706 - $chunks_in_this_source = 1; // Default for non-chunked content
5707 -
5708 - if ($group['is_chunked']) {
5709 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5710 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5711 -
5712 - // Fetch chunks for this URL with limit
5713 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5714 -
5715 - // If fetching all chunks fails, fall back to matched chunks
5716 - if (empty($full_text)) {
5717 - // Sort matched chunks by index and concatenate
5718 - usort($group['chunks'], function($a, $b) {
5719 - return $a['chunk_index'] <=> $b['chunk_index'];
5720 - });
5721 -
5722 - $chunk_texts = array();
5723 - $chunks_in_this_source = 0;
5724 - foreach ($group['chunks'] as $chunk) {
5725 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5726 - break;
5727 - }
5728 - $chunk_texts[] = $chunk['text'];
5729 - $chunks_in_this_source++;
3592 +
3593 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3594 + $added_document_ids[] = $result['id'];
3595 +
3596 + $content .= "## Reference " . ($index + 1) . " ##\n";
3597 + $content .= $chunk_content . "\n\n";
3598 +
3599 + // PDF surrounding pages logic (unchanged)
3600 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
3601 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
3602 + "SELECT id, article_content, role_restriction FROM {$system_prompt_table}
3603 + WHERE id IN (
3604 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
3605 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
3606 + )",
3607 + $result['id'],
3608 + $result['id']
3609 + ));
3610 +
3611 + // NEW: Check role access for surrounding content too
3612 + if (!empty($surrounding_content[0])) {
3613 + $surrounding_role = $surrounding_content[0]->role_restriction ?? 'public';
3614 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3615 + $content .= "## Related Content ##\n";
3616 + $content .= $surrounding_content[0]->article_content . "\n\n";
3617 + $added_document_ids[] = $surrounding_content[0]->id;
5730 3618 }
5731 - $full_text = implode("\n\n", $chunk_texts);
5732 3619 }
5733 - } else {
5734 - $full_text = $group['single_text'];
5735 - $chunks_in_this_source = 1;
5736 - }
5737 -
5738 - if (!empty($full_text)) {
5739 - // Strip URLs from content if citation links are disabled
5740 - if (!$citation_links_enabled) {
5741 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5742 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5743 - }
5744 -
5745 - // Use numbered reference for URL-based entries, plain info label for manual entries
5746 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5747 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5748 - $matches_used++;
5749 - $content .= "## Reference " . $matches_used . " ##\n";
5750 - $content .= $full_text . "\n\n";
5751 -
5752 - // Only include citation URLs if citation links are enabled
5753 - if ($citation_links_enabled) {
5754 - $valid_urls[] = $source_url;
5755 - $content .= "URL: " . $source_url . "\n\n";
3620 +
3621 + if (!empty($surrounding_content[1])) {
3622 + $surrounding_role = $surrounding_content[1]->role_restriction ?? 'public';
3623 + if ($knowledge_manager->mxchat_user_has_content_access($surrounding_role)) {
3624 + $content .= "## Related Content ##\n";
3625 + $content .= $surrounding_content[1]->article_content . "\n\n";
3626 + $added_document_ids[] = $surrounding_content[1]->id;
5756 3627 }
5757 - } else {
5758 - // Manual entry — no reference number, no citation
5759 - $content .= "## Information ##\n";
5760 - $content .= $full_text . "\n\n";
5761 3628 }
5762 -
5763 - // Extract any URLs from the text content itself (only if citation links enabled)
5764 - if ($citation_links_enabled) {
5765 - preg_match_all(
5766 - '#\bhttps?://[^\s<>"\']+#i',
5767 - $full_text,
5768 - $content_urls
5769 - );
5770 - if (!empty($content_urls[0])) {
5771 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5772 - }
5773 - }
5774 -
5775 - $total_chunks_used += $chunks_in_this_source;
5776 3629 }
5777 3630 }
5778 -
5779 - // NEW: Store unique valid URLs for validation
5780 - $this->current_valid_urls = array_unique($valid_urls);
5781 -
5782 - // Store sources and chunks counts for testing/transcript display
5783 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5784 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5785 -
5786 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5787 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5788 -
3631 +
5789 3632 // Add response guidelines
5790 - if (empty($top_urls)) {
3633 + if (empty($top_results)) {
5791 3634 $content = "No reference information was found for this query.\n\n";
5792 3635 } else {
5793 - // Build response guidelines based on citation links setting
5794 3636 $content .= "\n## Response Guidelines ##\n" .
5795 3637 "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5796 3638 "Be conversational and friendly, but never mention your knowledge base or training data. " .
5797 3639 "If you don't have specific information or are uncertain about any details, it's always " .
5798 3640 "better to honestly say you don't know rather than making up or guessing at answers. " .
5799 - "When information is incomplete, let them know you are unsure.\n\n";
5800 -
5801 - // Only add hyperlink instructions if citation links are enabled
5802 - if ($citation_links_enabled) {
5803 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5804 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5805 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5806 - } else {
5807 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5808 - "Simply provide helpful answers based on the reference information without citing sources.";
5809 - }
3641 + "When information is incomplete, let them know you are unsure.";
5810 3642 }
5811 3643
5812 3644 return trim($content);
5813 3645 }
5814 3646
5815 -/**
5816 - * Fetch and reassemble chunks for a URL from WordPress database
5817 - *
5818 - * @param string $source_url The source URL to fetch chunks for
5819 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5820 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5821 - * @return string Reassembled content from chunks
5822 - */
5823 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5824 - global $wpdb;
5825 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5826 -
5827 - // Fetch all rows with this source_url
5828 - $rows = $wpdb->get_results($wpdb->prepare(
5829 - "SELECT article_content FROM {$table}
5830 - WHERE source_url = %s
5831 - ORDER BY id ASC",
5832 - $source_url
5833 - ));
5834 -
5835 - if (empty($rows)) {
5836 - $chunk_count = 0;
5837 - return '';
5838 - }
5839 -
5840 - // Parse and sort chunks by index
5841 - $chunks = array();
5842 - foreach ($rows as $row) {
5843 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5844 -
5845 - if ($parsed['is_chunked']) {
5846 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5847 - $chunks[$chunk_index] = $parsed['text'];
5848 - } else {
5849 - // Non-chunked content - just return it
5850 - $chunks[] = $parsed['text'];
5851 - }
5852 - }
5853 -
5854 - // Sort by chunk index
5855 - ksort($chunks);
5856 -
5857 - // Apply chunk limit if specified
5858 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5859 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5860 - }
5861 -
5862 - // Store actual chunk count
5863 - $chunk_count = count($chunks);
5864 -
5865 - // Reassemble content
5866 - return implode("\n\n", $chunks);
5867 -}
5868 -
5869 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5870 - global $wpdb;
3647 +private function find_relevant_content_pinecone($user_embedding) {
3648 + global $wpdb; // For single role lookups
3649 + $options = get_option('mxchat_pinecone_addon_options', array());
3650 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
3651 + $host = $options['mxchat_pinecone_host'] ?? '';
5871 3652
5872 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5873 - //error_log(" - bot_id: " . $bot_id);
5874 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5875 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5876 -
5877 - // Use bot-specific config or fall back to default
5878 - if ($bot_config === null) {
5879 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5880 - }
5881 -
5882 - $api_key = $bot_config['api_key'] ?? '';
5883 - $host = $bot_config['host'] ?? '';
5884 - $namespace = $bot_config['namespace'] ?? '';
5885 -
5886 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5887 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5888 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5889 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5890 -
5891 3653 // Initialize similarity analysis storage
5892 3654 $this->last_similarity_analysis = [
5893 3655 'knowledge_base_type' => 'Pinecone',
5894 - 'bot_id' => $bot_id,
5895 - 'namespace' => $namespace,
5896 3656 'top_matches' => [],
5897 3657 'threshold_used' => 0,
5898 3658 'total_checked' => 0
5899 3659 ];
5900 3660
5901 - // NEW: Initialize valid URLs array
5902 - $valid_urls = [];
5903 -
5904 3661 if (empty($host) || empty($api_key)) {
5905 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5906 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5907 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5908 - // Store empty array for valid URLs since we can't proceed
5909 - $this->current_valid_urls = [];
5910 3662 return '';
5911 3663 }
5912 3664
5913 3665 // Get knowledge manager instance for role checking
@@ -5912,37 +3664,26 @@
5912 3664
5913 3665 // Get knowledge manager instance for role checking
5914 3666 $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5915 3667
5916 - // Get the similarity threshold from the bot options or main options
5917 - $bot_options = $this->get_bot_options($bot_id);
5918 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
3668 + // Get the similarity threshold from the main options
3669 + $main_options = get_option('mxchat_options', []);
3670 + $similarity_threshold = isset($main_options['similarity_threshold'])
3671 + ? ((int) $main_options['similarity_threshold']) / 100
3672 + : 0.75;
5919 3673
5920 - $similarity_threshold = isset($current_options['similarity_threshold'])
5921 - ? ((int) $current_options['similarity_threshold']) / 100
5922 - : 0.35;
5923 -
5924 3674 $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5925 3675
5926 - // Prepare the query request for Pinecone
3676 + // Prepare the query request for Pinecone (request more for testing)
5927 3677 $api_endpoint = "https://{$host}/query";
5928 3678
5929 3679 $request_body = array(
5930 3680 'vector' => $user_embedding,
5931 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
3681 + 'topK' => 20, // Request more to get good testing data
5932 3682 'includeMetadata' => true,
5933 3683 'includeValues' => true
5934 3684 );
5935 3685
5936 - // Add namespace if specified for this bot
5937 - if (!empty($namespace)) {
5938 - $request_body['namespace'] = $namespace;
5939 - }
5940 -
5941 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5942 - //error_log(" - Endpoint: " . $api_endpoint);
5943 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5944 -
5945 3686 $response = wp_remote_post($api_endpoint, array(
5946 3687 'headers' => array(
5947 3688 'Api-Key' => $api_key,
5948 3689 'accept' => 'application/json',
@@ -5952,242 +3693,62 @@
5952 3693 'timeout' => 30
5953 3694 ));
5954 3695
5955 3696 if (is_wp_error($response)) {
5956 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5957 - // Store empty array for valid URLs
5958 - $this->current_valid_urls = [];
5959 3697 return '';
5960 3698 }
5961 3699
5962 3700 $response_code = wp_remote_retrieve_response_code($response);
5963 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5964 -
5965 3701 if ($response_code !== 200) {
5966 - $response_body = wp_remote_retrieve_body($response);
5967 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5968 - // Store empty array for valid URLs
5969 - $this->current_valid_urls = [];
5970 3702 return '';
5971 3703 }
5972 3704
5973 - // ADD DETAILED DEBUG SECTION HERE
5974 - $response_body = wp_remote_retrieve_body($response);
5975 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5976 -
5977 - $results = json_decode($response_body, true);
5978 -
5979 - if (json_last_error() !== JSON_ERROR_NONE) {
5980 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5981 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5982 - // Store empty array for valid URLs
5983 - $this->current_valid_urls = [];
5984 - return '';
5985 - }
5986 -
5987 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5988 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5989 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5990 -
3705 + $results = json_decode(wp_remote_retrieve_body($response), true);
5991 3706 if (empty($results['matches'])) {
5992 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5993 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5994 - // Store empty array for valid URLs
5995 - $this->current_valid_urls = [];
5996 3707 return '';
5997 3708 }
5998 3709
5999 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
6000 -
6001 - // Log first match details for debugging
6002 - if (!empty($results['matches'][0])) {
6003 - $first_match = $results['matches'][0];
6004 - //error_log("MXCHAT DEBUG: First match details:");
6005 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
6006 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
6007 - if (isset($first_match['metadata'])) {
6008 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
6009 - }
6010 - }
6011 -
6012 3710 // Initialize the final content
6013 3711 $content = '';
6014 3712 $matches_used = 0;
6015 3713 $matches_used_for_context = [];
6016 - $total_chunks_used = 0;
6017 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6018 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6019 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6020 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
6021 -
6022 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6023 - // Use fresh options to ensure we get the latest setting value
6024 - $fresh_options = get_option('mxchat_options', []);
6025 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6026 -
6027 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
6028 - $url_groups = array();
6029 -
3714 +
3715 + // Process each match for actual content generation (lazy role checking)
6030 3716 foreach ($results['matches'] as $index => $match) {
6031 3717 // Skip if similarity is below threshold
6032 3718 if ($match['score'] < $similarity_threshold) {
6033 3719 continue;
6034 3720 }
6035 -
6036 - $metadata = $match['metadata'] ?? array();
6037 - $source_url = $metadata['source_url'] ?? '';
6038 - $match_id = $match['id'] ?? '';
6039 -
6040 - // LAZY ROLE CHECK: Only check role for content we're actually considering
6041 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
6042 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6043 -
6044 - // Skip if user doesn't have access
6045 - if (!$has_access) {
6046 - continue;
6047 - }
6048 -
6049 - // Use a unique key for manual entries without a source URL
6050 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
6051 -
6052 - // Group by source URL (or unique key for manual entries)
6053 - if (!isset($url_groups[$group_key])) {
6054 - $url_groups[$group_key] = array(
6055 - 'source_url' => $source_url,
6056 - 'best_score' => 0,
6057 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
6058 - 'chunks' => array(),
6059 - 'single_text' => ''
6060 - );
6061 - }
6062 -
6063 - // Track best score for this group
6064 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
6065 - $url_groups[$group_key]['best_score'] = $match['score'];
6066 - }
6067 -
6068 - // Store chunk info or single text
6069 - if ($url_groups[$group_key]['is_chunked']) {
6070 - $url_groups[$group_key]['chunks'][] = array(
6071 - 'id' => $match_id,
6072 - 'score' => $match['score'],
6073 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
6074 - 'text' => $metadata['text'] ?? ''
6075 - );
6076 - } else {
6077 - // Non-chunked content - just store the text
6078 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
6079 - $url_groups[$group_key]['single_id'] = $match_id;
6080 - }
6081 - }
6082 -
6083 - // Sort URL groups by best score (highest first)
6084 - uasort($url_groups, function($a, $b) {
6085 - return $b['best_score'] <=> $a['best_score'];
6086 - });
6087 -
6088 - // Get RAG sources limit from options (default 6, min 3, max 10)
6089 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6090 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6091 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
6092 -
6093 - // Take top N unique URLs based on user setting
6094 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
6095 -
6096 - // Track which match IDs are actually used for context
6097 - foreach ($top_urls as $group) {
6098 - if ($group['is_chunked']) {
6099 - foreach ($group['chunks'] as $chunk) {
6100 - $matches_used_for_context[] = $chunk['id'];
6101 - }
6102 - } elseif (!empty($group['single_id'])) {
6103 - $matches_used_for_context[] = $group['single_id'];
6104 - }
6105 - }
6106 -
6107 - // Build content from top sources
6108 - foreach ($top_urls as $group_key => $group) {
6109 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6110 -
6111 - // Stop if we've hit the total chunk limit
6112 - if ($total_chunks_used >= $max_total_chunks) {
3721 +
3722 + // Limit to top 5 matches above threshold
3723 + if ($matches_used >= 5) {
6113 3724 break;
6114 3725 }
6115 -
6116 - $full_text = '';
6117 - $chunks_in_this_source = 1; // Default for non-chunked content
6118 -
6119 - if ($group['is_chunked']) {
6120 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6121 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6122 -
6123 - // Fetch chunks for this URL with limit
6124 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6125 -
6126 - // If fetching all chunks fails, fall back to matched chunks
6127 - if (empty($full_text)) {
6128 - // Sort matched chunks by index and concatenate
6129 - usort($group['chunks'], function($a, $b) {
6130 - return $a['chunk_index'] <=> $b['chunk_index'];
6131 - });
6132 -
6133 - $chunk_texts = array();
6134 - $chunks_in_this_source = 0;
6135 - foreach ($group['chunks'] as $chunk) {
6136 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6137 - break;
6138 - }
6139 - $chunk_texts[] = $chunk['text'];
6140 - $chunks_in_this_source++;
6141 - }
6142 - $full_text = implode("\n\n", $chunk_texts);
3726 +
3727 + if (!empty($match['metadata']['text'])) {
3728 + // LAZY ROLE CHECK: Only check role for content we're actually considering
3729 + $match_id = $match['id'] ?? '';
3730 + $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
3731 + $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
3732 +
3733 + // Skip if user doesn't have access
3734 + if (!$has_access) {
3735 + continue;
6143 3736 }
6144 - } else {
6145 - $full_text = $group['single_text'];
6146 - $chunks_in_this_source = 1;
6147 - }
6148 -
6149 - if (!empty($full_text)) {
6150 - // Strip URLs from content if citation links are disabled
6151 - if (!$citation_links_enabled) {
6152 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6153 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
3737 +
3738 + // User has access - add to content
3739 + $content .= "## Reference " . ($matches_used + 1) . " ##\n";
3740 + $content .= $match['metadata']['text'] . "\n\n";
3741 +
3742 + if (!empty($match['metadata']['source_url'])) {
3743 + $content .= "URL: " . $match['metadata']['source_url'] . "\n\n";
6154 3744 }
6155 -
6156 - // Use numbered reference for URL-based entries, plain info label for manual entries
6157 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6158 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6159 - $matches_used++;
6160 - $content .= "## Reference " . $matches_used . " ##\n";
6161 - $content .= $full_text . "\n\n";
6162 -
6163 - // Only include citation URLs if citation links are enabled
6164 - if ($citation_links_enabled) {
6165 - $valid_urls[] = $source_url;
6166 - $content .= "URL: " . $source_url . "\n\n";
6167 - }
6168 - } else {
6169 - // Manual entry — no reference number, no citation
6170 - $content .= "## Information ##\n";
6171 - $content .= $full_text . "\n\n";
6172 - }
6173 -
6174 - // Extract any URLs from the text content itself (only if citation links enabled)
6175 - if ($citation_links_enabled) {
6176 - preg_match_all(
6177 - '#\bhttps?://[^\s<>"\']+#i',
6178 - $full_text,
6179 - $content_urls
6180 - );
6181 - if (!empty($content_urls[0])) {
6182 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6183 - }
6184 - }
6185 -
6186 - $total_chunks_used += $chunks_in_this_source;
3745 +
3746 + $matches_used_for_context[] = $match['id'] ?? $index;
3747 + $matches_used++;
6187 3748 }
6188 3749 }
6189 -
3750 +
6190 3751 // Process ALL matches for testing data (top 10) - with role checking for testing display
6191 3752 $all_matches = [];
6192 3753 foreach ($results['matches'] as $index => $match) {
6193 3754 if ($index >= 10) break; // Limit to top 10 for testing
@@ -6207,19 +3768,9 @@
6207 3768 $source_display = substr(trim($content_preview), 0, 50) . '...';
6208 3769 }
6209 3770
6210 3771 $match_id_for_display = $match['id'] ?? $index;
6211 -
6212 - // Check for chunk metadata in Pinecone
6213 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6214 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6215 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6216 -
6217 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6218 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6219 - $is_chunk = true;
6220 - }
6221 -
3772 +
6222 3773 $all_matches[] = [
6223 3774 'document_id' => $match_id_for_display,
6224 3775 'similarity' => $match['score'],
6225 3776 'similarity_percentage' => round($match['score'] * 100, 2),
@@ -6228,12 +3779,9 @@
6228 3779 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6229 3780 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6230 3781 'role_restriction' => $role_restriction,
6231 3782 'has_access' => $has_access,
6232 - 'filtered_out' => !$has_access,
6233 - 'is_chunk' => $is_chunk,
6234 - 'chunk_index' => $chunk_index,
6235 - 'total_chunks' => $total_chunks
3783 + 'filtered_out' => !$has_access
6236 3784 ];
6237 3785 }
6238 3786
6239 3787 // Store for testing panel
@@ -6238,40 +3786,23 @@
6238 3786
6239 3787 // Store for testing panel
6240 3788 $this->last_similarity_analysis['top_matches'] = $all_matches;
6241 3789 $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6242 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6243 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6244 -
6245 - // NEW: Store unique valid URLs for validation
6246 - $this->current_valid_urls = array_unique($valid_urls);
6247 -
6248 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6249 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6250 -
3790 +
3791 + //error_log("MxChat Testing: Stored " . count($this->last_similarity_analysis['top_matches']) . " Pinecone matches for testing");
3792 +
6251 3793 // Add response guidelines
6252 3794 if ($matches_used === 0) {
6253 3795 $content = "No reference information was found for this query.\n\n";
6254 3796 } else {
6255 - // Build response guidelines based on citation links setting
6256 - $content .= "\n## Response Guidelines ##\n" .
6257 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6258 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6259 - "If you don't have specific information or are uncertain about any details, it's always " .
6260 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6261 - "When information is incomplete, let them know you are unsure.\n\n";
6262 -
6263 - // Only add hyperlink instructions if citation links are enabled
6264 - if ($citation_links_enabled) {
6265 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6266 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6267 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6268 - } else {
6269 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6270 - "Simply provide helpful answers based on the reference information without citing sources.";
6271 - }
3797 + $content .= "\n## Response Guidelines ##\n" .
3798 + "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
3799 + "Be conversational and friendly, but never mention your knowledge base or training data. " .
3800 + "If you don't have specific information or are uncertain about any details, it's always " .
3801 + "better to honestly say you don't know rather than making up or guessing at answers. " .
3802 + "When information is incomplete, let them know you are unsure.";
6272 3803 }
6273 -
3804 +
6274 3805 return trim($content);
6275 3806 }
6276 3807
6277 3808 /**
@@ -6311,518 +3842,12 @@
6311 3842 }
6312 3843
6313 3844 // Cache individual role for 1 hour
6314 3845 wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6315 -
3846 +
6316 3847 return $role_restriction;
6317 3848 }
6318 3849
6319 -/**
6320 - * Fetch and reassemble all chunks for a URL from Pinecone
6321 - *
6322 - * @param string $source_url The source URL to fetch chunks for
6323 - * @param array $bot_config Bot-specific Pinecone configuration
6324 - * @return string Reassembled content from all chunks
6325 - */
6326 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6327 - $api_key = $bot_config['api_key'] ?? '';
6328 - $host = $bot_config['host'] ?? '';
6329 - $namespace = $bot_config['namespace'] ?? '';
6330 -
6331 - if (empty($host) || empty($api_key)) {
6332 - $chunk_count = 0;
6333 - return '';
6334 - }
6335 -
6336 - $base_hash = md5($source_url);
6337 -
6338 - // Use Pinecone list API to find all chunk vectors with this prefix
6339 - $list_url = "https://{$host}/vectors/list";
6340 -
6341 - // Limit to max_chunks if specified, otherwise fetch up to 100
6342 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6343 -
6344 - $list_body = array(
6345 - 'prefix' => $base_hash . '_chunk_',
6346 - 'limit' => $fetch_limit
6347 - );
6348 -
6349 - if (!empty($namespace)) {
6350 - $list_body['namespace'] = $namespace;
6351 - }
6352 -
6353 - $list_response = wp_remote_post($list_url, array(
6354 - 'headers' => array(
6355 - 'Api-Key' => $api_key,
6356 - 'accept' => 'application/json',
6357 - 'content-type' => 'application/json'
6358 - ),
6359 - 'body' => wp_json_encode($list_body),
6360 - 'timeout' => 30
6361 - ));
6362 -
6363 - if (is_wp_error($list_response)) {
6364 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6365 - return '';
6366 - }
6367 -
6368 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6369 -
6370 - if (empty($list_data['vectors'])) {
6371 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6372 - return '';
6373 - }
6374 -
6375 - // Extract vector IDs
6376 - $vector_ids = array();
6377 - foreach ($list_data['vectors'] as $vector) {
6378 - if (isset($vector['id'])) {
6379 - $vector_ids[] = $vector['id'];
6380 - }
6381 - }
6382 -
6383 - if (empty($vector_ids)) {
6384 - return '';
6385 - }
6386 -
6387 - // Fetch all chunk content
6388 - $fetch_url = "https://{$host}/vectors/fetch";
6389 -
6390 - $fetch_body = array(
6391 - 'ids' => $vector_ids
6392 - );
6393 -
6394 - if (!empty($namespace)) {
6395 - $fetch_body['namespace'] = $namespace;
6396 - }
6397 -
6398 - $fetch_response = wp_remote_post($fetch_url, array(
6399 - 'headers' => array(
6400 - 'Api-Key' => $api_key,
6401 - 'accept' => 'application/json',
6402 - 'content-type' => 'application/json'
6403 - ),
6404 - 'body' => wp_json_encode($fetch_body),
6405 - 'timeout' => 30
6406 - ));
6407 -
6408 - if (is_wp_error($fetch_response)) {
6409 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6410 - return '';
6411 - }
6412 -
6413 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6414 -
6415 - if (empty($fetch_data['vectors'])) {
6416 - return '';
6417 - }
6418 -
6419 - // Sort chunks by index and reassemble
6420 - $chunks = array();
6421 - foreach ($fetch_data['vectors'] as $id => $vector) {
6422 - $metadata = $vector['metadata'] ?? array();
6423 - $chunk_index = $metadata['chunk_index'] ?? 0;
6424 - $text = $metadata['text'] ?? '';
6425 -
6426 - // Store chunk with its index
6427 - $chunks[$chunk_index] = $text;
6428 - }
6429 -
6430 - // Sort by chunk index
6431 - ksort($chunks);
6432 -
6433 - // Apply chunk limit if specified
6434 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6435 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6436 - }
6437 -
6438 - // Store actual chunk count
6439 - $chunk_count = count($chunks);
6440 -
6441 - // Reassemble content
6442 - return implode("\n\n", $chunks);
6443 -}
6444 -
6445 -/**
6446 - * Search for relevant content using OpenAI Vector Store (File Search)
6447 - *
6448 - * @param string $user_query The user's query text
6449 - * @param string $bot_id The bot ID
6450 - * @param array $vectorstore_config Vector Store configuration
6451 - * @return string Formatted context string with references
6452 - */
6453 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6454 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6455 - //error_log(" - bot_id: " . $bot_id);
6456 - //error_log(" - user_query length: " . strlen($user_query));
6457 -
6458 - // Get OpenAI API key
6459 - $mxchat_options = get_option('mxchat_options', array());
6460 - $api_key = $mxchat_options['api_key'] ?? '';
6461 -
6462 - // Reset vectorstore error tracking
6463 - $this->last_vectorstore_error = null;
6464 -
6465 - if (empty($api_key)) {
6466 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6467 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6468 - $this->current_valid_urls = [];
6469 - return '';
6470 - }
6471 -
6472 - // Get Vector Store configuration
6473 - if (empty($vectorstore_config)) {
6474 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6475 - }
6476 -
6477 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6478 - $max_results = $vectorstore_config['max_results'] ?? 5;
6479 -
6480 - if (empty($vectorstore_ids_string)) {
6481 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6482 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6483 - $this->current_valid_urls = [];
6484 - return '';
6485 - }
6486 -
6487 - // Parse Vector Store IDs
6488 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6489 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6490 -
6491 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6492 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6493 -
6494 - // Initialize similarity analysis storage
6495 - $this->last_similarity_analysis = [
6496 - 'knowledge_base_type' => 'OpenAI Vector Store',
6497 - 'bot_id' => $bot_id,
6498 - 'vectorstore_ids' => $vectorstore_ids,
6499 - 'top_matches' => [],
6500 - 'threshold_used' => 0,
6501 - 'total_checked' => 0
6502 - ];
6503 -
6504 - $valid_urls = [];
6505 -
6506 - // Get the selected model
6507 - $bot_options = $this->get_bot_options($bot_id);
6508 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6509 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6510 -
6511 - // Verify it's an OpenAI model
6512 - if (!$this->is_openai_chat_model($selected_model)) {
6513 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6514 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6515 - $this->current_valid_urls = [];
6516 - return '';
6517 - }
6518 -
6519 - // Use OpenAI Responses API with file_search tool
6520 - $request_body = array(
6521 - 'model' => $selected_model,
6522 - 'input' => $user_query,
6523 - 'tools' => array(
6524 - array(
6525 - 'type' => 'file_search',
6526 - 'vector_store_ids' => $vectorstore_ids,
6527 - 'max_num_results' => intval($max_results)
6528 - )
6529 - ),
6530 - 'include' => array('output[*].file_search_call.search_results')
6531 - );
6532 -
6533 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6534 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6535 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6536 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6537 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6538 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6539 -
6540 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6541 - 'headers' => array(
6542 - 'Authorization' => 'Bearer ' . $api_key,
6543 - 'Content-Type' => 'application/json'
6544 - ),
6545 - 'body' => wp_json_encode($request_body),
6546 - 'timeout' => 60
6547 - ));
6548 -
6549 - if (is_wp_error($response)) {
6550 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6551 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6552 - $this->current_valid_urls = [];
6553 - return '';
6554 - }
6555 -
6556 - $response_code = wp_remote_retrieve_response_code($response);
6557 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6558 -
6559 - $response_body = wp_remote_retrieve_body($response);
6560 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6561 -
6562 - if ($response_code !== 200) {
6563 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6564 - $api_error_detail = '';
6565 - $decoded_error = json_decode($response_body, true);
6566 - if (isset($decoded_error['error']['message'])) {
6567 - $api_error_detail = $decoded_error['error']['message'];
6568 - }
6569 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6570 - $this->current_valid_urls = [];
6571 - return '';
6572 - }
6573 - $result = json_decode($response_body, true);
6574 -
6575 - if (json_last_error() !== JSON_ERROR_NONE) {
6576 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6577 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6578 - $this->current_valid_urls = [];
6579 - return '';
6580 - }
6581 -
6582 - // Debug: Log the structure of the result
6583 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6584 - if (isset($result['output'])) {
6585 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6586 - foreach ($result['output'] as $idx => $out) {
6587 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6588 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6589 - }
6590 - } else {
6591 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6592 - }
6593 -
6594 - // Extract file search results from the response
6595 - $content = '';
6596 - $matches_used = 0;
6597 - $all_matches = [];
6598 -
6599 - // The Responses API returns output array with tool results
6600 - if (isset($result['output']) && is_array($result['output'])) {
6601 - foreach ($result['output'] as $output_item) {
6602 - // Look for file_search_call results
6603 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6604 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6605 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6606 -
6607 - // Check for search_results in the output item directly
6608 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6609 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6610 -
6611 - if (empty($search_results)) {
6612 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6613 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6614 - }
6615 -
6616 - foreach ($search_results as $index => $search_result) {
6617 - $filename = $search_result['filename'] ?? '';
6618 - $score = $search_result['score'] ?? 0;
6619 - $text_content = '';
6620 -
6621 - // Extract text content from the result
6622 - // The text can be directly on the result OR nested under content array
6623 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6624 - // Direct text field (OpenAI's actual format)
6625 - $text_content = $search_result['text'];
6626 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6627 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6628 - // Nested content array format
6629 - foreach ($search_result['content'] as $content_item) {
6630 - if (isset($content_item['text'])) {
6631 - $text_content .= $content_item['text'] . "\n";
6632 - }
6633 - }
6634 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6635 - } else {
6636 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6637 - }
6638 -
6639 - if (!empty($text_content)) {
6640 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6641 - $content .= trim($text_content) . "\n\n";
6642 -
6643 - if (!empty($filename)) {
6644 - $content .= "Source: " . $filename . "\n\n";
6645 - }
6646 -
6647 - // Extract URLs from content
6648 - preg_match_all(
6649 - '#\bhttps?://[^\s<>"\']+#i',
6650 - $text_content,
6651 - $content_urls
6652 - );
6653 - if (!empty($content_urls[0])) {
6654 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6655 - }
6656 -
6657 - $matches_used++;
6658 - }
6659 -
6660 - // Store for similarity analysis
6661 - $all_matches[] = [
6662 - 'document_id' => $filename ?: ('result_' . $index),
6663 - 'similarity' => $score,
6664 - 'similarity_percentage' => round($score * 100, 2),
6665 - 'above_threshold' => true,
6666 - 'source_display' => $filename,
6667 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6668 - 'used_for_context' => true,
6669 - 'role_restriction' => 'public',
6670 - 'has_access' => true,
6671 - 'filtered_out' => false
6672 - ];
6673 - }
6674 - }
6675 -
6676 - // Also check for message content with annotations (citations)
6677 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6678 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6679 - foreach ($output_item['content'] as $content_block) {
6680 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6681 - foreach ($content_block['annotations'] as $annotation) {
6682 - if (isset($annotation['filename'])) {
6683 - $filename = $annotation['filename'];
6684 - $score = $annotation['score'] ?? 0;
6685 - $text_content = '';
6686 -
6687 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6688 - foreach ($annotation['content'] as $ann_content) {
6689 - if (isset($ann_content['text'])) {
6690 - $text_content .= $ann_content['text'] . "\n";
6691 - }
6692 - }
6693 - }
6694 -
6695 - if (!empty($text_content) && $matches_used < $max_results) {
6696 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6697 - $content .= trim($text_content) . "\n\n";
6698 - $content .= "Source: " . $filename . "\n\n";
6699 -
6700 - preg_match_all(
6701 - '#\bhttps?://[^\s<>"\']+#i',
6702 - $text_content,
6703 - $content_urls
6704 - );
6705 - if (!empty($content_urls[0])) {
6706 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6707 - }
6708 -
6709 - $matches_used++;
6710 -
6711 - $all_matches[] = [
6712 - 'document_id' => $filename,
6713 - 'similarity' => $score,
6714 - 'similarity_percentage' => round($score * 100, 2),
6715 - 'above_threshold' => true,
6716 - 'source_display' => $filename,
6717 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6718 - 'used_for_context' => true,
6719 - 'role_restriction' => 'public',
6720 - 'has_access' => true,
6721 - 'filtered_out' => false
6722 - ];
6723 - }
6724 - }
6725 - }
6726 - }
6727 - }
6728 - }
6729 - }
6730 - }
6731 - }
6732 -
6733 - // Store for testing panel
6734 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6735 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6736 -
6737 - // Store unique valid URLs for validation
6738 - $this->current_valid_urls = array_unique($valid_urls);
6739 -
6740 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6741 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6742 -
6743 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6744 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6745 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6746 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6747 - if ($matches_used > 0) {
6748 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6749 - }
6750 -
6751 - // Check if citation links are enabled
6752 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6753 -
6754 - // Add response guidelines
6755 - if ($matches_used === 0) {
6756 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6757 - $content = "No reference information was found for this query.\n\n";
6758 - } else {
6759 - // Build response guidelines based on citation links setting
6760 - $content .= "\n## Response Guidelines ##\n" .
6761 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6762 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6763 - "If you don't have specific information or are uncertain about any details, it's always " .
6764 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6765 - "When information is incomplete, let them know you are unsure.\n\n";
6766 -
6767 - // Only add hyperlink instructions if citation links are enabled
6768 - if ($citation_links_enabled) {
6769 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6770 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6771 - } else {
6772 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6773 - "Simply provide helpful answers based on the reference information without citing sources.";
6774 - }
6775 - }
6776 -
6777 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6778 -
6779 - return trim($content);
6780 -}
6781 -
6782 -/**
6783 - * Check if the given model is an OpenAI chat model
6784 - *
6785 - * @param string $model The model ID
6786 - * @return bool True if it's an OpenAI model
6787 - */
6788 -private function is_openai_chat_model($model) {
6789 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6790 - foreach ($openai_prefixes as $prefix) {
6791 - if (strpos($model, $prefix) === 0) {
6792 - return true;
6793 - }
6794 - }
6795 - return false;
6796 -}
6797 -
6798 -/**
6799 - * Get bot-specific Vector Store configuration
6800 - *
6801 - * @param string $bot_id The bot ID
6802 - * @return array Configuration array
6803 - */
6804 -private function get_bot_vectorstore_config($bot_id = 'default') {
6805 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6806 -
6807 - // Default global settings
6808 - $default_config = array(
6809 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6810 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6811 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6812 - );
6813 -
6814 - // Allow multi-bot plugin to override with bot-specific settings
6815 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6816 -
6817 - // Preserve max_results from global settings if not set in bot config
6818 - if (!isset($bot_config['max_results'])) {
6819 - $bot_config['max_results'] = $default_config['max_results'];
6820 - }
6821 -
6822 - return $bot_config;
6823 -}
6824 -
6825 3850 private function mxchat_find_relevant_products($user_embedding) {
6826 3851 //error_log('MXChat Vector Search: Starting product search...');
6827 3852
6828 3853 // Retrieve the add-on settings from the database
@@ -6843,75 +3868,73 @@
6843 3868 }
6844 3869 private function find_relevant_products_wordpress($user_embedding) {
6845 3870 global $wpdb;
6846 3871 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3872 + $cache_key = 'mxchat_system_prompt_embeddings';
3873 + $batch_size = 500;
6847 3874
6848 - if (!is_array($user_embedding)) {
6849 - return '';
6850 - }
3875 + // Original WordPress database search logic
3876 + // [Previous implementation remains the same]
3877 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3878 + if ($embeddings === false) {
3879 + $embeddings = [];
3880 + $offset = 0;
6851 3881
6852 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6853 - // results above the similarity threshold. Peak memory is bounded by
6854 - // $batch_size embedding rows plus a 3-element top list.
6855 - $batch_size = 250;
6856 - $similarity_threshold = 0.85;
6857 - $top_k = 3;
6858 - $top_results = [];
6859 - $offset = 0;
3882 + do {
3883 + $query = $wpdb->prepare(
3884 + "SELECT id, embedding_vector
3885 + FROM {$system_prompt_table}
3886 + LIMIT %d OFFSET %d",
3887 + $batch_size,
3888 + $offset
3889 + );
6860 3890
6861 - do {
6862 - $batch = $wpdb->get_results($wpdb->prepare(
6863 - "SELECT id, embedding_vector
6864 - FROM {$system_prompt_table}
6865 - LIMIT %d OFFSET %d",
6866 - $batch_size,
6867 - $offset
6868 - ));
3891 + $batch = $wpdb->get_results($query);
3892 + if (empty($batch)) {
3893 + break;
3894 + }
6869 3895
6870 - if (empty($batch)) {
6871 - break;
6872 - }
3896 + $embeddings = array_merge($embeddings, $batch);
3897 + $offset += $batch_size;
6873 3898
6874 - foreach ($batch as $row) {
6875 - $database_embedding = $row->embedding_vector
6876 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6877 - : null;
3899 + unset($batch);
6878 3900
6879 - if (!is_array($database_embedding)) {
6880 - unset($database_embedding);
6881 - continue;
6882 - }
3901 + } while (true);
6883 3902
3903 + if (empty($embeddings)) {
3904 + return '';
3905 + }
3906 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3907 + }
3908 +
3909 + $relevant_results = [];
3910 + foreach ($embeddings as $embedding) {
3911 + $database_embedding = $embedding->embedding_vector
3912 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3913 + : null;
3914 + if (is_array($database_embedding) && is_array($user_embedding)) {
6884 3915 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6885 - unset($database_embedding);
6886 -
6887 - if ($similarity < $similarity_threshold) {
6888 - continue;
6889 - }
6890 -
6891 - // Insert into bounded top-K (kept sorted descending)
6892 - if (count($top_results) < $top_k) {
6893 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6894 - usort($top_results, function ($a, $b) {
6895 - return $b['similarity'] <=> $a['similarity'];
6896 - });
6897 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6898 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6899 - usort($top_results, function ($a, $b) {
6900 - return $b['similarity'] <=> $a['similarity'];
6901 - });
6902 - }
3916 + $relevant_results[] = [
3917 + 'id' => $embedding->id,
3918 + 'similarity' => $similarity
3919 + ];
6903 3920 }
3921 + unset($database_embedding);
3922 + }
6904 3923
6905 - unset($batch);
6906 - $offset += $batch_size;
6907 - } while (true);
3924 + // Use fixed threshold for products
3925 + $similarity_threshold = 0.85;
6908 3926
6909 - if (empty($top_results)) {
6910 - return '';
6911 - }
3927 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3928 + return $result['similarity'] >= $similarity_threshold;
3929 + });
3930 + usort($relevant_results, function ($a, $b) {
3931 + return $b['similarity'] <=> $a['similarity'];
3932 + });
6912 3933
3934 + $top_results = array_slice($relevant_results, 0, 5);
6913 3935 $content = '';
3936 +
6914 3937 foreach ($top_results as $result) {
6915 3938 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6916 3939 $content .= $chunk_content . "\n\n";
6917 3940 }
@@ -6917,10 +3940,8 @@
6917 3940 }
6918 3941
6919 3942 return trim($content);
6920 3943 }
6921 -
6922 -
6923 3944 private function find_relevant_products_pinecone($user_embedding) {
6924 3945 //error_log('Starting Pinecone product search...');
6925 3946
6926 3947 $options = get_option('mxchat_pinecone_addon_options', array());
@@ -6995,10 +4016,8 @@
6995 4016 }
6996 4017
6997 4018 return trim($content);
6998 4019 }
6999 -
7000 -
7001 4020 private function fetch_content_with_product_links($most_relevant_id) {
7002 4021 global $wpdb;
7003 4022 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
7004 4023
@@ -7018,581 +4037,13 @@
7018 4037 return null;
7019 4038 }
7020 4039
7021 4040 /**
7022 - * Get system instructions for a specific bot or default
7023 - * Checks for multi-bot add-on and uses bot-specific instructions if available
7024 - * Automatically strips URLs if citation links are disabled
7025 - * Replaces {visitor_name} placeholder with actual visitor name if available
7026 - *
7027 - * @param string $bot_id The bot ID to get instructions for
7028 - * @param string $session_id Optional session ID to lookup visitor name
4041 + * Modified streaming functions to include testing data
7029 4042 */
7030 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
7031 - $instructions = '';
7032 4043
7033 - // Check if multi-bot add-on is active
7034 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
7035 - // Get bot-specific options from multi-bot add-on
7036 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
7037 -
7038 - // If bot has custom system instructions, use those
7039 - if (!empty($bot_options['system_prompt_instructions'])) {
7040 - $instructions = $bot_options['system_prompt_instructions'];
7041 - }
7042 - }
7043 -
7044 - // Fall back to default system instructions
7045 - if (empty($instructions)) {
7046 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7047 - }
7048 -
7049 - // Check if citation links are disabled - if so, strip URLs from instructions
7050 - $fresh_options = get_option('mxchat_options', []);
7051 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
7052 -
7053 - if (!$citation_links_enabled && !empty($instructions)) {
7054 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
7055 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
7056 - }
7057 -
7058 - // Replace {visitor_name} placeholder with actual visitor name if available
7059 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
7060 - $name_option_key = "mxchat_name_{$session_id}";
7061 - $visitor_name = get_option($name_option_key, '');
7062 -
7063 - if (!empty($visitor_name)) {
7064 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
7065 - } else {
7066 - // Remove placeholder if no name is available
7067 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
7068 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
7069 - }
7070 - }
7071 -
7072 - // Allow developers to filter system instructions and process shortcodes
7073 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
7074 - $instructions = do_shortcode($instructions);
7075 -
7076 - return $instructions;
7077 -}
7078 -/**
7079 - * Get the current bot ID from session or request context
7080 - */
7081 -private function get_current_bot_id($session_id = '') {
7082 - // First, check if bot_id is passed in the current request
7083 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
7084 - return sanitize_key($_POST['bot_id']);
7085 - }
7086 -
7087 - // If not in POST, try to get it from session data
7088 - if (!empty($session_id)) {
7089 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
7090 - if (!empty($bot_id)) {
7091 - return $bot_id;
7092 - }
7093 - }
7094 -
7095 - // Fall back to default
7096 - return 'default';
7097 -}
7098 -/* ====================================================================== *
7099 - * Native function-calling loop (plan-mxchat-20260617-a41dee)
7100 - *
7101 - * Model-driven tool use. The model is offered MxChat's enabled callbacks as
7102 - * tools (sourced from MxChat_Tool_Registry, the single source the admin AI
7103 - * Tools checklist also reads). When the model calls a tool, the matching
7104 - * callback runs through its EXISTING permission checks, its output is fed
7105 - * back, and the loop continues up to a depth cap. INDEPENDENT of the
7106 - * intent→callback router — it runs only after intents miss, and works with
7107 - * ZERO Actions created.
7108 - *
7109 - * Entered ONLY when: function calling is enabled + the active model is
7110 - * tool-capable + at least one tool is enabled. Default-off, so existing
7111 - * installs never enter this branch (byte-for-byte unchanged behavior). The
7112 - * tool round is buffered (non-streaming) per the plan; the final answer is
7113 - * emitted via the same SSE/JSON envelopes the normal path uses.
7114 - * ====================================================================== */
7115 -
7116 -/** Gate: should the function-calling loop handle this turn? */
7117 -private function mxchat_fc_should_run($selected_model) {
7118 - if (!class_exists('MxChat_Tool_Registry') || !MxChat_Tool_Registry::is_enabled()) {
7119 - return false;
7120 - }
7121 - if (class_exists('MxChat_Model_Catalog') && !MxChat_Model_Catalog::supports_tools($selected_model)) {
7122 - return false;
7123 - }
7124 - $tools = MxChat_Tool_Registry::enabled_tools();
7125 - return !empty($tools);
7126 -}
7127 -
7128 -private function mxchat_fc_log($msg) {
7129 - if (defined('MXCHAT_DEV_MODE') && MXCHAT_DEV_MODE) {
7130 - error_log('[MxChat FC] ' . $msg);
7131 - }
7132 -}
7133 -
7134 -/**
7135 - * Resolve provider transport details. Returns null when FC can't run for this
7136 - * model/config (missing key, unsupported provider) so the caller falls back to
7137 - * the normal path. OpenAI/xAI/DeepSeek/OpenRouter/Custom share the
7138 - * OpenAI-compatible 'openai' family; Claude and Gemini are distinct.
7139 - */
7140 -private function mxchat_fc_resolve_provider($selected_model, $opts) {
7141 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
7142 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
7143 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
7144 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
7145 - if ($selected_model === 'openrouter') {
7146 - $model = isset($opts['openrouter_selected_model']) ? $opts['openrouter_selected_model'] : '';
7147 - $key = isset($opts['openrouter_api_key']) ? $opts['openrouter_api_key'] : '';
7148 - if ($model === '' || $key === '') return null;
7149 - return array('family'=>'openai','model'=>$model,'url'=>'https://openrouter.ai/api/v1/chat/completions',
7150 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7151 - }
7152 - $prefix = strtolower(explode('-', $selected_model)[0]);
7153 - switch ($prefix) {
7154 - case 'gpt': case 'o1': case 'o3': case 'o4':
7155 - $key = isset($opts['api_key']) ? $opts['api_key'] : '';
7156 - if ($key === '') return null;
7157 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.openai.com/v1/chat/completions',
7158 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7159 - case 'claude':
7160 - $key = isset($opts['claude_api_key']) ? $opts['claude_api_key'] : '';
7161 - if ($key === '') return null;
7162 - return array('family'=>'anthropic','model'=>$selected_model,'url'=>'https://api.anthropic.com/v1/messages',
7163 - 'headers'=>array('Content-Type'=>'application/json','x-api-key'=>$key,'anthropic-version'=>'2023-06-01'),'tag'=>'anthropic');
7164 - case 'gemini':
7165 - $key = isset($opts['gemini_api_key']) ? $opts['gemini_api_key'] : '';
7166 - if ($key === '') return null;
7167 - return array('family'=>'gemini','model'=>$selected_model,'key'=>$key,'tag'=>'gemini');
7168 - case 'grok': case 'xai':
7169 - $key = isset($opts['xai_api_key']) ? $opts['xai_api_key'] : '';
7170 - if ($key === '') return null;
7171 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.x.ai/v1/chat/completions',
7172 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'xai');
7173 - case 'deepseek':
7174 - $key = isset($opts['deepseek_api_key']) ? $opts['deepseek_api_key'] : '';
7175 - if ($key === '') return null;
7176 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.deepseek.com/v1/chat/completions',
7177 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7178 - case 'custom':
7179 - $base = isset($opts['custom_provider_base_url']) ? rtrim($opts['custom_provider_base_url'], '/') : '';
7180 - $key = isset($opts['custom_provider_api_key']) ? $opts['custom_provider_api_key'] : '';
7181 - $model = isset($opts['custom_provider_model']) ? $opts['custom_provider_model'] : '';
7182 - if ($base === '' || $model === '') return null;
7183 - $url = (strpos($base, 'chat/completions') !== false) ? $base : $base . '/chat/completions';
7184 - $headers = array('Content-Type'=>'application/json');
7185 - if ($key !== '') $headers['Authorization'] = 'Bearer '.$key;
7186 - return array('family'=>'openai','model'=>$model,'url'=>$url,'headers'=>$headers,'tag'=>'openai');
7187 - }
7188 - return null;
7189 -}
7190 -
7191 -/**
7192 - * Top-level function-calling attempt. Returns:
7193 - * ['handled'=>true, 'text'=>'<final answer>'] when the model used ≥1 tool
7194 - * ['handled'=>false] otherwise (caller falls back
7195 - * to the normal streamed path)
7196 - */
7197 -private function mxchat_fc_attempt($message, $relevant_content, $conversation_history, $selected_model, $opts, $session_id, $user_id) {
7198 - $prov = $this->mxchat_fc_resolve_provider($selected_model, $opts);
7199 - if (!$prov) {
7200 - return array('handled' => false);
7201 - }
7202 - $tools = MxChat_Tool_Registry::enabled_tools();
7203 - if (empty($tools)) {
7204 - return array('handled' => false);
7205 - }
7206 -
7207 - $bot_id = $this->get_current_bot_id($session_id);
7208 - $system = $this->get_system_instructions($bot_id, $session_id);
7209 -
7210 - // Force callbacks into return-mode (some echo SSE directly when streaming);
7211 - // we buffer the whole tool round, then emit once. Restored in finally.
7212 - $prev_streaming = $this->is_streaming;
7213 - $this->is_streaming = false;
4044 +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) {
7214 4045 try {
7215 - if ($prov['family'] === 'anthropic') {
7216 - return $this->mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7217 - } elseif ($prov['family'] === 'gemini') {
7218 - return $this->mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7219 - }
7220 - return $this->mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7221 - } catch (\Throwable $e) {
7222 - $this->mxchat_fc_log('attempt threw: ' . $e->getMessage());
7223 - return array('handled' => false);
7224 - } finally {
7225 - $this->is_streaming = $prev_streaming;
7226 - }
7227 -}
7228 -
7229 -/** Normalize MxChat history rows to [{role:user|assistant, content}]. */
7230 -private function mxchat_fc_normalize_history($conversation_history) {
7231 - $out = array();
7232 - if (!is_array($conversation_history)) return $out;
7233 - foreach ($conversation_history as $m) {
7234 - if (!is_array($m) || !isset($m['role']) || !isset($m['content'])) continue;
7235 - $role = $m['role'];
7236 - if ($role === 'bot' || $role === 'agent') $role = 'assistant';
7237 - if (!in_array($role, array('user', 'assistant'), true)) $role = 'user';
7238 - $out[] = array('role' => $role, 'content' => (string) $m['content']);
7239 - }
7240 - return $out;
7241 -}
7242 -
7243 -/** Execute the matched callback for a tool call. Returns ['ok'=>bool,'content'=>string]. */
7244 -private function mxchat_fc_execute_tool($tool_name, $args, $orig_message, $user_id, $session_id) {
7245 - $tool = MxChat_Tool_Registry::tool_by_name($tool_name, true); // enabled-only
7246 - if (!$tool) {
7247 - return array('ok' => false, 'content' => 'This tool is not available or not enabled.');
7248 - }
7249 - $fn = $tool['callback'];
7250 -
7251 - // MxChat callbacks are message-driven: hand them the model's `query`
7252 - // (falling back to the original user message).
7253 - $query = '';
7254 - if (is_array($args) && isset($args['query']) && is_string($args['query'])) {
7255 - $query = $args['query'];
7256 - }
7257 - if ($query === '') $query = $orig_message;
7258 -
7259 - // Synthetic intent row (matches wp_mxchat_intents columns → no undefined-prop warnings).
7260 - $synthetic_intent = (object) array(
7261 - 'id' => 0, 'intent_label' => $tool['label'], 'phrases' => '',
7262 - 'embedding_vector' => '', 'callback_function' => $fn,
7263 - 'similarity_threshold' => 0.0, 'enabled' => 1, 'enabled_bots' => null,
7264 - );
7265 -
7266 - try {
7267 - if (!empty($tool['is_addon'])) {
7268 - $result = apply_filters($fn, false, $query, $user_id, $session_id, $synthetic_intent);
7269 - } elseif (method_exists($this, $fn)) {
7270 - $result = call_user_func(array($this, $fn), $query, $user_id, $session_id, $synthetic_intent, null);
7271 - } else {
7272 - return array('ok' => false, 'content' => 'Tool implementation not found.');
7273 - }
7274 - } catch (\Throwable $e) {
7275 - $this->mxchat_fc_log("tool {$fn} threw: " . $e->getMessage());
7276 - return array('ok' => false, 'content' => 'The tool failed to run.');
7277 - }
7278 -
7279 - // plan-mxchat-20260617-48a57a — surface UI-bearing tool output.
7280 - // If the callback produced a UI element (generated image, product card, image
7281 - // gallery), its html MUST reach the FRONTEND as a real rendered bot message —
7282 - // NOT be stripped to text and handed to the model to paraphrase (that was the
7283 - // bug: under function calling, UI-bearing actions rendered nothing). Capture
7284 - // the html here; the FC outcome handler emits it in the response envelope.
7285 - $ui = $this->mxchat_fc_ui_payload_from($result);
7286 - if ($ui['html'] !== '' || !empty($ui['images'])) {
7287 - if ($ui['html'] !== '') {
7288 - $this->fc_ui_html .= ($this->fc_ui_html !== '' ? "\n" : '') . $ui['html'];
7289 - }
7290 - if (!empty($ui['images']) && is_array($ui['images'])) {
7291 - $this->fc_ui_images = array_merge($this->fc_ui_images, $ui['images']);
7292 - }
7293 - $this->fc_ui_captured = true;
7294 -
7295 - // Persist the html to the transcript ONLY if the callback did not already
7296 - // do so itself. Core image/search callbacks self-save (text + html);
7297 - // add-on callbacks (e.g. woo product cards) return html for the caller to
7298 - // save. ui_self_saves carries this from the registry; default by source
7299 - // (core self-saves, add-on does not) when a tool predates the flag.
7300 - $self_saves = array_key_exists('ui_self_saves', $tool)
7301 - ? !empty($tool['ui_self_saves'])
7302 - : empty($tool['is_addon']);
7303 - if ($ui['html'] !== '' && !$self_saves) {
7304 - $this->mxchat_save_chat_message($session_id, 'bot', $ui['html']);
7305 - }
7306 -
7307 - // Hand the MODEL a short acknowledgment (never the raw or stripped html)
7308 - // so the loop can add a one-line caption without trying to re-describe a
7309 - // visual it cannot see and without duplicating the displayed element.
7310 - $summary = isset($ui['text']) ? trim((string) $ui['text']) : '';
7311 - $ack = __('[A visual result has already been shown to the user in the chat. Do not repeat or describe it in detail — reply with at most a brief one-line caption.]', 'mxchat');
7312 - $content = $summary !== '' ? ($ack . ' ' . $summary) : $ack;
7313 - $this->mxchat_fc_log("executed {$fn} → [ui payload surfaced] " . substr($content, 0, 120));
7314 - return array('ok' => true, 'content' => $content);
7315 - }
7316 -
7317 - $content = $this->mxchat_fc_stringify_result($result);
7318 - $this->mxchat_fc_log("executed {$fn} → " . substr($content, 0, 160));
7319 - return array('ok' => true, 'content' => $content);
7320 -}
7321 -
7322 -/**
7323 - * Extract a UI payload (html + images + text) from a tool callback's return,
7324 - * falling back to $this->fallbackResponse for callbacks that return true after
7325 - * setting it. plan-mxchat-20260617-48a57a.
7326 - *
7327 - * @return array{html:string,images:array,text:string}
7328 - */
7329 -private function mxchat_fc_ui_payload_from($result) {
7330 - $src = null;
7331 - if (is_array($result)) {
7332 - $src = $result;
7333 - } elseif ($result === true && isset($this->fallbackResponse) && is_array($this->fallbackResponse)) {
7334 - $src = $this->fallbackResponse;
7335 - }
7336 - $html = (is_array($src) && isset($src['html']) && is_string($src['html'])) ? $src['html'] : '';
7337 - $images = (is_array($src) && isset($src['images']) && is_array($src['images'])) ? $src['images'] : array();
7338 - $text = (is_array($src) && isset($src['text'])) ? (string) $src['text'] : '';
7339 - return array('html' => $html, 'images' => $images, 'text' => $text);
7340 -}
7341 -
7342 -/** Coerce a callback's return (string|array|true|false) into a tool-result string. */
7343 -private function mxchat_fc_stringify_result($result) {
7344 - if (is_string($result)) {
7345 - return $result === '' ? 'No result.' : $result;
7346 - }
7347 - if ($result === true) {
7348 - // Callbacks that set fallbackResponse and return true.
7349 - $fb = isset($this->fallbackResponse) ? $this->fallbackResponse : null;
7350 - if (is_array($fb)) {
7351 - if (!empty($fb['text'])) return (string) $fb['text'];
7352 - if (!empty($fb['html'])) return wp_strip_all_tags((string) $fb['html']);
7353 - }
7354 - return 'Done.';
7355 - }
7356 - if ($result === false || $result === null) {
7357 - return 'No result.';
7358 - }
7359 - if (is_array($result)) {
7360 - if (isset($result['text']) && $result['text'] !== '') return (string) $result['text'];
7361 - if (isset($result['html']) && $result['html'] !== '') return wp_strip_all_tags((string) $result['html']);
7362 - $json = wp_json_encode($result);
7363 - return $json !== false ? $json : 'No result.';
7364 - }
7365 - return (string) $result;
7366 -}
7367 -
7368 -/** HTTP code + decoded body for a function-calling request. */
7369 -private function mxchat_fc_post($url, $body, $headers, $tag) {
7370 - $args = array(
7371 - 'body' => wp_json_encode($body),
7372 - 'headers' => $headers,
7373 - 'timeout' => 60,
7374 - 'redirection' => 5,
7375 - 'blocking' => true,
7376 - 'httpversion' => '1.0',
7377 - 'sslverify' => true,
7378 - );
7379 - $response = $this->mxchat_provider_call_with_retry($url, $args, $tag);
7380 - if (is_wp_error($response)) {
7381 - return array('code' => 0, 'data' => null, 'error' => $response->get_error_message());
7382 - }
7383 - $code = (int) wp_remote_retrieve_response_code($response);
7384 - $data = json_decode(wp_remote_retrieve_body($response), true);
7385 - return array('code' => $code, 'data' => $data, 'error' => null);
7386 -}
7387 -
7388 -/* ---------------- OpenAI-compatible loop (OpenAI/xAI/DeepSeek/OpenRouter/Custom) -------------- */
7389 -private function mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7390 - $messages = array();
7391 - $messages[] = array('role' => 'system', 'content' => $system . ' ' . $relevant_content);
7392 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7393 - $messages[] = $m;
7394 - }
7395 -
7396 - $depth = MxChat_Tool_Registry::max_depth();
7397 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7398 - $tool_schema = MxChat_Tool_Registry::to_openai_tools($tools);
7399 - $used_tool = false;
7400 - $calls_made = 0;
7401 -
7402 - for ($step = 0; $step <= $depth; $step++) {
7403 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7404 - $body = array('model' => $prov['model'], 'messages' => $messages, 'temperature' => 1, 'stream' => false);
7405 - if ($offer_tools) {
7406 - $body['tools'] = $tool_schema;
7407 - $body['tool_choice'] = 'auto';
7408 - }
7409 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7410 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7411 - $this->mxchat_fc_log('openai call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7412 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7413 - }
7414 - $msg = isset($r['data']['choices'][0]['message']) ? $r['data']['choices'][0]['message'] : null;
7415 - if (!$msg) {
7416 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7417 - }
7418 - $tool_calls = isset($msg['tool_calls']) && is_array($msg['tool_calls']) ? $msg['tool_calls'] : array();
7419 - if (empty($tool_calls)) {
7420 - $text = isset($msg['content']) ? trim((string) $msg['content']) : '';
7421 - if (!$used_tool) return array('handled' => false); // model never used a tool → normal path
7422 - return array('handled' => true, 'text' => ($text !== '' ? $text : $this->mxchat_fc_giveup_text()));
7423 - }
7424 - // Append the assistant tool-call turn verbatim, then a tool result per call.
7425 - $used_tool = true;
7426 - $messages[] = $msg;
7427 - foreach ($tool_calls as $tc) {
7428 - if ($calls_made >= $budget) break;
7429 - $calls_made++;
7430 - $name = isset($tc['function']['name']) ? $tc['function']['name'] : '';
7431 - $args = array();
7432 - if (isset($tc['function']['arguments'])) {
7433 - $decoded = json_decode($tc['function']['arguments'], true);
7434 - if (is_array($decoded)) $args = $decoded;
7435 - }
7436 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7437 - $messages[] = array(
7438 - 'role' => 'tool',
7439 - 'tool_call_id' => isset($tc['id']) ? $tc['id'] : '',
7440 - 'content' => $exec['content'],
7441 - );
7442 - }
7443 - }
7444 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7445 -}
7446 -
7447 -/* ---------------- Anthropic Claude loop ---------------- */
7448 -private function mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7449 - $messages = $this->mxchat_fc_normalize_history($conversation_history);
7450 - $messages[] = array('role' => 'user', 'content' => $relevant_content);
7451 -
7452 - $depth = MxChat_Tool_Registry::max_depth();
7453 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7454 - $tool_schema = MxChat_Tool_Registry::to_anthropic_tools($tools);
7455 - $omit_temp = $this->mxchat_claude_omits_temperature($prov['model']);
7456 - $used_tool = false;
7457 - $calls_made = 0;
7458 -
7459 - for ($step = 0; $step <= $depth; $step++) {
7460 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7461 - $body = array('model' => $prov['model'], 'max_tokens' => 1024, 'temperature' => 0.8,
7462 - 'messages' => $messages, 'system' => $system);
7463 - if ($omit_temp) unset($body['temperature']);
7464 - if ($offer_tools) {
7465 - $body['tools'] = $tool_schema;
7466 - $body['tool_choice'] = array('type' => 'auto');
7467 - }
7468 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7469 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7470 - $this->mxchat_fc_log('anthropic call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7471 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7472 - }
7473 - $content = isset($r['data']['content']) && is_array($r['data']['content']) ? $r['data']['content'] : array();
7474 - $tool_uses = array();
7475 - $text_out = '';
7476 - foreach ($content as $block) {
7477 - if (!isset($block['type'])) continue;
7478 - if ($block['type'] === 'tool_use') {
7479 - $tool_uses[] = $block;
7480 - } elseif ($block['type'] === 'text' && isset($block['text'])) {
7481 - $text_out .= $block['text'];
7482 - }
7483 - }
7484 - if (empty($tool_uses)) {
7485 - if (!$used_tool) return array('handled' => false);
7486 - $text_out = trim($text_out);
7487 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7488 - }
7489 - // Append the assistant turn (the full content array), then a user turn of tool_result blocks.
7490 - $used_tool = true;
7491 - $messages[] = array('role' => 'assistant', 'content' => $content);
7492 - $results = array();
7493 - foreach ($tool_uses as $tu) {
7494 - if ($calls_made >= $budget) break;
7495 - $calls_made++;
7496 - $name = isset($tu['name']) ? $tu['name'] : '';
7497 - $args = isset($tu['input']) && is_array($tu['input']) ? $tu['input'] : array();
7498 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7499 - $results[] = array(
7500 - 'type' => 'tool_result',
7501 - 'tool_use_id' => isset($tu['id']) ? $tu['id'] : '',
7502 - 'content' => $exec['content'],
7503 - );
7504 - }
7505 - $messages[] = array('role' => 'user', 'content' => $results);
7506 - }
7507 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7508 -}
7509 -
7510 -/* ---------------- Google Gemini loop ---------------- */
7511 -private function mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7512 - $contents = array();
7513 - $contents[] = array('role' => 'user', 'parts' => array(array('text' => '[System Instructions] ' . $system . ' ' . $relevant_content)));
7514 - $contents[] = array('role' => 'model', 'parts' => array(array('text' => 'I understand and will follow these instructions.')));
7515 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7516 - $contents[] = array('role' => ($m['role'] === 'assistant' ? 'model' : 'user'),
7517 - 'parts' => array(array('text' => $m['content'])));
7518 - }
7519 -
7520 - $depth = MxChat_Tool_Registry::max_depth();
7521 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7522 - $tool_schema = MxChat_Tool_Registry::to_gemini_tools($tools);
7523 - // Function calling (tools + functionDeclarations + toolConfig) is a v1beta feature on the
7524 - // Generative Language REST API. The v1 endpoint silently ignores the tools array, so a
7525 - // non-preview model (e.g. gemini-2.5-pro, gemini-3.5-flash, gemini-3.1-flash-lite) would
7526 - // just answer in text and never emit a tool call. Always use v1beta for the FC loop —
7527 - // confirmed against Google's function-calling docs (their REST example targets
7528 - // v1beta/models/gemini-3.5-flash:generateContent). v1beta is a superset, so every model
7529 - // reachable on v1 is also reachable here.
7530 - $api_version = 'v1beta';
7531 - $url = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $prov['model'] . ':generateContent?key=' . $prov['key'];
7532 - $headers = array('Content-Type' => 'application/json');
7533 - $used_tool = false;
7534 - $calls_made = 0;
7535 -
7536 - for ($step = 0; $step <= $depth; $step++) {
7537 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7538 - $body = array(
7539 - 'contents' => $contents,
7540 - 'generationConfig' => array('temperature' => 0.7, 'topP' => 0.95, 'topK' => 40, 'maxOutputTokens' => 8192),
7541 - );
7542 - if ($offer_tools) {
7543 - $body['tools'] = $tool_schema;
7544 - $body['toolConfig'] = array('functionCallingConfig' => array('mode' => 'AUTO'));
7545 - }
7546 - $r = $this->mxchat_fc_post($url, $body, $headers, 'gemini');
7547 - if ($r['code'] !== 200 || !is_array($r['data']) || isset($r['data']['error'])) {
7548 - $this->mxchat_fc_log('gemini call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7549 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7550 - }
7551 - $parts = isset($r['data']['candidates'][0]['content']['parts']) && is_array($r['data']['candidates'][0]['content']['parts'])
7552 - ? $r['data']['candidates'][0]['content']['parts'] : array();
7553 - $fn_calls = array();
7554 - $text_out = '';
7555 - foreach ($parts as $p) {
7556 - if (isset($p['functionCall'])) {
7557 - $fn_calls[] = $p['functionCall'];
7558 - } elseif (isset($p['text'])) {
7559 - $text_out .= $p['text'];
7560 - }
7561 - }
7562 - if (empty($fn_calls)) {
7563 - if (!$used_tool) return array('handled' => false);
7564 - $text_out = trim($text_out);
7565 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7566 - }
7567 - // Append the model turn (its parts) then a user turn of functionResponse parts.
7568 - $used_tool = true;
7569 - $contents[] = array('role' => 'model', 'parts' => $parts);
7570 - $resp_parts = array();
7571 - foreach ($fn_calls as $fcall) {
7572 - if ($calls_made >= $budget) break;
7573 - $calls_made++;
7574 - $name = isset($fcall['name']) ? $fcall['name'] : '';
7575 - $args = isset($fcall['args']) && is_array($fcall['args']) ? $fcall['args'] : array();
7576 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7577 - $fr = array('name' => $name, 'response' => array('result' => $exec['content']));
7578 - // Gemini 3 function calls carry a unique id; echo the matching id back in the
7579 - // functionResponse so the model maps the result to the right call (Google REST
7580 - // guidance). Older models omit the id — then we send none, exactly as before.
7581 - if (isset($fcall['id']) && $fcall['id'] !== '') { $fr['id'] = $fcall['id']; }
7582 - $resp_parts[] = array('functionResponse' => $fr);
7583 - }
7584 - $contents[] = array('role' => 'user', 'parts' => $resp_parts);
7585 - }
7586 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7587 -}
7588 -
7589 -private function mxchat_fc_giveup_text() {
7590 - return esc_html__('I looked into that but could not put together a final answer. Please try rephrasing your request.', 'mxchat');
7591 -}
7592 -
7593 -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') {
7594 - try {
7595 4046 if (!$relevant_content) {
7596 4047 $error_response = [
7597 4048 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7598 4049 'error_code' => 'no_relevant_content'
@@ -7597,75 +4048,25 @@
7597 4048 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7598 4049 'error_code' => 'no_relevant_content'
7599 4050 ];
7600 4051
4052 + // Add testing data to error response if available
7601 4053 if ($testing_data !== null) {
7602 4054 $error_response['testing_data'] = $testing_data;
4055 + //error_log("MxChat Testing: Added testing data to no_relevant_content error");
7603 4056 }
7604 4057
7605 4058 return $error_response;
7606 4059 }
7607 4060
4061 + // Ensure conversation_history is an array
7608 4062 if (!is_array($conversation_history)) {
7609 4063 $conversation_history = array();
7610 4064 }
7611 4065
7612 - // Check if this is an OpenRouter model
7613 - if ($selected_model === 'openrouter') {
7614 - // Get the actual OpenRouter model from options
7615 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7616 -
7617 - if (empty($openrouter_selected_model)) {
7618 - $error_response = [
7619 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7620 - 'error_code' => 'no_openrouter_model_selected'
7621 - ];
7622 - if ($testing_data !== null) {
7623 - $error_response['testing_data'] = $testing_data;
7624 - }
7625 - return $error_response;
7626 - }
7627 -
7628 - if (empty($openrouter_api_key)) {
7629 - $error_response = [
7630 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7631 - 'error_code' => 'missing_openrouter_api_key'
7632 - ];
7633 - if ($testing_data !== null) {
7634 - $error_response['testing_data'] = $testing_data;
7635 - }
7636 - return $error_response;
7637 - }
7638 -
7639 - if ($streaming) {
7640 - return $this->mxchat_generate_response_openrouter_stream(
7641 - $openrouter_selected_model,
7642 - $openrouter_api_key,
7643 - $conversation_history,
7644 - $relevant_content,
7645 - $session_id,
7646 - $testing_data
7647 - );
7648 - } else {
7649 - $response = $this->mxchat_generate_response_openrouter(
7650 - $openrouter_selected_model,
7651 - $openrouter_api_key,
7652 - $conversation_history,
7653 - $relevant_content,
7654 - $session_id
7655 - );
7656 - }
7657 -
7658 - if (is_array($response) && isset($response['error'])) {
7659 - if ($testing_data !== null) {
7660 - $response['testing_data'] = $testing_data;
7661 - }
7662 - return $response;
7663 - }
7664 -
7665 - return $response;
7666 - }
7667 -
4066 + // Get selected model with default fallback
4067 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
4068 +
7668 4069 // Extract model prefix to determine the provider
7669 4070 $model_parts = explode('-', $selected_model);
7670 4071 $provider = strtolower($model_parts[0]);
7671 4072
@@ -7685,10 +4086,9 @@
7685 4086 $response = $this->mxchat_generate_response_gemini(
7686 4087 $selected_model,
7687 4088 $gemini_api_key,
7688 4089 $conversation_history,
7689 - $relevant_content,
7690 - $session_id
4090 + $relevant_content
7691 4091 );
7692 4092 break;
7693 4093
7694 4094 case 'claude':
@@ -7708,9 +4108,9 @@
7708 4108 $claude_api_key,
7709 4109 $conversation_history,
7710 4110 $relevant_content,
7711 4111 $session_id,
7712 - $testing_data
4112 + $testing_data // Pass testing data
7713 4113 );
7714 4114 } else {
7715 4115 $response = $this->mxchat_generate_response_claude(
7716 4116 $selected_model,
@@ -7715,10 +4115,9 @@
7715 4115 $response = $this->mxchat_generate_response_claude(
7716 4116 $selected_model,
7717 4117 $claude_api_key,
7718 4118 $conversation_history,
7719 - $relevant_content,
7720 - $session_id
4119 + $relevant_content
7721 4120 );
7722 4121 }
7723 4122 break;
7724 4123
@@ -7739,9 +4138,9 @@
7739 4138 $xai_api_key,
7740 4139 $conversation_history,
7741 4140 $relevant_content,
7742 4141 $session_id,
7743 - $testing_data
4142 + $testing_data // Pass testing data
7744 4143 );
7745 4144 } else {
7746 4145 $response = $this->mxchat_generate_response_xai(
7747 4146 $selected_model,
@@ -7746,10 +4145,9 @@
7746 4145 $response = $this->mxchat_generate_response_xai(
7747 4146 $selected_model,
7748 4147 $xai_api_key,
7749 4148 $conversation_history,
7750 - $relevant_content,
7751 - $session_id
4149 + $relevant_content
7752 4150 );
7753 4151 }
7754 4152 break;
7755 4153
@@ -7770,9 +4168,9 @@
7770 4168 $deepseek_api_key,
7771 4169 $conversation_history,
7772 4170 $relevant_content,
7773 4171 $session_id,
7774 - $testing_data
4172 + $testing_data // Pass testing data
7775 4173 );
7776 4174 } else {
7777 4175 $response = $this->mxchat_generate_response_deepseek(
7778 4176 $selected_model,
@@ -7777,44 +4175,13 @@
7777 4175 $response = $this->mxchat_generate_response_deepseek(
7778 4176 $selected_model,
7779 4177 $deepseek_api_key,
7780 4178 $conversation_history,
7781 - $relevant_content,
7782 - $session_id
4179 + $relevant_content
7783 4180 );
7784 4181 }
7785 4182 break;
7786 4183
7787 - case 'custom':
7788 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7789 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7790 - if (empty($cp_base_url)) {
7791 - $error_response = [
7792 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7793 - 'error_code' => 'missing_custom_provider_base_url'
7794 - ];
7795 - if ($testing_data !== null) {
7796 - $error_response['testing_data'] = $testing_data;
7797 - }
7798 - return $error_response;
7799 - }
7800 - if ($streaming) {
7801 - return $this->mxchat_generate_response_custom_stream(
7802 - $selected_model,
7803 - $conversation_history,
7804 - $relevant_content,
7805 - $session_id,
7806 - $testing_data
7807 - );
7808 - } else {
7809 - $response = $this->mxchat_generate_response_custom(
7810 - $selected_model,
7811 - $conversation_history,
7812 - $relevant_content
7813 - );
7814 - }
7815 - break;
7816 -
7817 4184 case 'gpt':
7818 4185 case 'o1':
7819 4186 if (empty($api_key)) {
7820 4187 $error_response = [
@@ -7825,27 +4192,9 @@
7825 4192 $error_response['testing_data'] = $testing_data;
7826 4193 }
7827 4194 return $error_response;
7828 4195 }
7829 -
7830 - // Check if web search is enabled for this OpenAI model
7831 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7832 - // Models that don't support web search
7833 - $unsupported_web_search_models = array('gpt-4.1-nano');
7834 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7835 -
7836 - if ($web_search_enabled && $model_supports_web_search) {
7837 - // Use Responses API (required for some models, or when web search is enabled)
7838 - return $this->mxchat_generate_response_openai_web_search(
7839 - $selected_model,
7840 - $api_key,
7841 - $conversation_history,
7842 - $relevant_content,
7843 - $session_id,
7844 - $testing_data,
7845 - $streaming
7846 - );
7847 - } elseif ($streaming) {
4196 + if ($streaming) {
7848 4197 return $this->mxchat_generate_response_openai_stream(
7849 4198 $selected_model,
7850 4199 $api_key,
7851 4200 $conversation_history,
@@ -7850,9 +4199,9 @@
7850 4199 $api_key,
7851 4200 $conversation_history,
7852 4201 $relevant_content,
7853 4202 $session_id,
7854 - $testing_data
4203 + $testing_data // Pass testing data
7855 4204 );
7856 4205 } else {
7857 4206 $response = $this->mxchat_generate_response_openai(
7858 4207 $selected_model,
@@ -7857,15 +4206,15 @@
7857 4206 $response = $this->mxchat_generate_response_openai(
7858 4207 $selected_model,
7859 4208 $api_key,
7860 4209 $conversation_history,
7861 - $relevant_content,
7862 - $session_id
4210 + $relevant_content
7863 4211 );
7864 4212 }
7865 4213 break;
7866 4214
7867 4215 default:
4216 + // Default to OpenAI for custom models or unrecognized prefixes
7868 4217 if (empty($api_key)) {
7869 4218 $error_response = [
7870 4219 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7871 4220 'error_code' => 'missing_openai_api_key'
@@ -7874,25 +4223,9 @@
7874 4223 $error_response['testing_data'] = $testing_data;
7875 4224 }
7876 4225 return $error_response;
7877 4226 }
7878 -
7879 - // Check if web search is enabled (default case also handles OpenAI models)
7880 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7881 - $unsupported_web_search_models = array('gpt-4.1-nano');
7882 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7883 -
7884 - if ($web_search_enabled && $model_supports_web_search) {
7885 - return $this->mxchat_generate_response_openai_web_search(
7886 - $selected_model,
7887 - $api_key,
7888 - $conversation_history,
7889 - $relevant_content,
7890 - $session_id,
7891 - $testing_data,
7892 - $streaming
7893 - );
7894 - } elseif ($streaming) {
4227 + if ($streaming) {
7895 4228 return $this->mxchat_generate_response_openai_stream(
7896 4229 $selected_model,
7897 4230 $api_key,
7898 4231 $conversation_history,
@@ -7897,9 +4230,9 @@
7897 4230 $api_key,
7898 4231 $conversation_history,
7899 4232 $relevant_content,
7900 4233 $session_id,
7901 - $testing_data
4234 + $testing_data // Pass testing data
7902 4235 );
7903 4236 } else {
7904 4237 $response = $this->mxchat_generate_response_openai(
7905 4238 $selected_model,
@@ -7904,25 +4237,30 @@
7904 4237 $response = $this->mxchat_generate_response_openai(
7905 4238 $selected_model,
7906 4239 $api_key,
7907 4240 $conversation_history,
7908 - $relevant_content,
7909 - $session_id
4241 + $relevant_content
7910 4242 );
7911 4243 }
7912 4244 break;
7913 4245 }
7914 4246
4247 + // Check if the response is an error array from the provider-specific function
7915 4248 if (is_array($response) && isset($response['error'])) {
4249 + // Add testing data to error response if available
7916 4250 if ($testing_data !== null) {
7917 4251 $response['testing_data'] = $testing_data;
4252 + //error_log("MxChat Testing: Added testing data to provider error response");
7918 4253 }
7919 - return $response;
4254 + return $response; // Pass through the error with testing data
7920 4255 }
7921 4256
4257 + // For successful non-streaming responses, we don't add testing data here
4258 + // because it will be added in the main handler
7922 4259 return $response;
7923 4260
7924 4261 } catch (Exception $e) {
4262 + //error_log('MXChat Error: ' . $e->getMessage());
7925 4263 $error_response = [
7926 4264 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7927 4265 'error_code' => 'system_exception',
7928 4266 'exception_details' => $e->getMessage()
@@ -7927,24 +4265,29 @@
7927 4265 'error_code' => 'system_exception',
7928 4266 'exception_details' => $e->getMessage()
7929 4267 ];
7930 4268
4269 + // Add testing data to exception response if available
7931 4270 if ($testing_data !== null) {
7932 4271 $error_response['testing_data'] = $testing_data;
4272 + //error_log("MxChat Testing: Added testing data to exception response");
7933 4273 }
7934 4274
7935 4275 return $error_response;
7936 4276 }
7937 4277 }
7938 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4278 +
4279 +private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7939 4280 try {
7940 - $bot_id = $this->get_current_bot_id($session_id);
7941 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4281 + // Get system prompt instructions from options
4282 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7942 4283
4284 + // Ensure conversation_history is an array
7943 4285 if (!is_array($conversation_history)) {
7944 4286 $conversation_history = array();
7945 4287 }
7946 4288
4289 + // Format conversation history for OpenAI
7947 4290 $formatted_conversation = array();
7948 4291
7949 4292 $formatted_conversation[] = array(
7950 4293 'role' => 'system',
@@ -7956,9 +4299,9 @@
7956 4299 $role = $message['role'];
7957 4300 if ($role === 'bot' || $role === 'agent') {
7958 4301 $role = 'assistant';
7959 4302 }
7960 - if (!in_array($role, ['system', 'assistant', 'user'])) {
4303 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7961 4304 $role = 'user';
7962 4305 }
7963 4306 $formatted_conversation[] = array(
7964 4307 'role' => $role,
@@ -7966,22 +4309,19 @@
7966 4309 );
7967 4310 }
7968 4311 }
7969 4312
4313 + // Check if we can actually stream
7970 4314 if (headers_sent() || !function_exists('curl_init')) {
7971 - $regular_response = $this->mxchat_generate_response_openrouter(
4315 + // Fallback to regular response with testing data
4316 + //error_log("MxChat: OpenAI streaming not possible, falling back to regular response");
4317 + $regular_response = $this->mxchat_generate_response_openai(
7972 4318 $selected_model,
7973 - $openrouter_api_key,
4319 + $api_key,
7974 4320 $conversation_history,
7975 - $relevant_content,
7976 - $session_id
4321 + $relevant_content
7977 4322 );
7978 4323
7979 - // Save bot response to transcript
7980 - if (!empty($regular_response) && !empty($session_id)) {
7981 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7982 - }
7983 -
7984 4324 $response_data = [
7985 4325 'text' => $regular_response,
7986 4326 'html' => '',
7987 4327 'session_id' => $session_id
@@ -7988,8 +4328,9 @@
7988 4328 ];
7989 4329
7990 4330 if ($testing_data !== null) {
7991 4331 $response_data['testing_data'] = $testing_data;
4332 + //error_log("MxChat Testing: Added testing data to OpenAI fallback response");
7992 4333 }
7993 4334
7994 4335 header('Content-Type: application/json');
7995 4336 echo json_encode($response_data);
@@ -7995,8 +4336,9 @@
7995 4336 echo json_encode($response_data);
7996 4337 return true;
7997 4338 }
7998 4339
4340 + // Prepare the request body with stream: true
7999 4341 $body = json_encode([
8000 4342 'model' => $selected_model,
8001 4343 'messages' => $formatted_conversation,
8002 4344 'temperature' => 1,
@@ -8002,226 +4344,78 @@
8002 4344 'temperature' => 1,
8003 4345 'stream' => true
8004 4346 ]);
8005 4347
8006 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
8007 - // inside WRITEFUNCTION on first byte of a successful upstream.
8008 -
8009 - $captured_status_code = 0;
8010 - $captured_body_pre_stream = '';
8011 - $full_response = '';
4348 + // Use cURL for streaming support
4349 + $ch = curl_init();
4350 + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
4351 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4352 + curl_setopt($ch, CURLOPT_POST, true);
4353 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4354 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4355 + 'Content-Type: application/json',
4356 + 'Authorization: Bearer ' . $api_key
4357 + ));
4358 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4359 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4360 +
4361 + $full_response = ''; // Accumulate full response for saving
8012 4362 $stream_started = false;
8013 - $buffer = '';
8014 - $errno = 0;
8015 - $last_curl_error = '';
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);
4363 +
4364 + // Buffer control for real-time streaming
4365 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4366 + // Send testing data as the first event if available
4367 + if (!$stream_started && $testing_data !== null) {
4368 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4369 + flush();
4370 + $stream_started = true;
4371 + //error_log("MxChat Testing: Sent testing data in OpenAI stream");
8023 4372 }
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, 'https://openrouter.ai/api/v1/chat/completions');
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, array(
8037 - 'Content-Type: application/json',
8038 - 'Authorization: Bearer ' . $openrouter_api_key,
8039 - 'HTTP-Referer: ' . home_url(),
8040 - 'X-Title: ' . get_bloginfo('name')
8041 - ));
8042 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8043 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8044 -
8045 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8046 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8047 - $captured_status_code = (int) $m[1];
4373 +
4374 + // Process each chunk of data
4375 + $lines = explode("\n", $data);
4376 +
4377 + foreach ($lines as $line) {
4378 + if (trim($line) === '' || strpos($line, 'data: ') !== 0) {
4379 + continue;
8048 4380 }
8049 - return strlen($header);
8050 - });
8051 -
8052 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8053 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8054 - $captured_body_pre_stream .= $data;
8055 - return strlen($data);
4381 +
4382 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4383 +
4384 + if ($json_str === '[DONE]') {
4385 + echo "data: [DONE]\n\n";
4386 + flush();
4387 + continue;
8056 4388 }
8057 -
8058 - if (!$this->streaming_headers_sent) {
8059 - $this->setup_streaming_headers();
8060 - }
8061 -
8062 - if (!$stream_started && $testing_data !== null) {
8063 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4389 +
4390 + $json = json_decode($json_str, true);
4391 + if (isset($json['choices'][0]['delta']['content'])) {
4392 + $content = $json['choices'][0]['delta']['content'];
4393 + $full_response .= $content; // Accumulate
4394 + // Send as SSE format
4395 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
8064 4396 flush();
8065 - $stream_started = true;
8066 4397 }
8067 -
8068 - $buffer .= $data;
8069 - $lines = explode("\n", $buffer);
8070 - $buffer = array_pop($lines);
8071 -
8072 - foreach ($lines as $line) {
8073 - if (trim($line) === '') {
8074 - continue;
8075 - }
8076 - if (strpos($line, 'data: ') !== 0) {
8077 - continue;
8078 - }
8079 -
8080 - $json_str = substr($line, 6);
8081 -
8082 - if (trim($json_str) === '[DONE]') {
8083 - echo "data: [DONE]\n\n";
8084 - flush();
8085 - continue;
8086 - }
8087 -
8088 - $json = json_decode(trim($json_str), true);
8089 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8090 - $content = $json['choices'][0]['delta']['content'];
8091 - $full_response .= $content;
8092 -
8093 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8094 - flush();
8095 - }
8096 - }
8097 -
8098 - return strlen($data);
8099 - });
8100 -
8101 - $response = curl_exec($ch);
8102 - $errno = curl_errno($ch);
8103 - $last_curl_error = curl_error($ch);
8104 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8105 - curl_close($ch);
8106 -
8107 - if (!$errno && $http_code === 200) {
8108 - break;
8109 4398 }
8110 -
8111 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8112 - $can_retry = !$this->streaming_headers_sent
8113 - && ($attempt + 1) < $max_attempts
8114 - && $is_transient;
8115 -
8116 - if (defined('WP_DEBUG') && WP_DEBUG) {
8117 - error_log(sprintf(
8118 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8119 - $attempt + 1, $max_attempts, $http_code, $errno,
8120 - $is_transient ? 'yes' : 'no',
8121 - $can_retry ? 'Retrying.' : 'Giving up.'
8122 - ));
8123 - }
8124 -
8125 - if (!$can_retry) {
8126 - break;
8127 - }
8128 - }
8129 -
8130 - if (!$errno && $http_code === 200) {
8131 - if (!empty($full_response) && !empty($session_id)) {
8132 - $rag_context_for_storage = null;
8133 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8134 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8135 -
8136 - if ($has_rag_data || $has_action_data) {
8137 - $rag_context_for_storage = [];
8138 -
8139 - if ($has_rag_data) {
8140 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8141 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8142 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8143 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8144 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8145 - }
8146 -
8147 - if ($has_action_data) {
8148 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8149 - }
8150 - }
8151 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8152 - }
8153 - return true;
8154 - }
8155 -
8156 - return $this->mxchat_stream_emit_fallback(
8157 - 'openai',
8158 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8159 - $session_id,
8160 - $testing_data
8161 - );
8162 -
8163 - } catch (Exception $e) {
8164 - return $this->mxchat_stream_emit_fallback(
8165 - 'openai',
8166 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8167 - $session_id,
8168 - $testing_data
8169 - );
8170 - }
8171 -}
8172 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8173 - try {
8174 - $bot_id = $this->get_current_bot_id($session_id);
4399 +
4400 + return strlen($data);
4401 + });
8175 4402
8176 - // Get system prompt instructions using centralized function
8177 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
4403 + $response = curl_exec($ch);
4404 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8178 4405
8179 - // Ensure conversation_history is an array
8180 - if (!is_array($conversation_history)) {
8181 - $conversation_history = array();
8182 - }
8183 -
8184 - // Format conversation history for OpenAI
8185 - $formatted_conversation = array();
8186 -
8187 - $formatted_conversation[] = array(
8188 - 'role' => 'system',
8189 - 'content' => $system_prompt_instructions . " " . $relevant_content
8190 - );
8191 -
8192 - foreach ($conversation_history as $message) {
8193 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8194 - $role = $message['role'];
8195 - if ($role === 'bot' || $role === 'agent') {
8196 - $role = 'assistant';
8197 - }
8198 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8199 - $role = 'user';
8200 - }
8201 - $formatted_conversation[] = array(
8202 - 'role' => $role,
8203 - 'content' => $message['content']
8204 - );
8205 - }
8206 - }
8207 -
8208 - // Check if we can actually stream
8209 - if (headers_sent() || !function_exists('curl_init')) {
8210 - // Fallback to regular response with testing data
4406 + if (curl_errno($ch) || $http_code !== 200) {
4407 + curl_close($ch);
4408 +
4409 + // Fallback to regular response
4410 + //error_log("MxChat: OpenAI streaming failed, falling back");
8211 4411 $regular_response = $this->mxchat_generate_response_openai(
8212 4412 $selected_model,
8213 4413 $api_key,
8214 4414 $conversation_history,
8215 - $relevant_content,
8216 - $session_id
4415 + $relevant_content
8217 4416 );
8218 4417
8219 - // Save bot response to transcript
8220 - if (!empty($regular_response) && !empty($session_id)) {
8221 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8222 - }
8223 -
8224 4418 $response_data = [
8225 4419 'text' => $regular_response,
8226 4420 'html' => '',
8227 4421 'session_id' => $session_id
@@ -8228,8 +4422,9 @@
8228 4422 ];
8229 4423
8230 4424 if ($testing_data !== null) {
8231 4425 $response_data['testing_data'] = $testing_data;
4426 + //error_log("MxChat Testing: Added testing data to OpenAI error fallback");
8232 4427 }
8233 4428
8234 4429 header('Content-Type: application/json');
8235 4430 echo json_encode($response_data);
@@ -8234,916 +4429,50 @@
8234 4429 header('Content-Type: application/json');
8235 4430 echo json_encode($response_data);
8236 4431 return true;
8237 4432 }
8238 -
8239 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
8240 - $is_gpt5_model = (
8241 - strpos($selected_model, 'gpt-5') === 0 ||
8242 - $selected_model === 'gpt-5.2' ||
8243 - $selected_model === 'gpt-5.1-2025-11-13' ||
8244 - $selected_model === 'gpt-5' ||
8245 - $selected_model === 'gpt-5-mini' ||
8246 - $selected_model === 'gpt-5-nano'
8247 - );
8248 -
8249 - // Build request body with optimal settings for fast streaming
8250 - $request_body = [
8251 - 'model' => $selected_model,
8252 - 'messages' => $formatted_conversation,
8253 - 'temperature' => 1,
8254 - 'stream' => true
8255 - ];
8256 -
8257 - // Add reasoning_effort only for GPT-5 models that support it
8258 - // These chat models don't support reasoning_effort parameter
8259 - $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');
8260 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
8261 - // GPT-5.1 uses 'low' instead of 'minimal'
8262 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8263 - $request_body['reasoning_effort'] = 'low';
8264 - } elseif ($selected_model === 'gpt-5.5') {
8265 - $request_body['reasoning_effort'] = 'none';
8266 - } elseif ($selected_model === 'gpt-5.4') {
8267 - $request_body['reasoning_effort'] = 'none';
8268 - } else {
8269 - $request_body['reasoning_effort'] = 'minimal';
8270 - }
4433 +
4434 + curl_close($ch);
4435 +
4436 + // Save the complete response to maintain chat persistence
4437 + if (!empty($full_response) && !empty($session_id)) {
4438 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8271 4439 }
8272 -
8273 - $body = json_encode($request_body);
8274 -
8275 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
8276 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
8277 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
8278 -
8279 - $captured_status_code = 0;
8280 - $captured_body_pre_stream = '';
8281 - $full_response = '';
8282 - $stream_started = false;
8283 - $buffer = '';
8284 - $errno = 0;
8285 - $last_curl_error = '';
8286 - $http_code = 0;
8287 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8288 - $backoff_ms = array(0, 750, 2000);
8289 -
8290 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8291 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8292 - usleep($backoff_ms[$attempt] * 1000);
8293 - }
8294 -
8295 - // Reset per-attempt capture state.
8296 - $captured_status_code = 0;
8297 - $captured_body_pre_stream = '';
8298 - $full_response = '';
8299 - $stream_started = false;
8300 - $buffer = '';
8301 -
8302 - $ch = curl_init();
8303 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
8304 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8305 - curl_setopt($ch, CURLOPT_POST, true);
8306 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8307 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8308 - 'Content-Type: application/json',
8309 - 'Authorization: Bearer ' . $api_key
8310 - ));
8311 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8312 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8313 -
8314 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
8315 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8316 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8317 - $captured_status_code = (int) $m[1];
8318 - }
8319 - return strlen($header);
8320 - });
8321 -
8322 - // Buffer control for real-time streaming
8323 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8324 - // V2 guard: if upstream returned non-200, buffer body for transient
8325 - // classification and DO NOT emit to client. Stream channel must NOT open.
8326 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8327 - $captured_body_pre_stream .= $data;
8328 - return strlen($data);
8329 - }
8330 -
8331 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
8332 - // After this point streaming_headers_sent === true → retry is structurally blocked.
8333 - if (!$this->streaming_headers_sent) {
8334 - $this->setup_streaming_headers();
8335 - }
8336 -
8337 - // Send testing data as the first event if available
8338 - if (!$stream_started && $testing_data !== null) {
8339 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8340 - flush();
8341 - $stream_started = true;
8342 - }
8343 -
8344 - // CRITICAL FIX: Append new data to buffer
8345 - $buffer .= $data;
8346 -
8347 - // Process complete lines only
8348 - $lines = explode("\n", $buffer);
8349 -
8350 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8351 - $buffer = array_pop($lines);
8352 -
8353 - foreach ($lines as $line) {
8354 - if (trim($line) === '') {
8355 - continue;
8356 - }
8357 - if (strpos($line, 'data: ') !== 0) {
8358 - continue;
8359 - }
8360 -
8361 - $json_str = substr($line, 6);
8362 -
8363 - if (trim($json_str) === '[DONE]') {
8364 - echo "data: [DONE]\n\n";
8365 - flush();
8366 - continue;
8367 - }
8368 -
8369 - $json = json_decode(trim($json_str), true);
8370 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8371 - $content = $json['choices'][0]['delta']['content'];
8372 - $full_response .= $content;
8373 -
8374 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8375 - flush();
8376 - }
8377 - }
8378 -
8379 - return strlen($data);
8380 - });
8381 -
8382 - $response = curl_exec($ch);
8383 - $errno = curl_errno($ch);
8384 - $last_curl_error = curl_error($ch);
8385 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8386 - curl_close($ch);
8387 -
8388 - if (!$errno && $http_code === 200) {
8389 - break; // Happy path — WRITEFUNCTION already streamed everything.
8390 - }
8391 -
8392 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8393 - $can_retry = !$this->streaming_headers_sent
8394 - && ($attempt + 1) < $max_attempts
8395 - && $is_transient;
8396 -
8397 - if (defined('WP_DEBUG') && WP_DEBUG) {
8398 - error_log(sprintf(
8399 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8400 - $attempt + 1, $max_attempts, $http_code, $errno,
8401 - $is_transient ? 'yes' : 'no',
8402 - $can_retry ? 'Retrying.' : 'Giving up.'
8403 - ));
8404 - }
8405 -
8406 - if (!$can_retry) {
8407 - break;
8408 - }
8409 - }
8410 -
8411 - // Post-loop branch.
8412 - if (!$errno && $http_code === 200) {
8413 - // Happy path — save the complete response to maintain chat persistence.
8414 - if (!empty($full_response) && !empty($session_id)) {
8415 - $rag_context_for_storage = null;
8416 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8417 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8418 -
8419 - if ($has_rag_data || $has_action_data) {
8420 - $rag_context_for_storage = [];
8421 -
8422 - if ($has_rag_data) {
8423 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8424 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8425 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8426 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8427 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8428 - }
8429 -
8430 - if ($has_action_data) {
8431 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8432 - }
8433 - }
8434 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8435 - }
8436 -
8437 - return true;
8438 - }
8439 -
8440 - // Failure path — branch on whether SSE channel was opened.
8441 - return $this->mxchat_stream_emit_fallback(
8442 - 'openai',
8443 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8444 - $session_id,
8445 - $testing_data
8446 - );
8447 -
4440 +
4441 + return true; // Indicate streaming completed successfully
4442 +
8448 4443 } catch (Exception $e) {
8449 - return $this->mxchat_stream_emit_fallback(
8450 - 'openai',
8451 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8452 - $session_id,
8453 - $testing_data
4444 + //error_log("MxChat OpenAI streaming exception: " . $e->getMessage());
4445 +
4446 + // Fallback to regular response
4447 + $regular_response = $this->mxchat_generate_response_openai(
4448 + $selected_model,
4449 + $api_key,
4450 + $conversation_history,
4451 + $relevant_content
8454 4452 );
8455 - }
8456 -}
8457 -
8458 -/**
8459 - * Shared fallback emitter for streaming chat functions. Two outcomes:
8460 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
8461 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
8462 - * normal bot bubble. Transcript row is persisted.
8463 - * - streaming_headers_sent === false: SSE channel never opened (retries
8464 - * exhausted on initial connect). Emit a clean JSON response — the path
8465 - * the widget would normally hit if streaming wasn't even attempted.
8466 - *
8467 - * Used by all six *_stream functions after their per-attempt retry loop.
8468 - */
8469 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
8470 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
8471 -
8472 - if ($this->streaming_headers_sent) {
8473 - if ($is_error_array) {
8474 - echo "data: " . json_encode([
8475 - 'error' => true,
8476 - 'error_message' => $regular_response['error'],
8477 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8478 - 'text' => $regular_response['error'],
8479 - 'message' => $regular_response['error']
8480 - ]) . "\n\n";
8481 - echo "data: [DONE]\n\n";
8482 - flush();
8483 - return true;
4453 +
4454 + $response_data = [
4455 + 'text' => $regular_response,
4456 + 'html' => '',
4457 + 'session_id' => $session_id
4458 + ];
4459 +
4460 + if ($testing_data !== null) {
4461 + $response_data['testing_data'] = $testing_data;
4462 + //error_log("MxChat Testing: Added testing data to OpenAI exception fallback");
8484 4463 }
8485 - $fallback_message = (string) $regular_response;
8486 - if (!empty($fallback_message) && !empty($session_id)) {
8487 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8488 - }
8489 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
8490 - echo "data: [DONE]\n\n";
8491 - flush();
8492 - return true;
8493 - }
8494 -
8495 - // SSE channel never opened — clean JSON fallback.
8496 - if ($is_error_array) {
4464 +
8497 4465 header('Content-Type: application/json');
8498 - echo json_encode(array(
8499 - 'error' => true,
8500 - 'error_message' => $regular_response['error'],
8501 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8502 - 'text' => $regular_response['error'],
8503 - 'message' => $regular_response['error'],
8504 - ));
4466 + echo json_encode($response_data);
8505 4467 return true;
8506 4468 }
8507 -
8508 - $fallback_message = (string) $regular_response;
8509 - if (!empty($fallback_message) && !empty($session_id)) {
8510 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8511 - }
8512 - $response_data = array(
8513 - 'text' => $fallback_message,
8514 - 'html' => '',
8515 - 'session_id' => $session_id,
8516 - );
8517 - if ($testing_data !== null) {
8518 - $response_data['testing_data'] = $testing_data;
8519 - }
8520 - header('Content-Type: application/json');
8521 - echo json_encode($response_data);
8522 - return true;
8523 4469 }
8524 -
8525 -/**
8526 - * Resolve custom (OpenAI-compatible) provider config from settings.
8527 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
8528 - */
8529 -private function mxchat_resolve_custom_provider() {
8530 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
8531 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
8532 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
8533 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
8534 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
8535 -
8536 - $chat_url = $base_url . '/chat/completions';
8537 - if (!empty($api_version)) {
8538 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
8539 - }
8540 -
8541 - $headers = array('Content-Type: application/json');
8542 - if (!empty($api_key)) {
8543 - if ($auth_scheme === 'api-key') {
8544 - $headers[] = 'api-key: ' . $api_key;
8545 - } else {
8546 - $headers[] = 'Authorization: Bearer ' . $api_key;
8547 - }
8548 - }
8549 -
8550 - return array(
8551 - 'base_url' => $base_url,
8552 - 'api_key' => $api_key,
8553 - 'model' => $model !== '' ? $model : 'default',
8554 - 'auth_scheme' => $auth_scheme,
8555 - 'api_version' => $api_version,
8556 - 'chat_url' => $chat_url,
8557 - 'headers' => $headers,
8558 - );
8559 -}
8560 -
8561 -/**
8562 - * Streaming chat completion against an OpenAI-compatible custom provider
8563 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
8564 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
8565 - */
8566 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
4470 +private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8567 4471 try {
8568 - $cfg = $this->mxchat_resolve_custom_provider();
8569 - if (empty($cfg['base_url'])) {
8570 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8571 - }
4472 + // Get system prompt instructions from options
4473 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8572 4474
8573 - $bot_id = $this->get_current_bot_id($session_id);
8574 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8575 - if (!is_array($conversation_history)) {
8576 - $conversation_history = array();
8577 - }
8578 -
8579 - $formatted_conversation = array();
8580 - $formatted_conversation[] = array(
8581 - 'role' => 'system',
8582 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8583 - );
8584 - foreach ($conversation_history as $message) {
8585 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8586 - $role = $message['role'];
8587 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8588 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8589 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
8590 - }
8591 - }
8592 -
8593 - if (headers_sent() || !function_exists('curl_init')) {
8594 - // No streaming capability — fall through to non-stream wrapper
8595 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
8596 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
8597 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
8598 - }
8599 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
8600 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
8601 - header('Content-Type: application/json');
8602 - echo json_encode($response_data);
8603 - return true;
8604 - }
8605 -
8606 - $request_body = array(
8607 - 'model' => $cfg['model'],
8608 - 'messages' => $formatted_conversation,
8609 - 'stream' => true,
8610 - );
8611 - $body = json_encode($request_body);
8612 -
8613 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8614 -
8615 - $captured_status_code = 0;
8616 - $captured_body_pre_stream = '';
8617 - $full_response = '';
8618 - $stream_started = false;
8619 - $buffer = '';
8620 - $errno = 0;
8621 - $http_code = 0;
8622 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8623 - $backoff_ms = array(0, 750, 2000);
8624 -
8625 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8626 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8627 - usleep($backoff_ms[$attempt] * 1000);
8628 - }
8629 -
8630 - $captured_status_code = 0;
8631 - $captured_body_pre_stream = '';
8632 - $full_response = '';
8633 - $stream_started = false;
8634 - $buffer = '';
8635 -
8636 - $ch = curl_init();
8637 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8638 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8639 - curl_setopt($ch, CURLOPT_POST, true);
8640 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8641 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8642 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8643 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8644 -
8645 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8646 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8647 - $captured_status_code = (int) $m[1];
8648 - }
8649 - return strlen($header);
8650 - });
8651 -
8652 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8653 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8654 - $captured_body_pre_stream .= $data;
8655 - return strlen($data);
8656 - }
8657 -
8658 - if (!$this->streaming_headers_sent) {
8659 - $this->setup_streaming_headers();
8660 - }
8661 -
8662 - if (!$stream_started && $testing_data !== null) {
8663 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8664 - flush();
8665 - $stream_started = true;
8666 - }
8667 - $buffer .= $data;
8668 - $lines = explode("\n", $buffer);
8669 - $buffer = array_pop($lines);
8670 - foreach ($lines as $line) {
8671 - if (trim($line) === '') { continue; }
8672 - if (strpos($line, 'data: ') !== 0) { continue; }
8673 - $json_str = substr($line, 6);
8674 - if (trim($json_str) === '[DONE]') {
8675 - echo "data: [DONE]\n\n";
8676 - flush();
8677 - continue;
8678 - }
8679 - $json = json_decode(trim($json_str), true);
8680 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8681 - $content = $json['choices'][0]['delta']['content'];
8682 - $full_response .= $content;
8683 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8684 - flush();
8685 - }
8686 - }
8687 - return strlen($data);
8688 - });
8689 -
8690 - $response = curl_exec($ch);
8691 - $errno = curl_errno($ch);
8692 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8693 - curl_close($ch);
8694 -
8695 - if (!$errno && $http_code === 200) {
8696 - break;
8697 - }
8698 -
8699 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8700 - $can_retry = !$this->streaming_headers_sent
8701 - && ($attempt + 1) < $max_attempts
8702 - && $is_transient;
8703 -
8704 - if (defined('WP_DEBUG') && WP_DEBUG) {
8705 - error_log(sprintf(
8706 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8707 - $attempt + 1, $max_attempts, $http_code, $errno,
8708 - $is_transient ? 'yes' : 'no',
8709 - $can_retry ? 'Retrying.' : 'Giving up.'
8710 - ));
8711 - }
8712 -
8713 - if (!$can_retry) {
8714 - break;
8715 - }
8716 - }
8717 -
8718 - if (!$errno && $http_code === 200) {
8719 - if (!empty($full_response) && !empty($session_id)) {
8720 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8721 - }
8722 - return true;
8723 - }
8724 -
8725 - return $this->mxchat_stream_emit_fallback(
8726 - 'openai',
8727 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8728 - $session_id,
8729 - $testing_data
8730 - );
8731 -
8732 - } catch (Exception $e) {
8733 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8734 - }
8735 -}
8736 -
8737 -/**
8738 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8739 - * Returns string content on success, array['error'=>...] on failure.
8740 - */
8741 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8742 - $cfg = $this->mxchat_resolve_custom_provider();
8743 - if (empty($cfg['base_url'])) {
8744 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8745 - }
8746 -
8747 - $bot_id = $this->get_current_bot_id(null);
8748 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8749 - if (!is_array($conversation_history)) {
8750 - $conversation_history = array();
8751 - }
8752 -
8753 - $messages = array(array(
8754 - 'role' => 'system',
8755 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8756 - ));
8757 - foreach ($conversation_history as $message) {
8758 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8759 - $role = $message['role'];
8760 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8761 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8762 - $messages[] = array('role' => $role, 'content' => $message['content']);
8763 - }
8764 - }
8765 -
8766 - $headers_assoc = array('Content-Type' => 'application/json');
8767 - if (!empty($cfg['api_key'])) {
8768 - if ($cfg['auth_scheme'] === 'api-key') {
8769 - $headers_assoc['api-key'] = $cfg['api_key'];
8770 - } else {
8771 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8772 - }
8773 - }
8774 -
8775 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8776 - 'headers' => $headers_assoc,
8777 - 'body' => wp_json_encode(array(
8778 - 'model' => $cfg['model'],
8779 - 'messages' => $messages,
8780 - )),
8781 - 'timeout' => 120,
8782 - ), 'openai');
8783 -
8784 - if (is_wp_error($response)) {
8785 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8786 - }
8787 - $code = (int) wp_remote_retrieve_response_code($response);
8788 - if ($code < 200 || $code >= 300) {
8789 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8790 - }
8791 - $body = json_decode(wp_remote_retrieve_body($response), true);
8792 - if (isset($body['choices'][0]['message']['content'])) {
8793 - return (string) $body['choices'][0]['message']['content'];
8794 - }
8795 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8796 -}
8797 -
8798 -/**
8799 - * Generate response using OpenAI Responses API with web search tool
8800 - * This uses the newer Responses API which supports web search functionality
8801 - */
8802 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8803 - try {
8804 - $bot_id = $this->get_current_bot_id($session_id);
8805 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8806 -
8807 - if (!is_array($conversation_history)) {
8808 - $conversation_history = array();
8809 - }
8810 -
8811 - // Build the input for Responses API
8812 - // The Responses API uses a different format - we need to construct the input properly
8813 - $input_parts = [];
8814 -
8815 - // Add system instructions as context
8816 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8817 -
8818 - // Build conversation as input items for Responses API
8819 - foreach ($conversation_history as $message) {
8820 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8821 - $role = $message['role'];
8822 - if ($role === 'bot' || $role === 'agent') {
8823 - $role = 'assistant';
8824 - }
8825 - if (!in_array($role, ['assistant', 'user'])) {
8826 - $role = 'user';
8827 - }
8828 - $input_parts[] = [
8829 - 'type' => 'message',
8830 - 'role' => $role,
8831 - 'content' => $message['content']
8832 - ];
8833 - }
8834 - }
8835 -
8836 - // Build request body for Responses API
8837 - $request_body = [
8838 - 'model' => $selected_model,
8839 - 'input' => $input_parts,
8840 - 'instructions' => $system_context,
8841 - 'stream' => $streaming
8842 - ];
8843 -
8844 - // Only add web search tool if web search is enabled in settings
8845 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8846 - if ($web_search_enabled) {
8847 - $request_body['tools'] = [
8848 - ['type' => 'web_search']
8849 - ];
8850 - }
8851 -
8852 - // Add reasoning effort for supported models
8853 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8854 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8855 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8856 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8857 - $request_body['reasoning'] = ['effort' => 'low'];
8858 - } elseif ($selected_model === 'gpt-5.5') {
8859 - $request_body['reasoning'] = ['effort' => 'low'];
8860 - } elseif ($selected_model === 'gpt-5.4') {
8861 - $request_body['reasoning'] = ['effort' => 'low'];
8862 - }
8863 - }
8864 -
8865 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8866 -
8867 - if ($streaming) {
8868 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8869 - } else {
8870 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8871 - }
8872 -
8873 - } catch (Exception $e) {
8874 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8875 - return [
8876 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8877 - 'error_code' => 'web_search_exception'
8878 - ];
8879 - }
8880 -}
8881 -
8882 -/**
8883 - * Handle non-streaming web search response
8884 - */
8885 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8886 - $request_body['stream'] = false;
8887 -
8888 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8889 - 'headers' => array(
8890 - 'Authorization' => 'Bearer ' . $api_key,
8891 - 'Content-Type' => 'application/json'
8892 - ),
8893 - 'body' => json_encode($request_body),
8894 - 'timeout' => 90
8895 - ), 'openai');
8896 -
8897 - if (is_wp_error($response)) {
8898 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8899 - return [
8900 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8901 - 'error_code' => 'web_search_connection_error'
8902 - ];
8903 - }
8904 -
8905 - $response_code = wp_remote_retrieve_response_code($response);
8906 - $response_body = wp_remote_retrieve_body($response);
8907 -
8908 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8909 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8910 -
8911 - if ($response_code !== 200) {
8912 - $error_data = json_decode($response_body, true);
8913 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8914 - return [
8915 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8916 - 'error_code' => 'web_search_api_error'
8917 - ];
8918 - }
8919 -
8920 - $result = json_decode($response_body, true);
8921 -
8922 - if (json_last_error() !== JSON_ERROR_NONE) {
8923 - return [
8924 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8925 - 'error_code' => 'web_search_json_error'
8926 - ];
8927 - }
8928 -
8929 - // Extract the response text and citations from Responses API format
8930 - $output_text = '';
8931 - $citations = [];
8932 -
8933 - if (isset($result['output'])) {
8934 - foreach ($result['output'] as $output_item) {
8935 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8936 - foreach ($output_item['content'] as $content_item) {
8937 - if ($content_item['type'] === 'output_text') {
8938 - $output_text .= $content_item['text'];
8939 -
8940 - // Extract citations/annotations
8941 - if (isset($content_item['annotations'])) {
8942 - foreach ($content_item['annotations'] as $annotation) {
8943 - if ($annotation['type'] === 'url_citation') {
8944 - $citations[] = [
8945 - 'url' => $annotation['url'],
8946 - 'title' => $annotation['title'] ?? ''
8947 - ];
8948 - }
8949 - }
8950 - }
8951 - }
8952 - }
8953 - }
8954 - }
8955 - }
8956 -
8957 - // If we have citations, append them to the response
8958 - if (!empty($citations)) {
8959 - $output_text .= "\n\n**Sources:**\n";
8960 - $seen_urls = [];
8961 - foreach ($citations as $citation) {
8962 - if (!in_array($citation['url'], $seen_urls)) {
8963 - $seen_urls[] = $citation['url'];
8964 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8965 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8966 - }
8967 - }
8968 - }
8969 -
8970 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8971 - // which includes rag_context for the "sources" link in transcripts.
8972 -
8973 - return $output_text;
8974 -}
8975 -
8976 -/**
8977 - * Handle streaming web search response using Responses API
8978 - */
8979 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8980 - $request_body['stream'] = true;
8981 -
8982 - // Check if we can stream
8983 - if (headers_sent() || !function_exists('curl_init')) {
8984 - // Fallback to non-streaming
8985 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8986 - }
8987 -
8988 - // Setup streaming headers
8989 - $this->setup_streaming_headers();
8990 -
8991 - $ch = curl_init();
8992 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8993 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8994 - curl_setopt($ch, CURLOPT_POST, true);
8995 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8996 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8997 - 'Content-Type: application/json',
8998 - 'Authorization: Bearer ' . $api_key
8999 - ));
9000 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9001 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
9002 -
9003 - $full_response = '';
9004 - $stream_started = false;
9005 - $buffer = '';
9006 - $citations = [];
9007 -
9008 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
9009 - // Send testing data as first event if available
9010 - if (!$stream_started && $testing_data !== null) {
9011 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9012 - flush();
9013 - $stream_started = true;
9014 - }
9015 -
9016 - $buffer .= $data;
9017 - $lines = explode("\n", $buffer);
9018 - $buffer = array_pop($lines);
9019 -
9020 - foreach ($lines as $line) {
9021 - if (trim($line) === '') continue;
9022 - if (strpos($line, 'data: ') !== 0) continue;
9023 -
9024 - $json_str = substr($line, 6);
9025 -
9026 - if (trim($json_str) === '[DONE]') {
9027 - // Append citations if we have any
9028 - if (!empty($citations)) {
9029 - $citation_text = "\n\n**Sources:**\n";
9030 - $seen_urls = [];
9031 - foreach ($citations as $citation) {
9032 - if (!in_array($citation['url'], $seen_urls)) {
9033 - $seen_urls[] = $citation['url'];
9034 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9035 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9036 - }
9037 - }
9038 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
9039 - $full_response .= $citation_text;
9040 - flush();
9041 - }
9042 - echo "data: [DONE]\n\n";
9043 - flush();
9044 - continue;
9045 - }
9046 -
9047 - $json = json_decode(trim($json_str), true);
9048 - if (!$json) continue;
9049 -
9050 - // Handle Responses API streaming events
9051 - // The format is different from Chat Completions
9052 - if (isset($json['type'])) {
9053 - switch ($json['type']) {
9054 - case 'response.output_text.delta':
9055 - // Text content delta
9056 - if (isset($json['delta'])) {
9057 - $content = $json['delta'];
9058 - $full_response .= $content;
9059 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9060 - flush();
9061 - }
9062 - break;
9063 -
9064 - case 'response.output_item.done':
9065 - // Check for citations in completed items
9066 - if (isset($json['item']['content'])) {
9067 - foreach ($json['item']['content'] as $content_item) {
9068 - if (isset($content_item['annotations'])) {
9069 - foreach ($content_item['annotations'] as $annotation) {
9070 - if ($annotation['type'] === 'url_citation') {
9071 - $citations[] = [
9072 - 'url' => $annotation['url'],
9073 - 'title' => $annotation['title'] ?? ''
9074 - ];
9075 - }
9076 - }
9077 - }
9078 - }
9079 - }
9080 - break;
9081 - }
9082 - }
9083 - }
9084 -
9085 - return strlen($data);
9086 - });
9087 -
9088 - $response = curl_exec($ch);
9089 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9090 -
9091 - if (curl_errno($ch) || $http_code !== 200) {
9092 - $curl_error = curl_error($ch);
9093 - curl_close($ch);
9094 -
9095 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
9096 -
9097 - return $this->mxchat_stream_emit_fallback(
9098 - 'web_search',
9099 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
9100 - $session_id,
9101 - $testing_data
9102 - );
9103 - }
9104 -
9105 - curl_close($ch);
9106 -
9107 - // Save the complete response with RAG context so the "sources" link
9108 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
9109 - if (!empty($full_response) && !empty($session_id)) {
9110 - $rag_context_for_storage = null;
9111 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9112 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9113 -
9114 - if ($has_rag_data || $has_action_data) {
9115 - $rag_context_for_storage = [];
9116 -
9117 - if ($has_rag_data) {
9118 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9119 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9120 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9121 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9122 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9123 - }
9124 -
9125 - if ($has_action_data) {
9126 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9127 - }
9128 - }
9129 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9130 - }
9131 -
9132 - return true;
9133 -}
9134 -
9135 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9136 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
9137 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
9138 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
9139 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
9140 - try {
9141 - // Get bot ID from session or request
9142 - $bot_id = $this->get_current_bot_id($session_id);
9143 -
9144 - // Get system prompt instructions using centralized function
9145 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9146 4475 // Ensure conversation_history is an array
9147 4476 if (!is_array($conversation_history)) {
9148 4477 $conversation_history = array();
9149 4478 }
@@ -9175,9 +4504,9 @@
9175 4504 'content' => $relevant_content
9176 4505 ];
9177 4506
9178 4507 // Prepare the request body with stream: true
9179 - $payload = [
4508 + $body = json_encode([
9180 4509 'model' => $selected_model,
9181 4510 'messages' => $conversation_history,
9182 4511 'max_tokens' => 1000,
9183 4512 'temperature' => 0.8,
@@ -9182,11 +4511,9 @@
9182 4511 'max_tokens' => 1000,
9183 4512 'temperature' => 0.8,
9184 4513 'system' => $system_prompt_instructions,
9185 4514 'stream' => true
9186 - ];
9187 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9188 - $body = json_encode($payload);
4515 + ]);
9189 4516
9190 4517 // Check if we can actually stream (headers not sent, etc.)
9191 4518 if (headers_sent() || !function_exists('curl_init')) {
9192 4519 // Fallback to regular response with testing data
@@ -9194,17 +4521,11 @@
9194 4521 $regular_response = $this->mxchat_generate_response_claude(
9195 4522 $selected_model,
9196 4523 $claude_api_key,
9197 4524 array_slice($conversation_history, 0, -1), // Remove the added content
9198 - $relevant_content,
9199 - $session_id
4525 + $relevant_content
9200 4526 );
9201 4527
9202 - // Save bot response to transcript
9203 - if (!empty($regular_response) && !empty($session_id)) {
9204 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9205 - }
9206 -
9207 4528 // Return as JSON with testing data
9208 4529 $response_data = [
9209 4530 'text' => $regular_response,
9210 4531 'html' => '',
@@ -9223,197 +4544,162 @@
9223 4544 echo json_encode($response_data);
9224 4545 return true; // Indicate we handled the response
9225 4546 }
9226 4547
9227 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
4548 + // Use cURL for streaming support
4549 + $ch = curl_init();
4550 + curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
4551 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4552 + curl_setopt($ch, CURLOPT_POST, true);
4553 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4554 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4555 + 'Content-Type: application/json',
4556 + 'x-api-key: ' . $claude_api_key,
4557 + 'anthropic-version: 2023-06-01'
4558 + ));
4559 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4560 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9228 4561
9229 - $captured_status_code = 0;
9230 - $captured_body_pre_stream = '';
9231 - $full_response = '';
4562 + $full_response = ''; // Accumulate full response for saving
9232 4563 $stream_started = false;
9233 - $buffer = '';
9234 - $errno = 0;
9235 - $http_code = 0;
9236 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9237 - $backoff_ms = array(0, 750, 2000);
9238 4564
9239 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9240 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9241 - usleep($backoff_ms[$attempt] * 1000);
4565 + // Buffer control for real-time streaming
4566 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4567 + // Send testing data as the first event if available
4568 + if (!$stream_started && $testing_data !== null) {
4569 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4570 + flush();
4571 + $stream_started = true;
4572 + //error_log("MxChat Testing: Sent testing data in Claude stream");
9242 4573 }
4574 +
4575 + // Process each chunk of data
4576 + $lines = explode("\n", $data);
9243 4577
9244 - $captured_status_code = 0;
9245 - $captured_body_pre_stream = '';
9246 - $full_response = '';
9247 - $stream_started = false;
9248 - $buffer = '';
9249 -
9250 - $ch = curl_init();
9251 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
9252 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9253 - curl_setopt($ch, CURLOPT_POST, true);
9254 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9255 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9256 - 'Content-Type: application/json',
9257 - 'x-api-key: ' . $claude_api_key,
9258 - 'anthropic-version: 2023-06-01'
9259 - ));
9260 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9261 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9262 -
9263 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9264 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9265 - $captured_status_code = (int) $m[1];
4578 + foreach ($lines as $line) {
4579 + if (trim($line) === '') {
4580 + continue;
9266 4581 }
9267 - return strlen($header);
9268 - });
9269 4582
9270 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9271 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9272 - $captured_body_pre_stream .= $data;
9273 - return strlen($data);
4583 + // Claude uses event: and data: format
4584 + if (strpos($line, 'event: ') === 0) {
4585 + // Store the event type for the next data line
4586 + continue;
9274 4587 }
9275 4588
9276 - if (!$this->streaming_headers_sent) {
9277 - $this->setup_streaming_headers();
9278 - }
4589 + if (strpos($line, 'data: ') === 0) {
4590 + $json_str = substr($line, 6); // Remove 'data: ' prefix
9279 4591
9280 - if (!$stream_started && $testing_data !== null) {
9281 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9282 - flush();
9283 - $stream_started = true;
9284 - }
9285 -
9286 - $buffer .= $data;
9287 - $lines = explode("\n", $buffer);
9288 - $buffer = array_pop($lines);
9289 -
9290 - foreach ($lines as $line) {
9291 - if (trim($line) === '') {
4592 + $json = json_decode($json_str, true);
4593 + if (json_last_error() !== JSON_ERROR_NONE) {
9292 4594 continue;
9293 4595 }
9294 4596
9295 - if (strpos($line, 'event: ') === 0) {
9296 - continue;
9297 - }
4597 + // Handle different event types
4598 + if (isset($json['type'])) {
4599 + switch ($json['type']) {
4600 + case 'content_block_delta':
4601 + if (isset($json['delta']['text'])) {
4602 + $content = $json['delta']['text'];
4603 + $full_response .= $content; // Accumulate
4604 + // Send as SSE format compatible with your frontend
4605 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
4606 + flush();
4607 + }
4608 + break;
9298 4609
9299 - if (strpos($line, 'data: ') === 0) {
9300 - $json_str = substr($line, 6);
4610 + case 'message_stop':
4611 + echo "data: [DONE]\n\n";
4612 + flush();
4613 + break;
9301 4614
9302 - $json = json_decode(trim($json_str), true);
9303 - if (json_last_error() !== JSON_ERROR_NONE) {
9304 - continue;
4615 + case 'error':
4616 + echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
4617 + flush();
4618 + break;
9305 4619 }
9306 -
9307 - if (isset($json['type'])) {
9308 - switch ($json['type']) {
9309 - case 'content_block_delta':
9310 - if (isset($json['delta']['text'])) {
9311 - $content = $json['delta']['text'];
9312 - $full_response .= $content;
9313 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9314 - flush();
9315 - }
9316 - break;
9317 -
9318 - case 'message_stop':
9319 - echo "data: [DONE]\n\n";
9320 - flush();
9321 - break;
9322 -
9323 - case 'error':
9324 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
9325 - flush();
9326 - break;
9327 - }
9328 - }
9329 4620 }
9330 4621 }
9331 -
9332 - return strlen($data);
9333 - });
9334 -
9335 - $response = curl_exec($ch);
9336 - $errno = curl_errno($ch);
9337 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9338 - curl_close($ch);
9339 -
9340 - if (!$errno && $http_code === 200) {
9341 - break;
9342 4622 }
9343 4623
9344 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
9345 - $can_retry = !$this->streaming_headers_sent
9346 - && ($attempt + 1) < $max_attempts
9347 - && $is_transient;
4624 + return strlen($data);
4625 + });
9348 4626
9349 - if (defined('WP_DEBUG') && WP_DEBUG) {
9350 - error_log(sprintf(
9351 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9352 - $attempt + 1, $max_attempts, $http_code, $errno,
9353 - $is_transient ? 'yes' : 'no',
9354 - $can_retry ? 'Retrying.' : 'Giving up.'
9355 - ));
9356 - }
4627 + $response = curl_exec($ch);
4628 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9357 4629
9358 - if (!$can_retry) {
9359 - break;
9360 - }
4630 + if (curl_errno($ch)) {
4631 + curl_close($ch);
4632 + throw new Exception('cURL Error: ' . curl_error($ch));
9361 4633 }
9362 4634
9363 - if ($errno || $http_code !== 200) {
9364 - return $this->mxchat_stream_emit_fallback(
9365 - 'anthropic',
9366 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content, $session_id),
9367 - $session_id,
9368 - $testing_data
4635 + curl_close($ch);
4636 +
4637 + if ($http_code !== 200) {
4638 + // Fallback to regular response
4639 + //error_log("MxChat: Claude streaming failed with HTTP $http_code, falling back");
4640 + $regular_response = $this->mxchat_generate_response_claude(
4641 + $selected_model,
4642 + $claude_api_key,
4643 + array_slice($conversation_history, 0, -1), // Remove the added content
4644 + $relevant_content
9369 4645 );
4646 +
4647 + $response_data = [
4648 + 'text' => $regular_response,
4649 + 'html' => '',
4650 + 'session_id' => $session_id
4651 + ];
4652 +
4653 + if ($testing_data !== null) {
4654 + $response_data['testing_data'] = $testing_data;
4655 + //error_log("MxChat Testing: Added testing data to Claude error fallback");
4656 + }
4657 +
4658 + header('Content-Type: application/json');
4659 + echo json_encode($response_data);
4660 + return true;
9370 4661 }
9371 4662
9372 4663 // Save the complete response to maintain chat persistence
9373 4664 if (!empty($full_response) && !empty($session_id)) {
9374 - // Prepare RAG context for streaming response
9375 - $rag_context_for_storage = null;
9376 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9377 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9378 -
9379 - if ($has_rag_data || $has_action_data) {
9380 - $rag_context_for_storage = [];
9381 -
9382 - if ($has_rag_data) {
9383 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9384 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9385 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9386 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9387 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9388 - }
9389 -
9390 - if ($has_action_data) {
9391 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9392 - }
9393 - }
9394 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
4665 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9395 4666 }
9396 4667
9397 4668 return true; // Indicate streaming completed successfully
9398 4669
9399 4670 } catch (Exception $e) {
9400 - return $this->mxchat_stream_emit_fallback(
9401 - 'anthropic',
9402 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id),
9403 - $session_id,
9404 - $testing_data
4671 + //error_log("MxChat Claude streaming exception: " . $e->getMessage());
4672 +
4673 + // Fallback to regular response on exception
4674 + $regular_response = $this->mxchat_generate_response_claude(
4675 + $selected_model,
4676 + $claude_api_key,
4677 + $conversation_history,
4678 + $relevant_content
9405 4679 );
4680 +
4681 + $response_data = [
4682 + 'text' => $regular_response,
4683 + 'html' => '',
4684 + 'session_id' => $session_id
4685 + ];
4686 +
4687 + if ($testing_data !== null) {
4688 + $response_data['testing_data'] = $testing_data;
4689 + //error_log("MxChat Testing: Added testing data to Claude exception fallback");
4690 + }
4691 +
4692 + header('Content-Type: application/json');
4693 + echo json_encode($response_data);
4694 + return true;
9406 4695 }
9407 4696 }
9408 4697 private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9409 4698 try {
9410 - // Get bot ID from session or request
9411 - $bot_id = $this->get_current_bot_id($session_id);
4699 + // Get system prompt instructions from options
4700 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
9412 4701
9413 - // Get system prompt instructions using centralized function
9414 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9415 -
9416 4702 // Ensure conversation_history is an array
9417 4703 if (!is_array($conversation_history)) {
9418 4704 $conversation_history = array();
9419 4705 }
@@ -9449,17 +4735,11 @@
9449 4735 $regular_response = $this->mxchat_generate_response_xai(
9450 4736 $selected_model,
9451 4737 $xai_api_key,
9452 4738 $conversation_history,
9453 - $relevant_content,
9454 - $session_id
4739 + $relevant_content
9455 4740 );
9456 4741
9457 - // Save bot response to transcript
9458 - if (!empty($regular_response) && !empty($session_id)) {
9459 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9460 - }
9461 -
9462 4742 $response_data = [
9463 4743 'text' => $regular_response,
9464 4744 'html' => '',
9465 4745 'session_id' => $session_id
@@ -9482,179 +4762,135 @@
9482 4762 'temperature' => 0.8,
9483 4763 'stream' => true
9484 4764 ]);
9485 4765
9486 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9487 -
9488 - $captured_status_code = 0;
9489 - $captured_body_pre_stream = '';
9490 - $full_response = '';
4766 + // Use cURL for streaming support
4767 + $ch = curl_init();
4768 + curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
4769 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4770 + curl_setopt($ch, CURLOPT_POST, true);
4771 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4772 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4773 + 'Content-Type: application/json',
4774 + 'Authorization: Bearer ' . $xai_api_key
4775 + ));
4776 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4777 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4778 +
4779 + $full_response = ''; // Accumulate full response for saving
9491 4780 $stream_started = false;
9492 - $buffer = '';
9493 - $errno = 0;
9494 - $http_code = 0;
9495 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9496 - $backoff_ms = array(0, 750, 2000);
9497 -
9498 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9499 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9500 - usleep($backoff_ms[$attempt] * 1000);
4781 +
4782 + // Buffer control for real-time streaming
4783 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4784 + // Send testing data as the first event if available
4785 + if (!$stream_started && $testing_data !== null) {
4786 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4787 + flush();
4788 + $stream_started = true;
4789 + //error_log("MxChat Testing: Sent testing data in X.AI stream");
9501 4790 }
9502 -
9503 - $captured_status_code = 0;
9504 - $captured_body_pre_stream = '';
9505 - $full_response = '';
9506 - $stream_started = false;
9507 - $buffer = '';
9508 -
9509 - $ch = curl_init();
9510 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
9511 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9512 - curl_setopt($ch, CURLOPT_POST, true);
9513 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9514 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9515 - 'Content-Type: application/json',
9516 - 'Authorization: Bearer ' . $xai_api_key
9517 - ));
9518 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9519 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9520 -
9521 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9522 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9523 - $captured_status_code = (int) $m[1];
4791 +
4792 + // Process each chunk of data
4793 + $lines = explode("\n", $data);
4794 +
4795 + foreach ($lines as $line) {
4796 + if (trim($line) === '' || strpos($line, 'data: ') !== 0) {
4797 + continue;
9524 4798 }
9525 - return strlen($header);
9526 - });
9527 -
9528 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9529 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9530 - $captured_body_pre_stream .= $data;
9531 - return strlen($data);
4799 +
4800 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4801 +
4802 + if ($json_str === '[DONE]') {
4803 + echo "data: [DONE]\n\n";
4804 + flush();
4805 + continue;
9532 4806 }
9533 -
9534 - if (!$this->streaming_headers_sent) {
9535 - $this->setup_streaming_headers();
9536 - }
9537 -
9538 - if (!$stream_started && $testing_data !== null) {
9539 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4807 +
4808 + $json = json_decode($json_str, true);
4809 + if (isset($json['choices'][0]['delta']['content'])) {
4810 + $content = $json['choices'][0]['delta']['content'];
4811 + $full_response .= $content; // Accumulate
4812 + // Send as SSE format
4813 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
9540 4814 flush();
9541 - $stream_started = true;
9542 4815 }
9543 -
9544 - $buffer .= $data;
9545 - $lines = explode("\n", $buffer);
9546 - $buffer = array_pop($lines);
9547 -
9548 - foreach ($lines as $line) {
9549 - if (trim($line) === '') {
9550 - continue;
9551 - }
9552 - if (strpos($line, 'data: ') !== 0) {
9553 - continue;
9554 - }
9555 -
9556 - $json_str = substr($line, 6);
9557 -
9558 - if (trim($json_str) === '[DONE]') {
9559 - echo "data: [DONE]\n\n";
9560 - flush();
9561 - continue;
9562 - }
9563 -
9564 - $json = json_decode(trim($json_str), true);
9565 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9566 - $content = $json['choices'][0]['delta']['content'];
9567 - $full_response .= $content;
9568 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9569 - flush();
9570 - }
9571 - }
9572 -
9573 - return strlen($data);
9574 - });
9575 -
9576 - $response = curl_exec($ch);
9577 - $errno = curl_errno($ch);
9578 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
4816 + }
4817 +
4818 + return strlen($data);
4819 + });
4820 +
4821 + $response = curl_exec($ch);
4822 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
4823 +
4824 + if (curl_errno($ch) || $http_code !== 200) {
9579 4825 curl_close($ch);
9580 -
9581 - if (!$errno && $http_code === 200) {
9582 - break;
4826 +
4827 + // Fallback to regular response
4828 + //error_log("MxChat: X.AI streaming failed, falling back");
4829 + $regular_response = $this->mxchat_generate_response_xai(
4830 + $selected_model,
4831 + $xai_api_key,
4832 + $conversation_history,
4833 + $relevant_content
4834 + );
4835 +
4836 + $response_data = [
4837 + 'text' => $regular_response,
4838 + 'html' => '',
4839 + 'session_id' => $session_id
4840 + ];
4841 +
4842 + if ($testing_data !== null) {
4843 + $response_data['testing_data'] = $testing_data;
4844 + //error_log("MxChat Testing: Added testing data to X.AI error fallback");
9583 4845 }
9584 -
9585 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
9586 - $can_retry = !$this->streaming_headers_sent
9587 - && ($attempt + 1) < $max_attempts
9588 - && $is_transient;
9589 -
9590 - if (defined('WP_DEBUG') && WP_DEBUG) {
9591 - error_log(sprintf(
9592 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9593 - $attempt + 1, $max_attempts, $http_code, $errno,
9594 - $is_transient ? 'yes' : 'no',
9595 - $can_retry ? 'Retrying.' : 'Giving up.'
9596 - ));
9597 - }
9598 -
9599 - if (!$can_retry) {
9600 - break;
9601 - }
4846 +
4847 + header('Content-Type: application/json');
4848 + echo json_encode($response_data);
4849 + return true;
9602 4850 }
9603 -
9604 - if ($errno || $http_code !== 200) {
9605 - return $this->mxchat_stream_emit_fallback(
9606 - 'xai',
9607 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id),
9608 - $session_id,
9609 - $testing_data
9610 - );
9611 - }
9612 -
4851 +
4852 + curl_close($ch);
4853 +
9613 4854 // Save the complete response to maintain chat persistence
9614 4855 if (!empty($full_response) && !empty($session_id)) {
9615 - // Prepare RAG context for streaming response
9616 - $rag_context_for_storage = null;
9617 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9618 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9619 -
9620 - if ($has_rag_data || $has_action_data) {
9621 - $rag_context_for_storage = [];
9622 -
9623 - if ($has_rag_data) {
9624 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9625 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9626 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9627 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9628 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9629 - }
9630 -
9631 - if ($has_action_data) {
9632 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9633 - }
9634 - }
9635 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
4856 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9636 4857 }
9637 -
4858 +
9638 4859 return true; // Indicate streaming completed successfully
9639 -
4860 +
9640 4861 } catch (Exception $e) {
9641 - return $this->mxchat_stream_emit_fallback(
9642 - 'xai',
9643 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9644 - $session_id,
9645 - $testing_data
4862 + //error_log("MxChat X.AI streaming exception: " . $e->getMessage());
4863 +
4864 + // Fallback to regular response
4865 + $regular_response = $this->mxchat_generate_response_xai(
4866 + $selected_model,
4867 + $xai_api_key,
4868 + $conversation_history,
4869 + $relevant_content
9646 4870 );
4871 +
4872 + $response_data = [
4873 + 'text' => $regular_response,
4874 + 'html' => '',
4875 + 'session_id' => $session_id
4876 + ];
4877 +
4878 + if ($testing_data !== null) {
4879 + $response_data['testing_data'] = $testing_data;
4880 + //error_log("MxChat Testing: Added testing data to X.AI exception fallback");
4881 + }
4882 +
4883 + header('Content-Type: application/json');
4884 + echo json_encode($response_data);
4885 + return true;
9647 4886 }
9648 4887 }
9649 4888 private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9650 4889 try {
9651 - // Get bot ID from session or request
9652 - $bot_id = $this->get_current_bot_id($session_id);
4890 + // Get system prompt instructions from options
4891 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
9653 4892
9654 - // Get system prompt instructions using centralized function
9655 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9656 -
9657 4893 // Ensure conversation_history is an array
9658 4894 if (!is_array($conversation_history)) {
9659 4895 $conversation_history = array();
9660 4896 }
@@ -9690,17 +4926,11 @@
9690 4926 $regular_response = $this->mxchat_generate_response_deepseek(
9691 4927 $selected_model,
9692 4928 $deepseek_api_key,
9693 4929 $conversation_history,
9694 - $relevant_content,
9695 - $session_id
4930 + $relevant_content
9696 4931 );
9697 4932
9698 - // Save bot response to transcript
9699 - if (!empty($regular_response) && !empty($session_id)) {
9700 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9701 - }
9702 -
9703 4933 $response_data = [
9704 4934 'text' => $regular_response,
9705 4935 'html' => '',
9706 4936 'session_id' => $session_id
@@ -9723,347 +4953,158 @@
9723 4953 'temperature' => 0.8,
9724 4954 'stream' => true
9725 4955 ]);
9726 4956
9727 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9728 -
9729 - $captured_status_code = 0;
9730 - $captured_body_pre_stream = '';
9731 - $full_response = '';
4957 + // Use cURL for streaming support
4958 + $ch = curl_init();
4959 + curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
4960 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
4961 + curl_setopt($ch, CURLOPT_POST, true);
4962 + curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
4963 + curl_setopt($ch, CURLOPT_HTTPHEADER, array(
4964 + 'Content-Type: application/json',
4965 + 'Authorization: Bearer ' . $deepseek_api_key
4966 + ));
4967 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4968 + curl_setopt($ch, CURLOPT_TIMEOUT, 60);
4969 +
4970 + $full_response = ''; // Accumulate full response for saving
9732 4971 $stream_started = false;
9733 - $buffer = '';
9734 - $errno = 0;
9735 - $http_code = 0;
9736 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9737 - $backoff_ms = array(0, 750, 2000);
9738 -
9739 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9740 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9741 - usleep($backoff_ms[$attempt] * 1000);
4972 +
4973 + // Buffer control for real-time streaming
4974 + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, $testing_data) {
4975 + // Send testing data as the first event if available
4976 + if (!$stream_started && $testing_data !== null) {
4977 + echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4978 + flush();
4979 + $stream_started = true;
4980 + //error_log("MxChat Testing: Sent testing data in DeepSeek stream");
9742 4981 }
9743 -
9744 - $captured_status_code = 0;
9745 - $captured_body_pre_stream = '';
9746 - $full_response = '';
9747 - $stream_started = false;
9748 - $buffer = '';
9749 -
9750 - $ch = curl_init();
9751 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9752 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9753 - curl_setopt($ch, CURLOPT_POST, true);
9754 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9755 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9756 - 'Content-Type: application/json',
9757 - 'Authorization: Bearer ' . $deepseek_api_key
9758 - ));
9759 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9760 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9761 -
9762 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9763 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9764 - $captured_status_code = (int) $m[1];
4982 +
4983 + // Process each chunk of data
4984 + $lines = explode("\n", $data);
4985 +
4986 + foreach ($lines as $line) {
4987 + if (trim($line) === '' || strpos($line, 'data: ') !== 0) {
4988 + continue;
9765 4989 }
9766 - return strlen($header);
9767 - });
9768 -
9769 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9770 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9771 - $captured_body_pre_stream .= $data;
9772 - return strlen($data);
4990 +
4991 + $json_str = substr($line, 6); // Remove 'data: ' prefix
4992 +
4993 + if ($json_str === '[DONE]') {
4994 + echo "data: [DONE]\n\n";
4995 + flush();
4996 + continue;
9773 4997 }
9774 -
9775 - if (!$this->streaming_headers_sent) {
9776 - $this->setup_streaming_headers();
9777 - }
9778 -
9779 - if (!$stream_started && $testing_data !== null) {
9780 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
4998 +
4999 + $json = json_decode($json_str, true);
5000 + if (isset($json['choices'][0]['delta']['content'])) {
5001 + $content = $json['choices'][0]['delta']['content'];
5002 + $full_response .= $content; // Accumulate
5003 + // Send as SSE format
5004 + echo "data: " . json_encode(['content' => $content]) . "\n\n";
9781 5005 flush();
9782 - $stream_started = true;
9783 5006 }
9784 -
9785 - $buffer .= $data;
9786 - $lines = explode("\n", $buffer);
9787 - $buffer = array_pop($lines);
9788 -
9789 - foreach ($lines as $line) {
9790 - if (trim($line) === '') {
9791 - continue;
9792 - }
9793 - if (strpos($line, 'data: ') !== 0) {
9794 - continue;
9795 - }
9796 -
9797 - $json_str = substr($line, 6);
9798 -
9799 - if (trim($json_str) === '[DONE]') {
9800 - echo "data: [DONE]\n\n";
9801 - flush();
9802 - continue;
9803 - }
9804 -
9805 - $json = json_decode(trim($json_str), true);
9806 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9807 - $content = $json['choices'][0]['delta']['content'];
9808 - $full_response .= $content;
9809 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9810 - flush();
9811 - }
5007 + }
5008 +
5009 + return strlen($data);
5010 + });
5011 +
5012 + $response = curl_exec($ch);
5013 + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5014 +
5015 + if (curl_errno($ch) || $http_code !== 200) {
5016 + $curl_error = curl_error($ch);
5017 + curl_close($ch);
5018 +
5019 + // Log the specific error for debugging
5020 + //error_log("MxChat: DeepSeek streaming failed - HTTP: $http_code, cURL: $curl_error");
5021 +
5022 + // Fallback to regular response
5023 + $regular_response = $this->mxchat_generate_response_deepseek(
5024 + $selected_model,
5025 + $deepseek_api_key,
5026 + $conversation_history,
5027 + $relevant_content
5028 + );
5029 +
5030 + // Handle error response from regular function
5031 + if (is_array($regular_response) && isset($regular_response['error'])) {
5032 + if ($testing_data !== null) {
5033 + $regular_response['testing_data'] = $testing_data;
9812 5034 }
9813 -
9814 - return strlen($data);
9815 - });
9816 -
9817 - $response = curl_exec($ch);
9818 - $errno = curl_errno($ch);
9819 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9820 - curl_close($ch);
9821 -
9822 - if (!$errno && $http_code === 200) {
9823 - break;
5035 + header('Content-Type: application/json');
5036 + echo json_encode($regular_response);
5037 + return true;
9824 5038 }
9825 -
9826 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9827 - $can_retry = !$this->streaming_headers_sent
9828 - && ($attempt + 1) < $max_attempts
9829 - && $is_transient;
9830 -
9831 - if (defined('WP_DEBUG') && WP_DEBUG) {
9832 - error_log(sprintf(
9833 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9834 - $attempt + 1, $max_attempts, $http_code, $errno,
9835 - $is_transient ? 'yes' : 'no',
9836 - $can_retry ? 'Retrying.' : 'Giving up.'
9837 - ));
5039 +
5040 + $response_data = [
5041 + 'text' => $regular_response,
5042 + 'html' => '',
5043 + 'session_id' => $session_id
5044 + ];
5045 +
5046 + if ($testing_data !== null) {
5047 + $response_data['testing_data'] = $testing_data;
5048 + //error_log("MxChat Testing: Added testing data to DeepSeek error fallback");
9838 5049 }
9839 -
9840 - if (!$can_retry) {
9841 - break;
9842 - }
5050 +
5051 + header('Content-Type: application/json');
5052 + echo json_encode($response_data);
5053 + return true;
9843 5054 }
9844 -
9845 - if ($errno || $http_code !== 200) {
9846 - return $this->mxchat_stream_emit_fallback(
9847 - 'openai',
9848 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id),
9849 - $session_id,
9850 - $testing_data
9851 - );
9852 - }
9853 -
5055 +
5056 + curl_close($ch);
5057 +
9854 5058 // Save the complete response to maintain chat persistence
9855 5059 if (!empty($full_response) && !empty($session_id)) {
9856 - // Prepare RAG context for streaming response
9857 - $rag_context_for_storage = null;
9858 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9859 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9860 -
9861 - if ($has_rag_data || $has_action_data) {
9862 - $rag_context_for_storage = [];
9863 -
9864 - if ($has_rag_data) {
9865 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9866 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9867 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9868 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9869 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9870 - }
9871 -
9872 - if ($has_action_data) {
9873 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9874 - }
9875 - }
9876 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
5060 + $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9877 5061 }
9878 -
5062 +
9879 5063 return true; // Indicate streaming completed successfully
9880 -
5064 +
9881 5065 } catch (Exception $e) {
9882 - return $this->mxchat_stream_emit_fallback(
9883 - 'openai',
9884 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9885 - $session_id,
9886 - $testing_data
5066 + //error_log("MxChat DeepSeek streaming exception: " . $e->getMessage());
5067 +
5068 + // Fallback to regular response
5069 + $regular_response = $this->mxchat_generate_response_deepseek(
5070 + $selected_model,
5071 + $deepseek_api_key,
5072 + $conversation_history,
5073 + $relevant_content
9887 5074 );
9888 - }
9889 -}
9890 -
9891 -
9892 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id = '') {
9893 - try {
9894 - if (!is_array($conversation_history)) {
9895 - $conversation_history = array();
9896 - }
9897 -
9898 - $bot_id = $this->get_current_bot_id($session_id);
9899 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9900 5075
9901 - $formatted_conversation = array();
9902 -
9903 - $formatted_conversation[] = array(
9904 - 'role' => 'system',
9905 - 'content' => $system_prompt_instructions . " " . $relevant_content
9906 - );
9907 -
9908 - foreach ($conversation_history as $message) {
9909 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9910 - $role = $message['role'];
9911 -
9912 - if ($role === 'bot' || $role === 'agent') {
9913 - $role = 'assistant';
9914 - }
9915 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9916 - $role = 'user';
9917 - }
9918 -
9919 - $formatted_conversation[] = array(
9920 - 'role' => $role,
9921 - 'content' => $message['content']
9922 - );
5076 + // Handle error response from regular function
5077 + if (is_array($regular_response) && isset($regular_response['error'])) {
5078 + if ($testing_data !== null) {
5079 + $regular_response['testing_data'] = $testing_data;
9923 5080 }
5081 + header('Content-Type: application/json');
5082 + echo json_encode($regular_response);
5083 + return true;
9924 5084 }
9925 -
9926 - $body = json_encode([
9927 - 'model' => $selected_model,
9928 - 'messages' => $formatted_conversation,
9929 - 'temperature' => 1,
9930 - ]);
9931 -
9932 - $args = [
9933 - 'body' => $body,
9934 - 'headers' => [
9935 - 'Content-Type' => 'application/json',
9936 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9937 - 'HTTP-Referer' => home_url(),
9938 - 'X-Title' => get_bloginfo('name'),
9939 - ],
9940 - 'timeout' => 60,
9941 - 'redirection' => 5,
9942 - 'blocking' => true,
9943 - 'httpversion' => '1.0',
9944 - 'sslverify' => true,
5085 +
5086 + $response_data = [
5087 + 'text' => $regular_response,
5088 + 'html' => '',
5089 + 'session_id' => $session_id
9945 5090 ];
9946 -
9947 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9948 -
9949 - if (is_wp_error($response)) {
9950 - $error_message = $response->get_error_message();
9951 - return [
9952 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenRouter'),
9953 - 'error_code' => 'openrouter_connection_error',
9954 - 'provider' => 'openrouter'
9955 - ];
5091 +
5092 + if ($testing_data !== null) {
5093 + $response_data['testing_data'] = $testing_data;
5094 + //error_log("MxChat Testing: Added testing data to DeepSeek exception fallback");
9956 5095 }
9957 -
9958 - $status_code = wp_remote_retrieve_response_code($response);
9959 - if ($status_code !== 200) {
9960 - $response_body = wp_remote_retrieve_body($response);
9961 - $decoded_response = json_decode($response_body, true);
9962 -
9963 - $error_message = isset($decoded_response['error']['message'])
9964 - ? $decoded_response['error']['message']
9965 - : 'HTTP Error ' . $status_code;
9966 -
9967 - return [
9968 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9969 - 'error_code' => 'openrouter_api_error',
9970 - 'provider' => 'openrouter',
9971 - 'status_code' => $status_code
9972 - ];
9973 - }
9974 -
9975 - $response_body = wp_remote_retrieve_body($response);
9976 - $decoded_response = json_decode($response_body, true);
9977 -
9978 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9979 - return trim($decoded_response['choices'][0]['message']['content']);
9980 - } else {
9981 - return [
9982 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9983 - 'error_code' => 'openrouter_response_format_error',
9984 - 'provider' => 'openrouter'
9985 - ];
9986 - }
9987 - } catch (Exception $e) {
9988 - return [
9989 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9990 - 'error_code' => 'openrouter_exception',
9991 - 'provider' => 'openrouter'
9992 - ];
5096 +
5097 + header('Content-Type: application/json');
5098 + echo json_encode($response_data);
5099 + return true;
9993 5100 }
9994 5101 }
9995 5102
9996 -/**
9997 - * Build a chat-bubble-safe message for a non-200 provider (chat) error.
9998 - *
9999 - * Visitors must NEVER see raw API internals (model names, key/billing/quota
10000 - * text). Admins (manage_options) get an actionable hint — and, for the common
10001 - * "model not available on this key" case, a direct pointer to change the model
10002 - * (the site owner can fix it in one click). Anthropic returns model-access as a
10003 - * 4xx with a message like "Claude Fable 5 is not available. Please use Opus 4.8."
10004 - *
10005 - * Provider-agnostic by design (reusable for the xai/gemini/deepseek branches),
10006 - * but Anthropic is the confirmed, reproduced case wired up here (plan 1d3b0f).
10007 - *
10008 - * @param int $http_code HTTP status from the provider.
10009 - * @param string $error_message Raw provider error.message (may be empty).
10010 - * @param string $provider_label Human provider name, e.g. 'Anthropic'.
10011 - * @return string Message safe to render as a chat bubble.
10012 - */
10013 -private function mxchat_friendly_chat_error($http_code, $error_message, $provider_label = '') {
10014 - $raw = trim((string) $error_message);
5103 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
5104 + // Get system prompt instructions from options
5105 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10015 5106
10016 - // Detect a model-access / availability problem the site owner can fix by
10017 - // choosing a different model. (Anthropic phrasing + the common API shapes.)
10018 - $low = strtolower($raw);
10019 - $is_model_access = (strpos($low, 'not available') !== false)
10020 - || (strpos($low, 'does not have access') !== false)
10021 - || (strpos($low, 'do not have access') !== false)
10022 - || (strpos($low, 'does not exist') !== false) // OpenAI: "model `x` does not exist or you do not have access"
10023 - || (strpos($low, 'model_not_found') !== false)
10024 - || (strpos($low, 'not_found_error') !== false)
10025 - || (strpos($low, 'model not found') !== false) // xAI
10026 - || (strpos($low, 'not found') !== false) // Gemini: "models/x is not found for API version ..."
10027 - || (strpos($low, 'permission_denied') !== false) // Gemini gated model
10028 - || (strpos($low, 'permission denied') !== false);
10029 -
10030 - if (current_user_can('manage_options')) {
10031 - if ($is_model_access) {
10032 - return $raw !== ''
10033 - ? sprintf(
10034 - /* translators: %s: raw provider error detail */
10035 - esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings. (Details: %s)', 'mxchat'),
10036 - $raw
10037 - )
10038 - : esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings.', 'mxchat');
10039 - }
10040 - return $raw !== ''
10041 - ? sprintf(
10042 - /* translators: 1: provider label, 2: raw provider error detail */
10043 - esc_html__('The AI provider (%1$s) returned an error: %2$s. Check your model and API key in MxChat → Settings.', 'mxchat'),
10044 - $provider_label !== '' ? $provider_label : esc_html__('AI', 'mxchat'),
10045 - $raw
10046 - )
10047 - : esc_html__('The AI provider returned an error. Check your model and API key in MxChat → Settings.', 'mxchat');
10048 - }
10049 -
10050 - // Visitors: friendly, generic, no internals leaked.
10051 - return esc_html__('Sorry, I\'m having trouble responding right now. Please try again in a moment.', 'mxchat');
10052 -}
10053 -
10054 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id = '') {
10055 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
10056 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
10057 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
10058 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
10059 -
10060 - // Get bot ID from session or request
10061 - $bot_id = $this->get_current_bot_id($session_id);
10062 -
10063 - // Get system prompt instructions using centralized function
10064 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10065 -
10066 5107 // Clean and validate conversation history
10067 5108 foreach ($conversation_history as &$message) {
10068 5109 // Convert bot and agent roles to assistant
10069 5110 if ($message['role'] === 'bot' || $message['role'] === 'agent') {
@@ -10090,17 +5131,15 @@
10090 5131 'content' => $relevant_content
10091 5132 ];
10092 5133
10093 5134 // Build request body
10094 - $payload = [
5135 + $body = json_encode([
10095 5136 'model' => $selected_model,
10096 5137 'max_tokens' => 1000,
10097 5138 'temperature' => 0.8,
10098 5139 'messages' => $conversation_history,
10099 5140 'system' => $system_prompt_instructions
10100 - ];
10101 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
10102 - $body = json_encode($payload);
5141 + ]);
10103 5142
10104 5143 // Set up API request
10105 5144 $args = [
10106 5145 'body' => $body,
@@ -10116,9 +5155,9 @@
10116 5155 'sslverify' => true,
10117 5156 ];
10118 5157
10119 5158 // Make API request
10120 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
5159 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
10121 5160
10122 5161 // Check for WordPress errors
10123 5162 if (is_wp_error($response)) {
10124 5163 //error_log("Claude API request error: " . $response->get_error_message());
@@ -10132,17 +5171,13 @@
10132 5171 //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
10133 5172
10134 5173 // Try to extract error message from response
10135 5174 $error_data = json_decode($error_body, true);
10136 - $error_message = isset($error_data['error']['message']) ?
10137 - $error_data['error']['message'] :
5175 + $error_message = isset($error_data['error']['message']) ?
5176 + $error_data['error']['message'] :
10138 5177 "HTTP error " . $http_code;
10139 -
10140 - // Surface an admin-actionable message (and a model-change pointer for the
10141 - // model-access case) without leaking raw API internals to visitors. This
10142 - // is the single chokepoint for BOTH the non-streaming and streaming Claude
10143 - // paths (the stream's non-200 fallback re-enters this method). plan 1d3b0f.
10144 - return $this->mxchat_friendly_chat_error($http_code, $error_message, 'Anthropic');
5178 +
5179 + return "Sorry, the API returned an error: " . $error_message;
10145 5180 }
10146 5181
10147 5182 // Parse response
10148 5183 $response_body = json_decode(wp_remote_retrieve_body($response), true);
@@ -10152,17 +5187,14 @@
10152 5187 //error_log("Claude API JSON decode error: " . json_last_error_msg());
10153 5188 return "Sorry, there was an error processing the API response.";
10154 5189 }
10155 5190
10156 - // Extract and validate response content. claude-fable-5 prepends a
10157 - // thinking block to content even with no thinking param — take the first
10158 - // TEXT block rather than content[0].
10159 - if (isset($response_body['content']) && is_array($response_body['content'])) {
10160 - foreach ($response_body['content'] as $block) {
10161 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
10162 - return trim($block['text']);
10163 - }
10164 - }
5191 + // Extract and validate response content
5192 + if (isset($response_body['content']) &&
5193 + is_array($response_body['content']) &&
5194 + !empty($response_body['content']) &&
5195 + isset($response_body['content'][0]['text'])) {
5196 + return trim($response_body['content'][0]['text']);
10165 5197 }
10166 5198
10167 5199 // Log unexpected response format
10168 5200 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
@@ -10167,9 +5199,9 @@
10167 5199 // Log unexpected response format
10168 5200 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
10169 5201 return "Sorry, I received an unexpected response format from the API.";
10170 5202 }
10171 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id = '') {
5203 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
10172 5204 try {
10173 5205 // Ensure conversation_history is an array
10174 5206 if (!is_array($conversation_history)) {
10175 5207 $conversation_history = array();
@@ -10174,16 +5206,11 @@
10174 5206 if (!is_array($conversation_history)) {
10175 5207 $conversation_history = array();
10176 5208 }
10177 5209
10178 - // Get bot ID from session or request. plan eb9c38: resolve the real bot
10179 - // from the session (was hardcoded '' → always default bot on multi-bot
10180 - // installs) and fix the undefined $session_id that fed get_system_instructions.
10181 - $bot_id = $this->get_current_bot_id($session_id);
5210 + // Get system prompt instructions from options
5211 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10182 5212
10183 - // Get system prompt instructions using centralized function
10184 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10185 -
10186 5213 // Create a new array for the formatted conversation
10187 5214 $formatted_conversation = array();
10188 5215
10189 5216 // Add system message first
@@ -10211,44 +5238,15 @@
10211 5238 );
10212 5239 }
10213 5240 }
10214 5241
10215 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
10216 - $is_gpt5_model = (
10217 - strpos($selected_model, 'gpt-5') === 0 ||
10218 - $selected_model === 'gpt-5.2' ||
10219 - $selected_model === 'gpt-5.1-2025-11-13' ||
10220 - $selected_model === 'gpt-5' ||
10221 - $selected_model === 'gpt-5-mini' ||
10222 - $selected_model === 'gpt-5-nano'
10223 - );
10224 -
10225 - // Build request body with optimal settings for fast responses
10226 - $request_body = [
5242 + $body = json_encode([
10227 5243 'model' => $selected_model,
10228 5244 'messages' => $formatted_conversation,
10229 5245 'temperature' => 1,
10230 5246 'stream' => false
10231 - ];
5247 + ]);
10232 5248
10233 - // Add reasoning_effort only for GPT-5 models that support it
10234 - // These chat models don't support reasoning_effort parameter
10235 - $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');
10236 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
10237 - // GPT-5.1 uses 'low' instead of 'minimal'
10238 - if ($selected_model === 'gpt-5.1-2025-11-13') {
10239 - $request_body['reasoning_effort'] = 'low';
10240 - } elseif ($selected_model === 'gpt-5.5') {
10241 - $request_body['reasoning_effort'] = 'none';
10242 - } elseif ($selected_model === 'gpt-5.4') {
10243 - $request_body['reasoning_effort'] = 'none';
10244 - } else {
10245 - $request_body['reasoning_effort'] = 'minimal';
10246 - }
10247 - }
10248 -
10249 - $body = json_encode($request_body);
10250 -
10251 5249 $args = [
10252 5250 'body' => $body,
10253 5251 'headers' => [
10254 5252 'Content-Type' => 'application/json',
@@ -10260,14 +5258,15 @@
10260 5258 'httpversion' => '1.0',
10261 5259 'sslverify' => true,
10262 5260 ];
10263 5261
10264 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
5262 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
10265 5263
10266 5264 if (is_wp_error($response)) {
10267 5265 $error_message = $response->get_error_message();
5266 + //error_log('OpenAI API Error: ' . $error_message);
10268 5267 return [
10269 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenAI'),
5268 + 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
10270 5269 'error_code' => 'openai_connection_error',
10271 5270 'provider' => 'openai'
10272 5271 ];
10273 5272 }
@@ -10284,8 +5283,10 @@
10284 5283 $error_type = isset($decoded_response['error']['type'])
10285 5284 ? $decoded_response['error']['type']
10286 5285 : 'unknown';
10287 5286
5287 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
5288 +
10288 5289 // Handle specific error types
10289 5290 switch ($error_type) {
10290 5291 case 'invalid_request_error':
10291 5292 if (strpos($error_message, 'API key') !== false) {
@@ -10318,13 +5319,11 @@
10318 5319 'provider' => 'openai'
10319 5320 ];
10320 5321 }
10321 5322
10322 - // Generic error fallback only — the typed cases above already produce
10323 - // clean messages. Route the raw-tail generic case through the leak-safe
10324 - // helper so visitors never see provider internals. plan 5da59a.
5323 + // Generic error fallback
10325 5324 return [
10326 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'OpenAI'),
5325 + 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
10327 5326 'error_code' => 'openai_api_error',
10328 5327 'provider' => 'openai',
10329 5328 'status_code' => $status_code
10330 5329 ];
@@ -10335,8 +5334,9 @@
10335 5334
10336 5335 if (isset($decoded_response['choices'][0]['message']['content'])) {
10337 5336 return trim($decoded_response['choices'][0]['message']['content']);
10338 5337 } else {
5338 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
10339 5339 return [
10340 5340 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
10341 5341 'error_code' => 'openai_response_format_error',
10342 5342 'provider' => 'openai'
@@ -10342,8 +5342,9 @@
10342 5342 'provider' => 'openai'
10343 5343 ];
10344 5344 }
10345 5345 } catch (Exception $e) {
5346 + //error_log('OpenAI Exception: ' . $e->getMessage());
10346 5347 return [
10347 5348 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
10348 5349 'error_code' => 'openai_exception',
10349 5350 'provider' => 'openai'
@@ -10349,17 +5350,13 @@
10349 5350 'provider' => 'openai'
10350 5351 ];
10351 5352 }
10352 5353 }
5354 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
5355 + try {
5356 + // Get system prompt instructions from options
5357 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10353 5358
10354 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id = '') {
10355 - try {
10356 - // Get bot ID from session or request
10357 - $bot_id = $this->get_current_bot_id($session_id);
10358 -
10359 - // Get system prompt instructions using centralized function
10360 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10361 -
10362 5359 // Add system prompt to relevant content
10363 5360 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10364 5361
10365 5362 // Prepend system instructions to the conversation history
@@ -10408,9 +5405,9 @@
10408 5405 'sslverify' => true,
10409 5406 ];
10410 5407
10411 5408 // Make the API request
10412 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
5409 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
10413 5410
10414 5411 // Process the response
10415 5412 if (is_wp_error($response)) {
10416 5413 $error_message = $response->get_error_message();
@@ -10415,9 +5412,9 @@
10415 5412 if (is_wp_error($response)) {
10416 5413 $error_message = $response->get_error_message();
10417 5414 //error_log('X.AI API Error: ' . $error_message);
10418 5415 return [
10419 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'X.AI'),
5416 + 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
10420 5417 'error_code' => 'xai_connection_error',
10421 5418 'provider' => 'xai'
10422 5419 ];
10423 5420 }
@@ -10511,14 +5508,11 @@
10511 5508 'provider' => 'xai'
10512 5509 ];
10513 5510 }
10514 5511
10515 - // Generic error fallback. Route the user-facing text through the
10516 - // leak-safe helper (admins get an actionable hint, visitors a generic
10517 - // fallback) instead of echoing raw provider internals. Preserve the
10518 - // structured contract (error_code/provider/status_code) for logging. plan 5da59a.
5512 + // Generic error fallback with the actual error message
10519 5513 return [
10520 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'xAI'),
5514 + 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
10521 5515 'error_code' => 'xai_api_error',
10522 5516 'provider' => 'xai',
10523 5517 'status_code' => $status_code
10524 5518 ];
@@ -10547,9 +5541,9 @@
10547 5541 }
10548 5542
10549 5543
10550 5544 }
10551 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id = '') {
5545 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
10552 5546 try {
10553 5547 // Ensure conversation_history is an array
10554 5548 if (!is_array($conversation_history)) {
10555 5549 $conversation_history = array();
@@ -10554,14 +5548,11 @@
10554 5548 if (!is_array($conversation_history)) {
10555 5549 $conversation_history = array();
10556 5550 }
10557 5551
10558 - // Get bot ID from session or request
10559 - $bot_id = $this->get_current_bot_id($session_id);
10560 -
10561 - // Get system prompt instructions using centralized function
10562 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10563 -
5552 + // Get system prompt instructions from options
5553 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
5554 +
10564 5555 // Create a new array for the formatted conversation
10565 5556 $formatted_conversation = array();
10566 5557
10567 5558 // Add system message first
@@ -10609,15 +5600,15 @@
10609 5600 'httpversion' => '1.0',
10610 5601 'sslverify' => true,
10611 5602 ];
10612 5603
10613 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
5604 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
10614 5605
10615 5606 if (is_wp_error($response)) {
10616 5607 $error_message = $response->get_error_message();
10617 5608 //error_log('DeepSeek API Error: ' . $error_message);
10618 5609 return [
10619 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'DeepSeek'),
5610 + 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
10620 5611 'error_code' => 'deepseek_connection_error',
10621 5612 'provider' => 'deepseek'
10622 5613 ];
10623 5614 }
@@ -10681,11 +5672,11 @@
10681 5672 'provider' => 'deepseek'
10682 5673 ];
10683 5674 }
10684 5675
10685 - // Generic error fallback — leak-safe helper (see plan 5da59a / 1d3b0f).
5676 + // Generic error fallback
10686 5677 return [
10687 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'DeepSeek'),
5678 + 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
10688 5679 'error_code' => 'deepseek_api_error',
10689 5680 'provider' => 'deepseek',
10690 5681 'status_code' => $status_code
10691 5682 ];
@@ -10712,20 +5703,12 @@
10712 5703 'provider' => 'deepseek'
10713 5704 ];
10714 5705 }
10715 5706 }
10716 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content, $session_id = '') {
10717 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10718 - // Auto-rescue existing installs whose saved model is the dead ID.
10719 - if ($selected_model === 'gemini-3-pro-preview') {
10720 - $selected_model = 'gemini-3.1-pro-preview';
10721 - }
10722 - // Get bot ID from session or request
10723 - $bot_id = $this->get_current_bot_id($session_id);
10724 -
10725 - // Get system prompt instructions using centralized function
10726 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10727 -
5707 +private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
5708 + // Get system prompt instructions from options
5709 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
5710 +
10728 5711 // Add system prompt to relevant content
10729 5712 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10730 5713
10731 5714 // Format messages for Gemini API
@@ -10820,11 +5803,9 @@
10820 5803 ]
10821 5804 ]);
10822 5805
10823 5806 // Prepare the API endpoint
10824 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10825 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10826 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
5807 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10827 5808
10828 5809 // Set up the API request
10829 5810 $args = [
10830 5811 'body' => $body,
@@ -10838,31 +5819,22 @@
10838 5819 'sslverify' => true,
10839 5820 ];
10840 5821
10841 5822 // Make the API request
10842 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10843 -
5823 + $response = wp_remote_post($api_endpoint, $args);
5824 +
10844 5825 // Process the response
10845 5826 if (is_wp_error($response)) {
10846 - // plan b13282: route the transport-error string through the leak-safe helper
10847 - // (admin-actionable, generic for visitors) instead of echoing the raw WP HTTP
10848 - // error. http_code 0 = no HTTP response, so the helper uses the generic branch.
10849 - return $this->mxchat_friendly_chat_error(0, $response->get_error_message(), 'Gemini');
5827 + return "Sorry, there was an error processing your request: " . $response->get_error_message();
10850 5828 }
10851 5829
10852 5830 $response_body = json_decode(wp_remote_retrieve_body($response), true);
10853 5831
10854 - // Handle potential errors in the response. Gemini surfaces errors as a
10855 - // 200/non-200 body with an `error` envelope; route the user-facing text
10856 - // through the leak-safe helper (admin-actionable, no visitor leak) rather
10857 - // than echoing the raw provider message. plan 5da59a.
5832 + // Handle potential errors in the response
10858 5833 if (isset($response_body['error'])) {
10859 5834 //error_log('Gemini API Error: ' . json_encode($response_body['error']));
10860 - $gemini_error_message = isset($response_body['error']['message'])
10861 - ? $response_body['error']['message']
10862 - : 'Unknown error';
10863 - $gemini_http_code = wp_remote_retrieve_response_code($response);
10864 - return $this->mxchat_friendly_chat_error($gemini_http_code, $gemini_error_message, 'Gemini');
5835 + return "Sorry, there was an error with the Gemini API: " .
5836 + (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
10865 5837 }
10866 5838
10867 5839 // Extract the response text
10868 5840 if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
@@ -10872,12 +5844,11 @@
10872 5844 return "Sorry, I couldn't process that request. The response format was unexpected.";
10873 5845 }
10874 5846 }
10875 5847
10876 -
10877 5848 public function test_streaming_request() {
10878 5849 $options = get_option('mxchat_options', []);
10879 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
5850 + $model = $options['model'] ?? 'gpt-4o';
10880 5851
10881 5852 // Detect provider from model prefix
10882 5853 $provider = strtolower(explode('-', $model)[0]);
10883 5854
@@ -10961,10 +5932,9 @@
10961 5932 $response = $this->mxchat_generate_response_deepseek(
10962 5933 $selected_model,
10963 5934 $deepseek_api_key,
10964 5935 $conversation_history,
10965 - $relevant_content,
10966 - $session_id
5936 + $relevant_content
10967 5937 );
10968 5938 }
10969 5939 break;
10970 5940
@@ -11004,8 +5974,9 @@
11004 5974
11005 5975 return true;
11006 5976 }
11007 5977
5978 +
11008 5979 public function mxchat_dismiss_pre_chat_message() {
11009 5980 // Get and sanitize the user identifier
11010 5981 $user_id = $this->mxchat_get_user_identifier();
11011 5982 $user_id = sanitize_key($user_id);
@@ -11059,63 +6030,40 @@
11059 6030
11060 6031 return $dotProduct / ($normA * $normB);
11061 6032 }
11062 6033
11063 -
11064 6034 public function mxchat_enqueue_scripts_styles() {
11065 - // Fetch options from the database first to check loading strategy
11066 - $this->options = get_option('mxchat_options');
11067 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11068 -
11069 - // Always enqueue CSS immediately
6035 + // Define version numbers for the styles and scripts
6036 + $chat_style_version = '2.4.0';
6037 + $chat_script_version = '2.4.0';
6038 + // Enqueue the script
6039 + wp_enqueue_script(
6040 + 'mxchat-chat-js',
6041 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
6042 + array('jquery'),
6043 + $chat_script_version,
6044 + true
6045 + );
6046 + // Enqueue the CSS
11070 6047 wp_enqueue_style(
11071 6048 'mxchat-chat-css',
11072 6049 plugin_dir_url(__FILE__) . '../css/chat-style.css',
11073 6050 array(),
11074 - MXCHAT_VERSION
6051 + $chat_style_version
11075 6052 );
11076 -
11077 - // Handle script loading based on strategy
11078 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11079 - // Enqueue the script normally
11080 - wp_enqueue_script(
11081 - 'mxchat-chat-js',
11082 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
11083 - array('jquery'),
11084 - MXCHAT_VERSION,
11085 - true
11086 - );
11087 -
11088 - // Add defer attribute if strategy is 'defer'
11089 - if ($loading_strategy === 'defer') {
11090 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
11091 - }
11092 - } else {
11093 - // For delay or interaction-based loading, we'll use a custom loader
11094 - // Don't enqueue the main script - we'll load it dynamically
11095 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
11096 - }
11097 -
6053 + // Fetch options from the database
6054 + $this->options = get_option('mxchat_options');
11098 6055 $prompts_options = get_option('mxchat_prompts_options', array());
11099 -
11100 - // Check if AI theme is active - if so, skip inline colors in JavaScript
11101 - $theme_options = get_option('mxchat_theme_options', array());
11102 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11103 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11104 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11105 -
6056 +
11106 6057 // Prepare settings for JavaScript
11107 6058 $style_settings = array(
11108 6059 'ajax_url' => admin_url('admin-ajax.php'),
11109 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
11110 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
11111 - // as a one-shot fallback for the first interaction on a fresh page load
11112 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
11113 - // but the widget refetches before each subsequent send.
11114 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11115 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
6060 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
6061 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-4o',
6062 + 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
11116 6063 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11117 6064 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
6065 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
11118 6066 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11119 6067 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11120 6068 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11121 6069 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -11130,8 +6078,9 @@
11130 6078 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11131 6079 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11132 6080 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11133 6081 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
6082 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
11134 6083 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11135 6084 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11136 6085 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11137 6086 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
@@ -11137,145 +6086,15 @@
11137 6086 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11138 6087 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
11139 6088 'initial_email_state' => null, // Also fixed this undefined variable
11140 6089 'skip_email_check' => true,
11141 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11142 - 'skip_inline_colors' => $skip_inline_colors,
11143 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
6090 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
11144 6091 );
11145 -
11146 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11147 - // print/transcript, satisfaction rating) come from the shared
11148 - // dynamic-settings method so this inline payload and the first-open
11149 - // refresh endpoint can never drift (plan-32db95).
11150 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11151 -
11152 - // For normal/defer loading, use wp_localize_script
11153 - // For delayed loading, we store settings in a transient to be output inline
11154 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11155 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11156 - } else {
11157 - // Store settings for the delayed loader to use
11158 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
11159 - }
6092 + // Pass the settings to the script
6093 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11160 6094 }
11161 6095
11162 -/**
11163 - * Output the delayed script loader for performance optimization
11164 - */
11165 -public function mxchat_output_delayed_script_loader() {
11166 - $this->options = get_option('mxchat_options');
11167 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11168 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
11169 6096
11170 - // Get the stored settings
11171 - $prompts_options = get_option('mxchat_prompts_options', array());
11172 - $theme_options = get_option('mxchat_theme_options', array());
11173 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11174 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11175 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11176 -
11177 - $style_settings = array(
11178 - 'ajax_url' => admin_url('admin-ajax.php'),
11179 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
11180 - // before each send. This inline value is a one-shot fallback for the first interaction.
11181 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11182 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11183 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11184 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11185 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11186 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11187 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11188 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11189 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11190 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11191 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11192 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11193 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11194 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11195 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11196 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11197 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11198 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11199 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11200 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11201 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11202 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11203 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11204 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11205 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
11206 - 'initial_email_state' => null,
11207 - 'skip_email_check' => true,
11208 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11209 - 'skip_inline_colors' => $skip_inline_colors,
11210 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11211 - );
11212 -
11213 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11214 - // print/transcript, satisfaction rating) come from the shared
11215 - // dynamic-settings method so this inline payload and the first-open
11216 - // refresh endpoint can never drift (plan-32db95).
11217 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11218 -
11219 - // Determine delay time based on strategy
11220 - $delay_ms = 0;
11221 - switch ($loading_strategy) {
11222 - case 'delay_1s':
11223 - $delay_ms = 1000;
11224 - break;
11225 - case 'delay_3s':
11226 - $delay_ms = 3000;
11227 - break;
11228 - case 'delay_5s':
11229 - $delay_ms = 5000;
11230 - break;
11231 - }
11232 -
11233 - ?>
11234 - <script type="text/javascript">
11235 - (function() {
11236 - var mxchatLoaded = false;
11237 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
11238 - window.mxchatChat = mxchatChat;
11239 -
11240 - function loadMxChatScript() {
11241 - if (mxchatLoaded) return;
11242 - mxchatLoaded = true;
11243 -
11244 - function appendChatScript() {
11245 - var script = document.createElement('script');
11246 - script.src = <?php echo wp_json_encode($script_url); ?>;
11247 - script.type = 'text/javascript';
11248 - document.body.appendChild(script);
11249 - }
11250 -
11251 - if (typeof jQuery !== 'undefined') {
11252 - appendChatScript();
11253 - } else {
11254 - var jq = document.createElement('script');
11255 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
11256 - jq.onload = appendChatScript;
11257 - document.body.appendChild(jq);
11258 - }
11259 - }
11260 -
11261 - <?php if ($loading_strategy === 'on_interaction'): ?>
11262 - // Load on user interaction
11263 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
11264 - events.forEach(function(evt) {
11265 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
11266 - });
11267 - // Fallback: load after 8 seconds if no interaction
11268 - setTimeout(loadMxChatScript, 8000);
11269 - <?php else: ?>
11270 - // Load after specified delay
11271 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
11272 - <?php endif; ?>
11273 - })();
11274 - </script>
11275 - <?php
11276 -}
11277 -
11278 6097 /**
11279 6098 * Setup the cron jobs for rate limits with guard against multiple calls
11280 6099 */
11281 6100 public function setup_rate_limit_cron_jobs() {
@@ -11413,9 +6232,9 @@
11413 6232 update_option('mxchat_next_rate_limit_check', time() + $check_interval);
11414 6233 }
11415 6234 }
11416 6235 /**
11417 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
6236 + * Enhanced rate limit check that includes fallback cleanup
11418 6237 */
11419 6238 public function check_rate_limit() {
11420 6239 // Check if we need to run fallback cleanup
11421 6240 $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
@@ -11425,66 +6244,11 @@
11425 6244 $this->mxchat_reset_rate_limits();
11426 6245 update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
11427 6246 }
11428 6247
11429 - // Get bot ID from current request context
11430 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
6248 + // Continue with your existing rate limit logic...
6249 + $all_options = get_option('mxchat_options', []);
11431 6250
11432 - // Get bot-specific options (includes rate limits if overridden)
11433 - $bot_options = $this->get_bot_options($bot_id);
11434 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
11435 -
11436 - // Use bot-specific rate limits if available, otherwise fall back to default
11437 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
11438 -
11439 - // -------------------------------------------------------------------
11440 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
11441 - // it acts as a hard ceiling across all users + all roles. Default is
11442 - // 'unlimited' so existing installs are unchanged. Counter key drops
11443 - // both <role> and <user_id> segments — single pool per bot.
11444 - // -------------------------------------------------------------------
11445 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
11446 - ? $current_options['rate_limits_global']
11447 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
11448 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
11449 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
11450 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
11451 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11452 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
11453 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
11454 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
11455 - if ((int) $global_data['count'] === 0) {
11456 - $global_data['timestamp'] = time();
11457 - update_option($global_option, $global_data);
11458 - }
11459 - $now = time();
11460 - $ts = (int) $global_data['timestamp'];
11461 - $reset = false;
11462 - switch ($global_timeframe) {
11463 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
11464 - case 'daily': $reset = ($now - $ts) >= 86400; break;
11465 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
11466 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
11467 - }
11468 - if ($reset) {
11469 - $global_data = ['count' => 0, 'timestamp' => $now];
11470 - update_option($global_option, $global_data);
11471 - }
11472 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
11473 - $global_msg = !empty($global_cfg['message'])
11474 - ? $global_cfg['message']
11475 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
11476 - return [
11477 - 'error' => true,
11478 - 'message' => $this->process_rate_limit_message_html($global_msg),
11479 - ];
11480 - }
11481 - // Reserve the slot for this request. Per-role check below also increments
11482 - // its own counter — that is intentional, both ceilings apply independently.
11483 - $global_data['count']++;
11484 - update_option($global_option, $global_data);
11485 - }
11486 -
11487 6251 // Determine user role or if logged out
11488 6252 if (is_user_logged_in()) {
11489 6253 $user = wp_get_current_user();
11490 6254 $user_id = $user->ID;
@@ -11504,13 +6268,13 @@
11504 6268 $user_id = $this->get_client_ip();
11505 6269 }
11506 6270
11507 6271 // Check if rate limits are configured for this role
11508 - if (!isset($rate_limits_source[$role])) {
6272 + if (!isset($all_options['rate_limits'][$role])) {
11509 6273 return true; // No limit set for this role
11510 6274 }
11511 6275
11512 - $limit = $rate_limits_source[$role]['limit'];
6276 + $limit = $all_options['rate_limits'][$role]['limit'];
11513 6277
11514 6278 // If unlimited, return true immediately
11515 6279 if ($limit === 'unlimited') {
11516 6280 return true;
@@ -11515,16 +6279,13 @@
11515 6279 if ($limit === 'unlimited') {
11516 6280 return true;
11517 6281 }
11518 6282
11519 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
6283 + // Get the option name for this user/role with safer naming
11520 6284 $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
11521 6285 $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
11522 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
6286 + $option_name = 'mxchat_chat_limit_' . $safe_role . '_' . $safe_user_id;
11523 6287
11524 - // Include bot_id in option name so each bot has separate rate limits
11525 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
11526 -
11527 6288 // Get the counter data
11528 6289 $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
11529 6290
11530 6291 // If first request or counter reset needed, set the initial timestamp
@@ -11533,10 +6294,10 @@
11533 6294 update_option($option_name, $limit_data);
11534 6295 }
11535 6296
11536 6297 // Get the timeframe
11537 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
11538 - $rate_limits_source[$role]['timeframe'] : 'daily';
6298 + $timeframe = isset($all_options['rate_limits'][$role]['timeframe']) ?
6299 + $all_options['rate_limits'][$role]['timeframe'] : 'daily';
11539 6300
11540 6301 // Check if the counter needs to be reset based on timeframe
11541 6302 $current_time = time();
11542 6303 $timestamp = $limit_data['timestamp'];
@@ -11565,10 +6326,10 @@
11565 6326
11566 6327 // Check if user has exceeded their limit
11567 6328 if ($limit_data['count'] >= intval($limit)) {
11568 6329 // Get the custom message for this role
11569 - $message = !empty($rate_limits_source[$role]['message'])
11570 - ? $rate_limits_source[$role]['message']
6330 + $message = !empty($all_options['rate_limits'][$role]['message'])
6331 + ? $all_options['rate_limits'][$role]['message']
11571 6332 : __('Rate limit exceeded. Please try again later.', 'mxchat');
11572 6333
11573 6334 // Add timeframe information to the message if placeholders exist
11574 6335 $timeframe_label = '';
@@ -11858,11 +6619,8 @@
11858 6619
11859 6620 /**
11860 6621 * AJAX handler to get system information for testing panel
11861 6622 */
11862 -/**
11863 - * AJAX handler to get system information for testing panel
11864 - */
11865 6623 public function mxchat_get_system_info() {
11866 6624 // Verify nonce for security
11867 6625 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11868 6626 wp_send_json_error(['message' => 'Invalid nonce']);
@@ -11880,24 +6638,10 @@
11880 6638 ? $this->options['system_prompt_instructions']
11881 6639 : 'No system prompt configured';
11882 6640
11883 6641 // Get selected model
11884 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
6642 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
11885 6643
11886 - // Check if OpenRouter is being used
11887 - $is_openrouter = ($selected_model === 'openrouter');
11888 - $openrouter_model = '';
11889 -
11890 - if ($is_openrouter) {
11891 - // Get the actual OpenRouter model that's selected
11892 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11893 - ? $this->options['openrouter_selected_model']
11894 - : 'No OpenRouter model selected';
11895 -
11896 - // Update selected_model display to show both
11897 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11898 - }
11899 -
11900 6644 // Get API key status (just check if they exist, don't expose the keys)
11901 6645 $api_status = [];
11902 6646 $api_status['openai'] = !empty($this->options['api_key']);
11903 6647 $api_status['claude'] = !empty($this->options['claude_api_key']);
@@ -11903,15 +6647,12 @@
11903 6647 $api_status['claude'] = !empty($this->options['claude_api_key']);
11904 6648 $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11905 6649 $api_status['xai'] = !empty($this->options['xai_api_key']);
11906 6650 $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11907 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11908 6651
11909 6652 wp_send_json_success([
11910 6653 'system_prompt' => $system_prompt,
11911 6654 'selected_model' => $selected_model,
11912 - 'is_openrouter' => $is_openrouter,
11913 - 'openrouter_model' => $openrouter_model,
11914 6655 'api_status' => $api_status
11915 6656 ]);
11916 6657 }
11917 6658
@@ -11930,12 +6671,12 @@
11930 6671 wp_send_json_error(['message' => 'Unauthorized']);
11931 6672 return;
11932 6673 }
11933 6674
11934 - // Get similarity threshold from main options (default 35%)
6675 + // Get similarity threshold from main options (default 75%)
11935 6676 $similarity_threshold = isset($this->options['similarity_threshold'])
11936 6677 ? ((int) $this->options['similarity_threshold']) / 100
11937 - : 0.35;
6678 + : 0.75;
11938 6679
11939 6680 wp_send_json_success([
11940 6681 'threshold' => $similarity_threshold,
11941 6682 'threshold_percentage' => ($similarity_threshold * 100) . '%'
@@ -11950,42 +6691,24 @@
11950 6691 if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11951 6692 wp_send_json_error(['message' => 'Invalid nonce']);
11952 6693 return;
11953 6694 }
11954 -
6695 +
11955 6696 // Only allow admin users
11956 6697 if (!current_user_can('administrator')) {
11957 6698 wp_send_json_error(['message' => 'Unauthorized']);
11958 6699 return;
11959 6700 }
11960 -
11961 - // Check OpenAI Vector Store first (takes priority)
11962 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11963 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11964 -
11965 - if ($use_vectorstore) {
11966 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11967 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11968 -
11969 - $kb_info = [
11970 - 'type' => 'OpenAI Vector Store',
11971 - 'status' => 'Active',
11972 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11973 - ];
11974 -
11975 - wp_send_json_success($kb_info);
11976 - return;
11977 - }
11978 -
6701 +
11979 6702 // Check Pinecone vs WordPress
11980 6703 $addon_options = get_option('mxchat_pinecone_addon_options', array());
11981 6704 $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11982 -
6705 +
11983 6706 $kb_info = [
11984 6707 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11985 6708 'status' => 'Active'
11986 6709 ];
11987 -
6710 +
11988 6711 // Get document count
11989 6712 if ($use_pinecone) {
11990 6713 $kb_info['documents'] = 'Connected to Pinecone';
11991 6714 $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
@@ -11995,9 +6718,9 @@
11995 6718 $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11996 6719 $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11997 6720 $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11998 6721 }
11999 -
6722 +
12000 6723 wp_send_json_success($kb_info);
12001 6724 }
12002 6725
12003 6726 /**
@@ -12079,13 +6802,9 @@
12079 6802 // Clear any other session-specific transients
12080 6803 delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
12081 6804 delete_transient("mxchat_include_pdf_in_context_{$session_id}");
12082 6805 delete_transient("mxchat_include_word_in_context_{$session_id}");
12083 -
12084 - // Clear form addon state (pending forms and submitted forms)
12085 - delete_option("mxchat_pending_form_{$session_id}");
12086 - delete_option("mxchat_submitted_forms_{$session_id}");
12087 -
6806 +
12088 6807 //error_log("MxChat: Cleared all data for session: {$session_id}");
12089 6808 }
12090 6809
12091 6810 /**
@@ -12120,15 +6839,15 @@
12120 6839 $testing_data = [
12121 6840 'query' => $message,
12122 6841 'timestamp' => time(),
12123 6842 'top_matches' => [],
12124 - 'action_matches' => [] // Add action matches
6843 + 'action_matches' => [] // NEW: Add action matches
12125 6844 ];
12126 6845
12127 6846 // Get similarity threshold
12128 6847 $similarity_threshold = isset($this->options['similarity_threshold'])
12129 6848 ? ((int) $this->options['similarity_threshold']) / 100
12130 - : 0.35;
6849 + : 0.75;
12131 6850
12132 6851 $testing_data['similarity_threshold'] = $similarity_threshold;
12133 6852
12134 6853 // Use the real similarity analysis if available
@@ -12143,9 +6862,9 @@
12143 6862
12144 6863 $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
12145 6864 }
12146 6865
12147 - // Include action analysis if available
6866 + // NEW: Include action analysis if available
12148 6867 if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
12149 6868 $testing_data['action_matches'] = $this->last_action_analysis;
12150 6869
12151 6870 // Clear it after capturing to avoid stale data
@@ -12156,13 +6875,13 @@
12156 6875 }
12157 6876
12158 6877
12159 6878 /**
12160 - * Track URL clicks from chatbot responses
6879 + * NEW: Track URL clicks from chatbot responses
12161 6880 */
12162 6881 public function mxchat_track_url_click() {
12163 6882 // Verify nonce for security
12164 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
6883 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
12165 6884 wp_send_json_error(['message' => 'Invalid nonce']);
12166 6885 wp_die();
12167 6886 }
12168 6887
@@ -12195,9 +6914,9 @@
12195 6914 wp_die();
12196 6915 }
12197 6916
12198 6917 /**
12199 - * Get URL click analytics for a session
6918 + * NEW: Get URL click analytics for a session
12200 6919 */
12201 6920 public function mxchat_get_url_clicks($session_id) {
12202 6921 global $wpdb;
12203 6922 $table_name = $wpdb->prefix . 'mxchat_url_clicks';
@@ -12209,13 +6928,13 @@
12209 6928
12210 6929 return $clicks;
12211 6930 }
12212 6931 /**
12213 - * Track the originating page where chat was started
6932 + * NEW: Track the originating page where chat was started
12214 6933 */
12215 6934 public function mxchat_track_originating_page() {
12216 6935 // Verify nonce
12217 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
6936 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
12218 6937 wp_send_json_error(['message' => 'Invalid nonce']);
12219 6938 wp_die();
12220 6939 }
12221 6940
@@ -12260,169 +6979,8 @@
12260 6979 wp_send_json_success(['message' => 'Originating page tracked']);
12261 6980 wp_die();
12262 6981 }
12263 6982
12264 -/**
12265 - * Validate and clean URLs from AI response
12266 - * Removes any URLs that aren't in the knowledge base
12267 - *
12268 - * @param string $response_text The AI-generated response
12269 - * @param array $valid_urls Array of URLs from the knowledge base
12270 - * @return string Cleaned response with invalid URLs removed/flagged
12271 - */
12272 -private function validate_and_clean_urls($response_text, $valid_urls) {
12273 - // DEBUG: Log what we're working with
12274 - //error_log("=== MxChat URL Validation Debug ===");
12275 - //error_log("Valid URLs count: " . count($valid_urls));
12276 - //error_log("Valid URLs: " . print_r($valid_urls, true));
12277 - //error_log("Response text length: " . strlen($response_text));
12278 - //error_log("Response text preview: " . substr($response_text, 0, 500));
12279 -
12280 - // If no valid URLs provided or empty response, return as-is
12281 - if (empty($valid_urls) || empty($response_text)) {
12282 - //error_log("Validation skipped - empty valid_urls or response");
12283 - return $response_text;
12284 - }
12285 -
12286 - // Extract all URLs from the AI response
12287 - // This regex matches http:// and https:// URLs
12288 - preg_match_all(
12289 - '#\bhttps?://[^\s<>"\')\]]+#i',
12290 - $response_text,
12291 - $matches
12292 - );
12293 -
12294 - // If no URLs found in response, return as-is
12295 - if (empty($matches[0])) {
12296 - //error_log("No URLs found in response");
12297 - return $response_text;
12298 - }
12299 -
12300 - $found_urls = $matches[0];
12301 - $cleaned_response = $response_text;
12302 - $removed_count = 0;
12303 -
12304 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
12305 - $normalized_valid_urls = array_map(function($url) {
12306 - // Remove trailing slash
12307 - $url = rtrim($url, '/');
12308 - // Remove URL fragments (#section)
12309 - $url = preg_replace('/#.*$/', '', $url);
12310 - // Remove trailing punctuation that might have been captured
12311 - $url = rtrim($url, '.,;:!?');
12312 - return $url;
12313 - }, $valid_urls);
12314 -
12315 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
12316 -
12317 - foreach ($found_urls as $found_url) {
12318 - // Clean up the found URL (remove trailing punctuation that might have been captured)
12319 - $clean_found_url = rtrim($found_url, '.,;:!?)');
12320 -
12321 - // DEBUG: Log each URL being checked
12322 - //error_log("Checking found URL: " . $found_url);
12323 -
12324 - // Normalize for comparison
12325 - $normalized_found = rtrim($clean_found_url, '/');
12326 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
12327 -
12328 - //error_log("Normalized found URL: " . $normalized_found);
12329 -
12330 - // Check if this URL exists in our valid URLs list
12331 - $is_valid = false;
12332 -
12333 - //error_log("Starting validation checks for: " . $normalized_found);
12334 -
12335 - // First, try exact match
12336 - if (in_array($normalized_found, $normalized_valid_urls)) {
12337 - $is_valid = true;
12338 - //error_log("EXACT MATCH FOUND");
12339 - } else {
12340 - //error_log("No exact match, checking variations...");
12341 - // If no exact match, check if it's a variation (with query params, etc.)
12342 - foreach ($normalized_valid_urls as $valid_url) {
12343 - //error_log(" Comparing against valid URL: " . $valid_url);
12344 -
12345 - // Check if the found URL starts with a valid URL (handles query params)
12346 - if (strpos($normalized_found, $valid_url) === 0) {
12347 - // Check what comes after the valid URL
12348 - $remainder = substr($normalized_found, strlen($valid_url));
12349 -
12350 - // Only valid if:
12351 - // 1. Exact match (remainder is empty)
12352 - // 2. Query params (starts with ?)
12353 - // 3. Fragment (starts with #)
12354 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
12355 - $is_valid = true;
12356 - //error_log(" MATCH: Found URL is valid variation of base URL");
12357 - break;
12358 - } else {
12359 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
12360 - }
12361 - }
12362 - // Also check the reverse (in case valid URL has query params)
12363 - if (strpos($valid_url, $normalized_found) === 0) {
12364 - $is_valid = true;
12365 - //error_log(" MATCH: Valid URL starts with found URL");
12366 - break;
12367 - }
12368 - }
12369 -
12370 - if (!$is_valid) {
12371 - //error_log("NO MATCH FOUND - URL should be removed");
12372 - }
12373 - }
12374 -
12375 - // If URL is not valid, remove it from the response
12376 - if (!$is_valid) {
12377 - // Log the removal for debugging
12378 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
12379 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
12380 -
12381 - $removed_count++;
12382 -
12383 - // Check if URL is part of a markdown link: [text](url)
12384 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
12385 - if (preg_match($markdown_pattern, $cleaned_response)) {
12386 - //error_log("Found markdown link, removing but keeping text");
12387 - // Remove the markdown link but keep the text
12388 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
12389 - }
12390 - // Check if URL is part of an HTML link: <a href="url">text</a>
12391 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
12392 - //error_log("Found HTML link, removing but keeping text");
12393 - // Remove the HTML link but keep the text
12394 - $link_text = $link_match[1];
12395 - $cleaned_response = preg_replace(
12396 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
12397 - $link_text,
12398 - $cleaned_response
12399 - );
12400 - }
12401 - // Otherwise just remove the bare URL
12402 - else {
12403 - //error_log("Removing bare URL");
12404 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
12405 - }
12406 - }
12407 - }
12408 -
12409 - // Log summary if any URLs were removed
12410 - if ($removed_count > 0) {
12411 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
12412 - } else {
12413 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
12414 - }
12415 -
12416 - // Clean up any double spaces or awkward punctuation left behind
12417 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
12418 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
12419 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
12420 -
12421 - //error_log("Final cleaned response: " . $cleaned_response);
12422 -
12423 - return trim($cleaned_response);
12424 -}
12425 6983
12426 6984 /**
12427 6985 * AJAX handler to get current chat mode for a session
12428 6986 */
@@ -12427,9 +6985,9 @@
12427 6985 * AJAX handler to get current chat mode for a session
12428 6986 */
12429 6987 public function mxchat_get_current_chat_mode() {
12430 6988 // Verify nonce for security
12431 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
6989 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
12432 6990 wp_send_json_error(['message' => 'Invalid nonce']);
12433 6991 wp_die();
12434 6992 }
12435 6993