PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.1.7
MxChat – AI Chatbot & Content Generation for WordPress v2.1.7
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 +1604 -9758 3.2.122.1.7 View file →
@@ -8,258 +8,28 @@
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 - 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 13
31 14 /**
32 - * Setup streaming headers - call this right before actually streaming
33 - * This delays header setup to allow actions/forms to return JSON responses
15 + * Setup the cron jobs for rate limits
34 16 */
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 -
62 - if (!$enabled) {
63 - return wp_remote_post($url, $args);
17 +public function setup_rate_limit_cron_jobs() {
18 + // Clear previous schedules
19 + wp_clear_scheduled_hook('mxchat_reset_rate_limits');
20 + wp_clear_scheduled_hook('mxchat_reset_hourly_rate_limits');
21 + wp_clear_scheduled_hook('mxchat_reset_daily_rate_limits');
22 + wp_clear_scheduled_hook('mxchat_reset_weekly_rate_limits');
23 + wp_clear_scheduled_hook('mxchat_reset_monthly_rate_limits');
24 +
25 + // Schedule the main rate limit reset check (runs hourly)
26 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
27 + wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
64 28 }
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 29 }
95 30
96 31 /**
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 32 * Class constructor
263 33 */
264 34 public function __construct() {
265 35 $this->options = get_option('mxchat_options');
@@ -266,8 +36,11 @@
266 36 $this->prompts_options = get_option('mxchat_prompts_options', array());
267 37 $this->chat_count = get_option('mxchat_chat_count', 0);
268 38 $this->word_handler = new MXChat_Word_Handler($this->options);
269 39
40 + // Setup the cron jobs for rate limits
41 + $this->setup_rate_limit_cron_jobs();
42 +
270 43 // Add all action hooks
271 44 add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
272 45 add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
273 46 add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
@@ -308,126 +81,11 @@
308 81 add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
309 82 add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
310 83 add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
311 84 add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
312 -
313 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
314 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
315 -
316 - // Testing panel AJAX actions
317 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
318 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
319 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
320 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
321 - // Add to your existing constructor, in the section with other AJAX actions:
322 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
323 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
324 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
325 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
326 - // Add chat mode checking actions
327 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
328 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
329 -
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 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
338 -
339 -
340 85 }
341 86
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 87
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 -// In your core plugin's check_actions_for_addons method:
420 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
421 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
422 -
423 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
424 -
425 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
426 -
427 - return $result;
428 -}
429 -
430 88 private function mxchat_increment_chat_count() {
431 89 $chat_count = get_option('mxchat_chat_count', 0);
432 90 $chat_count++;
433 91 update_option('mxchat_chat_count', $chat_count);
@@ -439,22 +97,8 @@
439 97 wp_die();
440 98 }
441 99
442 100 $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 101 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
458 102 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
459 103
460 104 if (empty($history)) {
@@ -471,25 +115,26 @@
471 115 'chat_mode' => $chat_mode
472 116 ]);
473 117 wp_die();
474 118 }
475 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
476 - $history = get_option("mxchat_history_{$session_id}", []);
119 +private function mxchat_fetch_conversation_history_for_ajax($session_id) {
120 + $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history based on session ID
121 + $formatted_history = [];
477 122
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';
123 + // Format the history to align with the expected structure for OpenAI
124 + foreach ($history as $entry) {
125 + $formatted_history[] = [
126 + 'role' => $entry['role'], // Ensure this matches 'user' or 'assistant'
127 + 'content' => $entry['content']
128 + ];
129 + }
481 130
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 - }
131 + return $formatted_history;
132 +}
491 133
134 +
135 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
136 + $history = get_option("mxchat_history_{$session_id}", []);
492 137 $formatted_history = [];
493 138
494 139 // Adjusted for code-heavy conversations
495 140 $max_tokens = 120000; // Context window size
@@ -524,9 +169,9 @@
524 169 if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
525 170 continue;
526 171 }
527 172
528 - // More accurate token estimation (1 token ≈ 4 characters)
173 + // More accurate token estimation (1 token ≈ 4 characters)
529 174 $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
530 175
531 176 // Check token budget with the new estimate
532 177 if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
@@ -563,17 +208,8 @@
563 208
564 209 public function register_routes() {
565 210 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
566 211
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 212 register_rest_route('mxchat/v1', '/stream', [
577 213 'methods' => 'GET',
578 214 'callback' => [$this, 'mxchat_stream_events'],
579 215 'permission_callback' => [$this, 'verify_chat_session'],
@@ -589,112 +225,13 @@
589 225 'methods' => 'POST',
590 226 'callback' => [$this, 'handle_slack_interaction'],
591 227 'permission_callback' => [$this, 'verify_slack_request'],
592 228 ]);
593 -
594 - register_rest_route('mxchat/v1', '/slack-messages', [
595 - 'methods' => 'POST',
596 - 'callback' => [$this, 'handle_slack_messages'],
597 - 'permission_callback' => [$this, 'verify_slack_request'],
598 - ]);
599 229
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 230 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
608 231 }
609 232
610 233 /**
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 234 * Verify valid chat session
698 235 */
699 236 public function verify_chat_session($request) {
700 237 $session_id = $request->get_param('session_id');
@@ -730,11 +267,10 @@
730 267 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
731 268 return false;
732 269 }
733 270
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();
271 + // Get raw request body
272 + $request_body = file_get_contents('php://input');
737 273
738 274 // Create the signature base string
739 275 $sig_basestring = "v0:{$timestamp}:{$request_body}";
740 276
@@ -743,43 +279,8 @@
743 279
744 280 // Compare signatures
745 281 return hash_equals($my_signature, $slack_signature);
746 282 }
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 283 public function mxchat_stream_events(WP_REST_Request $request) {
783 284 header('Content-Type: text/event-stream');
784 285 header('Cache-Control: no-cache');
785 286 header('Connection: keep-alive');
@@ -813,45 +314,20 @@
813 314
814 315
815 316
816 317
817 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
318 +private function mxchat_save_chat_message($session_id, $role, $message) {
818 319 global $wpdb;
320 +
819 321 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
820 322 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
821 -
822 - // Check if this is the first message in a new session (before any other database operations)
823 - $is_new_session = false;
824 - if ($role === 'user') { // Only check for user messages, not bot responses
825 - $existing_messages = $wpdb->get_var($wpdb->prepare(
826 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
827 - $session_id
828 - ));
829 - $is_new_session = ($existing_messages == 0);
830 -
831 - // Log for debugging
832 - if ($is_new_session) {
833 - //error_log("[DEBUG] This is a NEW session - first message");
834 - }
835 - }
836 -
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 -
323 +
849 324 // 1) Extract agent name if present
850 325 $agent_name = '';
851 326 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
852 327 $agent_name = $matches[1];
853 328 $message = str_replace("Agent: $agent_name - ", '', $message);
329 +
854 330 $session_meta_key = "mxchat_agent_name_{$session_id}";
855 331 if (empty(get_option($session_meta_key))) {
856 332 update_option($session_meta_key, $agent_name);
857 333 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
@@ -856,57 +332,42 @@
856 332 update_option($session_meta_key, $agent_name);
857 333 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
858 334 }
859 335 }
860 -
336 +
861 337 // 2) Generate unique message_id
862 338 $message_id = uniqid();
863 339 //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
864 -
340 +
865 341 // 3) Determine user_id
866 342 $user_id = is_user_logged_in() ? get_current_user_id() : 0;
867 -
343 +
868 344 // 4) Determine user_identifier
869 345 $user_identifier = $agent_name
870 346 ? $agent_name
871 347 : MxChat_User::mxchat_get_user_identifier();
872 -
348 +
873 349 // 5) Determine displayed_name
874 350 $user_email = MxChat_User::mxchat_get_user_email();
875 351 $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
876 -
352 +
877 353 // 6) Check for a saved email in wp_options
878 354 $email_option_key = "mxchat_email_{$session_id}";
879 355 $saved_email = get_option($email_option_key);
880 356 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
881 -
882 - // Check for a saved name in wp_options
883 - $name_option_key = "mxchat_name_{$session_id}";
884 - $saved_name = get_option($name_option_key);
885 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
886 -
887 - // If found, update DB user_email and user_name
888 - if ($saved_email || $saved_name) {
889 - $update_data = [];
890 - if ($saved_email) {
891 - $update_data['user_email'] = $saved_email;
892 - }
893 - if ($saved_name) {
894 - $update_data['user_name'] = $saved_name;
895 - }
896 -
897 - if (!empty($update_data)) {
898 - $update_res = $wpdb->update(
899 - $table_name,
900 - $update_data,
901 - ['session_id' => $session_id],
902 - array_fill(0, count($update_data), '%s'),
903 - ['%s']
904 - );
905 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
906 - }
357 +
358 + // If found, update DB user_email
359 + if ($saved_email) {
360 + $update_res = $wpdb->update(
361 + $table_name,
362 + ['user_email' => $saved_email],
363 + ['session_id' => $session_id],
364 + ['%s'],
365 + ['%s']
366 + );
367 + //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email update for session_id {$session_id}. update_res: {$update_res}");
907 368 }
908 -
369 +
909 370 // 7) Save to session history in wp_options
910 371 $history_key = "mxchat_history_{$session_id}";
911 372 $history = get_option($history_key, []);
912 373 $history[] = [
@@ -915,351 +376,33 @@
915 376 'content' => $message,
916 377 'timestamp' => round(microtime(true) * 1000),
917 378 'agent_name' => $displayed_name,
918 379 ];
919 - update_option($history_key, $history, 'no');
380 + update_option($history_key, $history);
920 381 //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
921 -
382 +
922 383 // 8) Save the message to DB (INSERT)
923 384 $insert_data = [
924 385 'user_id' => $user_id,
925 386 'user_identifier'=> $user_identifier,
926 387 'user_email' => $saved_email ?: $user_email,
927 - 'user_name' => $saved_name ?: '', // Add name to insert data
928 388 'session_id' => $session_id,
929 389 'role' => $role,
930 390 'message' => $message,
931 391 'timestamp' => current_time('mysql', 1),
932 392 ];
933 -
934 - // IMPROVED: Handle originating page data
935 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
936 -
937 - if ($columns_exist) {
938 - if ($is_new_session && $role === 'user') {
939 - // For the first user message, set originating page data
940 -
941 - // First check if we have it from the parameter
942 - if ($originating_page && !empty($originating_page['url'])) {
943 - $insert_data['originating_page_url'] = $originating_page['url'];
944 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
945 -
946 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
947 - }
948 - // Otherwise check if it's stored in the instance property
949 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
950 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
951 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
952 -
953 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
954 -
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;
958 - }
959 - // Fallback to HTTP_REFERER if nothing else is available
960 - else if (isset($_SERVER['HTTP_REFERER'])) {
961 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
962 - $insert_data['originating_page_url'] = $referer_url;
963 -
964 - // Generate title from URL
965 - $parsed_url = parse_url($referer_url);
966 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
967 -
968 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
969 - $insert_data['originating_page_title'] = 'Homepage';
970 - } else {
971 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
972 - $insert_data['originating_page_title'] = ucwords(trim($title));
973 - }
974 -
975 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
976 - }
977 -
978 - // Store for this session so all messages have the same originating page
979 - if (!empty($insert_data['originating_page_url'])) {
980 - update_option("mxchat_originating_page_{$session_id}", [
981 - 'url' => $insert_data['originating_page_url'],
982 - 'title' => $insert_data['originating_page_title']
983 - ], 'no');
984 - }
985 - } else {
986 - // For subsequent messages in the session, use the stored originating page
987 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
988 - if ($stored_originating && !empty($stored_originating['url'])) {
989 - $insert_data['originating_page_url'] = $stored_originating['url'];
990 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
991 - }
992 - }
993 - }
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 -
1003 393 $wpdb->insert($table_name, $insert_data);
1004 394 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
1005 -
1006 - // 9) Send notification email if this is the first user message in a new session
1007 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
1008 - $this->send_new_chat_notification($session_id, array(
1009 - 'identifier' => $user_identifier,
1010 - 'email' => $saved_email ?: $user_email,
1011 - 'ip' => $_SERVER['REMOTE_ADDR']
1012 - ));
1013 - }
1014 -
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 -
395 +
1020 396 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1021 397 return $message_id;
1022 398 }
1023 399
1024 -private function send_new_chat_notification($session_id, $user_info = array()) {
1025 - $options = get_option('mxchat_transcripts_options');
1026 -
1027 - // Check if notifications are enabled
1028 - if (empty($options['mxchat_enable_notifications'])) {
1029 - return false;
1030 - }
1031 -
1032 - // Get notification email
1033 - $to = !empty($options['mxchat_notification_email']) ?
1034 - $options['mxchat_notification_email'] :
1035 - get_option('admin_email');
1036 -
1037 - if (!is_email($to)) {
1038 - return false;
1039 - }
1040 -
1041 - // Prepare email content
1042 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1043 -
1044 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1045 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1046 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1047 -
1048 - $message = sprintf(
1049 - "A new chat session has started on your website.\n\n" .
1050 - "Session ID: %s\n" .
1051 - "User: %s\n" .
1052 - "Email: %s\n" .
1053 - "IP Address: %s\n" .
1054 - "Time: %s\n\n" .
1055 - "View transcripts: %s",
1056 - $session_id,
1057 - $user_identifier,
1058 - $user_email,
1059 - $user_ip,
1060 - current_time('mysql'),
1061 - admin_url('admin.php?page=mxchat-transcripts')
1062 - );
1063 -
1064 - // Send email
1065 - return wp_mail($to, $subject, $message);
1066 -}
1067 -
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 400 public function mxchat_handle_save_email_and_response() {
1255 401 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1256 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1257 402
1258 - nocache_headers();
1259 -
1260 403 // Validate nonce
1261 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
404 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1262 405 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1263 406 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1264 407 wp_die();
1265 408 }
@@ -1265,41 +408,22 @@
1265 408 }
1266 409
1267 410 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1268 411 $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1269 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1270 412
1271 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
413 + //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}");
1272 414
1273 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
415 + if (empty($session_id) || empty($email)) {
1274 416 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1275 417 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1276 418 wp_die();
1277 419 }
1278 420
1279 - // Validate name if provided (check if name field is enabled and name is required)
1280 - $options = get_option('mxchat_options', []);
1281 - $name_field_enabled = isset($options['enable_name_field']) &&
1282 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1283 -
1284 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1285 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1286 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1287 - wp_die();
1288 - }
421 + // 1) Always store in wp_options
422 + $option_key = "mxchat_email_{$session_id}";
423 + update_option($option_key, $email);
424 + //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$option_key} => {$email}");
1289 425
1290 - // 1) Always store email in wp_options
1291 - $email_option_key = "mxchat_email_{$session_id}";
1292 - update_option($email_option_key, $email, 'no');
1293 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1294 -
1295 - // Store name in wp_options if provided
1296 - if (!empty($name)) {
1297 - $name_option_key = "mxchat_name_{$session_id}";
1298 - update_option($name_option_key, $name, 'no');
1299 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1300 - }
1301 -
1302 426 // 2) (Optional) Also store in DB if a row already exists
1303 427 global $wpdb;
1304 428 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1305 429
@@ -1309,30 +433,21 @@
1309 433
1310 434 //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1311 435
1312 436 if ($session_count) {
1313 - // Update both user_email and user_name if row(s) exist
1314 - if (!empty($name)) {
1315 - $update_sql = $wpdb->prepare(
1316 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1317 - $email,
1318 - $name,
1319 - $session_id
1320 - );
1321 - } else {
1322 - $update_sql = $wpdb->prepare(
1323 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1324 - $email,
1325 - $session_id
1326 - );
1327 - }
437 + // Update user_email if row(s) exist
438 + $update_sql = $wpdb->prepare(
439 + "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
440 + $email,
441 + $session_id
442 + );
1328 443 $wpdb->query($update_sql);
1329 444 //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1330 445 } else {
1331 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
446 + //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email is only in wp_options.");
1332 447 }
1333 448
1334 - // Provide success response (same as original)
449 + // Provide success response
1335 450 $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1336 451 //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1337 452 wp_send_json_success(['message' => $bot_message]);
1338 453 wp_die();
@@ -1340,17 +455,15 @@
1340 455
1341 456 public function mxchat_check_email_provided() {
1342 457 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1343 458
1344 - nocache_headers();
1345 -
1346 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
459 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1347 460 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1348 461 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1349 462 }
1350 463
1351 464 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1352 - if (empty($session_id) || $session_id === 'null') {
465 + if (empty($session_id)) {
1353 466 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1354 467 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1355 468 }
1356 469
@@ -1357,82 +470,61 @@
1357 470 // Check if the user is logged in
1358 471 if (is_user_logged_in()) {
1359 472 $current_user = wp_get_current_user();
1360 473 //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1361 -
1362 - // Get user's display name for logged in users
1363 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1364 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1365 -
1366 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1367 - if (!empty($user_name)) {
1368 - $response_data['name'] = $user_name;
1369 - }
1370 -
1371 - wp_send_json_success($response_data);
474 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
1372 475 }
1373 476
1374 - // Check if name field is required
1375 - $options = get_option('mxchat_options', []);
1376 - $name_field_enabled = isset($options['enable_name_field']) &&
1377 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
477 + $option_key = "mxchat_email_{$session_id}";
478 + $stored_email = get_option($option_key, '');
1378 479
1379 - $email_option_key = "mxchat_email_{$session_id}";
1380 - $stored_email = get_option($email_option_key, '');
1381 -
1382 - // Check for stored name
1383 - $name_option_key = "mxchat_name_{$session_id}";
1384 - $stored_name = get_option($name_option_key, '');
480 + //error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
1385 481
1386 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1387 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1388 -
1389 - // Check if we have email and name (if name is required)
1390 - $has_required_info = !empty($stored_email);
1391 -
1392 - if ($name_field_enabled) {
1393 - $has_required_info = $has_required_info && !empty($stored_name);
1394 - }
1395 -
1396 - if ($has_required_info) {
1397 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1398 -
1399 - $response_data = ['email' => $stored_email];
1400 - if (!empty($stored_name)) {
1401 - $response_data['name'] = $stored_name;
1402 - }
1403 -
1404 - wp_send_json_success($response_data);
482 + if (!empty($stored_email)) {
483 + //error_log("[DEBUG] mxchat_check_email_provided -> Email found, returning success");
484 + wp_send_json_success(['email' => $stored_email]);
1405 485 } else {
1406 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
486 + //error_log("[DEBUG] mxchat_check_email_provided -> No email found, returning error");
1407 487 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1408 488 }
1409 489 }
1410 490
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
491 +// Add this to your plugin's main PHP file
492 +public function mxchat_check_new_messages() {
493 + if (!isset($_POST['session_id']) || !isset($_POST['last_seen_id'])) {
494 + wp_send_json_error(['message' => 'Missing required parameters']);
495 + wp_die();
496 + }
497 +
498 + $session_id = sanitize_text_field($_POST['session_id']);
499 + $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
500 +
501 + // Get chat history
502 + $history = get_option("mxchat_history_{$session_id}", []);
503 +
504 + if (empty($history)) {
505 + wp_send_json_success([
506 + 'hasNewMessages' => false,
507 + 'new_messages' => []
1433 508 ]);
509 + wp_die();
1434 510 }
511 +
512 + // Filter new messages
513 + $new_messages = array_filter($history, function($message) use ($last_seen_id) {
514 + return isset($message['id']) && $message['id'] > $last_seen_id;
515 + });
516 +
517 + // Sort by ID to ensure proper order
518 + usort($new_messages, function($a, $b) {
519 + return $a['id'] <=> $b['id'];
520 + });
521 +
522 + wp_send_json_success([
523 + 'hasNewMessages' => !empty($new_messages),
524 + 'new_messages' => array_values($new_messages),
525 + 'latestMessageId' => end($new_messages)['id'] ?? $last_seen_id
526 + ]);
1435 527 wp_die();
1436 528 }
1437 529
1438 530 public function mxchat_handle_chat_request() {
@@ -1437,30 +529,10 @@
1437 529
1438 530 public function mxchat_handle_chat_request() {
1439 531 global $wpdb;
1440 532
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);
1445 -
1446 - // Get bot-specific options
1447 - $bot_options = $this->get_bot_options($bot_id);
1448 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1449 533
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 - // Check if MX Chat Moderation is active
534 + // Check if MX Chat Moderation is active
1463 535 if (class_exists('MX_Chat_Moderation')) {
1464 536 // Get user email and IP
1465 537 $user_email = '';
1466 538 $user_ip = $_SERVER['REMOTE_ADDR'];
@@ -1494,373 +566,184 @@
1494 566 wp_die();
1495 567 }
1496 568 }
1497 569
1498 - $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1499 - $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;
570 +$this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
571 +$this->productCardHtml = '';
1504 572
1505 - // Get the actual WordPress user ID if logged in
1506 - $is_logged_in = is_user_logged_in();
1507 - if ($is_logged_in) {
1508 - $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1509 - } else {
1510 - // For logged-out users, use your existing identifier method
1511 - $user_id = $this->mxchat_get_user_identifier();
1512 - }
573 +// Get the actual WordPress user ID if logged in
574 +$is_logged_in = is_user_logged_in();
575 +if ($is_logged_in) {
576 + $user_id = get_current_user_id(); // This will get the actual WordPress user ID
577 +} else {
578 + // For logged-out users, use your existing identifier method
579 + $user_id = $this->mxchat_get_user_identifier();
580 +}
1513 581
1514 - // Get and sanitize the user identifier
1515 - $user_id = sanitize_key($user_id);
582 +// Get and sanitize the user identifier
583 +$user_id = sanitize_key($user_id);
1516 584
1517 - // Check rate limit using new settings structure
1518 - $rate_limit_result = $this->check_rate_limit();
585 +// Check rate limit using new settings structure
586 +$rate_limit_result = $this->check_rate_limit();
1519 587
1520 - if ($rate_limit_result !== true) {
1521 - wp_send_json([
1522 - 'success' => false,
1523 - 'message' => $rate_limit_result['message'],
1524 - 'status' => 'rate_limit_exceeded'
1525 - ]);
1526 - wp_die();
1527 - }
588 +// Add this at the start of your rate limit checking in mxchat_handle_chat_request()
589 +//error_log('MXChat Rate Limit: Starting rate limit check in handle_chat_request()');
1528 590
591 +// Then right after checking the result:
592 +if ($rate_limit_result !== true) {
593 + //error_log('MXChat Rate Limit: Rate limit exceeded, returning error');
594 + wp_send_json([
595 + 'success' => false,
596 + 'message' => $rate_limit_result['message'],
597 + 'status' => 'rate_limit_exceeded'
598 + ]);
599 + wp_die();
600 +} else {
601 + //error_log('MXChat Rate Limit: Check passed successfully');
602 +}
603 +
1529 604 // Rest of your existing code...
1530 605 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
606 + //error_log("Session ID: $session_id");
1531 607
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 608 if (empty($session_id)) {
609 + //error_log("Error: Session ID is missing.");
1542 610 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1543 611 wp_die();
1544 612 }
1545 613
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 614 // Validate and sanitize the incoming message
1556 615 if (empty($_POST['message'])) {
616 + //error_log("Error: No message received.");
1557 617 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1558 618 wp_die();
1559 619 }
1560 620
1561 - // Enforce the configurable max input length (plan a3fae2 part C). 0 = unlimited.
1562 - // Server-side guard backing the textarea's client-side maxlength (which is bypassable).
1563 - // Reads the global core setting and measures characters (mb_strlen on the unslashed
1564 - // raw POST), matching the maxlength semantics.
1565 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
1566 - if ($mxchat_max_input_length > 0) {
1567 - $mxchat_incoming_raw = is_string($_POST['message']) ? wp_unslash($_POST['message']) : '';
1568 - if (mb_strlen($mxchat_incoming_raw) > $mxchat_max_input_length) {
1569 - wp_send_json([
1570 - 'success' => false,
1571 - /* translators: %d: maximum allowed characters */
1572 - 'message' => sprintf(esc_html__('Your message is too long. Please keep it under %d characters.', 'mxchat'), $mxchat_max_input_length),
1573 - 'status' => 'message_too_long'
1574 - ]);
1575 - wp_die();
1576 - }
1577 - }
1578 621
622 +// Modify the message sanitization to preserve PHP tags in code blocks
623 +$allowed_tags = [
624 + 'pre' => [],
625 + 'code' => ['class' => true],
626 + 'span' => ['class' => true],
627 + 'div' => ['class' => true],
628 +];
1579 629
1580 - // Track originating page for first message in session
1581 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
630 +// First preserve code blocks
631 +$message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
632 + return htmlspecialchars_decode($matches[0]);
633 +}, $_POST['message']);
1582 634
1583 - // Check if originating page columns exist
1584 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
635 +// Then apply sanitization
636 +$message = wp_kses($message, $allowed_tags);
1585 637
1586 - if ($columns_exist) {
1587 - // Check if this session already has messages
1588 - $message_count = $wpdb->get_var($wpdb->prepare(
1589 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1590 - $session_id
1591 - ));
1592 -
1593 - // If this is the first message in the session
1594 - if ($message_count == 0) {
1595 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1596 - $originating_url = '';
1597 - $originating_title = '';
1598 -
1599 - // Try to get from POST data first (sent by JavaScript)
1600 - if (isset($_POST['current_page_url'])) {
1601 - $originating_url = esc_url_raw($_POST['current_page_url']);
1602 - $originating_title = isset($_POST['current_page_title'])
1603 - ? sanitize_text_field($_POST['current_page_title'])
1604 - : '';
1605 - }
1606 - // Fallback to HTTP_REFERER if not provided by JavaScript
1607 - else if (isset($_SERVER['HTTP_REFERER'])) {
1608 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1609 - }
1610 -
1611 - // Generate title if we have URL but no title
1612 - if ($originating_url && empty($originating_title)) {
1613 - $parsed_url = parse_url($originating_url);
1614 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1615 -
1616 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1617 - $originating_title = 'Homepage';
1618 - } else {
1619 - // Clean up the path to make a readable title
1620 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1621 - $originating_title = ucwords(trim($originating_title));
1622 - }
1623 - }
1624 -
1625 - // Store for later use when saving the message
1626 - $this->pending_originating_page = [
1627 - 'url' => $originating_url,
1628 - 'title' => $originating_title
1629 - ];
1630 - }
1631 - }
1632 -
1633 -
638 +// Decode code blocks
639 +$message = preg_replace_callback('/(&lt;pre&gt;&lt;code.*?&gt;.*?&lt;\/code&gt;&lt;\/pre&gt;)/s', function($matches) {
640 + return htmlspecialchars_decode($matches[1]);
641 +}, $message);
1634 642
1635 - // Get page context if provided
1636 - $page_context = null;
1637 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1638 - $page_context_raw = stripslashes($_POST['page_context']);
1639 - $page_context = json_decode($page_context_raw, true);
1640 -
1641 - // Validate page context structure
1642 - if (is_array($page_context) &&
1643 - isset($page_context['url']) &&
1644 - isset($page_context['title']) &&
1645 - isset($page_context['content'])) {
1646 -
1647 - // Sanitize page context
1648 - $page_context['url'] = esc_url_raw($page_context['url']);
1649 - $page_context['title'] = sanitize_text_field($page_context['title']);
1650 - $page_context['content'] = wp_kses_post($page_context['content']);
1651 - } else {
1652 - $page_context = null;
1653 - }
1654 - }
643 +$message = trim($message);
1655 644
1656 - // Modify the message sanitization to preserve PHP tags in code blocks
1657 - $allowed_tags = [
1658 - 'pre' => [],
1659 - 'code' => ['class' => true],
1660 - 'span' => ['class' => true],
1661 - 'div' => ['class' => true],
1662 - ];
645 +// Preserve code blocks from markdown conversion
646 +$message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1663 647
1664 - // First preserve code blocks
1665 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1666 - return htmlspecialchars_decode($matches[0]);
1667 - }, $_POST['message']);
648 +// Check if any add-ons want to pre-process this message (for web search etc.)
649 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1668 650
1669 - // Then apply sanitization
1670 - $message = wp_kses($message, $allowed_tags);
651 +// If the pre-processing returned a result (not the original message), use it directly
652 +if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
653 + // Save the AI response
654 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
655 +
656 + // Save HTML content if provided
657 + if (!empty($pre_processed_result['html'])) {
658 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
659 + }
660 +
661 + // Return the response
662 + wp_send_json([
663 + 'text' => $pre_processed_result['text'],
664 + 'html' => $pre_processed_result['html'] ?? '',
665 + 'session_id' => $session_id
666 + ]);
667 + wp_die();
668 +}
1671 669
1672 - // Preserve code blocks from markdown conversion
1673 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1674 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
670 + // Save the user's message
671 + $this->mxchat_save_chat_message($session_id, 'user', $message);
1675 672
1676 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1677 - // Always initialize testing data for admins (no toggle needed)
1678 - $testing_data = null;
1679 - if (current_user_can('administrator')) {
1680 - // For vision messages, use the original user message for the query display
1681 - $query_for_testing = $message;
1682 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1683 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1684 - }
1685 -
1686 - $testing_data = [
1687 - 'query' => $query_for_testing,
1688 - 'timestamp' => time(),
1689 - 'top_matches' => [],
1690 - 'action_matches' => [], // Initialize action matches array
1691 - 'page_context' => $page_context, // Include page context in testing data
1692 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1693 - 'bot_id' => $bot_id // Include bot ID in testing data
1694 - ];
1695 -
1696 - // Get similarity threshold from bot options or default options
1697 - $similarity_threshold = isset($current_options['similarity_threshold'])
1698 - ? ((int) $current_options['similarity_threshold']) / 100
1699 - : 0.35;
1700 -
1701 - $testing_data['similarity_threshold'] = $similarity_threshold;
1702 -
1703 - // Determine knowledge base type using bot-specific config
1704 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1705 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1706 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1707 - }
1708 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
673 + // Check if the message is an email address
674 + if (is_email($message)) {
675 + // Add the email to Loops
676 + $this->add_email_to_loops($message);
1709 677
1710 - // Add debug before and after:
1711 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1712 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1713 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
678 + // Send success response
679 + $response_message = $this->options['email_capture_response'] ??
680 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
1714 681
682 + wp_send_json([
683 + 'success' => true,
684 + 'status' => 'email_captured',
685 + 'message' => $response_message
686 + ]);
687 + wp_die();
688 + }
1715 689
1716 - // If the pre-processing returned a result (not the original message), use it directly
1717 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1718 - // Save the AI response
1719 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1720 -
1721 - // Save HTML content if provided
1722 - if (!empty($pre_processed_result['html'])) {
1723 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1724 - }
1725 -
1726 - // Add testing data if admin
1727 - $response_data = [
1728 - 'text' => $pre_processed_result['text'],
1729 - 'html' => $pre_processed_result['html'] ?? '',
1730 - 'session_id' => $session_id
1731 - ];
1732 -
1733 - if ($testing_data !== null) {
1734 - $response_data['testing_data'] = $testing_data;
1735 - }
1736 -
1737 - wp_send_json($response_data);
1738 - wp_die();
1739 - }
690 + $intent_info = '';
1740 691
1741 - // Save the user's message - handle vision processed messages differently
1742 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1743 - // For vision messages, save the original user message with image indicator
1744 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1745 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1746 - $image_count = intval($_POST['vision_images_count']);
1747 - $original_message .= " [{$image_count} image(s)]";
1748 - }
1749 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1750 - } else {
1751 - // Regular message - save as normal
1752 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1753 - }
692 + // Check chat mode
693 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
694 + //error_log("Chat Mode: $chat_mode");
1754 695
1755 -
1756 - if (is_email($message)) {
1757 - // Add the email to Loops
1758 - $this->add_email_to_loops($message);
1759 -
1760 - // Get the user's success message instruction using current_options
1761 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1762 -
1763 - // Set instruction for AI using the user's success message
1764 - $this->current_action_instruction = $user_success_message;
1765 -
1766 - // Clear the email capture transient since we got the email
1767 - delete_transient('mxchat_email_capture_' . $user_id);
1768 - }
1769 -
1770 - // Check if we're in an email capture flow but user hasn't provided email yet
1771 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1772 - // Check if the message contains an email (not the whole message being an email)
1773 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1774 - $extracted_email = $matches[0];
1775 -
1776 - // Add the extracted email to Loops
1777 - $this->add_email_to_loops($extracted_email);
1778 -
1779 - // Get the user's success message instruction using current_options
1780 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1781 -
1782 - // Set instruction for AI using the user's success message
1783 - $this->current_action_instruction = $user_success_message;
1784 -
1785 - // Clear the email capture transient since we got the email
1786 - delete_transient('mxchat_email_capture_' . $user_id);
1787 - }
1788 - // If no email found but we're in capture mode, remind them
1789 - else {
1790 - // Get the original instruction to remind them using current_options
1791 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1792 - $this->current_action_instruction = $original_instruction;
1793 - }
1794 - }
696 + // Handle agent mode
697 + if ($chat_mode === 'agent') {
698 + // First, check for switch intent before doing anything else
699 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1795 700
1796 - $intent_info = '';
701 + // If we matched an intent and it's the switch intent, handle it
702 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
703 + //error_log("Switch to chatbot intent detected");
1797 704
1798 - // Check chat mode
1799 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
705 + // Update chat mode first
706 + update_option("mxchat_mode_{$session_id}", 'ai');
1800 707
1801 - // Handle agent mode
1802 - // Handle agent mode
1803 - if ($chat_mode === 'agent') {
1804 - // First, check for switch intent before doing anything else
1805 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
708 + // Clear any existing PDF context to start fresh
709 + $this->clear_pdf_transients($session_id);
1806 710
1807 - // Capture action analysis for testing panel after intent check
1808 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1809 - $testing_data['action_matches'] = $this->last_action_analysis;
1810 - }
1811 -
1812 - // Around line 506, in the agent mode handling section:
1813 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1814 - // Update chat mode first
1815 - update_option("mxchat_mode_{$session_id}", 'ai');
1816 -
1817 - // Clear any existing PDF context to start fresh
1818 - $this->clear_pdf_transients($session_id);
1819 -
1820 - // Prepare clean switch response with explicit chat_mode
1821 - $response_data = [
1822 - 'text' => $this->fallbackResponse['text'],
1823 - 'html' => $this->fallbackResponse['html'] ?? '',
1824 - 'session_id' => $session_id,
1825 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1826 - ];
1827 -
1828 - if ($testing_data !== null) {
1829 - $response_data['testing_data'] = $testing_data;
1830 - }
1831 -
1832 - // Save the mode switch message
1833 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1834 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1835 -
1836 - // Send response and exit
1837 - wp_send_json($response_data);
1838 - wp_die();
1839 - } elseif (!$intent_matched) {
1840 - // No intent matched, handle live agent message
1841 - try {
1842 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
711 + // Prepare clean switch response
712 + $response_data = [
713 + 'text' => $this->fallbackResponse['text'],
714 + 'html' => '',
715 + 'session_id' => $session_id,
716 + 'chat_mode' => 'ai'
717 + ];
1843 718
1844 - $agent_response = [
1845 - 'status' => 'waiting_for_agent',
1846 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1847 - ];
1848 -
1849 - if ($testing_data !== null) {
1850 - $agent_response['testing_data'] = $testing_data;
1851 - }
719 + // Save the mode switch message
720 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
721 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1852 722
1853 - wp_send_json_success($agent_response);
1854 - } catch (\Exception $e) {
1855 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1856 - }
1857 - wp_die();
723 + // Send response and exit
724 + wp_send_json($response_data);
725 + wp_die();
726 + } elseif (!$intent_matched) {
727 + // No intent matched, handle live agent message
728 + try {
729 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
730 + //error_log("Message sent to agent.");
731 +
732 + wp_send_json_success([
733 + 'status' => 'waiting_for_agent',
734 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
735 + ]);
736 + } catch (\Exception $e) {
737 + //error_log("Error sending message to agent: " . $e->getMessage());
738 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1858 739 }
740 + wp_die();
1859 741 }
742 + }
1860 743
1861 744 // Step 1: Check for new PDF URL in the message
1862 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
745 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1863 746 $new_pdf_url = $matches[0];
1864 747
1865 748 // Check if this is likely a PDF-related request
1866 749 $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
@@ -1882,15 +765,15 @@
1882 765
1883 766 // Clear previous PDF transients
1884 767 $this->clear_pdf_transients($session_id);
1885 768
1886 - // Process new PDF using current_options
1887 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
769 + // Process new PDF
770 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
1888 771 $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1889 772
1890 773 if ($embeddings === 'too_many_pages') {
1891 774 $error_text = sprintf(
1892 - $current_options['pdf_intent_error_text'] ??
775 + $this->options['pdf_intent_error_text'] ??
1893 776 esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1894 777 $max_pages
1895 778 );
1896 779 $this->fallbackResponse['text'] = $error_text;
@@ -1895,13 +778,15 @@
1895 778 );
1896 779 $this->fallbackResponse['text'] = $error_text;
1897 780 } elseif ($embeddings) {
1898 781 // Store new PDF information
782 + // Create a more meaningful filename from URL
1899 783 $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1900 784
1901 - // If the filename is generic, create a more descriptive one
785 + // If the filename is generic (like results_download.php), create a more descriptive one
1902 786 if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1903 787 strpos($pdf_filename, '.php') !== false) {
788 + // Create a timestamp-based name
1904 789 $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1905 790 }
1906 791
1907 792 set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
@@ -1908,257 +793,155 @@
1908 793 set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1909 794 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1910 795 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1911 796
1912 - $success_text = $current_options['pdf_intent_success_text'] ??
797 + $success_text = $this->options['pdf_intent_success_text'] ??
1913 798 esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1914 799
1915 - $pdf_response = [
800 + // Return success with filename for UI update
801 + wp_send_json([
1916 802 'success' => true,
1917 803 'message' => $success_text,
1918 804 'data' => [
1919 805 'filename' => $pdf_filename
1920 806 ]
1921 - ];
1922 -
1923 - if ($testing_data !== null) {
1924 - $pdf_response['testing_data'] = $testing_data;
1925 - }
1926 -
1927 - wp_send_json($pdf_response);
807 + ]);
1928 808 wp_die();
1929 809 } else {
1930 - $error_text = $current_options['pdf_intent_error_text'] ??
810 + $error_text = $this->options['pdf_intent_error_text'] ??
1931 811 esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1932 812 $this->fallbackResponse['text'] = $error_text;
1933 813 }
1934 814
1935 - $pdf_error_response = [
815 + wp_send_json([
1936 816 'success' => false,
1937 817 'message' => $this->fallbackResponse['text']
1938 - ];
1939 -
1940 - if ($testing_data !== null) {
1941 - $pdf_error_response['testing_data'] = $testing_data;
1942 - }
1943 -
1944 - wp_send_json($pdf_error_response);
818 + ]);
1945 819 wp_die();
1946 820 }
1947 821 }
1948 822 }
1949 -
1950 -
1951 - // Step 2: Detect intent and handle intent-based responses
1952 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1953 -
1954 - // Capture action analysis for testing panel after intent check
1955 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1956 - $testing_data['action_matches'] = $this->last_action_analysis;
823 +
824 +
825 +// Add this before the intent check section (before Step 2) in mxchat_handle_chat_request
826 +// Check if there's an active recommendation flow session
827 +$flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
828 +if (!empty($flow_state) && isset($flow_state['flow_id'])) {
829 + //error_log('MXCHAT DEBUG: Detected active recommendation flow, routing directly');
830 +
831 + // Create a dummy intent object that matches the original intent
832 + $dummy_intent = new stdClass();
833 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
834 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
835 +
836 + // Call the recommendation flow handler directly
837 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
838 +
839 + // If the handler returned a response, send it
840 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
841 + // Save the bot's response to the chat history
842 + if (!empty($response_data['text'])) {
843 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
1957 844 }
1958 -
1959 - // Step 3: Handle the intent result appropriately
1960 - if ($intent_result !== false) {
1961 - // Intent was matched - ALWAYS send as JSON response, never streaming
1962 -
1963 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1964 - // Intent returned a direct response array
1965 - $response_data = [
1966 - 'text' => $intent_result['text'] ?? '',
1967 - 'html' => $intent_result['html'] ?? '',
1968 - 'session_id' => $session_id
1969 - ];
1970 -
1971 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1972 - if (isset($intent_result['chat_mode'])) {
1973 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1974 - }
1975 -
1976 - if ($testing_data !== null) {
1977 - $response_data['testing_data'] = $testing_data;
1978 - }
1979 -
1980 - wp_send_json($response_data);
1981 - wp_die();
1982 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1983 - // Intent returned true and set fallbackResponse
1984 -
1985 - // SAVE TO TRANSCRIPT
1986 - if (!empty($this->fallbackResponse['text'])) {
1987 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1988 - }
1989 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1990 - if (!empty($this->fallbackResponse['html'])) {
1991 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1992 - }
1993 -
1994 - $response_data = [
1995 - 'text' => $this->fallbackResponse['text'] ?? '',
1996 - 'html' => $this->fallbackResponse['html'] ?? '',
1997 - 'session_id' => $session_id
1998 - ];
1999 -
2000 - if (isset($this->fallbackResponse['chat_mode'])) {
2001 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
2002 - }
2003 -
2004 - if ($testing_data !== null) {
2005 - $response_data['testing_data'] = $testing_data;
2006 - }
2007 -
2008 - wp_send_json($response_data);
2009 - wp_die();
2010 - }
845 + if (!empty($response_data['html'])) {
846 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
2011 847 }
848 +
849 + // Send the response
850 + wp_send_json($response_data);
851 + wp_die();
852 + }
853 +
854 + // If we reach here, the flow handler didn't provide a usable response
855 + // We'll continue with regular processing
856 + //error_log('MXCHAT DEBUG: Recommendation flow handler did not provide a usable response');
857 +}
2012 858
2013 - // If we get here, no intent matched OR the intent didn't provide a usable response
859 + // Step 2: Detect intent and handle intent-based responses
860 +$intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
861 +//error_log("Intent Result Type: " . gettype($intent_result));
2014 862
2015 - // Step 4: Generate AI response
2016 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
2017 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
2018 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
2019 - $this->mxchat_increment_chat_count();
863 +// Step 3: Handle the intent result appropriately
864 +if ($intent_result !== false) {
865 + // The intent was matched and handled
866 + //error_log("Intent was matched and handled.");
867 +
868 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
869 + // Intent returned a direct response array
870 + //error_log("Intent returned a direct response.");
871 + $response_data = [
872 + 'text' => $intent_result['text'] ?? '',
873 + 'html' => $intent_result['html'] ?? '',
874 + 'session_id' => $session_id
875 + ];
2020 876
2021 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
2022 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
2023 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
877 + wp_send_json($response_data);
878 + wp_die();
879 + }
880 + else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
881 + // Intent returned true and set fallbackResponse
882 + //error_log("Intent returned true with fallbackResponse set.");
883 + $response_data = [
884 + 'text' => $this->fallbackResponse['text'] ?? '',
885 + 'html' => $this->fallbackResponse['html'] ?? '',
886 + 'session_id' => $session_id
887 + ];
2024 888
2025 - // Check if the embedding generation returned an error
2026 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
2027 - $error_message = $user_message_embedding['error'];
2028 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
889 + wp_send_json($response_data);
890 + wp_die();
891 + }
892 +
893 + // Intent was matched but no usable response was provided
894 + // This shouldn't happen with proper intent implementation
895 + //error_log("Warning: Intent matched but no response provided.");
896 +}
2029 897
2030 - // FIXED: Send error in appropriate format based on streaming mode
2031 - if ($is_streaming) {
2032 - echo "data: " . json_encode([
2033 - 'error' => true,
2034 - 'error_message' => $error_message,
2035 - 'error_code' => $error_code,
2036 - 'text' => $error_message,
2037 - 'message' => $error_message
2038 - ]) . "\n\n";
2039 - echo "data: [DONE]\n\n";
2040 - flush();
2041 - } else {
898 + // If we get here, no intent matched OR the intent didn't provide a usable response
899 + //error_log("No matching intent or usable response. Generating AI response.");
900 +
901 + // Step 4: Generate AI response
902 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
903 + $this->mxchat_increment_chat_count();
904 +
905 + // Generate embedding for the user's query
906 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
907 +
908 + // Check if the embedding generation returned an error
909 + if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
910 + $error_message = $user_message_embedding['error'];
911 + $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
912 +
913 + //error_log("Embedding error for session $session_id: $error_message (Code: $error_code)");
914 +
915 + // Important: Structure the error data correctly for wp_send_json_error
2042 916 wp_send_json_error([
2043 917 'error_message' => $error_message,
2044 918 'error_code' => $error_code
2045 919 ]);
920 + wp_die();
2046 921 }
2047 - wp_die();
2048 - }
2049 -
2050 - // Check if the embedding is valid
2051 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2052 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2053 -
2054 - // FIXED: Send error in appropriate format based on streaming mode
2055 - if ($is_streaming) {
2056 - echo "data: " . json_encode([
2057 - 'error' => true,
2058 - 'error_message' => $error_message,
2059 - 'error_code' => 'invalid_embedding',
2060 - 'text' => $error_message,
2061 - 'message' => $error_message
2062 - ]) . "\n\n";
2063 - echo "data: [DONE]\n\n";
2064 - flush();
2065 - } else {
922 +
923 + // Check if the embedding is valid
924 + if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
925 + //error_log("Failed to generate message embedding for session $session_id");
2066 926 wp_send_json_error([
2067 - 'error_message' => $error_message,
927 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
2068 928 'error_code' => 'invalid_embedding'
2069 929 ]);
930 + wp_die();
2070 931 }
2071 - wp_die();
2072 - }
2073 932
2074 933 // Build context with both knowledge base and PDF content if available
2075 934 $context_content = "User asked: '{$message}'\n\n";
2076 -
2077 - // Add action instruction if present (add this right after the above line)
2078 - if (!empty($this->current_action_instruction)) {
2079 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2080 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2081 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2082 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2083 -
2084 - // Clear the instruction after using it
2085 - $this->current_action_instruction = null;
2086 - }
2087 935
2088 936
2089 - // Add page context if available and contextual awareness is enabled using current_options
2090 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2091 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2092 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2093 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2094 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2095 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2096 - }
937 + // Get relevant content from knowledge base
938 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
939 + if (!empty($relevant_content)) {
940 + $context_content .= "Relevant content from knowledge database:\n" . $relevant_content . "\n\n";
941 + }
2097 942
2098 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2099 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2100 -
2101 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2102 - // Use fresh options to ensure we get the latest setting value
2103 - $fresh_options = get_option('mxchat_options', []);
2104 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2105 943
2106 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2107 - if ($citation_links_enabled && !empty($system_instructions)) {
2108 - preg_match_all(
2109 - '#\bhttps?://[^\s<>"\']+#i',
2110 - $system_instructions,
2111 - $system_instruction_urls
2112 - );
2113 -
2114 - if (!empty($system_instruction_urls[0])) {
2115 - // Merge with existing valid URLs
2116 - $this->current_valid_urls = array_merge(
2117 - $this->current_valid_urls,
2118 - $system_instruction_urls[0]
2119 - );
2120 - // Remove duplicates
2121 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2122 -
2123 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2124 - }
2125 - }
2126 -
2127 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2128 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2129 - // Update testing data with the REAL similarity analysis
2130 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2131 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2132 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2133 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2134 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2135 -}
2136 -// ===== END SIMILARITY DATA CAPTURE =====
2137 -
2138 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2139 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2140 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2141 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2142 -}
2143 -
2144 - if (!empty($relevant_content)) {
2145 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2146 - } else {
2147 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2148 - }
2149 -
2150 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2151 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2152 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2153 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2154 - foreach ($this->current_valid_urls as $url) {
2155 - $context_content .= "- " . $url . "\n";
2156 - }
2157 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2158 - $context_content .= "===== END APPROVED URLS =====\n\n";
2159 - }
2160 -
2161 944 // Check for and include PDF content
2162 945 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2163 946 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2164 947 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2189,111 +972,24 @@
2189 972 }
2190 973
2191 974 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2192 975
2193 - // Extract model from current options for bot-specific model support
2194 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2195 -
2196 - // ===== Native function-calling fallback (plan-mxchat-20260617-a41dee) =====
2197 - // Intents already missed (we're past the intent router). If function
2198 - // calling is enabled and the active model is tool-capable, let the model
2199 - // SELECT and run registered callbacks as tools — independent of intents,
2200 - // works with zero Actions. The tool round is buffered; the final answer is
2201 - // emitted via the SAME envelopes the normal path uses. Default-off, so
2202 - // existing installs never enter this branch.
2203 - if ($this->mxchat_fc_should_run($selected_model)) {
2204 - $fc_outcome = $this->mxchat_fc_attempt(
2205 - $message,
2206 - $context_content,
2207 - $conversation_history,
2208 - $selected_model,
2209 - $current_options,
2210 - $session_id,
2211 - $user_id
2212 - );
2213 - if (is_array($fc_outcome) && !empty($fc_outcome['handled'])) {
2214 - $fc_text = isset($fc_outcome['text']) ? $fc_outcome['text'] : '';
2215 - if (!empty($this->current_valid_urls)) {
2216 - $fc_text = $this->validate_and_clean_urls($fc_text, $this->current_valid_urls);
2217 - }
2218 - // plan-mxchat-20260617-48a57a — surface any UI element a tool
2219 - // produced (generated image / product card / image gallery) so the
2220 - // widget RENDERS it, instead of emitting only the model's text.
2221 - // The html was already saved to the transcript in
2222 - // mxchat_fc_execute_tool (or by the callback itself for self-saving
2223 - // core tools), so we persist ONLY the model's caption text here.
2224 - $fc_html = isset($this->fc_ui_html) ? $this->fc_ui_html : '';
2225 -
2226 - if ($fc_text !== '') {
2227 - $this->mxchat_save_chat_message($session_id, 'bot', $fc_text, null, null);
2228 - }
2229 -
2230 - if ($is_streaming) {
2231 - // The frontend SSE reader routes any event carrying text/html
2232 - // to handleNonStreamResponse(), which renders text + html in a
2233 - // single bot message — so emit one complete event (mirrors the
2234 - // intent path's text/html envelope).
2235 - $sse = array('session_id' => $session_id);
2236 - if ($fc_text !== '') $sse['text'] = $fc_text;
2237 - if ($fc_html !== '') $sse['html'] = $fc_html;
2238 - if ($fc_text === '' && $fc_html === '') $sse['text'] = $this->mxchat_fc_giveup_text();
2239 - echo "data: " . wp_json_encode($sse) . "\n\n";
2240 - echo "data: [DONE]\n\n";
2241 - flush();
2242 - } else {
2243 - $fc_response_data = array('text' => $fc_text, 'html' => $fc_html, 'session_id' => $session_id);
2244 - if ($testing_data !== null) {
2245 - $fc_response_data['testing_data'] = $testing_data;
2246 - }
2247 - wp_send_json($fc_response_data);
2248 - }
2249 - wp_die();
2250 - }
2251 - }
2252 - // ===== end function-calling fallback =====
2253 -
976 + // Generate the response using the full context
2254 977 $response = $this->mxchat_generate_response(
2255 978 $context_content,
2256 - $current_options['api_key'] ?? $this->options['api_key'],
2257 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2258 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2259 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2260 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2261 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2262 - $conversation_history,
2263 - $is_streaming,
2264 - $session_id,
2265 - $testing_data,
2266 - $selected_model
979 + $this->options['api_key'],
980 + $this->options['xai_api_key'],
981 + $this->options['claude_api_key'],
982 + $this->options['deepseek_api_key'],
983 + $this->options['gemini_api_key'],
984 + $conversation_history
2267 985 );
2268 986
2269 - // Handle streaming vs non-streaming responses
2270 - if ($is_streaming) {
2271 - // Check if streaming actually happened or if it fell back to regular response
2272 - if ($response === true) {
2273 - wp_die();
2274 - }
2275 - // If we get here, streaming fell back to regular response, continue
2276 - // But if there's an error, we need to send it as SSE format since headers are already set
2277 - if (is_array($response) && isset($response['error'])) {
2278 - $error_message = $response['error'];
2279 - $error_code = $response['error_code'] ?? 'api_error';
2280 - // Send error in SSE format that the client JS can handle
2281 - echo "data: " . json_encode([
2282 - 'error' => true,
2283 - 'error_message' => $error_message,
2284 - 'error_code' => $error_code,
2285 - 'text' => $error_message, // Also include as text for fallback handling
2286 - 'message' => $error_message
2287 - ]) . "\n\n";
2288 - echo "data: [DONE]\n\n";
2289 - flush();
2290 - wp_die();
2291 - }
2292 - }
2293 -
2294 - // Check if the response is an error array (non-streaming mode)
987 + // Check if the response is an error array
2295 988 if (is_array($response) && isset($response['error'])) {
989 + //error_log("AI Response Error: " . $response['error'] . " (Code: " . ($response['error_code'] ?? 'unknown') . ")");
990 +
991 + // Send a user-friendly error message
2296 992 wp_send_json_error([
2297 993 'error_message' => $response['error'],
2298 994 'error_code' => $response['error_code'] ?? 'api_error'
2299 995 ]);
@@ -2299,219 +995,77 @@
2299 995 ]);
2300 996 wp_die();
2301 997 }
2302 998
2303 - // DEBUG: Check what we have
2304 - //error_log("=== BEFORE URL VALIDATION ===");
2305 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2306 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2307 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2308 -
2309 - // If we get here, the response is valid text - now validate URLs
2310 - if (!empty($this->current_valid_urls)) {
2311 - //error_log("CALLING validate_and_clean_urls");
2312 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2313 - } else {
2314 - //error_log("SKIPPING validation - current_valid_urls is empty");
2315 - }
2316 - // ===== END URL VALIDATION =====
999 + // If we get here, the response is valid text
1000 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2317 1001
2318 - // Prepare RAG context data for storage (only include documents used for context)
2319 - $rag_context_for_storage = null;
2320 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2321 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
1002 + // Step 5: Save additional content if available
1003 + if (!empty($this->productCardHtml)) {
1004 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
1005 + }
2322 1006
2323 - if ($has_rag_data || $has_action_data) {
2324 - $rag_context_for_storage = [];
1007 + if (!empty($this->fallbackResponse['html'])) {
1008 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1009 + }
2325 1010
2326 - // Add RAG/source data if available
2327 - if ($has_rag_data) {
2328 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2329 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2330 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2331 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2332 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2333 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2334 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2335 - }
1011 + // Step 6: Return the response
1012 + $response_data = [
1013 + 'text' => $response,
1014 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1015 + 'session_id' => $session_id
1016 + ];
2336 1017
2337 - // Add action analysis data if available
2338 - if ($has_action_data) {
2339 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2340 - }
2341 - }
2342 -
2343 - // Save the cleaned response with RAG context
2344 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2345 -
2346 - // Step 5: Save additional content if available
2347 - if (!empty($this->productCardHtml)) {
2348 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2349 - }
2350 -
2351 - if (!empty($this->fallbackResponse['html'])) {
2352 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2353 - }
2354 -
2355 - // Step 6: Return the response
2356 - // DEBUG: Check if newlines exist in the response
2357 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2358 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2359 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2360 -
2361 - $response_data = [
2362 - 'text' => $response,
2363 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2364 - 'session_id' => $session_id
2365 - ];
2366 -
2367 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2368 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2369 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2370 - }
2371 -
2372 - // Also pass it as a top-level field so JS can show a better error message to admins
2373 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2374 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2375 - }
2376 -
2377 - // Always add testing data for admins (no toggle needed)
2378 - if ($testing_data !== null) {
2379 - $response_data['testing_data'] = $testing_data;
2380 - }
2381 -
2382 - wp_send_json($response_data);
2383 - wp_die();
1018 + wp_send_json($response_data);
1019 + wp_die();
2384 1020 }
2385 1021
2386 -/**
2387 - * Get bot-specific options for multi-bot functionality
2388 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2389 - */
2390 -// Also debug the bot options retrieval
2391 -private function get_bot_options($bot_id = 'default') {
2392 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2393 -
2394 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2395 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2396 - return array();
2397 - }
2398 -
2399 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2400 -
2401 - if (!empty($bot_options)) {
2402 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2403 - if (isset($bot_options['similarity_threshold'])) {
2404 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2405 - }
2406 - }
2407 -
2408 - return is_array($bot_options) ? $bot_options : array();
2409 -}
2410 -
2411 -/**
2412 - * Get bot-specific Pinecone configuration
2413 - * Used in the knowledge retrieval functions
2414 - */
2415 -// Also add debugging to your get_bot_pinecone_config function
2416 -private function get_bot_pinecone_config($bot_id = 'default') {
2417 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2418 -
2419 - // If default bot or multi-bot add-on not active, use default Pinecone config
2420 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2421 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2422 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2423 - $config = array(
2424 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2425 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2426 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2427 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2428 - );
2429 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2430 - return $config;
2431 - }
2432 -
2433 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2434 -
2435 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2436 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2437 -
2438 - if (!empty($bot_pinecone_config)) {
2439 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2440 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2441 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2442 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2443 - } else {
2444 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2445 - }
2446 -
2447 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2448 -}
2449 -
2450 -
2451 1022 // Updated function to check intents and invoke the callback function
2452 1023 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2453 1024 global $wpdb;
2454 1025 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2455 1026
2456 - // Get the current bot_id
2457 - $current_bot_id = $this->get_current_bot_id($session_id);
1027 + //error_log('🔍 MXCHAT DEBUG: Intent Check Started ==================');
1028 + //error_log("🔍 MXCHAT DEBUG: Message: '$message'");
1029 + //error_log("🔍 MXCHAT DEBUG: Chat Mode: $chat_mode");
2458 1030
2459 1031 // Generate the user embedding
1032 + //error_log('🔄 MXCHAT DEBUG: Generating user embedding');
2460 1033 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2461 -
1034 +
2462 1035 // Check if embedding generation returned an error
2463 1036 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2464 1037 $error_message = $user_embedding['error'];
2465 1038 $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2466 -
2467 - // FIXED: Send error in appropriate format based on streaming mode
2468 - if ($this->is_streaming) {
2469 - echo "data: " . json_encode([
2470 - 'error' => true,
2471 - 'error_message' => $error_message,
2472 - 'error_code' => $error_code,
2473 - 'text' => $error_message,
2474 - 'message' => $error_message
2475 - ]) . "\n\n";
2476 - echo "data: [DONE]\n\n";
2477 - flush();
2478 - } else {
2479 - wp_send_json_error([
2480 - 'error_message' => $error_message,
2481 - 'error_code' => $error_code
2482 - ]);
2483 - }
1039 +
1040 + //error_log("❌ MXCHAT DEBUG: Embedding error: $error_message (Code: $error_code)");
1041 +
1042 + // Send the error to the frontend
1043 + wp_send_json_error([
1044 + 'error_message' => $error_message,
1045 + 'error_code' => $error_code
1046 + ]);
2484 1047 wp_die();
2485 1048 }
2486 -
2487 - // Check if embedding is valid
1049 +
1050 + // Check if embedding is valid (not an error and is an array)
2488 1051 if (!is_array($user_embedding) || empty($user_embedding)) {
2489 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2490 -
2491 - // FIXED: Send error in appropriate format based on streaming mode
2492 - if ($this->is_streaming) {
2493 - echo "data: " . json_encode([
2494 - 'error' => true,
2495 - 'error_message' => $error_message,
2496 - 'error_code' => 'invalid_embedding',
2497 - 'text' => $error_message,
2498 - 'message' => $error_message
2499 - ]) . "\n\n";
2500 - echo "data: [DONE]\n\n";
2501 - flush();
2502 - } else {
2503 - wp_send_json_error([
2504 - 'error_message' => $error_message,
2505 - 'error_code' => 'invalid_embedding'
2506 - ]);
2507 - }
1052 + //error_log('❌ MXCHAT DEBUG: Failed to generate user embedding');
1053 +
1054 + // Send a generic error to the frontend
1055 + wp_send_json_error([
1056 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1057 + 'error_code' => 'invalid_embedding'
1058 + ]);
2508 1059 wp_die();
2509 1060 }
2510 -
1061 +
1062 + //error_log('✅ MXCHAT DEBUG: User embedding generated successfully');
1063 +
2511 1064 // Fetch intents from the database
2512 1065 $table_name = $wpdb->prefix . 'mxchat_intents';
2513 1066 if ($chat_mode === 'agent') {
1067 + //error_log('🔍 MXCHAT DEBUG: Agent mode - fetching only chatbot switch intent');
2514 1068 $query = $wpdb->prepare(
2515 1069 "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2516 1070 'mxchat_handle_switch_to_chatbot_intent'
2517 1071 );
@@ -2516,139 +1070,78 @@
2516 1070 'mxchat_handle_switch_to_chatbot_intent'
2517 1071 );
2518 1072 $intents = $wpdb->get_results($query);
2519 1073 } else {
1074 + //error_log('🔍 MXCHAT DEBUG: AI mode - fetching all enabled intents');
1075 + // Only fetch enabled intents (either explicitly enabled with 1 or implicitly enabled with NULL for backward compatibility)
2520 1076 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2521 1077 }
2522 -
1078 +
1079 + //error_log('🔍 MXCHAT DEBUG: Found ' . count($intents) . ' enabled intents to check');
1080 +
2523 1081 if (empty($intents)) {
1082 + //error_log('❌ MXCHAT DEBUG: No enabled intents found in database');
2524 1083 return false;
2525 1084 }
2526 -
2527 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2528 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2529 - $phrases_by_intent = [];
2530 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2531 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2532 - foreach ($all_phrases as $p) {
2533 - $phrases_by_intent[$p->intent_id][] = $p;
2534 - }
2535 - }
2536 -
1085 +
2537 1086 $highest_similarity = -INF;
2538 1087 $matched_intent = null;
2539 -
2540 - // Array to store action analysis for testing panel
2541 - $action_analysis = [];
2542 -
1088 +
1089 + //error_log('📊 MXCHAT DEBUG: Intent Similarity Scores ==================');
2543 1090 foreach ($intents as $intent) {
2544 - // Additional check for enabled state
1091 + //error_log("🔄 MXCHAT DEBUG: Checking intent: '{$intent->intent_label}' (callback: {$intent->callback_function})");
1092 +
1093 + // Additional check for enabled state in case database structure was modified
2545 1094 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2546 1095 if (!$is_enabled) {
1096 + //error_log("⚠️ MXCHAT DEBUG: Skipping disabled intent: {$intent->intent_label}");
2547 1097 continue;
2548 1098 }
2549 -
2550 - // Check if this action is enabled for the current bot
2551 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2552 - continue;
2553 - }
2554 -
2555 - $best_similarity = -INF;
2556 - $matched_phrase_text = '';
2557 -
2558 - // Check legacy embedding vector (existing behavior)
1099 +
2559 1100 $intent_embedding_serialized = $intent->embedding_vector;
2560 1101 $intent_embedding = $intent_embedding_serialized
2561 1102 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2562 1103 : null;
2563 -
2564 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2565 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2566 - if ($legacy_similarity > $best_similarity) {
2567 - $best_similarity = $legacy_similarity;
2568 - $matched_phrase_text = 'legacy';
2569 - }
2570 - }
2571 -
2572 - // Check individual phrase vectors
2573 - if (isset($phrases_by_intent[$intent->id])) {
2574 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2575 - $phrase_embedding = $phrase_row->embedding_vector
2576 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2577 - : null;
2578 - if (!is_array($phrase_embedding)) {
2579 - continue;
2580 - }
2581 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2582 - if ($phrase_similarity > $best_similarity) {
2583 - $best_similarity = $phrase_similarity;
2584 - $matched_phrase_text = $phrase_row->phrase;
2585 - }
2586 - }
2587 - }
2588 -
2589 - // Skip if no valid embedding was found at all
2590 - if ($best_similarity === -INF) {
1104 +
1105 + if (!is_array($intent_embedding)) {
1106 + //error_log("❌ MXCHAT DEBUG: Invalid embedding for intent: {$intent->intent_label}");
2591 1107 continue;
2592 1108 }
2593 -
2594 - $similarity = $best_similarity;
1109 +
1110 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2595 1111 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2596 -
2597 - // Store action analysis data for testing panel
2598 - $action_analysis[] = [
2599 - 'intent_label' => $intent->intent_label,
2600 - 'callback_function' => $intent->callback_function,
2601 - 'similarity' => round($similarity, 4),
2602 - 'similarity_percentage' => round($similarity * 100, 2),
2603 - 'threshold' => $intent_threshold,
2604 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2605 - 'above_threshold' => $similarity >= $intent_threshold,
2606 - 'matched_phrase' => $matched_phrase_text,
2607 - 'triggered' => false // Will be updated below if this intent is triggered
2608 - ];
2609 -
1112 +
1113 + //error_log("📊 MXCHAT DEBUG: Intent '{$intent->intent_label}' similarity: {$similarity}, threshold: {$intent_threshold}");
1114 +
2610 1115 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2611 1116 $highest_similarity = $similarity;
2612 1117 $matched_intent = $intent;
1118 + //error_log("✅ MXCHAT DEBUG: New best match: '{$intent->intent_label}' with similarity {$similarity}");
2613 1119 }
2614 1120 }
1121 + //error_log('📊 MXCHAT DEBUG: End Intent Scores ==================');
2615 1122
2616 - // Mark the triggered action if any
2617 1123 if ($matched_intent) {
2618 - foreach ($action_analysis as &$action) {
2619 - if ($action['intent_label'] === $matched_intent->intent_label) {
2620 - $action['triggered'] = true;
2621 - break;
2622 - }
2623 - }
2624 - }
2625 -
2626 - // Sort actions by similarity (highest first) and store for testing panel
2627 - usort($action_analysis, function($a, $b) {
2628 - return $b['similarity'] <=> $a['similarity'];
2629 - });
2630 -
2631 - // Store action analysis for testing panel capture
2632 - $this->last_action_analysis = $action_analysis;
2633 -
2634 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2635 - if ($matched_intent) {
1124 + //error_log("🎯 MXCHAT DEBUG: Final Intent Match: '{$matched_intent->intent_label}'");
1125 + //error_log("🔄 MXCHAT DEBUG: Invoking callback: {$matched_intent->callback_function}");
1126 +
2636 1127 // If the callback is a method on this instance (core callback), call it directly
2637 1128 if (method_exists($this, $matched_intent->callback_function)) {
1129 + //error_log('🔍 MXCHAT DEBUG: Using direct method call for core callback');
2638 1130 $callback_result = call_user_func(
2639 - [$this, $matched_intent->callback_function],
2640 - $message,
2641 - $user_id,
2642 - $session_id,
2643 - $matched_intent,
2644 - $user_context ?? null
2645 - );
1131 + [$this, $matched_intent->callback_function],
1132 + $message,
1133 + $user_id,
1134 + $session_id,
1135 + $matched_intent,
1136 + $user_context
1137 + );
2646 1138 } else {
1139 + //error_log('🔍 MXCHAT DEBUG: Using apply_filters for add-on callback');
2647 1140 // Otherwise, use apply_filters for add-on callbacks
2648 1141 $callback_result = apply_filters(
2649 1142 $matched_intent->callback_function,
2650 - false,
1143 + false, // default return value
2651 1144 $message,
2652 1145 $user_id,
2653 1146 $session_id,
2654 1147 $matched_intent
@@ -2654,50 +1147,26 @@
2654 1147 $matched_intent
2655 1148 );
2656 1149 }
2657 1150
2658 - // Handle the callback result properly
1151 + //error_log('🔍 MXCHAT DEBUG: Callback result type: ' . gettype($callback_result));
2659 1152 if ($callback_result !== false) {
2660 - // If callback returned an array with chat_mode, use it directly
2661 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2662 - $this->fallbackResponse = $callback_result;
2663 - return $callback_result; // Return the full array
2664 - } else {
2665 - $this->fallbackResponse = $callback_result;
2666 - return true;
2667 - }
1153 + //error_log('✅ MXCHAT DEBUG: Intent handled successfully');
1154 + $this->fallbackResponse = $callback_result;
1155 + return true;
2668 1156 }
1157 + //error_log('❌ MXCHAT DEBUG: Callback returned false');
1158 + } else {
1159 + //error_log('❌ MXCHAT DEBUG: No matching intent found');
2669 1160 }
2670 1161
1162 + //error_log('🔍 MXCHAT DEBUG: Intent Check Completed ==================');
2671 1163 return false;
2672 1164 }
2673 1165
2674 -/**
2675 - * Check if an action is enabled for a specific bot
2676 - */
2677 -private function is_action_enabled_for_bot($intent, $bot_id) {
2678 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2679 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2680 - return true;
2681 - }
2682 1166
2683 - $enabled_bots = json_decode($intent->enabled_bots, true);
2684 1167
2685 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2686 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2687 - return true;
2688 - }
2689 1168
2690 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2691 - // default-bot actions are testable from the admin panel
2692 - if ($bot_id === 'testing') {
2693 - $bot_id = 'default';
2694 - }
2695 -
2696 - // Check if the current bot is in the enabled bots list
2697 - return in_array($bot_id, $enabled_bots);
2698 -}
2699 -
2700 1169 // Helper function to clear PDF and Word document related transients
2701 1170 private function clear_pdf_transients($session_id) {
2702 1171 // PDF transients
2703 1172 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2716,38 +1185,34 @@
2716 1185
2717 1186
2718 1187 //verified good
2719 1188 public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2720 - // Get the user's original instruction/message
2721 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2722 -
2723 - // Set instruction for AI - just pass along what the user wanted to say
2724 - $this->current_action_instruction = $user_instruction;
2725 -
2726 - // Set the transient to track email capture flow
1189 + // Log the message safely
1190 + //error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
1191 +
1192 + // Initiate email capture flow
1193 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
1194 +
2727 1195 set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2728 -
2729 - // Return false to let the AI generate the response
2730 - return false;
1196 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1197 +
1198 + // Respond to the user
1199 + wp_send_json(['message' => $response]);
1200 + wp_die();
2731 1201 }
2732 1202
2733 1203 public function mxchat_generate_image($message, $user_id, $session_id) {
2734 1204 //error_log("Starting image generation for message: " . $message);
2735 -
2736 - // Prepare a prompt for OpenAI image generation
1205 +
1206 + // Prepare a prompt for DALL-E
2737 1207 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2738 -
2739 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2740 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2741 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2742 - $image_response = $this->mxchat_generate_custom_image($prompt);
2743 - } else {
2744 - // Use the existing OpenAI API key
2745 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2746 - // Call OpenAI GPT Image to generate an image
2747 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2748 - }
2749 1208
1209 + // Use the existing OpenAI API key
1210 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1211 +
1212 + // Call DALL-E to generate an image
1213 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1214 +
2750 1215 // Check if the response contains an image URL
2751 1216 if (isset($image_response['imageUrl'])) {
2752 1217 $image_url = esc_url_raw($image_response['imageUrl']);
2753 1218
@@ -2790,125 +1255,24 @@
2790 1255 // Return the response directly instead of relying on the property
2791 1256 return $this->fallbackResponse;
2792 1257 }
2793 1258 }
2794 -
2795 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2796 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2797 -
2798 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2799 - if (empty($gemini_api_key)) {
2800 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2801 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2802 - return ['text' => $response_text, 'html' => '', 'images' => []];
2803 - }
2804 -
2805 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2806 -
2807 - if (isset($image_response['imageUrl'])) {
2808 - $image_url = esc_url_raw($image_response['imageUrl']);
2809 -
2810 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2811 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2812 -
2813 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2814 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2815 -
2816 - $this->fallbackResponse = [
2817 - 'text' => $response_text,
2818 - 'html' => $response_html,
2819 - 'images' => [$image_url]
2820 - ];
2821 -
2822 - return $this->fallbackResponse;
2823 - } else {
2824 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2825 -
2826 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2827 -
2828 - $this->fallbackResponse = [
2829 - 'text' => $response_text,
2830 - 'html' => '',
2831 - 'images' => []
2832 - ];
2833 -
2834 - return $this->fallbackResponse;
2835 - }
2836 -}
2837 -
2838 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2839 - // Map the real mime type to a matching file extension so the saved file's
2840 - // extension always agrees with its bytes. A mismatch (e.g. Imagen returning
2841 - // webp bytes that were written into a ".png" file) makes the browser refuse
2842 - // to render the image even though the file saved successfully and the bot
2843 - // reported success — that was the Gemini/Imagen "image never renders" bug.
2844 - // OpenAI + custom-provider paths pass 'image/png' explicitly, so they are
2845 - // unaffected; this only matters for providers that return another type.
2846 - $mime_to_ext = [
2847 - 'image/jpeg' => 'jpg',
2848 - 'image/jpg' => 'jpg',
2849 - 'image/png' => 'png',
2850 - 'image/webp' => 'webp',
2851 - 'image/gif' => 'gif',
2852 - ];
2853 - $mime_type = strtolower(trim((string) $mime_type));
2854 - if (isset($mime_to_ext[$mime_type])) {
2855 - $extension = $mime_to_ext[$mime_type];
2856 - } else {
2857 - // Unknown/unsupported type: fall back to png and normalize the stored
2858 - // mime so the attachment record and the file extension stay consistent.
2859 - $extension = 'png';
2860 - $mime_type = 'image/png';
2861 - }
2862 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2863 - $decoded = base64_decode($base64_data);
2864 -
2865 - if ($decoded === false) {
2866 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2867 - }
2868 -
2869 - $upload = wp_upload_bits($filename, null, $decoded);
2870 -
2871 - if (!empty($upload['error'])) {
2872 - return new \WP_Error('upload_failed', $upload['error']);
2873 - }
2874 -
2875 - $attach_id = wp_insert_attachment([
2876 - 'post_mime_type' => $mime_type,
2877 - 'post_title' => $prefix,
2878 - 'post_content' => '',
2879 - 'post_status' => 'inherit',
2880 - ], $upload['file']);
2881 -
2882 - if (is_wp_error($attach_id)) {
2883 - return $attach_id;
2884 - }
2885 -
2886 - require_once ABSPATH . 'wp-admin/includes/image.php';
2887 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2888 - wp_update_attachment_metadata($attach_id, $metadata);
2889 -
2890 - return esc_url_raw(wp_get_attachment_url($attach_id));
2891 -}
2892 -
2893 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1259 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2894 1260 $api_url = 'https://api.openai.com/v1/images/generations';
2895 1261 $body = json_encode([
2896 - 'prompt' => sanitize_text_field($prompt),
2897 - 'n' => 1,
2898 - 'size' => '1024x1024',
2899 - 'quality' => 'medium',
2900 - 'output_format' => 'png',
2901 - 'model' => sanitize_text_field($model),
1262 + 'prompt' => sanitize_text_field($prompt),
1263 + 'n' => 1,
1264 + 'size' => '1024x1024',
1265 + 'model' => sanitize_text_field($model),
2902 1266 ]);
2903 1267
2904 1268 $args = [
2905 - 'body' => $body,
1269 + 'body' => $body,
2906 1270 'headers' => [
2907 - 'Content-Type' => 'application/json',
1271 + 'Content-Type' => 'application/json',
2908 1272 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2909 1273 ],
2910 - 'method' => 'POST',
1274 + 'method' => 'POST',
2911 1275 'timeout' => absint($timeout),
2912 1276 ];
2913 1277
2914 1278 $response = wp_remote_post($api_url, $args);
@@ -2913,114 +1277,23 @@
2913 1277
2914 1278 $response = wp_remote_post($api_url, $args);
2915 1279
2916 1280 if (is_wp_error($response)) {
1281 + //error_log("DALL-E request failed: " . $response->get_error_message());
2917 1282 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2918 1283 }
2919 1284
2920 1285 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2921 1286
2922 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2923 - if ($b64) {
2924 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2925 - if (is_wp_error($saved_url)) {
2926 - return ['error' => $saved_url->get_error_message()];
2927 - }
2928 - return ['imageUrl' => $saved_url];
1287 + if (isset($response_body['data'][0]['url'])) {
1288 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2929 1289 } else {
1290 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2930 1291 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2931 1292 }
2932 1293 }
2933 1294
2934 1295 /**
2935 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2936 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2937 - */
2938 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2939 - $cfg = $this->mxchat_resolve_custom_provider();
2940 - if (empty($cfg['base_url'])) {
2941 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2942 - }
2943 - $url = $cfg['base_url'] . '/images/generations';
2944 - if (!empty($cfg['api_version'])) {
2945 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2946 - }
2947 - $body = wp_json_encode([
2948 - 'prompt' => sanitize_text_field($prompt),
2949 - 'n' => 1,
2950 - 'size' => '1024x1024',
2951 - 'model' => $cfg['model'],
2952 - ]);
2953 - $response = wp_remote_post($url, [
2954 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2955 - 'body' => $body,
2956 - 'method' => 'POST',
2957 - 'timeout' => absint($timeout),
2958 - ]);
2959 - if (is_wp_error($response)) {
2960 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2961 - }
2962 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2963 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2964 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2965 - if ($b64) {
2966 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2967 - if (is_wp_error($saved)) {
2968 - return ['error' => $saved->get_error_message()];
2969 - }
2970 - return ['imageUrl' => $saved];
2971 - }
2972 - $remote_url = $resp['data'][0]['url'] ?? null;
2973 - if ($remote_url) {
2974 - return ['imageUrl' => esc_url_raw($remote_url)];
2975 - }
2976 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2977 - return ['error' => esc_html($err_msg)];
2978 -}
2979 -
2980 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2981 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2982 -
2983 - $body = json_encode([
2984 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2985 - 'parameters' => [
2986 - 'sampleCount' => 1,
2987 - 'aspectRatio' => '1:1',
2988 - ],
2989 - ]);
2990 -
2991 - $args = [
2992 - 'body' => $body,
2993 - 'headers' => [
2994 - 'Content-Type' => 'application/json',
2995 - 'x-goog-api-key' => sanitize_text_field($api_key),
2996 - ],
2997 - 'method' => 'POST',
2998 - 'timeout' => absint($timeout),
2999 - ];
3000 -
3001 - $response = wp_remote_post($api_url, $args);
3002 -
3003 - if (is_wp_error($response)) {
3004 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
3005 - }
3006 -
3007 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
3008 -
3009 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
3010 - if ($b64) {
3011 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
3012 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
3013 - if (is_wp_error($saved_url)) {
3014 - return ['error' => $saved_url->get_error_message()];
3015 - }
3016 - return ['imageUrl' => $saved_url];
3017 - } else {
3018 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3019 - }
3020 -}
3021 -
3022 -/**
3023 1296 * Handle web search requests.
3024 1297 *
3025 1298 * Sends the refined search query to the Brave Search API and uses the
3026 1299 * results to generate a conversational response with the AI model.
@@ -3068,10 +1341,10 @@
3068 1341 $transient_key = 'mxchat_search_' . md5($refined_search_query);
3069 1342 $results = get_transient($transient_key);
3070 1343
3071 1344 if (false === $results) {
3072 - // SECURITY FIX: Changed to wp_safe_remote_get
3073 - $response = wp_safe_remote_get(
1345 + // Fetch new results from the Brave Search API
1346 + $response = wp_remote_get(
3074 1347 $api_url,
3075 1348 array(
3076 1349 'headers' => array(
3077 1350 'Accept' => 'application/json',
@@ -3150,28 +1423,119 @@
3150 1423 'html' => ''
3151 1424 );
3152 1425 }
3153 1426 }
1427 +/**
1428 + * Format search results into a natural text summary.
1429 + *
1430 + * @since 1.0.0
1431 + * @param array $results The search results from the API.
1432 + * @param string $query The original search query.
1433 + * @return string The text summary of the top results.
1434 + */
1435 +private function format_search_results( $results, $query ) {
1436 + $summary = sprintf(
1437 + esc_html__( 'Here are the most relevant results for "%s":', 'mxchat' ),
1438 + esc_html( $query )
1439 + ) . "\n\n";
3154 1440
3155 -//very good
1441 + $max_results = min( count( $results ), 3 );
1442 + for ( $i = 0; $i < $max_results; $i++ ) {
1443 + $result = $results[ $i ];
1444 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1445 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1446 +
1447 + // Append title and description to the summary
1448 + $summary .= sprintf(
1449 + "%s\n%s\n\n",
1450 + esc_html( $title ),
1451 + esc_html( $description )
1452 + );
1453 + }
1454 +
1455 + return $summary;
1456 +}
1457 +
3156 1458 /**
3157 - * Handle image search requests from the chatbot
1459 + * Generate HTML markup for search results.
3158 1460 *
3159 - * @param string $message The user's search query
3160 - * @param int $user_id The user's ID
3161 - * @param string $session_id The chat session ID
3162 - * @return array Response array with text and HTML content
1461 + * @since 1.0.0
1462 + * @param array $results The search results from the API.
1463 + * @param string $query The user-refined query.
1464 + * @return string The HTML markup for displaying the results.
3163 1465 */
1466 +private function generate_search_results_html( $results, $query ) {
1467 + ob_start();
1468 + ?>
1469 + <div class="mxchat-search-results">
1470 + <?php foreach ( $results as $result ) :
1471 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1472 + $url = isset( $result['url'] ) ? esc_url( $result['url'] ) : '#';
1473 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1474 + $favicon = isset( $result['meta_url']['favicon'] ) ? esc_url( $result['meta_url']['favicon'] ) : '';
1475 + $thumbnail = isset( $result['thumbnail']['src'] ) ? esc_url( $result['thumbnail']['src'] ) : '';
1476 + $domain = parse_url( $url, PHP_URL_HOST );
1477 + ?>
1478 + <div class="mxchat-search-item">
1479 + <div class="mxchat-search-header">
1480 + <?php if ( $favicon ) : ?>
1481 + <img
1482 + src="<?php echo esc_url( $favicon ); ?>"
1483 + class="mxchat-site-icon"
1484 + alt="<?php echo esc_attr__( 'Site icon', 'mxchat' ); ?>"
1485 + width="16"
1486 + height="16"
1487 + />
1488 + <?php endif; ?>
1489 + <div class="mxchat-site-url"><?php echo esc_html( $domain ); ?></div>
1490 + </div>
1491 +
1492 + <div class="mxchat-search-content">
1493 + <h3 class="mxchat-search-title">
1494 + <a href="<?php echo esc_url( $url ); ?>"
1495 + target="_blank"
1496 + rel="noopener noreferrer"
1497 + >
1498 + <?php echo esc_html( $title ); ?>
1499 + </a>
1500 + </h3>
1501 +
1502 + <?php if ( $thumbnail ) : ?>
1503 + <div class="mxchat-search-thumbnail">
1504 + <img
1505 + src="<?php echo esc_url( $thumbnail ); ?>"
1506 + alt="<?php echo esc_attr__( 'Thumbnail image', 'mxchat' ); ?>"
1507 + loading="lazy"
1508 + />
1509 + </div>
1510 + <?php endif; ?>
1511 +
1512 + <div class="mxchat-search-description">
1513 + <?php echo esc_html( $description ); ?>
1514 + </div>
1515 + </div>
1516 + </div>
1517 + <?php endforeach; ?>
1518 + </div>
1519 + <?php
1520 + return ob_get_clean();
1521 +}
1522 +
1523 +
1524 +//very good
3164 1525 public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3165 - // Step 1: Interpret the search query using the user's selected AI model
1526 +
1527 + // Step 1: Interpret the search query for better results
3166 1528 $refined_search_query = $this->mxchat_interpret_search_query($message);
3167 1529
1530 +
3168 1531 // If no query was interpreted, return a fallback message
3169 1532 if (empty($refined_search_query)) {
3170 - return array(
1533 + $this->fallbackResponse = [
3171 1534 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3172 1535 'html' => "",
3173 - );
1536 + ];
1537 + return;
3174 1538 }
3175 1539
3176 1540 // Brave API URL
3177 1541 $api_url = 'https://api.search.brave.com/res/v1/images/search';
@@ -3180,12 +1544,19 @@
3180 1544 $options = get_option('mxchat_options');
3181 1545 $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3182 1546
3183 1547 if (empty($api_key)) {
3184 - return array(
1548 +/*
1549 + if (defined('WP_DEBUG') && WP_DEBUG) {
1550 + //error_log("Brave API key is missing.");
1551 + }
1552 +*/
1553 +
1554 + $this->fallbackResponse = [
3185 1555 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3186 1556 'html' => "",
3187 - );
1557 + ];
1558 + return;
3188 1559 }
3189 1560
3190 1561 $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3191 1562 $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
@@ -3196,8 +1567,16 @@
3196 1567 'count' => $image_count,
3197 1568 'safesearch' => $safe_search,
3198 1569 ], $api_url);
3199 1570
1571 +/*
1572 + // Log the final API URL for the search
1573 + if (defined('WP_DEBUG') && WP_DEBUG) {
1574 + //error_log("Final API URL for Brave Image Search: " . esc_url_raw($api_url));
1575 + }
1576 +*/
1577 +
1578 +
3200 1579 // Implement caching
3201 1580 $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3202 1581 $body = get_transient($transient_key);
3203 1582
@@ -3210,16 +1589,22 @@
3210 1589 ],
3211 1590 'timeout' => 10,
3212 1591 ];
3213 1592
3214 - // SECURITY FIX: Changed to wp_safe_remote_get
3215 - $response = wp_safe_remote_get($api_url, $args);
1593 + $response = wp_remote_get($api_url, $args);
3216 1594
3217 1595 if (is_wp_error($response)) {
3218 - return array(
1596 +/*
1597 + if (defined('WP_DEBUG') && WP_DEBUG) {
1598 + //error_log("Brave Image API request failed: " . $response->get_error_message());
1599 + }
1600 +*/
1601 +
1602 + $this->fallbackResponse = [
3219 1603 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3220 1604 'html' => "",
3221 - );
1605 + ];
1606 + return;
3222 1607 }
3223 1608
3224 1609 $body = json_decode(wp_remote_retrieve_body($response), true);
3225 1610 set_transient($transient_key, $body, HOUR_IN_SECONDS);
@@ -3227,16 +1612,10 @@
3227 1612
3228 1613 // Process the API response
3229 1614 if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3230 1615 $html_output = '<div class="mxchat-image-gallery">';
3231 -
3232 - // Get the configured image count (1-6)
3233 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3234 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3235 -
3236 - // Use only the requested number of images
3237 - for ($i = 0; $i < $display_count; $i++) {
3238 - $image = $body['results'][$i];
1616 +
1617 + foreach ($body['results'] as $image) {
3239 1618 $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3240 1619 $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3241 1620 $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3242 1621
@@ -3250,149 +1629,47 @@
3250 1629 }
3251 1630
3252 1631 $html_output .= '</div>';
3253 1632
3254 - // Create response text
3255 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3256 -
3257 - // Save both response text and HTML to chat history
3258 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1633 + $this->fallbackResponse = [
1634 + 'text' => "",
1635 + 'html' => $html_output,
1636 + ];
1637 +
1638 + // Save response in chat history
3259 1639 $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3260 1640
3261 - // Return the combined response
3262 - return array(
3263 - 'text' => $response_text,
3264 - 'html' => $html_output,
3265 - );
3266 1641 } else {
3267 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3268 -
3269 - // Save the error message to chat history
3270 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3271 -
3272 - return array(
3273 - 'text' => $response_text,
1642 +/*
1643 + if (defined('WP_DEBUG') && WP_DEBUG) {
1644 + //error_log("Brave Image API response did not contain expected data structure or was empty: " . print_r($body, true));
1645 + }
1646 +*/
1647 +
1648 + $this->fallbackResponse = [
1649 + 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3274 1650 'html' => "",
3275 - );
1651 + ];
3276 1652 }
3277 1653 }
3278 -
3279 -/**
3280 - * Interpret the search query using the user's selected AI model
3281 - *
3282 - * @param string $user_query The original query from the user
3283 - * @return string The refined search query
3284 - */
3285 1654 public function mxchat_interpret_search_query($user_query) {
3286 1655 $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');
3287 1656
3288 - // Get options and determine the selected model
3289 - $options = $this->options ?? get_option('mxchat_options');
3290 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
1657 + // Retrieve OpenAI API key using 'api_key' as the option key
1658 + $api_key = isset($this->options['api_key']) ? sanitize_text_field($this->options['api_key']) : sanitize_text_field(get_option('mxchat_options')['api_key']);
3291 1659
3292 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3293 - if ($selected_model === 'custom-provider') {
3294 - return $this->interpret_query_with_custom($user_query, $system_prompt);
1660 + /*
1661 + // Log the API key check, without exposing the key
1662 + if (defined('WP_DEBUG') && WP_DEBUG) {
1663 + //error_log("Retrieved OpenAI API Key: " . ($api_key ? "Present" : "Missing"));
3295 1664 }
1665 + */
3296 1666
3297 - // Extract model prefix to determine the provider
3298 - $model_parts = explode('-', $selected_model);
3299 - $provider = strtolower($model_parts[0]);
3300 -
3301 - // Determine which API key to use based on the provider
3302 - switch ($provider) {
3303 - case 'gemini':
3304 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3305 - if (empty($api_key)) {
3306 - return sanitize_text_field($user_query); // Default to original query if API key missing
3307 - }
3308 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3309 -
3310 - case 'claude':
3311 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3312 - if (empty($api_key)) {
3313 - return sanitize_text_field($user_query);
3314 - }
3315 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3316 -
3317 - case 'grok':
3318 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3319 - if (empty($api_key)) {
3320 - return sanitize_text_field($user_query);
3321 - }
3322 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3323 -
3324 - case 'deepseek':
3325 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3326 - if (empty($api_key)) {
3327 - return sanitize_text_field($user_query);
3328 - }
3329 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3330 -
3331 - case 'gpt':
3332 - default:
3333 - // Default to OpenAI for custom models or unrecognized prefixes
3334 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3335 - if (empty($api_key)) {
3336 - return sanitize_text_field($user_query);
3337 - }
3338 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
1667 + if (empty($api_key)) {
1668 + //error_log("OpenAI API key is missing.");
1669 + return sanitize_text_field($user_query); // Default to the original query if API key is missing
3339 1670 }
3340 -}
3341 1671
3342 -/**
3343 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3344 - * Uses the same base URL + auth scheme as the chat dispatcher.
3345 - */
3346 -private function interpret_query_with_custom($user_query, $system_prompt) {
3347 - $cfg = $this->mxchat_resolve_custom_provider();
3348 - if (empty($cfg['base_url'])) {
3349 - return sanitize_text_field($user_query);
3350 - }
3351 - $args = [
3352 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3353 - 'body' => wp_json_encode([
3354 - 'model' => $cfg['model'],
3355 - 'messages' => [
3356 - ['role' => 'system', 'content' => $system_prompt],
3357 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3358 - ],
3359 - 'temperature' => 0.2,
3360 - 'max_tokens' => 20,
3361 - ]),
3362 - 'method' => 'POST',
3363 - 'timeout' => 15,
3364 - ];
3365 - $response = wp_remote_post($cfg['chat_url'], $args);
3366 - if (is_wp_error($response)) {
3367 - return sanitize_text_field($user_query);
3368 - }
3369 - $body = json_decode(wp_remote_retrieve_body($response), true);
3370 - return isset($body['choices'][0]['message']['content'])
3371 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3372 - : sanitize_text_field($user_query);
3373 -}
3374 -
3375 -/**
3376 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3377 - * into the assoc-array form wp_remote_post expects.
3378 - */
3379 -private function mxchat_custom_provider_assoc_headers($cfg) {
3380 - $headers = ['Content-Type' => 'application/json'];
3381 - if (!empty($cfg['api_key'])) {
3382 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3383 - $headers['api-key'] = $cfg['api_key'];
3384 - } else {
3385 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3386 - }
3387 - }
3388 - return $headers;
3389 -}
3390 -
3391 -/**
3392 - * Interpret query using OpenAI models
3393 - */
3394 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3395 1672 $url = 'https://api.openai.com/v1/chat/completions';
3396 1673 $args = [
3397 1674 'headers' => [
3398 1675 'Authorization' => 'Bearer ' . $api_key,
@@ -3398,9 +1675,9 @@
3398 1675 'Authorization' => 'Bearer ' . $api_key,
3399 1676 'Content-Type' => 'application/json',
3400 1677 ],
3401 1678 'body' => wp_json_encode([
3402 - 'model' => $model,
1679 + 'model' => 'gpt-3.5-turbo',
3403 1680 'messages' => [
3404 1681 ['role' => 'system', 'content' => $system_prompt],
3405 1682 ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3406 1683 ],
@@ -3407,204 +1684,188 @@
3407 1684 'temperature' => 0.2,
3408 1685 'max_tokens' => 20,
3409 1686 ]),
3410 1687 'method' => 'POST',
3411 - 'timeout' => 15,
3412 1688 ];
3413 1689
3414 1690 $response = wp_remote_post($url, $args);
1691 +
3415 1692 if (is_wp_error($response)) {
3416 - return sanitize_text_field($user_query);
1693 + //error_log("OpenAI request failed: " . $response->get_error_message());
1694 + return sanitize_text_field($user_query); // Fallback to the original query if there's an error
3417 1695 }
3418 1696
3419 1697 $body = json_decode(wp_remote_retrieve_body($response), true);
3420 - return isset($body['choices'][0]['message']['content'])
3421 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3422 - : sanitize_text_field($user_query);
3423 -}
3424 1698
3425 -/**
3426 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3427 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3428 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3429 - * stripping. We never send a `thinking` param either, which is required for
3430 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3431 - */
3432 -private function mxchat_claude_omits_temperature($model) {
3433 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3434 - return in_array($model, $no_temp, true);
3435 -}
1699 + // Check for a valid response and sanitize output
1700 + if (isset($body['choices'][0]['message']['content'])) {
1701 + $interpreted_query = sanitize_text_field(trim($body['choices'][0]['message']['content']));
3436 1702
3437 -/**
3438 - * Interpret query using Claude models
3439 - */
3440 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3441 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3442 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3443 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3444 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3445 - $url = 'https://api.anthropic.com/v1/messages';
1703 + /*
1704 + // Log the interpreted query for debugging
1705 + if (defined('WP_DEBUG') && WP_DEBUG) {
1706 + //error_log("Interpreted search query: " . $interpreted_query);
1707 + }
1708 + */
3446 1709
3447 - $payload = [
3448 - 'model' => $model,
3449 - 'system' => $system_prompt,
3450 - 'messages' => [
3451 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3452 - ],
3453 - 'max_tokens' => 20,
3454 - 'temperature' => 0.2,
3455 - ];
3456 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3457 -
3458 - $args = [
3459 - 'headers' => [
3460 - 'Content-Type' => 'application/json',
3461 - 'x-api-key' => $api_key,
3462 - 'anthropic-version' => '2023-06-01',
3463 - ],
3464 - 'body' => wp_json_encode($payload),
3465 - 'method' => 'POST',
3466 - 'timeout' => 15,
3467 - ];
3468 -
3469 - $response = wp_remote_post($url, $args);
3470 - if (is_wp_error($response)) {
1710 + return $interpreted_query;
1711 + } else {
1712 + //error_log("Unexpected API response format: " . print_r($body, true));
3471 1713 return sanitize_text_field($user_query);
3472 1714 }
1715 +}
3473 1716
3474 - $body = json_decode(wp_remote_retrieve_body($response), true);
3475 - // claude-fable-5 prepends a thinking block to content — take the first
3476 - // TEXT block, not content[0].
3477 - foreach ((array) ($body['content'] ?? array()) as $block) {
3478 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3479 - return sanitize_text_field(trim($block['text']));
3480 - }
3481 - }
3482 1717
3483 - return sanitize_text_field($user_query);
3484 -}
3485 1718
3486 -/**
3487 - * Interpret query using Gemini models
3488 - */
3489 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3490 - if ($model === 'gemini-3-pro-preview') {
3491 - $model = 'gemini-3.1-pro-preview';
3492 - }
3493 - // Use v1beta for preview models, v1 for stable models
3494 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
1719 +private function find_product_in_message($message) {
1720 + global $wpdb;
3495 1721
3496 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
1722 + // Get embedding for the search query
1723 + $query_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
3497 1724
3498 - $args = [
3499 - 'headers' => [
3500 - 'Content-Type' => 'application/json',
3501 - ],
3502 - 'body' => wp_json_encode([
3503 - 'contents' => [
3504 - [
3505 - 'role' => 'user',
3506 - 'parts' => [
3507 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3508 - ]
3509 - ]
3510 - ],
3511 - 'generationConfig' => [
3512 - 'temperature' => 0.2,
3513 - 'maxOutputTokens' => 20,
3514 - ],
3515 - ]),
3516 - 'method' => 'POST',
3517 - 'timeout' => 15,
3518 - ];
1725 + // Check if embedding generation returned an error
1726 + if (is_array($query_embedding) && isset($query_embedding['error'])) {
1727 + $error_message = $query_embedding['error'];
1728 + $error_code = $query_embedding['error_code'] ?? 'embedding_error';
1729 +
1730 + //error_log("Product search embedding error: $error_message (Code: $error_code)");
1731 +
1732 + // Set a user-friendly fallback response
1733 + $this->fallbackResponse['text'] = esc_html__("I'm having trouble processing your product search. Please try again later or contact support if this persists.", 'mxchat');
1734 +
1735 + // Also store the technical error for admin users
1736 + $this->fallbackResponse['admin_error'] = $error_message;
1737 + $this->fallbackResponse['error_code'] = $error_code;
1738 +
1739 + return null;
1740 + }
3519 1741
3520 - $response = wp_remote_post($url, $args);
3521 - if (is_wp_error($response)) {
3522 - return sanitize_text_field($user_query);
1742 + // Check if embedding is valid
1743 + if (!is_array($query_embedding) || empty($query_embedding)) {
1744 + //error_log("Failed to generate embedding for product search");
1745 + $this->fallbackResponse['text'] = esc_html__("I couldn't process your product search. Please try again with different wording.", 'mxchat');
1746 + return null;
3523 1747 }
3524 1748
3525 - $body = json_decode(wp_remote_retrieve_body($response), true);
3526 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3527 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
1749 + // Get relevant content as string
1750 + $relevant_content = $this->mxchat_find_relevant_products($query_embedding);
1751 + if (empty($relevant_content)) {
1752 + // Return null to indicate no results and set fallback response
1753 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Could you please be more specific about the product you're looking for?", 'mxchat');
1754 + return null;
3528 1755 }
3529 -
3530 - return sanitize_text_field($user_query);
3531 -}
3532 1756
3533 -/**
3534 - * Interpret query using X.AI (Grok) models
3535 - */
3536 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3537 - $url = 'https://api.xai.com/v1/chat/completions';
3538 -
3539 - $args = [
3540 - 'headers' => [
3541 - 'Content-Type' => 'application/json',
3542 - 'Authorization' => 'Bearer ' . $api_key,
3543 - ],
3544 - 'body' => wp_json_encode([
3545 - 'model' => $model,
3546 - 'messages' => [
3547 - ['role' => 'system', 'content' => $system_prompt],
3548 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3549 - ],
3550 - 'temperature' => 0.2,
3551 - 'max_tokens' => 20,
3552 - ]),
3553 - 'method' => 'POST',
3554 - 'timeout' => 15,
3555 - ];
3556 -
3557 - $response = wp_remote_post($url, $args);
3558 - if (is_wp_error($response)) {
3559 - return sanitize_text_field($user_query);
1757 +
1758 + // Extract product URLs from the content
1759 + preg_match_all('/https?:\/\/[^\s<>"\']+?\/product\/[^\s<>"\']+/', $relevant_content, $matches);
1760 +
1761 + if (!empty($matches[0])) {
1762 + // Try each URL found
1763 + foreach ($matches[0] as $url) {
1764 + // Clean the URL
1765 + $url = rtrim($url, '/."\']');
1766 +
1767 + // Get the product slug
1768 + $path = parse_url($url, PHP_URL_PATH);
1769 + $slug = basename(rtrim($path, '/'));
1770 +
1771 + // Find product by slug
1772 + $args = array(
1773 + 'post_type' => 'product',
1774 + 'post_status' => 'publish',
1775 + 'name' => $slug,
1776 + 'posts_per_page' => 1
1777 + );
1778 +
1779 + $products = get_posts($args);
1780 +
1781 + if (!empty($products)) {
1782 + $product_id = $products[0]->ID;
1783 + $product = wc_get_product($product_id);
1784 +
1785 + if ($product && $product->is_purchasable()) {
1786 + return $product_id;
1787 + }
1788 + }
1789 + }
3560 1790 }
3561 -
3562 - $body = json_decode(wp_remote_retrieve_body($response), true);
3563 - if (isset($body['choices'][0]['message']['content'])) {
3564 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
1791 +
1792 + // Fallback: Look for product names in the content
1793 + $products = wc_get_products([
1794 + 'status' => 'publish',
1795 + 'limit' => -1,
1796 + 'return' => 'all'
1797 + ]);
1798 +
1799 + foreach ($products as $product) {
1800 + $name = $product->get_name();
1801 + if (stripos($relevant_content, $name) !== false) {
1802 + if ($product->is_purchasable()) {
1803 + return $product->get_id();
1804 + }
1805 + }
3565 1806 }
3566 -
3567 - return sanitize_text_field($user_query);
1807 +
1808 + // If no product is found after all checks, set the fallback response
1809 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Try to be more specific", 'mxchat');
1810 + return null;
3568 1811 }
3569 1812
3570 -/**
3571 - * Interpret query using DeepSeek models
3572 - */
3573 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3574 - $url = 'https://api.deepseek.com/v1/chat/completions';
3575 -
3576 - $args = [
3577 - 'headers' => [
3578 - 'Content-Type' => 'application/json',
3579 - 'Authorization' => 'Bearer ' . $api_key,
3580 - ],
3581 - 'body' => wp_json_encode([
3582 - 'model' => $model,
3583 - 'messages' => [
3584 - ['role' => 'system', 'content' => $system_prompt],
3585 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3586 - ],
3587 - 'temperature' => 0.2,
3588 - 'max_tokens' => 20,
3589 - ]),
3590 - 'method' => 'POST',
3591 - 'timeout' => 15,
3592 - ];
3593 -
3594 - $response = wp_remote_post($url, $args);
3595 - if (is_wp_error($response)) {
3596 - return sanitize_text_field($user_query);
1813 +// New method to handle intent responses
1814 +private function generate_intent_response($context_content, $session_id) {
1815 + // Convert the context array to a structured string for the AI
1816 + $context_string = $this->format_intent_context($context_content);
1817 + // Generate AI response using the context
1818 + $response = $this->mxchat_generate_response(
1819 + $context_string,
1820 + $this->options['api_key'],
1821 + $this->options['xai_api_key'],
1822 + $this->options['claude_api_key'],
1823 + $this->options['deepseek_api_key'],
1824 + $this->options['gemini_api_key'], // Added Gemini API key
1825 + $this->mxchat_fetch_conversation_history_for_ai($session_id)
1826 + );
1827 + $this->fallbackResponse['text'] = $response;
1828 + return true;
1829 +}
1830 +
1831 +// Helper method to format intent context
1832 +private function format_intent_context($context) {
1833 + $context_string = esc_html__("INTENT CONTEXT:\n", 'mxchat');
1834 +
1835 + switch ($context['intent']) {
1836 + case 'add_to_cart':
1837 + if ($context['status'] === 'success') {
1838 + $context_string .= esc_html__("Action: Successfully added product to cart\n", 'mxchat');
1839 + $context_string .= sprintf(esc_html__("Product: %s\n", 'mxchat'), $context['product']['name']);
1840 + $context_string .= esc_html__("Available actions: ", 'mxchat') . implode(', ', $context['available_actions']) . "\n";
1841 + $context_string .= sprintf(esc_html__("Cart URL: %s\n", 'mxchat'), $context['cart_url']);
1842 + $context_string .= esc_html__("\nPlease inform the user of the successful addition and their available options.", 'mxchat');
1843 + } else {
1844 + $context_string .= esc_html__("Action: Failed to add product to cart\n", 'mxchat');
1845 + $context_string .= sprintf(esc_html__("Reason: %s\n", 'mxchat'), $context['reason']);
1846 + switch ($context['reason']) {
1847 + case 'woocommerce_not_available':
1848 + $context_string .= esc_html__("\nPlease inform the user that shopping features are not available.", 'mxchat');
1849 + break;
1850 + case 'no_product_context':
1851 + $context_string .= esc_html__("\nPlease ask the user to specify which product they want to add.", 'mxchat');
1852 + break;
1853 + case 'product_not_found':
1854 + $context_string .= esc_html__("\nPlease inform the user that the product couldn't be found and ask them to try again.", 'mxchat');
1855 + break;
1856 + case 'add_to_cart_failed':
1857 + $context_string .= esc_html__("\nPlease apologize to the user and suggest they try again or ask for assistance.", 'mxchat');
1858 + break;
1859 + }
1860 + }
1861 + break;
3597 1862 }
3598 -
3599 - $body = json_decode(wp_remote_retrieve_body($response), true);
3600 - if (isset($body['choices'][0]['message']['content'])) {
3601 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3602 - }
3603 -
3604 - return sanitize_text_field($user_query);
1863 +
1864 + return $context_string;
3605 1865 }
3606 1866
1867 +
3607 1868 //very good
3608 1869 private function add_email_to_loops($email) {
3609 1870 // Sanitize the email
3610 1871 $email = sanitize_email($email);
@@ -3690,207 +1951,95 @@
3690 1951 $this->fallbackResponse['text'] = '';
3691 1952 }
3692 1953
3693 1954
3694 -/**
3695 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3696 - */
3697 1955 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3698 - // CLEAR DEBUG LOGGING
3699 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3700 - //error_log("PDF Source: " . $pdf_source);
3701 - //error_log("Max Pages: " . $max_pages);
3702 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3703 -
3704 - // Check if Advanced Claude Toolbar is available and enabled
3705 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3706 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3707 -
3708 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3709 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3710 -
3711 - if ($claude_available && $claude_enabled) {
3712 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3713 -
3714 - // Attempt Claude processing first
3715 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3716 -
3717 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3718 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3719 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3720 -
3721 - // Log first page details for verification
3722 - if (isset($claude_result[0])) {
3723 - $first_page = $claude_result[0];
3724 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3725 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3726 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3727 - }
3728 -
3729 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3730 - return $claude_result;
3731 - } else {
3732 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3733 - //error_log("Claude result type: " . gettype($claude_result));
3734 - if (is_array($claude_result)) {
3735 - //error_log("Claude result count: " . count($claude_result));
3736 - }
3737 - }
3738 - }
3739 -
3740 - // Fallback to basic processing
3741 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3742 -
3743 1956 $upload_dir = wp_upload_dir();
3744 1957 $temp_file = null;
3745 -
1958 +
3746 1959 try {
3747 - // Your existing basic processing code here...
3748 - // (I'll include the key parts with debug logging)
3749 -
1960 + // Handle URL vs local file
3750 1961 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3751 - //error_log("Downloading PDF from URL...");
3752 -
3753 - // SECURITY FIX: Validate URL before processing
3754 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3755 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
1962 + // Validate and download the file from URL
1963 + $temp_file = wp_tempnam($pdf_source); // Safe temporary file name
1964 + $response = wp_remote_get($pdf_source, ['timeout' => 60]);
1965 +
1966 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1967 + //error_log(esc_html__("Failed to download PDF. Error: ", 'mxchat') . print_r($response, true));
3756 1968 return false;
3757 1969 }
3758 -
3759 - $temp_file = wp_tempnam($pdf_source);
3760 -
3761 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3762 - $response = wp_safe_remote_get($pdf_source, [
3763 - 'timeout' => 60,
3764 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3765 - ]);
3766 -
3767 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3768 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3769 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
1970 +
1971 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
1972 +
1973 + // Validate that the downloaded file is a PDF
1974 + $mime_type = mime_content_type($temp_file);
1975 + if ($mime_type !== 'application/pdf') {
1976 + //error_log(esc_html__("Invalid MIME type detected for PDF: ", 'mxchat') . $mime_type);
1977 + unlink($temp_file);
3770 1978 return false;
3771 1979 }
3772 -
3773 - global $wp_filesystem;
3774 - if (empty($wp_filesystem)) {
3775 - require_once ABSPATH . 'wp-admin/includes/file.php';
3776 - WP_Filesystem();
3777 - }
3778 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3779 - //error_log("✅ PDF downloaded successfully");
3780 1980 } else {
1981 + // For local files, use the provided path directly
3781 1982 $temp_file = $pdf_source;
3782 - //error_log("Using local PDF file: " . $temp_file);
3783 1983 }
3784 -
3785 - // Parse PDF
3786 - //error_log("Parsing PDF with basic parser...");
3787 - mxchat_load_pdf_parser();
1984 +
1985 + // Parse and process the PDF
3788 1986 $parser = new \Smalot\PdfParser\Parser();
3789 1987 $pdf = $parser->parseFile($temp_file);
3790 1988 $pages = $pdf->getPages();
3791 -
3792 - //error_log("PDF contains " . count($pages) . " pages");
3793 -
1989 +
3794 1990 if (count($pages) > $max_pages) {
3795 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3796 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
1991 + //error_log(esc_html__("PDF exceeds the maximum allowed pages: ", 'mxchat') . count($pages));
1992 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3797 1993 unlink($temp_file);
3798 1994 }
3799 - return 'too_many_pages';
1995 + return esc_html__('too_many_pages', 'mxchat');
3800 1996 }
3801 -
1997 +
3802 1998 $embeddings = [];
3803 - $processed_pages = 0;
3804 -
3805 1999 foreach ($pages as $page_number => $page) {
3806 2000 $text = $page->getText();
3807 -
2001 +
2002 + // Ensure text is non-empty before generating embeddings
3808 2003 if (empty(trim($text))) {
3809 - //error_log("Skipping empty page: " . ($page_number + 1));
2004 + //error_log(esc_html__("Skipping empty page: ", 'mxchat') . ($page_number + 1));
3810 2005 continue;
3811 2006 }
3812 -
3813 - $text = $this->mxchat_clean_text($text);
3814 -
2007 +
3815 2008 $embedding = $this->mxchat_generate_embedding(
3816 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
2009 + esc_html__("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3817 2010 $this->options['api_key']
3818 2011 );
3819 -
2012 +
3820 2013 if ($embedding) {
3821 2014 $embeddings[] = [
3822 2015 'page_number' => $page_number + 1,
3823 2016 'embedding' => $embedding,
3824 2017 'text' => $text,
3825 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3826 - 'processing_method' => 'basic_pdf_parser'
3827 2018 ];
3828 - $processed_pages++;
2019 + } else {
2020 + //error_log(esc_html__("Failed to generate embedding for page ", 'mxchat') . ($page_number + 1));
3829 2021 }
3830 2022 }
3831 -
3832 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3833 -
3834 - // Cleanup
3835 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
2023 +
2024 + // Clean up downloaded file if it was from URL
2025 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3836 2026 unlink($temp_file);
3837 2027 }
3838 -
3839 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
2028 +
3840 2029 return $embeddings;
3841 -
2030 +
3842 2031 } catch (\Exception $e) {
3843 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
2032 + // //error_log(esc_html__("Error parsing or processing PDF: ", 'mxchat') . $e->getMessage());
2033 +
2034 + // Cleanup in case of exception
3844 2035 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3845 2036 unlink($temp_file);
3846 2037 }
3847 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3848 - return false;
3849 - }
3850 -}
3851 2038
3852 -
3853 -/**
3854 - * Validate PDF URL for security
3855 - * Prevents SSRF attacks by blocking dangerous URLs
3856 - */
3857 -
3858 -private function mxchat_is_safe_pdf_url($url) {
3859 - // Use WordPress core function for comprehensive validation
3860 - // This blocks localhost, private IPs, and reserved IP ranges
3861 - $validated_url = wp_http_validate_url($url);
3862 -
3863 - if ($validated_url === false) {
3864 2039 return false;
3865 2040 }
3866 -
3867 - // Additional check: only allow HTTP/HTTPS schemes
3868 - $parsed = parse_url($url);
3869 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3870 - return false;
3871 - }
3872 -
3873 - return true;
3874 2041 }
3875 -
3876 -
3877 -private function mxchat_clean_text($text) {
3878 - // Remove excessive whitespace
3879 - $text = preg_replace('/\s+/', ' ', $text);
3880 -
3881 - // Remove control characters except newlines and tabs
3882 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3883 -
3884 - // Normalize line endings
3885 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3886 -
3887 - // Trim whitespace
3888 - $text = trim($text);
3889 -
3890 - return $text;
3891 -}
3892 -
3893 2042 private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3894 2043 //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3895 2044
3896 2045 $most_relevant = null;
@@ -3913,14 +2062,11 @@
3913 2062 }
3914 2063
3915 2064 return [];
3916 2065 }
3917 -
3918 -
2066 +// Add this to your class
3919 2067 public function handle_pdf_upload() {
3920 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3921 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3922 - }
2068 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3923 2069
3924 2070 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3925 2071 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3926 2072 return;
@@ -3925,29 +2071,12 @@
3925 2071 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3926 2072 return;
3927 2073 }
3928 2074
3929 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3930 - $options = get_option('mxchat_options', array());
3931 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3932 -
3933 - if ($show_pdf_button !== 'on') {
3934 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3935 - return;
3936 - }
3937 -
3938 2075 $file = $_FILES['pdf_file'];
3939 2076 $session_id = sanitize_text_field($_POST['session_id']);
3940 2077 $original_filename = sanitize_text_field($file['name']);
3941 2078
3942 - // Update session owner if it changed (e.g. IP changed due to network switch)
3943 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3944 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3945 -
3946 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3947 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3948 - }
3949 -
3950 2079 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3951 2080 if ($file_type['type'] !== 'application/pdf') {
3952 2081 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3953 2082 return;
@@ -3953,12 +2082,9 @@
3953 2082 return;
3954 2083 }
3955 2084
3956 2085 $upload_dir = wp_upload_dir();
3957 -
3958 - // SECURITY FIX: Generate random filename without exposing session_id
3959 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3960 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2086 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3961 2087 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3962 2088
3963 2089 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3964 2090 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3989,9 +2115,8 @@
3989 2115 return;
3990 2116 }
3991 2117
3992 2118 if (!empty($embeddings)) {
3993 - // Store the mapping between session and the random filename
3994 2119 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3995 2120 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3996 2121 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3997 2122 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -4012,11 +2137,9 @@
4012 2137 wp_send_json_error($error_message);
4013 2138 return;
4014 2139 }
4015 2140 public function handle_pdf_remove() {
4016 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
4017 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4018 - }
2141 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
4019 2142
4020 2143 if (empty($_POST['session_id'])) {
4021 2144 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
4022 2145 wp_die();
@@ -4037,8 +2160,10 @@
4037 2160 wp_die();
4038 2161 }
4039 2162
4040 2163
2164 +
2165 +
4041 2166 function mxchat_fetch_new_messages() {
4042 2167 $session_id = sanitize_text_field($_POST['session_id']);
4043 2168 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
4044 2169 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -4051,31 +2176,14 @@
4051 2176 }
4052 2177
4053 2178 $history = get_option("mxchat_history_{$session_id}", []);
4054 2179
4055 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
4056 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
4057 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
4058 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
4059 -
4060 2180 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
4061 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
4062 -
4063 2181 // If persistence is enabled, show all new messages
4064 2182 if ($persistence_enabled) {
4065 - $has_id = !empty($message['id']);
4066 - $is_agent = $message['role'] === 'agent';
4067 -
4068 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
4069 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
4070 - $is_newer = true;
4071 - } else {
4072 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
4073 - }
4074 -
4075 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
4076 -
4077 - return $has_id && $is_newer && $is_agent;
2183 + return !empty($message['id']) &&
2184 + strcmp($message['id'], $last_seen_id) > 0 &&
2185 + $message['role'] === 'agent';
4078 2186 }
4079 2187
4080 2188 // If persistence is disabled, only show messages after initial timestamp
4081 2189 return !empty($message['id']) &&
@@ -4082,19 +2190,17 @@
4082 2190 $message['role'] === 'agent' &&
4083 2191 $message['timestamp'] > $initial_timestamp;
4084 2192 });
4085 2193
4086 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2194 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
4087 2195
4088 - // Include current chat mode so frontend can detect agent→AI transitions
4089 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
4090 -
4091 2196 wp_send_json_success([
4092 - 'new_messages' => array_values($new_messages),
4093 - 'chat_mode' => $chat_mode
2197 + 'new_messages' => array_values($new_messages)
4094 2198 ]);
4095 2199 wp_die();
4096 2200 }
2201 +
2202 +
4097 2203 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
4098 2204 // First check if live agents are available
4099 2205 $live_agent_available = $this->options['live_agent_status'] ?? 'off';
4100 2206 if ($live_agent_available !== 'on') {
@@ -4113,101 +2219,18 @@
4113 2219 ]);
4114 2220 wp_die();
4115 2221 }
4116 2222
4117 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4118 -
4119 - if (empty($slack_bot_token)) {
2223 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2224 + if (empty($slack_webhook_url)) {
4120 2225 return false;
4121 2226 }
4122 2227
4123 - // Check if channel already exists for this session
4124 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4125 -
4126 - if (empty($channel_id)) {
4127 - // Create new channel with session ID as name
4128 - $channel_name = $this->generate_channel_name($session_id);
4129 -
4130 - //error_log("Attempting to create channel: $channel_name");
4131 -
4132 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4133 - 'headers' => [
4134 - 'Content-Type' => 'application/json',
4135 - 'Authorization' => 'Bearer ' . $slack_bot_token
4136 - ],
4137 - 'body' => json_encode([
4138 - 'name' => $channel_name,
4139 - 'is_private' => false // Public channel - anyone in workspace can join
4140 - ])
4141 - ]);
4142 -
4143 - if (!is_wp_error($response)) {
4144 - $response_body = wp_remote_retrieve_body($response);
4145 - $response_data = json_decode($response_body, true);
4146 -
4147 - //error_log("Channel creation response: " . $response_body);
4148 -
4149 - if (isset($response_data['ok']) && $response_data['ok']) {
4150 - $channel_id = $response_data['channel']['id'];
4151 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4152 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4153 - update_option("mxchat_channel_{$session_id}", $channel_id);
4154 -
4155 - // Auto-invite agents to the channel
4156 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4157 -
4158 - if (!empty($agent_user_ids)) {
4159 - // Parse user IDs (one per line)
4160 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4161 -
4162 - foreach ($user_ids as $user_id_to_invite) {
4163 - //error_log("Inviting user to channel: $user_id_to_invite");
4164 -
4165 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4166 - 'headers' => [
4167 - 'Content-Type' => 'application/json',
4168 - 'Authorization' => 'Bearer ' . $slack_bot_token
4169 - ],
4170 - 'body' => json_encode([
4171 - 'channel' => $channel_id,
4172 - 'users' => $user_id_to_invite
4173 - ])
4174 - ]);
4175 -
4176 - if (!is_wp_error($invite_response)) {
4177 - $invite_body = wp_remote_retrieve_body($invite_response);
4178 - $invite_data = json_decode($invite_body, true);
4179 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4180 -
4181 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4182 - //error_log("Successfully invited user $user_id_to_invite to channel");
4183 - } else {
4184 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4185 - }
4186 - } else {
4187 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4188 - }
4189 - }
4190 - } else {
4191 - //error_log("No agent user IDs configured for auto-invite");
4192 - }
4193 - } else {
4194 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4195 - }
4196 - } else {
4197 - //error_log("WP Error creating channel: " . $response->get_error_message());
4198 - }
4199 -
4200 - if (empty($channel_id)) {
4201 - return false; // Failed to create channel
4202 - }
4203 - }
4204 -
4205 - // Get recent chat history
2228 + // Get recent chat history (last 5 messages)
4206 2229 $history = get_option("mxchat_history_{$session_id}", []);
4207 - $recent_history = array_slice($history, -5);
2230 + $recent_history = array_slice($history, -5); // Get last 5 messages
4208 2231
4209 - // Format conversation context
2232 + // Format conversation history
4210 2233 $conversation_context = "";
4211 2234 if (!empty($recent_history)) {
4212 2235 $conversation_context = "*Recent Conversation:*\n";
4213 2236 foreach ($recent_history as $hist_message) {
@@ -4218,296 +2241,84 @@
4218 2241 }
4219 2242
4220 2243 update_option("mxchat_mode_{$session_id}", 'agent');
4221 2244
4222 - // Send message to channel
4223 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4224 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4225 - $channel_message .= "*User ID:* `{$user_id}`\n";
2245 + $webhook_data = [
2246 + 'blocks' => [
2247 + [
2248 + 'type' => 'header',
2249 + 'text' => [
2250 + 'type' => 'plain_text',
2251 + 'text' => '🔔 New Live Agent Request',
2252 + 'emoji' => true
2253 + ]
2254 + ],
2255 + [
2256 + 'type' => 'section',
2257 + 'fields' => [
2258 + [
2259 + 'type' => 'mrkdwn',
2260 + 'text' => sprintf('*User ID:*\n`%s`', $user_id)
2261 + ],
2262 + [
2263 + 'type' => 'mrkdwn',
2264 + 'text' => sprintf('*Session ID:*\n`%s`', $session_id)
2265 + ]
2266 + ]
2267 + ]
2268 + ]
2269 + ];
4226 2270
4227 - // Surface the captured visitor identity so the agent knows who they're talking to —
4228 - // guest User IDs are 0, but the pre-chat gate / login / transcript often has name+email (plan-e2195b).
4229 - $visitor = $this->mxchat_get_visitor_identity($session_id);
4230 - if (!empty($visitor['name']) && !empty($visitor['email'])) {
4231 - $channel_message .= "*Visitor:* {$visitor['name']} <{$visitor['email']}>\n";
4232 - } elseif (!empty($visitor['email'])) {
4233 - $channel_message .= "*Visitor:* <{$visitor['email']}>\n";
4234 - } elseif (!empty($visitor['name'])) {
4235 - $channel_message .= "*Visitor:* {$visitor['name']}\n";
2271 + // Add conversation history if exists
2272 + if (!empty($conversation_context)) {
2273 + $webhook_data['blocks'][] = [
2274 + 'type' => 'section',
2275 + 'text' => [
2276 + 'type' => 'mrkdwn',
2277 + 'text' => $conversation_context
2278 + ]
2279 + ];
4236 2280 }
4237 - $channel_message .= "\n";
4238 2281
4239 - if (!empty($conversation_context)) {
4240 - $channel_message .= $conversation_context;
4241 - }
4242 -
4243 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4244 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
2282 + // Add the current message
2283 + $webhook_data['blocks'][] = [
2284 + 'type' => 'section',
2285 + 'text' => [
2286 + 'type' => 'mrkdwn',
2287 + 'text' => sprintf('*Current Message:*\n%s', $message)
2288 + ]
2289 + ];
4245 2290
4246 - wp_remote_post('https://slack.com/api/chat.postMessage', [
2291 + // Add the reply button
2292 + $webhook_data['blocks'][] = [
2293 + 'type' => 'actions',
2294 + 'elements' => [
2295 + [
2296 + 'type' => 'button',
2297 + 'text' => [
2298 + 'type' => 'plain_text',
2299 + 'text' => '✍️ Reply',
2300 + 'emoji' => true
2301 + ],
2302 + 'value' => $session_id,
2303 + 'action_id' => 'reply_to_user',
2304 + 'style' => 'primary'
2305 + ]
2306 + ]
2307 + ];
2308 +
2309 + $response = wp_remote_post($slack_webhook_url, [
2310 + 'body' => json_encode($webhook_data),
4247 2311 'headers' => [
4248 2312 'Content-Type' => 'application/json',
4249 - 'Authorization' => 'Bearer ' . $slack_bot_token
4250 2313 ],
4251 - 'body' => json_encode([
4252 - 'channel' => $channel_id,
4253 - 'text' => $channel_message,
4254 - 'mrkdwn' => true
4255 - ])
4256 2314 ]);
4257 2315
4258 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4259 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4260 -
4261 - $this->fallbackResponse = [
4262 - 'text' => $success_message,
4263 - 'html' => '',
4264 - 'images' => [],
4265 - 'chat_mode' => 'agent'
4266 - ];
4267 -
4268 - wp_send_json([
4269 - 'success' => true,
4270 - 'text' => $success_message,
4271 - 'html' => '',
4272 - 'chat_mode' => 'agent',
4273 - 'session_id' => $session_id,
4274 - 'fallbackResponse' => $this->fallbackResponse
4275 - ]);
4276 - wp_die();
4277 -}
4278 -
4279 -private function generate_channel_name($session_id) {
4280 - $email = null;
4281 - $name = null;
4282 -
4283 - // 1. First priority: Check if user is logged in and get their info
4284 - if (is_user_logged_in()) {
4285 - $current_user = wp_get_current_user();
4286 - if (!empty($current_user->user_email)) {
4287 - $email = $current_user->user_email;
4288 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4289 - }
4290 - if (!empty($current_user->display_name)) {
4291 - $name = $current_user->display_name;
4292 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4293 - }
4294 - }
4295 -
4296 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4297 - if (empty($email)) {
4298 - $email_option_key = "mxchat_email_{$session_id}";
4299 - $saved_email = get_option($email_option_key);
4300 - if (!empty($saved_email)) {
4301 - $email = $saved_email;
4302 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4303 - }
4304 - }
4305 -
4306 - if (empty($name)) {
4307 - $name_option_key = "mxchat_name_{$session_id}";
4308 - $saved_name = get_option($name_option_key);
4309 - if (!empty($saved_name)) {
4310 - $name = $saved_name;
4311 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4312 - }
4313 - }
4314 -
4315 - // 3. Third priority: Check existing chat transcript for email/name
4316 - if (empty($email) || empty($name)) {
4317 - global $wpdb;
4318 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4319 - $existing_data = $wpdb->get_row($wpdb->prepare(
4320 - "SELECT user_email, user_name FROM $table_name WHERE session_id = %s AND (user_email IS NOT NULL OR user_name IS NOT NULL) LIMIT 1",
4321 - $session_id
4322 - ));
4323 -
4324 - if ($existing_data) {
4325 - if (empty($email) && !empty($existing_data->user_email)) {
4326 - $email = $existing_data->user_email;
4327 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4328 - }
4329 - if (empty($name) && !empty($existing_data->user_name)) {
4330 - $name = $existing_data->user_name;
4331 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4332 - }
4333 - }
4334 - }
4335 -
4336 - // 4. Generate channel name based on priority: Name > Email > Session ID
4337 - $channel_name = '';
4338 -
4339 - if (!empty($name)) {
4340 - // Convert name to valid Slack channel name
4341 - $base_name = strtolower(trim($name));
4342 - // Replace spaces and invalid characters
4343 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4344 - $base_name = preg_replace('/\s+/', '-', $base_name);
4345 - $base_name = trim($base_name, '-');
4346 -
4347 - // Get last 4 characters of session ID for uniqueness
4348 - $session_suffix = substr($session_id, -4);
4349 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4350 -
4351 - // Slack channel names have a 21 character limit
4352 - if (strlen($channel_name) > 21) {
4353 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4354 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4355 - $truncated_name = substr($base_name, 0, $available_space);
4356 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4357 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4358 - }
4359 -
4360 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4361 -
4362 - } elseif (!empty($email)) {
4363 - // Convert email to valid Slack channel name (your existing logic)
4364 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4365 - // Remove any remaining invalid characters
4366 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4367 - // Ensure it doesn't end with a hyphen
4368 - $channel_name = rtrim($channel_name, '-');
4369 - // Slack channel names have a 21 character limit, so truncate if needed
4370 - if (strlen($channel_name) > 21) {
4371 - $channel_name = substr($channel_name, 0, 21);
4372 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4373 - }
4374 -
4375 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4376 -
4377 - } else {
4378 - // Fallback to session ID if no name or email found
4379 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4380 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4381 - }
4382 -
4383 - // Final validation - ensure channel name meets Slack requirements
4384 - if (strlen($channel_name) > 21) {
4385 - $channel_name = substr($channel_name, 0, 21);
4386 - $channel_name = rtrim($channel_name, '-');
4387 - }
4388 -
4389 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4390 - return $channel_name;
4391 -}
4392 -
4393 -/**
4394 - * Telegram Live Agent Handover
4395 - * Creates a forum topic in the Telegram group and notifies agents
4396 - */
4397 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4398 - // Check if Telegram agents are available
4399 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4400 - if ($telegram_available !== 'on') {
4401 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4402 - $this->fallbackResponse = [
4403 - 'text' => $away_message,
4404 - 'html' => '',
4405 - 'images' => [],
4406 - 'chat_mode' => 'ai'
4407 - ];
4408 - wp_send_json([
4409 - 'text' => $away_message,
4410 - 'html' => '',
4411 - 'chat_mode' => 'ai',
4412 - 'session_id' => $session_id
4413 - ]);
4414 - wp_die();
4415 - }
4416 -
4417 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4418 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4419 -
4420 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
2316 + if (is_wp_error($response)) {
4421 2317 return false;
4422 2318 }
4423 2319
4424 - // Check if topic already exists for this session
4425 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4426 -
4427 - if (empty($topic_id)) {
4428 - // Generate topic name
4429 - $topic_name = $this->generate_telegram_topic_name($session_id);
4430 -
4431 - // Random icon color (Telegram forum topic colors)
4432 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4433 - $icon_color = $icon_colors[array_rand($icon_colors)];
4434 -
4435 - // Create forum topic
4436 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4437 - 'headers' => ['Content-Type' => 'application/json'],
4438 - 'body' => json_encode([
4439 - 'chat_id' => $telegram_group_id,
4440 - 'name' => $topic_name,
4441 - 'icon_color' => $icon_color
4442 - ])
4443 - ]);
4444 -
4445 - if (!is_wp_error($response)) {
4446 - $response_body = wp_remote_retrieve_body($response);
4447 - $response_data = json_decode($response_body, true);
4448 -
4449 - if (isset($response_data['ok']) && $response_data['ok']) {
4450 - $topic_id = $response_data['result']['message_thread_id'];
4451 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4452 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4453 - }
4454 - }
4455 -
4456 - if (empty($topic_id)) {
4457 - return false; // Failed to create topic
4458 - }
4459 - }
4460 -
4461 - // Get recent chat history
4462 - $history = get_option("mxchat_history_{$session_id}", []);
4463 - $recent_history = array_slice($history, -5);
4464 -
4465 - // Format conversation context for Telegram (HTML format)
4466 - $conversation_context = "";
4467 - if (!empty($recent_history)) {
4468 - $conversation_context = "<b>Recent Conversation:</b>\n";
4469 - foreach ($recent_history as $hist_message) {
4470 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4471 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4472 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4473 - }
4474 - $conversation_context .= "\n";
4475 - }
4476 -
4477 - // Get user info
4478 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4479 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4480 -
4481 - // Update session mode
4482 - update_option("mxchat_mode_{$session_id}", 'agent');
4483 -
4484 - // Send initial message to topic
4485 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4486 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4487 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4488 - $topic_message .= "<b>User:</b> {$user_name}\n";
4489 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4490 -
4491 - if (!empty($conversation_context)) {
4492 - $topic_message .= $conversation_context;
4493 - }
4494 -
4495 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4496 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4497 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4498 -
4499 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4500 - 'headers' => ['Content-Type' => 'application/json'],
4501 - 'body' => json_encode([
4502 - 'chat_id' => $telegram_group_id,
4503 - 'message_thread_id' => $topic_id,
4504 - 'text' => $topic_message,
4505 - 'parse_mode' => 'HTML'
4506 - ])
4507 - ]);
4508 -
4509 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
2320 + $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4510 2321 $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4511 2322
4512 2323 $this->fallbackResponse = [
4513 2324 'text' => $success_message,
@@ -4525,278 +2336,79 @@
4525 2336 'fallbackResponse' => $this->fallbackResponse
4526 2337 ]);
4527 2338 wp_die();
4528 2339 }
4529 -
4530 -/**
4531 - * Generate topic name for Telegram forum
4532 - */
4533 -private function generate_telegram_topic_name($session_id) {
4534 - $name = null;
4535 - $email = null;
4536 -
4537 - // Check logged in user
4538 - if (is_user_logged_in()) {
4539 - $current_user = wp_get_current_user();
4540 - if (!empty($current_user->display_name)) {
4541 - $name = $current_user->display_name;
4542 - }
4543 - if (!empty($current_user->user_email)) {
4544 - $email = $current_user->user_email;
4545 - }
4546 - }
4547 -
4548 - // Check session data
4549 - if (empty($name)) {
4550 - $name = get_option("mxchat_name_{$session_id}");
4551 - }
4552 - if (empty($email)) {
4553 - $email = get_option("mxchat_email_{$session_id}");
4554 - }
4555 -
4556 - // Generate topic name
4557 - $session_suffix = substr($session_id, -6);
4558 -
4559 - if (!empty($name)) {
4560 - // Clean name for topic (max 128 chars in Telegram)
4561 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4562 - $clean_name = trim($clean_name);
4563 - if (strlen($clean_name) > 50) {
4564 - $clean_name = substr($clean_name, 0, 50);
4565 - }
4566 - return "Chat - {$clean_name} ({$session_suffix})";
4567 - } elseif (!empty($email)) {
4568 - // Use email prefix
4569 - $email_prefix = explode('@', $email)[0];
4570 - if (strlen($email_prefix) > 30) {
4571 - $email_prefix = substr($email_prefix, 0, 30);
4572 - }
4573 - return "Chat - {$email_prefix} ({$session_suffix})";
4574 - }
4575 -
4576 - return "Chat - {$session_suffix}";
4577 -}
4578 -
4579 -/**
4580 - * Send user message to Telegram agent
4581 - */
4582 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4583 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4584 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4585 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4586 -
4587 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4588 - return false;
4589 - }
4590 -
4591 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4592 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4593 -
4594 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4595 - 'headers' => ['Content-Type' => 'application/json'],
4596 - 'body' => json_encode([
4597 - 'chat_id' => $group_id,
4598 - 'message_thread_id' => $topic_id,
4599 - 'text' => $user_message,
4600 - 'parse_mode' => 'HTML'
4601 - ])
4602 - ]);
4603 -
4604 - return !is_wp_error($response);
4605 -}
4606 -
4607 -/**
4608 - * Handle incoming Telegram webhook
4609 - */
4610 -public function handle_telegram_webhook(WP_REST_Request $request) {
4611 - $body = $request->get_body();
4612 - $data = json_decode($body, true);
4613 -
4614 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4615 -
4616 - // Handle message events from forum topics
4617 - if (isset($data['message'])) {
4618 - $message_data = $data['message'];
4619 -
4620 - // Skip if not from a forum topic
4621 - if (!isset($message_data['message_thread_id'])) {
4622 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4623 - return new WP_REST_Response(['ok' => true]);
4624 - }
4625 -
4626 - // Skip bot messages
4627 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4628 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4629 - return new WP_REST_Response(['ok' => true]);
4630 - }
4631 -
4632 - $chat_id = $message_data['chat']['id'] ?? '';
4633 - $topic_id = $message_data['message_thread_id'];
4634 - $message_text = $message_data['text'] ?? '';
4635 - $message_id = $message_data['message_id'] ?? '';
4636 - $from = $message_data['from'] ?? [];
4637 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4638 - if (empty($agent_name)) {
4639 - $agent_name = $from['username'] ?? 'Agent';
4640 - }
4641 -
4642 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4643 -
4644 - // Skip empty messages
4645 - if (empty($message_text)) {
4646 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4647 - return new WP_REST_Response(['ok' => true]);
4648 - }
4649 -
4650 - // Find session ID by topic ID - cast to string for comparison
4651 - global $wpdb;
4652 - $topic_id_str = strval($topic_id);
4653 - $session_option = $wpdb->get_var(
4654 - $wpdb->prepare(
4655 - "SELECT option_name FROM {$wpdb->options}
4656 - WHERE option_name LIKE %s
4657 - AND option_value = %s",
4658 - 'mxchat_telegram_topic_%',
4659 - $topic_id_str
4660 - )
4661 - );
4662 -
4663 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4664 -
4665 - if ($session_option) {
4666 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4667 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4668 -
4669 - // Verify the group ID matches
4670 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4671 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4672 -
4673 - if (strval($stored_group_id) != strval($chat_id)) {
4674 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4675 - return new WP_REST_Response(['ok' => true]);
4676 - }
4677 -
4678 - // Check for closure commands
4679 - $lower_text = strtolower(trim($message_text));
4680 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4681 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4682 - // End the live agent session
4683 - update_option("mxchat_mode_{$session_id}", 'ai');
4684 -
4685 - // Save disconnect message
4686 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4687 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4688 -
4689 - // Notify in Telegram
4690 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4691 - if (!empty($telegram_bot_token)) {
4692 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4693 - 'headers' => ['Content-Type' => 'application/json'],
4694 - 'body' => json_encode([
4695 - 'chat_id' => $chat_id,
4696 - 'message_thread_id' => $topic_id,
4697 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4698 - 'parse_mode' => 'HTML'
4699 - ])
4700 - ]);
4701 -
4702 - // Optionally close the topic
4703 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4704 - 'headers' => ['Content-Type' => 'application/json'],
4705 - 'body' => json_encode([
4706 - 'chat_id' => $chat_id,
4707 - 'message_thread_id' => $topic_id
4708 - ])
4709 - ]);
4710 - }
4711 -
4712 - return new WP_REST_Response(['ok' => true]);
4713 - }
4714 -
4715 - // Deduplicate messages
4716 - $message_key = md5($session_id . $message_id . $message_text);
4717 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4718 -
4719 - if (in_array($message_key, $processed_messages)) {
4720 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4721 - return new WP_REST_Response(['ok' => true]);
4722 - }
4723 -
4724 - $processed_messages[] = $message_key;
4725 - if (count($processed_messages) > 50) {
4726 - $processed_messages = array_slice($processed_messages, -50);
4727 - }
4728 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4729 -
4730 - // Save the agent message - format with agent name prefix for proper parsing
4731 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4732 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4733 -
4734 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4735 -
4736 - // Verify the message was saved to history
4737 - $history = get_option("mxchat_history_{$session_id}", []);
4738 - $last_message = end($history);
4739 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4740 -
4741 - // Send confirmation back to Telegram
4742 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4743 - if (!empty($telegram_bot_token)) {
4744 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4745 - if (!get_transient($confirm_key)) {
4746 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4747 - 'headers' => ['Content-Type' => 'application/json'],
4748 - 'body' => json_encode([
4749 - 'chat_id' => $chat_id,
4750 - 'message_thread_id' => $topic_id,
4751 - 'text' => "✅ <i>Message sent to user</i>",
4752 - 'parse_mode' => 'HTML',
4753 - 'reply_to_message_id' => $message_id
4754 - ])
4755 - ]);
4756 - set_transient($confirm_key, true, 300);
4757 - }
4758 - }
4759 - } else {
4760 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4761 - }
4762 - } else {
4763 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4764 - }
4765 -
4766 - return new WP_REST_Response(['ok' => true]);
4767 -}
4768 -
4769 2340 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4770 - // Check if this is a Telegram agent session
4771 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4772 - if (!empty($telegram_topic_id)) {
4773 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4774 - }
2341 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
4775 2342
4776 - // Otherwise, try Slack
4777 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4778 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4779 -
4780 - if (empty($slack_bot_token) || empty($channel_id)) {
2343 + if (empty($slack_webhook_url)) {
2344 + //error_log(esc_html__('Slack Webhook URL is not configured.', 'mxchat'));
4781 2345 return false;
4782 2346 }
4783 2347
4784 - $user_message = "💬 *User:* {$message}";
2348 + $webhook_data = [
2349 + 'blocks' => [
2350 + [
2351 + 'type' => 'header',
2352 + 'text' => [
2353 + 'type' => 'plain_text',
2354 + 'text' => esc_html__('📩 New Chat Message', 'mxchat'),
2355 + 'emoji' => true
2356 + ]
2357 + ],
2358 + [
2359 + 'type' => 'section',
2360 + 'fields' => [
2361 + [
2362 + 'type' => 'mrkdwn',
2363 + 'text' => sprintf(esc_html__('*User ID:*\n`%s`', 'mxchat'), $user_id)
2364 + ],
2365 + [
2366 + 'type' => 'mrkdwn',
2367 + 'text' => sprintf(esc_html__('*Session ID:*\n`%s`', 'mxchat'), $session_id)
2368 + ]
2369 + ]
2370 + ],
2371 + [
2372 + 'type' => 'section',
2373 + 'text' => [
2374 + 'type' => 'mrkdwn',
2375 + 'text' => sprintf(esc_html__('*Message:*\n%s', 'mxchat'), $message)
2376 + ]
2377 + ],
2378 + [
2379 + 'type' => 'actions',
2380 + 'elements' => [
2381 + [
2382 + 'type' => 'button',
2383 + 'text' => [
2384 + 'type' => 'plain_text',
2385 + 'text' => esc_html__('✍️ Reply', 'mxchat'),
2386 + 'emoji' => true
2387 + ],
2388 + 'value' => $session_id,
2389 + 'action_id' => 'reply_to_user',
2390 + 'style' => 'primary'
2391 + ]
2392 + ]
2393 + ]
2394 + ]
2395 + ];
4785 2396
4786 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
2397 + $response = wp_remote_post($slack_webhook_url, [
2398 + 'body' => json_encode($webhook_data),
4787 2399 'headers' => [
4788 2400 'Content-Type' => 'application/json',
4789 - 'Authorization' => 'Bearer ' . $slack_bot_token
4790 2401 ],
4791 - 'body' => json_encode([
4792 - 'channel' => $channel_id,
4793 - 'text' => $user_message,
4794 - 'mrkdwn' => true
4795 - ])
4796 2402 ]);
4797 2403
4798 - return !is_wp_error($response);
2404 + if (is_wp_error($response)) {
2405 + //error_log(esc_html__('Error sending message to Slack: ', 'mxchat') . $response->get_error_message());
2406 + return false;
2407 + }
2408 +
2409 + //error_log(esc_html__('Message sent to Slack successfully.', 'mxchat'));
2410 + return true;
4799 2411 }
4800 2412 public function handle_slack_interaction(WP_REST_Request $request) {
4801 2413 //error_log('Received Slack interaction');
4802 2414
@@ -4884,8 +2496,9 @@
4884 2496
4885 2497 // Default acknowledgment
4886 2498 return new WP_REST_Response(['ok' => true]);
4887 2499 }
2500 +
4888 2501 public function mxchat_handle_agent_response(WP_REST_Request $request) {
4889 2502 //error_log('Received agent response request');
4890 2503 //error_log('Request data: ' . print_r($request->get_params(), true));
4891 2504 // //error_log('Raw body: ' . file_get_contents('php://input'));
@@ -4930,255 +2543,29 @@
4930 2543 'response_type' => 'in_channel',
4931 2544 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4932 2545 ], 200);
4933 2546 }
4934 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4935 - // Update mode to AI
4936 - update_option("mxchat_mode_{$session_id}", 'ai');
4937 -
4938 - // Clear any existing PDF context to start fresh
4939 - $this->clear_pdf_transients($session_id);
4940 -
4941 - // Set the response with explicit chat_mode
4942 - $this->fallbackResponse = [
4943 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4944 - 'html' => '',
4945 - 'images' => [],
4946 - 'chat_mode' => 'ai' // Ensure this is set
4947 - ];
4948 -
4949 - // Return the complete response array instead of just true
4950 - return $this->fallbackResponse;
4951 -}
4952 2547
4953 -/**
4954 - * Normalize Slack mrkdwn before relaying an agent's message to the web visitor.
4955 - * Slack's Events API auto-wraps URLs as <https://url> or <https://url|Label>, wraps
4956 - * mentions as <@U…>/<#C…|name>, and HTML-escapes &, <, >. Relayed raw, the visitor
4957 - * sees a broken/doubled link with a trailing > (plan-e2195b). Unwrap links FIRST, then
4958 - * unescape entities LAST so extracted URLs (which can contain &amp;) are not corrupted.
4959 - */
4960 -private function normalize_slack_text($text) {
4961 - if (!is_string($text) || $text === '') {
4962 - return $text;
4963 - }
4964 2548
4965 - $text = preg_replace_callback('/<([^>|]+)(?:\|([^>]*))?>/', function ($m) {
4966 - $target = $m[1];
4967 - $label = isset($m[2]) ? $m[2] : '';
2549 +public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
2550 + //error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
4968 2551
4969 - // User/channel mentions: <@U…> or <#C…|name> — prefer the human label, else drop the id.
4970 - if (isset($target[0]) && ($target[0] === '@' || $target[0] === '#')) {
4971 - return $label !== '' ? $label : '';
4972 - }
4973 - // mailto:/tel: — strip the scheme for display.
4974 - if (stripos($target, 'mailto:') === 0) {
4975 - $addr = substr($target, 7);
4976 - return ($label !== '' && $label !== $addr) ? "{$label} ({$addr})" : $addr;
4977 - }
4978 - if (stripos($target, 'tel:') === 0) {
4979 - $num = substr($target, 4);
4980 - return ($label !== '' && $label !== $num) ? "{$label} ({$num})" : $num;
4981 - }
4982 - // Regular URL: <url|Label> -> "Label (url)"; bare <url> -> "url".
4983 - if ($label !== '' && $label !== $target) {
4984 - return "{$label} ({$target})";
4985 - }
4986 - return $target;
4987 - }, $text);
2552 + // Just update mode to AI
2553 + update_option("mxchat_mode_{$session_id}", 'ai');
4988 2554
4989 - // Entity-unescape LAST (after link extraction) so &amp; inside URLs is repaired too.
4990 - $text = str_replace(array('&amp;', '&lt;', '&gt;'), array('&', '<', '>'), $text);
2555 + // Initialize states
2556 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2557 + $this->productCardHtml = '';
4991 2558
4992 - return $text;
4993 -}
2559 + // Set the response message
2560 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
4994 2561
4995 -/**
4996 - * Resolve the visitor's name + email for a session, mirroring generate_channel_name()'s
4997 - * priority order: logged-in user, then the pre-chat gate options (mxchat_email_/mxchat_name_),
4998 - * then the chat transcript. Returns ['name' => ..., 'email' => ...] (either may be ''). plan-e2195b.
4999 - */
5000 -private function mxchat_get_visitor_identity($session_id) {
5001 - $email = '';
5002 - $name = '';
5003 -
5004 - if (is_user_logged_in()) {
5005 - $current_user = wp_get_current_user();
5006 - if (!empty($current_user->user_email)) { $email = $current_user->user_email; }
5007 - if (!empty($current_user->display_name)) { $name = $current_user->display_name; }
5008 - }
5009 -
5010 - if (empty($email)) {
5011 - $saved_email = get_option("mxchat_email_{$session_id}", '');
5012 - if (!empty($saved_email)) { $email = $saved_email; }
5013 - }
5014 - if (empty($name)) {
5015 - $saved_name = get_option("mxchat_name_{$session_id}", '');
5016 - if (!empty($saved_name)) { $name = $saved_name; }
5017 - }
5018 -
5019 - if (empty($email) || empty($name)) {
5020 - global $wpdb;
5021 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
5022 - $existing_data = $wpdb->get_row($wpdb->prepare(
5023 - "SELECT user_email, user_name FROM $table_name WHERE session_id = %s AND (user_email IS NOT NULL OR user_name IS NOT NULL) LIMIT 1",
5024 - $session_id
5025 - ));
5026 - if ($existing_data) {
5027 - if (empty($email) && !empty($existing_data->user_email)) { $email = $existing_data->user_email; }
5028 - if (empty($name) && !empty($existing_data->user_name)) { $name = $existing_data->user_name; }
5029 - }
5030 - }
5031 -
5032 - return array('name' => $name, 'email' => $email);
2562 + return true; // Intent was handled
5033 2563 }
5034 2564
5035 -public function handle_slack_messages(WP_REST_Request $request) {
5036 - // Log the incoming request for debugging
5037 - //error_log('Slack events request received: ' . $request->get_body());
5038 -
5039 - $body = $request->get_body();
5040 - $data = json_decode($body, true);
5041 -
5042 - // Handle Slack URL verification
5043 - if (isset($data['type']) && $data['type'] === 'url_verification') {
5044 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
5045 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
5046 - }
5047 -
5048 - // IMPORTANT: Handle Slack's event deduplication
5049 - if (isset($data['event_id'])) {
5050 - $event_id = $data['event_id'];
5051 - $processed_events = get_transient('mxchat_slack_events') ?: [];
5052 -
5053 - // Check if we've already processed this event
5054 - if (in_array($event_id, $processed_events)) {
5055 - //error_log("Duplicate event detected: $event_id");
5056 - return new WP_REST_Response(['ok' => true]);
5057 - }
5058 -
5059 - // Add this event to processed list
5060 - $processed_events[] = $event_id;
5061 - // Keep only last 100 events to prevent memory issues
5062 - if (count($processed_events) > 100) {
5063 - $processed_events = array_slice($processed_events, -100);
5064 - }
5065 - // Store for 1 hour
5066 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
5067 - }
5068 -
5069 - // Handle message events
5070 - if (isset($data['event']) && $data['event']['type'] === 'message') {
5071 - $event = $data['event'];
5072 -
5073 - // Skip bot messages and messages with subtypes (like bot_message)
5074 - if (isset($event['bot_id']) || isset($event['subtype'])) {
5075 - return new WP_REST_Response(['ok' => true]);
5076 - }
5077 -
5078 - // Additional check: Skip if this is a threaded reply to our confirmation
5079 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
5080 - return new WP_REST_Response(['ok' => true]);
5081 - }
5082 -
5083 - $channel_id = $event['channel'];
5084 - $message_text = $event['text'] ?? '';
5085 - $message_ts = $event['ts'] ?? '';
5086 2565
5087 - // Find session ID by looking for matching channel
5088 - global $wpdb;
5089 - $session_option = $wpdb->get_var(
5090 - $wpdb->prepare(
5091 - "SELECT option_name FROM {$wpdb->options}
5092 - WHERE option_name LIKE 'mxchat_channel_%'
5093 - AND option_value = %s",
5094 - $channel_id
5095 - )
5096 - );
5097 2566
5098 - if ($session_option) {
5099 - $session_id = str_replace('mxchat_channel_', '', $session_option);
5100 2567
5101 - // Create a unique key for this specific message
5102 - $message_key = md5($session_id . $message_ts . $message_text);
5103 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
5104 -
5105 - // Check if we've already processed this exact message
5106 - if (in_array($message_key, $processed_messages)) {
5107 - //error_log("Duplicate message detected for session $session_id");
5108 - return new WP_REST_Response(['ok' => true]);
5109 - }
5110 -
5111 - // Add to processed messages
5112 - $processed_messages[] = $message_key;
5113 - // Keep only last 50 messages per session
5114 - if (count($processed_messages) > 50) {
5115 - $processed_messages = array_slice($processed_messages, -50);
5116 - }
5117 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
5118 -
5119 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5120 -
5121 - // Handle agent ending the chat — transfer back to AI
5122 - // Format: "!endchat" or "!endchat <custom message to user>"
5123 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
5124 - update_option("mxchat_mode_{$session_id}", 'ai');
5125 -
5126 - // Extract custom message after !endchat, or use empty string
5127 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
5128 -
5129 - // Send the agent's custom farewell message if provided
5130 - if (!empty($custom_message)) {
5131 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($custom_message));
5132 - }
5133 -
5134 - // Confirm in Slack channel
5135 - if (!empty($slack_bot_token)) {
5136 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5137 - 'headers' => [
5138 - 'Content-Type' => 'application/json',
5139 - 'Authorization' => 'Bearer ' . $slack_bot_token
5140 - ],
5141 - 'body' => json_encode([
5142 - 'channel' => $channel_id,
5143 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
5144 - 'mrkdwn' => true
5145 - ])
5146 - ]);
5147 - }
5148 -
5149 - return new WP_REST_Response(['ok' => true]);
5150 - }
5151 -
5152 - // Save the agent message (normalize Slack link/entity formatting first — plan-e2195b)
5153 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($message_text));
5154 -
5155 - // Send confirmation back to Slack (only once)
5156 - if (!empty($slack_bot_token)) {
5157 - // Use a transient to prevent duplicate confirmations
5158 - $confirm_key = 'mxchat_confirm_' . $message_key;
5159 - if (!get_transient($confirm_key)) {
5160 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5161 - 'headers' => [
5162 - 'Content-Type' => 'application/json',
5163 - 'Authorization' => 'Bearer ' . $slack_bot_token
5164 - ],
5165 - 'body' => json_encode([
5166 - 'channel' => $channel_id,
5167 - 'text' => "✅ _Message sent to user_",
5168 - 'thread_ts' => $event['ts'] // Reply in thread
5169 - ])
5170 - ]);
5171 - // Set transient to prevent duplicate confirmations
5172 - set_transient($confirm_key, true, 300); // 5 minutes
5173 - }
5174 - }
5175 - }
5176 - }
5177 -
5178 - return new WP_REST_Response(['ok' => true]);
5179 -}
5180 -
5181 2568 // For the word upload handler
5182 2569 public function mxchat_handle_word_upload() {
5183 2570 // Delegate to word handler
5184 2571 $this->word_handler->mxchat_handle_word_upload();
@@ -5205,15 +2592,9 @@
5205 2592 try {
5206 2593 // Get options and selected model
5207 2594 $options = get_option('mxchat_options');
5208 2595 $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5209 -
5210 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
5211 - // Off by default so existing sites see byte-identical behavior.
5212 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5213 - return $this->mxchat_generate_embedding_custom($text);
5214 - }
5215 -
2596 +
5216 2597 // Determine endpoint and API key based on model
5217 2598 if (strpos($selected_model, 'voyage') === 0) {
5218 2599 $endpoint = 'https://api.voyageai.com/v1/embeddings';
5219 2600 $api_key = $options['voyage_api_key'] ?? '';
@@ -5225,20 +2606,8 @@
5225 2606 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5226 2607 'error_code' => 'missing_voyage_api_key'
5227 2608 ];
5228 2609 }
5229 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5230 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5231 - $api_key = $options['gemini_api_key'] ?? '';
5232 -
5233 - // Check if Gemini API key is missing
5234 - if (empty($api_key)) {
5235 - //error_log('Gemini API key is missing');
5236 - return [
5237 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5238 - 'error_code' => 'missing_gemini_api_key'
5239 - ];
5240 - }
5241 2610 } else {
5242 2611 $endpoint = 'https://api.openai.com/v1/embeddings';
5243 2612 // Use the passed API key for OpenAI
5244 2613
@@ -5260,49 +2629,26 @@
5260 2629 'error_code' => 'empty_embedding_text'
5261 2630 ];
5262 2631 }
5263 2632
5264 - // Prepare request body based on provider
5265 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5266 - // Gemini API format
5267 - $request_body = [
5268 - 'model' => 'models/' . $selected_model,
5269 - 'content' => [
5270 - 'parts' => [
5271 - ['text' => $text]
5272 - ]
5273 - ],
5274 - 'outputDimensionality' => 1536
5275 - ];
5276 -
5277 - // Prepare headers for Gemini (API key as query parameter)
5278 - $endpoint .= '?key=' . $api_key;
5279 - $headers = [
5280 - 'Content-Type' => 'application/json'
5281 - ];
5282 - } else {
5283 - // OpenAI/Voyage API format
5284 - $request_body = [
5285 - 'input' => $text,
5286 - 'model' => $selected_model
5287 - ];
5288 -
5289 - // Add output_dimension for voyage-3-large
5290 - if ($selected_model === 'voyage-3-large') {
5291 - $request_body['output_dimension'] = 2048;
5292 - }
5293 -
5294 - // Prepare headers for OpenAI/Voyage
5295 - $headers = [
5296 - 'Content-Type' => 'application/json',
5297 - 'Authorization' => 'Bearer ' . $api_key
5298 - ];
2633 + // Prepare request body with conditional output_dimension
2634 + $request_body = [
2635 + 'input' => $text,
2636 + 'model' => $selected_model
2637 + ];
2638 +
2639 + // Add output_dimension for voyage-3-large
2640 + if ($selected_model === 'voyage-3-large') {
2641 + $request_body['output_dimension'] = 2048;
5299 2642 }
5300 2643
5301 2644 // Prepare request arguments
5302 2645 $args = [
5303 2646 'body' => wp_json_encode($request_body),
5304 - 'headers' => $headers,
2647 + 'headers' => [
2648 + 'Content-Type' => 'application/json',
2649 + 'Authorization' => 'Bearer ' . $api_key,
2650 + ],
5305 2651 'timeout' => 60,
5306 2652 'redirection' => 5,
5307 2653 'blocking' => true,
5308 2654 'httpversion' => '1.0',
@@ -5376,31 +2722,16 @@
5376 2722 }
5377 2723
5378 2724 $response_body = json_decode(wp_remote_retrieve_body($response), true);
5379 2725
5380 - // Handle different response formats based on provider
5381 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5382 - // Gemini API response format
5383 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5384 - return $response_body['embedding']['values'];
5385 - } else {
5386 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5387 - return [
5388 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5389 - 'error_code' => 'invalid_gemini_embedding_response'
5390 - ];
5391 - }
2726 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
2727 + return $response_body['data'][0]['embedding'];
5392 2728 } else {
5393 - // OpenAI/Voyage API response format
5394 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5395 - return $response_body['data'][0]['embedding'];
5396 - } else {
5397 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5398 - return [
5399 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5400 - 'error_code' => 'invalid_embedding_response'
5401 - ];
5402 - }
2729 + //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
2730 + return [
2731 + 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
2732 + 'error_code' => 'invalid_embedding_response'
2733 + ];
5403 2734 }
5404 2735 } catch (Exception $e) {
5405 2736 //error_log('Embedding Exception: ' . $e->getMessage());
5406 2737 return [
@@ -5410,651 +2741,203 @@
5410 2741 }
5411 2742 }
5412 2743
5413 2744
5414 -/**
5415 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5416 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5417 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5418 - */
5419 -private function mxchat_generate_embedding_custom($text) {
5420 - if (empty($text)) {
5421 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5422 - }
5423 - $cfg = $this->mxchat_resolve_custom_provider();
5424 - if (empty($cfg['base_url'])) {
5425 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5426 - }
2745 +private function mxchat_find_relevant_content($user_embedding) {
2746 + //error_log('MXChat Vector Search: Starting content search...');
5427 2747
5428 - $options = get_option('mxchat_options');
5429 - $embed_url = $cfg['base_url'] . '/embeddings';
5430 - if (!empty($cfg['api_version'])) {
5431 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5432 - }
5433 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5434 - ? trim((string) $options['custom_provider_embedding_model'])
5435 - : $cfg['model'];
2748 + // Retrieve the add-on settings from the database.
2749 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5436 2750
5437 - $response = wp_remote_post($embed_url, [
5438 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5439 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5440 - 'timeout' => 60,
5441 - ]);
5442 - if (is_wp_error($response)) {
5443 - return [
5444 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5445 - 'error_code' => 'embedding_custom_connection_error',
5446 - ];
5447 - }
5448 - $status = wp_remote_retrieve_response_code($response);
5449 - $body = json_decode(wp_remote_retrieve_body($response), true);
5450 - if ($status !== 200) {
5451 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5452 - return [
5453 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5454 - 'error_code' => 'embedding_custom_api_error',
5455 - 'status_code' => $status,
5456 - ];
5457 - }
5458 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5459 - return $body['data'][0]['embedding'];
5460 - }
5461 - return [
5462 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5463 - 'error_code' => 'embedding_custom_invalid_response',
5464 - ];
5465 -}
2751 + // Determine whether Pinecone is enabled.
2752 + // We expect the sanitized setting to be a string '1' if enabled, otherwise '0'.
2753 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5466 2754
5467 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5468 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
2755 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
5469 2756
5470 - // Check for OpenAI Vector Store first (takes priority when enabled)
5471 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5472 -
5473 - if ($bot_vectorstore_config['use_vectorstore']) {
5474 - // Get current model to verify it's an OpenAI model
5475 - $bot_options = $this->get_bot_options($bot_id);
5476 - $mxchat_options = get_option('mxchat_options', array());
5477 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5478 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5479 -
5480 - if ($this->is_openai_chat_model($selected_model)) {
5481 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5482 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5483 - } else {
5484 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5485 - }
5486 - }
5487 -
5488 - // Get bot-specific Pinecone configuration
5489 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5490 -
5491 - // Debug: Log the Pinecone configuration
5492 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5493 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5494 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5495 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5496 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5497 -
5498 - // Determine whether to use Pinecone based on bot configuration
5499 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5500 -
5501 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5502 -
5503 - if ($use_pinecone) {
5504 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
2757 + if ($use_pinecone === 1) {
2758 + //error_log('MXChat Vector Search: Using Pinecone database');
2759 + return $this->find_relevant_content_pinecone($user_embedding);
5505 2760 } else {
5506 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
2761 + //error_log('MXChat Vector Search: Using WordPress database');
2762 + return $this->find_relevant_content_wordpress($user_embedding);
5507 2763 }
5508 2764 }
5509 2765
5510 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
2766 +
2767 +private function find_relevant_content_wordpress($user_embedding) {
5511 2768 global $wpdb;
5512 2769 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5513 - // Initialize similarity analysis storage
5514 - $this->last_similarity_analysis = [
5515 - 'knowledge_base_type' => 'WordPress Database',
5516 - 'bot_id' => $bot_id,
5517 - 'top_matches' => [],
5518 - 'threshold_used' => 0,
5519 - 'total_checked' => 0
5520 - ];
2770 + $cache_key = 'mxchat_system_prompt_embeddings';
2771 + $batch_size = 500;
5521 2772
5522 - // NEW: Initialize valid URLs array
5523 - $valid_urls = [];
2773 + // Log start of matching process
2774 + //error_log('[MXCHAT] Starting similarity matching process');
5524 2775
5525 - // Get bot-specific options for similarity threshold
5526 - $bot_options = $this->get_bot_options($bot_id);
5527 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
2776 + // Retrieve embeddings from cache or database
2777 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2778 + if ($embeddings === false) {
2779 + //error_log('[MXCHAT] Cache miss - loading embeddings from database');
2780 + $embeddings = [];
2781 + $offset = 0;
5528 2782
5529 - // Get knowledge manager instance for role checking
5530 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2783 + // Load in batches and build cache
2784 + do {
2785 + $query = $wpdb->prepare(
2786 + "SELECT id, embedding_vector
2787 + FROM {$system_prompt_table}
2788 + LIMIT %d OFFSET %d",
2789 + $batch_size,
2790 + $offset
2791 + );
5531 2792
5532 - // Get base similarity threshold from bot options or default options
5533 - $similarity_threshold = isset($current_options['similarity_threshold'])
5534 - ? ((int) $current_options['similarity_threshold']) / 100
5535 - : 0.35;
5536 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5537 -
5538 - // Precompute bot_filter once, outside the streaming loop
5539 - $bot_filter = '';
5540 - if ($bot_id !== 'default') {
5541 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5542 - if ($column_exists) {
5543 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5544 - }
5545 - }
5546 -
5547 - // ===== STREAMING TOP-K PASS =====
5548 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5549 - // - top 10 by raw similarity (for the testing/debug display panel)
5550 - // - candidates above threshold with access (capped) for context assembly
5551 - // This bounds peak memory regardless of knowledge base size and avoids loading
5552 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5553 - $batch_size = 250;
5554 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5555 - $top_display = [];
5556 - $candidates = [];
5557 - $total_checked = 0;
5558 - $offset = 0;
5559 -
5560 - do {
5561 - $batch = $wpdb->get_results($wpdb->prepare(
5562 - "SELECT id, embedding_vector, source_url, role_restriction
5563 - FROM {$system_prompt_table}
5564 - WHERE 1=1 {$bot_filter}
5565 - LIMIT %d OFFSET %d",
5566 - $batch_size,
5567 - $offset
5568 - ));
5569 -
5570 - if (empty($batch)) {
5571 - break;
5572 - }
5573 -
5574 - foreach ($batch as $row) {
5575 - $database_embedding = $row->embedding_vector
5576 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5577 - : null;
5578 -
5579 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5580 - unset($database_embedding);
5581 - continue;
2793 + $batch = $wpdb->get_results($query);
2794 + if (empty($batch)) {
2795 + break;
5582 2796 }
5583 2797
5584 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5585 - unset($database_embedding);
2798 + $embeddings = array_merge($embeddings, $batch);
2799 + $offset += $batch_size;
5586 2800
5587 - $role_restriction = $row->role_restriction ?? 'public';
5588 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5589 - $source_url = $row->source_url ?? '';
2801 + // Free memory
2802 + unset($batch);
5590 2803
5591 - // Maintain top 10 display buffer (insert-if-beats-worst)
5592 - if (count($top_display) < 10) {
5593 - $top_display[] = [
5594 - 'id' => $row->id,
5595 - 'similarity' => $similarity,
5596 - 'source_url' => $source_url,
5597 - 'role_restriction' => $role_restriction,
5598 - 'has_access' => $has_access,
5599 - ];
5600 - usort($top_display, function ($a, $b) {
5601 - return $b['similarity'] <=> $a['similarity'];
5602 - });
5603 - } elseif ($similarity > $top_display[9]['similarity']) {
5604 - $top_display[9] = [
5605 - 'id' => $row->id,
5606 - 'similarity' => $similarity,
5607 - 'source_url' => $source_url,
5608 - 'role_restriction' => $role_restriction,
5609 - 'has_access' => $has_access,
5610 - ];
5611 - usort($top_display, function ($a, $b) {
5612 - return $b['similarity'] <=> $a['similarity'];
5613 - });
5614 - }
2804 + } while (true);
5615 2805
5616 - // Track candidates for context assembly (above threshold + has access)
5617 - if ($similarity >= $similarity_threshold && $has_access) {
5618 - $candidates[] = [
5619 - 'id' => $row->id,
5620 - 'similarity' => $similarity,
5621 - 'source_url' => $source_url,
5622 - ];
5623 - }
5624 -
5625 - $total_checked++;
2806 + if (empty($embeddings)) {
2807 + //error_log('[MXCHAT] No embeddings found in database');
2808 + return ''; // Return an empty string if no embeddings found
5626 2809 }
5627 -
5628 - unset($batch);
5629 -
5630 - // Trim candidates periodically to cap memory during long scans
5631 - if (count($candidates) > $max_candidates) {
5632 - usort($candidates, function ($a, $b) {
5633 - return $b['similarity'] <=> $a['similarity'];
5634 - });
5635 - $candidates = array_slice($candidates, 0, $max_candidates);
5636 - }
5637 -
5638 - $offset += $batch_size;
5639 - } while (true);
5640 -
5641 - if ($total_checked === 0) {
5642 - $this->current_valid_urls = [];
5643 - return '';
2810 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2811 + //error_log('[MXCHAT] Cached ' . count($embeddings) . ' embeddings');
2812 + } else {
2813 + //error_log('[MXCHAT] Using ' . count($embeddings) . ' cached embeddings');
5644 2814 }
5645 2815
5646 - // Final candidates sort (best first)
5647 - if (count($candidates) > 1) {
5648 - usort($candidates, function ($a, $b) {
5649 - return $b['similarity'] <=> $a['similarity'];
5650 - });
5651 - }
5652 -
5653 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5654 - // Gather unique IDs we actually need (top_display + candidates) and pull
5655 - // article_content in bounded IN() batches. This avoids loading content for
5656 - // every row during the similarity scan.
5657 - $needed_ids = [];
5658 - foreach ($top_display as $item) {
5659 - $needed_ids[$item['id']] = true;
5660 - }
5661 - foreach ($candidates as $item) {
5662 - $needed_ids[$item['id']] = true;
5663 - }
5664 - $needed_ids = array_keys($needed_ids);
5665 -
5666 - $content_map = [];
5667 - if (!empty($needed_ids)) {
5668 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5669 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5670 - $rows = $wpdb->get_results($wpdb->prepare(
5671 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5672 - ...$chunk_ids
5673 - ));
5674 - foreach ($rows as $r) {
5675 - $content_map[$r->id] = $r->article_content;
2816 + // Initialize array to store relevant results with similarity scores
2817 + $relevant_results = [];
2818 +
2819 + // Get the similarity threshold from the main options array only
2820 + $main_options = get_option('mxchat_options', []);
2821 + $similarity_threshold = isset($main_options['similarity_threshold'])
2822 + ? ((int) $main_options['similarity_threshold']) / 100
2823 + : 0.8; // Default to 80%
2824 +
2825 + //error_log('[MXCHAT] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
2826 +
2827 + // Iterate through embeddings to calculate similarity
2828 + foreach ($embeddings as $embedding) {
2829 + $database_embedding = $embedding->embedding_vector
2830 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2831 + : null;
2832 + if (is_array($database_embedding) && is_array($user_embedding)) {
2833 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
2834 +
2835 + // Log each similarity score over 0.5 to reduce log spam
2836 + if ($similarity > 0.1) {
2837 + //error_log(sprintf('[MXCHAT] ID: %d | Similarity Score: %.4f', $embedding->id, $similarity));
5676 2838 }
5677 - unset($rows);
2839 +
2840 + $relevant_results[] = [
2841 + 'id' => $embedding->id,
2842 + 'similarity' => $similarity
2843 + ];
5678 2844 }
2845 + // Free memory
2846 + unset($database_embedding);
5679 2847 }
5680 2848
5681 - // Build the all_similarities display array from the top 10
5682 - $all_similarities = [];
5683 - foreach ($top_display as $item) {
5684 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5685 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5686 - $is_chunk = $parsed_for_display['is_chunked'];
5687 - $chunk_meta = $parsed_for_display['metadata'];
5688 -
5689 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5690 - $source_display = $item['source_url'];
5691 - } else {
5692 - $content_preview = strip_tags($article_content_for_parse);
5693 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5694 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5695 - }
5696 -
5697 - $all_similarities[] = [
5698 - 'document_id' => $item['id'],
5699 - 'similarity' => $item['similarity'],
5700 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5701 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5702 - 'source_display' => $source_display,
5703 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5704 - 'used_for_context' => false,
5705 - 'role_restriction' => $item['role_restriction'],
5706 - 'has_access' => $item['has_access'],
5707 - 'filtered_out' => !$item['has_access'],
5708 - 'is_chunk' => $is_chunk,
5709 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5710 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5711 - ];
5712 - }
5713 -
5714 - // Build url_groups from candidates for chunk reassembly
5715 - $url_groups = array();
5716 - foreach ($candidates as $cand) {
5717 - $article_content = $content_map[$cand['id']] ?? '';
5718 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5719 - $is_chunked = $parsed['is_chunked'];
5720 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5721 - $text_content = $parsed['text'];
5722 -
5723 - $source_url = $cand['source_url'];
5724 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5725 -
5726 - if (!isset($url_groups[$group_key])) {
5727 - $url_groups[$group_key] = array(
5728 - 'source_url' => $source_url,
5729 - 'best_score' => 0,
5730 - 'is_chunked' => $is_chunked,
5731 - 'chunks' => array(),
5732 - 'single_text' => '',
5733 - 'single_id' => null
5734 - );
5735 - }
5736 -
5737 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5738 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5739 - }
5740 -
5741 - if ($is_chunked) {
5742 - $url_groups[$group_key]['is_chunked'] = true;
5743 - $url_groups[$group_key]['chunks'][] = array(
5744 - 'id' => $cand['id'],
5745 - 'score' => $cand['similarity'],
5746 - 'chunk_index' => $chunk_index,
5747 - 'text' => $text_content
5748 - );
5749 - } else {
5750 - $url_groups[$group_key]['single_text'] = $text_content;
5751 - $url_groups[$group_key]['single_id'] = $cand['id'];
5752 - }
5753 - }
5754 -
5755 - // Sort ALL similarities for testing display (highest first)
5756 - usort($all_similarities, function ($a, $b) {
2849 + // Filter and sort relevant results by similarity
2850 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2851 + return $result['similarity'] >= $similarity_threshold;
2852 + });
2853 + usort($relevant_results, function ($a, $b) {
5757 2854 return $b['similarity'] <=> $a['similarity'];
5758 2855 });
5759 2856
5760 - // Sort URL groups by best score (highest first)
5761 - uasort($url_groups, function($a, $b) {
5762 - return $b['best_score'] <=> $a['best_score'];
5763 - });
2857 + // Log number of results that met threshold
2858 + //error_log('[MXCHAT] ' . count($relevant_results) . ' results met the similarity threshold');
5764 2859
5765 - // Get RAG sources limit from options (default 6, min 3, max 10)
5766 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5767 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5768 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
2860 + // Limit to the top 5 results
2861 + $top_results = array_slice($relevant_results, 0, 5);
5769 2862
5770 - // Take top N unique URLs based on user setting
5771 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
2863 + // Log the top matches
2864 + //error_log('[MXCHAT] Top matching results:');
2865 + foreach ($top_results as $index => $result) {
5772 2866
5773 - // Track which document IDs are used for context
5774 - $used_document_ids = [];
5775 - foreach ($top_urls as $group) {
5776 - if ($group['is_chunked']) {
5777 - foreach ($group['chunks'] as $chunk) {
5778 - $used_document_ids[] = $chunk['id'];
5779 - }
5780 - } elseif ($group['single_id']) {
5781 - $used_document_ids[] = $group['single_id'];
5782 - }
5783 2867 }
5784 2868
5785 - // Update the all_similarities array to mark which were actually used
5786 - foreach ($all_similarities as &$similarity_item) {
5787 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5788 - }
5789 -
5790 - // Store top 10 for testing panel
5791 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5792 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5793 -
5794 - // Initialize final content
2869 + // Initialize the final content
5795 2870 $content = '';
5796 - $matches_used = 0;
5797 - $total_chunks_used = 0;
5798 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5799 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5800 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5801 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5802 2871
5803 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5804 - // Use fresh options to ensure we get the latest setting value
5805 - $fresh_options = get_option('mxchat_options', []);
5806 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5807 -
5808 - // Build content from top sources
5809 - foreach ($top_urls as $group_key => $group) {
5810 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5811 -
5812 - // Stop if we've hit the total chunk limit
5813 - if ($total_chunks_used >= $max_total_chunks) {
5814 - break;
5815 - }
5816 -
5817 - $full_text = '';
5818 - $chunks_in_this_source = 1; // Default for non-chunked content
5819 -
5820 - if ($group['is_chunked']) {
5821 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5822 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5823 -
5824 - // Fetch chunks for this URL with limit
5825 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5826 -
5827 - // If fetching all chunks fails, fall back to matched chunks
5828 - if (empty($full_text)) {
5829 - // Sort matched chunks by index and concatenate
5830 - usort($group['chunks'], function($a, $b) {
5831 - return $a['chunk_index'] <=> $b['chunk_index'];
5832 - });
5833 -
5834 - $chunk_texts = array();
5835 - $chunks_in_this_source = 0;
5836 - foreach ($group['chunks'] as $chunk) {
5837 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5838 - break;
5839 - }
5840 - $chunk_texts[] = $chunk['text'];
5841 - $chunks_in_this_source++;
5842 - }
5843 - $full_text = implode("\n\n", $chunk_texts);
2872 + // Fetch and combine content for the top results
2873 + foreach ($top_results as $result) {
2874 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
2875 + // Check if the content is PDF-related and add surrounding pages
2876 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
2877 + //error_log('[MXCHAT] ID ' . $result['id'] . ' is PDF content, adding surrounding pages');
2878 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
2879 + "SELECT id, article_content FROM {$system_prompt_table}
2880 + WHERE id IN (
2881 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
2882 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
2883 + )",
2884 + $result['id'],
2885 + $result['id']
2886 + ));
2887 + // Add previous content if it exists
2888 + if (!empty($surrounding_content[0])) {
2889 + $content .= $surrounding_content[0]->article_content . "\n\n";
5844 2890 }
5845 - } else {
5846 - $full_text = $group['single_text'];
5847 - $chunks_in_this_source = 1;
5848 - }
5849 -
5850 - if (!empty($full_text)) {
5851 - // Strip URLs from content if citation links are disabled
5852 - if (!$citation_links_enabled) {
5853 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5854 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
2891 + // Add the main chunk content
2892 + $content .= $chunk_content . "\n\n";
2893 + // Add next content if it exists
2894 + if (!empty($surrounding_content[1])) {
2895 + $content .= $surrounding_content[1]->article_content . "\n\n";
5855 2896 }
5856 -
5857 - // Use numbered reference for URL-based entries, plain info label for manual entries
5858 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5859 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5860 - $matches_used++;
5861 - $content .= "## Reference " . $matches_used . " ##\n";
5862 - $content .= $full_text . "\n\n";
5863 -
5864 - // Only include citation URLs if citation links are enabled
5865 - if ($citation_links_enabled) {
5866 - $valid_urls[] = $source_url;
5867 - $content .= "URL: " . $source_url . "\n\n";
5868 - }
5869 - } else {
5870 - // Manual entry — no reference number, no citation
5871 - $content .= "## Information ##\n";
5872 - $content .= $full_text . "\n\n";
5873 - }
5874 -
5875 - // Extract any URLs from the text content itself (only if citation links enabled)
5876 - if ($citation_links_enabled) {
5877 - preg_match_all(
5878 - '#\bhttps?://[^\s<>"\']+#i',
5879 - $full_text,
5880 - $content_urls
5881 - );
5882 - if (!empty($content_urls[0])) {
5883 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5884 - }
5885 - }
5886 -
5887 - $total_chunks_used += $chunks_in_this_source;
5888 - }
5889 - }
5890 -
5891 - // NEW: Store unique valid URLs for validation
5892 - $this->current_valid_urls = array_unique($valid_urls);
5893 -
5894 - // Store sources and chunks counts for testing/transcript display
5895 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5896 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5897 -
5898 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5899 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5900 -
5901 - // Add response guidelines
5902 - if (empty($top_urls)) {
5903 - $content = "No reference information was found for this query.\n\n";
5904 - } else {
5905 - // Build response guidelines based on citation links setting
5906 - $content .= "\n## Response Guidelines ##\n" .
5907 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5908 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5909 - "If you don't have specific information or are uncertain about any details, it's always " .
5910 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5911 - "When information is incomplete, let them know you are unsure.\n\n";
5912 -
5913 - // Only add hyperlink instructions if citation links are enabled
5914 - if ($citation_links_enabled) {
5915 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5916 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5917 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5918 2897 } else {
5919 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5920 - "Simply provide helpful answers based on the reference information without citing sources.";
2898 + // For non-PDF content, add directly
2899 + $content .= $chunk_content . "\n\n";
5921 2900 }
5922 2901 }
5923 2902
2903 + // Log content length
2904 + //error_log('[MXCHAT] Retrieved content length: ' . strlen(trim($content)) . ' characters');
2905 +
5924 2906 return trim($content);
5925 2907 }
5926 2908
5927 -/**
5928 - * Fetch and reassemble chunks for a URL from WordPress database
5929 - *
5930 - * @param string $source_url The source URL to fetch chunks for
5931 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5932 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5933 - * @return string Reassembled content from chunks
5934 - */
5935 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5936 - global $wpdb;
5937 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5938 2909
5939 - // Fetch all rows with this source_url
5940 - $rows = $wpdb->get_results($wpdb->prepare(
5941 - "SELECT article_content FROM {$table}
5942 - WHERE source_url = %s
5943 - ORDER BY id ASC",
5944 - $source_url
5945 - ));
5946 -
5947 - if (empty($rows)) {
5948 - $chunk_count = 0;
5949 - return '';
5950 - }
5951 -
5952 - // Parse and sort chunks by index
5953 - $chunks = array();
5954 - foreach ($rows as $row) {
5955 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5956 -
5957 - if ($parsed['is_chunked']) {
5958 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5959 - $chunks[$chunk_index] = $parsed['text'];
5960 - } else {
5961 - // Non-chunked content - just return it
5962 - $chunks[] = $parsed['text'];
5963 - }
5964 - }
5965 -
5966 - // Sort by chunk index
5967 - ksort($chunks);
5968 -
5969 - // Apply chunk limit if specified
5970 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5971 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5972 - }
5973 -
5974 - // Store actual chunk count
5975 - $chunk_count = count($chunks);
5976 -
5977 - // Reassemble content
5978 - return implode("\n\n", $chunks);
5979 -}
5980 -
5981 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5982 - global $wpdb;
2910 +private function find_relevant_content_pinecone($user_embedding) {
2911 + $options = get_option('mxchat_pinecone_addon_options', array());
2912 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
2913 + $host = $options['mxchat_pinecone_host'] ?? '';
5983 2914
5984 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5985 - //error_log(" - bot_id: " . $bot_id);
5986 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5987 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5988 -
5989 - // Use bot-specific config or fall back to default
5990 - if ($bot_config === null) {
5991 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5992 - }
5993 -
5994 - $api_key = $bot_config['api_key'] ?? '';
5995 - $host = $bot_config['host'] ?? '';
5996 - $namespace = $bot_config['namespace'] ?? '';
5997 -
5998 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5999 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
6000 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
6001 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
6002 -
6003 - // Initialize similarity analysis storage
6004 - $this->last_similarity_analysis = [
6005 - 'knowledge_base_type' => 'Pinecone',
6006 - 'bot_id' => $bot_id,
6007 - 'namespace' => $namespace,
6008 - 'top_matches' => [],
6009 - 'threshold_used' => 0,
6010 - 'total_checked' => 0
6011 - ];
6012 -
6013 - // NEW: Initialize valid URLs array
6014 - $valid_urls = [];
6015 -
6016 2915 if (empty($host) || empty($api_key)) {
6017 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
6018 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
6019 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
6020 - // Store empty array for valid URLs since we can't proceed
6021 - $this->current_valid_urls = [];
2916 + //error_log('[MXCHAT Debug] Pinecone credentials not properly configured');
6022 2917 return '';
6023 2918 }
6024 2919
6025 - // Get knowledge manager instance for role checking
6026 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2920 + // Get the similarity threshold from the main options array only
2921 + $main_options = get_option('mxchat_options', []);
2922 + $similarity_threshold = isset($main_options['similarity_threshold'])
2923 + ? ((int) $main_options['similarity_threshold']) / 100
2924 + : 0.8; // Default to 80%
6027 2925
6028 - // Get the similarity threshold from the bot options or main options
6029 - $bot_options = $this->get_bot_options($bot_id);
6030 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
2926 + //error_log('[MXCHAT Debug] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
6031 2927
6032 - $similarity_threshold = isset($current_options['similarity_threshold'])
6033 - ? ((int) $current_options['similarity_threshold']) / 100
6034 - : 0.35;
6035 -
6036 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
6037 -
6038 2928 // Prepare the query request for Pinecone
6039 2929 $api_endpoint = "https://{$host}/query";
6040 2930
2931 + //error_log('[MXCHAT Debug] Querying Pinecone at: ' . $api_endpoint);
2932 +
6041 2933 $request_body = array(
6042 2934 'vector' => $user_embedding,
6043 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
2935 + 'topK' => 5,
6044 2936 'includeMetadata' => true,
6045 2937 'includeValues' => true
6046 2938 );
6047 2939
6048 - // Add namespace if specified for this bot
6049 - if (!empty($namespace)) {
6050 - $request_body['namespace'] = $namespace;
6051 - }
6052 -
6053 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
6054 - //error_log(" - Endpoint: " . $api_endpoint);
6055 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
6056 -
6057 2940 $response = wp_remote_post($api_endpoint, array(
6058 2941 'headers' => array(
6059 2942 'Api-Key' => $api_key,
6060 2943 'accept' => 'application/json',
@@ -6064,887 +2947,54 @@
6064 2947 'timeout' => 30
6065 2948 ));
6066 2949
6067 2950 if (is_wp_error($response)) {
6068 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
6069 - // Store empty array for valid URLs
6070 - $this->current_valid_urls = [];
2951 + //error_log('[MXCHAT Debug] Pinecone query error: ' . $response->get_error_message());
6071 2952 return '';
6072 2953 }
6073 2954
6074 2955 $response_code = wp_remote_retrieve_response_code($response);
6075 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
6076 -
6077 2956 if ($response_code !== 200) {
6078 - $response_body = wp_remote_retrieve_body($response);
6079 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
6080 - // Store empty array for valid URLs
6081 - $this->current_valid_urls = [];
2957 + //error_log('[MXCHAT Debug] Pinecone API error: ' . wp_remote_retrieve_body($response));
6082 2958 return '';
6083 2959 }
6084 2960
6085 - // ADD DETAILED DEBUG SECTION HERE
6086 - $response_body = wp_remote_retrieve_body($response);
6087 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
6088 -
6089 - $results = json_decode($response_body, true);
6090 -
6091 - if (json_last_error() !== JSON_ERROR_NONE) {
6092 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
6093 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
6094 - // Store empty array for valid URLs
6095 - $this->current_valid_urls = [];
6096 - return '';
6097 - }
6098 -
6099 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
6100 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
6101 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
6102 -
2961 + $results = json_decode(wp_remote_retrieve_body($response), true);
6103 2962 if (empty($results['matches'])) {
6104 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
6105 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
6106 - // Store empty array for valid URLs
6107 - $this->current_valid_urls = [];
2963 + //error_log('[MXCHAT Debug] No matches found in Pinecone response');
6108 2964 return '';
6109 2965 }
6110 2966
6111 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
2967 + //error_log('[MXCHAT Debug] Found ' . count($results['matches']) . ' matches in Pinecone');
6112 2968
6113 - // Log first match details for debugging
6114 - if (!empty($results['matches'][0])) {
6115 - $first_match = $results['matches'][0];
6116 - //error_log("MXCHAT DEBUG: First match details:");
6117 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
6118 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
6119 - if (isset($first_match['metadata'])) {
6120 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
6121 - }
6122 - }
6123 -
6124 2969 // Initialize the final content
6125 2970 $content = '';
6126 - $matches_used = 0;
6127 - $matches_used_for_context = [];
6128 - $total_chunks_used = 0;
6129 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6130 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6131 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6132 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
2971 + $matches_above_threshold = 0;
2972 +
2973 + // Process each match
2974 + foreach ($results['matches'] as $index => $match) {
2975 + // Log score for each match
6133 2976
6134 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6135 - // Use fresh options to ensure we get the latest setting value
6136 - $fresh_options = get_option('mxchat_options', []);
6137 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6138 -
6139 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
6140 - $url_groups = array();
6141 -
6142 - foreach ($results['matches'] as $index => $match) {
6143 2977 // Skip if similarity is below threshold
6144 2978 if ($match['score'] < $similarity_threshold) {
6145 2979 continue;
6146 2980 }
6147 -
6148 - $metadata = $match['metadata'] ?? array();
6149 - $source_url = $metadata['source_url'] ?? '';
6150 - $match_id = $match['id'] ?? '';
6151 -
6152 - // LAZY ROLE CHECK: Only check role for content we're actually considering
6153 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
6154 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6155 -
6156 - // Skip if user doesn't have access
6157 - if (!$has_access) {
6158 - continue;
6159 - }
6160 -
6161 - // Use a unique key for manual entries without a source URL
6162 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
6163 -
6164 - // Group by source URL (or unique key for manual entries)
6165 - if (!isset($url_groups[$group_key])) {
6166 - $url_groups[$group_key] = array(
6167 - 'source_url' => $source_url,
6168 - 'best_score' => 0,
6169 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
6170 - 'chunks' => array(),
6171 - 'single_text' => ''
6172 - );
6173 - }
6174 -
6175 - // Track best score for this group
6176 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
6177 - $url_groups[$group_key]['best_score'] = $match['score'];
6178 - }
6179 -
6180 - // Store chunk info or single text
6181 - if ($url_groups[$group_key]['is_chunked']) {
6182 - $url_groups[$group_key]['chunks'][] = array(
6183 - 'id' => $match_id,
6184 - 'score' => $match['score'],
6185 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
6186 - 'text' => $metadata['text'] ?? ''
6187 - );
6188 - } else {
6189 - // Non-chunked content - just store the text
6190 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
6191 - $url_groups[$group_key]['single_id'] = $match_id;
6192 - }
6193 - }
6194 -
6195 - // Sort URL groups by best score (highest first)
6196 - uasort($url_groups, function($a, $b) {
6197 - return $b['best_score'] <=> $a['best_score'];
6198 - });
6199 -
6200 - // Get RAG sources limit from options (default 6, min 3, max 10)
6201 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6202 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6203 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
6204 -
6205 - // Take top N unique URLs based on user setting
6206 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
6207 -
6208 - // Track which match IDs are actually used for context
6209 - foreach ($top_urls as $group) {
6210 - if ($group['is_chunked']) {
6211 - foreach ($group['chunks'] as $chunk) {
6212 - $matches_used_for_context[] = $chunk['id'];
6213 - }
6214 - } elseif (!empty($group['single_id'])) {
6215 - $matches_used_for_context[] = $group['single_id'];
6216 - }
6217 - }
6218 -
6219 - // Build content from top sources
6220 - foreach ($top_urls as $group_key => $group) {
6221 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6222 -
6223 - // Stop if we've hit the total chunk limit
6224 - if ($total_chunks_used >= $max_total_chunks) {
6225 - break;
6226 - }
6227 -
6228 - $full_text = '';
6229 - $chunks_in_this_source = 1; // Default for non-chunked content
6230 -
6231 - if ($group['is_chunked']) {
6232 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6233 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6234 -
6235 - // Fetch chunks for this URL with limit
6236 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6237 -
6238 - // If fetching all chunks fails, fall back to matched chunks
6239 - if (empty($full_text)) {
6240 - // Sort matched chunks by index and concatenate
6241 - usort($group['chunks'], function($a, $b) {
6242 - return $a['chunk_index'] <=> $b['chunk_index'];
6243 - });
6244 -
6245 - $chunk_texts = array();
6246 - $chunks_in_this_source = 0;
6247 - foreach ($group['chunks'] as $chunk) {
6248 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6249 - break;
6250 - }
6251 - $chunk_texts[] = $chunk['text'];
6252 - $chunks_in_this_source++;
6253 - }
6254 - $full_text = implode("\n\n", $chunk_texts);
6255 - }
6256 - } else {
6257 - $full_text = $group['single_text'];
6258 - $chunks_in_this_source = 1;
6259 - }
6260 -
6261 - if (!empty($full_text)) {
6262 - // Strip URLs from content if citation links are disabled
6263 - if (!$citation_links_enabled) {
6264 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6265 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6266 - }
6267 -
6268 - // Use numbered reference for URL-based entries, plain info label for manual entries
6269 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6270 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6271 - $matches_used++;
6272 - $content .= "## Reference " . $matches_used . " ##\n";
6273 - $content .= $full_text . "\n\n";
6274 -
6275 - // Only include citation URLs if citation links are enabled
6276 - if ($citation_links_enabled) {
6277 - $valid_urls[] = $source_url;
6278 - $content .= "URL: " . $source_url . "\n\n";
6279 - }
6280 - } else {
6281 - // Manual entry — no reference number, no citation. Count it as a USED
6282 - // source (plan-mxchat-20260622-c1fe6a): without this, manual/Direct-Content
6283 - // entries (empty or mxchat:// source_url) never increment $matches_used, so
6284 - // the gate below (`if ($matches_used === 0)`) discards manual-only context on
6285 - // the Pinecone backend and the model is told "No reference information was
6286 - // found" — even though the testing panel reports used_for_context:true. It
6287 - // also corrects the cosmetic sources_used:0 the panel/transcript showed. The
6288 - // sibling local/WP-DB builder gates on empty($top_urls), so it never had this
6289 - // bug; this brings Pinecone to parity. Manual entries are still uncited (not
6290 - // added to $valid_urls, no "URL:" line).
6291 - $matches_used++;
6292 - $content .= "## Information ##\n";
6293 - $content .= $full_text . "\n\n";
6294 - }
6295 -
6296 - // Extract any URLs from the text content itself (only if citation links enabled)
6297 - if ($citation_links_enabled) {
6298 - preg_match_all(
6299 - '#\bhttps?://[^\s<>"\']+#i',
6300 - $full_text,
6301 - $content_urls
6302 - );
6303 - if (!empty($content_urls[0])) {
6304 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6305 - }
6306 - }
6307 -
6308 - $total_chunks_used += $chunks_in_this_source;
6309 - }
6310 - }
6311 -
6312 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6313 - $all_matches = [];
6314 - foreach ($results['matches'] as $index => $match) {
6315 - if ($index >= 10) break; // Limit to top 10 for testing
6316 2981
6317 - $match_id = $match['id'] ?? '';
2982 + $matches_above_threshold++;
6318 2983
6319 - // Check role access for testing display (use cache if available)
6320 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6321 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6322 -
6323 - $source_display = '';
6324 - if (!empty($match['metadata']['source_url'])) {
6325 - $source_display = $match['metadata']['source_url'];
6326 - } else {
6327 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6328 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6329 - $source_display = substr(trim($content_preview), 0, 50) . '...';
2984 + if (!empty($match['metadata']['text']) && !empty($match['metadata']['source_url'])) {
2985 + // Add content with citation
2986 + $content .= $match['metadata']['text'] . "\n";
2987 + $content .= "Source: " . $match['metadata']['source_url'] . "\n\n";
6330 2988 }
6331 -
6332 - $match_id_for_display = $match['id'] ?? $index;
6333 -
6334 - // Check for chunk metadata in Pinecone
6335 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6336 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6337 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6338 -
6339 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6340 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6341 - $is_chunk = true;
6342 - }
6343 -
6344 - $all_matches[] = [
6345 - 'document_id' => $match_id_for_display,
6346 - 'similarity' => $match['score'],
6347 - 'similarity_percentage' => round($match['score'] * 100, 2),
6348 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6349 - 'source_display' => $source_display,
6350 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6351 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6352 - 'role_restriction' => $role_restriction,
6353 - 'has_access' => $has_access,
6354 - 'filtered_out' => !$has_access,
6355 - 'is_chunk' => $is_chunk,
6356 - 'chunk_index' => $chunk_index,
6357 - 'total_chunks' => $total_chunks
6358 - ];
6359 2989 }
6360 2990
6361 - // Store for testing panel
6362 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6363 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6364 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6365 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6366 -
6367 - // NEW: Store unique valid URLs for validation
6368 - $this->current_valid_urls = array_unique($valid_urls);
6369 -
6370 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6371 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6372 -
6373 - // Add response guidelines
6374 - if ($matches_used === 0) {
6375 - $content = "No reference information was found for this query.\n\n";
6376 - } else {
6377 - // Build response guidelines based on citation links setting
6378 - $content .= "\n## Response Guidelines ##\n" .
6379 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6380 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6381 - "If you don't have specific information or are uncertain about any details, it's always " .
6382 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6383 - "When information is incomplete, let them know you are unsure.\n\n";
6384 -
6385 - // Only add hyperlink instructions if citation links are enabled
6386 - if ($citation_links_enabled) {
6387 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6388 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6389 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6390 - } else {
6391 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6392 - "Simply provide helpful answers based on the reference information without citing sources.";
6393 - }
6394 - }
6395 -
6396 - return trim($content);
6397 -}
6398 -
6399 -/**
6400 - * Get role restriction for a single vector (with caching)
6401 - */
6402 -private function get_single_vector_role($vector_id, $metadata = array()) {
6403 - global $wpdb;
2991 + //error_log('[MXCHAT Debug] Total matches used (above threshold): ' . $matches_above_threshold);
2992 + //error_log('[MXCHAT Debug] Content length returned: ' . strlen(trim($content)) . ' characters');
6404 2993
6405 - if (empty($vector_id)) {
6406 - return 'public';
6407 - }
6408 -
6409 - // Check cache first
6410 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6411 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6412 -
6413 - if ($cached_role !== false) {
6414 - return $cached_role;
6415 - }
6416 -
6417 - $role_restriction = 'public';
6418 -
6419 - // First try Pinecone metadata
6420 - if (!empty($metadata['role_restriction'])) {
6421 - $role_restriction = $metadata['role_restriction'];
6422 - } else {
6423 - // Check WordPress table for user-modified roles
6424 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6425 - $stored_role = $wpdb->get_var($wpdb->prepare(
6426 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6427 - $vector_id
6428 - ));
6429 -
6430 - if ($stored_role) {
6431 - $role_restriction = $stored_role;
6432 - }
6433 - }
6434 -
6435 - // Cache individual role for 1 hour
6436 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6437 -
6438 - return $role_restriction;
6439 -}
6440 -
6441 -/**
6442 - * Fetch and reassemble all chunks for a URL from Pinecone
6443 - *
6444 - * @param string $source_url The source URL to fetch chunks for
6445 - * @param array $bot_config Bot-specific Pinecone configuration
6446 - * @return string Reassembled content from all chunks
6447 - */
6448 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6449 - $api_key = $bot_config['api_key'] ?? '';
6450 - $host = $bot_config['host'] ?? '';
6451 - $namespace = $bot_config['namespace'] ?? '';
6452 -
6453 - if (empty($host) || empty($api_key)) {
6454 - $chunk_count = 0;
6455 - return '';
6456 - }
6457 -
6458 - $base_hash = md5($source_url);
6459 -
6460 - // Use Pinecone list API to find all chunk vectors with this prefix
6461 - $list_url = "https://{$host}/vectors/list";
6462 -
6463 - // Limit to max_chunks if specified, otherwise fetch up to 100
6464 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6465 -
6466 - $list_body = array(
6467 - 'prefix' => $base_hash . '_chunk_',
6468 - 'limit' => $fetch_limit
6469 - );
6470 -
6471 - if (!empty($namespace)) {
6472 - $list_body['namespace'] = $namespace;
6473 - }
6474 -
6475 - $list_response = wp_remote_post($list_url, array(
6476 - 'headers' => array(
6477 - 'Api-Key' => $api_key,
6478 - 'accept' => 'application/json',
6479 - 'content-type' => 'application/json'
6480 - ),
6481 - 'body' => wp_json_encode($list_body),
6482 - 'timeout' => 30
6483 - ));
6484 -
6485 - if (is_wp_error($list_response)) {
6486 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6487 - return '';
6488 - }
6489 -
6490 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6491 -
6492 - if (empty($list_data['vectors'])) {
6493 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6494 - return '';
6495 - }
6496 -
6497 - // Extract vector IDs
6498 - $vector_ids = array();
6499 - foreach ($list_data['vectors'] as $vector) {
6500 - if (isset($vector['id'])) {
6501 - $vector_ids[] = $vector['id'];
6502 - }
6503 - }
6504 -
6505 - if (empty($vector_ids)) {
6506 - return '';
6507 - }
6508 -
6509 - // Fetch all chunk content
6510 - $fetch_url = "https://{$host}/vectors/fetch";
6511 -
6512 - $fetch_body = array(
6513 - 'ids' => $vector_ids
6514 - );
6515 -
6516 - if (!empty($namespace)) {
6517 - $fetch_body['namespace'] = $namespace;
6518 - }
6519 -
6520 - $fetch_response = wp_remote_post($fetch_url, array(
6521 - 'headers' => array(
6522 - 'Api-Key' => $api_key,
6523 - 'accept' => 'application/json',
6524 - 'content-type' => 'application/json'
6525 - ),
6526 - 'body' => wp_json_encode($fetch_body),
6527 - 'timeout' => 30
6528 - ));
6529 -
6530 - if (is_wp_error($fetch_response)) {
6531 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6532 - return '';
6533 - }
6534 -
6535 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6536 -
6537 - if (empty($fetch_data['vectors'])) {
6538 - return '';
6539 - }
6540 -
6541 - // Sort chunks by index and reassemble
6542 - $chunks = array();
6543 - foreach ($fetch_data['vectors'] as $id => $vector) {
6544 - $metadata = $vector['metadata'] ?? array();
6545 - $chunk_index = $metadata['chunk_index'] ?? 0;
6546 - $text = $metadata['text'] ?? '';
6547 -
6548 - // Store chunk with its index
6549 - $chunks[$chunk_index] = $text;
6550 - }
6551 -
6552 - // Sort by chunk index
6553 - ksort($chunks);
6554 -
6555 - // Apply chunk limit if specified
6556 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6557 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6558 - }
6559 -
6560 - // Store actual chunk count
6561 - $chunk_count = count($chunks);
6562 -
6563 - // Reassemble content
6564 - return implode("\n\n", $chunks);
6565 -}
6566 -
6567 -/**
6568 - * Search for relevant content using OpenAI Vector Store (File Search)
6569 - *
6570 - * @param string $user_query The user's query text
6571 - * @param string $bot_id The bot ID
6572 - * @param array $vectorstore_config Vector Store configuration
6573 - * @return string Formatted context string with references
6574 - */
6575 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6576 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6577 - //error_log(" - bot_id: " . $bot_id);
6578 - //error_log(" - user_query length: " . strlen($user_query));
6579 -
6580 - // Get OpenAI API key
6581 - $mxchat_options = get_option('mxchat_options', array());
6582 - $api_key = $mxchat_options['api_key'] ?? '';
6583 -
6584 - // Reset vectorstore error tracking
6585 - $this->last_vectorstore_error = null;
6586 -
6587 - if (empty($api_key)) {
6588 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6589 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6590 - $this->current_valid_urls = [];
6591 - return '';
6592 - }
6593 -
6594 - // Get Vector Store configuration
6595 - if (empty($vectorstore_config)) {
6596 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6597 - }
6598 -
6599 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6600 - $max_results = $vectorstore_config['max_results'] ?? 5;
6601 -
6602 - if (empty($vectorstore_ids_string)) {
6603 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6604 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6605 - $this->current_valid_urls = [];
6606 - return '';
6607 - }
6608 -
6609 - // Parse Vector Store IDs
6610 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6611 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6612 -
6613 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6614 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6615 -
6616 - // Initialize similarity analysis storage
6617 - $this->last_similarity_analysis = [
6618 - 'knowledge_base_type' => 'OpenAI Vector Store',
6619 - 'bot_id' => $bot_id,
6620 - 'vectorstore_ids' => $vectorstore_ids,
6621 - 'top_matches' => [],
6622 - 'threshold_used' => 0,
6623 - 'total_checked' => 0
6624 - ];
6625 -
6626 - $valid_urls = [];
6627 -
6628 - // Get the selected model
6629 - $bot_options = $this->get_bot_options($bot_id);
6630 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6631 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6632 -
6633 - // Verify it's an OpenAI model
6634 - if (!$this->is_openai_chat_model($selected_model)) {
6635 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6636 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6637 - $this->current_valid_urls = [];
6638 - return '';
6639 - }
6640 -
6641 - // Use OpenAI Responses API with file_search tool
6642 - $request_body = array(
6643 - 'model' => $selected_model,
6644 - 'input' => $user_query,
6645 - 'tools' => array(
6646 - array(
6647 - 'type' => 'file_search',
6648 - 'vector_store_ids' => $vectorstore_ids,
6649 - 'max_num_results' => intval($max_results)
6650 - )
6651 - ),
6652 - 'include' => array('output[*].file_search_call.search_results')
6653 - );
6654 -
6655 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6656 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6657 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6658 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6659 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6660 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6661 -
6662 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6663 - 'headers' => array(
6664 - 'Authorization' => 'Bearer ' . $api_key,
6665 - 'Content-Type' => 'application/json'
6666 - ),
6667 - 'body' => wp_json_encode($request_body),
6668 - 'timeout' => 60
6669 - ));
6670 -
6671 - if (is_wp_error($response)) {
6672 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6673 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6674 - $this->current_valid_urls = [];
6675 - return '';
6676 - }
6677 -
6678 - $response_code = wp_remote_retrieve_response_code($response);
6679 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6680 -
6681 - $response_body = wp_remote_retrieve_body($response);
6682 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6683 -
6684 - if ($response_code !== 200) {
6685 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6686 - $api_error_detail = '';
6687 - $decoded_error = json_decode($response_body, true);
6688 - if (isset($decoded_error['error']['message'])) {
6689 - $api_error_detail = $decoded_error['error']['message'];
6690 - }
6691 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6692 - $this->current_valid_urls = [];
6693 - return '';
6694 - }
6695 - $result = json_decode($response_body, true);
6696 -
6697 - if (json_last_error() !== JSON_ERROR_NONE) {
6698 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6699 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6700 - $this->current_valid_urls = [];
6701 - return '';
6702 - }
6703 -
6704 - // Debug: Log the structure of the result
6705 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6706 - if (isset($result['output'])) {
6707 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6708 - foreach ($result['output'] as $idx => $out) {
6709 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6710 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6711 - }
6712 - } else {
6713 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6714 - }
6715 -
6716 - // Extract file search results from the response
6717 - $content = '';
6718 - $matches_used = 0;
6719 - $all_matches = [];
6720 -
6721 - // The Responses API returns output array with tool results
6722 - if (isset($result['output']) && is_array($result['output'])) {
6723 - foreach ($result['output'] as $output_item) {
6724 - // Look for file_search_call results
6725 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6726 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6727 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6728 -
6729 - // Check for search_results in the output item directly
6730 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6731 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6732 -
6733 - if (empty($search_results)) {
6734 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6735 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6736 - }
6737 -
6738 - foreach ($search_results as $index => $search_result) {
6739 - $filename = $search_result['filename'] ?? '';
6740 - $score = $search_result['score'] ?? 0;
6741 - $text_content = '';
6742 -
6743 - // Extract text content from the result
6744 - // The text can be directly on the result OR nested under content array
6745 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6746 - // Direct text field (OpenAI's actual format)
6747 - $text_content = $search_result['text'];
6748 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6749 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6750 - // Nested content array format
6751 - foreach ($search_result['content'] as $content_item) {
6752 - if (isset($content_item['text'])) {
6753 - $text_content .= $content_item['text'] . "\n";
6754 - }
6755 - }
6756 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6757 - } else {
6758 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6759 - }
6760 -
6761 - if (!empty($text_content)) {
6762 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6763 - $content .= trim($text_content) . "\n\n";
6764 -
6765 - if (!empty($filename)) {
6766 - $content .= "Source: " . $filename . "\n\n";
6767 - }
6768 -
6769 - // Extract URLs from content
6770 - preg_match_all(
6771 - '#\bhttps?://[^\s<>"\']+#i',
6772 - $text_content,
6773 - $content_urls
6774 - );
6775 - if (!empty($content_urls[0])) {
6776 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6777 - }
6778 -
6779 - $matches_used++;
6780 - }
6781 -
6782 - // Store for similarity analysis
6783 - $all_matches[] = [
6784 - 'document_id' => $filename ?: ('result_' . $index),
6785 - 'similarity' => $score,
6786 - 'similarity_percentage' => round($score * 100, 2),
6787 - 'above_threshold' => true,
6788 - 'source_display' => $filename,
6789 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6790 - 'used_for_context' => true,
6791 - 'role_restriction' => 'public',
6792 - 'has_access' => true,
6793 - 'filtered_out' => false
6794 - ];
6795 - }
6796 - }
6797 -
6798 - // Also check for message content with annotations (citations)
6799 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6800 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6801 - foreach ($output_item['content'] as $content_block) {
6802 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6803 - foreach ($content_block['annotations'] as $annotation) {
6804 - if (isset($annotation['filename'])) {
6805 - $filename = $annotation['filename'];
6806 - $score = $annotation['score'] ?? 0;
6807 - $text_content = '';
6808 -
6809 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6810 - foreach ($annotation['content'] as $ann_content) {
6811 - if (isset($ann_content['text'])) {
6812 - $text_content .= $ann_content['text'] . "\n";
6813 - }
6814 - }
6815 - }
6816 -
6817 - if (!empty($text_content) && $matches_used < $max_results) {
6818 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6819 - $content .= trim($text_content) . "\n\n";
6820 - $content .= "Source: " . $filename . "\n\n";
6821 -
6822 - preg_match_all(
6823 - '#\bhttps?://[^\s<>"\']+#i',
6824 - $text_content,
6825 - $content_urls
6826 - );
6827 - if (!empty($content_urls[0])) {
6828 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6829 - }
6830 -
6831 - $matches_used++;
6832 -
6833 - $all_matches[] = [
6834 - 'document_id' => $filename,
6835 - 'similarity' => $score,
6836 - 'similarity_percentage' => round($score * 100, 2),
6837 - 'above_threshold' => true,
6838 - 'source_display' => $filename,
6839 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6840 - 'used_for_context' => true,
6841 - 'role_restriction' => 'public',
6842 - 'has_access' => true,
6843 - 'filtered_out' => false
6844 - ];
6845 - }
6846 - }
6847 - }
6848 - }
6849 - }
6850 - }
6851 - }
6852 - }
6853 - }
6854 -
6855 - // Store for testing panel
6856 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6857 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6858 -
6859 - // Store unique valid URLs for validation
6860 - $this->current_valid_urls = array_unique($valid_urls);
6861 -
6862 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6863 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6864 -
6865 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6866 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6867 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6868 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6869 - if ($matches_used > 0) {
6870 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6871 - }
6872 -
6873 - // Check if citation links are enabled
6874 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6875 -
6876 - // Add response guidelines
6877 - if ($matches_used === 0) {
6878 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6879 - $content = "No reference information was found for this query.\n\n";
6880 - } else {
6881 - // Build response guidelines based on citation links setting
6882 - $content .= "\n## Response Guidelines ##\n" .
6883 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6884 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6885 - "If you don't have specific information or are uncertain about any details, it's always " .
6886 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6887 - "When information is incomplete, let them know you are unsure.\n\n";
6888 -
6889 - // Only add hyperlink instructions if citation links are enabled
6890 - if ($citation_links_enabled) {
6891 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6892 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6893 - } else {
6894 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6895 - "Simply provide helpful answers based on the reference information without citing sources.";
6896 - }
6897 - }
6898 -
6899 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6900 -
6901 2994 return trim($content);
6902 2995 }
6903 2996
6904 -/**
6905 - * Check if the given model is an OpenAI chat model
6906 - *
6907 - * @param string $model The model ID
6908 - * @return bool True if it's an OpenAI model
6909 - */
6910 -private function is_openai_chat_model($model) {
6911 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6912 - foreach ($openai_prefixes as $prefix) {
6913 - if (strpos($model, $prefix) === 0) {
6914 - return true;
6915 - }
6916 - }
6917 - return false;
6918 -}
6919 -
6920 -/**
6921 - * Get bot-specific Vector Store configuration
6922 - *
6923 - * @param string $bot_id The bot ID
6924 - * @return array Configuration array
6925 - */
6926 -private function get_bot_vectorstore_config($bot_id = 'default') {
6927 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6928 -
6929 - // Default global settings
6930 - $default_config = array(
6931 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6932 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6933 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6934 - );
6935 -
6936 - // Allow multi-bot plugin to override with bot-specific settings
6937 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6938 -
6939 - // Preserve max_results from global settings if not set in bot config
6940 - if (!isset($bot_config['max_results'])) {
6941 - $bot_config['max_results'] = $default_config['max_results'];
6942 - }
6943 -
6944 - return $bot_config;
6945 -}
6946 -
6947 2997 private function mxchat_find_relevant_products($user_embedding) {
6948 2998 //error_log('MXChat Vector Search: Starting product search...');
6949 2999
6950 3000 // Retrieve the add-on settings from the database
@@ -6962,78 +3012,77 @@
6962 3012 //error_log('MXChat Vector Search: Using WordPress database for products');
6963 3013 return $this->find_relevant_products_wordpress($user_embedding);
6964 3014 }
6965 3015 }
3016 +
6966 3017 private function find_relevant_products_wordpress($user_embedding) {
6967 3018 global $wpdb;
6968 3019 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3020 + $cache_key = 'mxchat_system_prompt_embeddings';
3021 + $batch_size = 500;
6969 3022
6970 - if (!is_array($user_embedding)) {
6971 - return '';
6972 - }
3023 + // Original WordPress database search logic
3024 + // [Previous implementation remains the same]
3025 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3026 + if ($embeddings === false) {
3027 + $embeddings = [];
3028 + $offset = 0;
6973 3029
6974 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6975 - // results above the similarity threshold. Peak memory is bounded by
6976 - // $batch_size embedding rows plus a 3-element top list.
6977 - $batch_size = 250;
6978 - $similarity_threshold = 0.85;
6979 - $top_k = 3;
6980 - $top_results = [];
6981 - $offset = 0;
3030 + do {
3031 + $query = $wpdb->prepare(
3032 + "SELECT id, embedding_vector
3033 + FROM {$system_prompt_table}
3034 + LIMIT %d OFFSET %d",
3035 + $batch_size,
3036 + $offset
3037 + );
6982 3038
6983 - do {
6984 - $batch = $wpdb->get_results($wpdb->prepare(
6985 - "SELECT id, embedding_vector
6986 - FROM {$system_prompt_table}
6987 - LIMIT %d OFFSET %d",
6988 - $batch_size,
6989 - $offset
6990 - ));
3039 + $batch = $wpdb->get_results($query);
3040 + if (empty($batch)) {
3041 + break;
3042 + }
6991 3043
6992 - if (empty($batch)) {
6993 - break;
6994 - }
3044 + $embeddings = array_merge($embeddings, $batch);
3045 + $offset += $batch_size;
6995 3046
6996 - foreach ($batch as $row) {
6997 - $database_embedding = $row->embedding_vector
6998 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6999 - : null;
3047 + unset($batch);
7000 3048
7001 - if (!is_array($database_embedding)) {
7002 - unset($database_embedding);
7003 - continue;
7004 - }
3049 + } while (true);
7005 3050
3051 + if (empty($embeddings)) {
3052 + return '';
3053 + }
3054 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3055 + }
3056 +
3057 + $relevant_results = [];
3058 + foreach ($embeddings as $embedding) {
3059 + $database_embedding = $embedding->embedding_vector
3060 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3061 + : null;
3062 + if (is_array($database_embedding) && is_array($user_embedding)) {
7006 3063 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
7007 - unset($database_embedding);
7008 -
7009 - if ($similarity < $similarity_threshold) {
7010 - continue;
7011 - }
7012 -
7013 - // Insert into bounded top-K (kept sorted descending)
7014 - if (count($top_results) < $top_k) {
7015 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
7016 - usort($top_results, function ($a, $b) {
7017 - return $b['similarity'] <=> $a['similarity'];
7018 - });
7019 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
7020 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
7021 - usort($top_results, function ($a, $b) {
7022 - return $b['similarity'] <=> $a['similarity'];
7023 - });
7024 - }
3064 + $relevant_results[] = [
3065 + 'id' => $embedding->id,
3066 + 'similarity' => $similarity
3067 + ];
7025 3068 }
3069 + unset($database_embedding);
3070 + }
7026 3071
7027 - unset($batch);
7028 - $offset += $batch_size;
7029 - } while (true);
3072 + // Use fixed threshold for products
3073 + $similarity_threshold = 0.85;
7030 3074
7031 - if (empty($top_results)) {
7032 - return '';
7033 - }
3075 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3076 + return $result['similarity'] >= $similarity_threshold;
3077 + });
3078 + usort($relevant_results, function ($a, $b) {
3079 + return $b['similarity'] <=> $a['similarity'];
3080 + });
7034 3081
3082 + $top_results = array_slice($relevant_results, 0, 5);
7035 3083 $content = '';
3084 +
7036 3085 foreach ($top_results as $result) {
7037 3086 $chunk_content = $this->fetch_content_with_product_links($result['id']);
7038 3087 $content .= $chunk_content . "\n\n";
7039 3088 }
@@ -7040,9 +3089,9 @@
7040 3089
7041 3090 return trim($content);
7042 3091 }
7043 3092
7044 -
3093 +// Modified search function with correct filter syntax
7045 3094 private function find_relevant_products_pinecone($user_embedding) {
7046 3095 //error_log('Starting Pinecone product search...');
7047 3096
7048 3097 $options = get_option('mxchat_pinecone_addon_options', array());
@@ -7139,655 +3188,25 @@
7139 3188
7140 3189 return null;
7141 3190 }
7142 3191
7143 -/**
7144 - * Get system instructions for a specific bot or default
7145 - * Checks for multi-bot add-on and uses bot-specific instructions if available
7146 - * Automatically strips URLs if citation links are disabled
7147 - * Replaces {visitor_name} placeholder with actual visitor name if available
7148 - *
7149 - * @param string $bot_id The bot ID to get instructions for
7150 - * @param string $session_id Optional session ID to lookup visitor name
7151 - */
7152 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
7153 - $instructions = '';
7154 -
7155 - // Check if multi-bot add-on is active
7156 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
7157 - // Get bot-specific options from multi-bot add-on
7158 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
7159 -
7160 - // If bot has custom system instructions, use those
7161 - if (!empty($bot_options['system_prompt_instructions'])) {
7162 - $instructions = $bot_options['system_prompt_instructions'];
7163 - }
7164 - }
7165 -
7166 - // Fall back to default system instructions
7167 - if (empty($instructions)) {
7168 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7169 - }
7170 -
7171 - // Check if citation links are disabled - if so, strip URLs from instructions
7172 - $fresh_options = get_option('mxchat_options', []);
7173 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
7174 -
7175 - if (!$citation_links_enabled && !empty($instructions)) {
7176 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
7177 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
7178 - }
7179 -
7180 - // Replace {visitor_name} placeholder with actual visitor name if available
7181 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
7182 - $name_option_key = "mxchat_name_{$session_id}";
7183 - $visitor_name = get_option($name_option_key, '');
7184 -
7185 - if (!empty($visitor_name)) {
7186 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
7187 - } else {
7188 - // Remove placeholder if no name is available
7189 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
7190 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
7191 - }
7192 - }
7193 -
7194 - // Allow developers to filter system instructions and process shortcodes
7195 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
7196 - $instructions = do_shortcode($instructions);
7197 -
7198 - return $instructions;
7199 -}
7200 -/**
7201 - * Get the current bot ID from session or request context
7202 - */
7203 -private function get_current_bot_id($session_id = '') {
7204 - // First, check if bot_id is passed in the current request
7205 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
7206 - return sanitize_key($_POST['bot_id']);
7207 - }
7208 -
7209 - // If not in POST, try to get it from session data
7210 - if (!empty($session_id)) {
7211 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
7212 - if (!empty($bot_id)) {
7213 - return $bot_id;
7214 - }
7215 - }
7216 -
7217 - // Fall back to default
7218 - return 'default';
7219 -}
7220 -/* ====================================================================== *
7221 - * Native function-calling loop (plan-mxchat-20260617-a41dee)
7222 - *
7223 - * Model-driven tool use. The model is offered MxChat's enabled callbacks as
7224 - * tools (sourced from MxChat_Tool_Registry, the single source the admin AI
7225 - * Tools checklist also reads). When the model calls a tool, the matching
7226 - * callback runs through its EXISTING permission checks, its output is fed
7227 - * back, and the loop continues up to a depth cap. INDEPENDENT of the
7228 - * intent→callback router — it runs only after intents miss, and works with
7229 - * ZERO Actions created.
7230 - *
7231 - * Entered ONLY when: function calling is enabled + the active model is
7232 - * tool-capable + at least one tool is enabled. Default-off, so existing
7233 - * installs never enter this branch (byte-for-byte unchanged behavior). The
7234 - * tool round is buffered (non-streaming) per the plan; the final answer is
7235 - * emitted via the same SSE/JSON envelopes the normal path uses.
7236 - * ====================================================================== */
7237 -
7238 -/** Gate: should the function-calling loop handle this turn? */
7239 -private function mxchat_fc_should_run($selected_model) {
7240 - if (!class_exists('MxChat_Tool_Registry') || !MxChat_Tool_Registry::is_enabled()) {
7241 - return false;
7242 - }
7243 - if (class_exists('MxChat_Model_Catalog') && !MxChat_Model_Catalog::supports_tools($selected_model)) {
7244 - return false;
7245 - }
7246 - $tools = MxChat_Tool_Registry::enabled_tools();
7247 - return !empty($tools);
7248 -}
7249 -
7250 -private function mxchat_fc_log($msg) {
7251 - if (defined('MXCHAT_DEV_MODE') && MXCHAT_DEV_MODE) {
7252 - error_log('[MxChat FC] ' . $msg);
7253 - }
7254 -}
7255 -
7256 -/**
7257 - * Resolve provider transport details. Returns null when FC can't run for this
7258 - * model/config (missing key, unsupported provider) so the caller falls back to
7259 - * the normal path. OpenAI/xAI/DeepSeek/OpenRouter/Custom share the
7260 - * OpenAI-compatible 'openai' family; Claude and Gemini are distinct.
7261 - */
7262 -private function mxchat_fc_resolve_provider($selected_model, $opts) {
7263 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
7264 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
7265 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
7266 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
7267 - if ($selected_model === 'openrouter') {
7268 - $model = isset($opts['openrouter_selected_model']) ? $opts['openrouter_selected_model'] : '';
7269 - $key = isset($opts['openrouter_api_key']) ? $opts['openrouter_api_key'] : '';
7270 - if ($model === '' || $key === '') return null;
7271 - return array('family'=>'openai','model'=>$model,'url'=>'https://openrouter.ai/api/v1/chat/completions',
7272 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7273 - }
7274 - $prefix = strtolower(explode('-', $selected_model)[0]);
7275 - switch ($prefix) {
7276 - case 'gpt': case 'o1': case 'o3': case 'o4':
7277 - $key = isset($opts['api_key']) ? $opts['api_key'] : '';
7278 - if ($key === '') return null;
7279 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.openai.com/v1/chat/completions',
7280 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7281 - case 'claude':
7282 - $key = isset($opts['claude_api_key']) ? $opts['claude_api_key'] : '';
7283 - if ($key === '') return null;
7284 - return array('family'=>'anthropic','model'=>$selected_model,'url'=>'https://api.anthropic.com/v1/messages',
7285 - 'headers'=>array('Content-Type'=>'application/json','x-api-key'=>$key,'anthropic-version'=>'2023-06-01'),'tag'=>'anthropic');
7286 - case 'gemini':
7287 - $key = isset($opts['gemini_api_key']) ? $opts['gemini_api_key'] : '';
7288 - if ($key === '') return null;
7289 - return array('family'=>'gemini','model'=>$selected_model,'key'=>$key,'tag'=>'gemini');
7290 - case 'grok': case 'xai':
7291 - $key = isset($opts['xai_api_key']) ? $opts['xai_api_key'] : '';
7292 - if ($key === '') return null;
7293 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.x.ai/v1/chat/completions',
7294 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'xai');
7295 - case 'deepseek':
7296 - $key = isset($opts['deepseek_api_key']) ? $opts['deepseek_api_key'] : '';
7297 - if ($key === '') return null;
7298 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.deepseek.com/v1/chat/completions',
7299 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7300 - case 'custom':
7301 - $base = isset($opts['custom_provider_base_url']) ? rtrim($opts['custom_provider_base_url'], '/') : '';
7302 - $key = isset($opts['custom_provider_api_key']) ? $opts['custom_provider_api_key'] : '';
7303 - $model = isset($opts['custom_provider_model']) ? $opts['custom_provider_model'] : '';
7304 - if ($base === '' || $model === '') return null;
7305 - $url = (strpos($base, 'chat/completions') !== false) ? $base : $base . '/chat/completions';
7306 - $headers = array('Content-Type'=>'application/json');
7307 - if ($key !== '') $headers['Authorization'] = 'Bearer '.$key;
7308 - return array('family'=>'openai','model'=>$model,'url'=>$url,'headers'=>$headers,'tag'=>'openai');
7309 - }
7310 - return null;
7311 -}
7312 -
7313 -/**
7314 - * Top-level function-calling attempt. Returns:
7315 - * ['handled'=>true, 'text'=>'<final answer>'] when the model used ≥1 tool
7316 - * ['handled'=>false] otherwise (caller falls back
7317 - * to the normal streamed path)
7318 - */
7319 -private function mxchat_fc_attempt($message, $relevant_content, $conversation_history, $selected_model, $opts, $session_id, $user_id) {
7320 - $prov = $this->mxchat_fc_resolve_provider($selected_model, $opts);
7321 - if (!$prov) {
7322 - return array('handled' => false);
7323 - }
7324 - $tools = MxChat_Tool_Registry::enabled_tools();
7325 - if (empty($tools)) {
7326 - return array('handled' => false);
7327 - }
7328 -
7329 - $bot_id = $this->get_current_bot_id($session_id);
7330 - $system = $this->get_system_instructions($bot_id, $session_id);
7331 -
7332 - // Force callbacks into return-mode (some echo SSE directly when streaming);
7333 - // we buffer the whole tool round, then emit once. Restored in finally.
7334 - $prev_streaming = $this->is_streaming;
7335 - $this->is_streaming = false;
3192 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history) {
7336 3193 try {
7337 - if ($prov['family'] === 'anthropic') {
7338 - return $this->mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7339 - } elseif ($prov['family'] === 'gemini') {
7340 - return $this->mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7341 - }
7342 - return $this->mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7343 - } catch (\Throwable $e) {
7344 - $this->mxchat_fc_log('attempt threw: ' . $e->getMessage());
7345 - return array('handled' => false);
7346 - } finally {
7347 - $this->is_streaming = $prev_streaming;
7348 - }
7349 -}
7350 -
7351 -/** Normalize MxChat history rows to [{role:user|assistant, content}]. */
7352 -private function mxchat_fc_normalize_history($conversation_history) {
7353 - $out = array();
7354 - if (!is_array($conversation_history)) return $out;
7355 - foreach ($conversation_history as $m) {
7356 - if (!is_array($m) || !isset($m['role']) || !isset($m['content'])) continue;
7357 - $role = $m['role'];
7358 - if ($role === 'bot' || $role === 'agent') $role = 'assistant';
7359 - if (!in_array($role, array('user', 'assistant'), true)) $role = 'user';
7360 - $out[] = array('role' => $role, 'content' => (string) $m['content']);
7361 - }
7362 - return $out;
7363 -}
7364 -
7365 -/** Execute the matched callback for a tool call. Returns ['ok'=>bool,'content'=>string]. */
7366 -private function mxchat_fc_execute_tool($tool_name, $args, $orig_message, $user_id, $session_id) {
7367 - $tool = MxChat_Tool_Registry::tool_by_name($tool_name, true); // enabled-only
7368 - if (!$tool) {
7369 - return array('ok' => false, 'content' => 'This tool is not available or not enabled.');
7370 - }
7371 - $fn = $tool['callback'];
7372 -
7373 - // MxChat callbacks are message-driven: hand them the model's `query`
7374 - // (falling back to the original user message).
7375 - $query = '';
7376 - if (is_array($args) && isset($args['query']) && is_string($args['query'])) {
7377 - $query = $args['query'];
7378 - }
7379 - if ($query === '') $query = $orig_message;
7380 -
7381 - // Synthetic intent row (matches wp_mxchat_intents columns → no undefined-prop warnings).
7382 - $synthetic_intent = (object) array(
7383 - 'id' => 0, 'intent_label' => $tool['label'], 'phrases' => '',
7384 - 'embedding_vector' => '', 'callback_function' => $fn,
7385 - 'similarity_threshold' => 0.0, 'enabled' => 1, 'enabled_bots' => null,
7386 - );
7387 -
7388 - try {
7389 - if (!empty($tool['is_addon'])) {
7390 - $result = apply_filters($fn, false, $query, $user_id, $session_id, $synthetic_intent);
7391 - } elseif (method_exists($this, $fn)) {
7392 - $result = call_user_func(array($this, $fn), $query, $user_id, $session_id, $synthetic_intent, null);
7393 - } else {
7394 - return array('ok' => false, 'content' => 'Tool implementation not found.');
7395 - }
7396 - } catch (\Throwable $e) {
7397 - $this->mxchat_fc_log("tool {$fn} threw: " . $e->getMessage());
7398 - return array('ok' => false, 'content' => 'The tool failed to run.');
7399 - }
7400 -
7401 - // plan-mxchat-20260617-48a57a — surface UI-bearing tool output.
7402 - // If the callback produced a UI element (generated image, product card, image
7403 - // gallery), its html MUST reach the FRONTEND as a real rendered bot message —
7404 - // NOT be stripped to text and handed to the model to paraphrase (that was the
7405 - // bug: under function calling, UI-bearing actions rendered nothing). Capture
7406 - // the html here; the FC outcome handler emits it in the response envelope.
7407 - $ui = $this->mxchat_fc_ui_payload_from($result);
7408 - if ($ui['html'] !== '' || !empty($ui['images'])) {
7409 - if ($ui['html'] !== '') {
7410 - $this->fc_ui_html .= ($this->fc_ui_html !== '' ? "\n" : '') . $ui['html'];
7411 - }
7412 - if (!empty($ui['images']) && is_array($ui['images'])) {
7413 - $this->fc_ui_images = array_merge($this->fc_ui_images, $ui['images']);
7414 - }
7415 - $this->fc_ui_captured = true;
7416 -
7417 - // Persist the html to the transcript ONLY if the callback did not already
7418 - // do so itself. Core image/search callbacks self-save (text + html);
7419 - // add-on callbacks (e.g. woo product cards) return html for the caller to
7420 - // save. ui_self_saves carries this from the registry; default by source
7421 - // (core self-saves, add-on does not) when a tool predates the flag.
7422 - $self_saves = array_key_exists('ui_self_saves', $tool)
7423 - ? !empty($tool['ui_self_saves'])
7424 - : empty($tool['is_addon']);
7425 - if ($ui['html'] !== '' && !$self_saves) {
7426 - $this->mxchat_save_chat_message($session_id, 'bot', $ui['html']);
7427 - }
7428 -
7429 - // Hand the MODEL a short acknowledgment (never the raw or stripped html)
7430 - // so the loop can add a one-line caption without trying to re-describe a
7431 - // visual it cannot see and without duplicating the displayed element.
7432 - $summary = isset($ui['text']) ? trim((string) $ui['text']) : '';
7433 - $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');
7434 - $content = $summary !== '' ? ($ack . ' ' . $summary) : $ack;
7435 - $this->mxchat_fc_log("executed {$fn} → [ui payload surfaced] " . substr($content, 0, 120));
7436 - return array('ok' => true, 'content' => $content);
7437 - }
7438 -
7439 - $content = $this->mxchat_fc_stringify_result($result);
7440 - $this->mxchat_fc_log("executed {$fn} → " . substr($content, 0, 160));
7441 - return array('ok' => true, 'content' => $content);
7442 -}
7443 -
7444 -/**
7445 - * Extract a UI payload (html + images + text) from a tool callback's return,
7446 - * falling back to $this->fallbackResponse for callbacks that return true after
7447 - * setting it. plan-mxchat-20260617-48a57a.
7448 - *
7449 - * @return array{html:string,images:array,text:string}
7450 - */
7451 -private function mxchat_fc_ui_payload_from($result) {
7452 - $src = null;
7453 - if (is_array($result)) {
7454 - $src = $result;
7455 - } elseif ($result === true && isset($this->fallbackResponse) && is_array($this->fallbackResponse)) {
7456 - $src = $this->fallbackResponse;
7457 - }
7458 - $html = (is_array($src) && isset($src['html']) && is_string($src['html'])) ? $src['html'] : '';
7459 - $images = (is_array($src) && isset($src['images']) && is_array($src['images'])) ? $src['images'] : array();
7460 - $text = (is_array($src) && isset($src['text'])) ? (string) $src['text'] : '';
7461 - return array('html' => $html, 'images' => $images, 'text' => $text);
7462 -}
7463 -
7464 -/** Coerce a callback's return (string|array|true|false) into a tool-result string. */
7465 -private function mxchat_fc_stringify_result($result) {
7466 - if (is_string($result)) {
7467 - return $result === '' ? 'No result.' : $result;
7468 - }
7469 - if ($result === true) {
7470 - // Callbacks that set fallbackResponse and return true.
7471 - $fb = isset($this->fallbackResponse) ? $this->fallbackResponse : null;
7472 - if (is_array($fb)) {
7473 - if (!empty($fb['text'])) return (string) $fb['text'];
7474 - if (!empty($fb['html'])) return wp_strip_all_tags((string) $fb['html']);
7475 - }
7476 - return 'Done.';
7477 - }
7478 - if ($result === false || $result === null) {
7479 - return 'No result.';
7480 - }
7481 - if (is_array($result)) {
7482 - if (isset($result['text']) && $result['text'] !== '') return (string) $result['text'];
7483 - if (isset($result['html']) && $result['html'] !== '') return wp_strip_all_tags((string) $result['html']);
7484 - $json = wp_json_encode($result);
7485 - return $json !== false ? $json : 'No result.';
7486 - }
7487 - return (string) $result;
7488 -}
7489 -
7490 -/** HTTP code + decoded body for a function-calling request. */
7491 -private function mxchat_fc_post($url, $body, $headers, $tag) {
7492 - $args = array(
7493 - 'body' => wp_json_encode($body),
7494 - 'headers' => $headers,
7495 - 'timeout' => 60,
7496 - 'redirection' => 5,
7497 - 'blocking' => true,
7498 - 'httpversion' => '1.0',
7499 - 'sslverify' => true,
7500 - );
7501 - $response = $this->mxchat_provider_call_with_retry($url, $args, $tag);
7502 - if (is_wp_error($response)) {
7503 - return array('code' => 0, 'data' => null, 'error' => $response->get_error_message());
7504 - }
7505 - $code = (int) wp_remote_retrieve_response_code($response);
7506 - $data = json_decode(wp_remote_retrieve_body($response), true);
7507 - return array('code' => $code, 'data' => $data, 'error' => null);
7508 -}
7509 -
7510 -/* ---------------- OpenAI-compatible loop (OpenAI/xAI/DeepSeek/OpenRouter/Custom) -------------- */
7511 -private function mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7512 - $messages = array();
7513 - $messages[] = array('role' => 'system', 'content' => $system . ' ' . $relevant_content);
7514 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7515 - $messages[] = $m;
7516 - }
7517 -
7518 - $depth = MxChat_Tool_Registry::max_depth();
7519 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7520 - $tool_schema = MxChat_Tool_Registry::to_openai_tools($tools);
7521 - $used_tool = false;
7522 - $calls_made = 0;
7523 -
7524 - for ($step = 0; $step <= $depth; $step++) {
7525 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7526 - $body = array('model' => $prov['model'], 'messages' => $messages, 'temperature' => 1, 'stream' => false);
7527 - if ($offer_tools) {
7528 - $body['tools'] = $tool_schema;
7529 - $body['tool_choice'] = 'auto';
7530 - }
7531 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7532 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7533 - $this->mxchat_fc_log('openai call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7534 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7535 - }
7536 - $msg = isset($r['data']['choices'][0]['message']) ? $r['data']['choices'][0]['message'] : null;
7537 - if (!$msg) {
7538 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7539 - }
7540 - $tool_calls = isset($msg['tool_calls']) && is_array($msg['tool_calls']) ? $msg['tool_calls'] : array();
7541 - if (empty($tool_calls)) {
7542 - $text = isset($msg['content']) ? trim((string) $msg['content']) : '';
7543 - if (!$used_tool) return array('handled' => false); // model never used a tool → normal path
7544 - return array('handled' => true, 'text' => ($text !== '' ? $text : $this->mxchat_fc_giveup_text()));
7545 - }
7546 - // Append the assistant tool-call turn verbatim, then a tool result per call.
7547 - $used_tool = true;
7548 - $messages[] = $msg;
7549 - foreach ($tool_calls as $tc) {
7550 - if ($calls_made >= $budget) break;
7551 - $calls_made++;
7552 - $name = isset($tc['function']['name']) ? $tc['function']['name'] : '';
7553 - $args = array();
7554 - if (isset($tc['function']['arguments'])) {
7555 - $decoded = json_decode($tc['function']['arguments'], true);
7556 - if (is_array($decoded)) $args = $decoded;
7557 - }
7558 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7559 - $messages[] = array(
7560 - 'role' => 'tool',
7561 - 'tool_call_id' => isset($tc['id']) ? $tc['id'] : '',
7562 - 'content' => $exec['content'],
7563 - );
7564 - }
7565 - }
7566 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7567 -}
7568 -
7569 -/* ---------------- Anthropic Claude loop ---------------- */
7570 -private function mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7571 - $messages = $this->mxchat_fc_normalize_history($conversation_history);
7572 - $messages[] = array('role' => 'user', 'content' => $relevant_content);
7573 -
7574 - $depth = MxChat_Tool_Registry::max_depth();
7575 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7576 - $tool_schema = MxChat_Tool_Registry::to_anthropic_tools($tools);
7577 - $omit_temp = $this->mxchat_claude_omits_temperature($prov['model']);
7578 - $used_tool = false;
7579 - $calls_made = 0;
7580 -
7581 - for ($step = 0; $step <= $depth; $step++) {
7582 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7583 - $body = array('model' => $prov['model'], 'max_tokens' => 1024, 'temperature' => 0.8,
7584 - 'messages' => $messages, 'system' => $system);
7585 - if ($omit_temp) unset($body['temperature']);
7586 - if ($offer_tools) {
7587 - $body['tools'] = $tool_schema;
7588 - $body['tool_choice'] = array('type' => 'auto');
7589 - }
7590 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7591 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7592 - $this->mxchat_fc_log('anthropic call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7593 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7594 - }
7595 - $content = isset($r['data']['content']) && is_array($r['data']['content']) ? $r['data']['content'] : array();
7596 - $tool_uses = array();
7597 - $text_out = '';
7598 - foreach ($content as $block) {
7599 - if (!isset($block['type'])) continue;
7600 - if ($block['type'] === 'tool_use') {
7601 - $tool_uses[] = $block;
7602 - } elseif ($block['type'] === 'text' && isset($block['text'])) {
7603 - $text_out .= $block['text'];
7604 - }
7605 - }
7606 - if (empty($tool_uses)) {
7607 - if (!$used_tool) return array('handled' => false);
7608 - $text_out = trim($text_out);
7609 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7610 - }
7611 - // Append the assistant turn (the full content array), then a user turn of tool_result blocks.
7612 - $used_tool = true;
7613 - $messages[] = array('role' => 'assistant', 'content' => $content);
7614 - $results = array();
7615 - foreach ($tool_uses as $tu) {
7616 - if ($calls_made >= $budget) break;
7617 - $calls_made++;
7618 - $name = isset($tu['name']) ? $tu['name'] : '';
7619 - $args = isset($tu['input']) && is_array($tu['input']) ? $tu['input'] : array();
7620 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7621 - $results[] = array(
7622 - 'type' => 'tool_result',
7623 - 'tool_use_id' => isset($tu['id']) ? $tu['id'] : '',
7624 - 'content' => $exec['content'],
7625 - );
7626 - }
7627 - $messages[] = array('role' => 'user', 'content' => $results);
7628 - }
7629 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7630 -}
7631 -
7632 -/* ---------------- Google Gemini loop ---------------- */
7633 -private function mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7634 - $contents = array();
7635 - $contents[] = array('role' => 'user', 'parts' => array(array('text' => '[System Instructions] ' . $system . ' ' . $relevant_content)));
7636 - $contents[] = array('role' => 'model', 'parts' => array(array('text' => 'I understand and will follow these instructions.')));
7637 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7638 - $contents[] = array('role' => ($m['role'] === 'assistant' ? 'model' : 'user'),
7639 - 'parts' => array(array('text' => $m['content'])));
7640 - }
7641 -
7642 - $depth = MxChat_Tool_Registry::max_depth();
7643 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7644 - $tool_schema = MxChat_Tool_Registry::to_gemini_tools($tools);
7645 - // Function calling (tools + functionDeclarations + toolConfig) is a v1beta feature on the
7646 - // Generative Language REST API. The v1 endpoint silently ignores the tools array, so a
7647 - // non-preview model (e.g. gemini-2.5-pro, gemini-3.5-flash, gemini-3.1-flash-lite) would
7648 - // just answer in text and never emit a tool call. Always use v1beta for the FC loop —
7649 - // confirmed against Google's function-calling docs (their REST example targets
7650 - // v1beta/models/gemini-3.5-flash:generateContent). v1beta is a superset, so every model
7651 - // reachable on v1 is also reachable here.
7652 - $api_version = 'v1beta';
7653 - $url = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $prov['model'] . ':generateContent?key=' . $prov['key'];
7654 - $headers = array('Content-Type' => 'application/json');
7655 - $used_tool = false;
7656 - $calls_made = 0;
7657 -
7658 - for ($step = 0; $step <= $depth; $step++) {
7659 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7660 - $body = array(
7661 - 'contents' => $contents,
7662 - 'generationConfig' => array('temperature' => 0.7, 'topP' => 0.95, 'topK' => 40, 'maxOutputTokens' => 8192),
7663 - );
7664 - if ($offer_tools) {
7665 - $body['tools'] = $tool_schema;
7666 - $body['toolConfig'] = array('functionCallingConfig' => array('mode' => 'AUTO'));
7667 - }
7668 - $r = $this->mxchat_fc_post($url, $body, $headers, 'gemini');
7669 - if ($r['code'] !== 200 || !is_array($r['data']) || isset($r['data']['error'])) {
7670 - $this->mxchat_fc_log('gemini call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7671 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7672 - }
7673 - $parts = isset($r['data']['candidates'][0]['content']['parts']) && is_array($r['data']['candidates'][0]['content']['parts'])
7674 - ? $r['data']['candidates'][0]['content']['parts'] : array();
7675 - $fn_calls = array();
7676 - $text_out = '';
7677 - foreach ($parts as $p) {
7678 - if (isset($p['functionCall'])) {
7679 - $fn_calls[] = $p['functionCall'];
7680 - } elseif (isset($p['text'])) {
7681 - $text_out .= $p['text'];
7682 - }
7683 - }
7684 - if (empty($fn_calls)) {
7685 - if (!$used_tool) return array('handled' => false);
7686 - $text_out = trim($text_out);
7687 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7688 - }
7689 - // Append the model turn (its parts) then a user turn of functionResponse parts.
7690 - $used_tool = true;
7691 - $contents[] = array('role' => 'model', 'parts' => $parts);
7692 - $resp_parts = array();
7693 - foreach ($fn_calls as $fcall) {
7694 - if ($calls_made >= $budget) break;
7695 - $calls_made++;
7696 - $name = isset($fcall['name']) ? $fcall['name'] : '';
7697 - $args = isset($fcall['args']) && is_array($fcall['args']) ? $fcall['args'] : array();
7698 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7699 - $fr = array('name' => $name, 'response' => array('result' => $exec['content']));
7700 - // Gemini 3 function calls carry a unique id; echo the matching id back in the
7701 - // functionResponse so the model maps the result to the right call (Google REST
7702 - // guidance). Older models omit the id — then we send none, exactly as before.
7703 - if (isset($fcall['id']) && $fcall['id'] !== '') { $fr['id'] = $fcall['id']; }
7704 - $resp_parts[] = array('functionResponse' => $fr);
7705 - }
7706 - $contents[] = array('role' => 'user', 'parts' => $resp_parts);
7707 - }
7708 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7709 -}
7710 -
7711 -private function mxchat_fc_giveup_text() {
7712 - return esc_html__('I looked into that but could not put together a final answer. Please try rephrasing your request.', 'mxchat');
7713 -}
7714 -
7715 -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') {
7716 - try {
7717 3194 if (!$relevant_content) {
7718 - $error_response = [
3195 + return [
7719 3196 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7720 3197 'error_code' => 'no_relevant_content'
7721 3198 ];
7722 -
7723 - if ($testing_data !== null) {
7724 - $error_response['testing_data'] = $testing_data;
7725 - }
7726 -
7727 - return $error_response;
7728 3199 }
7729 3200
3201 + // Ensure conversation_history is an array
7730 3202 if (!is_array($conversation_history)) {
7731 3203 $conversation_history = array();
7732 3204 }
7733 3205
7734 - // Check if this is an OpenRouter model
7735 - if ($selected_model === 'openrouter') {
7736 - // Get the actual OpenRouter model from options
7737 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7738 -
7739 - if (empty($openrouter_selected_model)) {
7740 - $error_response = [
7741 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7742 - 'error_code' => 'no_openrouter_model_selected'
7743 - ];
7744 - if ($testing_data !== null) {
7745 - $error_response['testing_data'] = $testing_data;
7746 - }
7747 - return $error_response;
7748 - }
7749 -
7750 - if (empty($openrouter_api_key)) {
7751 - $error_response = [
7752 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7753 - 'error_code' => 'missing_openrouter_api_key'
7754 - ];
7755 - if ($testing_data !== null) {
7756 - $error_response['testing_data'] = $testing_data;
7757 - }
7758 - return $error_response;
7759 - }
7760 -
7761 - if ($streaming) {
7762 - return $this->mxchat_generate_response_openrouter_stream(
7763 - $openrouter_selected_model,
7764 - $openrouter_api_key,
7765 - $conversation_history,
7766 - $relevant_content,
7767 - $session_id,
7768 - $testing_data
7769 - );
7770 - } else {
7771 - $response = $this->mxchat_generate_response_openrouter(
7772 - $openrouter_selected_model,
7773 - $openrouter_api_key,
7774 - $conversation_history,
7775 - $relevant_content,
7776 - $session_id
7777 - );
7778 - }
7779 -
7780 - if (is_array($response) && isset($response['error'])) {
7781 - if ($testing_data !== null) {
7782 - $response['testing_data'] = $testing_data;
7783 - }
7784 - return $response;
7785 - }
7786 -
7787 - return $response;
7788 - }
7789 -
3206 + // Get selected model with default fallback
3207 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
3208 +
7790 3209 // Extract model prefix to determine the provider
7791 3210 $model_parts = explode('-', $selected_model);
7792 3211 $provider = strtolower($model_parts[0]);
7793 3212
@@ -7794,2011 +3213,147 @@
7794 3213 // Handle model selection based on provider prefix
7795 3214 switch ($provider) {
7796 3215 case 'gemini':
7797 3216 if (empty($gemini_api_key)) {
7798 - $error_response = [
3217 + return [
7799 3218 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
7800 3219 'error_code' => 'missing_gemini_api_key'
7801 3220 ];
7802 - if ($testing_data !== null) {
7803 - $error_response['testing_data'] = $testing_data;
7804 - }
7805 - return $error_response;
7806 3221 }
7807 3222 $response = $this->mxchat_generate_response_gemini(
7808 3223 $selected_model,
7809 3224 $gemini_api_key,
7810 3225 $conversation_history,
7811 - $relevant_content,
7812 - $session_id
3226 + $relevant_content
7813 3227 );
7814 3228 break;
7815 3229
7816 3230 case 'claude':
7817 3231 if (empty($claude_api_key)) {
7818 - $error_response = [
3232 + return [
7819 3233 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
7820 3234 'error_code' => 'missing_claude_api_key'
7821 3235 ];
7822 - if ($testing_data !== null) {
7823 - $error_response['testing_data'] = $testing_data;
7824 - }
7825 - return $error_response;
7826 3236 }
7827 - if ($streaming) {
7828 - return $this->mxchat_generate_response_claude_stream(
7829 - $selected_model,
7830 - $claude_api_key,
7831 - $conversation_history,
7832 - $relevant_content,
7833 - $session_id,
7834 - $testing_data
7835 - );
7836 - } else {
7837 - $response = $this->mxchat_generate_response_claude(
7838 - $selected_model,
7839 - $claude_api_key,
7840 - $conversation_history,
7841 - $relevant_content,
7842 - $session_id
7843 - );
7844 - }
3237 + $response = $this->mxchat_generate_response_claude(
3238 + $selected_model,
3239 + $claude_api_key,
3240 + $conversation_history,
3241 + $relevant_content
3242 + );
7845 3243 break;
7846 3244
7847 3245 case 'grok':
7848 3246 if (empty($xai_api_key)) {
7849 - $error_response = [
3247 + return [
7850 3248 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
7851 3249 'error_code' => 'missing_xai_api_key'
7852 3250 ];
7853 - if ($testing_data !== null) {
7854 - $error_response['testing_data'] = $testing_data;
7855 - }
7856 - return $error_response;
7857 3251 }
7858 - if ($streaming) {
7859 - return $this->mxchat_generate_response_xai_stream(
7860 - $selected_model,
7861 - $xai_api_key,
7862 - $conversation_history,
7863 - $relevant_content,
7864 - $session_id,
7865 - $testing_data
7866 - );
7867 - } else {
7868 - $response = $this->mxchat_generate_response_xai(
7869 - $selected_model,
7870 - $xai_api_key,
7871 - $conversation_history,
7872 - $relevant_content,
7873 - $session_id
7874 - );
7875 - }
3252 + $response = $this->mxchat_generate_response_xai(
3253 + $selected_model,
3254 + $xai_api_key,
3255 + $conversation_history,
3256 + $relevant_content
3257 + );
7876 3258 break;
7877 3259
7878 3260 case 'deepseek':
7879 3261 if (empty($deepseek_api_key)) {
7880 - $error_response = [
3262 + return [
7881 3263 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
7882 3264 'error_code' => 'missing_deepseek_api_key'
7883 3265 ];
7884 - if ($testing_data !== null) {
7885 - $error_response['testing_data'] = $testing_data;
7886 - }
7887 - return $error_response;
7888 3266 }
7889 - if ($streaming) {
7890 - return $this->mxchat_generate_response_deepseek_stream(
7891 - $selected_model,
7892 - $deepseek_api_key,
7893 - $conversation_history,
7894 - $relevant_content,
7895 - $session_id,
7896 - $testing_data
7897 - );
7898 - } else {
7899 - $response = $this->mxchat_generate_response_deepseek(
7900 - $selected_model,
7901 - $deepseek_api_key,
7902 - $conversation_history,
7903 - $relevant_content,
7904 - $session_id
7905 - );
7906 - }
3267 + $response = $this->mxchat_generate_response_deepseek(
3268 + $selected_model,
3269 + $deepseek_api_key,
3270 + $conversation_history,
3271 + $relevant_content
3272 + );
7907 3273 break;
7908 3274
7909 - case 'custom':
7910 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7911 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7912 - if (empty($cp_base_url)) {
7913 - $error_response = [
7914 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7915 - 'error_code' => 'missing_custom_provider_base_url'
7916 - ];
7917 - if ($testing_data !== null) {
7918 - $error_response['testing_data'] = $testing_data;
7919 - }
7920 - return $error_response;
7921 - }
7922 - if ($streaming) {
7923 - return $this->mxchat_generate_response_custom_stream(
7924 - $selected_model,
7925 - $conversation_history,
7926 - $relevant_content,
7927 - $session_id,
7928 - $testing_data
7929 - );
7930 - } else {
7931 - $response = $this->mxchat_generate_response_custom(
7932 - $selected_model,
7933 - $conversation_history,
7934 - $relevant_content
7935 - );
7936 - }
7937 - break;
7938 -
7939 3275 case 'gpt':
7940 - case 'o1':
7941 3276 if (empty($api_key)) {
7942 - $error_response = [
3277 + return [
7943 3278 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7944 3279 'error_code' => 'missing_openai_api_key'
7945 3280 ];
7946 - if ($testing_data !== null) {
7947 - $error_response['testing_data'] = $testing_data;
7948 - }
7949 - return $error_response;
7950 3281 }
7951 -
7952 - // Check if web search is enabled for this OpenAI model
7953 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7954 - // Models that don't support web search
7955 - $unsupported_web_search_models = array('gpt-4.1-nano');
7956 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7957 -
7958 - if ($web_search_enabled && $model_supports_web_search) {
7959 - // Use Responses API (required for some models, or when web search is enabled)
7960 - return $this->mxchat_generate_response_openai_web_search(
7961 - $selected_model,
7962 - $api_key,
7963 - $conversation_history,
7964 - $relevant_content,
7965 - $session_id,
7966 - $testing_data,
7967 - $streaming
7968 - );
7969 - } elseif ($streaming) {
7970 - return $this->mxchat_generate_response_openai_stream(
7971 - $selected_model,
7972 - $api_key,
7973 - $conversation_history,
7974 - $relevant_content,
7975 - $session_id,
7976 - $testing_data
7977 - );
7978 - } else {
7979 - $response = $this->mxchat_generate_response_openai(
7980 - $selected_model,
7981 - $api_key,
7982 - $conversation_history,
7983 - $relevant_content,
7984 - $session_id
7985 - );
7986 - }
3282 + $response = $this->mxchat_generate_response_openai(
3283 + $selected_model,
3284 + $api_key,
3285 + $conversation_history,
3286 + $relevant_content
3287 + );
7987 3288 break;
7988 3289
7989 3290 default:
3291 + // Default to OpenAI for custom models or unrecognized prefixes
7990 3292 if (empty($api_key)) {
7991 - $error_response = [
3293 + return [
7992 3294 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7993 3295 'error_code' => 'missing_openai_api_key'
7994 3296 ];
7995 - if ($testing_data !== null) {
7996 - $error_response['testing_data'] = $testing_data;
7997 - }
7998 - return $error_response;
7999 3297 }
8000 -
8001 - // Check if web search is enabled (default case also handles OpenAI models)
8002 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8003 - $unsupported_web_search_models = array('gpt-4.1-nano');
8004 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
8005 -
8006 - if ($web_search_enabled && $model_supports_web_search) {
8007 - return $this->mxchat_generate_response_openai_web_search(
8008 - $selected_model,
8009 - $api_key,
8010 - $conversation_history,
8011 - $relevant_content,
8012 - $session_id,
8013 - $testing_data,
8014 - $streaming
8015 - );
8016 - } elseif ($streaming) {
8017 - return $this->mxchat_generate_response_openai_stream(
8018 - $selected_model,
8019 - $api_key,
8020 - $conversation_history,
8021 - $relevant_content,
8022 - $session_id,
8023 - $testing_data
8024 - );
8025 - } else {
8026 - $response = $this->mxchat_generate_response_openai(
8027 - $selected_model,
8028 - $api_key,
8029 - $conversation_history,
8030 - $relevant_content,
8031 - $session_id
8032 - );
8033 - }
3298 + $response = $this->mxchat_generate_response_openai(
3299 + $selected_model,
3300 + $api_key,
3301 + $conversation_history,
3302 + $relevant_content
3303 + );
8034 3304 break;
8035 3305 }
8036 3306
3307 + // Check if the response is an error array from the provider-specific function
8037 3308 if (is_array($response) && isset($response['error'])) {
8038 - if ($testing_data !== null) {
8039 - $response['testing_data'] = $testing_data;
8040 - }
8041 - return $response;
3309 + return $response; // Pass through the error
8042 3310 }
8043 3311
8044 3312 return $response;
8045 3313
8046 3314 } catch (Exception $e) {
8047 - $error_response = [
3315 + //error_log('MXChat Error: ' . $e->getMessage());
3316 + return [
8048 3317 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
8049 3318 'error_code' => 'system_exception',
8050 3319 'exception_details' => $e->getMessage()
8051 3320 ];
8052 -
8053 - if ($testing_data !== null) {
8054 - $error_response['testing_data'] = $testing_data;
8055 - }
8056 -
8057 - return $error_response;
8058 3321 }
8059 3322 }
8060 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8061 - try {
8062 - $bot_id = $this->get_current_bot_id($session_id);
8063 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8064 -
8065 - if (!is_array($conversation_history)) {
8066 - $conversation_history = array();
8067 - }
8068 3323
8069 - $formatted_conversation = array();
8070 -
8071 - $formatted_conversation[] = array(
8072 - 'role' => 'system',
8073 - 'content' => $system_prompt_instructions . " " . $relevant_content
8074 - );
8075 -
8076 - foreach ($conversation_history as $message) {
8077 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8078 - $role = $message['role'];
8079 - if ($role === 'bot' || $role === 'agent') {
8080 - $role = 'assistant';
8081 - }
8082 - if (!in_array($role, ['system', 'assistant', 'user'])) {
8083 - $role = 'user';
8084 - }
8085 - $formatted_conversation[] = array(
8086 - 'role' => $role,
8087 - 'content' => $message['content']
8088 - );
8089 - }
8090 - }
8091 -
8092 - if (headers_sent() || !function_exists('curl_init')) {
8093 - $regular_response = $this->mxchat_generate_response_openrouter(
8094 - $selected_model,
8095 - $openrouter_api_key,
8096 - $conversation_history,
8097 - $relevant_content,
8098 - $session_id
8099 - );
8100 -
8101 - // Save bot response to transcript
8102 - if (!empty($regular_response) && !empty($session_id)) {
8103 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8104 - }
8105 -
8106 - $response_data = [
8107 - 'text' => $regular_response,
8108 - 'html' => '',
8109 - 'session_id' => $session_id
8110 - ];
8111 -
8112 - if ($testing_data !== null) {
8113 - $response_data['testing_data'] = $testing_data;
8114 - }
8115 -
8116 - header('Content-Type: application/json');
8117 - echo json_encode($response_data);
8118 - return true;
8119 - }
8120 -
8121 - $body = json_encode([
8122 - 'model' => $selected_model,
8123 - 'messages' => $formatted_conversation,
8124 - 'temperature' => 1,
8125 - 'stream' => true
8126 - ]);
8127 -
8128 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
8129 - // inside WRITEFUNCTION on first byte of a successful upstream.
8130 -
8131 - $captured_status_code = 0;
8132 - $captured_body_pre_stream = '';
8133 - $full_response = '';
8134 - $stream_started = false;
8135 - $buffer = '';
8136 - $errno = 0;
8137 - $last_curl_error = '';
8138 - $http_code = 0;
8139 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8140 - $backoff_ms = array(0, 750, 2000);
8141 -
8142 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8143 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8144 - usleep($backoff_ms[$attempt] * 1000);
8145 - }
8146 -
8147 - $captured_status_code = 0;
8148 - $captured_body_pre_stream = '';
8149 - $full_response = '';
8150 - $stream_started = false;
8151 - $buffer = '';
8152 -
8153 - $ch = curl_init();
8154 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
8155 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8156 - curl_setopt($ch, CURLOPT_POST, true);
8157 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8158 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8159 - 'Content-Type: application/json',
8160 - 'Authorization: Bearer ' . $openrouter_api_key,
8161 - 'HTTP-Referer: ' . home_url(),
8162 - 'X-Title: ' . get_bloginfo('name')
8163 - ));
8164 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8165 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8166 -
8167 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8168 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8169 - $captured_status_code = (int) $m[1];
8170 - }
8171 - return strlen($header);
8172 - });
8173 -
8174 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8175 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8176 - $captured_body_pre_stream .= $data;
8177 - return strlen($data);
8178 - }
8179 -
8180 - if (!$this->streaming_headers_sent) {
8181 - $this->setup_streaming_headers();
8182 - }
8183 -
8184 - if (!$stream_started && $testing_data !== null) {
8185 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8186 - flush();
8187 - $stream_started = true;
8188 - }
8189 -
8190 - $buffer .= $data;
8191 - $lines = explode("\n", $buffer);
8192 - $buffer = array_pop($lines);
8193 -
8194 - foreach ($lines as $line) {
8195 - if (trim($line) === '') {
8196 - continue;
8197 - }
8198 - if (strpos($line, 'data: ') !== 0) {
8199 - continue;
8200 - }
8201 -
8202 - $json_str = substr($line, 6);
8203 -
8204 - if (trim($json_str) === '[DONE]') {
8205 - echo "data: [DONE]\n\n";
8206 - flush();
8207 - continue;
8208 - }
8209 -
8210 - $json = json_decode(trim($json_str), true);
8211 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8212 - $content = $json['choices'][0]['delta']['content'];
8213 - $full_response .= $content;
8214 -
8215 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8216 - flush();
8217 - }
8218 - }
8219 -
8220 - return strlen($data);
8221 - });
8222 -
8223 - $response = curl_exec($ch);
8224 - $errno = curl_errno($ch);
8225 - $last_curl_error = curl_error($ch);
8226 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8227 - curl_close($ch);
8228 -
8229 - if (!$errno && $http_code === 200) {
8230 - break;
8231 - }
8232 -
8233 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8234 - $can_retry = !$this->streaming_headers_sent
8235 - && ($attempt + 1) < $max_attempts
8236 - && $is_transient;
8237 -
8238 - if (defined('WP_DEBUG') && WP_DEBUG) {
8239 - error_log(sprintf(
8240 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8241 - $attempt + 1, $max_attempts, $http_code, $errno,
8242 - $is_transient ? 'yes' : 'no',
8243 - $can_retry ? 'Retrying.' : 'Giving up.'
8244 - ));
8245 - }
8246 -
8247 - if (!$can_retry) {
8248 - break;
8249 - }
8250 - }
8251 -
8252 - if (!$errno && $http_code === 200) {
8253 - if (!empty($full_response) && !empty($session_id)) {
8254 - $rag_context_for_storage = null;
8255 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8256 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8257 -
8258 - if ($has_rag_data || $has_action_data) {
8259 - $rag_context_for_storage = [];
8260 -
8261 - if ($has_rag_data) {
8262 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8263 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8264 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8265 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8266 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8267 - }
8268 -
8269 - if ($has_action_data) {
8270 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8271 - }
8272 - }
8273 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8274 - }
8275 - return true;
8276 - }
8277 -
8278 - return $this->mxchat_stream_emit_fallback(
8279 - 'openai',
8280 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8281 - $session_id,
8282 - $testing_data
8283 - );
8284 -
8285 - } catch (Exception $e) {
8286 - return $this->mxchat_stream_emit_fallback(
8287 - 'openai',
8288 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8289 - $session_id,
8290 - $testing_data
8291 - );
8292 - }
8293 -}
8294 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
3324 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
8295 3325 try {
8296 - $bot_id = $this->get_current_bot_id($session_id);
8297 -
8298 - // Get system prompt instructions using centralized function
8299 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8300 -
8301 3326 // Ensure conversation_history is an array
8302 3327 if (!is_array($conversation_history)) {
8303 3328 $conversation_history = array();
8304 3329 }
8305 3330
8306 - // Format conversation history for OpenAI
3331 + // Get system prompt instructions from options
3332 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3333 +
3334 + // Create a new array for the formatted conversation
8307 3335 $formatted_conversation = array();
8308 3336
3337 + // Add system message first
8309 3338 $formatted_conversation[] = array(
8310 3339 'role' => 'system',
8311 3340 'content' => $system_prompt_instructions . " " . $relevant_content
8312 3341 );
8313 3342
3343 + // Add the rest of the conversation history
8314 3344 foreach ($conversation_history as $message) {
8315 3345 if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8316 3346 $role = $message['role'];
8317 - if ($role === 'bot' || $role === 'agent') {
8318 - $role = 'assistant';
8319 - }
8320 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8321 - $role = 'user';
8322 - }
8323 - $formatted_conversation[] = array(
8324 - 'role' => $role,
8325 - 'content' => $message['content']
8326 - );
8327 - }
8328 - }
8329 3347
8330 - // Check if we can actually stream
8331 - if (headers_sent() || !function_exists('curl_init')) {
8332 - // Fallback to regular response with testing data
8333 - $regular_response = $this->mxchat_generate_response_openai(
8334 - $selected_model,
8335 - $api_key,
8336 - $conversation_history,
8337 - $relevant_content,
8338 - $session_id
8339 - );
8340 -
8341 - // Save bot response to transcript
8342 - if (!empty($regular_response) && !empty($session_id)) {
8343 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8344 - }
8345 -
8346 - $response_data = [
8347 - 'text' => $regular_response,
8348 - 'html' => '',
8349 - 'session_id' => $session_id
8350 - ];
8351 -
8352 - if ($testing_data !== null) {
8353 - $response_data['testing_data'] = $testing_data;
8354 - }
8355 -
8356 - header('Content-Type: application/json');
8357 - echo json_encode($response_data);
8358 - return true;
8359 - }
8360 -
8361 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
8362 - $is_gpt5_model = (
8363 - strpos($selected_model, 'gpt-5') === 0 ||
8364 - $selected_model === 'gpt-5.2' ||
8365 - $selected_model === 'gpt-5.1-2025-11-13' ||
8366 - $selected_model === 'gpt-5' ||
8367 - $selected_model === 'gpt-5-mini' ||
8368 - $selected_model === 'gpt-5-nano'
8369 - );
8370 -
8371 - // Build request body with optimal settings for fast streaming
8372 - $request_body = [
8373 - 'model' => $selected_model,
8374 - 'messages' => $formatted_conversation,
8375 - 'temperature' => 1,
8376 - 'stream' => true
8377 - ];
8378 -
8379 - // Add reasoning_effort only for GPT-5 models that support it
8380 - // These chat models don't support reasoning_effort parameter
8381 - $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');
8382 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
8383 - // GPT-5.1 uses 'low' instead of 'minimal'
8384 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8385 - $request_body['reasoning_effort'] = 'low';
8386 - } elseif ($selected_model === 'gpt-5.5') {
8387 - $request_body['reasoning_effort'] = 'none';
8388 - } elseif ($selected_model === 'gpt-5.4') {
8389 - $request_body['reasoning_effort'] = 'none';
8390 - } else {
8391 - $request_body['reasoning_effort'] = 'minimal';
8392 - }
8393 - }
8394 -
8395 - $body = json_encode($request_body);
8396 -
8397 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
8398 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
8399 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
8400 -
8401 - $captured_status_code = 0;
8402 - $captured_body_pre_stream = '';
8403 - $full_response = '';
8404 - $stream_started = false;
8405 - $buffer = '';
8406 - $errno = 0;
8407 - $last_curl_error = '';
8408 - $http_code = 0;
8409 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8410 - $backoff_ms = array(0, 750, 2000);
8411 -
8412 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8413 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8414 - usleep($backoff_ms[$attempt] * 1000);
8415 - }
8416 -
8417 - // Reset per-attempt capture state.
8418 - $captured_status_code = 0;
8419 - $captured_body_pre_stream = '';
8420 - $full_response = '';
8421 - $stream_started = false;
8422 - $buffer = '';
8423 -
8424 - $ch = curl_init();
8425 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
8426 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8427 - curl_setopt($ch, CURLOPT_POST, true);
8428 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8429 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8430 - 'Content-Type: application/json',
8431 - 'Authorization: Bearer ' . $api_key
8432 - ));
8433 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8434 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8435 -
8436 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
8437 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8438 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8439 - $captured_status_code = (int) $m[1];
8440 - }
8441 - return strlen($header);
8442 - });
8443 -
8444 - // Buffer control for real-time streaming
8445 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8446 - // V2 guard: if upstream returned non-200, buffer body for transient
8447 - // classification and DO NOT emit to client. Stream channel must NOT open.
8448 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8449 - $captured_body_pre_stream .= $data;
8450 - return strlen($data);
8451 - }
8452 -
8453 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
8454 - // After this point streaming_headers_sent === true → retry is structurally blocked.
8455 - if (!$this->streaming_headers_sent) {
8456 - $this->setup_streaming_headers();
8457 - }
8458 -
8459 - // Send testing data as the first event if available
8460 - if (!$stream_started && $testing_data !== null) {
8461 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8462 - flush();
8463 - $stream_started = true;
8464 - }
8465 -
8466 - // CRITICAL FIX: Append new data to buffer
8467 - $buffer .= $data;
8468 -
8469 - // Process complete lines only
8470 - $lines = explode("\n", $buffer);
8471 -
8472 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8473 - $buffer = array_pop($lines);
8474 -
8475 - foreach ($lines as $line) {
8476 - if (trim($line) === '') {
8477 - continue;
8478 - }
8479 - if (strpos($line, 'data: ') !== 0) {
8480 - continue;
8481 - }
8482 -
8483 - $json_str = substr($line, 6);
8484 -
8485 - if (trim($json_str) === '[DONE]') {
8486 - echo "data: [DONE]\n\n";
8487 - flush();
8488 - continue;
8489 - }
8490 -
8491 - $json = json_decode(trim($json_str), true);
8492 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8493 - $content = $json['choices'][0]['delta']['content'];
8494 - $full_response .= $content;
8495 -
8496 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8497 - flush();
8498 - }
8499 - }
8500 -
8501 - return strlen($data);
8502 - });
8503 -
8504 - $response = curl_exec($ch);
8505 - $errno = curl_errno($ch);
8506 - $last_curl_error = curl_error($ch);
8507 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8508 - curl_close($ch);
8509 -
8510 - if (!$errno && $http_code === 200) {
8511 - break; // Happy path — WRITEFUNCTION already streamed everything.
8512 - }
8513 -
8514 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8515 - $can_retry = !$this->streaming_headers_sent
8516 - && ($attempt + 1) < $max_attempts
8517 - && $is_transient;
8518 -
8519 - if (defined('WP_DEBUG') && WP_DEBUG) {
8520 - error_log(sprintf(
8521 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8522 - $attempt + 1, $max_attempts, $http_code, $errno,
8523 - $is_transient ? 'yes' : 'no',
8524 - $can_retry ? 'Retrying.' : 'Giving up.'
8525 - ));
8526 - }
8527 -
8528 - if (!$can_retry) {
8529 - break;
8530 - }
8531 - }
8532 -
8533 - // Post-loop branch.
8534 - if (!$errno && $http_code === 200) {
8535 - // Happy path — save the complete response to maintain chat persistence.
8536 - if (!empty($full_response) && !empty($session_id)) {
8537 - $rag_context_for_storage = null;
8538 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8539 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8540 -
8541 - if ($has_rag_data || $has_action_data) {
8542 - $rag_context_for_storage = [];
8543 -
8544 - if ($has_rag_data) {
8545 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8546 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8547 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8548 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8549 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8550 - }
8551 -
8552 - if ($has_action_data) {
8553 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8554 - }
8555 - }
8556 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8557 - }
8558 -
8559 - return true;
8560 - }
8561 -
8562 - // Failure path — branch on whether SSE channel was opened.
8563 - return $this->mxchat_stream_emit_fallback(
8564 - 'openai',
8565 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8566 - $session_id,
8567 - $testing_data
8568 - );
8569 -
8570 - } catch (Exception $e) {
8571 - return $this->mxchat_stream_emit_fallback(
8572 - 'openai',
8573 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8574 - $session_id,
8575 - $testing_data
8576 - );
8577 - }
8578 -}
8579 -
8580 -/**
8581 - * Shared fallback emitter for streaming chat functions. Two outcomes:
8582 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
8583 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
8584 - * normal bot bubble. Transcript row is persisted.
8585 - * - streaming_headers_sent === false: SSE channel never opened (retries
8586 - * exhausted on initial connect). Emit a clean JSON response — the path
8587 - * the widget would normally hit if streaming wasn't even attempted.
8588 - *
8589 - * Used by all six *_stream functions after their per-attempt retry loop.
8590 - */
8591 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
8592 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
8593 -
8594 - if ($this->streaming_headers_sent) {
8595 - if ($is_error_array) {
8596 - echo "data: " . json_encode([
8597 - 'error' => true,
8598 - 'error_message' => $regular_response['error'],
8599 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8600 - 'text' => $regular_response['error'],
8601 - 'message' => $regular_response['error']
8602 - ]) . "\n\n";
8603 - echo "data: [DONE]\n\n";
8604 - flush();
8605 - return true;
8606 - }
8607 - $fallback_message = (string) $regular_response;
8608 - if (!empty($fallback_message) && !empty($session_id)) {
8609 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8610 - }
8611 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
8612 - echo "data: [DONE]\n\n";
8613 - flush();
8614 - return true;
8615 - }
8616 -
8617 - // SSE channel never opened — clean JSON fallback.
8618 - if ($is_error_array) {
8619 - header('Content-Type: application/json');
8620 - echo json_encode(array(
8621 - 'error' => true,
8622 - 'error_message' => $regular_response['error'],
8623 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8624 - 'text' => $regular_response['error'],
8625 - 'message' => $regular_response['error'],
8626 - ));
8627 - return true;
8628 - }
8629 -
8630 - $fallback_message = (string) $regular_response;
8631 - if (!empty($fallback_message) && !empty($session_id)) {
8632 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8633 - }
8634 - $response_data = array(
8635 - 'text' => $fallback_message,
8636 - 'html' => '',
8637 - 'session_id' => $session_id,
8638 - );
8639 - if ($testing_data !== null) {
8640 - $response_data['testing_data'] = $testing_data;
8641 - }
8642 - header('Content-Type: application/json');
8643 - echo json_encode($response_data);
8644 - return true;
8645 -}
8646 -
8647 -/**
8648 - * Resolve custom (OpenAI-compatible) provider config from settings.
8649 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
8650 - */
8651 -private function mxchat_resolve_custom_provider() {
8652 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
8653 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
8654 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
8655 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
8656 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
8657 -
8658 - $chat_url = $base_url . '/chat/completions';
8659 - if (!empty($api_version)) {
8660 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
8661 - }
8662 -
8663 - $headers = array('Content-Type: application/json');
8664 - if (!empty($api_key)) {
8665 - if ($auth_scheme === 'api-key') {
8666 - $headers[] = 'api-key: ' . $api_key;
8667 - } else {
8668 - $headers[] = 'Authorization: Bearer ' . $api_key;
8669 - }
8670 - }
8671 -
8672 - return array(
8673 - 'base_url' => $base_url,
8674 - 'api_key' => $api_key,
8675 - 'model' => $model !== '' ? $model : 'default',
8676 - 'auth_scheme' => $auth_scheme,
8677 - 'api_version' => $api_version,
8678 - 'chat_url' => $chat_url,
8679 - 'headers' => $headers,
8680 - );
8681 -}
8682 -
8683 -/**
8684 - * Streaming chat completion against an OpenAI-compatible custom provider
8685 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
8686 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
8687 - */
8688 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8689 - try {
8690 - $cfg = $this->mxchat_resolve_custom_provider();
8691 - if (empty($cfg['base_url'])) {
8692 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8693 - }
8694 -
8695 - $bot_id = $this->get_current_bot_id($session_id);
8696 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8697 - if (!is_array($conversation_history)) {
8698 - $conversation_history = array();
8699 - }
8700 -
8701 - $formatted_conversation = array();
8702 - $formatted_conversation[] = array(
8703 - 'role' => 'system',
8704 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8705 - );
8706 - foreach ($conversation_history as $message) {
8707 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8708 - $role = $message['role'];
8709 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8710 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8711 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
8712 - }
8713 - }
8714 -
8715 - if (headers_sent() || !function_exists('curl_init')) {
8716 - // No streaming capability — fall through to non-stream wrapper
8717 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
8718 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
8719 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
8720 - }
8721 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
8722 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
8723 - header('Content-Type: application/json');
8724 - echo json_encode($response_data);
8725 - return true;
8726 - }
8727 -
8728 - $request_body = array(
8729 - 'model' => $cfg['model'],
8730 - 'messages' => $formatted_conversation,
8731 - 'stream' => true,
8732 - );
8733 - $body = json_encode($request_body);
8734 -
8735 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8736 -
8737 - $captured_status_code = 0;
8738 - $captured_body_pre_stream = '';
8739 - $full_response = '';
8740 - $stream_started = false;
8741 - $buffer = '';
8742 - $errno = 0;
8743 - $http_code = 0;
8744 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8745 - $backoff_ms = array(0, 750, 2000);
8746 -
8747 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8748 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8749 - usleep($backoff_ms[$attempt] * 1000);
8750 - }
8751 -
8752 - $captured_status_code = 0;
8753 - $captured_body_pre_stream = '';
8754 - $full_response = '';
8755 - $stream_started = false;
8756 - $buffer = '';
8757 -
8758 - $ch = curl_init();
8759 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8760 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8761 - curl_setopt($ch, CURLOPT_POST, true);
8762 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8763 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8764 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8765 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8766 -
8767 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8768 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8769 - $captured_status_code = (int) $m[1];
8770 - }
8771 - return strlen($header);
8772 - });
8773 -
8774 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8775 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8776 - $captured_body_pre_stream .= $data;
8777 - return strlen($data);
8778 - }
8779 -
8780 - if (!$this->streaming_headers_sent) {
8781 - $this->setup_streaming_headers();
8782 - }
8783 -
8784 - if (!$stream_started && $testing_data !== null) {
8785 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8786 - flush();
8787 - $stream_started = true;
8788 - }
8789 - $buffer .= $data;
8790 - $lines = explode("\n", $buffer);
8791 - $buffer = array_pop($lines);
8792 - foreach ($lines as $line) {
8793 - if (trim($line) === '') { continue; }
8794 - if (strpos($line, 'data: ') !== 0) { continue; }
8795 - $json_str = substr($line, 6);
8796 - if (trim($json_str) === '[DONE]') {
8797 - echo "data: [DONE]\n\n";
8798 - flush();
8799 - continue;
8800 - }
8801 - $json = json_decode(trim($json_str), true);
8802 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8803 - $content = $json['choices'][0]['delta']['content'];
8804 - $full_response .= $content;
8805 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8806 - flush();
8807 - }
8808 - }
8809 - return strlen($data);
8810 - });
8811 -
8812 - $response = curl_exec($ch);
8813 - $errno = curl_errno($ch);
8814 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8815 - curl_close($ch);
8816 -
8817 - if (!$errno && $http_code === 200) {
8818 - break;
8819 - }
8820 -
8821 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8822 - $can_retry = !$this->streaming_headers_sent
8823 - && ($attempt + 1) < $max_attempts
8824 - && $is_transient;
8825 -
8826 - if (defined('WP_DEBUG') && WP_DEBUG) {
8827 - error_log(sprintf(
8828 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8829 - $attempt + 1, $max_attempts, $http_code, $errno,
8830 - $is_transient ? 'yes' : 'no',
8831 - $can_retry ? 'Retrying.' : 'Giving up.'
8832 - ));
8833 - }
8834 -
8835 - if (!$can_retry) {
8836 - break;
8837 - }
8838 - }
8839 -
8840 - if (!$errno && $http_code === 200) {
8841 - if (!empty($full_response) && !empty($session_id)) {
8842 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8843 - }
8844 - return true;
8845 - }
8846 -
8847 - return $this->mxchat_stream_emit_fallback(
8848 - 'openai',
8849 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8850 - $session_id,
8851 - $testing_data
8852 - );
8853 -
8854 - } catch (Exception $e) {
8855 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8856 - }
8857 -}
8858 -
8859 -/**
8860 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8861 - * Returns string content on success, array['error'=>...] on failure.
8862 - */
8863 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8864 - $cfg = $this->mxchat_resolve_custom_provider();
8865 - if (empty($cfg['base_url'])) {
8866 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8867 - }
8868 -
8869 - $bot_id = $this->get_current_bot_id(null);
8870 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8871 - if (!is_array($conversation_history)) {
8872 - $conversation_history = array();
8873 - }
8874 -
8875 - $messages = array(array(
8876 - 'role' => 'system',
8877 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8878 - ));
8879 - foreach ($conversation_history as $message) {
8880 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8881 - $role = $message['role'];
8882 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8883 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8884 - $messages[] = array('role' => $role, 'content' => $message['content']);
8885 - }
8886 - }
8887 -
8888 - $headers_assoc = array('Content-Type' => 'application/json');
8889 - if (!empty($cfg['api_key'])) {
8890 - if ($cfg['auth_scheme'] === 'api-key') {
8891 - $headers_assoc['api-key'] = $cfg['api_key'];
8892 - } else {
8893 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8894 - }
8895 - }
8896 -
8897 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8898 - 'headers' => $headers_assoc,
8899 - 'body' => wp_json_encode(array(
8900 - 'model' => $cfg['model'],
8901 - 'messages' => $messages,
8902 - )),
8903 - 'timeout' => 120,
8904 - ), 'openai');
8905 -
8906 - if (is_wp_error($response)) {
8907 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8908 - }
8909 - $code = (int) wp_remote_retrieve_response_code($response);
8910 - if ($code < 200 || $code >= 300) {
8911 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8912 - }
8913 - $body = json_decode(wp_remote_retrieve_body($response), true);
8914 - if (isset($body['choices'][0]['message']['content'])) {
8915 - return (string) $body['choices'][0]['message']['content'];
8916 - }
8917 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8918 -}
8919 -
8920 -/**
8921 - * Generate response using OpenAI Responses API with web search tool
8922 - * This uses the newer Responses API which supports web search functionality
8923 - */
8924 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8925 - try {
8926 - $bot_id = $this->get_current_bot_id($session_id);
8927 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8928 -
8929 - if (!is_array($conversation_history)) {
8930 - $conversation_history = array();
8931 - }
8932 -
8933 - // Build the input for Responses API
8934 - // The Responses API uses a different format - we need to construct the input properly
8935 - $input_parts = [];
8936 -
8937 - // Add system instructions as context
8938 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8939 -
8940 - // Build conversation as input items for Responses API
8941 - foreach ($conversation_history as $message) {
8942 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8943 - $role = $message['role'];
3348 + // Convert roles to supported format
8944 3349 if ($role === 'bot' || $role === 'agent') {
8945 3350 $role = 'assistant';
8946 3351 }
8947 - if (!in_array($role, ['assistant', 'user'])) {
8948 - $role = 'user';
8949 - }
8950 - $input_parts[] = [
8951 - 'type' => 'message',
8952 - 'role' => $role,
8953 - 'content' => $message['content']
8954 - ];
8955 - }
8956 - }
8957 -
8958 - // Build request body for Responses API
8959 - $request_body = [
8960 - 'model' => $selected_model,
8961 - 'input' => $input_parts,
8962 - 'instructions' => $system_context,
8963 - 'stream' => $streaming
8964 - ];
8965 -
8966 - // Only add web search tool if web search is enabled in settings
8967 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8968 - if ($web_search_enabled) {
8969 - $request_body['tools'] = [
8970 - ['type' => 'web_search']
8971 - ];
8972 - }
8973 -
8974 - // Add reasoning effort for supported models
8975 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8976 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8977 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8978 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8979 - $request_body['reasoning'] = ['effort' => 'low'];
8980 - } elseif ($selected_model === 'gpt-5.5') {
8981 - $request_body['reasoning'] = ['effort' => 'low'];
8982 - } elseif ($selected_model === 'gpt-5.4') {
8983 - $request_body['reasoning'] = ['effort' => 'low'];
8984 - }
8985 - }
8986 -
8987 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8988 -
8989 - if ($streaming) {
8990 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8991 - } else {
8992 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8993 - }
8994 -
8995 - } catch (Exception $e) {
8996 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8997 - return [
8998 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8999 - 'error_code' => 'web_search_exception'
9000 - ];
9001 - }
9002 -}
9003 -
9004 -/**
9005 - * Handle non-streaming web search response
9006 - */
9007 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9008 - $request_body['stream'] = false;
9009 -
9010 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
9011 - 'headers' => array(
9012 - 'Authorization' => 'Bearer ' . $api_key,
9013 - 'Content-Type' => 'application/json'
9014 - ),
9015 - 'body' => json_encode($request_body),
9016 - 'timeout' => 90
9017 - ), 'openai');
9018 -
9019 - if (is_wp_error($response)) {
9020 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
9021 - return [
9022 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
9023 - 'error_code' => 'web_search_connection_error'
9024 - ];
9025 - }
9026 -
9027 - $response_code = wp_remote_retrieve_response_code($response);
9028 - $response_body = wp_remote_retrieve_body($response);
9029 -
9030 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
9031 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
9032 -
9033 - if ($response_code !== 200) {
9034 - $error_data = json_decode($response_body, true);
9035 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
9036 - return [
9037 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
9038 - 'error_code' => 'web_search_api_error'
9039 - ];
9040 - }
9041 -
9042 - $result = json_decode($response_body, true);
9043 -
9044 - if (json_last_error() !== JSON_ERROR_NONE) {
9045 - return [
9046 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
9047 - 'error_code' => 'web_search_json_error'
9048 - ];
9049 - }
9050 -
9051 - // Extract the response text and citations from Responses API format
9052 - $output_text = '';
9053 - $citations = [];
9054 -
9055 - if (isset($result['output'])) {
9056 - foreach ($result['output'] as $output_item) {
9057 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
9058 - foreach ($output_item['content'] as $content_item) {
9059 - if ($content_item['type'] === 'output_text') {
9060 - $output_text .= $content_item['text'];
9061 -
9062 - // Extract citations/annotations
9063 - if (isset($content_item['annotations'])) {
9064 - foreach ($content_item['annotations'] as $annotation) {
9065 - if ($annotation['type'] === 'url_citation') {
9066 - $citations[] = [
9067 - 'url' => $annotation['url'],
9068 - 'title' => $annotation['title'] ?? ''
9069 - ];
9070 - }
9071 - }
9072 - }
9073 - }
9074 - }
9075 - }
9076 - }
9077 - }
9078 -
9079 - // If we have citations, append them to the response
9080 - if (!empty($citations)) {
9081 - $output_text .= "\n\n**Sources:**\n";
9082 - $seen_urls = [];
9083 - foreach ($citations as $citation) {
9084 - if (!in_array($citation['url'], $seen_urls)) {
9085 - $seen_urls[] = $citation['url'];
9086 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9087 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9088 - }
9089 - }
9090 - }
9091 -
9092 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
9093 - // which includes rag_context for the "sources" link in transcripts.
9094 -
9095 - return $output_text;
9096 -}
9097 -
9098 -/**
9099 - * Handle streaming web search response using Responses API
9100 - */
9101 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9102 - $request_body['stream'] = true;
9103 -
9104 - // Check if we can stream
9105 - if (headers_sent() || !function_exists('curl_init')) {
9106 - // Fallback to non-streaming
9107 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
9108 - }
9109 -
9110 - // Setup streaming headers
9111 - $this->setup_streaming_headers();
9112 -
9113 - $ch = curl_init();
9114 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
9115 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9116 - curl_setopt($ch, CURLOPT_POST, true);
9117 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
9118 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9119 - 'Content-Type: application/json',
9120 - 'Authorization: Bearer ' . $api_key
9121 - ));
9122 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9123 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
9124 -
9125 - $full_response = '';
9126 - $stream_started = false;
9127 - $buffer = '';
9128 - $citations = [];
9129 -
9130 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
9131 - // Send testing data as first event if available
9132 - if (!$stream_started && $testing_data !== null) {
9133 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9134 - flush();
9135 - $stream_started = true;
9136 - }
9137 -
9138 - $buffer .= $data;
9139 - $lines = explode("\n", $buffer);
9140 - $buffer = array_pop($lines);
9141 -
9142 - foreach ($lines as $line) {
9143 - if (trim($line) === '') continue;
9144 - if (strpos($line, 'data: ') !== 0) continue;
9145 -
9146 - $json_str = substr($line, 6);
9147 -
9148 - if (trim($json_str) === '[DONE]') {
9149 - // Append citations if we have any
9150 - if (!empty($citations)) {
9151 - $citation_text = "\n\n**Sources:**\n";
9152 - $seen_urls = [];
9153 - foreach ($citations as $citation) {
9154 - if (!in_array($citation['url'], $seen_urls)) {
9155 - $seen_urls[] = $citation['url'];
9156 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9157 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9158 - }
9159 - }
9160 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
9161 - $full_response .= $citation_text;
9162 - flush();
9163 - }
9164 - echo "data: [DONE]\n\n";
9165 - flush();
9166 - continue;
9167 - }
9168 -
9169 - $json = json_decode(trim($json_str), true);
9170 - if (!$json) continue;
9171 -
9172 - // Handle Responses API streaming events
9173 - // The format is different from Chat Completions
9174 - if (isset($json['type'])) {
9175 - switch ($json['type']) {
9176 - case 'response.output_text.delta':
9177 - // Text content delta
9178 - if (isset($json['delta'])) {
9179 - $content = $json['delta'];
9180 - $full_response .= $content;
9181 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9182 - flush();
9183 - }
9184 - break;
9185 -
9186 - case 'response.output_item.done':
9187 - // Check for citations in completed items
9188 - if (isset($json['item']['content'])) {
9189 - foreach ($json['item']['content'] as $content_item) {
9190 - if (isset($content_item['annotations'])) {
9191 - foreach ($content_item['annotations'] as $annotation) {
9192 - if ($annotation['type'] === 'url_citation') {
9193 - $citations[] = [
9194 - 'url' => $annotation['url'],
9195 - 'title' => $annotation['title'] ?? ''
9196 - ];
9197 - }
9198 - }
9199 - }
9200 - }
9201 - }
9202 - break;
9203 - }
9204 - }
9205 - }
9206 -
9207 - return strlen($data);
9208 - });
9209 -
9210 - $response = curl_exec($ch);
9211 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9212 -
9213 - if (curl_errno($ch) || $http_code !== 200) {
9214 - $curl_error = curl_error($ch);
9215 - curl_close($ch);
9216 -
9217 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
9218 -
9219 - return $this->mxchat_stream_emit_fallback(
9220 - 'web_search',
9221 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
9222 - $session_id,
9223 - $testing_data
9224 - );
9225 - }
9226 -
9227 - curl_close($ch);
9228 -
9229 - // Save the complete response with RAG context so the "sources" link
9230 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
9231 - if (!empty($full_response) && !empty($session_id)) {
9232 - $rag_context_for_storage = null;
9233 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9234 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9235 -
9236 - if ($has_rag_data || $has_action_data) {
9237 - $rag_context_for_storage = [];
9238 -
9239 - if ($has_rag_data) {
9240 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9241 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9242 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9243 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9244 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9245 - }
9246 -
9247 - if ($has_action_data) {
9248 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9249 - }
9250 - }
9251 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9252 - }
9253 -
9254 - return true;
9255 -}
9256 -
9257 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9258 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
9259 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
9260 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
9261 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
9262 - try {
9263 - // Get bot ID from session or request
9264 - $bot_id = $this->get_current_bot_id($session_id);
9265 -
9266 - // Get system prompt instructions using centralized function
9267 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9268 - // Ensure conversation_history is an array
9269 - if (!is_array($conversation_history)) {
9270 - $conversation_history = array();
9271 - }
9272 -
9273 - // Clean and validate conversation history
9274 - foreach ($conversation_history as &$message) {
9275 - // Convert bot and agent roles to assistant
9276 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
9277 - $message['role'] = 'assistant';
9278 - }
9279 -
9280 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
9281 - if (!in_array($message['role'], ['assistant', 'user'])) {
9282 - $message['role'] = 'user';
9283 - }
9284 -
9285 - // Ensure content field exists
9286 - if (!isset($message['content']) || empty($message['content'])) {
9287 - $message['content'] = '';
9288 - }
9289 -
9290 - // Remove any unsupported fields
9291 - $message = array_intersect_key($message, array_flip(['role', 'content']));
9292 - }
9293 -
9294 - // Add relevant content as the latest user message
9295 - $conversation_history[] = [
9296 - 'role' => 'user',
9297 - 'content' => $relevant_content
9298 - ];
9299 -
9300 - // Prepare the request body with stream: true
9301 - $payload = [
9302 - 'model' => $selected_model,
9303 - 'messages' => $conversation_history,
9304 - 'max_tokens' => 1000,
9305 - 'temperature' => 0.8,
9306 - 'system' => $system_prompt_instructions,
9307 - 'stream' => true
9308 - ];
9309 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9310 - $body = json_encode($payload);
9311 -
9312 - // Check if we can actually stream (headers not sent, etc.)
9313 - if (headers_sent() || !function_exists('curl_init')) {
9314 - // Fallback to regular response with testing data
9315 - //error_log("MxChat: Streaming not possible, falling back to regular response");
9316 - $regular_response = $this->mxchat_generate_response_claude(
9317 - $selected_model,
9318 - $claude_api_key,
9319 - array_slice($conversation_history, 0, -1), // Remove the added content
9320 - $relevant_content,
9321 - $session_id
9322 - );
9323 -
9324 - // Save bot response to transcript
9325 - if (!empty($regular_response) && !empty($session_id)) {
9326 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9327 - }
9328 -
9329 - // Return as JSON with testing data
9330 - $response_data = [
9331 - 'text' => $regular_response,
9332 - 'html' => '',
9333 - 'session_id' => $session_id
9334 - ];
9335 -
9336 - if ($testing_data !== null) {
9337 - $response_data['testing_data'] = $testing_data;
9338 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
9339 - }
9340 -
9341 - // Clear any streaming headers and send JSON
9342 - if (headers_sent() === false) {
9343 - header('Content-Type: application/json');
9344 - }
9345 - echo json_encode($response_data);
9346 - return true; // Indicate we handled the response
9347 - }
9348 -
9349 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9350 -
9351 - $captured_status_code = 0;
9352 - $captured_body_pre_stream = '';
9353 - $full_response = '';
9354 - $stream_started = false;
9355 - $buffer = '';
9356 - $errno = 0;
9357 - $http_code = 0;
9358 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9359 - $backoff_ms = array(0, 750, 2000);
9360 -
9361 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9362 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9363 - usleep($backoff_ms[$attempt] * 1000);
9364 - }
9365 -
9366 - $captured_status_code = 0;
9367 - $captured_body_pre_stream = '';
9368 - $full_response = '';
9369 - $stream_started = false;
9370 - $buffer = '';
9371 -
9372 - $ch = curl_init();
9373 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
9374 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9375 - curl_setopt($ch, CURLOPT_POST, true);
9376 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9377 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9378 - 'Content-Type: application/json',
9379 - 'x-api-key: ' . $claude_api_key,
9380 - 'anthropic-version: 2023-06-01'
9381 - ));
9382 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9383 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9384 -
9385 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9386 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9387 - $captured_status_code = (int) $m[1];
9388 - }
9389 - return strlen($header);
9390 - });
9391 -
9392 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9393 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9394 - $captured_body_pre_stream .= $data;
9395 - return strlen($data);
9396 - }
9397 -
9398 - if (!$this->streaming_headers_sent) {
9399 - $this->setup_streaming_headers();
9400 - }
9401 -
9402 - if (!$stream_started && $testing_data !== null) {
9403 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9404 - flush();
9405 - $stream_started = true;
9406 - }
9407 -
9408 - $buffer .= $data;
9409 - $lines = explode("\n", $buffer);
9410 - $buffer = array_pop($lines);
9411 -
9412 - foreach ($lines as $line) {
9413 - if (trim($line) === '') {
9414 - continue;
9415 - }
9416 -
9417 - if (strpos($line, 'event: ') === 0) {
9418 - continue;
9419 - }
9420 -
9421 - if (strpos($line, 'data: ') === 0) {
9422 - $json_str = substr($line, 6);
9423 -
9424 - $json = json_decode(trim($json_str), true);
9425 - if (json_last_error() !== JSON_ERROR_NONE) {
9426 - continue;
9427 - }
9428 -
9429 - if (isset($json['type'])) {
9430 - switch ($json['type']) {
9431 - case 'content_block_delta':
9432 - if (isset($json['delta']['text'])) {
9433 - $content = $json['delta']['text'];
9434 - $full_response .= $content;
9435 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9436 - flush();
9437 - }
9438 - break;
9439 -
9440 - case 'message_stop':
9441 - echo "data: [DONE]\n\n";
9442 - flush();
9443 - break;
9444 -
9445 - case 'error':
9446 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
9447 - flush();
9448 - break;
9449 - }
9450 - }
9451 - }
9452 - }
9453 -
9454 - return strlen($data);
9455 - });
9456 -
9457 - $response = curl_exec($ch);
9458 - $errno = curl_errno($ch);
9459 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9460 - curl_close($ch);
9461 -
9462 - if (!$errno && $http_code === 200) {
9463 - break;
9464 - }
9465 -
9466 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
9467 - $can_retry = !$this->streaming_headers_sent
9468 - && ($attempt + 1) < $max_attempts
9469 - && $is_transient;
9470 -
9471 - if (defined('WP_DEBUG') && WP_DEBUG) {
9472 - error_log(sprintf(
9473 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9474 - $attempt + 1, $max_attempts, $http_code, $errno,
9475 - $is_transient ? 'yes' : 'no',
9476 - $can_retry ? 'Retrying.' : 'Giving up.'
9477 - ));
9478 - }
9479 -
9480 - if (!$can_retry) {
9481 - break;
9482 - }
9483 - }
9484 -
9485 - if ($errno || $http_code !== 200) {
9486 - return $this->mxchat_stream_emit_fallback(
9487 - 'anthropic',
9488 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content, $session_id),
9489 - $session_id,
9490 - $testing_data
9491 - );
9492 - }
9493 -
9494 - // Save the complete response to maintain chat persistence
9495 - if (!empty($full_response) && !empty($session_id)) {
9496 - // Prepare RAG context for streaming response
9497 - $rag_context_for_storage = null;
9498 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9499 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9500 -
9501 - if ($has_rag_data || $has_action_data) {
9502 - $rag_context_for_storage = [];
9503 -
9504 - if ($has_rag_data) {
9505 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9506 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9507 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9508 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9509 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9510 - }
9511 -
9512 - if ($has_action_data) {
9513 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9514 - }
9515 - }
9516 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9517 - }
9518 -
9519 - return true; // Indicate streaming completed successfully
9520 -
9521 - } catch (Exception $e) {
9522 - return $this->mxchat_stream_emit_fallback(
9523 - 'anthropic',
9524 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id),
9525 - $session_id,
9526 - $testing_data
9527 - );
9528 - }
9529 -}
9530 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9531 - try {
9532 - // Get bot ID from session or request
9533 - $bot_id = $this->get_current_bot_id($session_id);
9534 -
9535 - // Get system prompt instructions using centralized function
9536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9537 -
9538 - // Ensure conversation_history is an array
9539 - if (!is_array($conversation_history)) {
9540 - $conversation_history = array();
9541 - }
9542 -
9543 - // Format conversation history for X.AI (same as OpenAI format)
9544 - $formatted_conversation = array();
9545 -
9546 - $formatted_conversation[] = array(
9547 - 'role' => 'system',
9548 - 'content' => $system_prompt_instructions . " " . $relevant_content
9549 - );
9550 -
9551 - foreach ($conversation_history as $message) {
9552 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9553 - $role = $message['role'];
9554 - if ($role === 'bot' || $role === 'agent') {
9555 - $role = 'assistant';
9556 - }
9557 3352 if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9558 3353 $role = 'user';
9559 3354 }
9560 - $formatted_conversation[] = array(
9561 - 'role' => $role,
9562 - 'content' => $message['content']
9563 - );
9564 - }
9565 - }
9566 3355
9567 - // Check if we can actually stream
9568 - if (headers_sent() || !function_exists('curl_init')) {
9569 - // Fallback to regular response with testing data
9570 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
9571 - $regular_response = $this->mxchat_generate_response_xai(
9572 - $selected_model,
9573 - $xai_api_key,
9574 - $conversation_history,
9575 - $relevant_content,
9576 - $session_id
9577 - );
9578 -
9579 - // Save bot response to transcript
9580 - if (!empty($regular_response) && !empty($session_id)) {
9581 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9582 - }
9583 -
9584 - $response_data = [
9585 - 'text' => $regular_response,
9586 - 'html' => '',
9587 - 'session_id' => $session_id
9588 - ];
9589 -
9590 - if ($testing_data !== null) {
9591 - $response_data['testing_data'] = $testing_data;
9592 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
9593 - }
9594 -
9595 - header('Content-Type: application/json');
9596 - echo json_encode($response_data);
9597 - return true;
9598 - }
9599 -
9600 - // Prepare the request body with stream: true
9601 - $body = json_encode([
9602 - 'model' => $selected_model,
9603 - 'messages' => $formatted_conversation,
9604 - 'temperature' => 0.8,
9605 - 'stream' => true
9606 - ]);
9607 -
9608 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9609 -
9610 - $captured_status_code = 0;
9611 - $captured_body_pre_stream = '';
9612 - $full_response = '';
9613 - $stream_started = false;
9614 - $buffer = '';
9615 - $errno = 0;
9616 - $http_code = 0;
9617 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9618 - $backoff_ms = array(0, 750, 2000);
9619 -
9620 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9621 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9622 - usleep($backoff_ms[$attempt] * 1000);
9623 - }
9624 -
9625 - $captured_status_code = 0;
9626 - $captured_body_pre_stream = '';
9627 - $full_response = '';
9628 - $stream_started = false;
9629 - $buffer = '';
9630 -
9631 - $ch = curl_init();
9632 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
9633 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9634 - curl_setopt($ch, CURLOPT_POST, true);
9635 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9636 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9637 - 'Content-Type: application/json',
9638 - 'Authorization: Bearer ' . $xai_api_key
9639 - ));
9640 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9641 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9642 -
9643 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9644 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9645 - $captured_status_code = (int) $m[1];
9646 - }
9647 - return strlen($header);
9648 - });
9649 -
9650 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9651 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9652 - $captured_body_pre_stream .= $data;
9653 - return strlen($data);
9654 - }
9655 -
9656 - if (!$this->streaming_headers_sent) {
9657 - $this->setup_streaming_headers();
9658 - }
9659 -
9660 - if (!$stream_started && $testing_data !== null) {
9661 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9662 - flush();
9663 - $stream_started = true;
9664 - }
9665 -
9666 - $buffer .= $data;
9667 - $lines = explode("\n", $buffer);
9668 - $buffer = array_pop($lines);
9669 -
9670 - foreach ($lines as $line) {
9671 - if (trim($line) === '') {
9672 - continue;
9673 - }
9674 - if (strpos($line, 'data: ') !== 0) {
9675 - continue;
9676 - }
9677 -
9678 - $json_str = substr($line, 6);
9679 -
9680 - if (trim($json_str) === '[DONE]') {
9681 - echo "data: [DONE]\n\n";
9682 - flush();
9683 - continue;
9684 - }
9685 -
9686 - $json = json_decode(trim($json_str), true);
9687 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9688 - $content = $json['choices'][0]['delta']['content'];
9689 - $full_response .= $content;
9690 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9691 - flush();
9692 - }
9693 - }
9694 -
9695 - return strlen($data);
9696 - });
9697 -
9698 - $response = curl_exec($ch);
9699 - $errno = curl_errno($ch);
9700 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9701 - curl_close($ch);
9702 -
9703 - if (!$errno && $http_code === 200) {
9704 - break;
9705 - }
9706 -
9707 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
9708 - $can_retry = !$this->streaming_headers_sent
9709 - && ($attempt + 1) < $max_attempts
9710 - && $is_transient;
9711 -
9712 - if (defined('WP_DEBUG') && WP_DEBUG) {
9713 - error_log(sprintf(
9714 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9715 - $attempt + 1, $max_attempts, $http_code, $errno,
9716 - $is_transient ? 'yes' : 'no',
9717 - $can_retry ? 'Retrying.' : 'Giving up.'
9718 - ));
9719 - }
9720 -
9721 - if (!$can_retry) {
9722 - break;
9723 - }
9724 - }
9725 -
9726 - if ($errno || $http_code !== 200) {
9727 - return $this->mxchat_stream_emit_fallback(
9728 - 'xai',
9729 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id),
9730 - $session_id,
9731 - $testing_data
9732 - );
9733 - }
9734 -
9735 - // Save the complete response to maintain chat persistence
9736 - if (!empty($full_response) && !empty($session_id)) {
9737 - // Prepare RAG context for streaming response
9738 - $rag_context_for_storage = null;
9739 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9740 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9741 -
9742 - if ($has_rag_data || $has_action_data) {
9743 - $rag_context_for_storage = [];
9744 -
9745 - if ($has_rag_data) {
9746 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9747 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9748 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9749 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9750 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9751 - }
9752 -
9753 - if ($has_action_data) {
9754 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9755 - }
9756 - }
9757 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9758 - }
9759 -
9760 - return true; // Indicate streaming completed successfully
9761 -
9762 - } catch (Exception $e) {
9763 - return $this->mxchat_stream_emit_fallback(
9764 - 'xai',
9765 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9766 - $session_id,
9767 - $testing_data
9768 - );
9769 - }
9770 -}
9771 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9772 - try {
9773 - // Get bot ID from session or request
9774 - $bot_id = $this->get_current_bot_id($session_id);
9775 -
9776 - // Get system prompt instructions using centralized function
9777 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9778 -
9779 - // Ensure conversation_history is an array
9780 - if (!is_array($conversation_history)) {
9781 - $conversation_history = array();
9782 - }
9783 -
9784 - // Format conversation history for DeepSeek
9785 - $formatted_conversation = array();
9786 -
9787 - $formatted_conversation[] = array(
9788 - 'role' => 'system',
9789 - 'content' => $system_prompt_instructions . " " . $relevant_content
9790 - );
9791 -
9792 - foreach ($conversation_history as $message) {
9793 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9794 - $role = $message['role'];
9795 - if ($role === 'bot' || $role === 'agent') {
9796 - $role = 'assistant';
9797 - }
9798 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9799 - $role = 'user';
9800 - }
9801 3356 $formatted_conversation[] = array(
9802 3357 'role' => $role,
9803 3358 'content' => $message['content']
9804 3359 );
@@ -9804,261 +3359,20 @@
9804 3359 );
9805 3360 }
9806 3361 }
9807 3362
9808 - // Check if we can actually stream
9809 - if (headers_sent() || !function_exists('curl_init')) {
9810 - // Fallback to regular response with testing data
9811 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9812 - $regular_response = $this->mxchat_generate_response_deepseek(
9813 - $selected_model,
9814 - $deepseek_api_key,
9815 - $conversation_history,
9816 - $relevant_content,
9817 - $session_id
9818 - );
9819 -
9820 - // Save bot response to transcript
9821 - if (!empty($regular_response) && !empty($session_id)) {
9822 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9823 - }
9824 -
9825 - $response_data = [
9826 - 'text' => $regular_response,
9827 - 'html' => '',
9828 - 'session_id' => $session_id
9829 - ];
9830 -
9831 - if ($testing_data !== null) {
9832 - $response_data['testing_data'] = $testing_data;
9833 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
9834 - }
9835 -
9836 - header('Content-Type: application/json');
9837 - echo json_encode($response_data);
9838 - return true;
9839 - }
9840 -
9841 - // Prepare the request body with stream: true
9842 3363 $body = json_encode([
9843 3364 'model' => $selected_model,
9844 3365 'messages' => $formatted_conversation,
9845 3366 'temperature' => 0.8,
9846 - 'stream' => true
3367 + 'stream' => false
9847 3368 ]);
9848 3369
9849 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9850 -
9851 - $captured_status_code = 0;
9852 - $captured_body_pre_stream = '';
9853 - $full_response = '';
9854 - $stream_started = false;
9855 - $buffer = '';
9856 - $errno = 0;
9857 - $http_code = 0;
9858 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9859 - $backoff_ms = array(0, 750, 2000);
9860 -
9861 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9862 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9863 - usleep($backoff_ms[$attempt] * 1000);
9864 - }
9865 -
9866 - $captured_status_code = 0;
9867 - $captured_body_pre_stream = '';
9868 - $full_response = '';
9869 - $stream_started = false;
9870 - $buffer = '';
9871 -
9872 - $ch = curl_init();
9873 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9874 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9875 - curl_setopt($ch, CURLOPT_POST, true);
9876 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9877 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9878 - 'Content-Type: application/json',
9879 - 'Authorization: Bearer ' . $deepseek_api_key
9880 - ));
9881 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9882 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9883 -
9884 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9885 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9886 - $captured_status_code = (int) $m[1];
9887 - }
9888 - return strlen($header);
9889 - });
9890 -
9891 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9892 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9893 - $captured_body_pre_stream .= $data;
9894 - return strlen($data);
9895 - }
9896 -
9897 - if (!$this->streaming_headers_sent) {
9898 - $this->setup_streaming_headers();
9899 - }
9900 -
9901 - if (!$stream_started && $testing_data !== null) {
9902 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9903 - flush();
9904 - $stream_started = true;
9905 - }
9906 -
9907 - $buffer .= $data;
9908 - $lines = explode("\n", $buffer);
9909 - $buffer = array_pop($lines);
9910 -
9911 - foreach ($lines as $line) {
9912 - if (trim($line) === '') {
9913 - continue;
9914 - }
9915 - if (strpos($line, 'data: ') !== 0) {
9916 - continue;
9917 - }
9918 -
9919 - $json_str = substr($line, 6);
9920 -
9921 - if (trim($json_str) === '[DONE]') {
9922 - echo "data: [DONE]\n\n";
9923 - flush();
9924 - continue;
9925 - }
9926 -
9927 - $json = json_decode(trim($json_str), true);
9928 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9929 - $content = $json['choices'][0]['delta']['content'];
9930 - $full_response .= $content;
9931 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9932 - flush();
9933 - }
9934 - }
9935 -
9936 - return strlen($data);
9937 - });
9938 -
9939 - $response = curl_exec($ch);
9940 - $errno = curl_errno($ch);
9941 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9942 - curl_close($ch);
9943 -
9944 - if (!$errno && $http_code === 200) {
9945 - break;
9946 - }
9947 -
9948 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9949 - $can_retry = !$this->streaming_headers_sent
9950 - && ($attempt + 1) < $max_attempts
9951 - && $is_transient;
9952 -
9953 - if (defined('WP_DEBUG') && WP_DEBUG) {
9954 - error_log(sprintf(
9955 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9956 - $attempt + 1, $max_attempts, $http_code, $errno,
9957 - $is_transient ? 'yes' : 'no',
9958 - $can_retry ? 'Retrying.' : 'Giving up.'
9959 - ));
9960 - }
9961 -
9962 - if (!$can_retry) {
9963 - break;
9964 - }
9965 - }
9966 -
9967 - if ($errno || $http_code !== 200) {
9968 - return $this->mxchat_stream_emit_fallback(
9969 - 'openai',
9970 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id),
9971 - $session_id,
9972 - $testing_data
9973 - );
9974 - }
9975 -
9976 - // Save the complete response to maintain chat persistence
9977 - if (!empty($full_response) && !empty($session_id)) {
9978 - // Prepare RAG context for streaming response
9979 - $rag_context_for_storage = null;
9980 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9981 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9982 -
9983 - if ($has_rag_data || $has_action_data) {
9984 - $rag_context_for_storage = [];
9985 -
9986 - if ($has_rag_data) {
9987 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9988 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9989 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9990 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9991 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9992 - }
9993 -
9994 - if ($has_action_data) {
9995 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9996 - }
9997 - }
9998 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9999 - }
10000 -
10001 - return true; // Indicate streaming completed successfully
10002 -
10003 - } catch (Exception $e) {
10004 - return $this->mxchat_stream_emit_fallback(
10005 - 'openai',
10006 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
10007 - $session_id,
10008 - $testing_data
10009 - );
10010 - }
10011 -}
10012 -
10013 -
10014 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id = '') {
10015 - try {
10016 - if (!is_array($conversation_history)) {
10017 - $conversation_history = array();
10018 - }
10019 -
10020 - $bot_id = $this->get_current_bot_id($session_id);
10021 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10022 -
10023 - $formatted_conversation = array();
10024 -
10025 - $formatted_conversation[] = array(
10026 - 'role' => 'system',
10027 - 'content' => $system_prompt_instructions . " " . $relevant_content
10028 - );
10029 -
10030 - foreach ($conversation_history as $message) {
10031 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10032 - $role = $message['role'];
10033 -
10034 - if ($role === 'bot' || $role === 'agent') {
10035 - $role = 'assistant';
10036 - }
10037 - if (!in_array($role, ['system', 'assistant', 'user'])) {
10038 - $role = 'user';
10039 - }
10040 -
10041 - $formatted_conversation[] = array(
10042 - 'role' => $role,
10043 - 'content' => $message['content']
10044 - );
10045 - }
10046 - }
10047 -
10048 - $body = json_encode([
10049 - 'model' => $selected_model,
10050 - 'messages' => $formatted_conversation,
10051 - 'temperature' => 1,
10052 - ]);
10053 -
10054 3370 $args = [
10055 3371 'body' => $body,
10056 3372 'headers' => [
10057 3373 'Content-Type' => 'application/json',
10058 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
10059 - 'HTTP-Referer' => home_url(),
10060 - 'X-Title' => get_bloginfo('name'),
3374 + 'Authorization' => 'Bearer ' . $deepseek_api_key,
10061 3375 ],
10062 3376 'timeout' => 60,
10063 3377 'redirection' => 5,
10064 3378 'blocking' => true,
@@ -10065,16 +3379,17 @@
10065 3379 'httpversion' => '1.0',
10066 3380 'sslverify' => true,
10067 3381 ];
10068 3382
10069 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
3383 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
10070 3384
10071 3385 if (is_wp_error($response)) {
10072 3386 $error_message = $response->get_error_message();
3387 + //error_log('DeepSeek API Error: ' . $error_message);
10073 3388 return [
10074 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenRouter'),
10075 - 'error_code' => 'openrouter_connection_error',
10076 - 'provider' => 'openrouter'
3389 + 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
3390 + 'error_code' => 'deepseek_connection_error',
3391 + 'provider' => 'deepseek'
10077 3392 ];
10078 3393 }
10079 3394
10080 3395 $status_code = wp_remote_retrieve_response_code($response);
@@ -10080,17 +3395,69 @@
10080 3395 $status_code = wp_remote_retrieve_response_code($response);
10081 3396 if ($status_code !== 200) {
10082 3397 $response_body = wp_remote_retrieve_body($response);
10083 3398 $decoded_response = json_decode($response_body, true);
10084 -
3399 +
10085 3400 $error_message = isset($decoded_response['error']['message'])
10086 3401 ? $decoded_response['error']['message']
10087 3402 : 'HTTP Error ' . $status_code;
10088 -
3403 +
3404 + $error_type = isset($decoded_response['error']['type'])
3405 + ? $decoded_response['error']['type']
3406 + : 'unknown';
3407 +
3408 + //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
3409 +
3410 + // Handle specific error types
3411 + switch ($status_code) {
3412 + case 401:
3413 + return [
3414 + 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
3415 + 'error_code' => 'deepseek_auth_error',
3416 + 'provider' => 'deepseek'
3417 + ];
3418 +
3419 + case 400:
3420 + if (strpos($error_message, 'API key') !== false) {
3421 + return [
3422 + 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
3423 + 'error_code' => 'deepseek_invalid_api_key',
3424 + 'provider' => 'deepseek'
3425 + ];
3426 + }
3427 + break;
3428 +
3429 + case 429:
3430 + if (strpos($error_message, 'quota') !== false) {
3431 + return [
3432 + 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
3433 + 'error_code' => 'deepseek_quota_exceeded',
3434 + 'provider' => 'deepseek'
3435 + ];
3436 + } else {
3437 + return [
3438 + 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
3439 + 'error_code' => 'deepseek_rate_limit',
3440 + 'provider' => 'deepseek'
3441 + ];
3442 + }
3443 +
3444 + case 500:
3445 + case 502:
3446 + case 503:
3447 + case 504:
3448 + return [
3449 + 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
3450 + 'error_code' => 'deepseek_service_unavailable',
3451 + 'provider' => 'deepseek'
3452 + ];
3453 + }
3454 +
3455 + // Generic error fallback
10089 3456 return [
10090 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
10091 - 'error_code' => 'openrouter_api_error',
10092 - 'provider' => 'openrouter',
3457 + 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
3458 + 'error_code' => 'deepseek_api_error',
3459 + 'provider' => 'deepseek',
10093 3460 'status_code' => $status_code
10094 3461 ];
10095 3462 }
10096 3463
@@ -10099,199 +3466,26 @@
10099 3466
10100 3467 if (isset($decoded_response['choices'][0]['message']['content'])) {
10101 3468 return trim($decoded_response['choices'][0]['message']['content']);
10102 3469 } else {
3470 + //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10103 3471 return [
10104 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
10105 - 'error_code' => 'openrouter_response_format_error',
10106 - 'provider' => 'openrouter'
3472 + 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
3473 + 'error_code' => 'deepseek_response_format_error',
3474 + 'provider' => 'deepseek'
10107 3475 ];
10108 3476 }
10109 3477 } catch (Exception $e) {
3478 + //error_log('DeepSeek Exception: ' . $e->getMessage());
10110 3479 return [
10111 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
10112 - 'error_code' => 'openrouter_exception',
10113 - 'provider' => 'openrouter'
3480 + 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
3481 + 'error_code' => 'deepseek_exception',
3482 + 'provider' => 'deepseek'
10114 3483 ];
10115 3484 }
10116 3485 }
10117 3486
10118 -/**
10119 - * Build a chat-bubble-safe message for a non-200 provider (chat) error.
10120 - *
10121 - * Visitors must NEVER see raw API internals (model names, key/billing/quota
10122 - * text). Admins (manage_options) get an actionable hint — and, for the common
10123 - * "model not available on this key" case, a direct pointer to change the model
10124 - * (the site owner can fix it in one click). Anthropic returns model-access as a
10125 - * 4xx with a message like "Claude Fable 5 is not available. Please use Opus 4.8."
10126 - *
10127 - * Provider-agnostic by design (reusable for the xai/gemini/deepseek branches),
10128 - * but Anthropic is the confirmed, reproduced case wired up here (plan 1d3b0f).
10129 - *
10130 - * @param int $http_code HTTP status from the provider.
10131 - * @param string $error_message Raw provider error.message (may be empty).
10132 - * @param string $provider_label Human provider name, e.g. 'Anthropic'.
10133 - * @return string Message safe to render as a chat bubble.
10134 - */
10135 -private function mxchat_friendly_chat_error($http_code, $error_message, $provider_label = '') {
10136 - $raw = trim((string) $error_message);
10137 -
10138 - // Detect a model-access / availability problem the site owner can fix by
10139 - // choosing a different model. (Anthropic phrasing + the common API shapes.)
10140 - $low = strtolower($raw);
10141 - $is_model_access = (strpos($low, 'not available') !== false)
10142 - || (strpos($low, 'does not have access') !== false)
10143 - || (strpos($low, 'do not have access') !== false)
10144 - || (strpos($low, 'does not exist') !== false) // OpenAI: "model `x` does not exist or you do not have access"
10145 - || (strpos($low, 'model_not_found') !== false)
10146 - || (strpos($low, 'not_found_error') !== false)
10147 - || (strpos($low, 'model not found') !== false) // xAI
10148 - || (strpos($low, 'not found') !== false) // Gemini: "models/x is not found for API version ..."
10149 - || (strpos($low, 'permission_denied') !== false) // Gemini gated model
10150 - || (strpos($low, 'permission denied') !== false);
10151 -
10152 - if (current_user_can('manage_options')) {
10153 - if ($is_model_access) {
10154 - return $raw !== ''
10155 - ? sprintf(
10156 - /* translators: %s: raw provider error detail */
10157 - esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings. (Details: %s)', 'mxchat'),
10158 - $raw
10159 - )
10160 - : esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings.', 'mxchat');
10161 - }
10162 - return $raw !== ''
10163 - ? sprintf(
10164 - /* translators: 1: provider label, 2: raw provider error detail */
10165 - esc_html__('The AI provider (%1$s) returned an error: %2$s. Check your model and API key in MxChat → Settings.', 'mxchat'),
10166 - $provider_label !== '' ? $provider_label : esc_html__('AI', 'mxchat'),
10167 - $raw
10168 - )
10169 - : esc_html__('The AI provider returned an error. Check your model and API key in MxChat → Settings.', 'mxchat');
10170 - }
10171 -
10172 - // Visitors: friendly, generic, no internals leaked.
10173 - return esc_html__('Sorry, I\'m having trouble responding right now. Please try again in a moment.', 'mxchat');
10174 -}
10175 -
10176 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id = '') {
10177 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
10178 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
10179 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
10180 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
10181 -
10182 - // Get bot ID from session or request
10183 - $bot_id = $this->get_current_bot_id($session_id);
10184 -
10185 - // Get system prompt instructions using centralized function
10186 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10187 -
10188 - // Clean and validate conversation history
10189 - foreach ($conversation_history as &$message) {
10190 - // Convert bot and agent roles to assistant
10191 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
10192 - $message['role'] = 'assistant';
10193 - }
10194 -
10195 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
10196 - if (!in_array($message['role'], ['assistant', 'user'])) {
10197 - $message['role'] = 'user';
10198 - }
10199 -
10200 - // Ensure content field exists
10201 - if (!isset($message['content']) || empty($message['content'])) {
10202 - $message['content'] = '';
10203 - }
10204 -
10205 - // Remove any unsupported fields
10206 - $message = array_intersect_key($message, array_flip(['role', 'content']));
10207 - }
10208 -
10209 - // Add relevant content as the latest user message
10210 - $conversation_history[] = [
10211 - 'role' => 'user',
10212 - 'content' => $relevant_content
10213 - ];
10214 -
10215 - // Build request body
10216 - $payload = [
10217 - 'model' => $selected_model,
10218 - 'max_tokens' => 1000,
10219 - 'temperature' => 0.8,
10220 - 'messages' => $conversation_history,
10221 - 'system' => $system_prompt_instructions
10222 - ];
10223 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
10224 - $body = json_encode($payload);
10225 -
10226 - // Set up API request
10227 - $args = [
10228 - 'body' => $body,
10229 - 'headers' => [
10230 - 'Content-Type' => 'application/json',
10231 - 'x-api-key' => $claude_api_key,
10232 - 'anthropic-version' => '2023-06-01'
10233 - ],
10234 - 'timeout' => 60,
10235 - 'redirection' => 5,
10236 - 'blocking' => true,
10237 - 'httpversion' => '1.0',
10238 - 'sslverify' => true,
10239 - ];
10240 -
10241 - // Make API request
10242 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
10243 -
10244 - // Check for WordPress errors
10245 - if (is_wp_error($response)) {
10246 - //error_log("Claude API request error: " . $response->get_error_message());
10247 - return "Sorry, there was an error connecting to the API.";
10248 - }
10249 -
10250 - // Check HTTP response code
10251 - $http_code = wp_remote_retrieve_response_code($response);
10252 - if ($http_code !== 200) {
10253 - $error_body = wp_remote_retrieve_body($response);
10254 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
10255 -
10256 - // Try to extract error message from response
10257 - $error_data = json_decode($error_body, true);
10258 - $error_message = isset($error_data['error']['message']) ?
10259 - $error_data['error']['message'] :
10260 - "HTTP error " . $http_code;
10261 -
10262 - // Surface an admin-actionable message (and a model-change pointer for the
10263 - // model-access case) without leaking raw API internals to visitors. This
10264 - // is the single chokepoint for BOTH the non-streaming and streaming Claude
10265 - // paths (the stream's non-200 fallback re-enters this method). plan 1d3b0f.
10266 - return $this->mxchat_friendly_chat_error($http_code, $error_message, 'Anthropic');
10267 - }
10268 -
10269 - // Parse response
10270 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
10271 -
10272 - // Check for JSON decode errors
10273 - if (json_last_error() !== JSON_ERROR_NONE) {
10274 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
10275 - return "Sorry, there was an error processing the API response.";
10276 - }
10277 -
10278 - // Extract and validate response content. claude-fable-5 prepends a
10279 - // thinking block to content even with no thinking param — take the first
10280 - // TEXT block rather than content[0].
10281 - if (isset($response_body['content']) && is_array($response_body['content'])) {
10282 - foreach ($response_body['content'] as $block) {
10283 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
10284 - return trim($block['text']);
10285 - }
10286 - }
10287 - }
10288 -
10289 - // Log unexpected response format
10290 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
10291 - return "Sorry, I received an unexpected response format from the API.";
10292 -}
10293 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id = '') {
3487 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
10294 3488 try {
10295 3489 // Ensure conversation_history is an array
10296 3490 if (!is_array($conversation_history)) {
10297 3491 $conversation_history = array();
@@ -10296,16 +3490,11 @@
10296 3490 if (!is_array($conversation_history)) {
10297 3491 $conversation_history = array();
10298 3492 }
10299 3493
10300 - // Get bot ID from session or request. plan eb9c38: resolve the real bot
10301 - // from the session (was hardcoded '' → always default bot on multi-bot
10302 - // installs) and fix the undefined $session_id that fed get_system_instructions.
10303 - $bot_id = $this->get_current_bot_id($session_id);
3494 + // Get system prompt instructions from options
3495 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10304 3496
10305 - // Get system prompt instructions using centralized function
10306 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10307 -
10308 3497 // Create a new array for the formatted conversation
10309 3498 $formatted_conversation = array();
10310 3499
10311 3500 // Add system message first
@@ -10333,44 +3522,15 @@
10333 3522 );
10334 3523 }
10335 3524 }
10336 3525
10337 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
10338 - $is_gpt5_model = (
10339 - strpos($selected_model, 'gpt-5') === 0 ||
10340 - $selected_model === 'gpt-5.2' ||
10341 - $selected_model === 'gpt-5.1-2025-11-13' ||
10342 - $selected_model === 'gpt-5' ||
10343 - $selected_model === 'gpt-5-mini' ||
10344 - $selected_model === 'gpt-5-nano'
10345 - );
10346 -
10347 - // Build request body with optimal settings for fast responses
10348 - $request_body = [
3526 + $body = json_encode([
10349 3527 'model' => $selected_model,
10350 3528 'messages' => $formatted_conversation,
10351 - 'temperature' => 1,
3529 + 'temperature' => 0.8,
10352 3530 'stream' => false
10353 - ];
3531 + ]);
10354 3532
10355 - // Add reasoning_effort only for GPT-5 models that support it
10356 - // These chat models don't support reasoning_effort parameter
10357 - $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');
10358 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
10359 - // GPT-5.1 uses 'low' instead of 'minimal'
10360 - if ($selected_model === 'gpt-5.1-2025-11-13') {
10361 - $request_body['reasoning_effort'] = 'low';
10362 - } elseif ($selected_model === 'gpt-5.5') {
10363 - $request_body['reasoning_effort'] = 'none';
10364 - } elseif ($selected_model === 'gpt-5.4') {
10365 - $request_body['reasoning_effort'] = 'none';
10366 - } else {
10367 - $request_body['reasoning_effort'] = 'minimal';
10368 - }
10369 - }
10370 -
10371 - $body = json_encode($request_body);
10372 -
10373 3533 $args = [
10374 3534 'body' => $body,
10375 3535 'headers' => [
10376 3536 'Content-Type' => 'application/json',
@@ -10382,14 +3542,15 @@
10382 3542 'httpversion' => '1.0',
10383 3543 'sslverify' => true,
10384 3544 ];
10385 3545
10386 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
3546 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
10387 3547
10388 3548 if (is_wp_error($response)) {
10389 3549 $error_message = $response->get_error_message();
3550 + //error_log('OpenAI API Error: ' . $error_message);
10390 3551 return [
10391 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenAI'),
3552 + 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
10392 3553 'error_code' => 'openai_connection_error',
10393 3554 'provider' => 'openai'
10394 3555 ];
10395 3556 }
@@ -10406,8 +3567,10 @@
10406 3567 $error_type = isset($decoded_response['error']['type'])
10407 3568 ? $decoded_response['error']['type']
10408 3569 : 'unknown';
10409 3570
3571 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
3572 +
10410 3573 // Handle specific error types
10411 3574 switch ($error_type) {
10412 3575 case 'invalid_request_error':
10413 3576 if (strpos($error_message, 'API key') !== false) {
@@ -10440,13 +3603,11 @@
10440 3603 'provider' => 'openai'
10441 3604 ];
10442 3605 }
10443 3606
10444 - // Generic error fallback only — the typed cases above already produce
10445 - // clean messages. Route the raw-tail generic case through the leak-safe
10446 - // helper so visitors never see provider internals. plan 5da59a.
3607 + // Generic error fallback
10447 3608 return [
10448 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'OpenAI'),
3609 + 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
10449 3610 'error_code' => 'openai_api_error',
10450 3611 'provider' => 'openai',
10451 3612 'status_code' => $status_code
10452 3613 ];
@@ -10457,8 +3618,9 @@
10457 3618
10458 3619 if (isset($decoded_response['choices'][0]['message']['content'])) {
10459 3620 return trim($decoded_response['choices'][0]['message']['content']);
10460 3621 } else {
3622 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
10461 3623 return [
10462 3624 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
10463 3625 'error_code' => 'openai_response_format_error',
10464 3626 'provider' => 'openai'
@@ -10464,8 +3626,9 @@
10464 3626 'provider' => 'openai'
10465 3627 ];
10466 3628 }
10467 3629 } catch (Exception $e) {
3630 + //error_log('OpenAI Exception: ' . $e->getMessage());
10468 3631 return [
10469 3632 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
10470 3633 'error_code' => 'openai_exception',
10471 3634 'provider' => 'openai'
@@ -10471,17 +3634,13 @@
10471 3634 'provider' => 'openai'
10472 3635 ];
10473 3636 }
10474 3637 }
3638 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
3639 + try {
3640 + // Get system prompt instructions from options
3641 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10475 3642
10476 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id = '') {
10477 - try {
10478 - // Get bot ID from session or request
10479 - $bot_id = $this->get_current_bot_id($session_id);
10480 -
10481 - // Get system prompt instructions using centralized function
10482 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10483 -
10484 3643 // Add system prompt to relevant content
10485 3644 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10486 3645
10487 3646 // Prepend system instructions to the conversation history
@@ -10530,9 +3689,9 @@
10530 3689 'sslverify' => true,
10531 3690 ];
10532 3691
10533 3692 // Make the API request
10534 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
3693 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
10535 3694
10536 3695 // Process the response
10537 3696 if (is_wp_error($response)) {
10538 3697 $error_message = $response->get_error_message();
@@ -10537,9 +3696,9 @@
10537 3696 if (is_wp_error($response)) {
10538 3697 $error_message = $response->get_error_message();
10539 3698 //error_log('X.AI API Error: ' . $error_message);
10540 3699 return [
10541 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'X.AI'),
3700 + 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
10542 3701 'error_code' => 'xai_connection_error',
10543 3702 'provider' => 'xai'
10544 3703 ];
10545 3704 }
@@ -10633,14 +3792,11 @@
10633 3792 'provider' => 'xai'
10634 3793 ];
10635 3794 }
10636 3795
10637 - // Generic error fallback. Route the user-facing text through the
10638 - // leak-safe helper (admins get an actionable hint, visitors a generic
10639 - // fallback) instead of echoing raw provider internals. Preserve the
10640 - // structured contract (error_code/provider/status_code) for logging. plan 5da59a.
3796 + // Generic error fallback with the actual error message
10641 3797 return [
10642 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'xAI'),
3798 + 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
10643 3799 'error_code' => 'xai_api_error',
10644 3800 'provider' => 'xai',
10645 3801 'status_code' => $status_code
10646 3802 ];
@@ -10666,188 +3822,113 @@
10666 3822 'error_code' => 'xai_exception',
10667 3823 'provider' => 'xai'
10668 3824 ];
10669 3825 }
3826 +}
3827 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
3828 + // Get system prompt instructions from options
3829 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10670 3830
10671 -
10672 -}
10673 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id = '') {
10674 - try {
10675 - // Ensure conversation_history is an array
10676 - if (!is_array($conversation_history)) {
10677 - $conversation_history = array();
3831 + // Clean and validate conversation history
3832 + foreach ($conversation_history as &$message) {
3833 + // Convert bot and agent roles to assistant
3834 + if ($message['role'] === 'bot' || $message['role'] === 'agent') {
3835 + $message['role'] = 'assistant';
10678 3836 }
10679 -
10680 - // Get bot ID from session or request
10681 - $bot_id = $this->get_current_bot_id($session_id);
10682 3837
10683 - // Get system prompt instructions using centralized function
10684 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10685 -
10686 - // Create a new array for the formatted conversation
10687 - $formatted_conversation = array();
3838 + // Remove unsupported roles - Claude only supports 'assistant' and 'user'
3839 + if (!in_array($message['role'], ['assistant', 'user'])) {
3840 + $message['role'] = 'user';
3841 + }
10688 3842
10689 - // Add system message first
10690 - $formatted_conversation[] = array(
10691 - 'role' => 'system',
10692 - 'content' => $system_prompt_instructions . " " . $relevant_content
10693 - );
3843 + // Ensure content field exists
3844 + if (!isset($message['content']) || empty($message['content'])) {
3845 + $message['content'] = '';
3846 + }
10694 3847
10695 - // Add the rest of the conversation history
10696 - foreach ($conversation_history as $message) {
10697 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10698 - $role = $message['role'];
3848 + // Remove any unsupported fields
3849 + $message = array_intersect_key($message, array_flip(['role', 'content']));
3850 + }
10699 3851
10700 - // Convert roles to supported format
10701 - if ($role === 'bot' || $role === 'agent') {
10702 - $role = 'assistant';
10703 - }
10704 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10705 - $role = 'user';
10706 - }
3852 + // Add relevant content as the latest user message
3853 + $conversation_history[] = [
3854 + 'role' => 'user',
3855 + 'content' => $relevant_content
3856 + ];
10707 3857
10708 - $formatted_conversation[] = array(
10709 - 'role' => $role,
10710 - 'content' => $message['content']
10711 - );
10712 - }
10713 - }
3858 + // Build request body
3859 + $body = json_encode([
3860 + 'model' => $selected_model,
3861 + 'max_tokens' => 1000,
3862 + 'temperature' => 0.8,
3863 + 'messages' => $conversation_history,
3864 + 'system' => $system_prompt_instructions
3865 + ]);
10714 3866
10715 - $body = json_encode([
10716 - 'model' => $selected_model,
10717 - 'messages' => $formatted_conversation,
10718 - 'temperature' => 0.8,
10719 - 'stream' => false
10720 - ]);
10721 -
10722 - $args = [
10723 - 'body' => $body,
10724 - 'headers' => [
3867 + // Set up API request
3868 + $args = [
3869 + 'body' => $body,
3870 + 'headers' => [
10725 3871 'Content-Type' => 'application/json',
10726 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
3872 + 'x-api-key' => $claude_api_key,
3873 + 'anthropic-version' => '2023-06-01'
10727 3874 ],
10728 - 'timeout' => 60,
10729 - 'redirection' => 5,
10730 - 'blocking' => true,
10731 - 'httpversion' => '1.0',
10732 - 'sslverify' => true,
10733 - ];
3875 + 'timeout' => 60,
3876 + 'redirection' => 5,
3877 + 'blocking' => true,
3878 + 'httpversion' => '1.0',
3879 + 'sslverify' => true,
3880 + ];
10734 3881
10735 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
3882 + // Make API request
3883 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
10736 3884
10737 - if (is_wp_error($response)) {
10738 - $error_message = $response->get_error_message();
10739 - //error_log('DeepSeek API Error: ' . $error_message);
10740 - return [
10741 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'DeepSeek'),
10742 - 'error_code' => 'deepseek_connection_error',
10743 - 'provider' => 'deepseek'
10744 - ];
10745 - }
3885 + // Check for WordPress errors
3886 + if (is_wp_error($response)) {
3887 + //error_log("Claude API request error: " . $response->get_error_message());
3888 + return "Sorry, there was an error connecting to the API.";
3889 + }
10746 3890
10747 - $status_code = wp_remote_retrieve_response_code($response);
10748 - if ($status_code !== 200) {
10749 - $response_body = wp_remote_retrieve_body($response);
10750 - $decoded_response = json_decode($response_body, true);
3891 + // Check HTTP response code
3892 + $http_code = wp_remote_retrieve_response_code($response);
3893 + if ($http_code !== 200) {
3894 + $error_body = wp_remote_retrieve_body($response);
3895 + //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
3896 +
3897 + // Try to extract error message from response
3898 + $error_data = json_decode($error_body, true);
3899 + $error_message = isset($error_data['error']['message']) ?
3900 + $error_data['error']['message'] :
3901 + "HTTP error " . $http_code;
3902 +
3903 + return "Sorry, the API returned an error: " . $error_message;
3904 + }
10751 3905
10752 - $error_message = isset($decoded_response['error']['message'])
10753 - ? $decoded_response['error']['message']
10754 - : 'HTTP Error ' . $status_code;
10755 -
10756 - $error_type = isset($decoded_response['error']['type'])
10757 - ? $decoded_response['error']['type']
10758 - : 'unknown';
10759 -
10760 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
3906 + // Parse response
3907 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
3908 +
3909 + // Check for JSON decode errors
3910 + if (json_last_error() !== JSON_ERROR_NONE) {
3911 + //error_log("Claude API JSON decode error: " . json_last_error_msg());
3912 + return "Sorry, there was an error processing the API response.";
3913 + }
10761 3914
10762 - // Handle specific error types
10763 - switch ($status_code) {
10764 - case 401:
10765 - return [
10766 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
10767 - 'error_code' => 'deepseek_auth_error',
10768 - 'provider' => 'deepseek'
10769 - ];
10770 -
10771 - case 400:
10772 - if (strpos($error_message, 'API key') !== false) {
10773 - return [
10774 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
10775 - 'error_code' => 'deepseek_invalid_api_key',
10776 - 'provider' => 'deepseek'
10777 - ];
10778 - }
10779 - break;
10780 -
10781 - case 429:
10782 - if (strpos($error_message, 'quota') !== false) {
10783 - return [
10784 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
10785 - 'error_code' => 'deepseek_quota_exceeded',
10786 - 'provider' => 'deepseek'
10787 - ];
10788 - } else {
10789 - return [
10790 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
10791 - 'error_code' => 'deepseek_rate_limit',
10792 - 'provider' => 'deepseek'
10793 - ];
10794 - }
10795 -
10796 - case 500:
10797 - case 502:
10798 - case 503:
10799 - case 504:
10800 - return [
10801 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
10802 - 'error_code' => 'deepseek_service_unavailable',
10803 - 'provider' => 'deepseek'
10804 - ];
10805 - }
3915 + // Extract and validate response content
3916 + if (isset($response_body['content']) &&
3917 + is_array($response_body['content']) &&
3918 + !empty($response_body['content']) &&
3919 + isset($response_body['content'][0]['text'])) {
3920 + return trim($response_body['content'][0]['text']);
3921 + }
10806 3922
10807 - // Generic error fallback — leak-safe helper (see plan 5da59a / 1d3b0f).
10808 - return [
10809 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'DeepSeek'),
10810 - 'error_code' => 'deepseek_api_error',
10811 - 'provider' => 'deepseek',
10812 - 'status_code' => $status_code
10813 - ];
10814 - }
10815 -
10816 - $response_body = wp_remote_retrieve_body($response);
10817 - $decoded_response = json_decode($response_body, true);
10818 -
10819 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10820 - return trim($decoded_response['choices'][0]['message']['content']);
10821 - } else {
10822 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10823 - return [
10824 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
10825 - 'error_code' => 'deepseek_response_format_error',
10826 - 'provider' => 'deepseek'
10827 - ];
10828 - }
10829 - } catch (Exception $e) {
10830 - //error_log('DeepSeek Exception: ' . $e->getMessage());
10831 - return [
10832 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
10833 - 'error_code' => 'deepseek_exception',
10834 - 'provider' => 'deepseek'
10835 - ];
10836 - }
3923 + // Log unexpected response format
3924 + //error_log("Claude API unexpected response format: " . print_r($response_body, true));
3925 + return "Sorry, I received an unexpected response format from the API.";
10837 3926 }
10838 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content, $session_id = '') {
10839 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10840 - // Auto-rescue existing installs whose saved model is the dead ID.
10841 - if ($selected_model === 'gemini-3-pro-preview') {
10842 - $selected_model = 'gemini-3.1-pro-preview';
10843 - }
10844 - // Get bot ID from session or request
10845 - $bot_id = $this->get_current_bot_id($session_id);
10846 -
10847 - // Get system prompt instructions using centralized function
10848 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10849 -
3927 +private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
3928 + // Get system prompt instructions from options
3929 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3930 +
10850 3931 // Add system prompt to relevant content
10851 3932 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10852 3933
10853 3934 // Format messages for Gemini API
@@ -10912,24 +3993,10 @@
10912 3993 'parts' => $current_parts
10913 3994 ];
10914 3995 }
10915 3996
10916 - // Built-in Web Search grounding for Gemini (plan 46b9ea).
10917 - // The enable_web_search toggle historically routed ONLY to OpenAI's web_search
10918 - // tool; for a Gemini chat model it was a silent no-op. Gemini grounds natively
10919 - // (and free) via the Google Search tool, so when the toggle is on we attach it
10920 - // here on the PLAIN dispatch path. The function-calling loop (mxchat_fc_loop_gemini)
10921 - // is a SEPARATE path reached only when AI Tools are active, so grounding here
10922 - // never double-fires with function calling.
10923 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
10924 - // Gemini ids that do NOT support Google Search grounding (none today — every
10925 - // shipped chat model is 2.x/3.x and grounds natively). Kept as the explicit
10926 - // opt-out list mirroring the OpenAI $unsupported_web_search_models pattern.
10927 - $gemini_unsupported_grounding = array();
10928 - $grounding_active = $web_search_enabled && !in_array($selected_model, $gemini_unsupported_grounding, true);
10929 -
10930 3997 // Build the request body
10931 - $request_payload = [
3998 + $body = json_encode([
10932 3999 'contents' => $formatted_messages,
10933 4000 'generationConfig' => [
10934 4001 'temperature' => 0.7,
10935 4002 'topP' => 0.95,
@@ -10953,30 +4020,12 @@
10953 4020 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
10954 4021 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10955 4022 ]
10956 4023 ]
10957 - ];
10958 -
10959 - if ($grounding_active) {
10960 - // Gemini 1.5 used the older google_search_retrieval shape; 2.0+ uses the
10961 - // bare google_search tool. Branch by model family so a future 1.5 id still
10962 - // grounds (no 1.5 ships today, so this resolves to google_search). The empty
10963 - // tool config must serialize as a JSON object {}, not an array [].
10964 - if (strpos($selected_model, 'gemini-1.5') !== false) {
10965 - $request_payload['tools'] = [ ['google_search_retrieval' => new \stdClass()] ];
10966 - } else {
10967 - $request_payload['tools'] = [ ['google_search' => new \stdClass()] ];
10968 - }
10969 - }
10970 -
10971 - $body = json_encode($request_payload);
10972 -
4024 + ]);
4025 +
10973 4026 // Prepare the API endpoint
10974 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models.
10975 - // Grounding (the google_search tool) is a v1beta feature, so force v1beta whenever
10976 - // it's active — otherwise a stable model on v1 would silently drop the tool.
10977 - $api_version = ($grounding_active || strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10978 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
4027 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10979 4028
10980 4029 // Set up the API request
10981 4030 $args = [
10982 4031 'body' => $body,
@@ -10990,31 +4039,22 @@
10990 4039 'sslverify' => true,
10991 4040 ];
10992 4041
10993 4042 // Make the API request
10994 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10995 -
4043 + $response = wp_remote_post($api_endpoint, $args);
4044 +
10996 4045 // Process the response
10997 4046 if (is_wp_error($response)) {
10998 - // plan b13282: route the transport-error string through the leak-safe helper
10999 - // (admin-actionable, generic for visitors) instead of echoing the raw WP HTTP
11000 - // error. http_code 0 = no HTTP response, so the helper uses the generic branch.
11001 - return $this->mxchat_friendly_chat_error(0, $response->get_error_message(), 'Gemini');
4047 + return "Sorry, there was an error processing your request: " . $response->get_error_message();
11002 4048 }
11003 4049
11004 4050 $response_body = json_decode(wp_remote_retrieve_body($response), true);
11005 4051
11006 - // Handle potential errors in the response. Gemini surfaces errors as a
11007 - // 200/non-200 body with an `error` envelope; route the user-facing text
11008 - // through the leak-safe helper (admin-actionable, no visitor leak) rather
11009 - // than echoing the raw provider message. plan 5da59a.
4052 + // Handle potential errors in the response
11010 4053 if (isset($response_body['error'])) {
11011 4054 //error_log('Gemini API Error: ' . json_encode($response_body['error']));
11012 - $gemini_error_message = isset($response_body['error']['message'])
11013 - ? $response_body['error']['message']
11014 - : 'Unknown error';
11015 - $gemini_http_code = wp_remote_retrieve_response_code($response);
11016 - return $this->mxchat_friendly_chat_error($gemini_http_code, $gemini_error_message, 'Gemini');
4055 + return "Sorry, there was an error with the Gemini API: " .
4056 + (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
11017 4057 }
11018 4058
11019 4059 // Extract the response text
11020 4060 if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
@@ -11025,139 +4065,9 @@
11025 4065 }
11026 4066 }
11027 4067
11028 4068
11029 -public function test_streaming_request() {
11030 - $options = get_option('mxchat_options', []);
11031 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
11032 4069
11033 - // Detect provider from model prefix
11034 - $provider = strtolower(explode('-', $model)[0]);
11035 -
11036 - $sample_prompt = 'Hello! Can you stream this response back to me?';
11037 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
11038 - $headers = [];
11039 - $body = [];
11040 - $url = '';
11041 - $api_key = '';
11042 -
11043 - switch ($provider) {
11044 - case 'gpt':
11045 - case 'o1':
11046 - $api_key = $options['api_key'] ?? '';
11047 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
11048 - $url = 'https://api.openai.com/v1/chat/completions';
11049 - $headers = [
11050 - 'Content-Type: application/json',
11051 - 'Authorization: Bearer ' . $api_key
11052 - ];
11053 - $body = [
11054 - 'model' => $model,
11055 - 'messages' => $messages,
11056 - 'stream' => true
11057 - ];
11058 - break;
11059 -
11060 - case 'claude':
11061 - $api_key = $options['claude_api_key'] ?? '';
11062 - if (empty($api_key)) return '❌ Missing API key for Claude';
11063 - $url = 'https://api.anthropic.com/v1/messages';
11064 - $headers = [
11065 - 'Content-Type: application/json',
11066 - 'x-api-key: ' . $api_key,
11067 - 'anthropic-version: 2023-06-01'
11068 - ];
11069 - $body = [
11070 - 'model' => $model,
11071 - 'messages' => $messages,
11072 - 'max_tokens' => 100,
11073 - 'stream' => true
11074 - ];
11075 - break;
11076 -
11077 - case 'grok':
11078 - $api_key = $options['xai_api_key'] ?? '';
11079 - if (empty($api_key)) return '❌ Missing API key for X.AI';
11080 - $url = 'https://api.x.ai/v1/chat/completions';
11081 - $headers = [
11082 - 'Content-Type: application/json',
11083 - 'Authorization: Bearer ' . $api_key
11084 - ];
11085 - $body = [
11086 - 'model' => $model,
11087 - 'messages' => $messages,
11088 - 'stream' => true
11089 - ];
11090 - break;
11091 -
11092 - case 'deepseek':
11093 - if (empty($deepseek_api_key)) {
11094 - $error_response = [
11095 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
11096 - 'error_code' => 'missing_deepseek_api_key'
11097 - ];
11098 - if ($testing_data !== null) {
11099 - $error_response['testing_data'] = $testing_data;
11100 - }
11101 - return $error_response;
11102 - }
11103 - if ($streaming) {
11104 - return $this->mxchat_generate_response_deepseek_stream(
11105 - $selected_model,
11106 - $deepseek_api_key,
11107 - $conversation_history,
11108 - $relevant_content,
11109 - $session_id,
11110 - $testing_data // Pass testing data
11111 - );
11112 - } else {
11113 - $response = $this->mxchat_generate_response_deepseek(
11114 - $selected_model,
11115 - $deepseek_api_key,
11116 - $conversation_history,
11117 - $relevant_content,
11118 - $session_id
11119 - );
11120 - }
11121 - break;
11122 -
11123 - case 'gemini':
11124 - $api_key = $options['gemini_api_key'] ?? '';
11125 - if (empty($api_key)) return '❌ Missing API key for Gemini';
11126 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
11127 - $headers = ['Content-Type: application/json'];
11128 - $body = [
11129 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
11130 - 'generationConfig' => ['temperature' => 0.7]
11131 - ];
11132 - break;
11133 -
11134 - default:
11135 - return '❌ Unsupported provider: ' . $provider;
11136 - }
11137 -
11138 - // Do the actual streaming test
11139 - $ch = curl_init($url);
11140 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
11141 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
11142 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
11143 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
11144 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
11145 -
11146 - $response = curl_exec($ch);
11147 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
11148 - $error = curl_error($ch);
11149 - curl_close($ch);
11150 -
11151 - if ($error) return "❌ cURL error: $error";
11152 - if ($http_code !== 200) {
11153 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
11154 - return "❌ HTTP $http_code: $error_message";
11155 - }
11156 -
11157 - return true;
11158 -}
11159 -
11160 4070 public function mxchat_dismiss_pre_chat_message() {
11161 4071 // Get and sanitize the user identifier
11162 4072 $user_id = $this->mxchat_get_user_identifier();
11163 4073 $user_id = sanitize_key($user_id);
@@ -11211,63 +4121,40 @@
11211 4121
11212 4122 return $dotProduct / ($normA * $normB);
11213 4123 }
11214 4124
4125 +public function mxchat_enqueue_scripts_styles() {
4126 + // Define version numbers for the styles and scripts
4127 + $chat_style_version = '2.1.7';
4128 + $chat_script_version = '2.1.7';
11215 4129
11216 -public function mxchat_enqueue_scripts_styles() {
11217 - // Fetch options from the database first to check loading strategy
11218 - $this->options = get_option('mxchat_options');
11219 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
4130 + // Enqueue the script
4131 + wp_enqueue_script(
4132 + 'mxchat-chat-js',
4133 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
4134 + array('jquery'),
4135 + $chat_script_version,
4136 + true
4137 + );
11220 4138
11221 - // Always enqueue CSS immediately
4139 + // Enqueue the CSS
11222 4140 wp_enqueue_style(
11223 4141 'mxchat-chat-css',
11224 4142 plugin_dir_url(__FILE__) . '../css/chat-style.css',
11225 4143 array(),
11226 - MXCHAT_VERSION
4144 + $chat_style_version
11227 4145 );
11228 4146
11229 - // Handle script loading based on strategy
11230 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11231 - // Enqueue the script normally
11232 - wp_enqueue_script(
11233 - 'mxchat-chat-js',
11234 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
11235 - array('jquery'),
11236 - MXCHAT_VERSION,
11237 - true
11238 - );
11239 -
11240 - // Add defer attribute if strategy is 'defer'
11241 - if ($loading_strategy === 'defer') {
11242 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
11243 - }
11244 - } else {
11245 - // For delay or interaction-based loading, we'll use a custom loader
11246 - // Don't enqueue the main script - we'll load it dynamically
11247 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
11248 - }
11249 -
4147 + // Fetch options from the database
4148 + $this->options = get_option('mxchat_options');
11250 4149 $prompts_options = get_option('mxchat_prompts_options', array());
11251 4150
11252 - // Check if AI theme is active - if so, skip inline colors in JavaScript
11253 - $theme_options = get_option('mxchat_theme_options', array());
11254 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11255 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11256 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11257 -
11258 4151 // Prepare settings for JavaScript
11259 4152 $style_settings = array(
11260 4153 'ajax_url' => admin_url('admin-ajax.php'),
11261 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
11262 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
11263 - // as a one-shot fallback for the first interaction on a fresh page load
11264 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
11265 - // but the widget refetches before each subsequent send.
11266 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11267 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11268 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
4154 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
11269 4155 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
4156 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
11270 4157 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11271 4158 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11272 4159 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11273 4160 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -11279,416 +4166,188 @@
11279 4166 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11280 4167 'icon_color' => $this->options['icon_color'] ?? '#fff',
11281 4168 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11282 4169 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11283 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
4170 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off', // Example for consistency
4171 +
11284 4172 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11285 4173 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
4174 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
11286 4175 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11287 4176 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11288 4177 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
4178 +
11289 4179 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11290 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
11291 - 'initial_email_state' => null, // Also fixed this undefined variable
11292 - 'skip_email_check' => true,
11293 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11294 - 'skip_inline_colors' => $skip_inline_colors,
11295 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
4180 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
11296 4181 );
11297 4182
11298 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11299 - // print/transcript, satisfaction rating) come from the shared
11300 - // dynamic-settings method so this inline payload and the first-open
11301 - // refresh endpoint can never drift (plan-32db95).
11302 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11303 -
11304 - // For normal/defer loading, use wp_localize_script
11305 - // For delayed loading, we store settings in a transient to be output inline
11306 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11307 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11308 - } else {
11309 - // Store settings for the delayed loader to use
11310 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
11311 - }
4183 + // Pass the settings to the script
4184 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11312 4185 }
11313 4186
11314 -/**
11315 - * Output the delayed script loader for performance optimization
11316 - */
11317 -public function mxchat_output_delayed_script_loader() {
11318 - $this->options = get_option('mxchat_options');
11319 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11320 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
11321 4187
11322 - // Get the stored settings
11323 - $prompts_options = get_option('mxchat_prompts_options', array());
11324 - $theme_options = get_option('mxchat_theme_options', array());
11325 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11326 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11327 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11328 -
11329 - $style_settings = array(
11330 - 'ajax_url' => admin_url('admin-ajax.php'),
11331 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
11332 - // before each send. This inline value is a one-shot fallback for the first interaction.
11333 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11334 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11335 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11336 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11337 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11338 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11339 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11340 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11341 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11342 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11343 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11344 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11345 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11346 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11347 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11348 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11349 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11350 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11351 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11352 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11353 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11354 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11355 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11356 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11357 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
11358 - 'initial_email_state' => null,
11359 - 'skip_email_check' => true,
11360 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11361 - 'skip_inline_colors' => $skip_inline_colors,
11362 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11363 - );
11364 -
11365 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11366 - // print/transcript, satisfaction rating) come from the shared
11367 - // dynamic-settings method so this inline payload and the first-open
11368 - // refresh endpoint can never drift (plan-32db95).
11369 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11370 -
11371 - // Determine delay time based on strategy
11372 - $delay_ms = 0;
11373 - switch ($loading_strategy) {
11374 - case 'delay_1s':
11375 - $delay_ms = 1000;
11376 - break;
11377 - case 'delay_3s':
11378 - $delay_ms = 3000;
11379 - break;
11380 - case 'delay_5s':
11381 - $delay_ms = 5000;
11382 - break;
11383 - }
11384 -
11385 - ?>
11386 - <script type="text/javascript">
11387 - (function() {
11388 - var mxchatLoaded = false;
11389 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
11390 - window.mxchatChat = mxchatChat;
11391 -
11392 - function loadMxChatScript() {
11393 - if (mxchatLoaded) return;
11394 - mxchatLoaded = true;
11395 -
11396 - function appendChatScript() {
11397 - var script = document.createElement('script');
11398 - script.src = <?php echo wp_json_encode($script_url); ?>;
11399 - script.type = 'text/javascript';
11400 - document.body.appendChild(script);
11401 - }
11402 -
11403 - if (typeof jQuery !== 'undefined') {
11404 - appendChatScript();
11405 - } else {
11406 - var jq = document.createElement('script');
11407 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
11408 - jq.onload = appendChatScript;
11409 - document.body.appendChild(jq);
11410 - }
11411 - }
11412 -
11413 - <?php if ($loading_strategy === 'on_interaction'): ?>
11414 - // Load on user interaction
11415 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
11416 - events.forEach(function(evt) {
11417 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
11418 - });
11419 - // Fallback: load after 8 seconds if no interaction
11420 - setTimeout(loadMxChatScript, 8000);
11421 - <?php else: ?>
11422 - // Load after specified delay
11423 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
11424 - <?php endif; ?>
11425 - })();
11426 - </script>
11427 - <?php
11428 -}
11429 -
11430 -/**
11431 - * Setup the cron jobs for rate limits with guard against multiple calls
11432 - */
11433 -public function setup_rate_limit_cron_jobs() {
11434 - // Add a guard to prevent multiple rapid calls
11435 - $last_setup = get_transient('mxchat_cron_setup_guard');
11436 - if ($last_setup && (time() - $last_setup) < 60) {
11437 - // Don't run again if we ran less than 60 seconds ago
11438 - return;
11439 - }
4188 +// Modify the mxchat_reset_rate_limits function to handle different timeframes
4189 +public function mxchat_reset_rate_limits() {
4190 + global $wpdb;
4191 + $all_options = get_option('mxchat_options', []);
4192 + $current_time = time();
11440 4193
11441 - // Set the guard
11442 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
4194 + // Get all rate limit options
4195 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
11443 4196
11444 - try {
11445 - // First, check if WordPress cron is disabled
11446 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
11447 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
11448 - $this->setup_fallback_rate_limit_system();
11449 - return;
11450 - }
4197 + foreach ($option_names as $option_name) {
4198 + // Parse the option name to extract role and user ID
4199 + // Format: mxchat_chat_limit_ROLE_USERID or mxchat_chat_limit_logged_out_IP
4200 + $parts = explode('_', $option_name);
11451 4201
11452 - // Check if cron is already scheduled - if so, don't mess with it
11453 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
11454 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
11455 - return;
4202 + // Skip if the option name doesn't match our expected format
4203 + if (count($parts) < 4) {
4204 + continue;
11456 4205 }
11457 4206
11458 - // Clear any orphaned hooks (but don't loop indefinitely)
11459 - $hooks_to_clear = [
11460 - 'mxchat_reset_rate_limits',
11461 - 'mxchat_reset_hourly_rate_limits',
11462 - 'mxchat_reset_daily_rate_limits',
11463 - 'mxchat_reset_weekly_rate_limits',
11464 - 'mxchat_reset_monthly_rate_limits'
11465 - ];
4207 + // Extract role (may be multiple parts like 'shop_manager')
4208 + $role_parts = array_slice($parts, 3, -1); // Get all parts between 'mxchat_chat_limit_' and the last part (user ID)
4209 + $role = implode('_', $role_parts);
11466 4210
11467 - foreach ($hooks_to_clear as $hook) {
11468 - // Only clear a maximum of 3 instances to prevent infinite loops
11469 - $cleared = 0;
11470 - while (wp_next_scheduled($hook) && $cleared < 3) {
11471 - wp_clear_scheduled_hook($hook);
11472 - $cleared++;
11473 - }
4211 + // Skip if role doesn't exist in our settings
4212 + if (!isset($all_options['rate_limits'][$role])) {
4213 + continue;
11474 4214 }
11475 4215
11476 - // Small delay after clearing
11477 - usleep(100000); // 0.1 seconds
4216 + $timeframe = $all_options['rate_limits'][$role]['timeframe'];
4217 + $limit_data = get_option($option_name);
11478 4218
11479 - // Try to schedule the event
11480 - $initial_time = time() + 300; // Start in 5 minutes
11481 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
11482 -
11483 - if ($result === false) {
11484 - //error_log('MxChat: Failed to schedule cron, using fallback system');
11485 - $this->setup_fallback_rate_limit_system();
11486 - } else {
11487 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
4219 + if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
4220 + continue;
11488 4221 }
11489 4222
11490 - } catch (Exception $e) {
11491 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
11492 - $this->setup_fallback_rate_limit_system();
11493 - }
11494 -}
11495 -
11496 -/**
11497 - * Try alternative cron scheduling methods
11498 - */
11499 -private function try_alternative_cron_scheduling($initial_time) {
11500 - try {
11501 - // Method 1: Try with current time instead of future time
11502 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
11503 - if ($result1 !== false) {
11504 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
11505 - return true;
11506 - }
4223 + $timestamp = $limit_data['timestamp'];
4224 + $should_reset = false;
11507 4225
11508 - // Method 2: Try with a different interval
11509 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
11510 - if ($result2 !== false) {
11511 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
11512 - return true;
4226 + // Determine if we should reset based on the timeframe
4227 + switch ($timeframe) {
4228 + case 'hourly':
4229 + $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
4230 + break;
4231 + case 'daily':
4232 + $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
4233 + break;
4234 + case 'weekly':
4235 + $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
4236 + break;
4237 + case 'monthly':
4238 + $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
4239 + break;
11513 4240 }
11514 4241
11515 - // Method 3: Try wp_schedule_single_event first, then recurring
11516 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
11517 - if ($result3 !== false) {
11518 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
11519 - // Schedule the next one manually in the handler
11520 - return true;
4242 + // Reset the counter if the timeframe has passed
4243 + if ($should_reset) {
4244 + delete_option($option_name);
4245 + wp_cache_delete($option_name, 'options');
11521 4246 }
11522 -
11523 - return false;
11524 -
11525 - } catch (Exception $e) {
11526 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
11527 - return false;
11528 4247 }
4248 +
4249 + // Clean up any orphaned entries
4250 + wp_cache_delete('mxchat_all_chat_limits', 'options');
11529 4251 }
4252 +private function mxchat_fetch_woocommerce_products() {
4253 + // Ensure WooCommerce is active
4254 + if (!class_exists('WooCommerce')) {
4255 + return [];
4256 + }
11530 4257
11531 -/**
11532 - * Enhanced fallback rate limit system
11533 - */
11534 -private function setup_fallback_rate_limit_system() {
11535 - // Set a flag to use database-based rate limit cleanup
11536 - update_option('mxchat_use_fallback_rate_limits', true);
11537 -
11538 - // Schedule a one-time check to happen on the next plugin load
11539 - update_option('mxchat_next_rate_limit_check', time() + 3600);
11540 -
11541 - // Also set up a more frequent fallback check (every 4 hours)
11542 - update_option('mxchat_fallback_check_interval', 4 * 3600);
11543 -
11544 - //error_log('MxChat: Fallback rate limit system activated');
11545 -}
4258 + $args = array(
4259 + 'post_type' => 'product',
4260 + 'post_status' => 'publish',
4261 + 'posts_per_page' => -1,
4262 + );
11546 4263
11547 -/**
11548 - * Enhanced fallback check method
11549 - */
11550 -public function check_fallback_rate_limits() {
11551 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11552 -
11553 - if (!$use_fallback) {
11554 - return; // Regular cron is working
4264 + $products = get_posts($args);
4265 + $product_data = [];
4266 +
4267 + foreach ($products as $product) {
4268 + $product_id = $product->ID;
4269 + $product_obj = wc_get_product($product_id);
4270 +
4271 + $product_data[] = array(
4272 + 'id' => $product_id,
4273 + 'name' => $product_obj->get_name(),
4274 + 'description' => $product_obj->get_description(),
4275 + 'short_description' => $product_obj->get_short_description(),
4276 + 'url' => get_permalink($product_id),
4277 + 'price' => $product_obj->get_regular_price(),
4278 + 'sale_price' => $product_obj->get_sale_price(),
4279 + 'stock_status' => $product_obj->get_stock_status(),
4280 + 'sku' => $product_obj->get_sku(),
4281 + 'in_stock' => $product_obj->is_in_stock(),
4282 + 'total_sales' => $product_obj->get_total_sales(),
4283 + );
11555 4284 }
11556 -
11557 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11558 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
11559 -
11560 - if (time() >= $next_check) {
11561 - //error_log('MxChat: Running fallback rate limit cleanup');
11562 - $this->mxchat_reset_rate_limits();
11563 -
11564 - // Schedule next check
11565 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
11566 - }
4285 +
4286 + return $product_data;
11567 4287 }
4288 +
4289 +
11568 4290 /**
11569 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
4291 + * Check if the current user has exceeded their rate limit based on role
4292 + *
4293 + * @return true|array True if limit not exceeded, or array with error message if exceeded
11570 4294 */
11571 4295 public function check_rate_limit() {
11572 - // Check if we need to run fallback cleanup
11573 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11574 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
4296 + $all_options = get_option('mxchat_options', []);
4297 + //error_log('MXChat Rate Limit: Starting check');
4298 + //error_log('MXChat Rate Limit: Options: ' . print_r($all_options, true));
11575 4299
11576 - if ($use_fallback && time() >= $next_check) {
11577 - $this->mxchat_reset_rate_limits();
11578 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
11579 - }
11580 -
11581 - // Get bot ID from current request context
11582 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11583 -
11584 - // Get bot-specific options (includes rate limits if overridden)
11585 - $bot_options = $this->get_bot_options($bot_id);
11586 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
11587 -
11588 - // Use bot-specific rate limits if available, otherwise fall back to default
11589 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
11590 -
11591 - // -------------------------------------------------------------------
11592 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
11593 - // it acts as a hard ceiling across all users + all roles. Default is
11594 - // 'unlimited' so existing installs are unchanged. Counter key drops
11595 - // both <role> and <user_id> segments — single pool per bot.
11596 - // -------------------------------------------------------------------
11597 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
11598 - ? $current_options['rate_limits_global']
11599 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
11600 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
11601 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
11602 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
11603 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11604 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
11605 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
11606 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
11607 - if ((int) $global_data['count'] === 0) {
11608 - $global_data['timestamp'] = time();
11609 - update_option($global_option, $global_data);
11610 - }
11611 - $now = time();
11612 - $ts = (int) $global_data['timestamp'];
11613 - $reset = false;
11614 - switch ($global_timeframe) {
11615 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
11616 - case 'daily': $reset = ($now - $ts) >= 86400; break;
11617 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
11618 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
11619 - }
11620 - if ($reset) {
11621 - $global_data = ['count' => 0, 'timestamp' => $now];
11622 - update_option($global_option, $global_data);
11623 - }
11624 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
11625 - $global_msg = !empty($global_cfg['message'])
11626 - ? $global_cfg['message']
11627 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
11628 - return [
11629 - 'error' => true,
11630 - 'message' => $this->process_rate_limit_message_html($global_msg),
11631 - ];
11632 - }
11633 - // Reserve the slot for this request. Per-role check below also increments
11634 - // its own counter — that is intentional, both ceilings apply independently.
11635 - $global_data['count']++;
11636 - update_option($global_option, $global_data);
11637 - }
11638 -
11639 4300 // Determine user role or if logged out
11640 4301 if (is_user_logged_in()) {
11641 4302 $user = wp_get_current_user();
11642 4303 $user_id = $user->ID;
11643 4304
11644 - // Get the user's primary role using reset() to safely get the first element
4305 + // Get the user's primary role (first in the array)
11645 4306 $user_roles = $user->roles;
11646 -
11647 - // Safely get the first role regardless of array key structure
11648 - if (!empty($user_roles) && is_array($user_roles)) {
11649 - $role = reset($user_roles); // This safely gets the first element regardless of key
11650 - } else {
11651 - $role = 'subscriber'; // Default to subscriber if no role found
11652 - }
4307 + $role = !empty($user_roles) ? $user_roles[0] : 'subscriber'; // Default to subscriber if no role found
4308 + //error_log('MXChat Rate Limit: User ID: ' . $user_id . ', Role: ' . $role);
11653 4309 } else {
11654 4310 $role = 'logged_out';
11655 4311 // Use IP address for non-logged-in users
11656 4312 $user_id = $this->get_client_ip();
4313 + //error_log('MXChat Rate Limit: Logged out user IP: ' . $user_id);
11657 4314 }
11658 4315
11659 4316 // Check if rate limits are configured for this role
11660 - if (!isset($rate_limits_source[$role])) {
4317 + if (!isset($all_options['rate_limits'][$role])) {
4318 + //error_log('MXChat Rate Limit: No rate limit configured for role: ' . $role);
11661 4319 return true; // No limit set for this role
11662 4320 }
11663 4321
11664 - $limit = $rate_limits_source[$role]['limit'];
4322 + $limit = $all_options['rate_limits'][$role]['limit'];
4323 + //error_log('MXChat Rate Limit: Limit for role ' . $role . ': ' . $limit);
11665 4324
11666 4325 // If unlimited, return true immediately
11667 4326 if ($limit === 'unlimited') {
4327 + //error_log('MXChat Rate Limit: Unlimited setting, no limit applied');
11668 4328 return true;
11669 4329 }
11670 4330
11671 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
11672 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
11673 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
11674 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
4331 + // Get the option name for this user/role
4332 + $option_name = 'mxchat_chat_limit_' . $role . '_' . $user_id;
4333 + //error_log('MXChat Rate Limit: Option name: ' . $option_name);
11675 4334
11676 - // Include bot_id in option name so each bot has separate rate limits
11677 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
11678 -
11679 4335 // Get the counter data
11680 4336 $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
4337 + //error_log('MXChat Rate Limit: Current limit data: ' . print_r($limit_data, true));
11681 4338
11682 4339 // If first request or counter reset needed, set the initial timestamp
11683 4340 if ($limit_data['count'] === 0) {
11684 4341 $limit_data['timestamp'] = time();
11685 4342 update_option($option_name, $limit_data);
4343 + //error_log('MXChat Rate Limit: First request, initialized timestamp');
11686 4344 }
11687 4345
11688 4346 // Get the timeframe
11689 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
11690 - $rate_limits_source[$role]['timeframe'] : 'daily';
4347 + $timeframe = isset($all_options['rate_limits'][$role]['timeframe']) ?
4348 + $all_options['rate_limits'][$role]['timeframe'] : 'daily';
4349 + //error_log('MXChat Rate Limit: Timeframe: ' . $timeframe);
11691 4350
11692 4351 // Check if the counter needs to be reset based on timeframe
11693 4352 $current_time = time();
11694 4353 $timestamp = $limit_data['timestamp'];
@@ -11708,19 +4367,24 @@
11708 4367 $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
11709 4368 break;
11710 4369 }
11711 4370
4371 + //error_log('MXChat Rate Limit: Current time: ' . $current_time . ', Last timestamp: ' . $timestamp);
4372 + //error_log('MXChat Rate Limit: Time elapsed: ' . ($current_time - $timestamp) . ' seconds');
4373 + //error_log('MXChat Rate Limit: Should reset: ' . ($should_reset ? 'Yes' : 'No'));
4374 +
11712 4375 // Reset the counter if the timeframe has passed
11713 4376 if ($should_reset) {
11714 4377 $limit_data = ['count' => 0, 'timestamp' => $current_time];
11715 4378 update_option($option_name, $limit_data);
4379 + //error_log('MXChat Rate Limit: Reset counter to 0');
11716 4380 }
11717 4381
11718 4382 // Check if user has exceeded their limit
11719 4383 if ($limit_data['count'] >= intval($limit)) {
11720 4384 // Get the custom message for this role
11721 - $message = !empty($rate_limits_source[$role]['message'])
11722 - ? $rate_limits_source[$role]['message']
4385 + $message = !empty($all_options['rate_limits'][$role]['message'])
4386 + ? $all_options['rate_limits'][$role]['message']
11723 4387 : __('Rate limit exceeded. Please try again later.', 'mxchat');
11724 4388
11725 4389 // Add timeframe information to the message if placeholders exist
11726 4390 $timeframe_label = '';
@@ -11745,12 +4409,11 @@
11745 4409 [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
11746 4410 $message
11747 4411 );
11748 4412
11749 - // Process HTML links in the message
11750 - $message = $this->process_rate_limit_message_html($message);
4413 + //error_log('MXChat Rate Limit: Limit exceeded. Message: ' . $message);
11751 4414
11752 - // Return error with the processed message
4415 + // Return error with the custom message
11753 4416 return [
11754 4417 'error' => true,
11755 4418 'message' => $message
11756 4419 ];
@@ -11758,235 +4421,13 @@
11758 4421
11759 4422 // Increment the counter
11760 4423 $limit_data['count']++;
11761 4424 update_option($option_name, $limit_data);
4425 + //error_log('MXChat Rate Limit: Incremented counter to ' . $limit_data['count']);
11762 4426
11763 4427 return true;
11764 4428 }
11765 4429
11766 -/**
11767 - * Enhanced rate limit reset with better error handling
11768 - */
11769 -public function mxchat_reset_rate_limits() {
11770 - try {
11771 - global $wpdb;
11772 - $all_options = get_option('mxchat_options', []);
11773 - $current_time = time();
11774 -
11775 - // Get rate limit options with a safer query and limit
11776 - $option_names = $wpdb->get_col(
11777 - $wpdb->prepare(
11778 - "SELECT option_name FROM {$wpdb->options}
11779 - WHERE option_name LIKE %s
11780 - LIMIT 1000",
11781 - 'mxchat_chat_limit_%'
11782 - )
11783 - );
11784 -
11785 - if (empty($option_names)) {
11786 - return;
11787 - }
11788 -
11789 - $processed_count = 0;
11790 - $max_processing_time = 30; // Maximum 30 seconds
11791 - $start_time = time();
11792 -
11793 - foreach ($option_names as $option_name) {
11794 - // Check processing time limit
11795 - if ((time() - $start_time) > $max_processing_time) {
11796 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
11797 - break;
11798 - }
11799 -
11800 - // Parse the option name more safely
11801 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
11802 - continue;
11803 - }
11804 -
11805 - $role_and_user = $matches[1] . '_' . $matches[2];
11806 - $parts = explode('_', $role_and_user);
11807 -
11808 - if (count($parts) < 2) {
11809 - continue;
11810 - }
11811 -
11812 - // Extract role (everything except the last part which is user ID)
11813 - $user_id_part = array_pop($parts);
11814 - $role = implode('_', $parts);
11815 -
11816 - // Skip if role doesn't exist in our settings
11817 - if (!isset($all_options['rate_limits'][$role])) {
11818 - // Clean up orphaned entries
11819 - delete_option($option_name);
11820 - continue;
11821 - }
11822 -
11823 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
11824 - $limit_data = get_option($option_name);
11825 -
11826 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
11827 - // Clean up invalid entries
11828 - delete_option($option_name);
11829 - continue;
11830 - }
11831 -
11832 - $timestamp = $limit_data['timestamp'];
11833 - $should_reset = false;
11834 -
11835 - // Determine if we should reset based on the timeframe
11836 - switch ($timeframe) {
11837 - case 'hourly':
11838 - $should_reset = ($current_time - $timestamp) >= 3600;
11839 - break;
11840 - case 'daily':
11841 - $should_reset = ($current_time - $timestamp) >= 86400;
11842 - break;
11843 - case 'weekly':
11844 - $should_reset = ($current_time - $timestamp) >= 604800;
11845 - break;
11846 - case 'monthly':
11847 - $should_reset = ($current_time - $timestamp) >= 2592000;
11848 - break;
11849 - }
11850 -
11851 - // Reset the counter if the timeframe has passed
11852 - if ($should_reset) {
11853 - delete_option($option_name);
11854 - wp_cache_delete($option_name, 'options');
11855 - $processed_count++;
11856 - }
11857 - }
11858 -
11859 - // Clean up any orphaned cache entries
11860 - wp_cache_delete('mxchat_all_chat_limits', 'options');
11861 -
11862 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11863 -
11864 - } catch (Exception $e) {
11865 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11866 - }
11867 -}
11868 -
11869 -
11870 -/**
11871 - * Process HTML links in rate limit messages
11872 - *
11873 - * @param string $message The rate limit message
11874 - * @return string The processed message with safe HTML links
11875 - */
11876 -private function process_rate_limit_message_html($message) {
11877 - // Return original message if empty
11878 - if (empty($message)) {
11879 - return $message;
11880 - }
11881 -
11882 - // First, convert markdown links to HTML
11883 - $message = $this->convert_markdown_links($message);
11884 -
11885 - // Then, auto-convert any remaining plain URLs to links
11886 - $message = $this->auto_link_urls($message);
11887 -
11888 - // Allow basic HTML tags for links and formatting
11889 - $allowed_tags = [
11890 - 'a' => [
11891 - 'href' => true,
11892 - 'target' => true,
11893 - 'rel' => true,
11894 - 'title' => true,
11895 - 'class' => true
11896 - ],
11897 - 'strong' => [],
11898 - 'em' => [],
11899 - 'br' => [],
11900 - 'b' => [],
11901 - 'i' => [],
11902 - 'span' => ['class' => true]
11903 - ];
11904 -
11905 - // Sanitize but allow the specified HTML tags
11906 - $processed_message = wp_kses($message, $allowed_tags);
11907 -
11908 - // If wp_kses stripped everything, return the original message as plain text
11909 - if (empty($processed_message) && !empty($message)) {
11910 - // Strip all HTML and return plain text as fallback
11911 - return wp_strip_all_tags($message);
11912 - }
11913 -
11914 - return $processed_message;
11915 -}
11916 -
11917 -/**
11918 - * Convert markdown links to HTML
11919 - *
11920 - * @param string $text The text to process
11921 - * @return string The text with markdown links converted to HTML
11922 - */
11923 -private function convert_markdown_links($text) {
11924 - // Return original text if empty
11925 - if (empty($text)) {
11926 - return $text;
11927 - }
11928 -
11929 - // Pattern to match markdown links: [text](url)
11930 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
11931 -
11932 - $processed_text = preg_replace_callback($pattern, function($matches) {
11933 - $link_text = $matches[1];
11934 - $url = $matches[2];
11935 -
11936 - // Clean up any trailing punctuation from the URL
11937 - $url = rtrim($url, '.,;:!?');
11938 -
11939 - // Sanitize the link text and URL
11940 - $safe_text = esc_html($link_text);
11941 - $safe_url = esc_url($url);
11942 -
11943 - // Create the HTML link
11944 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
11945 - }, $text);
11946 -
11947 - // If preg_replace_callback failed, return original text
11948 - if ($processed_text === null) {
11949 - return $text;
11950 - }
11951 -
11952 - return $processed_text;
11953 -}
11954 -
11955 -/**
11956 - * Auto-convert plain URLs to clickable links
11957 - *
11958 - * @param string $text The text to process
11959 - * @return string The text with URLs converted to links
11960 - */
11961 -private function auto_link_urls($text) {
11962 - // Return original text if empty
11963 - if (empty($text)) {
11964 - return $text;
11965 - }
11966 -
11967 - // Simple pattern that avoids complex lookbehinds
11968 - // This will match URLs that are not already inside href attributes or markdown links
11969 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
11970 -
11971 - $processed_text = preg_replace_callback($pattern, function($matches) {
11972 - $url = $matches[0];
11973 - // Clean up any trailing punctuation that might have been captured
11974 - $url = rtrim($url, '.,;:!?');
11975 -
11976 - // Add target="_blank" and rel="noopener noreferrer" for security
11977 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
11978 - }, $text);
11979 -
11980 - // If preg_replace_callback failed, return original text
11981 - if ($processed_text === null) {
11982 - return $text;
11983 - }
11984 -
11985 - return $processed_text;
11986 -}
11987 -
11988 -
11989 4430 // Helper function to get client IP address
11990 4431 private function get_client_ip() {
11991 4432 // Check for shared internet/ISP IP
11992 4433 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
@@ -12006,602 +4447,7 @@
12006 4447
12007 4448 // Fallback
12008 4449 return 'unknown';
12009 4450 }
12010 -
12011 -/**
12012 - * AJAX handler to get system information for testing panel
12013 - */
12014 -/**
12015 - * AJAX handler to get system information for testing panel
12016 - */
12017 -public function mxchat_get_system_info() {
12018 - // Verify nonce for security
12019 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12020 - wp_send_json_error(['message' => 'Invalid nonce']);
12021 - return;
12022 - }
12023 -
12024 - // Only allow admin users
12025 - if (!current_user_can('administrator')) {
12026 - wp_send_json_error(['message' => 'Unauthorized']);
12027 - return;
12028 - }
12029 -
12030 - // Get system prompt from options
12031 - $system_prompt = isset($this->options['system_prompt_instructions'])
12032 - ? $this->options['system_prompt_instructions']
12033 - : 'No system prompt configured';
12034 -
12035 - // Get selected model
12036 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
12037 -
12038 - // Check if OpenRouter is being used
12039 - $is_openrouter = ($selected_model === 'openrouter');
12040 - $openrouter_model = '';
12041 -
12042 - if ($is_openrouter) {
12043 - // Get the actual OpenRouter model that's selected
12044 - $openrouter_model = isset($this->options['openrouter_selected_model'])
12045 - ? $this->options['openrouter_selected_model']
12046 - : 'No OpenRouter model selected';
12047 -
12048 - // Update selected_model display to show both
12049 - $selected_model = 'OpenRouter: ' . $openrouter_model;
12050 - }
12051 -
12052 - // Get API key status (just check if they exist, don't expose the keys)
12053 - $api_status = [];
12054 - $api_status['openai'] = !empty($this->options['api_key']);
12055 - $api_status['claude'] = !empty($this->options['claude_api_key']);
12056 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
12057 - $api_status['xai'] = !empty($this->options['xai_api_key']);
12058 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
12059 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
12060 -
12061 - wp_send_json_success([
12062 - 'system_prompt' => $system_prompt,
12063 - 'selected_model' => $selected_model,
12064 - 'is_openrouter' => $is_openrouter,
12065 - 'openrouter_model' => $openrouter_model,
12066 - 'api_status' => $api_status
12067 - ]);
12068 -}
12069 -
12070 -/**
12071 - * AJAX handler to get similarity threshold
12072 - */
12073 -public function mxchat_get_similarity_threshold() {
12074 - // Verify nonce for security
12075 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12076 - wp_send_json_error(['message' => 'Invalid nonce']);
12077 - return;
12078 - }
12079 -
12080 - // Only allow admin users
12081 - if (!current_user_can('administrator')) {
12082 - wp_send_json_error(['message' => 'Unauthorized']);
12083 - return;
12084 - }
12085 -
12086 - // Get similarity threshold from main options (default 35%)
12087 - $similarity_threshold = isset($this->options['similarity_threshold'])
12088 - ? ((int) $this->options['similarity_threshold']) / 100
12089 - : 0.35;
12090 -
12091 - wp_send_json_success([
12092 - 'threshold' => $similarity_threshold,
12093 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
12094 - ]);
12095 -}
12096 -
12097 -/**
12098 - * AJAX handler to get knowledge base status
12099 - */
12100 -public function mxchat_get_kb_status() {
12101 - // Verify nonce for security
12102 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12103 - wp_send_json_error(['message' => 'Invalid nonce']);
12104 - return;
12105 - }
12106 -
12107 - // Only allow admin users
12108 - if (!current_user_can('administrator')) {
12109 - wp_send_json_error(['message' => 'Unauthorized']);
12110 - return;
12111 - }
12112 -
12113 - // Check OpenAI Vector Store first (takes priority)
12114 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
12115 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
12116 -
12117 - if ($use_vectorstore) {
12118 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
12119 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
12120 -
12121 - $kb_info = [
12122 - 'type' => 'OpenAI Vector Store',
12123 - 'status' => 'Active',
12124 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
12125 - ];
12126 -
12127 - wp_send_json_success($kb_info);
12128 - return;
12129 - }
12130 -
12131 - // Check Pinecone vs WordPress
12132 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12133 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12134 -
12135 - $kb_info = [
12136 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
12137 - 'status' => 'Active'
12138 - ];
12139 -
12140 - // Get document count
12141 - if ($use_pinecone) {
12142 - $kb_info['documents'] = 'Connected to Pinecone';
12143 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
12144 - } else {
12145 - // Count documents in WordPress database
12146 - global $wpdb;
12147 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
12148 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
12149 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
12150 - }
12151 -
12152 - wp_send_json_success($kb_info);
12153 -}
12154 -
12155 -/**
12156 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
12157 - */
12158 -public function mxchat_start_fresh_session() {
12159 - // Verify nonce for security
12160 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12161 - wp_send_json_error(['message' => 'Invalid nonce']);
12162 - return;
12163 - }
12164 -
12165 - // Only allow admin users
12166 - if (!current_user_can('administrator')) {
12167 - wp_send_json_error(['message' => 'Unauthorized']);
12168 - return;
12169 - }
12170 -
12171 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
12172 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
12173 -
12174 - if (empty($old_session_id)) {
12175 - wp_send_json_error(['message' => 'Old session ID required']);
12176 - return;
12177 - }
12178 -
12179 - // If no new session ID provided, generate one
12180 - if (empty($new_session_id)) {
12181 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
12182 - }
12183 -
12184 - // Clear ALL data associated with the old session
12185 - $this->clear_complete_session_data($old_session_id);
12186 -
12187 - // Initialize the new session
12188 - $this->initialize_fresh_session($new_session_id);
12189 -
12190 - wp_send_json_success([
12191 - 'message' => 'Fresh session started successfully',
12192 - 'new_session_id' => $new_session_id,
12193 - 'old_session_id' => $old_session_id
12194 - ]);
12195 -}
12196 -
12197 -/**
12198 - * Clear ALL data associated with a session (ENHANCED)
12199 - */
12200 -private function clear_complete_session_data($session_id) {
12201 - // Clear chat history
12202 - delete_option("mxchat_history_{$session_id}");
12203 -
12204 - // Clear chat mode
12205 - delete_option("mxchat_mode_{$session_id}");
12206 -
12207 - // Clear any PDF/Word transients
12208 - $this->clear_pdf_transients($session_id);
12209 - if (method_exists($this, 'clear_word_transients')) {
12210 - $this->clear_word_transients($session_id);
12211 - }
12212 -
12213 - // Clear agent-related data
12214 - delete_option("mxchat_channel_{$session_id}");
12215 - delete_option("mxchat_agent_name_{$session_id}");
12216 - delete_option("mxchat_email_{$session_id}");
12217 -
12218 - // Clear any recommendation flow state
12219 - delete_option("mxchat_sr_flow_state_{$session_id}");
12220 -
12221 - // Clear any cached embeddings or context
12222 - delete_transient("mxchat_context_{$session_id}");
12223 - delete_transient("mxchat_last_query_{$session_id}");
12224 -
12225 - // Clear any testing data
12226 - delete_transient("mxchat_testing_data_{$session_id}");
12227 -
12228 - // Clear any rate limiting data for this session
12229 - delete_transient("mxchat_rate_limit_{$session_id}");
12230 -
12231 - // Clear any other session-specific transients
12232 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
12233 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
12234 - delete_transient("mxchat_include_word_in_context_{$session_id}");
12235 -
12236 - // Clear form addon state (pending forms and submitted forms)
12237 - delete_option("mxchat_pending_form_{$session_id}");
12238 - delete_option("mxchat_submitted_forms_{$session_id}");
12239 -
12240 - //error_log("MxChat: Cleared all data for session: {$session_id}");
12241 -}
12242 -
12243 -/**
12244 - * Initialize a fresh session with default data
12245 - */
12246 -private function initialize_fresh_session($session_id) {
12247 - // Set default chat mode
12248 - update_option("mxchat_mode_{$session_id}", 'ai');
12249 -
12250 - //error_log("MxChat: Initialized fresh session: {$session_id}");
12251 -}
12252 -
12253 -/**
12254 - * Helper method to clear Word document transients (if you have Word support)
12255 - */
12256 -private function clear_word_transients($session_id) {
12257 - delete_transient('mxchat_word_url_' . $session_id);
12258 - delete_transient('mxchat_word_filename_' . $session_id);
12259 - delete_transient('mxchat_word_embeddings_' . $session_id);
12260 - delete_transient('mxchat_include_word_in_context_' . $session_id);
12261 -}
12262 -
12263 -/**
12264 - * Simplified testing data capture method (CLEANED UP)
12265 - */
12266 -private function capture_testing_data($user_embedding, $message, $session_id) {
12267 - // Only capture for admin users
12268 - if (!current_user_can('administrator')) {
12269 - return null;
12270 - }
12271 -
12272 - $testing_data = [
12273 - 'query' => $message,
12274 - 'timestamp' => time(),
12275 - 'top_matches' => [],
12276 - 'action_matches' => [] // Add action matches
12277 - ];
12278 -
12279 - // Get similarity threshold
12280 - $similarity_threshold = isset($this->options['similarity_threshold'])
12281 - ? ((int) $this->options['similarity_threshold']) / 100
12282 - : 0.35;
12283 -
12284 - $testing_data['similarity_threshold'] = $similarity_threshold;
12285 -
12286 - // Use the real similarity analysis if available
12287 - if ($this->last_similarity_analysis !== null) {
12288 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
12289 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
12290 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
12291 - } else {
12292 - // Fallback: determine knowledge base type
12293 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12294 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12295 -
12296 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
12297 - }
12298 -
12299 - // Include action analysis if available
12300 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
12301 - $testing_data['action_matches'] = $this->last_action_analysis;
12302 -
12303 - // Clear it after capturing to avoid stale data
12304 - $this->last_action_analysis = null;
12305 - }
12306 -
12307 - return $testing_data;
12308 -}
12309 -
12310 -
12311 -/**
12312 - * Track URL clicks from chatbot responses
12313 - */
12314 -public function mxchat_track_url_click() {
12315 - // Verify nonce for security
12316 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12317 - wp_send_json_error(['message' => 'Invalid nonce']);
12318 - wp_die();
12319 - }
12320 -
12321 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12322 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
12323 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
12324 -
12325 - if (empty($session_id) || empty($clicked_url)) {
12326 - wp_send_json_error(['message' => 'Missing required data']);
12327 - wp_die();
12328 - }
12329 -
12330 - global $wpdb;
12331 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12332 -
12333 - // Insert click tracking record
12334 - $wpdb->insert(
12335 - $table_name,
12336 - [
12337 - 'session_id' => $session_id,
12338 - 'clicked_url' => $clicked_url,
12339 - 'message_context' => $message_context,
12340 - 'click_timestamp' => current_time('mysql', 1),
12341 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
12342 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
12343 - ]
12344 - );
12345 -
12346 - wp_send_json_success(['message' => 'Click tracked']);
12347 - wp_die();
12348 -}
12349 -
12350 -/**
12351 - * Get URL click analytics for a session
12352 - */
12353 -public function mxchat_get_url_clicks($session_id) {
12354 - global $wpdb;
12355 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12356 -
12357 - $clicks = $wpdb->get_results($wpdb->prepare(
12358 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
12359 - $session_id
12360 - ));
12361 -
12362 - return $clicks;
12363 -}
12364 -/**
12365 - * Track the originating page where chat was started
12366 - */
12367 -public function mxchat_track_originating_page() {
12368 - // Verify nonce
12369 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12370 - wp_send_json_error(['message' => 'Invalid nonce']);
12371 - wp_die();
12372 - }
12373 -
12374 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12375 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
12376 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
12377 -
12378 - if (empty($session_id)) {
12379 - wp_send_json_error(['message' => 'Missing session ID']);
12380 - wp_die();
12381 - }
12382 -
12383 - global $wpdb;
12384 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
12385 -
12386 - // Check if we've already tracked for this session
12387 - $existing = $wpdb->get_var($wpdb->prepare(
12388 - "SELECT COUNT(*) FROM $table_name
12389 - WHERE session_id = %s
12390 - AND originating_page_url IS NOT NULL",
12391 - $session_id
12392 - ));
12393 -
12394 - if ($existing > 0) {
12395 - wp_send_json_success(['message' => 'Already tracked']);
12396 - wp_die();
12397 - }
12398 -
12399 - // Update the first message in this session with originating page info
12400 - $wpdb->query($wpdb->prepare(
12401 - "UPDATE $table_name
12402 - SET originating_page_url = %s,
12403 - originating_page_title = %s
12404 - WHERE session_id = %s
12405 - ORDER BY timestamp ASC
12406 - LIMIT 1",
12407 - $page_url,
12408 - $page_title,
12409 - $session_id
12410 - ));
12411 -
12412 - wp_send_json_success(['message' => 'Originating page tracked']);
12413 - wp_die();
12414 -}
12415 -
12416 -/**
12417 - * Validate and clean URLs from AI response
12418 - * Removes any URLs that aren't in the knowledge base
12419 - *
12420 - * @param string $response_text The AI-generated response
12421 - * @param array $valid_urls Array of URLs from the knowledge base
12422 - * @return string Cleaned response with invalid URLs removed/flagged
12423 - */
12424 -private function validate_and_clean_urls($response_text, $valid_urls) {
12425 - // DEBUG: Log what we're working with
12426 - //error_log("=== MxChat URL Validation Debug ===");
12427 - //error_log("Valid URLs count: " . count($valid_urls));
12428 - //error_log("Valid URLs: " . print_r($valid_urls, true));
12429 - //error_log("Response text length: " . strlen($response_text));
12430 - //error_log("Response text preview: " . substr($response_text, 0, 500));
12431 -
12432 - // If no valid URLs provided or empty response, return as-is
12433 - if (empty($valid_urls) || empty($response_text)) {
12434 - //error_log("Validation skipped - empty valid_urls or response");
12435 - return $response_text;
12436 - }
12437 -
12438 - // Extract all URLs from the AI response
12439 - // This regex matches http:// and https:// URLs
12440 - preg_match_all(
12441 - '#\bhttps?://[^\s<>"\')\]]+#i',
12442 - $response_text,
12443 - $matches
12444 - );
12445 -
12446 - // If no URLs found in response, return as-is
12447 - if (empty($matches[0])) {
12448 - //error_log("No URLs found in response");
12449 - return $response_text;
12450 - }
12451 -
12452 - $found_urls = $matches[0];
12453 - $cleaned_response = $response_text;
12454 - $removed_count = 0;
12455 -
12456 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
12457 - $normalized_valid_urls = array_map(function($url) {
12458 - // Remove trailing slash
12459 - $url = rtrim($url, '/');
12460 - // Remove URL fragments (#section)
12461 - $url = preg_replace('/#.*$/', '', $url);
12462 - // Remove trailing punctuation that might have been captured
12463 - $url = rtrim($url, '.,;:!?');
12464 - return $url;
12465 - }, $valid_urls);
12466 -
12467 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
12468 -
12469 - foreach ($found_urls as $found_url) {
12470 - // Clean up the found URL (remove trailing punctuation that might have been captured)
12471 - $clean_found_url = rtrim($found_url, '.,;:!?)');
12472 -
12473 - // DEBUG: Log each URL being checked
12474 - //error_log("Checking found URL: " . $found_url);
12475 -
12476 - // Normalize for comparison
12477 - $normalized_found = rtrim($clean_found_url, '/');
12478 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
12479 -
12480 - //error_log("Normalized found URL: " . $normalized_found);
12481 -
12482 - // Check if this URL exists in our valid URLs list
12483 - $is_valid = false;
12484 -
12485 - //error_log("Starting validation checks for: " . $normalized_found);
12486 -
12487 - // First, try exact match
12488 - if (in_array($normalized_found, $normalized_valid_urls)) {
12489 - $is_valid = true;
12490 - //error_log("EXACT MATCH FOUND");
12491 - } else {
12492 - //error_log("No exact match, checking variations...");
12493 - // If no exact match, check if it's a variation (with query params, etc.)
12494 - foreach ($normalized_valid_urls as $valid_url) {
12495 - //error_log(" Comparing against valid URL: " . $valid_url);
12496 -
12497 - // Check if the found URL starts with a valid URL (handles query params)
12498 - if (strpos($normalized_found, $valid_url) === 0) {
12499 - // Check what comes after the valid URL
12500 - $remainder = substr($normalized_found, strlen($valid_url));
12501 -
12502 - // Only valid if:
12503 - // 1. Exact match (remainder is empty)
12504 - // 2. Query params (starts with ?)
12505 - // 3. Fragment (starts with #)
12506 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
12507 - $is_valid = true;
12508 - //error_log(" MATCH: Found URL is valid variation of base URL");
12509 - break;
12510 - } else {
12511 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
12512 - }
12513 - }
12514 - // Also check the reverse (in case valid URL has query params)
12515 - if (strpos($valid_url, $normalized_found) === 0) {
12516 - $is_valid = true;
12517 - //error_log(" MATCH: Valid URL starts with found URL");
12518 - break;
12519 - }
12520 - }
12521 -
12522 - if (!$is_valid) {
12523 - //error_log("NO MATCH FOUND - URL should be removed");
12524 - }
12525 - }
12526 -
12527 - // If URL is not valid, remove it from the response
12528 - if (!$is_valid) {
12529 - // Log the removal for debugging
12530 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
12531 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
12532 -
12533 - $removed_count++;
12534 -
12535 - // Check if URL is part of a markdown link: [text](url)
12536 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
12537 - if (preg_match($markdown_pattern, $cleaned_response)) {
12538 - //error_log("Found markdown link, removing but keeping text");
12539 - // Remove the markdown link but keep the text
12540 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
12541 - }
12542 - // Check if URL is part of an HTML link: <a href="url">text</a>
12543 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
12544 - //error_log("Found HTML link, removing but keeping text");
12545 - // Remove the HTML link but keep the text
12546 - $link_text = $link_match[1];
12547 - $cleaned_response = preg_replace(
12548 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
12549 - $link_text,
12550 - $cleaned_response
12551 - );
12552 - }
12553 - // Otherwise just remove the bare URL
12554 - else {
12555 - //error_log("Removing bare URL");
12556 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
12557 - }
12558 - }
12559 - }
12560 -
12561 - // Log summary if any URLs were removed
12562 - if ($removed_count > 0) {
12563 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
12564 - } else {
12565 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
12566 - }
12567 -
12568 - // Clean up any double spaces or awkward punctuation left behind
12569 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
12570 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
12571 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
12572 -
12573 - //error_log("Final cleaned response: " . $cleaned_response);
12574 -
12575 - return trim($cleaned_response);
12576 -}
12577 -
12578 -/**
12579 - * AJAX handler to get current chat mode for a session
12580 - */
12581 -public function mxchat_get_current_chat_mode() {
12582 - // Verify nonce for security
12583 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12584 - wp_send_json_error(['message' => 'Invalid nonce']);
12585 - wp_die();
12586 - }
12587 -
12588 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12589 -
12590 - if (empty($session_id)) {
12591 - wp_send_json_error(['message' => 'Session ID missing']);
12592 - wp_die();
12593 - }
12594 -
12595 - // Get the current chat mode for this session
12596 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
12597 -
12598 - wp_send_json_success([
12599 - 'chat_mode' => $chat_mode
12600 - ]);
12601 - wp_die();
12602 -}
12603 -
12604 -
12605 4451
12606 4452 }
12607 4453 ?>