PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.0.8
MxChat – AI Chatbot & Content Generation for WordPress v1.0.8
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 +271 -11462 3.2.91.0.8 View file →
@@ -4,5092 +4,261 @@
4 4 }
5 5
6 6 class MxChat_Integrator {
7 7 private $options;
8 - private $prompts_options;
9 8 private $chat_count;
10 - private $fallbackResponse;
11 - private $productCardHtml;
12 - private $word_handler;
13 - private $last_similarity_analysis = null;
14 - private $current_valid_urls = [];
15 - private $last_vectorstore_error = null;
16 - private $is_streaming = false; // ADDED: Track if current request is streaming
17 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
18 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
19 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
20 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
21 9
22 -/**
23 - * Setup streaming headers - call this right before actually streaming
24 - * This delays header setup to allow actions/forms to return JSON responses
25 - */
26 -/**
27 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
28 - *
29 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
30 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
31 - * specific "overloaded" / "rate limit" body string. Returns immediately on
32 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
33 - *
34 - * Drop-in replacement for wp_remote_post — returns the same shape
35 - * (WP_Error or response array) so the caller's existing error-handling
36 - * code path is unchanged.
37 - *
38 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
39 - * paths (the *_response_openai / *_response_claude / etc functions).
40 - * For the *_stream variants, the cURL initial-connect happens inside a
41 - * read-chunks loop — retrying there safely (without re-emitting partial
42 - * stream chunks to the client) is a separate problem. Streaming paths
43 - * are NOT wrapped in this build; tracked as a follow-on.
44 - *
45 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
46 - * (default true). When false, behavior is identical to plain wp_remote_post.
47 - */
48 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
49 - $opts = is_array($this->options ?? null) ? $this->options : array();
50 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
51 - (string) $opts['auto_retry_on_transient_error'] !== '0';
10 +public function __construct() {
11 + $this->options = get_option('mxchat_options');
12 + $this->chat_count = get_option('mxchat_chat_count', 0);
52 13
53 - if (!$enabled) {
54 - return wp_remote_post($url, $args);
55 - }
14 + // Add WooCommerce hooks
15 + add_action('wp_insert_post', array($this, 'mxchat_handle_product_change'), 10, 3);
56 16
57 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
58 - $last_response = null;
17 + // Ensure embeddings are removed when a product is moved to trash or permanently deleted
18 + add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
19 + add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
59 20
60 - foreach ($backoffs as $i => $delay_ms) {
61 - if ($delay_ms > 0) {
62 - usleep($delay_ms * 1000);
63 - }
64 - $response = wp_remote_post($url, $args);
65 - $last_response = $response;
66 -
67 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
68 - return $response;
69 - }
70 -
71 - if (defined('WP_DEBUG') && WP_DEBUG) {
72 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
73 - : (int) wp_remote_retrieve_response_code($response);
74 - error_log(sprintf(
75 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
76 - $provider_hint ?: 'unknown',
77 - $i + 1,
78 - $code_for_log,
79 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
80 - ));
81 - }
82 - }
83 -
84 - return $last_response;
85 -}
86 -
87 -/**
88 - * Returns true if a wp_remote_post response represents a TRANSIENT
89 - * provider error worth retrying. Conservative — only retries on signals
90 - * that are very likely to clear within a few seconds.
91 - *
92 - * Transient signals:
93 - * - WP_Error with timeout / connection / dns / ssl
94 - * - HTTP 429, 502, 503, 504
95 - * - Provider-specific overload bodies (gemini "overloaded", openai
96 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
97 - *
98 - * NOT transient (return false — fail-fast):
99 - * - 200/2xx (success)
100 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
101 - * budget; the user needs to fix something)
102 - * - Any other 4xx (assume permanent unless explicitly listed above)
103 - * - 5xx other than the four listed above (e.g. 500 generic server error
104 - * is often a malformed request on our side, not a transient outage)
105 - */
106 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
107 - if (is_wp_error($response)) {
108 - $code = $response->get_error_code();
109 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
110 - || stripos((string) $response->get_error_message(), 'timed out') !== false
111 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
112 - }
113 -
114 - $status = (int) wp_remote_retrieve_response_code($response);
115 - if (in_array($status, array(429, 502, 503, 504), true)) {
116 - return true;
117 - }
118 - if ($status >= 200 && $status < 300) {
119 - return false;
120 - }
121 - // Permanent 4xx that should fail fast — even with no body.
122 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
123 - return false;
124 - }
125 -
126 - // Provider-specific body inspection for the cases where the upstream
127 - // returns 200 with an error envelope (gemini does this for overload).
128 - $body = (string) wp_remote_retrieve_body($response);
129 - if ($body === '') {
130 - return false;
131 - }
132 - $lower = strtolower($body);
133 - $hint = strtolower((string) $provider_hint);
134 -
135 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
136 - || strpos($lower, 'high demand') !== false
137 - || strpos($lower, 'model is overloaded') !== false)) {
138 - return true;
139 - }
140 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
141 - || strpos($lower, '"type":"server_error"') !== false
142 - || strpos($lower, '"code":"server_error"') !== false)) {
143 - return true;
144 - }
145 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
146 - || strpos($lower, 'overloaded_error') !== false)) {
147 - return true;
148 - }
149 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
150 - return true;
151 - }
152 -
153 - return false;
154 -}
155 -
156 -/**
157 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
158 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
159 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
160 - * collect status separately from a plain wp_remote_post array shape, so the
161 - * non-streaming helper above can't be called directly. This delegate keeps
162 - * the classification rules identical across both paths.
163 - */
164 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
165 - if ($curl_errno) {
166 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
167 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
168 - return in_array($curl_errno, array(
169 - CURLE_OPERATION_TIMEDOUT,
170 - CURLE_COULDNT_CONNECT,
171 - CURLE_COULDNT_RESOLVE_HOST,
172 - CURLE_SSL_CONNECT_ERROR,
173 - CURLE_GOT_NOTHING,
174 - CURLE_SEND_ERROR,
175 - CURLE_RECV_ERROR,
176 - ), true);
177 - }
178 -
179 - $status = (int) $http_code;
180 - if (in_array($status, array(429, 502, 503, 504), true)) {
181 - return true;
182 - }
183 - if ($status >= 200 && $status < 300) {
184 - return false;
185 - }
186 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
187 - return false;
188 - }
189 -
190 - $body = (string) $body;
191 - if ($body === '') {
192 - return false;
193 - }
194 - $lower = strtolower($body);
195 - $hint = strtolower((string) $provider_hint);
196 -
197 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
198 - || strpos($lower, 'high demand') !== false
199 - || strpos($lower, 'model is overloaded') !== false)) {
200 - return true;
201 - }
202 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
203 - || strpos($lower, '"type":"server_error"') !== false
204 - || strpos($lower, '"code":"server_error"') !== false)) {
205 - return true;
206 - }
207 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
208 - || strpos($lower, 'overloaded_error') !== false)) {
209 - return true;
210 - }
211 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
212 - return true;
213 - }
214 -
215 - return false;
216 -}
217 -
218 -/**
219 - * Whether transient-error auto-retry is enabled in admin settings.
220 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
221 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
222 - */
223 -private function mxchat_retry_enabled() {
224 - $opts = is_array($this->options ?? null) ? $this->options : array();
225 - return !isset($opts['auto_retry_on_transient_error']) ||
226 - (string) $opts['auto_retry_on_transient_error'] !== '0';
227 -}
228 -
229 -private function setup_streaming_headers() {
230 - if ($this->streaming_headers_sent || headers_sent()) {
231 - return false;
232 - }
233 -
234 - // Disable output buffering
235 - while (ob_get_level()) {
236 - ob_end_flush();
237 - }
238 -
239 - // Set headers for SSE
240 - header('Content-Type: text/event-stream');
241 - header('Cache-Control: no-cache');
242 - header('Connection: keep-alive');
243 - header('X-Accel-Buffering: no');
244 -
245 - ob_implicit_flush(true);
246 - flush();
247 -
248 - $this->streaming_headers_sent = true;
249 - return true;
250 -}
251 -
252 -/**
253 - * Class constructor
254 - */
255 -public function __construct() {
256 - $this->options = get_option('mxchat_options');
257 - $this->prompts_options = get_option('mxchat_prompts_options', array());
258 - $this->chat_count = get_option('mxchat_chat_count', 0);
259 - $this->word_handler = new MXChat_Word_Handler($this->options);
260 -
261 - // Add all action hooks
262 21 add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
263 22 add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
264 23 add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
265 24 add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
266 25 add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
267 -
268 - // Add the AJAX actions for checking if the pre-chat message was dismissed
269 - add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
270 - add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
271 - add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
272 - add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
273 - add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
274 - add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
275 -
276 - // Add REST API routes registration
277 - add_action('rest_api_init', array($this, 'register_routes'));
278 - add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
279 - add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
280 -
281 - // Rate limit action - notice we removed the old schedule setup
282 - add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
283 -
284 - // File upload and handling actions
285 - add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
286 - add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
287 - add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
288 - add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
289 -
290 - // Word document handling actions
291 - add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
292 - add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
293 - add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
294 - add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
295 - add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
296 - add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
297 -
298 - // Email handling actions
299 - add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
300 - add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
301 - add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
302 - add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
303 -
304 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
305 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
306 -
307 - // Testing panel AJAX actions
308 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
309 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
310 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
311 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
312 - // Add to your existing constructor, in the section with other AJAX actions:
313 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
314 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
315 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
316 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
317 - // Add chat mode checking actions
318 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
319 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
320 -
321 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
322 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
323 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
324 26
325 - // Auto-email transcript action
326 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
327 -
328 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
329 -
330 -
331 -}
332 -
333 -/**
334 - * Return a fresh nonce so cached pages can replace the stale one.
335 - * With `with_settings`, also returns the current behavior-gate settings so
336 - * the widget can correct stale inline-localized values (plan-32db95).
337 - */
338 -public function mxchat_refresh_nonce() {
339 - nocache_headers();
340 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
341 - if (!empty($_REQUEST['with_settings'])) {
342 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
27 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
28 + wp_schedule_event(time(), 'daily', 'mxchat_reset_rate_limits');
343 29 }
344 - wp_send_json_success($payload);
345 -}
346 30
347 -/**
348 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
349 - *
350 - * Every widget setting ships inline in page HTML via wp_localize_script, so
351 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
352 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
353 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
354 - * caches PHP can reach; this payload covers the rest — the widget requests
355 - * it on first open (via the nonce-refresh endpoints) and merges it over
356 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
357 - * nonce uses.
358 - *
359 - * Behavior gates + labels ONLY — colors stay inline because they're also
360 - * server-inline-styled, and a runtime swap would visibly flash.
361 - *
362 - * Both wp_localize_script blocks merge this exact array, so the inline and
363 - * refreshed payloads cannot drift.
364 - *
365 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
366 - * instead of trusting the instance copy.
367 - * @return array
368 - */
369 -public function get_dynamic_widget_settings($fresh = false) {
370 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
371 - if (!is_array($options)) {
372 - $options = array();
373 - }
374 - return array(
375 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
376 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
377 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
378 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
379 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
380 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
381 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
382 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
383 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
384 - // scalars to string, and (string) false === '' — which the widget's
385 - // old gate read as enabled (plan-4bba64). The filter keeps its
386 - // boolean contract; only the emitted value is stringified.
387 - 'satisfaction_rating_enabled' => apply_filters(
388 - 'mxchat_satisfaction_rating_enabled',
389 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
390 - ) ? 'on' : 'off',
391 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
392 - 'satisfaction_rating_copy' => array(
393 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
394 - 'helpful' => esc_html__('Helpful', 'mxchat'),
395 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
396 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
397 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
398 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
399 - 'send' => esc_html__('Send', 'mxchat'),
400 - 'skip' => esc_html__('Skip', 'mxchat'),
401 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
402 - ),
403 - );
31 + add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
404 32 }
405 33
406 -// In your core plugin's check_actions_for_addons method:
407 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
408 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
409 -
410 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
411 -
412 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
413 -
414 - return $result;
415 -}
416 -
417 - private function mxchat_increment_chat_count() {
418 - $chat_count = get_option('mxchat_chat_count', 0);
419 - $chat_count++;
420 - update_option('mxchat_chat_count', $chat_count);
34 +public function mxchat_handle_product_change($post_id, $post, $update) {
35 + // Ensure this is a product post type
36 + if ($post->post_type !== 'product') {
37 + return;
421 38 }
422 39
423 -function mxchat_fetch_conversation_history() {
424 - if (empty($_POST['session_id'])) {
425 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
426 - wp_die();
427 - }
428 -
429 - $session_id = sanitize_text_field($_POST['session_id']);
430 -
431 - // SECURITY FIX: Verify session ownership before retrieving data
432 - // If IP/user changed, signal frontend to reset session instead of blocking
433 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
434 -
435 - // Check if this session has an owner recorded
436 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
437 -
438 - // Update session owner if it changed (e.g. IP changed due to network switch)
439 - // The session ID itself is the authentication — if the client has it, they own it
440 - if (!$session_owner || $session_owner !== $current_user_identifier) {
441 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
442 - }
443 -
444 - $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
445 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
446 -
447 - if (empty($history)) {
448 - // Even if history is empty, return the chat mode
449 - wp_send_json_success([
450 - 'conversation' => [],
451 - 'chat_mode' => $chat_mode
452 - ]);
453 - wp_die();
454 - }
455 -
456 - wp_send_json_success([
457 - 'conversation' => $history,
458 - 'chat_mode' => $chat_mode
459 - ]);
460 - wp_die();
461 -}
462 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
463 - $history = get_option("mxchat_history_{$session_id}", []);
464 -
465 - // Check persistence setting - when OFF, only include messages from current page load
466 - $options = get_option('mxchat_options', []);
467 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
468 -
469 - // Filter history when persistence is OFF to match what the user sees
470 - if (!$persistence_enabled && $session_start_timestamp > 0) {
471 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
472 - // Include messages from this page load onwards
473 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
474 - });
475 - // Re-index array after filtering
476 - $history = array_values($history);
477 - }
478 -
479 - $formatted_history = [];
480 -
481 - // Adjusted for code-heavy conversations
482 - $max_tokens = 120000; // Context window size
483 - $reserved_tokens = 5000; // Space for system prompts + current query
484 - $current_token_count = 0;
485 -
486 - // Allowed HTML tags for content sanitization
487 - $allowed_tags = [
488 - 'pre' => ['class' => true],
489 - 'code' => ['class' => true],
490 - 'span' => ['class' => true],
491 - 'div' => ['class' => true],
492 - 'strong' => [],
493 - 'em' => []
494 - ];
495 -
496 - foreach (array_reverse($history) as $entry) {
497 - // Preserve code blocks while sanitizing other HTML
498 - $clean_content = wp_kses($entry['content'], $allowed_tags);
499 -
500 - // Detect code blocks in content
501 - $has_code = false;
502 -// Replace the HTML check with:
503 -// Allow messages that contain code blocks or are plain text
504 -if (strpos($clean_content, '<pre') === false &&
505 - strpos($clean_content, '<code') === false &&
506 - $clean_content !== strip_tags($entry['content'])) {
507 - continue;
508 -}
509 -
510 - // Skip entries that lost significant content during sanitization
511 - if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
512 - continue;
513 - }
514 -
515 - // More accurate token estimation (1 token ≈ 4 characters)
516 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
517 -
518 - // Check token budget with the new estimate
519 - if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
520 - // Try to fit partial content if it's the first entry
521 - if (empty($formatted_history)) {
522 - $clean_content = mb_substr($clean_content, 0, ($max_tokens - $reserved_tokens) * 4);
523 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
40 + // Only generate embeddings if the product is published
41 + if ($post->post_status === 'publish') {
42 + // Delay the embedding slightly to ensure all product data is available
43 + add_action('shutdown', function() use ($post_id) {
44 + $product = wc_get_product($post_id);
45 + if ($product && $product->get_price() !== '') {
46 + $this->mxchat_store_product_embedding($product);
524 47 } else {
525 - break;
48 + // Optionally, log or handle the case where product data is incomplete
49 + error_log("Product {$post_id} does not have complete data. Embedding not generated.");
526 50 }
527 - }
528 -
529 - // Add to formatted history
530 - $formatted_history[] = [
531 - 'role' => $entry['role'],
532 - 'content' => $clean_content
533 - ];
534 -
535 - $current_token_count += $token_estimate;
51 + });
536 52 }
537 -
538 - // Reverse back to maintain chronological order
539 - $formatted_history = array_reverse($formatted_history);
540 -
541 - // Add system message about code context
542 - array_unshift($formatted_history, [
543 - 'role' => 'system',
544 - 'content' => 'Preserved code blocks are marked with [CODE BLOCK PRESERVED]. '
545 - . 'Maintain formatting and syntax highlighting when referencing code.'
546 - ]);
547 -
548 - return $formatted_history;
549 53 }
550 54
551 -public function register_routes() {
552 - //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
553 -
554 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
555 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
556 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
557 - register_rest_route('mxchat/v1', '/nonce', [
558 - 'methods' => 'GET',
559 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
560 - 'permission_callback' => '__return_true',
561 - ]);
562 -
563 - register_rest_route('mxchat/v1', '/stream', [
564 - 'methods' => 'GET',
565 - 'callback' => [$this, 'mxchat_stream_events'],
566 - 'permission_callback' => [$this, 'verify_chat_session'],
567 - ]);
568 -
569 - register_rest_route('mxchat/v1', '/agent-response', [
570 - 'methods' => 'POST',
571 - 'callback' => [$this, 'mxchat_handle_agent_response'],
572 - 'permission_callback' => [$this, 'verify_slack_request'],
573 - ]);
574 -
575 - register_rest_route('mxchat/v1', '/slack-interaction', [
576 - 'methods' => 'POST',
577 - 'callback' => [$this, 'handle_slack_interaction'],
578 - 'permission_callback' => [$this, 'verify_slack_request'],
579 - ]);
580 -
581 - register_rest_route('mxchat/v1', '/slack-messages', [
582 - 'methods' => 'POST',
583 - 'callback' => [$this, 'handle_slack_messages'],
584 - 'permission_callback' => [$this, 'verify_slack_request'],
585 - ]);
586 -
587 - // Telegram webhook endpoint
588 - register_rest_route('mxchat/v1', '/telegram-webhook', [
589 - 'methods' => 'POST',
590 - 'callback' => [$this, 'handle_telegram_webhook'],
591 - 'permission_callback' => [$this, 'verify_telegram_request'],
592 - ]);
593 -
594 - //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
595 -}
596 -
597 -/**
598 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
599 - * caches it for the session and includes it on every chat-send / stream-send /
600 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
601 - * we eliminate the entire class of "first-message Access denied" failures that
602 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
603 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
604 - * lives in the HTML body.
605 - *
606 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
607 - * client browser can't be used to flood the nonce-issuance path.
608 - *
609 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
610 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
611 - * backwards-compat window so cached pages still in users' browsers don't break
612 - * mid-session.
613 - *
614 - * @since 3.2.7
615 - */
616 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
617 - $ip = '';
618 - if (!empty($_SERVER['REMOTE_ADDR'])) {
619 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
55 +public function mxchat_handle_product_delete($post_id) {
56 + if (get_post_type($post_id) !== 'product') {
57 + return;
620 58 }
621 - if ($ip !== '') {
622 - // Best-effort rate limit. WP transients with sub-second TTL are racy
623 - // (parallel bursts can squeak through before set_transient completes);
624 - // we use 2s to make the gate slightly more reliable. Real production
625 - // rate-limiting at sub-second granularity needs Redis or DB row locks
626 - // — out of scope for this endpoint, which is already cheap.
627 - $key = 'mxchat_nonce_rl_' . md5($ip);
628 - if (get_transient($key)) {
629 - return new WP_REST_Response(array(
630 - 'error' => 'rate_limited',
631 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
632 - ), 429);
633 - }
634 - set_transient($key, 1, 2);
635 - }
636 59
637 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
638 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
639 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
640 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
641 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
642 - if ( ! is_user_logged_in() ) {
643 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
644 - if ( $maybe_uid ) {
645 - wp_set_current_user( $maybe_uid );
646 - }
647 - }
60 + global $wpdb;
61 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
648 62
649 - $payload = array(
650 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
651 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
652 - );
653 -
654 - // plan-32db95: the widget's first-open refresh asks for current behavior
655 - // settings in the same round-trip, so stale inline-localized values on
656 - // cached pages get corrected without a second request. All values in
657 - // this payload already ship in public page HTML — nothing sensitive.
658 - if ($request->get_param('with_settings')) {
659 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
660 - }
661 -
662 - return new WP_REST_Response($payload, 200);
63 + // Delete the embedding associated with this product
64 + $wpdb->delete($table_name, array('source_url' => get_permalink($post_id)), array('%s'));
663 65 }
664 66
665 -/**
666 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
667 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
668 - * action (inline-localized in older cached HTML). The legacy acceptance is
669 - * a 30-day backwards-compat window — to be removed in a follow-up release
670 - * after 2026-06-27.
671 - *
672 - * @param string $posted_nonce
673 - * @return bool
674 - */
675 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
676 - if (!is_string($posted_nonce) || $posted_nonce === '') {
677 - return false;
678 - }
679 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
680 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
681 -}
67 +private function mxchat_store_product_embedding($product) {
68 + if (isset($this->options['enable_woocommerce_integration']) && $this->options['enable_woocommerce_integration'] === '1') {
682 69
683 -/**
684 - * Verify valid chat session
685 - */
686 -public function verify_chat_session($request) {
687 - $session_id = $request->get_param('session_id');
688 - if (empty($session_id)) {
689 - //error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
690 - return false;
691 - }
70 + $source_url = get_permalink($product->get_id());
71 + $regular_price = $product->get_regular_price();
72 + $sale_price = $product->get_sale_price();
73 + $price = $sale_price ?: $regular_price;
692 74
693 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
694 - return $chat_mode === 'agent';
695 -}
75 + $description = $product->get_description() . "\n\n" .
76 + "Short Description: " . $product->get_short_description() . "\n" .
77 + "Price: " . $regular_price . "\n" .
78 + "Sale Price: " . ($sale_price ?: 'N/A') . "\n" .
79 + "SKU: " . $product->get_sku();
696 80
697 -/**
698 - * Verify request is coming from Slack.
699 - *
700 - * @param WP_REST_Request $request
701 - * @return bool True if valid, false otherwise.
702 - */
703 -public function verify_slack_request($request) {
704 - // Get the Slack signing secret from your plugin options
705 - $valid_key = $this->options['live_agent_secret_key'] ?? '';
81 + global $wpdb;
82 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
706 83
707 - if (empty($valid_key)) {
708 - //error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
709 - return false;
710 - }
84 + // Delete any existing embedding for this product
85 + $wpdb->delete($table_name, array('source_url' => $source_url), array('%s'));
711 86
712 - $timestamp = $request->get_header('X-Slack-Request-Timestamp');
713 - $slack_signature = $request->get_header('X-Slack-Signature');
714 -
715 - // Verify timestamp to prevent replay attacks
716 - if (abs(time() - intval($timestamp)) > 300) {
717 - //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
718 - return false;
87 + // Submit the new content and embedding to the database
88 + MxChat_Utils::submit_content_to_db($description, $source_url, $this->options['api_key']);
719 89 }
720 -
721 - // Get raw request body from the WP_REST_Request object
722 - // (php://input may already be consumed by WordPress at this point)
723 - $request_body = $request->get_body();
724 -
725 - // Create the signature base string
726 - $sig_basestring = "v0:{$timestamp}:{$request_body}";
727 -
728 - // Calculate expected signature
729 - $my_signature = 'v0=' . hash_hmac('sha256', $sig_basestring, $valid_key);
730 -
731 - // Compare signatures
732 - return hash_equals($my_signature, $slack_signature);
733 90 }
734 91
735 -/**
736 - * Verify request is coming from Telegram.
737 - *
738 - * @param WP_REST_Request $request
739 - * @return bool True if valid, false otherwise.
740 - */
741 -public function verify_telegram_request($request) {
742 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
743 92
744 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
745 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
746 93
747 - if (empty($secret_token)) {
748 - // If no secret is configured, allow the request (for initial setup)
749 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
750 - return true;
751 - }
752 94
753 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
754 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
755 95
756 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
757 -
758 - if (empty($request_token)) {
759 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
760 - return false;
96 + private function mxchat_increment_chat_count() {
97 + $chat_count = get_option('mxchat_chat_count', 0);
98 + $chat_count++;
99 + update_option('mxchat_chat_count', $chat_count);
761 100 }
762 101
763 - // Timing-safe comparison
764 - $result = hash_equals($secret_token, $request_token);
765 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
766 - return $result;
767 -}
768 -
769 -public function mxchat_stream_events(WP_REST_Request $request) {
770 - header('Content-Type: text/event-stream');
771 - header('Cache-Control: no-cache');
772 - header('Connection: keep-alive');
773 -
774 - $session_id = sanitize_text_field($request->get_param('session_id'));
775 - $last_seen_id = sanitize_text_field($request->get_param('last_seen_id')) ?: '';
776 -
777 - if (empty($session_id)) {
778 - echo esc_html__("event: error\ndata: ", 'mxchat') . esc_html__('Missing session_id', 'mxchat') . "\n\n";
779 - flush();
780 - exit;
781 - }
782 -
783 - $history = get_option("mxchat_history_{$session_id}", []);
784 -
785 - // Filter only new messages
786 - $new_messages = array_filter($history, function ($message) use ($last_seen_id) {
787 - return !empty($message['id']) && $message['id'] > $last_seen_id;
788 - });
789 -
790 - // Send new messages if available
791 - if (!empty($new_messages)) {
792 - echo esc_html__("event: newMessages\ndata: ", 'mxchat') . json_encode(array_values($new_messages)) . "\n\n";
793 - } else {
794 - // Keep the connection alive
795 - echo esc_html__("event: keepAlive\ndata: ", 'mxchat') . "{}\n\n";
796 - }
797 - flush();
798 - exit;
799 -}
800 -
801 -
802 -
803 -
804 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
102 +public function mxchat_fetch_conversation_history_for_ajax($session_id) {
805 103 global $wpdb;
806 104 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
807 - //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
808 -
809 - // Check if this is the first message in a new session (before any other database operations)
810 - $is_new_session = false;
811 - if ($role === 'user') { // Only check for user messages, not bot responses
812 - $existing_messages = $wpdb->get_var($wpdb->prepare(
813 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
814 - $session_id
815 - ));
816 - $is_new_session = ($existing_messages == 0);
817 -
818 - // Log for debugging
819 - if ($is_new_session) {
820 - //error_log("[DEBUG] This is a NEW session - first message");
821 - }
822 - }
823 -
824 - // SECURITY FIX: Set session ownership for new sessions
825 - if ($is_new_session && $role === 'user') {
826 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
827 - $session_owner_key = "mxchat_session_owner_{$session_id}";
828 -
829 - // Only set ownership if not already set
830 - if (!get_option($session_owner_key)) {
831 - update_option($session_owner_key, $current_user_identifier, 'no');
832 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
833 - }
834 - }
835 -
836 - // 1) Extract agent name if present
837 - $agent_name = '';
838 - if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
839 - $agent_name = $matches[1];
840 - $message = str_replace("Agent: $agent_name - ", '', $message);
841 - $session_meta_key = "mxchat_agent_name_{$session_id}";
842 - if (empty(get_option($session_meta_key))) {
843 - update_option($session_meta_key, $agent_name);
844 - //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
845 - }
846 - }
847 -
848 - // 2) Generate unique message_id
849 - $message_id = uniqid();
850 - //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
851 -
852 - // 3) Determine user_id
853 - $user_id = is_user_logged_in() ? get_current_user_id() : 0;
854 -
855 - // 4) Determine user_identifier
856 - $user_identifier = $agent_name
857 - ? $agent_name
858 - : MxChat_User::mxchat_get_user_identifier();
859 -
860 - // 5) Determine displayed_name
861 - $user_email = MxChat_User::mxchat_get_user_email();
862 - $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
863 -
864 - // 6) Check for a saved email in wp_options
865 - $email_option_key = "mxchat_email_{$session_id}";
866 - $saved_email = get_option($email_option_key);
867 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
868 -
869 - // Check for a saved name in wp_options
870 - $name_option_key = "mxchat_name_{$session_id}";
871 - $saved_name = get_option($name_option_key);
872 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
873 -
874 - // If found, update DB user_email and user_name
875 - if ($saved_email || $saved_name) {
876 - $update_data = [];
877 - if ($saved_email) {
878 - $update_data['user_email'] = $saved_email;
879 - }
880 - if ($saved_name) {
881 - $update_data['user_name'] = $saved_name;
882 - }
883 -
884 - if (!empty($update_data)) {
885 - $update_res = $wpdb->update(
886 - $table_name,
887 - $update_data,
888 - ['session_id' => $session_id],
889 - array_fill(0, count($update_data), '%s'),
890 - ['%s']
891 - );
892 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
893 - }
894 - }
895 -
896 - // 7) Save to session history in wp_options
897 - $history_key = "mxchat_history_{$session_id}";
898 - $history = get_option($history_key, []);
899 - $history[] = [
900 - 'id' => $message_id,
901 - 'role' => $role,
902 - 'content' => $message,
903 - 'timestamp' => round(microtime(true) * 1000),
904 - 'agent_name' => $displayed_name,
905 - ];
906 - update_option($history_key, $history, 'no');
907 - //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
908 -
909 - // 8) Save the message to DB (INSERT)
910 - $insert_data = [
911 - 'user_id' => $user_id,
912 - 'user_identifier'=> $user_identifier,
913 - 'user_email' => $saved_email ?: $user_email,
914 - 'user_name' => $saved_name ?: '', // Add name to insert data
915 - 'session_id' => $session_id,
916 - 'role' => $role,
917 - 'message' => $message,
918 - 'timestamp' => current_time('mysql', 1),
919 - ];
920 -
921 - // IMPROVED: Handle originating page data
922 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
923 -
924 - if ($columns_exist) {
925 - if ($is_new_session && $role === 'user') {
926 - // For the first user message, set originating page data
927 -
928 - // First check if we have it from the parameter
929 - if ($originating_page && !empty($originating_page['url'])) {
930 - $insert_data['originating_page_url'] = $originating_page['url'];
931 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
932 -
933 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
934 - }
935 - // Otherwise check if it's stored in the instance property
936 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
937 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
938 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
939 -
940 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
941 -
942 - // Clear after using (= null, not unset(): unset() undeclares the property
943 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
944 - $this->pending_originating_page = null;
945 - }
946 - // Fallback to HTTP_REFERER if nothing else is available
947 - else if (isset($_SERVER['HTTP_REFERER'])) {
948 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
949 - $insert_data['originating_page_url'] = $referer_url;
950 -
951 - // Generate title from URL
952 - $parsed_url = parse_url($referer_url);
953 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
954 -
955 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
956 - $insert_data['originating_page_title'] = 'Homepage';
957 - } else {
958 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
959 - $insert_data['originating_page_title'] = ucwords(trim($title));
960 - }
961 -
962 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
963 - }
964 -
965 - // Store for this session so all messages have the same originating page
966 - if (!empty($insert_data['originating_page_url'])) {
967 - update_option("mxchat_originating_page_{$session_id}", [
968 - 'url' => $insert_data['originating_page_url'],
969 - 'title' => $insert_data['originating_page_title']
970 - ], 'no');
971 - }
972 - } else {
973 - // For subsequent messages in the session, use the stored originating page
974 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
975 - if ($stored_originating && !empty($stored_originating['url'])) {
976 - $insert_data['originating_page_url'] = $stored_originating['url'];
977 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
978 - }
979 - }
980 - }
981 105
982 - // Add RAG context if provided (for bot messages)
983 - if ($rag_context !== null && $role === 'bot') {
984 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
985 - if ($rag_context_column_exists) {
986 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
987 - }
988 - }
989 -
990 - $wpdb->insert($table_name, $insert_data);
991 - //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
992 -
993 - // 9) Send notification email if this is the first user message in a new session
994 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
995 - $this->send_new_chat_notification($session_id, array(
996 - 'identifier' => $user_identifier,
997 - 'email' => $saved_email ?: $user_email,
998 - 'ip' => $_SERVER['REMOTE_ADDR']
999 - ));
1000 - }
1001 -
1002 - // 10) Schedule delayed transcript email if enabled and message is from user
1003 - if ($wpdb->insert_id && $role === 'user') {
1004 - $this->schedule_delayed_transcript_email($session_id);
1005 - }
1006 -
1007 - //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1008 - return $message_id;
1009 -}
1010 -
1011 -private function send_new_chat_notification($session_id, $user_info = array()) {
1012 - $options = get_option('mxchat_transcripts_options');
1013 -
1014 - // Check if notifications are enabled
1015 - if (empty($options['mxchat_enable_notifications'])) {
1016 - return false;
1017 - }
1018 -
1019 - // Get notification email
1020 - $to = !empty($options['mxchat_notification_email']) ?
1021 - $options['mxchat_notification_email'] :
1022 - get_option('admin_email');
1023 -
1024 - if (!is_email($to)) {
1025 - return false;
1026 - }
1027 -
1028 - // Prepare email content
1029 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1030 -
1031 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1032 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1033 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1034 -
1035 - $message = sprintf(
1036 - "A new chat session has started on your website.\n\n" .
1037 - "Session ID: %s\n" .
1038 - "User: %s\n" .
1039 - "Email: %s\n" .
1040 - "IP Address: %s\n" .
1041 - "Time: %s\n\n" .
1042 - "View transcripts: %s",
1043 - $session_id,
1044 - $user_identifier,
1045 - $user_email,
1046 - $user_ip,
1047 - current_time('mysql'),
1048 - admin_url('admin.php?page=mxchat-transcripts')
106 + // Prepare and execute the query safely
107 + $chat_transcripts = $wpdb->get_results(
108 + $wpdb->prepare("SELECT * FROM $table_name WHERE session_id = %s ORDER BY timestamp ASC", sanitize_text_field($session_id))
1049 109 );
1050 -
1051 - // Send email
1052 - return wp_mail($to, $subject, $message);
1053 -}
1054 110
1055 -/**
1056 - * Schedule delayed transcript email for a session
1057 - * Reschedules if a new user message is received
1058 - */
1059 -private function schedule_delayed_transcript_email($session_id) {
1060 - $options = get_option('mxchat_transcripts_options');
1061 -
1062 - // Check if auto-email is enabled
1063 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1064 - return;
111 + // Check if results are empty
112 + if (empty($chat_transcripts)) {
113 + return [];
1065 114 }
1066 -
1067 - // Get notification email
1068 - $email = !empty($options['mxchat_notification_email']) ?
1069 - $options['mxchat_notification_email'] :
1070 - get_option('admin_email');
1071 -
1072 - if (!is_email($email)) {
1073 - return;
1074 - }
1075 -
1076 - // Get delay in minutes (default 30)
1077 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1078 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1079 -
1080 - // Clear any existing scheduled event for this session
1081 - $hook = 'mxchat_send_delayed_transcript';
1082 - $args = array($session_id);
1083 - $timestamp = wp_next_scheduled($hook, $args);
1084 -
1085 - if ($timestamp) {
1086 - wp_unschedule_event($timestamp, $hook, $args);
1087 - }
1088 -
1089 - // Schedule new event
1090 - $schedule_time = time() + ($delay_minutes * 60);
1091 - wp_schedule_single_event($schedule_time, $hook, $args);
1092 -}
1093 115
1094 -/**
1095 - * Check if chat messages contain contact information (email or phone number)
1096 - *
1097 - * @param array $messages Array of message objects with 'message' property
1098 - * @param object|null $session_data Session data object with user_email property
1099 - * @return bool True if contact info found, false otherwise
1100 - */
1101 -private function chat_contains_contact_info($messages, $session_data = null) {
1102 - // Check if session already has a stored email
1103 - if ($session_data && !empty($session_data->user_email)) {
1104 - return true;
116 + // Build the conversation history
117 + $conversation_history = [];
118 + foreach ($chat_transcripts as $transcript) {
119 + $conversation_history[] = [
120 + 'role' => $transcript->role,
121 + 'content' => $transcript->message
122 + ];
1105 123 }
1106 124
1107 - // Email regex pattern
1108 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1109 -
1110 - // Phone number patterns (covers various formats including international, WhatsApp style)
1111 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1112 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1113 -
1114 - // Only check user messages (not assistant responses)
1115 - foreach ($messages as $msg) {
1116 - if ($msg->role !== 'user') {
1117 - continue;
1118 - }
1119 -
1120 - $message_text = $msg->message;
1121 -
1122 - // Check for email
1123 - if (preg_match($email_pattern, $message_text)) {
1124 - return true;
1125 - }
1126 -
1127 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1128 - if (preg_match($phone_pattern, $message_text, $matches)) {
1129 - // Count actual digits to avoid matching short numbers
1130 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1131 - if (strlen($digits_only) >= 7) {
1132 - return true;
1133 - }
1134 - }
1135 - }
1136 -
1137 - return false;
125 + return $conversation_history;
1138 126 }
1139 127
1140 -/**
1141 - * Send the delayed transcript email with .txt attachment
1142 - */
1143 -public function mxchat_send_delayed_transcript($session_id) {
1144 - global $wpdb;
1145 128
1146 - $options = get_option('mxchat_transcripts_options');
1147 -
1148 - // Get notification email
1149 - $to = !empty($options['mxchat_notification_email']) ?
1150 - $options['mxchat_notification_email'] :
1151 - get_option('admin_email');
1152 -
1153 - if (!is_email($to)) {
1154 - return false;
1155 - }
1156 -
1157 - // Get all messages for this session
1158 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1159 - $messages = $wpdb->get_results($wpdb->prepare(
1160 - "SELECT role, message, timestamp FROM {$table_name}
1161 - WHERE session_id = %s
1162 - ORDER BY timestamp ASC",
1163 - $session_id
1164 - ));
1165 -
1166 - if (empty($messages)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get session metadata
1171 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1172 - $session_data = $wpdb->get_row($wpdb->prepare(
1173 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1174 - $session_id
1175 - ));
1176 -
1177 - // Check if contact info is required and if it's present
1178 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1179 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1180 - // Contact info required but not found - skip sending
1181 - return false;
1182 - }
1183 -
1184 - // Build transcript content
1185 - $transcript_content = "Chat Transcript\n";
1186 - $transcript_content .= "================\n\n";
1187 - $transcript_content .= "Session ID: " . $session_id . "\n";
1188 -
1189 - if ($session_data) {
1190 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1191 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1192 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1193 - }
1194 -
1195 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1196 -
1197 - // Add messages
1198 - foreach ($messages as $msg) {
1199 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1200 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1201 - $transcript_content .= $msg->message . "\n\n";
1202 - }
1203 -
1204 - // Create temporary file for attachment using WP_Filesystem
1205 - $upload_dir = wp_upload_dir();
1206 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1207 - global $wp_filesystem;
1208 - if (empty($wp_filesystem)) {
1209 - require_once ABSPATH . 'wp-admin/includes/file.php';
1210 - WP_Filesystem();
1211 - }
1212 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1213 -
1214 - // Prepare email
1215 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1216 -
1217 - $message = "Please find attached the full chat transcript.\n\n";
1218 - $message .= "Session ID: {$session_id}\n";
1219 -
1220 - if ($session_data) {
1221 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1222 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1223 - }
1224 -
1225 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1226 -
1227 - // Send email with attachment
1228 - $attachments = array($temp_file);
1229 - $result = wp_mail($to, $subject, $message, '', $attachments);
1230 -
1231 - // Clean up temporary file
1232 - if (file_exists($temp_file)) {
1233 - unlink($temp_file);
1234 - }
1235 -
1236 - return $result;
1237 -}
1238 -
1239 -
1240 -
1241 -public function mxchat_handle_save_email_and_response() {
1242 - //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1243 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1244 -
1245 - nocache_headers();
1246 -
1247 - // Validate nonce
1248 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1249 - //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1250 - wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1251 - wp_die();
1252 - }
1253 -
1254 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1255 - $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1256 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1257 -
1258 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
1259 -
1260 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
1261 - //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1262 - wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1263 - wp_die();
1264 - }
1265 -
1266 - // Validate name if provided (check if name field is enabled and name is required)
1267 - $options = get_option('mxchat_options', []);
1268 - $name_field_enabled = isset($options['enable_name_field']) &&
1269 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1270 -
1271 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1272 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1273 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1274 - wp_die();
1275 - }
1276 -
1277 - // 1) Always store email in wp_options
1278 - $email_option_key = "mxchat_email_{$session_id}";
1279 - update_option($email_option_key, $email, 'no');
1280 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1281 -
1282 - // Store name in wp_options if provided
1283 - if (!empty($name)) {
1284 - $name_option_key = "mxchat_name_{$session_id}";
1285 - update_option($name_option_key, $name, 'no');
1286 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1287 - }
1288 -
1289 - // 2) (Optional) Also store in DB if a row already exists
129 +private function mxchat_save_chat_message($session_id, $role, $message) {
1290 130 global $wpdb;
1291 131 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1292 132
1293 - // Make sure we have a valid placeholder in prepare
1294 - $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", $session_id);
1295 - $session_count = $wpdb->get_var($sql);
133 + $user_id = is_user_logged_in() ? get_current_user_id() : 0;
134 + $user_identifier = MxChat_User::mxchat_get_user_identifier();
135 + $user_email = MxChat_User::mxchat_get_user_email();
1296 136
1297 - //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1298 -
1299 - if ($session_count) {
1300 - // Update both user_email and user_name if row(s) exist
1301 - if (!empty($name)) {
1302 - $update_sql = $wpdb->prepare(
1303 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1304 - $email,
1305 - $name,
1306 - $session_id
1307 - );
1308 - } else {
1309 - $update_sql = $wpdb->prepare(
1310 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1311 - $email,
1312 - $session_id
1313 - );
1314 - }
1315 - $wpdb->query($update_sql);
1316 - //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1317 - } else {
1318 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
1319 - }
1320 -
1321 - // Provide success response (same as original)
1322 - $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1323 - //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1324 - wp_send_json_success(['message' => $bot_message]);
1325 - wp_die();
137 + $wpdb->insert($table_name, [
138 + 'user_id' => $user_id,
139 + 'user_identifier' => $user_identifier,
140 + 'user_email' => $user_email,
141 + 'session_id' => $session_id,
142 + 'role' => $role,
143 + 'message' => $message,
144 + 'timestamp' => current_time('mysql', 1)
145 + ]);
1326 146 }
1327 147
1328 -public function mxchat_check_email_provided() {
1329 - //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1330 -
1331 - nocache_headers();
1332 -
1333 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1334 - //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1335 - wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1336 - }
1337 -
1338 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1339 - if (empty($session_id) || $session_id === 'null') {
1340 - //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1341 - wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1342 - }
1343 -
1344 - // Check if the user is logged in
1345 - if (is_user_logged_in()) {
1346 - $current_user = wp_get_current_user();
1347 - //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1348 -
1349 - // Get user's display name for logged in users
1350 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1351 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1352 -
1353 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1354 - if (!empty($user_name)) {
1355 - $response_data['name'] = $user_name;
1356 - }
1357 -
1358 - wp_send_json_success($response_data);
1359 - }
1360 -
1361 - // Check if name field is required
1362 - $options = get_option('mxchat_options', []);
1363 - $name_field_enabled = isset($options['enable_name_field']) &&
1364 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1365 -
1366 - $email_option_key = "mxchat_email_{$session_id}";
1367 - $stored_email = get_option($email_option_key, '');
1368 -
1369 - // Check for stored name
1370 - $name_option_key = "mxchat_name_{$session_id}";
1371 - $stored_name = get_option($name_option_key, '');
1372 -
1373 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1374 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1375 -
1376 - // Check if we have email and name (if name is required)
1377 - $has_required_info = !empty($stored_email);
1378 -
1379 - if ($name_field_enabled) {
1380 - $has_required_info = $has_required_info && !empty($stored_name);
1381 - }
1382 -
1383 - if ($has_required_info) {
1384 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1385 -
1386 - $response_data = ['email' => $stored_email];
1387 - if (!empty($stored_name)) {
1388 - $response_data['name'] = $stored_name;
1389 - }
1390 -
1391 - wp_send_json_success($response_data);
1392 - } else {
1393 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
1394 - wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1395 - }
1396 -}
1397 -
1398 -/**
1399 - * Send error response in appropriate format based on streaming mode
1400 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1401 - *
1402 - * @param string $error_message The error message to display
1403 - * @param string $error_code Optional error code for debugging
1404 - */
1405 -private function send_error_response($error_message, $error_code = 'api_error') {
1406 - if ($this->is_streaming) {
1407 - echo "data: " . json_encode([
1408 - 'error' => true,
1409 - 'error_message' => $error_message,
1410 - 'error_code' => $error_code,
1411 - 'text' => $error_message,
1412 - 'message' => $error_message
1413 - ]) . "\n\n";
1414 - echo "data: [DONE]\n\n";
1415 - flush();
1416 - } else {
1417 - wp_send_json_error([
1418 - 'error_message' => $error_message,
1419 - 'error_code' => $error_code
1420 - ]);
1421 - }
1422 - wp_die();
1423 -}
1424 -
1425 148 public function mxchat_handle_chat_request() {
1426 149 global $wpdb;
1427 150
1428 - // Debug: Log incoming bot_id
1429 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1430 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1431 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1432 -
1433 - // Get bot-specific options
1434 - $bot_options = $this->get_bot_options($bot_id);
1435 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1436 -
1437 - // Check if this is a streaming request
1438 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1439 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1440 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1441 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1442 -
1443 - // ADDED: Store streaming state in class property for use in private methods
1444 - $this->is_streaming = $is_streaming;
1445 -
1446 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1447 - // This allows actions/forms to return JSON responses without header conflicts
1448 -
1449 - // Check if MX Chat Moderation is active
1450 - if (class_exists('MX_Chat_Moderation')) {
1451 - // Get user email and IP
1452 - $user_email = '';
1453 - $user_ip = $_SERVER['REMOTE_ADDR'];
1454 -
1455 - // If user is logged in, get their email
1456 - if (is_user_logged_in()) {
1457 - $current_user = wp_get_current_user();
1458 - $user_email = $current_user->user_email;
1459 - }
1460 -
1461 - // Create ban handler instance
1462 - $ban_handler = new MX_Chat_Ban_Handler();
1463 -
1464 - // Check if user is banned by IP
1465 - if ($ban_handler->check_ban($user_ip, 'ip')) {
1466 - wp_send_json([
1467 - 'success' => false,
1468 - 'message' => esc_html__('Access denied. Your IP address has been banned.', 'mxchat'),
1469 - 'status' => 'banned'
1470 - ]);
1471 - wp_die();
1472 - }
1473 -
1474 - // If user is logged in, also check email
1475 - if (!empty($user_email) && $ban_handler->check_ban($user_email, 'email')) {
1476 - wp_send_json([
1477 - 'success' => false,
1478 - 'message' => esc_html__('Access denied. Your email address has been banned.', 'mxchat'),
1479 - 'status' => 'banned'
1480 - ]);
1481 - wp_die();
1482 - }
1483 - }
1484 -
1485 - $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1486 - $this->productCardHtml = '';
1487 -
1488 - // Get the actual WordPress user ID if logged in
1489 - $is_logged_in = is_user_logged_in();
1490 - if ($is_logged_in) {
1491 - $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1492 - } else {
1493 - // For logged-out users, use your existing identifier method
1494 - $user_id = $this->mxchat_get_user_identifier();
1495 - }
1496 -
1497 151 // Get and sanitize the user identifier
152 + $user_id = $this->mxchat_get_user_identifier();
1498 153 $user_id = sanitize_key($user_id);
1499 154
1500 - // Check rate limit using new settings structure
1501 - $rate_limit_result = $this->check_rate_limit();
155 + // Manage rate limiting
156 + $rate_limit_transient_key = 'mxchat_chat_limit_' . $user_id;
157 + $chat_count = get_transient($rate_limit_transient_key);
158 + $session_transient_key = 'mxchat_chat_session_' . $user_id;
159 + $session_id = get_transient($session_transient_key);
1502 160
1503 - if ($rate_limit_result !== true) {
1504 - wp_send_json([
1505 - 'success' => false,
1506 - 'message' => $rate_limit_result['message'],
1507 - 'status' => 'rate_limit_exceeded'
1508 - ]);
1509 - wp_die();
161 + if ($chat_count === false) {
162 + $chat_count = 0;
1510 163 }
1511 164
1512 - // Rest of your existing code...
1513 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1514 -
1515 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1516 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1517 - // the frontend FormData.append() to stringify a null session_id into the literal
1518 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1519 - // ghost sessions that group every visitor's first message under one row.
1520 - if ($session_id === 'null' || $session_id === 'undefined') {
1521 - $session_id = '';
165 + if ($session_id === false) {
166 + $session_id = uniqid('mxchat_chat_', true);
167 + set_transient($session_transient_key, $session_id, DAY_IN_SECONDS); // Store session ID for a day
1522 168 }
1523 169
1524 - if (empty($session_id)) {
1525 - wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1526 - wp_die();
1527 - }
170 + $rate_limit_option = isset($this->options['rate_limit']) ? $this->options['rate_limit'] : 'unlimited';
1528 171
1529 - // Update session owner if it changed (e.g. IP changed due to network switch)
1530 - // The session ID itself is the authentication — if the client has it, they own it
1531 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1532 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
172 + // Check if rate limit is not 'unlimited'
173 + if ($rate_limit_option !== 'unlimited') {
174 + $rate_limit = intval($rate_limit_option);
1533 175
1534 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1535 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1536 - }
1537 -
1538 - // Validate and sanitize the incoming message
1539 - if (empty($_POST['message'])) {
1540 - wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1541 - wp_die();
1542 - }
1543 -
1544 -
1545 - // Track originating page for first message in session
1546 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1547 -
1548 - // Check if originating page columns exist
1549 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1550 -
1551 - if ($columns_exist) {
1552 - // Check if this session already has messages
1553 - $message_count = $wpdb->get_var($wpdb->prepare(
1554 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1555 - $session_id
1556 - ));
1557 -
1558 - // If this is the first message in the session
1559 - if ($message_count == 0) {
1560 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1561 - $originating_url = '';
1562 - $originating_title = '';
1563 -
1564 - // Try to get from POST data first (sent by JavaScript)
1565 - if (isset($_POST['current_page_url'])) {
1566 - $originating_url = esc_url_raw($_POST['current_page_url']);
1567 - $originating_title = isset($_POST['current_page_title'])
1568 - ? sanitize_text_field($_POST['current_page_title'])
1569 - : '';
1570 - }
1571 - // Fallback to HTTP_REFERER if not provided by JavaScript
1572 - else if (isset($_SERVER['HTTP_REFERER'])) {
1573 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1574 - }
1575 -
1576 - // Generate title if we have URL but no title
1577 - if ($originating_url && empty($originating_title)) {
1578 - $parsed_url = parse_url($originating_url);
1579 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1580 -
1581 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1582 - $originating_title = 'Homepage';
1583 - } else {
1584 - // Clean up the path to make a readable title
1585 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1586 - $originating_title = ucwords(trim($originating_title));
1587 - }
1588 - }
1589 -
1590 - // Store for later use when saving the message
1591 - $this->pending_originating_page = [
1592 - 'url' => $originating_url,
1593 - 'title' => $originating_title
1594 - ];
1595 - }
1596 - }
1597 -
1598 -
1599 -
1600 - // Get page context if provided
1601 - $page_context = null;
1602 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1603 - $page_context_raw = stripslashes($_POST['page_context']);
1604 - $page_context = json_decode($page_context_raw, true);
1605 -
1606 - // Validate page context structure
1607 - if (is_array($page_context) &&
1608 - isset($page_context['url']) &&
1609 - isset($page_context['title']) &&
1610 - isset($page_context['content'])) {
1611 -
1612 - // Sanitize page context
1613 - $page_context['url'] = esc_url_raw($page_context['url']);
1614 - $page_context['title'] = sanitize_text_field($page_context['title']);
1615 - $page_context['content'] = wp_kses_post($page_context['content']);
1616 - } else {
1617 - $page_context = null;
1618 - }
1619 - }
1620 -
1621 - // Modify the message sanitization to preserve PHP tags in code blocks
1622 - $allowed_tags = [
1623 - 'pre' => [],
1624 - 'code' => ['class' => true],
1625 - 'span' => ['class' => true],
1626 - 'div' => ['class' => true],
1627 - ];
1628 -
1629 - // First preserve code blocks
1630 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1631 - return htmlspecialchars_decode($matches[0]);
1632 - }, $_POST['message']);
1633 -
1634 - // Then apply sanitization
1635 - $message = wp_kses($message, $allowed_tags);
1636 -
1637 - // Preserve code blocks from markdown conversion
1638 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1639 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1640 -
1641 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1642 - // Always initialize testing data for admins (no toggle needed)
1643 - $testing_data = null;
1644 - if (current_user_can('administrator')) {
1645 - // For vision messages, use the original user message for the query display
1646 - $query_for_testing = $message;
1647 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1648 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1649 - }
1650 -
1651 - $testing_data = [
1652 - 'query' => $query_for_testing,
1653 - 'timestamp' => time(),
1654 - 'top_matches' => [],
1655 - 'action_matches' => [], // Initialize action matches array
1656 - 'page_context' => $page_context, // Include page context in testing data
1657 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1658 - 'bot_id' => $bot_id // Include bot ID in testing data
1659 - ];
1660 -
1661 - // Get similarity threshold from bot options or default options
1662 - $similarity_threshold = isset($current_options['similarity_threshold'])
1663 - ? ((int) $current_options['similarity_threshold']) / 100
1664 - : 0.35;
1665 -
1666 - $testing_data['similarity_threshold'] = $similarity_threshold;
1667 -
1668 - // Determine knowledge base type using bot-specific config
1669 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1670 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1671 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1672 - }
1673 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
1674 -
1675 - // Add debug before and after:
1676 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1677 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1678 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
1679 -
1680 -
1681 - // If the pre-processing returned a result (not the original message), use it directly
1682 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1683 - // Save the AI response
1684 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1685 -
1686 - // Save HTML content if provided
1687 - if (!empty($pre_processed_result['html'])) {
1688 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1689 - }
1690 -
1691 - // Add testing data if admin
1692 - $response_data = [
1693 - 'text' => $pre_processed_result['text'],
1694 - 'html' => $pre_processed_result['html'] ?? '',
1695 - 'session_id' => $session_id
1696 - ];
1697 -
1698 - if ($testing_data !== null) {
1699 - $response_data['testing_data'] = $testing_data;
1700 - }
1701 -
1702 - wp_send_json($response_data);
176 + if ($chat_count >= $rate_limit) {
177 + wp_send_json_error('Rate limit exceeded. Please try again later.');
1703 178 wp_die();
1704 179 }
1705 -
1706 - // Save the user's message - handle vision processed messages differently
1707 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1708 - // For vision messages, save the original user message with image indicator
1709 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1710 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1711 - $image_count = intval($_POST['vision_images_count']);
1712 - $original_message .= " [{$image_count} image(s)]";
1713 - }
1714 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1715 - } else {
1716 - // Regular message - save as normal
1717 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1718 - }
1719 -
1720 -
1721 - if (is_email($message)) {
1722 - // Add the email to Loops
1723 - $this->add_email_to_loops($message);
1724 -
1725 - // Get the user's success message instruction using current_options
1726 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1727 -
1728 - // Set instruction for AI using the user's success message
1729 - $this->current_action_instruction = $user_success_message;
1730 -
1731 - // Clear the email capture transient since we got the email
1732 - delete_transient('mxchat_email_capture_' . $user_id);
1733 - }
1734 -
1735 - // Check if we're in an email capture flow but user hasn't provided email yet
1736 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1737 - // Check if the message contains an email (not the whole message being an email)
1738 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1739 - $extracted_email = $matches[0];
1740 -
1741 - // Add the extracted email to Loops
1742 - $this->add_email_to_loops($extracted_email);
1743 -
1744 - // Get the user's success message instruction using current_options
1745 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1746 -
1747 - // Set instruction for AI using the user's success message
1748 - $this->current_action_instruction = $user_success_message;
1749 -
1750 - // Clear the email capture transient since we got the email
1751 - delete_transient('mxchat_email_capture_' . $user_id);
1752 - }
1753 - // If no email found but we're in capture mode, remind them
1754 - else {
1755 - // Get the original instruction to remind them using current_options
1756 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1757 - $this->current_action_instruction = $original_instruction;
1758 - }
1759 - }
1760 -
1761 - $intent_info = '';
1762 -
1763 - // Check chat mode
1764 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1765 -
1766 - // Handle agent mode
1767 - // Handle agent mode
1768 - if ($chat_mode === 'agent') {
1769 - // First, check for switch intent before doing anything else
1770 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1771 -
1772 - // Capture action analysis for testing panel after intent check
1773 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1774 - $testing_data['action_matches'] = $this->last_action_analysis;
1775 - }
1776 -
1777 - // Around line 506, in the agent mode handling section:
1778 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1779 - // Update chat mode first
1780 - update_option("mxchat_mode_{$session_id}", 'ai');
1781 -
1782 - // Clear any existing PDF context to start fresh
1783 - $this->clear_pdf_transients($session_id);
1784 -
1785 - // Prepare clean switch response with explicit chat_mode
1786 - $response_data = [
1787 - 'text' => $this->fallbackResponse['text'],
1788 - 'html' => $this->fallbackResponse['html'] ?? '',
1789 - 'session_id' => $session_id,
1790 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1791 - ];
1792 -
1793 - if ($testing_data !== null) {
1794 - $response_data['testing_data'] = $testing_data;
1795 - }
1796 -
1797 - // Save the mode switch message
1798 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1799 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1800 -
1801 - // Send response and exit
1802 - wp_send_json($response_data);
1803 - wp_die();
1804 - } elseif (!$intent_matched) {
1805 - // No intent matched, handle live agent message
1806 - try {
1807 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1808 -
1809 - $agent_response = [
1810 - 'status' => 'waiting_for_agent',
1811 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1812 - ];
1813 -
1814 - if ($testing_data !== null) {
1815 - $agent_response['testing_data'] = $testing_data;
1816 - }
1817 -
1818 - wp_send_json_success($agent_response);
1819 - } catch (\Exception $e) {
1820 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1821 - }
1822 - wp_die();
1823 - }
1824 - }
1825 -
1826 - // Step 1: Check for new PDF URL in the message
1827 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1828 - $new_pdf_url = $matches[0];
1829 -
1830 - // Check if this is likely a PDF-related request
1831 - $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1832 - $is_pdf_request = false;
1833 -
1834 - foreach ($pdf_keywords as $keyword) {
1835 - if (stripos($message, $keyword) !== false) {
1836 - $is_pdf_request = true;
1837 - break;
1838 - }
1839 - }
1840 -
1841 - // If it looks like a PDF request or we're waiting for a PDF URL
1842 - if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1843 - // Validate HTTPS
1844 - if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1845 - // Extract filename from URL
1846 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1847 -
1848 - // Clear previous PDF transients
1849 - $this->clear_pdf_transients($session_id);
1850 -
1851 - // Process new PDF using current_options
1852 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
1853 - $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1854 -
1855 - if ($embeddings === 'too_many_pages') {
1856 - $error_text = sprintf(
1857 - $current_options['pdf_intent_error_text'] ??
1858 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1859 - $max_pages
1860 - );
1861 - $this->fallbackResponse['text'] = $error_text;
1862 - } elseif ($embeddings) {
1863 - // Store new PDF information
1864 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1865 -
1866 - // If the filename is generic, create a more descriptive one
1867 - if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1868 - strpos($pdf_filename, '.php') !== false) {
1869 - $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1870 - }
1871 -
1872 - set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1873 - set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1874 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1875 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1876 -
1877 - $success_text = $current_options['pdf_intent_success_text'] ??
1878 - esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1879 -
1880 - $pdf_response = [
1881 - 'success' => true,
1882 - 'message' => $success_text,
1883 - 'data' => [
1884 - 'filename' => $pdf_filename
1885 - ]
1886 - ];
1887 -
1888 - if ($testing_data !== null) {
1889 - $pdf_response['testing_data'] = $testing_data;
1890 - }
1891 -
1892 - wp_send_json($pdf_response);
1893 - wp_die();
1894 - } else {
1895 - $error_text = $current_options['pdf_intent_error_text'] ??
1896 - esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1897 - $this->fallbackResponse['text'] = $error_text;
1898 - }
1899 -
1900 - $pdf_error_response = [
1901 - 'success' => false,
1902 - 'message' => $this->fallbackResponse['text']
1903 - ];
1904 -
1905 - if ($testing_data !== null) {
1906 - $pdf_error_response['testing_data'] = $testing_data;
1907 - }
1908 -
1909 - wp_send_json($pdf_error_response);
1910 - wp_die();
1911 - }
1912 - }
1913 - }
1914 -
1915 -
1916 - // Step 2: Detect intent and handle intent-based responses
1917 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1918 -
1919 - // Capture action analysis for testing panel after intent check
1920 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1921 - $testing_data['action_matches'] = $this->last_action_analysis;
1922 - }
1923 -
1924 - // Step 3: Handle the intent result appropriately
1925 - if ($intent_result !== false) {
1926 - // Intent was matched - ALWAYS send as JSON response, never streaming
1927 -
1928 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1929 - // Intent returned a direct response array
1930 - $response_data = [
1931 - 'text' => $intent_result['text'] ?? '',
1932 - 'html' => $intent_result['html'] ?? '',
1933 - 'session_id' => $session_id
1934 - ];
1935 -
1936 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1937 - if (isset($intent_result['chat_mode'])) {
1938 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1939 - }
1940 -
1941 - if ($testing_data !== null) {
1942 - $response_data['testing_data'] = $testing_data;
1943 - }
1944 -
1945 - wp_send_json($response_data);
1946 - wp_die();
1947 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1948 - // Intent returned true and set fallbackResponse
1949 -
1950 - // SAVE TO TRANSCRIPT
1951 - if (!empty($this->fallbackResponse['text'])) {
1952 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1953 - }
1954 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1955 - if (!empty($this->fallbackResponse['html'])) {
1956 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1957 - }
1958 -
1959 - $response_data = [
1960 - 'text' => $this->fallbackResponse['text'] ?? '',
1961 - 'html' => $this->fallbackResponse['html'] ?? '',
1962 - 'session_id' => $session_id
1963 - ];
1964 -
1965 - if (isset($this->fallbackResponse['chat_mode'])) {
1966 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1967 - }
1968 -
1969 - if ($testing_data !== null) {
1970 - $response_data['testing_data'] = $testing_data;
1971 - }
1972 -
1973 - wp_send_json($response_data);
1974 - wp_die();
1975 - }
1976 - }
1977 -
1978 - // If we get here, no intent matched OR the intent didn't provide a usable response
1979 -
1980 - // Step 4: Generate AI response
1981 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1982 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
1983 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
1984 - $this->mxchat_increment_chat_count();
1985 -
1986 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1987 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
1988 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
1989 -
1990 - // Check if the embedding generation returned an error
1991 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1992 - $error_message = $user_message_embedding['error'];
1993 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
1994 -
1995 - // FIXED: Send error in appropriate format based on streaming mode
1996 - if ($is_streaming) {
1997 - echo "data: " . json_encode([
1998 - 'error' => true,
1999 - 'error_message' => $error_message,
2000 - 'error_code' => $error_code,
2001 - 'text' => $error_message,
2002 - 'message' => $error_message
2003 - ]) . "\n\n";
2004 - echo "data: [DONE]\n\n";
2005 - flush();
2006 - } else {
2007 - wp_send_json_error([
2008 - 'error_message' => $error_message,
2009 - 'error_code' => $error_code
2010 - ]);
2011 - }
2012 - wp_die();
2013 - }
2014 -
2015 - // Check if the embedding is valid
2016 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2017 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2018 -
2019 - // FIXED: Send error in appropriate format based on streaming mode
2020 - if ($is_streaming) {
2021 - echo "data: " . json_encode([
2022 - 'error' => true,
2023 - 'error_message' => $error_message,
2024 - 'error_code' => 'invalid_embedding',
2025 - 'text' => $error_message,
2026 - 'message' => $error_message
2027 - ]) . "\n\n";
2028 - echo "data: [DONE]\n\n";
2029 - flush();
2030 - } else {
2031 - wp_send_json_error([
2032 - 'error_message' => $error_message,
2033 - 'error_code' => 'invalid_embedding'
2034 - ]);
2035 - }
2036 - wp_die();
2037 - }
2038 -
2039 - // Build context with both knowledge base and PDF content if available
2040 - $context_content = "User asked: '{$message}'\n\n";
2041 -
2042 - // Add action instruction if present (add this right after the above line)
2043 - if (!empty($this->current_action_instruction)) {
2044 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2045 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2046 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2047 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2048 -
2049 - // Clear the instruction after using it
2050 - $this->current_action_instruction = null;
2051 - }
2052 -
2053 -
2054 - // Add page context if available and contextual awareness is enabled using current_options
2055 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2056 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2057 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2058 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2059 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2060 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2061 - }
2062 -
2063 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2064 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2065 -
2066 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2067 - // Use fresh options to ensure we get the latest setting value
2068 - $fresh_options = get_option('mxchat_options', []);
2069 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2070 -
2071 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2072 - if ($citation_links_enabled && !empty($system_instructions)) {
2073 - preg_match_all(
2074 - '#\bhttps?://[^\s<>"\']+#i',
2075 - $system_instructions,
2076 - $system_instruction_urls
2077 - );
2078 -
2079 - if (!empty($system_instruction_urls[0])) {
2080 - // Merge with existing valid URLs
2081 - $this->current_valid_urls = array_merge(
2082 - $this->current_valid_urls,
2083 - $system_instruction_urls[0]
2084 - );
2085 - // Remove duplicates
2086 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2087 -
2088 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2089 - }
2090 - }
2091 -
2092 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2093 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2094 - // Update testing data with the REAL similarity analysis
2095 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2096 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2097 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2098 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2099 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2100 -}
2101 -// ===== END SIMILARITY DATA CAPTURE =====
2102 -
2103 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2104 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2105 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2106 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2107 -}
2108 -
2109 - if (!empty($relevant_content)) {
2110 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2111 - } else {
2112 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2113 - }
2114 -
2115 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2116 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2117 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2118 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2119 - foreach ($this->current_valid_urls as $url) {
2120 - $context_content .= "- " . $url . "\n";
2121 - }
2122 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2123 - $context_content .= "===== END APPROVED URLS =====\n\n";
2124 - }
2125 -
2126 - // Check for and include PDF content
2127 - $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2128 - $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2129 - $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
2130 - if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
2131 - $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
2132 - if (!empty($relevant_pdf_pages)) {
2133 - $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
2134 - foreach ($relevant_pdf_pages as $page_data) {
2135 - $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2136 - }
2137 - $context_content .= "\n";
2138 - }
2139 - }
2140 -
2141 - // Check for and include Word content
2142 - $word_url = get_transient('mxchat_word_url_' . $session_id);
2143 - $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
2144 - $word_filename = get_transient('mxchat_word_filename_' . $session_id);
2145 - if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
2146 - $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
2147 - if (!empty($relevant_word_chunks)) {
2148 - $context_content .= "Relevant content from Word document '{$word_filename}':\n";
2149 - foreach ($relevant_word_chunks as $chunk_data) {
2150 - $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
2151 - }
2152 - $context_content .= "\n";
2153 - }
2154 - }
2155 -
2156 - $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2157 -
2158 - // Extract model from current options for bot-specific model support
2159 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2160 -
2161 - $response = $this->mxchat_generate_response(
2162 - $context_content,
2163 - $current_options['api_key'] ?? $this->options['api_key'],
2164 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2165 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2166 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2167 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2168 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2169 - $conversation_history,
2170 - $is_streaming,
2171 - $session_id,
2172 - $testing_data,
2173 - $selected_model
2174 - );
2175 -
2176 - // Handle streaming vs non-streaming responses
2177 - if ($is_streaming) {
2178 - // Check if streaming actually happened or if it fell back to regular response
2179 - if ($response === true) {
2180 - wp_die();
2181 - }
2182 - // If we get here, streaming fell back to regular response, continue
2183 - // But if there's an error, we need to send it as SSE format since headers are already set
2184 - if (is_array($response) && isset($response['error'])) {
2185 - $error_message = $response['error'];
2186 - $error_code = $response['error_code'] ?? 'api_error';
2187 - // Send error in SSE format that the client JS can handle
2188 - echo "data: " . json_encode([
2189 - 'error' => true,
2190 - 'error_message' => $error_message,
2191 - 'error_code' => $error_code,
2192 - 'text' => $error_message, // Also include as text for fallback handling
2193 - 'message' => $error_message
2194 - ]) . "\n\n";
2195 - echo "data: [DONE]\n\n";
2196 - flush();
2197 - wp_die();
2198 - }
2199 - }
2200 -
2201 - // Check if the response is an error array (non-streaming mode)
2202 - if (is_array($response) && isset($response['error'])) {
2203 - wp_send_json_error([
2204 - 'error_message' => $response['error'],
2205 - 'error_code' => $response['error_code'] ?? 'api_error'
2206 - ]);
2207 - wp_die();
2208 - }
2209 -
2210 - // DEBUG: Check what we have
2211 - //error_log("=== BEFORE URL VALIDATION ===");
2212 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2213 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2214 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2215 -
2216 - // If we get here, the response is valid text - now validate URLs
2217 - if (!empty($this->current_valid_urls)) {
2218 - //error_log("CALLING validate_and_clean_urls");
2219 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2220 - } else {
2221 - //error_log("SKIPPING validation - current_valid_urls is empty");
2222 - }
2223 - // ===== END URL VALIDATION =====
2224 -
2225 - // Prepare RAG context data for storage (only include documents used for context)
2226 - $rag_context_for_storage = null;
2227 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2228 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2229 -
2230 - if ($has_rag_data || $has_action_data) {
2231 - $rag_context_for_storage = [];
2232 -
2233 - // Add RAG/source data if available
2234 - if ($has_rag_data) {
2235 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2236 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2237 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2238 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2239 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2240 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2241 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2242 - }
2243 -
2244 - // Add action analysis data if available
2245 - if ($has_action_data) {
2246 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2247 - }
2248 - }
2249 -
2250 - // Save the cleaned response with RAG context
2251 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2252 -
2253 - // Step 5: Save additional content if available
2254 - if (!empty($this->productCardHtml)) {
2255 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2256 - }
2257 -
2258 - if (!empty($this->fallbackResponse['html'])) {
2259 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2260 - }
2261 -
2262 - // Step 6: Return the response
2263 - // DEBUG: Check if newlines exist in the response
2264 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2265 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2266 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2267 -
2268 - $response_data = [
2269 - 'text' => $response,
2270 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2271 - 'session_id' => $session_id
2272 - ];
2273 -
2274 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2275 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2276 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2277 - }
2278 -
2279 - // Also pass it as a top-level field so JS can show a better error message to admins
2280 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2281 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2282 - }
2283 -
2284 - // Always add testing data for admins (no toggle needed)
2285 - if ($testing_data !== null) {
2286 - $response_data['testing_data'] = $testing_data;
2287 - }
2288 -
2289 - wp_send_json($response_data);
2290 - wp_die();
2291 -}
2292 -
2293 -/**
2294 - * Get bot-specific options for multi-bot functionality
2295 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2296 - */
2297 -// Also debug the bot options retrieval
2298 -private function get_bot_options($bot_id = 'default') {
2299 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2300 -
2301 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2302 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2303 - return array();
180 + set_transient($rate_limit_transient_key, $chat_count + 1, DAY_IN_SECONDS);
2304 181 }
2305 -
2306 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2307 -
2308 - if (!empty($bot_options)) {
2309 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2310 - if (isset($bot_options['similarity_threshold'])) {
2311 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2312 - }
2313 - }
2314 -
2315 - return is_array($bot_options) ? $bot_options : array();
2316 -}
2317 182
2318 -/**
2319 - * Get bot-specific Pinecone configuration
2320 - * Used in the knowledge retrieval functions
2321 - */
2322 -// Also add debugging to your get_bot_pinecone_config function
2323 -private function get_bot_pinecone_config($bot_id = 'default') {
2324 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2325 -
2326 - // If default bot or multi-bot add-on not active, use default Pinecone config
2327 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2328 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2329 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2330 - $config = array(
2331 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2332 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2333 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2334 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2335 - );
2336 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2337 - return $config;
2338 - }
2339 -
2340 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2341 -
2342 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2343 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2344 -
2345 - if (!empty($bot_pinecone_config)) {
2346 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2347 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2348 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2349 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2350 - } else {
2351 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2352 - }
2353 -
2354 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2355 -}
2356 -
2357 -
2358 -// Updated function to check intents and invoke the callback function
2359 -private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2360 - global $wpdb;
2361 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2362 -
2363 - // Get the current bot_id
2364 - $current_bot_id = $this->get_current_bot_id($session_id);
2365 -
2366 - // Generate the user embedding
2367 - $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2368 -
2369 - // Check if embedding generation returned an error
2370 - if (is_array($user_embedding) && isset($user_embedding['error'])) {
2371 - $error_message = $user_embedding['error'];
2372 - $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2373 -
2374 - // FIXED: Send error in appropriate format based on streaming mode
2375 - if ($this->is_streaming) {
2376 - echo "data: " . json_encode([
2377 - 'error' => true,
2378 - 'error_message' => $error_message,
2379 - 'error_code' => $error_code,
2380 - 'text' => $error_message,
2381 - 'message' => $error_message
2382 - ]) . "\n\n";
2383 - echo "data: [DONE]\n\n";
2384 - flush();
2385 - } else {
2386 - wp_send_json_error([
2387 - 'error_message' => $error_message,
2388 - 'error_code' => $error_code
2389 - ]);
2390 - }
183 + // Validate and sanitize the incoming message
184 + if (!isset($_POST['message'])) {
185 + wp_send_json_error('No message received');
2391 186 wp_die();
2392 187 }
2393 188
2394 - // Check if embedding is valid
2395 - if (!is_array($user_embedding) || empty($user_embedding)) {
2396 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2397 -
2398 - // FIXED: Send error in appropriate format based on streaming mode
2399 - if ($this->is_streaming) {
2400 - echo "data: " . json_encode([
2401 - 'error' => true,
2402 - 'error_message' => $error_message,
2403 - 'error_code' => 'invalid_embedding',
2404 - 'text' => $error_message,
2405 - 'message' => $error_message
2406 - ]) . "\n\n";
2407 - echo "data: [DONE]\n\n";
2408 - flush();
2409 - } else {
2410 - wp_send_json_error([
2411 - 'error_message' => $error_message,
2412 - 'error_code' => 'invalid_embedding'
2413 - ]);
2414 - }
189 + $message = sanitize_text_field($_POST['message']);
190 + if (empty($message)) {
191 + wp_send_json_error('Message is empty or invalid.');
2415 192 wp_die();
2416 193 }
2417 194
2418 - // Fetch intents from the database
2419 - $table_name = $wpdb->prefix . 'mxchat_intents';
2420 - if ($chat_mode === 'agent') {
2421 - $query = $wpdb->prepare(
2422 - "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2423 - 'mxchat_handle_switch_to_chatbot_intent'
2424 - );
2425 - $intents = $wpdb->get_results($query);
2426 - } else {
2427 - $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2428 - }
195 + // Initialize the variable with the original message
196 + $message_with_order_details = $message;
2429 197
2430 - if (empty($intents)) {
2431 - return false;
2432 - }
198 + // Check if the user asked about orders
199 + if (MxChat_WooCommerce::mxchat_is_order_related_query($message)) {
200 + $order_details = MxChat_WooCommerce::mxchat_fetch_user_orders_details();
2433 201
2434 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2435 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2436 - $phrases_by_intent = [];
2437 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2438 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2439 - foreach ($all_phrases as $p) {
2440 - $phrases_by_intent[$p->intent_id][] = $p;
2441 - }
2442 - }
2443 -
2444 - $highest_similarity = -INF;
2445 - $matched_intent = null;
2446 -
2447 - // Array to store action analysis for testing panel
2448 - $action_analysis = [];
2449 -
2450 - foreach ($intents as $intent) {
2451 - // Additional check for enabled state
2452 - $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 - if (!$is_enabled) {
2454 - continue;
2455 - }
2456 -
2457 - // Check if this action is enabled for the current bot
2458 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2459 - continue;
2460 - }
2461 -
2462 - $best_similarity = -INF;
2463 - $matched_phrase_text = '';
2464 -
2465 - // Check legacy embedding vector (existing behavior)
2466 - $intent_embedding_serialized = $intent->embedding_vector;
2467 - $intent_embedding = $intent_embedding_serialized
2468 - ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2469 - : null;
2470 -
2471 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2472 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2473 - if ($legacy_similarity > $best_similarity) {
2474 - $best_similarity = $legacy_similarity;
2475 - $matched_phrase_text = 'legacy';
202 + // If order details are available, append them to the user's message
203 + if (!empty($order_details)) {
204 + $message_with_order_details = $message . "\n\n" . $order_details;
2476 205 }
2477 206 }
2478 207
2479 - // Check individual phrase vectors
2480 - if (isset($phrases_by_intent[$intent->id])) {
2481 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2482 - $phrase_embedding = $phrase_row->embedding_vector
2483 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2484 - : null;
2485 - if (!is_array($phrase_embedding)) {
2486 - continue;
2487 - }
2488 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2489 - if ($phrase_similarity > $best_similarity) {
2490 - $best_similarity = $phrase_similarity;
2491 - $matched_phrase_text = $phrase_row->phrase;
2492 - }
2493 - }
2494 - }
2495 208
2496 - // Skip if no valid embedding was found at all
2497 - if ($best_similarity === -INF) {
2498 - continue;
2499 - }
209 + // Save the combined message to the database
210 + $this->mxchat_save_chat_message($session_id, 'user', $message_with_order_details);
2500 211
2501 - $similarity = $best_similarity;
2502 - $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2503 -
2504 - // Store action analysis data for testing panel
2505 - $action_analysis[] = [
2506 - 'intent_label' => $intent->intent_label,
2507 - 'callback_function' => $intent->callback_function,
2508 - 'similarity' => round($similarity, 4),
2509 - 'similarity_percentage' => round($similarity * 100, 2),
2510 - 'threshold' => $intent_threshold,
2511 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2512 - 'above_threshold' => $similarity >= $intent_threshold,
2513 - 'matched_phrase' => $matched_phrase_text,
2514 - 'triggered' => false // Will be updated below if this intent is triggered
2515 - ];
2516 -
2517 - if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2518 - $highest_similarity = $similarity;
2519 - $matched_intent = $intent;
2520 - }
2521 - }
2522 -
2523 - // Mark the triggered action if any
2524 - if ($matched_intent) {
2525 - foreach ($action_analysis as &$action) {
2526 - if ($action['intent_label'] === $matched_intent->intent_label) {
2527 - $action['triggered'] = true;
2528 - break;
2529 - }
2530 - }
2531 - }
2532 -
2533 - // Sort actions by similarity (highest first) and store for testing panel
2534 - usort($action_analysis, function($a, $b) {
2535 - return $b['similarity'] <=> $a['similarity'];
2536 - });
2537 -
2538 - // Store action analysis for testing panel capture
2539 - $this->last_action_analysis = $action_analysis;
2540 -
2541 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2542 - if ($matched_intent) {
2543 - // If the callback is a method on this instance (core callback), call it directly
2544 - if (method_exists($this, $matched_intent->callback_function)) {
2545 - $callback_result = call_user_func(
2546 - [$this, $matched_intent->callback_function],
2547 - $message,
2548 - $user_id,
2549 - $session_id,
2550 - $matched_intent,
2551 - $user_context ?? null
2552 - );
2553 - } else {
2554 - // Otherwise, use apply_filters for add-on callbacks
2555 - $callback_result = apply_filters(
2556 - $matched_intent->callback_function,
2557 - false,
2558 - $message,
2559 - $user_id,
2560 - $session_id,
2561 - $matched_intent
2562 - );
2563 - }
2564 -
2565 - // Handle the callback result properly
2566 - if ($callback_result !== false) {
2567 - // If callback returned an array with chat_mode, use it directly
2568 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2569 - $this->fallbackResponse = $callback_result;
2570 - return $callback_result; // Return the full array
2571 - } else {
2572 - $this->fallbackResponse = $callback_result;
2573 - return true;
2574 - }
2575 - }
2576 - }
2577 -
2578 - return false;
2579 -}
2580 -
2581 -/**
2582 - * Check if an action is enabled for a specific bot
2583 - */
2584 -private function is_action_enabled_for_bot($intent, $bot_id) {
2585 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2586 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2587 - return true;
2588 - }
2589 -
2590 - $enabled_bots = json_decode($intent->enabled_bots, true);
2591 -
2592 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2593 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2594 - return true;
2595 - }
2596 -
2597 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2598 - // default-bot actions are testable from the admin panel
2599 - if ($bot_id === 'testing') {
2600 - $bot_id = 'default';
2601 - }
2602 -
2603 - // Check if the current bot is in the enabled bots list
2604 - return in_array($bot_id, $enabled_bots);
2605 -}
2606 -
2607 -// Helper function to clear PDF and Word document related transients
2608 -private function clear_pdf_transients($session_id) {
2609 - // PDF transients
2610 - delete_transient('mxchat_pdf_url_' . $session_id);
2611 - delete_transient('mxchat_pdf_embeddings_' . $session_id);
2612 - delete_transient('mxchat_include_pdf_in_context_' . $session_id);
2613 - delete_transient('mxchat_waiting_for_pdf_url_' . $session_id);
2614 -
2615 - // Word document transients
2616 - delete_transient('mxchat_word_url_' . $session_id);
2617 - delete_transient('mxchat_word_filename_' . $session_id);
2618 - delete_transient('mxchat_word_embeddings_' . $session_id);
2619 - delete_transient('mxchat_include_word_in_context_' . $session_id);
2620 - delete_transient('mxchat_waiting_for_word_' . $session_id);
2621 -}
2622 -
2623 -
2624 -
2625 -//verified good
2626 -public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2627 - // Get the user's original instruction/message
2628 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2629 -
2630 - // Set instruction for AI - just pass along what the user wanted to say
2631 - $this->current_action_instruction = $user_instruction;
2632 -
2633 - // Set the transient to track email capture flow
2634 - set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2635 -
2636 - // Return false to let the AI generate the response
2637 - return false;
2638 -}
2639 -
2640 -public function mxchat_generate_image($message, $user_id, $session_id) {
2641 - //error_log("Starting image generation for message: " . $message);
2642 -
2643 - // Prepare a prompt for OpenAI image generation
2644 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2645 -
2646 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2647 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2648 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2649 - $image_response = $this->mxchat_generate_custom_image($prompt);
2650 - } else {
2651 - // Use the existing OpenAI API key
2652 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2653 - // Call OpenAI GPT Image to generate an image
2654 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2655 - }
2656 -
2657 - // Check if the response contains an image URL
2658 - if (isset($image_response['imageUrl'])) {
2659 - $image_url = esc_url_raw($image_response['imageUrl']);
2660 -
2661 - // Construct the HTML with a CSS class instead of inline styles
2662 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2663 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2664 -
2665 - // Save the bot message with both text and HTML
2666 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2667 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2668 -
2669 - // Set the fallback response for the chat handler
2670 - $this->fallbackResponse = [
2671 - 'text' => $response_text,
2672 - 'html' => $response_html,
2673 - 'images' => [$image_url]
2674 - ];
2675 -
2676 - // For debugging/verification - Use json_encode to verify what's being set
2677 - //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
2678 -
2679 - // Return the response directly instead of relying on the property
2680 - return $this->fallbackResponse;
2681 - } else {
2682 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2683 -
2684 - // Save the error message
2685 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2686 -
2687 - // Set the fallback response for the chat handler
2688 - $this->fallbackResponse = [
2689 - 'text' => $response_text,
2690 - 'html' => '',
2691 - 'images' => []
2692 - ];
2693 -
2694 - //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2695 - //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
2696 -
2697 - // Return the response directly instead of relying on the property
2698 - return $this->fallbackResponse;
2699 - }
2700 -}
2701 -
2702 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2703 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2704 -
2705 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2706 - if (empty($gemini_api_key)) {
2707 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2708 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2709 - return ['text' => $response_text, 'html' => '', 'images' => []];
2710 - }
2711 -
2712 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2713 -
2714 - if (isset($image_response['imageUrl'])) {
2715 - $image_url = esc_url_raw($image_response['imageUrl']);
2716 -
2717 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2718 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2719 -
2720 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2721 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2722 -
2723 - $this->fallbackResponse = [
2724 - 'text' => $response_text,
2725 - 'html' => $response_html,
2726 - 'images' => [$image_url]
2727 - ];
2728 -
2729 - return $this->fallbackResponse;
2730 - } else {
2731 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2732 -
2733 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2734 -
2735 - $this->fallbackResponse = [
2736 - 'text' => $response_text,
2737 - 'html' => '',
2738 - 'images' => []
2739 - ];
2740 -
2741 - return $this->fallbackResponse;
2742 - }
2743 -}
2744 -
2745 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2746 - $extension = ($mime_type === 'image/jpeg') ? 'jpg' : 'png';
2747 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2748 - $decoded = base64_decode($base64_data);
2749 -
2750 - if ($decoded === false) {
2751 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2752 - }
2753 -
2754 - $upload = wp_upload_bits($filename, null, $decoded);
2755 -
2756 - if (!empty($upload['error'])) {
2757 - return new \WP_Error('upload_failed', $upload['error']);
2758 - }
2759 -
2760 - $attach_id = wp_insert_attachment([
2761 - 'post_mime_type' => $mime_type,
2762 - 'post_title' => $prefix,
2763 - 'post_content' => '',
2764 - 'post_status' => 'inherit',
2765 - ], $upload['file']);
2766 -
2767 - if (is_wp_error($attach_id)) {
2768 - return $attach_id;
2769 - }
2770 -
2771 - require_once ABSPATH . 'wp-admin/includes/image.php';
2772 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2773 - wp_update_attachment_metadata($attach_id, $metadata);
2774 -
2775 - return esc_url_raw(wp_get_attachment_url($attach_id));
2776 -}
2777 -
2778 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
2779 - $api_url = 'https://api.openai.com/v1/images/generations';
2780 - $body = json_encode([
2781 - 'prompt' => sanitize_text_field($prompt),
2782 - 'n' => 1,
2783 - 'size' => '1024x1024',
2784 - 'quality' => 'medium',
2785 - 'output_format' => 'png',
2786 - 'model' => sanitize_text_field($model),
2787 - ]);
2788 -
2789 - $args = [
2790 - 'body' => $body,
2791 - 'headers' => [
2792 - 'Content-Type' => 'application/json',
2793 - 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2794 - ],
2795 - 'method' => 'POST',
2796 - 'timeout' => absint($timeout),
2797 - ];
2798 -
2799 - $response = wp_remote_post($api_url, $args);
2800 -
2801 - if (is_wp_error($response)) {
2802 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2803 - }
2804 -
2805 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2806 -
2807 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2808 - if ($b64) {
2809 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2810 - if (is_wp_error($saved_url)) {
2811 - return ['error' => $saved_url->get_error_message()];
2812 - }
2813 - return ['imageUrl' => $saved_url];
2814 - } else {
2815 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2816 - }
2817 -}
2818 -
2819 -/**
2820 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2821 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2822 - */
2823 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2824 - $cfg = $this->mxchat_resolve_custom_provider();
2825 - if (empty($cfg['base_url'])) {
2826 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2827 - }
2828 - $url = $cfg['base_url'] . '/images/generations';
2829 - if (!empty($cfg['api_version'])) {
2830 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2831 - }
2832 - $body = wp_json_encode([
2833 - 'prompt' => sanitize_text_field($prompt),
2834 - 'n' => 1,
2835 - 'size' => '1024x1024',
2836 - 'model' => $cfg['model'],
2837 - ]);
2838 - $response = wp_remote_post($url, [
2839 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2840 - 'body' => $body,
2841 - 'method' => 'POST',
2842 - 'timeout' => absint($timeout),
2843 - ]);
2844 - if (is_wp_error($response)) {
2845 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2846 - }
2847 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2848 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2849 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2850 - if ($b64) {
2851 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2852 - if (is_wp_error($saved)) {
2853 - return ['error' => $saved->get_error_message()];
2854 - }
2855 - return ['imageUrl' => $saved];
2856 - }
2857 - $remote_url = $resp['data'][0]['url'] ?? null;
2858 - if ($remote_url) {
2859 - return ['imageUrl' => esc_url_raw($remote_url)];
2860 - }
2861 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2862 - return ['error' => esc_html($err_msg)];
2863 -}
2864 -
2865 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2866 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2867 -
2868 - $body = json_encode([
2869 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2870 - 'parameters' => [
2871 - 'sampleCount' => 1,
2872 - 'aspectRatio' => '1:1',
2873 - ],
2874 - ]);
2875 -
2876 - $args = [
2877 - 'body' => $body,
2878 - 'headers' => [
2879 - 'Content-Type' => 'application/json',
2880 - 'x-goog-api-key' => sanitize_text_field($api_key),
2881 - ],
2882 - 'method' => 'POST',
2883 - 'timeout' => absint($timeout),
2884 - ];
2885 -
2886 - $response = wp_remote_post($api_url, $args);
2887 -
2888 - if (is_wp_error($response)) {
2889 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2890 - }
2891 -
2892 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2893 -
2894 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2895 - if ($b64) {
2896 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2897 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2898 - if (is_wp_error($saved_url)) {
2899 - return ['error' => $saved_url->get_error_message()];
2900 - }
2901 - return ['imageUrl' => $saved_url];
2902 - } else {
2903 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2904 - }
2905 -}
2906 -
2907 -/**
2908 - * Handle web search requests.
2909 - *
2910 - * Sends the refined search query to the Brave Search API and uses the
2911 - * results to generate a conversational response with the AI model.
2912 - *
2913 - * @since 1.0.0
2914 - * @param string $message The user's search query.
2915 - * @param string $user_id The user identifier.
2916 - * @param string $session_id The current session ID.
2917 - * @return array Response array containing text with embedded HTML links
2918 - */
2919 -public function mxchat_handle_search_request($message, $user_id, $session_id) {
2920 - // Step 1: Interpret and refine the search query
2921 - $refined_search_query = $this->mxchat_interpret_search_query($message);
2922 - if (empty($refined_search_query)) {
2923 - return array(
2924 - 'text' => esc_html__('I apologize, but could you please rephrase your search request?', 'mxchat'),
2925 - 'html' => ''
2926 - );
2927 - }
2928 -
2929 - // Retrieve and validate API settings
2930 - $options = get_option('mxchat_options');
2931 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
2932 - $results_count = isset($options['brave_results_count']) ? absint($options['brave_results_count']) : 5;
2933 -
2934 - if (empty($api_key)) {
2935 - return array(
2936 - 'text' => esc_html__('Search functionality is temporarily unavailable. Please try again later.', 'mxchat'),
2937 - 'html' => ''
2938 - );
2939 - }
2940 -
2941 - // Build the API request URL
2942 - $api_url = add_query_arg(
2943 - array(
2944 - 'q' => rawurlencode($refined_search_query),
2945 - 'count' => $results_count,
2946 - 'text_decorations' => 'true',
2947 - 'rich_data' => 'true',
2948 - ),
2949 - 'https://api.search.brave.com/res/v1/web/search'
2950 - );
2951 -
2952 - // Attempt to retrieve cached results first
2953 - $transient_key = 'mxchat_search_' . md5($refined_search_query);
2954 - $results = get_transient($transient_key);
2955 -
2956 - if (false === $results) {
2957 - // SECURITY FIX: Changed to wp_safe_remote_get
2958 - $response = wp_safe_remote_get(
2959 - $api_url,
2960 - array(
2961 - 'headers' => array(
2962 - 'Accept' => 'application/json',
2963 - 'Accept-Encoding' => 'gzip',
2964 - 'X-Subscription-Token'=> $api_key,
2965 - ),
2966 - 'timeout' => 10,
2967 - )
2968 - );
2969 -
2970 - if (is_wp_error($response)) {
2971 - return array(
2972 - 'text' => esc_html__('I encountered an error while searching. Please try again.', 'mxchat'),
2973 - 'html' => ''
2974 - );
2975 - }
2976 -
2977 - $results = json_decode(wp_remote_retrieve_body($response), true);
2978 -
2979 - if (json_last_error() !== JSON_ERROR_NONE) {
2980 - return array(
2981 - 'text' => esc_html__('I received an invalid response from the search service.', 'mxchat'),
2982 - 'html' => ''
2983 - );
2984 - }
2985 -
2986 - // Cache results for one hour
2987 - set_transient($transient_key, $results, HOUR_IN_SECONDS);
2988 - }
2989 -
2990 - // Process results
2991 - if (!empty($results['web']['results']) && is_array($results['web']['results'])) {
2992 - // Create a more straightforward summary with HTML links
2993 - $search_results_text = '';
2994 -
2995 - // Add a simple intro
2996 - $search_results_text .= sprintf(
2997 - esc_html__("Here's what I found about '%s':", 'mxchat'),
2998 - esc_html($refined_search_query)
2999 - );
3000 -
3001 - // Add the top results with HTML links
3002 - foreach (array_slice($results['web']['results'], 0, 5) as $result) {
3003 - $title = isset($result['title']) ? wp_strip_all_tags($result['title']) : '';
3004 - $url = isset($result['url']) ? esc_url($result['url']) : '';
3005 - $description = isset($result['description']) ? wp_strip_all_tags($result['description']) : '';
3006 -
3007 - // Add a line break after the intro
3008 - $search_results_text .= '<br><br>';
3009 -
3010 - // Add title as a link
3011 - $search_results_text .= sprintf(
3012 - '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a><br>',
3013 - $url,
3014 - $title
3015 - );
3016 -
3017 - // Add a condensed description
3018 - $search_results_text .= sprintf("%s", $description);
3019 - }
3020 -
3021 - // Save to chat history
3022 - $this->mxchat_save_chat_message($session_id, 'bot', $search_results_text);
3023 -
3024 - // Return the formatted text with embedded HTML links
3025 - return array(
3026 - 'text' => $search_results_text,
3027 - 'html' => ''
3028 - );
3029 - } else {
3030 - return array(
3031 - 'text' => sprintf(
3032 - esc_html__('I searched for "%s" but couldn\'t find any relevant results. Would you like to try different search terms?', 'mxchat'),
3033 - esc_html($refined_search_query)
3034 - ),
3035 - 'html' => ''
3036 - );
3037 - }
3038 -}
3039 -
3040 -//very good
3041 -/**
3042 - * Handle image search requests from the chatbot
3043 - *
3044 - * @param string $message The user's search query
3045 - * @param int $user_id The user's ID
3046 - * @param string $session_id The chat session ID
3047 - * @return array Response array with text and HTML content
3048 - */
3049 -public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3050 - // Step 1: Interpret the search query using the user's selected AI model
3051 - $refined_search_query = $this->mxchat_interpret_search_query($message);
3052 -
3053 - // If no query was interpreted, return a fallback message
3054 - if (empty($refined_search_query)) {
3055 - return array(
3056 - 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3057 - 'html' => "",
3058 - );
3059 - }
3060 -
3061 - // Brave API URL
3062 - $api_url = 'https://api.search.brave.com/res/v1/images/search';
3063 -
3064 - // Retrieve Brave API settings
3065 - $options = get_option('mxchat_options');
3066 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3067 -
3068 - if (empty($api_key)) {
3069 - return array(
3070 - 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3071 - 'html' => "",
3072 - );
3073 - }
3074 -
3075 - $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3076 - $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
3077 -
3078 - // Append query parameters based on settings
3079 - $api_url = add_query_arg([
3080 - 'q' => rawurlencode($refined_search_query),
3081 - 'count' => $image_count,
3082 - 'safesearch' => $safe_search,
3083 - ], $api_url);
3084 -
3085 - // Implement caching
3086 - $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3087 - $body = get_transient($transient_key);
3088 -
3089 - if (false === $body) {
3090 - $args = [
3091 - 'headers' => [
3092 - 'Accept' => 'application/json',
3093 - 'Accept-Encoding' => 'gzip',
3094 - 'X-Subscription-Token' => $api_key,
3095 - ],
3096 - 'timeout' => 10,
3097 - ];
3098 -
3099 - // SECURITY FIX: Changed to wp_safe_remote_get
3100 - $response = wp_safe_remote_get($api_url, $args);
3101 -
3102 - if (is_wp_error($response)) {
3103 - return array(
3104 - 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3105 - 'html' => "",
3106 - );
3107 - }
3108 -
3109 - $body = json_decode(wp_remote_retrieve_body($response), true);
3110 - set_transient($transient_key, $body, HOUR_IN_SECONDS);
3111 - }
3112 -
3113 - // Process the API response
3114 - if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3115 - $html_output = '<div class="mxchat-image-gallery">';
3116 -
3117 - // Get the configured image count (1-6)
3118 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3119 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3120 -
3121 - // Use only the requested number of images
3122 - for ($i = 0; $i < $display_count; $i++) {
3123 - $image = $body['results'][$i];
3124 - $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3125 - $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3126 - $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3127 -
3128 - if ($image_url && $thumbnail_url) {
3129 - $html_output .= '<div class="mxchat-image-item">';
3130 - $html_output .= '<strong class="mxchat-image-title">' . $title . '</strong>';
3131 - $html_output .= '<a href="' . $image_url . '" target="_blank" rel="noopener noreferrer" class="mxchat-image-link">';
3132 - $html_output .= '<img src="' . $thumbnail_url . '" alt="' . $title . '" class="mxchat-image-thumbnail">';
3133 - $html_output .= '</a></div>';
3134 - }
3135 - }
3136 -
3137 - $html_output .= '</div>';
3138 -
3139 - // Create response text
3140 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3141 -
3142 - // Save both response text and HTML to chat history
3143 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3144 - $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3145 -
3146 - // Return the combined response
3147 - return array(
3148 - 'text' => $response_text,
3149 - 'html' => $html_output,
3150 - );
3151 - } else {
3152 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3153 -
3154 - // Save the error message to chat history
3155 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3156 -
3157 - return array(
3158 - 'text' => $response_text,
3159 - 'html' => "",
3160 - );
3161 - }
3162 -}
3163 -
3164 -/**
3165 - * Interpret the search query using the user's selected AI model
3166 - *
3167 - * @param string $user_query The original query from the user
3168 - * @return string The refined search query
3169 - */
3170 -public function mxchat_interpret_search_query($user_query) {
3171 - $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3172 -
3173 - // Get options and determine the selected model
3174 - $options = $this->options ?? get_option('mxchat_options');
3175 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
3176 -
3177 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3178 - if ($selected_model === 'custom-provider') {
3179 - return $this->interpret_query_with_custom($user_query, $system_prompt);
3180 - }
3181 -
3182 - // Extract model prefix to determine the provider
3183 - $model_parts = explode('-', $selected_model);
3184 - $provider = strtolower($model_parts[0]);
3185 -
3186 - // Determine which API key to use based on the provider
3187 - switch ($provider) {
3188 - case 'gemini':
3189 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3190 - if (empty($api_key)) {
3191 - return sanitize_text_field($user_query); // Default to original query if API key missing
3192 - }
3193 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3194 -
3195 - case 'claude':
3196 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3197 - if (empty($api_key)) {
3198 - return sanitize_text_field($user_query);
3199 - }
3200 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3201 -
3202 - case 'grok':
3203 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3204 - if (empty($api_key)) {
3205 - return sanitize_text_field($user_query);
3206 - }
3207 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3208 -
3209 - case 'deepseek':
3210 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3211 - if (empty($api_key)) {
3212 - return sanitize_text_field($user_query);
3213 - }
3214 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3215 -
3216 - case 'gpt':
3217 - default:
3218 - // Default to OpenAI for custom models or unrecognized prefixes
3219 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3220 - if (empty($api_key)) {
3221 - return sanitize_text_field($user_query);
3222 - }
3223 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
3224 - }
3225 -}
3226 -
3227 -/**
3228 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3229 - * Uses the same base URL + auth scheme as the chat dispatcher.
3230 - */
3231 -private function interpret_query_with_custom($user_query, $system_prompt) {
3232 - $cfg = $this->mxchat_resolve_custom_provider();
3233 - if (empty($cfg['base_url'])) {
3234 - return sanitize_text_field($user_query);
3235 - }
3236 - $args = [
3237 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3238 - 'body' => wp_json_encode([
3239 - 'model' => $cfg['model'],
3240 - 'messages' => [
3241 - ['role' => 'system', 'content' => $system_prompt],
3242 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3243 - ],
3244 - 'temperature' => 0.2,
3245 - 'max_tokens' => 20,
3246 - ]),
3247 - 'method' => 'POST',
3248 - 'timeout' => 15,
3249 - ];
3250 - $response = wp_remote_post($cfg['chat_url'], $args);
3251 - if (is_wp_error($response)) {
3252 - return sanitize_text_field($user_query);
3253 - }
3254 - $body = json_decode(wp_remote_retrieve_body($response), true);
3255 - return isset($body['choices'][0]['message']['content'])
3256 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3257 - : sanitize_text_field($user_query);
3258 -}
3259 -
3260 -/**
3261 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3262 - * into the assoc-array form wp_remote_post expects.
3263 - */
3264 -private function mxchat_custom_provider_assoc_headers($cfg) {
3265 - $headers = ['Content-Type' => 'application/json'];
3266 - if (!empty($cfg['api_key'])) {
3267 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3268 - $headers['api-key'] = $cfg['api_key'];
3269 - } else {
3270 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3271 - }
3272 - }
3273 - return $headers;
3274 -}
3275 -
3276 -/**
3277 - * Interpret query using OpenAI models
3278 - */
3279 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3280 - $url = 'https://api.openai.com/v1/chat/completions';
3281 - $args = [
3282 - 'headers' => [
3283 - 'Authorization' => 'Bearer ' . $api_key,
3284 - 'Content-Type' => 'application/json',
3285 - ],
3286 - 'body' => wp_json_encode([
3287 - 'model' => $model,
3288 - 'messages' => [
3289 - ['role' => 'system', 'content' => $system_prompt],
3290 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3291 - ],
3292 - 'temperature' => 0.2,
3293 - 'max_tokens' => 20,
3294 - ]),
3295 - 'method' => 'POST',
3296 - 'timeout' => 15,
3297 - ];
3298 -
3299 - $response = wp_remote_post($url, $args);
3300 - if (is_wp_error($response)) {
3301 - return sanitize_text_field($user_query);
3302 - }
3303 -
3304 - $body = json_decode(wp_remote_retrieve_body($response), true);
3305 - return isset($body['choices'][0]['message']['content'])
3306 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3307 - : sanitize_text_field($user_query);
3308 -}
3309 -
3310 -/**
3311 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3312 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3313 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3314 - * stripping. We never send a `thinking` param either, which is required for
3315 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3316 - */
3317 -private function mxchat_claude_omits_temperature($model) {
3318 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3319 - return in_array($model, $no_temp, true);
3320 -}
3321 -
3322 -/**
3323 - * Interpret query using Claude models
3324 - */
3325 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3326 - $url = 'https://api.anthropic.com/v1/messages';
3327 -
3328 - $payload = [
3329 - 'model' => $model,
3330 - 'system' => $system_prompt,
3331 - 'messages' => [
3332 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3333 - ],
3334 - 'max_tokens' => 20,
3335 - 'temperature' => 0.2,
3336 - ];
3337 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3338 -
3339 - $args = [
3340 - 'headers' => [
3341 - 'Content-Type' => 'application/json',
3342 - 'x-api-key' => $api_key,
3343 - 'anthropic-version' => '2023-06-01',
3344 - ],
3345 - 'body' => wp_json_encode($payload),
3346 - 'method' => 'POST',
3347 - 'timeout' => 15,
3348 - ];
3349 -
3350 - $response = wp_remote_post($url, $args);
3351 - if (is_wp_error($response)) {
3352 - return sanitize_text_field($user_query);
3353 - }
3354 -
3355 - $body = json_decode(wp_remote_retrieve_body($response), true);
3356 - // claude-fable-5 prepends a thinking block to content — take the first
3357 - // TEXT block, not content[0].
3358 - foreach ((array) ($body['content'] ?? array()) as $block) {
3359 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3360 - return sanitize_text_field(trim($block['text']));
3361 - }
3362 - }
3363 -
3364 - return sanitize_text_field($user_query);
3365 -}
3366 -
3367 -/**
3368 - * Interpret query using Gemini models
3369 - */
3370 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3371 - if ($model === 'gemini-3-pro-preview') {
3372 - $model = 'gemini-3.1-pro-preview';
3373 - }
3374 - // Use v1beta for preview models, v1 for stable models
3375 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3376 -
3377 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
3378 -
3379 - $args = [
3380 - 'headers' => [
3381 - 'Content-Type' => 'application/json',
3382 - ],
3383 - 'body' => wp_json_encode([
3384 - 'contents' => [
3385 - [
3386 - 'role' => 'user',
3387 - 'parts' => [
3388 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3389 - ]
3390 - ]
3391 - ],
3392 - 'generationConfig' => [
3393 - 'temperature' => 0.2,
3394 - 'maxOutputTokens' => 20,
3395 - ],
3396 - ]),
3397 - 'method' => 'POST',
3398 - 'timeout' => 15,
3399 - ];
3400 -
3401 - $response = wp_remote_post($url, $args);
3402 - if (is_wp_error($response)) {
3403 - return sanitize_text_field($user_query);
3404 - }
3405 -
3406 - $body = json_decode(wp_remote_retrieve_body($response), true);
3407 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3408 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3409 - }
3410 -
3411 - return sanitize_text_field($user_query);
3412 -}
3413 -
3414 -/**
3415 - * Interpret query using X.AI (Grok) models
3416 - */
3417 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3418 - $url = 'https://api.xai.com/v1/chat/completions';
3419 -
3420 - $args = [
3421 - 'headers' => [
3422 - 'Content-Type' => 'application/json',
3423 - 'Authorization' => 'Bearer ' . $api_key,
3424 - ],
3425 - 'body' => wp_json_encode([
3426 - 'model' => $model,
3427 - 'messages' => [
3428 - ['role' => 'system', 'content' => $system_prompt],
3429 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3430 - ],
3431 - 'temperature' => 0.2,
3432 - 'max_tokens' => 20,
3433 - ]),
3434 - 'method' => 'POST',
3435 - 'timeout' => 15,
3436 - ];
3437 -
3438 - $response = wp_remote_post($url, $args);
3439 - if (is_wp_error($response)) {
3440 - return sanitize_text_field($user_query);
3441 - }
3442 -
3443 - $body = json_decode(wp_remote_retrieve_body($response), true);
3444 - if (isset($body['choices'][0]['message']['content'])) {
3445 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3446 - }
3447 -
3448 - return sanitize_text_field($user_query);
3449 -}
3450 -
3451 -/**
3452 - * Interpret query using DeepSeek models
3453 - */
3454 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3455 - $url = 'https://api.deepseek.com/v1/chat/completions';
3456 -
3457 - $args = [
3458 - 'headers' => [
3459 - 'Content-Type' => 'application/json',
3460 - 'Authorization' => 'Bearer ' . $api_key,
3461 - ],
3462 - 'body' => wp_json_encode([
3463 - 'model' => $model,
3464 - 'messages' => [
3465 - ['role' => 'system', 'content' => $system_prompt],
3466 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3467 - ],
3468 - 'temperature' => 0.2,
3469 - 'max_tokens' => 20,
3470 - ]),
3471 - 'method' => 'POST',
3472 - 'timeout' => 15,
3473 - ];
3474 -
3475 - $response = wp_remote_post($url, $args);
3476 - if (is_wp_error($response)) {
3477 - return sanitize_text_field($user_query);
3478 - }
3479 -
3480 - $body = json_decode(wp_remote_retrieve_body($response), true);
3481 - if (isset($body['choices'][0]['message']['content'])) {
3482 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3483 - }
3484 -
3485 - return sanitize_text_field($user_query);
3486 -}
3487 -
3488 -//very good
3489 -private function add_email_to_loops($email) {
3490 - // Sanitize the email
3491 - $email = sanitize_email($email);
3492 -
3493 - // Retrieve and sanitize options
3494 - $api_key = isset($this->options['loops_api_key']) ? sanitize_text_field($this->options['loops_api_key']) : '';
3495 - $mailing_list_id = isset($this->options['loops_mailing_list']) ? sanitize_text_field($this->options['loops_mailing_list']) : '';
3496 -
3497 - // Check for missing API key or mailing list ID
3498 - if (empty($api_key) || empty($mailing_list_id)) {
3499 - //error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
3500 - return;
3501 - }
3502 -
3503 - $data = array(
3504 - 'email' => $email,
3505 - 'subscribed' => true,
3506 - 'source' => __('MxChat AI Chatbot', 'mxchat'),
3507 - 'mailingLists' => array($mailing_list_id => true),
3508 - );
3509 -
3510 - $url = 'https://app.loops.so/api/v1/contacts/create';
3511 - $args = array(
3512 - 'body' => wp_json_encode($data),
3513 - 'headers' => array(
3514 - 'Authorization' => 'Bearer ' . $api_key,
3515 - 'Content-Type' => 'application/json',
3516 - ),
3517 - 'method' => 'POST',
3518 - 'timeout' => 45,
3519 - );
3520 -
3521 - $response = wp_remote_post($url, $args);
3522 -
3523 - // Handle errors in the API request
3524 - if (is_wp_error($response)) {
3525 - //error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
3526 - return;
3527 - }
3528 -
3529 - // Check for non-200 HTTP responses
3530 - $response_code = wp_remote_retrieve_response_code($response);
3531 - if ($response_code != 200) {
3532 - $response_body = wp_remote_retrieve_body($response);
3533 - //error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
3534 - }
3535 -}
3536 -
3537 -public function mxchat_handle_pdf_discussion($message, $user_id, $session_id) {
3538 - // Get the maximum number of pages allowed from admin settings
3539 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3540 -
3541 - // Retrieve options for dynamic texts
3542 - $trigger_text = $this->options['pdf_intent_trigger_text'] ?? __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat');
3543 - $success_text = $this->options['pdf_intent_success_text'] ?? __("I've processed the PDF. What questions do you have about it?", 'mxchat');
3544 - $error_text = $this->options['pdf_intent_error_text'] ?? __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
3545 -
3546 - // Check for explicit request for new PDF
3547 - $new_pdf_requested = stripos($message, 'new') !== false ||
3548 - stripos($message, 'another') !== false ||
3549 - stripos($message, 'different') !== false;
3550 -
3551 - // If user mentions adding/reading a PDF, set waiting flag
3552 - if (stripos($message, 'pdf') !== false ||
3553 - stripos($message, 'document') !== false ||
3554 - stripos($message, 'read') !== false) {
3555 - set_transient('mxchat_waiting_for_pdf_url_' . $session_id, true, HOUR_IN_SECONDS);
3556 - $this->fallbackResponse['text'] = $trigger_text;
3557 - return;
3558 - }
3559 -
3560 - // If we're waiting for a URL or user requested new PDF
3561 - if ($new_pdf_requested || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
3562 - if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
3563 - // Process URL... (rest of your existing URL processing code)
3564 - } else {
3565 - $this->fallbackResponse['text'] = $trigger_text;
3566 - }
3567 - return;
3568 - }
3569 -
3570 - // Default to proceeding with conversation if no specific PDF action is needed
3571 - $this->fallbackResponse['text'] = '';
3572 -}
3573 -
3574 -
3575 -/**
3576 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3577 - */
3578 -private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3579 - // CLEAR DEBUG LOGGING
3580 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3581 - //error_log("PDF Source: " . $pdf_source);
3582 - //error_log("Max Pages: " . $max_pages);
3583 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3584 -
3585 - // Check if Advanced Claude Toolbar is available and enabled
3586 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3587 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3588 -
3589 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3590 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3591 -
3592 - if ($claude_available && $claude_enabled) {
3593 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3594 -
3595 - // Attempt Claude processing first
3596 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3597 -
3598 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3599 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3600 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3601 -
3602 - // Log first page details for verification
3603 - if (isset($claude_result[0])) {
3604 - $first_page = $claude_result[0];
3605 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3606 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3607 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3608 - }
3609 -
3610 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3611 - return $claude_result;
3612 - } else {
3613 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3614 - //error_log("Claude result type: " . gettype($claude_result));
3615 - if (is_array($claude_result)) {
3616 - //error_log("Claude result count: " . count($claude_result));
3617 - }
3618 - }
3619 - }
3620 -
3621 - // Fallback to basic processing
3622 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3623 -
3624 - $upload_dir = wp_upload_dir();
3625 - $temp_file = null;
3626 -
3627 - try {
3628 - // Your existing basic processing code here...
3629 - // (I'll include the key parts with debug logging)
3630 -
3631 - if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3632 - //error_log("Downloading PDF from URL...");
3633 -
3634 - // SECURITY FIX: Validate URL before processing
3635 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3636 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3637 - return false;
3638 - }
3639 -
3640 - $temp_file = wp_tempnam($pdf_source);
3641 -
3642 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3643 - $response = wp_safe_remote_get($pdf_source, [
3644 - 'timeout' => 60,
3645 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3646 - ]);
3647 -
3648 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3649 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3650 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3651 - return false;
3652 - }
3653 -
3654 - global $wp_filesystem;
3655 - if (empty($wp_filesystem)) {
3656 - require_once ABSPATH . 'wp-admin/includes/file.php';
3657 - WP_Filesystem();
3658 - }
3659 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3660 - //error_log("✅ PDF downloaded successfully");
3661 - } else {
3662 - $temp_file = $pdf_source;
3663 - //error_log("Using local PDF file: " . $temp_file);
3664 - }
3665 -
3666 - // Parse PDF
3667 - //error_log("Parsing PDF with basic parser...");
3668 - mxchat_load_pdf_parser();
3669 - $parser = new \Smalot\PdfParser\Parser();
3670 - $pdf = $parser->parseFile($temp_file);
3671 - $pages = $pdf->getPages();
3672 -
3673 - //error_log("PDF contains " . count($pages) . " pages");
3674 -
3675 - if (count($pages) > $max_pages) {
3676 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3677 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3678 - unlink($temp_file);
3679 - }
3680 - return 'too_many_pages';
3681 - }
3682 -
3683 - $embeddings = [];
3684 - $processed_pages = 0;
3685 -
3686 - foreach ($pages as $page_number => $page) {
3687 - $text = $page->getText();
3688 -
3689 - if (empty(trim($text))) {
3690 - //error_log("Skipping empty page: " . ($page_number + 1));
3691 - continue;
3692 - }
3693 -
3694 - $text = $this->mxchat_clean_text($text);
3695 -
3696 - $embedding = $this->mxchat_generate_embedding(
3697 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3698 - $this->options['api_key']
3699 - );
3700 -
3701 - if ($embedding) {
3702 - $embeddings[] = [
3703 - 'page_number' => $page_number + 1,
3704 - 'embedding' => $embedding,
3705 - 'text' => $text,
3706 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3707 - 'processing_method' => 'basic_pdf_parser'
3708 - ];
3709 - $processed_pages++;
3710 - }
3711 - }
3712 -
3713 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3714 -
3715 - // Cleanup
3716 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3717 - unlink($temp_file);
3718 - }
3719 -
3720 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
3721 - return $embeddings;
3722 -
3723 - } catch (\Exception $e) {
3724 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
3725 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3726 - unlink($temp_file);
3727 - }
3728 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3729 - return false;
3730 - }
3731 -}
3732 -
3733 -
3734 -/**
3735 - * Validate PDF URL for security
3736 - * Prevents SSRF attacks by blocking dangerous URLs
3737 - */
3738 -
3739 -private function mxchat_is_safe_pdf_url($url) {
3740 - // Use WordPress core function for comprehensive validation
3741 - // This blocks localhost, private IPs, and reserved IP ranges
3742 - $validated_url = wp_http_validate_url($url);
3743 -
3744 - if ($validated_url === false) {
3745 - return false;
3746 - }
3747 -
3748 - // Additional check: only allow HTTP/HTTPS schemes
3749 - $parsed = parse_url($url);
3750 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3751 - return false;
3752 - }
3753 -
3754 - return true;
3755 -}
3756 -
3757 -
3758 -private function mxchat_clean_text($text) {
3759 - // Remove excessive whitespace
3760 - $text = preg_replace('/\s+/', ' ', $text);
3761 -
3762 - // Remove control characters except newlines and tabs
3763 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3764 -
3765 - // Normalize line endings
3766 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3767 -
3768 - // Trim whitespace
3769 - $text = trim($text);
3770 -
3771 - return $text;
3772 -}
3773 -
3774 -private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3775 - //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3776 -
3777 - $most_relevant = null;
3778 - $highest_similarity = -INF;
3779 -
3780 - foreach ($embeddings as $page_data) {
3781 - $similarity = $this->mxchat_calculate_cosine_similarity($query_embedding, $page_data['embedding']);
3782 -
3783 - if ($similarity > $highest_similarity) {
3784 - $highest_similarity = $similarity;
3785 - $most_relevant = $page_data['page_number'];
3786 - }
3787 - }
3788 -
3789 - if (!is_null($most_relevant)) {
3790 - $page_numbers = range(max(1, $most_relevant - 1), min(count($embeddings), $most_relevant + 1));
3791 - return array_filter($embeddings, function ($page) use ($page_numbers) {
3792 - return in_array($page['page_number'], $page_numbers);
3793 - });
3794 - }
3795 -
3796 - return [];
3797 -}
3798 -
3799 -
3800 -public function handle_pdf_upload() {
3801 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3802 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3803 - }
3804 -
3805 - if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3806 - wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 - return;
3808 - }
3809 -
3810 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3811 - $options = get_option('mxchat_options', array());
3812 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3813 -
3814 - if ($show_pdf_button !== 'on') {
3815 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3816 - return;
3817 - }
3818 -
3819 - $file = $_FILES['pdf_file'];
3820 - $session_id = sanitize_text_field($_POST['session_id']);
3821 - $original_filename = sanitize_text_field($file['name']);
3822 -
3823 - // Update session owner if it changed (e.g. IP changed due to network switch)
3824 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3825 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3826 -
3827 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3828 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3829 - }
3830 -
3831 - $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3832 - if ($file_type['type'] !== 'application/pdf') {
3833 - wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3834 - return;
3835 - }
3836 -
3837 - $upload_dir = wp_upload_dir();
3838 -
3839 - // SECURITY FIX: Generate random filename without exposing session_id
3840 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3841 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
3842 - $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3843 -
3844 - if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3845 - wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
3846 - return;
3847 - }
3848 -
3849 - $this->clear_pdf_transients($session_id);
3850 -
3851 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3852 - $embeddings = $this->fetch_and_split_pdf_pages($pdf_path, $max_pages);
3853 -
3854 - if ($embeddings === 'too_many_pages') {
3855 - unlink($pdf_path);
3856 - $error_message = sprintf(
3857 - $this->options['pdf_intent_error_text'] ??
3858 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
3859 - $max_pages
3860 - );
3861 - wp_send_json_error($error_message);
3862 - return;
3863 - }
3864 -
3865 - if ($embeddings === false || empty($embeddings)) {
3866 - unlink($pdf_path);
3867 - $error_message = $this->options['pdf_intent_error_text'] ??
3868 - esc_html__('The uploaded PDF appears to be empty or contains unsupported content.', 'mxchat');
3869 - wp_send_json_error($error_message);
3870 - return;
3871 - }
3872 -
3873 - if (!empty($embeddings)) {
3874 - // Store the mapping between session and the random filename
3875 - set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3876 - set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3877 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3878 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
3879 -
3880 - $success_message = $this->options['pdf_intent_success_text'] ??
3881 - esc_html__("I've processed the PDF. What questions do you have about it?", 'mxchat');
3882 -
3883 - wp_send_json_success([
3884 - 'message' => $success_message,
3885 - 'filename' => $original_filename
3886 - ]);
3887 - return;
3888 - }
3889 -
3890 - unlink($pdf_path);
3891 - $error_message = $this->options['pdf_intent_error_text'] ??
3892 - esc_html__('Sorry, I couldn\'t process the PDF. Please ensure it\'s a valid file.', 'mxchat');
3893 - wp_send_json_error($error_message);
3894 - return;
3895 -}
3896 -public function handle_pdf_remove() {
3897 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3898 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3899 - }
3900 -
3901 - if (empty($_POST['session_id'])) {
3902 - wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
212 + // Generate and validate the embedding
213 + $user_message_embedding = $this->mxchat_generate_embedding($message_with_order_details, $this->options['api_key']);
214 + if (!is_array($user_message_embedding)) {
215 + wp_send_json_error('Error processing your message.');
3903 216 wp_die();
3904 217 }
3905 218
3906 - $session_id = sanitize_text_field($_POST['session_id']);
3907 - $pdf_path = get_transient('mxchat_pdf_url_' . $session_id);
219 + // Find relevant content based on embedding
220 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
3908 221
3909 - if ($pdf_path && file_exists($pdf_path)) {
3910 - unlink($pdf_path);
3911 - }
222 + // Fetch conversation history from the database
223 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ajax($session_id);
3912 224
3913 - $this->clear_pdf_transients($session_id);
225 + // Increment the chat count
226 + $this->mxchat_increment_chat_count();
3914 227
3915 - wp_send_json_success([
3916 - 'message' => esc_html__('PDF removed successfully.', 'mxchat')
3917 - ]);
3918 - wp_die();
3919 -}
228 + // Generate a response from the AI model
229 + $response = $this->mxchat_generate_response($relevant_content, $this->options['api_key'], $conversation_history);
3920 230
231 + // Save the bot response to the database
232 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
3921 233
3922 -function mxchat_fetch_new_messages() {
3923 - $session_id = sanitize_text_field($_POST['session_id']);
3924 - $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3925 - $persistence_enabled = $_POST['persistence_enabled'] === 'true';
3926 - $initial_timestamp = isset($_POST['initial_timestamp']) ? intval($_POST['initial_timestamp']) : 0;
234 + // Send the response back to the client
235 + wp_send_json(['message' => $response]);
3927 236
3928 - if (empty($session_id)) {
3929 - //error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
3930 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
3931 - wp_die();
3932 - }
3933 -
3934 - $history = get_option("mxchat_history_{$session_id}", []);
3935 -
3936 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
3937 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
3938 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
3939 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
3940 -
3941 - $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
3942 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
3943 -
3944 - // If persistence is enabled, show all new messages
3945 - if ($persistence_enabled) {
3946 - $has_id = !empty($message['id']);
3947 - $is_agent = $message['role'] === 'agent';
3948 -
3949 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
3950 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
3951 - $is_newer = true;
3952 - } else {
3953 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
3954 - }
3955 -
3956 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
3957 -
3958 - return $has_id && $is_newer && $is_agent;
3959 - }
3960 -
3961 - // If persistence is disabled, only show messages after initial timestamp
3962 - return !empty($message['id']) &&
3963 - $message['role'] === 'agent' &&
3964 - $message['timestamp'] > $initial_timestamp;
3965 - });
3966 -
3967 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
3968 -
3969 - // Include current chat mode so frontend can detect agent→AI transitions
3970 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3971 -
3972 - wp_send_json_success([
3973 - 'new_messages' => array_values($new_messages),
3974 - 'chat_mode' => $chat_mode
3975 - ]);
3976 237 wp_die();
3977 238 }
3978 -public function mxchat_live_agent_handover($message, $user_id, $session_id) {
3979 - // First check if live agents are available
3980 - $live_agent_available = $this->options['live_agent_status'] ?? 'off';
3981 - if ($live_agent_available !== 'on') {
3982 - $away_message = $this->options['live_agent_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
3983 - $this->fallbackResponse = [
3984 - 'text' => $away_message,
3985 - 'html' => '',
3986 - 'images' => [],
3987 - 'chat_mode' => 'ai'
3988 - ];
3989 - wp_send_json([
3990 - 'text' => $away_message,
3991 - 'html' => '',
3992 - 'chat_mode' => 'ai',
3993 - 'session_id' => $session_id
3994 - ]);
3995 - wp_die();
3996 - }
3997 239
3998 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
3999 -
4000 - if (empty($slack_bot_token)) {
4001 - return false;
4002 - }
4003 240
4004 - // Check if channel already exists for this session
4005 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4006 -
4007 - if (empty($channel_id)) {
4008 - // Create new channel with session ID as name
4009 - $channel_name = $this->generate_channel_name($session_id);
4010 -
4011 - //error_log("Attempting to create channel: $channel_name");
4012 -
4013 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4014 - 'headers' => [
4015 - 'Content-Type' => 'application/json',
4016 - 'Authorization' => 'Bearer ' . $slack_bot_token
4017 - ],
4018 - 'body' => json_encode([
4019 - 'name' => $channel_name,
4020 - 'is_private' => false // Public channel - anyone in workspace can join
4021 - ])
4022 - ]);
4023 -
4024 - if (!is_wp_error($response)) {
4025 - $response_body = wp_remote_retrieve_body($response);
4026 - $response_data = json_decode($response_body, true);
4027 -
4028 - //error_log("Channel creation response: " . $response_body);
4029 -
4030 - if (isset($response_data['ok']) && $response_data['ok']) {
4031 - $channel_id = $response_data['channel']['id'];
4032 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4033 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4034 - update_option("mxchat_channel_{$session_id}", $channel_id);
4035 -
4036 - // Auto-invite agents to the channel
4037 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4038 -
4039 - if (!empty($agent_user_ids)) {
4040 - // Parse user IDs (one per line)
4041 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4042 -
4043 - foreach ($user_ids as $user_id_to_invite) {
4044 - //error_log("Inviting user to channel: $user_id_to_invite");
4045 -
4046 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4047 - 'headers' => [
4048 - 'Content-Type' => 'application/json',
4049 - 'Authorization' => 'Bearer ' . $slack_bot_token
4050 - ],
4051 - 'body' => json_encode([
4052 - 'channel' => $channel_id,
4053 - 'users' => $user_id_to_invite
4054 - ])
4055 - ]);
4056 -
4057 - if (!is_wp_error($invite_response)) {
4058 - $invite_body = wp_remote_retrieve_body($invite_response);
4059 - $invite_data = json_decode($invite_body, true);
4060 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4061 -
4062 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4063 - //error_log("Successfully invited user $user_id_to_invite to channel");
4064 - } else {
4065 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4066 - }
4067 - } else {
4068 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4069 - }
4070 - }
4071 - } else {
4072 - //error_log("No agent user IDs configured for auto-invite");
4073 - }
4074 - } else {
4075 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4076 - }
4077 - } else {
4078 - //error_log("WP Error creating channel: " . $response->get_error_message());
4079 - }
4080 -
4081 - if (empty($channel_id)) {
4082 - return false; // Failed to create channel
4083 - }
4084 - }
4085 -
4086 - // Get recent chat history
4087 - $history = get_option("mxchat_history_{$session_id}", []);
4088 - $recent_history = array_slice($history, -5);
4089 -
4090 - // Format conversation context
4091 - $conversation_context = "";
4092 - if (!empty($recent_history)) {
4093 - $conversation_context = "*Recent Conversation:*\n";
4094 - foreach ($recent_history as $hist_message) {
4095 - $role_display = $hist_message['role'] === 'user' ? 'User' : 'AI';
4096 - $conversation_context .= ">{$role_display}: {$hist_message['content']}\n";
4097 - }
4098 - $conversation_context .= "\n";
4099 - }
4100 -
4101 - update_option("mxchat_mode_{$session_id}", 'agent');
4102 -
4103 - // Send message to channel
4104 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4105 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4106 - $channel_message .= "*User ID:* `{$user_id}`\n\n";
4107 -
4108 - if (!empty($conversation_context)) {
4109 - $channel_message .= $conversation_context;
4110 - }
4111 -
4112 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4113 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
4114 -
4115 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4116 - 'headers' => [
4117 - 'Content-Type' => 'application/json',
4118 - 'Authorization' => 'Bearer ' . $slack_bot_token
4119 - ],
4120 - 'body' => json_encode([
4121 - 'channel' => $channel_id,
4122 - 'text' => $channel_message,
4123 - 'mrkdwn' => true
4124 - ])
4125 - ]);
4126 -
4127 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4128 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4129 -
4130 - $this->fallbackResponse = [
4131 - 'text' => $success_message,
4132 - 'html' => '',
4133 - 'images' => [],
4134 - 'chat_mode' => 'agent'
4135 - ];
4136 -
4137 - wp_send_json([
4138 - 'success' => true,
4139 - 'text' => $success_message,
4140 - 'html' => '',
4141 - 'chat_mode' => 'agent',
4142 - 'session_id' => $session_id,
4143 - 'fallbackResponse' => $this->fallbackResponse
4144 - ]);
4145 - wp_die();
241 +private function mxchat_get_user_identifier() {
242 + return MxChat_User::mxchat_get_user_identifier();
4146 243 }
4147 244
4148 -private function generate_channel_name($session_id) {
4149 - $email = null;
4150 - $name = null;
4151 -
4152 - // 1. First priority: Check if user is logged in and get their info
4153 - if (is_user_logged_in()) {
4154 - $current_user = wp_get_current_user();
4155 - if (!empty($current_user->user_email)) {
4156 - $email = $current_user->user_email;
4157 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4158 - }
4159 - if (!empty($current_user->display_name)) {
4160 - $name = $current_user->display_name;
4161 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4162 - }
4163 - }
4164 -
4165 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4166 - if (empty($email)) {
4167 - $email_option_key = "mxchat_email_{$session_id}";
4168 - $saved_email = get_option($email_option_key);
4169 - if (!empty($saved_email)) {
4170 - $email = $saved_email;
4171 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4172 - }
4173 - }
4174 -
4175 - if (empty($name)) {
4176 - $name_option_key = "mxchat_name_{$session_id}";
4177 - $saved_name = get_option($name_option_key);
4178 - if (!empty($saved_name)) {
4179 - $name = $saved_name;
4180 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4181 - }
4182 - }
4183 -
4184 - // 3. Third priority: Check existing chat transcript for email/name
4185 - if (empty($email) || empty($name)) {
4186 - global $wpdb;
4187 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4188 - $existing_data = $wpdb->get_row($wpdb->prepare(
4189 - "SELECT user_email, user_name FROM $table_name WHERE session_id = %s AND (user_email IS NOT NULL OR user_name IS NOT NULL) LIMIT 1",
4190 - $session_id
4191 - ));
4192 -
4193 - if ($existing_data) {
4194 - if (empty($email) && !empty($existing_data->user_email)) {
4195 - $email = $existing_data->user_email;
4196 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4197 - }
4198 - if (empty($name) && !empty($existing_data->user_name)) {
4199 - $name = $existing_data->user_name;
4200 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4201 - }
4202 - }
4203 - }
4204 -
4205 - // 4. Generate channel name based on priority: Name > Email > Session ID
4206 - $channel_name = '';
4207 -
4208 - if (!empty($name)) {
4209 - // Convert name to valid Slack channel name
4210 - $base_name = strtolower(trim($name));
4211 - // Replace spaces and invalid characters
4212 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4213 - $base_name = preg_replace('/\s+/', '-', $base_name);
4214 - $base_name = trim($base_name, '-');
4215 -
4216 - // Get last 4 characters of session ID for uniqueness
4217 - $session_suffix = substr($session_id, -4);
4218 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4219 -
4220 - // Slack channel names have a 21 character limit
4221 - if (strlen($channel_name) > 21) {
4222 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4223 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4224 - $truncated_name = substr($base_name, 0, $available_space);
4225 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4226 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4227 - }
4228 -
4229 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4230 -
4231 - } elseif (!empty($email)) {
4232 - // Convert email to valid Slack channel name (your existing logic)
4233 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4234 - // Remove any remaining invalid characters
4235 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4236 - // Ensure it doesn't end with a hyphen
4237 - $channel_name = rtrim($channel_name, '-');
4238 - // Slack channel names have a 21 character limit, so truncate if needed
4239 - if (strlen($channel_name) > 21) {
4240 - $channel_name = substr($channel_name, 0, 21);
4241 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4242 - }
4243 -
4244 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4245 -
4246 - } else {
4247 - // Fallback to session ID if no name or email found
4248 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4249 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4250 - }
4251 -
4252 - // Final validation - ensure channel name meets Slack requirements
4253 - if (strlen($channel_name) > 21) {
4254 - $channel_name = substr($channel_name, 0, 21);
4255 - $channel_name = rtrim($channel_name, '-');
4256 - }
4257 -
4258 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4259 - return $channel_name;
4260 -}
4261 245
4262 -/**
4263 - * Telegram Live Agent Handover
4264 - * Creates a forum topic in the Telegram group and notifies agents
4265 - */
4266 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4267 - // Check if Telegram agents are available
4268 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4269 - if ($telegram_available !== 'on') {
4270 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4271 - $this->fallbackResponse = [
4272 - 'text' => $away_message,
4273 - 'html' => '',
4274 - 'images' => [],
4275 - 'chat_mode' => 'ai'
4276 - ];
4277 - wp_send_json([
4278 - 'text' => $away_message,
4279 - 'html' => '',
4280 - 'chat_mode' => 'ai',
4281 - 'session_id' => $session_id
4282 - ]);
4283 - wp_die();
4284 - }
4285 246
4286 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4287 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
247 + private function mxchat_generate_embedding($text, $api_key) {
248 + $endpoint = 'https://api.openai.com/v1/embeddings';
4288 249
4289 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
4290 - return false;
4291 - }
4292 -
4293 - // Check if topic already exists for this session
4294 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4295 -
4296 - if (empty($topic_id)) {
4297 - // Generate topic name
4298 - $topic_name = $this->generate_telegram_topic_name($session_id);
4299 -
4300 - // Random icon color (Telegram forum topic colors)
4301 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4302 - $icon_color = $icon_colors[array_rand($icon_colors)];
4303 -
4304 - // Create forum topic
4305 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4306 - 'headers' => ['Content-Type' => 'application/json'],
4307 - 'body' => json_encode([
4308 - 'chat_id' => $telegram_group_id,
4309 - 'name' => $topic_name,
4310 - 'icon_color' => $icon_color
4311 - ])
250 + $body = wp_json_encode([
251 + 'input' => $text,
252 + 'model' => 'text-embedding-ada-002'
4312 253 ]);
4313 254
4314 - if (!is_wp_error($response)) {
4315 - $response_body = wp_remote_retrieve_body($response);
4316 - $response_data = json_decode($response_body, true);
4317 -
4318 - if (isset($response_data['ok']) && $response_data['ok']) {
4319 - $topic_id = $response_data['result']['message_thread_id'];
4320 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4321 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4322 - }
4323 - }
4324 -
4325 - if (empty($topic_id)) {
4326 - return false; // Failed to create topic
4327 - }
4328 - }
4329 -
4330 - // Get recent chat history
4331 - $history = get_option("mxchat_history_{$session_id}", []);
4332 - $recent_history = array_slice($history, -5);
4333 -
4334 - // Format conversation context for Telegram (HTML format)
4335 - $conversation_context = "";
4336 - if (!empty($recent_history)) {
4337 - $conversation_context = "<b>Recent Conversation:</b>\n";
4338 - foreach ($recent_history as $hist_message) {
4339 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4340 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4341 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4342 - }
4343 - $conversation_context .= "\n";
4344 - }
4345 -
4346 - // Get user info
4347 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4348 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4349 -
4350 - // Update session mode
4351 - update_option("mxchat_mode_{$session_id}", 'agent');
4352 -
4353 - // Send initial message to topic
4354 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4355 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4356 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4357 - $topic_message .= "<b>User:</b> {$user_name}\n";
4358 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4359 -
4360 - if (!empty($conversation_context)) {
4361 - $topic_message .= $conversation_context;
4362 - }
4363 -
4364 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4365 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4366 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4367 -
4368 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4369 - 'headers' => ['Content-Type' => 'application/json'],
4370 - 'body' => json_encode([
4371 - 'chat_id' => $telegram_group_id,
4372 - 'message_thread_id' => $topic_id,
4373 - 'text' => $topic_message,
4374 - 'parse_mode' => 'HTML'
4375 - ])
4376 - ]);
4377 -
4378 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
4379 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4380 -
4381 - $this->fallbackResponse = [
4382 - 'text' => $success_message,
4383 - 'html' => '',
4384 - 'images' => [],
4385 - 'chat_mode' => 'agent'
4386 - ];
4387 -
4388 - wp_send_json([
4389 - 'success' => true,
4390 - 'text' => $success_message,
4391 - 'html' => '',
4392 - 'chat_mode' => 'agent',
4393 - 'session_id' => $session_id,
4394 - 'fallbackResponse' => $this->fallbackResponse
4395 - ]);
4396 - wp_die();
4397 -}
4398 -
4399 -/**
4400 - * Generate topic name for Telegram forum
4401 - */
4402 -private function generate_telegram_topic_name($session_id) {
4403 - $name = null;
4404 - $email = null;
4405 -
4406 - // Check logged in user
4407 - if (is_user_logged_in()) {
4408 - $current_user = wp_get_current_user();
4409 - if (!empty($current_user->display_name)) {
4410 - $name = $current_user->display_name;
4411 - }
4412 - if (!empty($current_user->user_email)) {
4413 - $email = $current_user->user_email;
4414 - }
4415 - }
4416 -
4417 - // Check session data
4418 - if (empty($name)) {
4419 - $name = get_option("mxchat_name_{$session_id}");
4420 - }
4421 - if (empty($email)) {
4422 - $email = get_option("mxchat_email_{$session_id}");
4423 - }
4424 -
4425 - // Generate topic name
4426 - $session_suffix = substr($session_id, -6);
4427 -
4428 - if (!empty($name)) {
4429 - // Clean name for topic (max 128 chars in Telegram)
4430 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4431 - $clean_name = trim($clean_name);
4432 - if (strlen($clean_name) > 50) {
4433 - $clean_name = substr($clean_name, 0, 50);
4434 - }
4435 - return "Chat - {$clean_name} ({$session_suffix})";
4436 - } elseif (!empty($email)) {
4437 - // Use email prefix
4438 - $email_prefix = explode('@', $email)[0];
4439 - if (strlen($email_prefix) > 30) {
4440 - $email_prefix = substr($email_prefix, 0, 30);
4441 - }
4442 - return "Chat - {$email_prefix} ({$session_suffix})";
4443 - }
4444 -
4445 - return "Chat - {$session_suffix}";
4446 -}
4447 -
4448 -/**
4449 - * Send user message to Telegram agent
4450 - */
4451 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4452 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4453 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4454 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4455 -
4456 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4457 - return false;
4458 - }
4459 -
4460 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4461 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4462 -
4463 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4464 - 'headers' => ['Content-Type' => 'application/json'],
4465 - 'body' => json_encode([
4466 - 'chat_id' => $group_id,
4467 - 'message_thread_id' => $topic_id,
4468 - 'text' => $user_message,
4469 - 'parse_mode' => 'HTML'
4470 - ])
4471 - ]);
4472 -
4473 - return !is_wp_error($response);
4474 -}
4475 -
4476 -/**
4477 - * Handle incoming Telegram webhook
4478 - */
4479 -public function handle_telegram_webhook(WP_REST_Request $request) {
4480 - $body = $request->get_body();
4481 - $data = json_decode($body, true);
4482 -
4483 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4484 -
4485 - // Handle message events from forum topics
4486 - if (isset($data['message'])) {
4487 - $message_data = $data['message'];
4488 -
4489 - // Skip if not from a forum topic
4490 - if (!isset($message_data['message_thread_id'])) {
4491 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4492 - return new WP_REST_Response(['ok' => true]);
4493 - }
4494 -
4495 - // Skip bot messages
4496 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4497 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4498 - return new WP_REST_Response(['ok' => true]);
4499 - }
4500 -
4501 - $chat_id = $message_data['chat']['id'] ?? '';
4502 - $topic_id = $message_data['message_thread_id'];
4503 - $message_text = $message_data['text'] ?? '';
4504 - $message_id = $message_data['message_id'] ?? '';
4505 - $from = $message_data['from'] ?? [];
4506 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4507 - if (empty($agent_name)) {
4508 - $agent_name = $from['username'] ?? 'Agent';
4509 - }
4510 -
4511 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4512 -
4513 - // Skip empty messages
4514 - if (empty($message_text)) {
4515 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4516 - return new WP_REST_Response(['ok' => true]);
4517 - }
4518 -
4519 - // Find session ID by topic ID - cast to string for comparison
4520 - global $wpdb;
4521 - $topic_id_str = strval($topic_id);
4522 - $session_option = $wpdb->get_var(
4523 - $wpdb->prepare(
4524 - "SELECT option_name FROM {$wpdb->options}
4525 - WHERE option_name LIKE %s
4526 - AND option_value = %s",
4527 - 'mxchat_telegram_topic_%',
4528 - $topic_id_str
4529 - )
4530 - );
4531 -
4532 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4533 -
4534 - if ($session_option) {
4535 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4536 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4537 -
4538 - // Verify the group ID matches
4539 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4540 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4541 -
4542 - if (strval($stored_group_id) != strval($chat_id)) {
4543 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4544 - return new WP_REST_Response(['ok' => true]);
4545 - }
4546 -
4547 - // Check for closure commands
4548 - $lower_text = strtolower(trim($message_text));
4549 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4550 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4551 - // End the live agent session
4552 - update_option("mxchat_mode_{$session_id}", 'ai');
4553 -
4554 - // Save disconnect message
4555 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4556 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4557 -
4558 - // Notify in Telegram
4559 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4560 - if (!empty($telegram_bot_token)) {
4561 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4562 - 'headers' => ['Content-Type' => 'application/json'],
4563 - 'body' => json_encode([
4564 - 'chat_id' => $chat_id,
4565 - 'message_thread_id' => $topic_id,
4566 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4567 - 'parse_mode' => 'HTML'
4568 - ])
4569 - ]);
4570 -
4571 - // Optionally close the topic
4572 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4573 - 'headers' => ['Content-Type' => 'application/json'],
4574 - 'body' => json_encode([
4575 - 'chat_id' => $chat_id,
4576 - 'message_thread_id' => $topic_id
4577 - ])
4578 - ]);
4579 - }
4580 -
4581 - return new WP_REST_Response(['ok' => true]);
4582 - }
4583 -
4584 - // Deduplicate messages
4585 - $message_key = md5($session_id . $message_id . $message_text);
4586 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4587 -
4588 - if (in_array($message_key, $processed_messages)) {
4589 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4590 - return new WP_REST_Response(['ok' => true]);
4591 - }
4592 -
4593 - $processed_messages[] = $message_key;
4594 - if (count($processed_messages) > 50) {
4595 - $processed_messages = array_slice($processed_messages, -50);
4596 - }
4597 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4598 -
4599 - // Save the agent message - format with agent name prefix for proper parsing
4600 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4601 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4602 -
4603 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4604 -
4605 - // Verify the message was saved to history
4606 - $history = get_option("mxchat_history_{$session_id}", []);
4607 - $last_message = end($history);
4608 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4609 -
4610 - // Send confirmation back to Telegram
4611 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4612 - if (!empty($telegram_bot_token)) {
4613 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4614 - if (!get_transient($confirm_key)) {
4615 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4616 - 'headers' => ['Content-Type' => 'application/json'],
4617 - 'body' => json_encode([
4618 - 'chat_id' => $chat_id,
4619 - 'message_thread_id' => $topic_id,
4620 - 'text' => "✅ <i>Message sent to user</i>",
4621 - 'parse_mode' => 'HTML',
4622 - 'reply_to_message_id' => $message_id
4623 - ])
4624 - ]);
4625 - set_transient($confirm_key, true, 300);
4626 - }
4627 - }
4628 - } else {
4629 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4630 - }
4631 - } else {
4632 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4633 - }
4634 -
4635 - return new WP_REST_Response(['ok' => true]);
4636 -}
4637 -
4638 -public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4639 - // Check if this is a Telegram agent session
4640 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4641 - if (!empty($telegram_topic_id)) {
4642 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4643 - }
4644 -
4645 - // Otherwise, try Slack
4646 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4647 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4648 -
4649 - if (empty($slack_bot_token) || empty($channel_id)) {
4650 - return false;
4651 - }
4652 -
4653 - $user_message = "💬 *User:* {$message}";
4654 -
4655 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
4656 - 'headers' => [
4657 - 'Content-Type' => 'application/json',
4658 - 'Authorization' => 'Bearer ' . $slack_bot_token
4659 - ],
4660 - 'body' => json_encode([
4661 - 'channel' => $channel_id,
4662 - 'text' => $user_message,
4663 - 'mrkdwn' => true
4664 - ])
4665 - ]);
4666 -
4667 - return !is_wp_error($response);
4668 -}
4669 -public function handle_slack_interaction(WP_REST_Request $request) {
4670 - //error_log('Received Slack interaction');
4671 -
4672 - $payload = json_decode($request->get_param('payload'), true);
4673 - //error_log('Payload: ' . print_r($payload, true));
4674 -
4675 - // Handle button click
4676 - if ($payload['type'] === 'block_actions' && $payload['actions'][0]['action_id'] === 'reply_to_user') {
4677 - $session_id = $payload['actions'][0]['value'];
4678 - $trigger_id = $payload['trigger_id'];
4679 -
4680 - // Get Bot Token from settings
4681 - $slack_token = $this->options['live_agent_bot_token'] ?? '';
4682 -
4683 - if (empty($slack_token)) {
4684 - //error_log('Slack Bot Token not configured');
4685 - return new WP_REST_Response(['error' => esc_html__('Bot token not configured', 'mxchat')], 400);
4686 - }
4687 - $response = wp_remote_post('https://slack.com/api/views.open', [
255 + $args = [
256 + 'body' => $body,
4688 257 'headers' => [
4689 258 'Content-Type' => 'application/json',
4690 - 'Authorization' => 'Bearer ' . $slack_token
259 + 'Authorization' => 'Bearer ' . $api_key,
4691 260 ],
4692 - 'body' => json_encode([
4693 - 'trigger_id' => $trigger_id,
4694 - 'view' => [
4695 - 'type' => 'modal',
4696 - 'callback_id' => 'reply_modal',
4697 - 'title' => [
4698 - 'type' => 'plain_text',
4699 - 'text' => __('Reply to User', 'mxchat')
4700 - ],
4701 - 'submit' => [
4702 - 'type' => 'plain_text',
4703 - 'text' => __('Send', 'mxchat')
4704 - ],
4705 - 'close' => [
4706 - 'type' => 'plain_text',
4707 - 'text' => __('Cancel', 'mxchat')
4708 - ],
4709 - 'blocks' => [
4710 - [
4711 - 'type' => 'input',
4712 - 'block_id' => 'reply_block',
4713 - 'label' => [
4714 - 'type' => 'plain_text',
4715 - 'text' => sprintf(__('Reply to session: %s', 'mxchat'), $session_id)
4716 - ],
4717 - 'element' => [
4718 - 'type' => 'plain_text_input',
4719 - 'action_id' => 'message',
4720 - 'multiline' => true,
4721 - 'placeholder' => [
4722 - 'type' => 'plain_text',
4723 - 'text' => __('Type your message here...', 'mxchat')
4724 - ]
4725 - ]
4726 - ]
4727 - ],
4728 - 'private_metadata' => $session_id
4729 - ]
4730 - ])
4731 - ]);
4732 -
4733 - //error_log('Views.open response: ' . print_r($response, true));
4734 -
4735 - // Return immediate acknowledgment
4736 - return new WP_REST_Response(['ok' => true]);
4737 - }
4738 -
4739 - // Handle modal submission
4740 -// Handle modal submission
4741 -if ($payload['type'] === 'view_submission') {
4742 - $session_id = $payload['view']['private_metadata'];
4743 - $message = $payload['view']['state']['values']['reply_block']['message']['value'];
4744 -
4745 - // Save the message (keep the message_id but don't include in response)
4746 - $this->mxchat_save_chat_message($session_id, 'agent', $message);
4747 -
4748 - // Keep the original response format for Slack
4749 - return new WP_REST_Response([
4750 - 'response_action' => 'clear'
4751 - ]);
4752 -}
4753 -
4754 - // Default acknowledgment
4755 - return new WP_REST_Response(['ok' => true]);
4756 -}
4757 -public function mxchat_handle_agent_response(WP_REST_Request $request) {
4758 - //error_log('Received agent response request');
4759 - //error_log('Request data: ' . print_r($request->get_params(), true));
4760 - // //error_log('Raw body: ' . file_get_contents('php://input'));
4761 -
4762 - // Get the data from Slack's slash command format
4763 - $command_text = $request->get_param('text');
4764 - // //error_log('Command text: ' . $command_text);
4765 -
4766 - if (empty($command_text)) {
4767 - //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
4768 - return new WP_REST_Response([
4769 - 'error' => esc_html__('Command text is required. Format: /reply session_id message', 'mxchat')
4770 - ], 400);
4771 - }
4772 -
4773 - // Split the command text into session_id and message
4774 - $parts = explode(' ', $command_text, 2);
4775 - if (count($parts) !== 2) {
4776 - //error_log('Agent response error: Invalid command format');
4777 - return new WP_REST_Response([
4778 - 'error' => esc_html__('Invalid format. Use: /reply session_id message', 'mxchat')
4779 - ], 400);
4780 - }
4781 -
4782 - $session_id = sanitize_text_field($parts[0]);
4783 - $message = sanitize_text_field($parts[1]);
4784 -
4785 - //error_log("Processing agent response - Session ID: $session_id, Message: $message");
4786 -
4787 - // Save the message
4788 - $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
4789 -
4790 - if (!$message_id) {
4791 - // //error_log('Failed to save agent message');
4792 - return new WP_REST_Response([
4793 - 'error' => esc_html__('Failed to save message', 'mxchat')
4794 - ], 500);
4795 - }
4796 -
4797 - // Return success response in Slack's expected format
4798 - return new WP_REST_Response([
4799 - 'response_type' => 'in_channel',
4800 - 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4801 - ], 200);
4802 -}
4803 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4804 - // Update mode to AI
4805 - update_option("mxchat_mode_{$session_id}", 'ai');
4806 -
4807 - // Clear any existing PDF context to start fresh
4808 - $this->clear_pdf_transients($session_id);
4809 -
4810 - // Set the response with explicit chat_mode
4811 - $this->fallbackResponse = [
4812 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4813 - 'html' => '',
4814 - 'images' => [],
4815 - 'chat_mode' => 'ai' // Ensure this is set
4816 - ];
4817 -
4818 - // Return the complete response array instead of just true
4819 - return $this->fallbackResponse;
4820 -}
4821 -
4822 -public function handle_slack_messages(WP_REST_Request $request) {
4823 - // Log the incoming request for debugging
4824 - //error_log('Slack events request received: ' . $request->get_body());
4825 -
4826 - $body = $request->get_body();
4827 - $data = json_decode($body, true);
4828 -
4829 - // Handle Slack URL verification
4830 - if (isset($data['type']) && $data['type'] === 'url_verification') {
4831 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
4832 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
4833 - }
4834 -
4835 - // IMPORTANT: Handle Slack's event deduplication
4836 - if (isset($data['event_id'])) {
4837 - $event_id = $data['event_id'];
4838 - $processed_events = get_transient('mxchat_slack_events') ?: [];
4839 -
4840 - // Check if we've already processed this event
4841 - if (in_array($event_id, $processed_events)) {
4842 - //error_log("Duplicate event detected: $event_id");
4843 - return new WP_REST_Response(['ok' => true]);
4844 - }
4845 -
4846 - // Add this event to processed list
4847 - $processed_events[] = $event_id;
4848 - // Keep only last 100 events to prevent memory issues
4849 - if (count($processed_events) > 100) {
4850 - $processed_events = array_slice($processed_events, -100);
4851 - }
4852 - // Store for 1 hour
4853 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
4854 - }
4855 -
4856 - // Handle message events
4857 - if (isset($data['event']) && $data['event']['type'] === 'message') {
4858 - $event = $data['event'];
4859 -
4860 - // Skip bot messages and messages with subtypes (like bot_message)
4861 - if (isset($event['bot_id']) || isset($event['subtype'])) {
4862 - return new WP_REST_Response(['ok' => true]);
4863 - }
4864 -
4865 - // Additional check: Skip if this is a threaded reply to our confirmation
4866 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
4867 - return new WP_REST_Response(['ok' => true]);
4868 - }
4869 -
4870 - $channel_id = $event['channel'];
4871 - $message_text = $event['text'] ?? '';
4872 - $message_ts = $event['ts'] ?? '';
4873 -
4874 - // Find session ID by looking for matching channel
4875 - global $wpdb;
4876 - $session_option = $wpdb->get_var(
4877 - $wpdb->prepare(
4878 - "SELECT option_name FROM {$wpdb->options}
4879 - WHERE option_name LIKE 'mxchat_channel_%'
4880 - AND option_value = %s",
4881 - $channel_id
4882 - )
4883 - );
4884 -
4885 - if ($session_option) {
4886 - $session_id = str_replace('mxchat_channel_', '', $session_option);
4887 -
4888 - // Create a unique key for this specific message
4889 - $message_key = md5($session_id . $message_ts . $message_text);
4890 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4891 -
4892 - // Check if we've already processed this exact message
4893 - if (in_array($message_key, $processed_messages)) {
4894 - //error_log("Duplicate message detected for session $session_id");
4895 - return new WP_REST_Response(['ok' => true]);
4896 - }
4897 -
4898 - // Add to processed messages
4899 - $processed_messages[] = $message_key;
4900 - // Keep only last 50 messages per session
4901 - if (count($processed_messages) > 50) {
4902 - $processed_messages = array_slice($processed_messages, -50);
4903 - }
4904 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4905 -
4906 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4907 -
4908 - // Handle agent ending the chat — transfer back to AI
4909 - // Format: "!endchat" or "!endchat <custom message to user>"
4910 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
4911 - update_option("mxchat_mode_{$session_id}", 'ai');
4912 -
4913 - // Extract custom message after !endchat, or use empty string
4914 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
4915 -
4916 - // Send the agent's custom farewell message if provided
4917 - if (!empty($custom_message)) {
4918 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
4919 - }
4920 -
4921 - // Confirm in Slack channel
4922 - if (!empty($slack_bot_token)) {
4923 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4924 - 'headers' => [
4925 - 'Content-Type' => 'application/json',
4926 - 'Authorization' => 'Bearer ' . $slack_bot_token
4927 - ],
4928 - 'body' => json_encode([
4929 - 'channel' => $channel_id,
4930 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
4931 - 'mrkdwn' => true
4932 - ])
4933 - ]);
4934 - }
4935 -
4936 - return new WP_REST_Response(['ok' => true]);
4937 - }
4938 -
4939 - // Save the agent message
4940 - $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4941 -
4942 - // Send confirmation back to Slack (only once)
4943 - if (!empty($slack_bot_token)) {
4944 - // Use a transient to prevent duplicate confirmations
4945 - $confirm_key = 'mxchat_confirm_' . $message_key;
4946 - if (!get_transient($confirm_key)) {
4947 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4948 - 'headers' => [
4949 - 'Content-Type' => 'application/json',
4950 - 'Authorization' => 'Bearer ' . $slack_bot_token
4951 - ],
4952 - 'body' => json_encode([
4953 - 'channel' => $channel_id,
4954 - 'text' => "✅ _Message sent to user_",
4955 - 'thread_ts' => $event['ts'] // Reply in thread
4956 - ])
4957 - ]);
4958 - // Set transient to prevent duplicate confirmations
4959 - set_transient($confirm_key, true, 300); // 5 minutes
4960 - }
4961 - }
4962 - }
4963 - }
4964 -
4965 - return new WP_REST_Response(['ok' => true]);
4966 -}
4967 -
4968 -// For the word upload handler
4969 -public function mxchat_handle_word_upload() {
4970 - // Delegate to word handler
4971 - $this->word_handler->mxchat_handle_word_upload();
4972 -}
4973 -
4974 -// For the word removal handler
4975 -public function mxchat_handle_word_remove() {
4976 - // Delegate to word handler
4977 - $this->word_handler->mxchat_handle_word_remove();
4978 -}
4979 -
4980 -// For the word status check
4981 -public function mxchat_check_word_status() {
4982 - // Delegate to word handler
4983 - $this->word_handler->mxchat_check_word_status();
4984 -}
4985 -
4986 -
4987 -private function mxchat_get_user_identifier() {
4988 - return MxChat_User::mxchat_get_user_identifier();
4989 -}
4990 -
4991 -private function mxchat_generate_embedding($text, $api_key) {
4992 - try {
4993 - // Get options and selected model
4994 - $options = get_option('mxchat_options');
4995 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4996 -
4997 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
4998 - // Off by default so existing sites see byte-identical behavior.
4999 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5000 - return $this->mxchat_generate_embedding_custom($text);
5001 - }
5002 -
5003 - // Determine endpoint and API key based on model
5004 - if (strpos($selected_model, 'voyage') === 0) {
5005 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
5006 - $api_key = $options['voyage_api_key'] ?? '';
5007 -
5008 - // Check if Voyage API key is missing
5009 - if (empty($api_key)) {
5010 - //error_log('Voyage API key is missing');
5011 - return [
5012 - 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5013 - 'error_code' => 'missing_voyage_api_key'
5014 - ];
5015 - }
5016 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5017 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5018 - $api_key = $options['gemini_api_key'] ?? '';
5019 -
5020 - // Check if Gemini API key is missing
5021 - if (empty($api_key)) {
5022 - //error_log('Gemini API key is missing');
5023 - return [
5024 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5025 - 'error_code' => 'missing_gemini_api_key'
5026 - ];
5027 - }
5028 - } else {
5029 - $endpoint = 'https://api.openai.com/v1/embeddings';
5030 - // Use the passed API key for OpenAI
5031 -
5032 - // Check if OpenAI API key is missing
5033 - if (empty($api_key)) {
5034 - //error_log('OpenAI API key is missing');
5035 - return [
5036 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
5037 - 'error_code' => 'missing_openai_api_key'
5038 - ];
5039 - }
5040 - }
5041 -
5042 - // Check if text is empty
5043 - if (empty($text)) {
5044 - //error_log('Empty text provided for embedding generation');
5045 - return [
5046 - 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
5047 - 'error_code' => 'empty_embedding_text'
5048 - ];
5049 - }
5050 -
5051 - // Prepare request body based on provider
5052 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5053 - // Gemini API format
5054 - $request_body = [
5055 - 'model' => 'models/' . $selected_model,
5056 - 'content' => [
5057 - 'parts' => [
5058 - ['text' => $text]
5059 - ]
5060 - ],
5061 - 'outputDimensionality' => 1536
5062 - ];
5063 -
5064 - // Prepare headers for Gemini (API key as query parameter)
5065 - $endpoint .= '?key=' . $api_key;
5066 - $headers = [
5067 - 'Content-Type' => 'application/json'
5068 - ];
5069 - } else {
5070 - // OpenAI/Voyage API format
5071 - $request_body = [
5072 - 'input' => $text,
5073 - 'model' => $selected_model
5074 - ];
5075 -
5076 - // Add output_dimension for voyage-3-large
5077 - if ($selected_model === 'voyage-3-large') {
5078 - $request_body['output_dimension'] = 2048;
5079 - }
5080 -
5081 - // Prepare headers for OpenAI/Voyage
5082 - $headers = [
5083 - 'Content-Type' => 'application/json',
5084 - 'Authorization' => 'Bearer ' . $api_key
5085 - ];
5086 - }
5087 -
5088 - // Prepare request arguments
5089 - $args = [
5090 - 'body' => wp_json_encode($request_body),
5091 - 'headers' => $headers,
5092 261 'timeout' => 60,
5093 262 'redirection' => 5,
5094 263 'blocking' => true,
5095 264 'httpversion' => '1.0',
@@ -5094,1810 +263,85 @@
5094 263 'blocking' => true,
5095 264 'httpversion' => '1.0',
5096 265 'sslverify' => true,
5097 266 ];
5098 -
5099 - // Make the request
267 +
5100 268 $response = wp_remote_post($endpoint, $args);
5101 -
5102 - // Handle WordPress errors
269 +
5103 270 if (is_wp_error($response)) {
5104 - $error_message = $response->get_error_message();
5105 - //error_log('Embedding Generation Error: ' . $error_message);
5106 - return [
5107 - 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
5108 - 'error_code' => 'embedding_connection_error'
5109 - ];
271 + return null;
5110 272 }
5111 -
5112 - // Check HTTP status code
5113 - $status_code = wp_remote_retrieve_response_code($response);
5114 - if ($status_code !== 200) {
5115 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5116 -
5117 - $error_message = isset($response_body['error']['message'])
5118 - ? $response_body['error']['message']
5119 - : 'HTTP Error ' . $status_code;
5120 -
5121 - $error_type = isset($response_body['error']['type'])
5122 - ? $response_body['error']['type']
5123 - : 'unknown';
5124 -
5125 - //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
5126 -
5127 - // Handle specific error types
5128 - switch ($error_type) {
5129 - case 'invalid_request_error':
5130 - if (strpos($error_message, 'API key') !== false) {
5131 - return [
5132 - 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
5133 - 'error_code' => 'embedding_invalid_api_key'
5134 - ];
5135 - }
5136 - break;
5137 -
5138 - case 'authentication_error':
5139 - return [
5140 - 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
5141 - 'error_code' => 'embedding_auth_error'
5142 - ];
5143 -
5144 - case 'rate_limit_exceeded':
5145 - return [
5146 - 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
5147 - 'error_code' => 'embedding_rate_limit'
5148 - ];
5149 -
5150 - case 'quota_exceeded':
5151 - return [
5152 - 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
5153 - 'error_code' => 'embedding_quota_exceeded'
5154 - ];
5155 - }
5156 -
5157 - // Generic error fallback
5158 - return [
5159 - 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
5160 - 'error_code' => 'embedding_api_error',
5161 - 'status_code' => $status_code
5162 - ];
5163 - }
5164 -
273 +
5165 274 $response_body = json_decode(wp_remote_retrieve_body($response), true);
5166 -
5167 - // Handle different response formats based on provider
5168 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5169 - // Gemini API response format
5170 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5171 - return $response_body['embedding']['values'];
5172 - } else {
5173 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5174 - return [
5175 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5176 - 'error_code' => 'invalid_gemini_embedding_response'
5177 - ];
5178 - }
5179 - } else {
5180 - // OpenAI/Voyage API response format
5181 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5182 - return $response_body['data'][0]['embedding'];
5183 - } else {
5184 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5185 - return [
5186 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5187 - 'error_code' => 'invalid_embedding_response'
5188 - ];
5189 - }
5190 - }
5191 - } catch (Exception $e) {
5192 - //error_log('Embedding Exception: ' . $e->getMessage());
5193 - return [
5194 - 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
5195 - 'error_code' => 'embedding_exception'
5196 - ];
5197 - }
5198 -}
5199 275
5200 -
5201 -/**
5202 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5203 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5204 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5205 - */
5206 -private function mxchat_generate_embedding_custom($text) {
5207 - if (empty($text)) {
5208 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5209 - }
5210 - $cfg = $this->mxchat_resolve_custom_provider();
5211 - if (empty($cfg['base_url'])) {
5212 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5213 - }
5214 -
5215 - $options = get_option('mxchat_options');
5216 - $embed_url = $cfg['base_url'] . '/embeddings';
5217 - if (!empty($cfg['api_version'])) {
5218 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5219 - }
5220 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5221 - ? trim((string) $options['custom_provider_embedding_model'])
5222 - : $cfg['model'];
5223 -
5224 - $response = wp_remote_post($embed_url, [
5225 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5226 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5227 - 'timeout' => 60,
5228 - ]);
5229 - if (is_wp_error($response)) {
5230 - return [
5231 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5232 - 'error_code' => 'embedding_custom_connection_error',
5233 - ];
5234 - }
5235 - $status = wp_remote_retrieve_response_code($response);
5236 - $body = json_decode(wp_remote_retrieve_body($response), true);
5237 - if ($status !== 200) {
5238 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5239 - return [
5240 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5241 - 'error_code' => 'embedding_custom_api_error',
5242 - 'status_code' => $status,
5243 - ];
5244 - }
5245 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5246 - return $body['data'][0]['embedding'];
5247 - }
5248 - return [
5249 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5250 - 'error_code' => 'embedding_custom_invalid_response',
5251 - ];
5252 -}
5253 -
5254 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5255 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5256 -
5257 - // Check for OpenAI Vector Store first (takes priority when enabled)
5258 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5259 -
5260 - if ($bot_vectorstore_config['use_vectorstore']) {
5261 - // Get current model to verify it's an OpenAI model
5262 - $bot_options = $this->get_bot_options($bot_id);
5263 - $mxchat_options = get_option('mxchat_options', array());
5264 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5265 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5266 -
5267 - if ($this->is_openai_chat_model($selected_model)) {
5268 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5269 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
276 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
277 + return $response_body['data'][0]['embedding'];
5270 278 } else {
5271 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
279 + return null;
5272 280 }
5273 281 }
5274 282
5275 - // Get bot-specific Pinecone configuration
5276 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5277 -
5278 - // Debug: Log the Pinecone configuration
5279 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5280 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5281 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5282 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5283 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5284 -
5285 - // Determine whether to use Pinecone based on bot configuration
5286 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5287 -
5288 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5289 -
5290 - if ($use_pinecone) {
5291 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
5292 - } else {
5293 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
5294 - }
5295 -}
5296 -
5297 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
283 +private function mxchat_find_relevant_content($user_embedding) {
5298 284 global $wpdb;
5299 285 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5300 - // Initialize similarity analysis storage
5301 - $this->last_similarity_analysis = [
5302 - 'knowledge_base_type' => 'WordPress Database',
5303 - 'bot_id' => $bot_id,
5304 - 'top_matches' => [],
5305 - 'threshold_used' => 0,
5306 - 'total_checked' => 0
5307 - ];
5308 286
5309 - // NEW: Initialize valid URLs array
5310 - $valid_urls = [];
287 + // Define a cache key for embeddings
288 + $cache_key = 'mxchat_system_prompt_embeddings';
5311 289
5312 - // Get bot-specific options for similarity threshold
5313 - $bot_options = $this->get_bot_options($bot_id);
5314 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
290 + // Attempt to get the embeddings from the cache
291 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
5315 292
5316 - // Get knowledge manager instance for role checking
5317 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
293 + if ($embeddings === false) {
294 + // Cache miss, query the database and cache the results
295 + $query = "SELECT id, embedding_vector FROM {$system_prompt_table}";
296 + $embeddings = $wpdb->get_results($query);
5318 297
5319 - // Get base similarity threshold from bot options or default options
5320 - $similarity_threshold = isset($current_options['similarity_threshold'])
5321 - ? ((int) $current_options['similarity_threshold']) / 100
5322 - : 0.35;
5323 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
298 + if ($embeddings === null || empty($embeddings)) {
299 + error_log("No embeddings found in the database.");
300 + return null; // Return null to handle no embeddings gracefully
301 + }
5324 302
5325 - // Precompute bot_filter once, outside the streaming loop
5326 - $bot_filter = '';
5327 - if ($bot_id !== 'default') {
5328 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5329 - if ($column_exists) {
5330 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5331 - }
303 + // Cache the results if successful
304 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600); // Cache for 1 hour
5332 305 }
5333 306
5334 - // ===== STREAMING TOP-K PASS =====
5335 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5336 - // - top 10 by raw similarity (for the testing/debug display panel)
5337 - // - candidates above threshold with access (capped) for context assembly
5338 - // This bounds peak memory regardless of knowledge base size and avoids loading
5339 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5340 - $batch_size = 250;
5341 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5342 - $top_display = [];
5343 - $candidates = [];
5344 - $total_checked = 0;
5345 - $offset = 0;
307 + $most_relevant_id = null;
308 + $highest_similarity = -INF;
5346 309
5347 - do {
5348 - $batch = $wpdb->get_results($wpdb->prepare(
5349 - "SELECT id, embedding_vector, source_url, role_restriction
5350 - FROM {$system_prompt_table}
5351 - WHERE 1=1 {$bot_filter}
5352 - LIMIT %d OFFSET %d",
5353 - $batch_size,
5354 - $offset
5355 - ));
310 + foreach ($embeddings as $embedding) {
311 + $database_embedding = maybe_unserialize($embedding->embedding_vector);
5356 312
5357 - if (empty($batch)) {
5358 - break;
5359 - }
313 + // Debugging: Log the embeddings
314 + // if (!is_array($database_embedding)) {
315 + // error_log("Invalid database embedding format for ID {$embedding->id}: " . print_r($database_embedding, true));
316 + // continue;
317 + // }
5360 318
5361 - foreach ($batch as $row) {
5362 - $database_embedding = $row->embedding_vector
5363 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5364 - : null;
5365 -
5366 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5367 - unset($database_embedding);
5368 - continue;
5369 - }
5370 -
319 + if (is_array($user_embedding)) {
5371 320 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5372 - unset($database_embedding);
5373 321
5374 - $role_restriction = $row->role_restriction ?? 'public';
5375 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5376 - $source_url = $row->source_url ?? '';
322 + // Debugging: Log the similarity score
323 + // error_log("Calculated similarity for ID {$embedding->id}: {$similarity}");
5377 324
5378 - // Maintain top 10 display buffer (insert-if-beats-worst)
5379 - if (count($top_display) < 10) {
5380 - $top_display[] = [
5381 - 'id' => $row->id,
5382 - 'similarity' => $similarity,
5383 - 'source_url' => $source_url,
5384 - 'role_restriction' => $role_restriction,
5385 - 'has_access' => $has_access,
5386 - ];
5387 - usort($top_display, function ($a, $b) {
5388 - return $b['similarity'] <=> $a['similarity'];
5389 - });
5390 - } elseif ($similarity > $top_display[9]['similarity']) {
5391 - $top_display[9] = [
5392 - 'id' => $row->id,
5393 - 'similarity' => $similarity,
5394 - 'source_url' => $source_url,
5395 - 'role_restriction' => $role_restriction,
5396 - 'has_access' => $has_access,
5397 - ];
5398 - usort($top_display, function ($a, $b) {
5399 - return $b['similarity'] <=> $a['similarity'];
5400 - });
325 + if ($similarity > $highest_similarity) {
326 + $highest_similarity = $similarity;
327 + $most_relevant_id = $embedding->id;
5401 328 }
5402 -
5403 - // Track candidates for context assembly (above threshold + has access)
5404 - if ($similarity >= $similarity_threshold && $has_access) {
5405 - $candidates[] = [
5406 - 'id' => $row->id,
5407 - 'similarity' => $similarity,
5408 - 'source_url' => $source_url,
5409 - ];
5410 - }
5411 -
5412 - $total_checked++;
5413 - }
5414 -
5415 - unset($batch);
5416 -
5417 - // Trim candidates periodically to cap memory during long scans
5418 - if (count($candidates) > $max_candidates) {
5419 - usort($candidates, function ($a, $b) {
5420 - return $b['similarity'] <=> $a['similarity'];
5421 - });
5422 - $candidates = array_slice($candidates, 0, $max_candidates);
5423 - }
5424 -
5425 - $offset += $batch_size;
5426 - } while (true);
5427 -
5428 - if ($total_checked === 0) {
5429 - $this->current_valid_urls = [];
5430 - return '';
5431 - }
5432 -
5433 - // Final candidates sort (best first)
5434 - if (count($candidates) > 1) {
5435 - usort($candidates, function ($a, $b) {
5436 - return $b['similarity'] <=> $a['similarity'];
5437 - });
5438 - }
5439 -
5440 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5441 - // Gather unique IDs we actually need (top_display + candidates) and pull
5442 - // article_content in bounded IN() batches. This avoids loading content for
5443 - // every row during the similarity scan.
5444 - $needed_ids = [];
5445 - foreach ($top_display as $item) {
5446 - $needed_ids[$item['id']] = true;
5447 - }
5448 - foreach ($candidates as $item) {
5449 - $needed_ids[$item['id']] = true;
5450 - }
5451 - $needed_ids = array_keys($needed_ids);
5452 -
5453 - $content_map = [];
5454 - if (!empty($needed_ids)) {
5455 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5456 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5457 - $rows = $wpdb->get_results($wpdb->prepare(
5458 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5459 - ...$chunk_ids
5460 - ));
5461 - foreach ($rows as $r) {
5462 - $content_map[$r->id] = $r->article_content;
5463 - }
5464 - unset($rows);
5465 - }
5466 - }
5467 -
5468 - // Build the all_similarities display array from the top 10
5469 - $all_similarities = [];
5470 - foreach ($top_display as $item) {
5471 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5472 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5473 - $is_chunk = $parsed_for_display['is_chunked'];
5474 - $chunk_meta = $parsed_for_display['metadata'];
5475 -
5476 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5477 - $source_display = $item['source_url'];
5478 329 } else {
5479 - $content_preview = strip_tags($article_content_for_parse);
5480 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5481 - $source_display = substr(trim($content_preview), 0, 50) . '...';
330 + // error_log("User embedding is not an array. Embedding data: " . print_r($user_embedding, true));
5482 331 }
5483 -
5484 - $all_similarities[] = [
5485 - 'document_id' => $item['id'],
5486 - 'similarity' => $item['similarity'],
5487 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5488 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5489 - 'source_display' => $source_display,
5490 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5491 - 'used_for_context' => false,
5492 - 'role_restriction' => $item['role_restriction'],
5493 - 'has_access' => $item['has_access'],
5494 - 'filtered_out' => !$item['has_access'],
5495 - 'is_chunk' => $is_chunk,
5496 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5497 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5498 - ];
5499 332 }
5500 333
5501 - // Build url_groups from candidates for chunk reassembly
5502 - $url_groups = array();
5503 - foreach ($candidates as $cand) {
5504 - $article_content = $content_map[$cand['id']] ?? '';
5505 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5506 - $is_chunked = $parsed['is_chunked'];
5507 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5508 - $text_content = $parsed['text'];
5509 -
5510 - $source_url = $cand['source_url'];
5511 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5512 -
5513 - if (!isset($url_groups[$group_key])) {
5514 - $url_groups[$group_key] = array(
5515 - 'source_url' => $source_url,
5516 - 'best_score' => 0,
5517 - 'is_chunked' => $is_chunked,
5518 - 'chunks' => array(),
5519 - 'single_text' => '',
5520 - 'single_id' => null
5521 - );
5522 - }
5523 -
5524 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5525 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5526 - }
5527 -
5528 - if ($is_chunked) {
5529 - $url_groups[$group_key]['is_chunked'] = true;
5530 - $url_groups[$group_key]['chunks'][] = array(
5531 - 'id' => $cand['id'],
5532 - 'score' => $cand['similarity'],
5533 - 'chunk_index' => $chunk_index,
5534 - 'text' => $text_content
5535 - );
5536 - } else {
5537 - $url_groups[$group_key]['single_text'] = $text_content;
5538 - $url_groups[$group_key]['single_id'] = $cand['id'];
5539 - }
334 + if ($most_relevant_id !== null) {
335 + // Fetch content with product links
336 + return $this->fetch_content_with_product_links($most_relevant_id);
5540 337 }
5541 338
5542 - // Sort ALL similarities for testing display (highest first)
5543 - usort($all_similarities, function ($a, $b) {
5544 - return $b['similarity'] <=> $a['similarity'];
5545 - });
5546 -
5547 - // Sort URL groups by best score (highest first)
5548 - uasort($url_groups, function($a, $b) {
5549 - return $b['best_score'] <=> $a['best_score'];
5550 - });
5551 -
5552 - // Get RAG sources limit from options (default 6, min 3, max 10)
5553 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5554 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5555 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5556 -
5557 - // Take top N unique URLs based on user setting
5558 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5559 -
5560 - // Track which document IDs are used for context
5561 - $used_document_ids = [];
5562 - foreach ($top_urls as $group) {
5563 - if ($group['is_chunked']) {
5564 - foreach ($group['chunks'] as $chunk) {
5565 - $used_document_ids[] = $chunk['id'];
5566 - }
5567 - } elseif ($group['single_id']) {
5568 - $used_document_ids[] = $group['single_id'];
5569 - }
5570 - }
5571 -
5572 - // Update the all_similarities array to mark which were actually used
5573 - foreach ($all_similarities as &$similarity_item) {
5574 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5575 - }
5576 -
5577 - // Store top 10 for testing panel
5578 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5579 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5580 -
5581 - // Initialize final content
5582 - $content = '';
5583 - $matches_used = 0;
5584 - $total_chunks_used = 0;
5585 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5586 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5587 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5588 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5589 -
5590 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5591 - // Use fresh options to ensure we get the latest setting value
5592 - $fresh_options = get_option('mxchat_options', []);
5593 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5594 -
5595 - // Build content from top sources
5596 - foreach ($top_urls as $group_key => $group) {
5597 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5598 -
5599 - // Stop if we've hit the total chunk limit
5600 - if ($total_chunks_used >= $max_total_chunks) {
5601 - break;
5602 - }
5603 -
5604 - $full_text = '';
5605 - $chunks_in_this_source = 1; // Default for non-chunked content
5606 -
5607 - if ($group['is_chunked']) {
5608 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5609 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5610 -
5611 - // Fetch chunks for this URL with limit
5612 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5613 -
5614 - // If fetching all chunks fails, fall back to matched chunks
5615 - if (empty($full_text)) {
5616 - // Sort matched chunks by index and concatenate
5617 - usort($group['chunks'], function($a, $b) {
5618 - return $a['chunk_index'] <=> $b['chunk_index'];
5619 - });
5620 -
5621 - $chunk_texts = array();
5622 - $chunks_in_this_source = 0;
5623 - foreach ($group['chunks'] as $chunk) {
5624 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5625 - break;
5626 - }
5627 - $chunk_texts[] = $chunk['text'];
5628 - $chunks_in_this_source++;
5629 - }
5630 - $full_text = implode("\n\n", $chunk_texts);
5631 - }
5632 - } else {
5633 - $full_text = $group['single_text'];
5634 - $chunks_in_this_source = 1;
5635 - }
5636 -
5637 - if (!empty($full_text)) {
5638 - // Strip URLs from content if citation links are disabled
5639 - if (!$citation_links_enabled) {
5640 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5641 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5642 - }
5643 -
5644 - // Use numbered reference for URL-based entries, plain info label for manual entries
5645 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5646 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5647 - $matches_used++;
5648 - $content .= "## Reference " . $matches_used . " ##\n";
5649 - $content .= $full_text . "\n\n";
5650 -
5651 - // Only include citation URLs if citation links are enabled
5652 - if ($citation_links_enabled) {
5653 - $valid_urls[] = $source_url;
5654 - $content .= "URL: " . $source_url . "\n\n";
5655 - }
5656 - } else {
5657 - // Manual entry — no reference number, no citation
5658 - $content .= "## Information ##\n";
5659 - $content .= $full_text . "\n\n";
5660 - }
5661 -
5662 - // Extract any URLs from the text content itself (only if citation links enabled)
5663 - if ($citation_links_enabled) {
5664 - preg_match_all(
5665 - '#\bhttps?://[^\s<>"\']+#i',
5666 - $full_text,
5667 - $content_urls
5668 - );
5669 - if (!empty($content_urls[0])) {
5670 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5671 - }
5672 - }
5673 -
5674 - $total_chunks_used += $chunks_in_this_source;
5675 - }
5676 - }
5677 -
5678 - // NEW: Store unique valid URLs for validation
5679 - $this->current_valid_urls = array_unique($valid_urls);
5680 -
5681 - // Store sources and chunks counts for testing/transcript display
5682 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5683 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5684 -
5685 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5686 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5687 -
5688 - // Add response guidelines
5689 - if (empty($top_urls)) {
5690 - $content = "No reference information was found for this query.\n\n";
5691 - } else {
5692 - // Build response guidelines based on citation links setting
5693 - $content .= "\n## Response Guidelines ##\n" .
5694 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5695 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5696 - "If you don't have specific information or are uncertain about any details, it's always " .
5697 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5698 - "When information is incomplete, let them know you are unsure.\n\n";
5699 -
5700 - // Only add hyperlink instructions if citation links are enabled
5701 - if ($citation_links_enabled) {
5702 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5703 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5704 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5705 - } else {
5706 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5707 - "Simply provide helpful answers based on the reference information without citing sources.";
5708 - }
5709 - }
5710 -
5711 - return trim($content);
339 + error_log("No relevant content found. Most relevant ID was null.");
340 + return null; // Return null if no relevant content is found
5712 341 }
5713 342
5714 -/**
5715 - * Fetch and reassemble chunks for a URL from WordPress database
5716 - *
5717 - * @param string $source_url The source URL to fetch chunks for
5718 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5719 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5720 - * @return string Reassembled content from chunks
5721 - */
5722 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5723 - global $wpdb;
5724 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5725 343
5726 - // Fetch all rows with this source_url
5727 - $rows = $wpdb->get_results($wpdb->prepare(
5728 - "SELECT article_content FROM {$table}
5729 - WHERE source_url = %s
5730 - ORDER BY id ASC",
5731 - $source_url
5732 - ));
5733 -
5734 - if (empty($rows)) {
5735 - $chunk_count = 0;
5736 - return '';
5737 - }
5738 -
5739 - // Parse and sort chunks by index
5740 - $chunks = array();
5741 - foreach ($rows as $row) {
5742 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5743 -
5744 - if ($parsed['is_chunked']) {
5745 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5746 - $chunks[$chunk_index] = $parsed['text'];
5747 - } else {
5748 - // Non-chunked content - just return it
5749 - $chunks[] = $parsed['text'];
5750 - }
5751 - }
5752 -
5753 - // Sort by chunk index
5754 - ksort($chunks);
5755 -
5756 - // Apply chunk limit if specified
5757 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5758 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5759 - }
5760 -
5761 - // Store actual chunk count
5762 - $chunk_count = count($chunks);
5763 -
5764 - // Reassemble content
5765 - return implode("\n\n", $chunks);
5766 -}
5767 -
5768 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5769 - global $wpdb;
5770 -
5771 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5772 - //error_log(" - bot_id: " . $bot_id);
5773 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5774 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5775 -
5776 - // Use bot-specific config or fall back to default
5777 - if ($bot_config === null) {
5778 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5779 - }
5780 -
5781 - $api_key = $bot_config['api_key'] ?? '';
5782 - $host = $bot_config['host'] ?? '';
5783 - $namespace = $bot_config['namespace'] ?? '';
5784 -
5785 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5786 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5787 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5788 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5789 -
5790 - // Initialize similarity analysis storage
5791 - $this->last_similarity_analysis = [
5792 - 'knowledge_base_type' => 'Pinecone',
5793 - 'bot_id' => $bot_id,
5794 - 'namespace' => $namespace,
5795 - 'top_matches' => [],
5796 - 'threshold_used' => 0,
5797 - 'total_checked' => 0
5798 - ];
5799 -
5800 - // NEW: Initialize valid URLs array
5801 - $valid_urls = [];
5802 -
5803 - if (empty($host) || empty($api_key)) {
5804 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5805 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5806 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5807 - // Store empty array for valid URLs since we can't proceed
5808 - $this->current_valid_urls = [];
5809 - return '';
5810 - }
5811 -
5812 - // Get knowledge manager instance for role checking
5813 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5814 -
5815 - // Get the similarity threshold from the bot options or main options
5816 - $bot_options = $this->get_bot_options($bot_id);
5817 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
5818 -
5819 - $similarity_threshold = isset($current_options['similarity_threshold'])
5820 - ? ((int) $current_options['similarity_threshold']) / 100
5821 - : 0.35;
5822 -
5823 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5824 -
5825 - // Prepare the query request for Pinecone
5826 - $api_endpoint = "https://{$host}/query";
5827 -
5828 - $request_body = array(
5829 - 'vector' => $user_embedding,
5830 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
5831 - 'includeMetadata' => true,
5832 - 'includeValues' => true
5833 - );
5834 -
5835 - // Add namespace if specified for this bot
5836 - if (!empty($namespace)) {
5837 - $request_body['namespace'] = $namespace;
5838 - }
5839 -
5840 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5841 - //error_log(" - Endpoint: " . $api_endpoint);
5842 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5843 -
5844 - $response = wp_remote_post($api_endpoint, array(
5845 - 'headers' => array(
5846 - 'Api-Key' => $api_key,
5847 - 'accept' => 'application/json',
5848 - 'content-type' => 'application/json'
5849 - ),
5850 - 'body' => wp_json_encode($request_body),
5851 - 'timeout' => 30
5852 - ));
5853 -
5854 - if (is_wp_error($response)) {
5855 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5856 - // Store empty array for valid URLs
5857 - $this->current_valid_urls = [];
5858 - return '';
5859 - }
5860 -
5861 - $response_code = wp_remote_retrieve_response_code($response);
5862 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5863 -
5864 - if ($response_code !== 200) {
5865 - $response_body = wp_remote_retrieve_body($response);
5866 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5867 - // Store empty array for valid URLs
5868 - $this->current_valid_urls = [];
5869 - return '';
5870 - }
5871 -
5872 - // ADD DETAILED DEBUG SECTION HERE
5873 - $response_body = wp_remote_retrieve_body($response);
5874 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5875 -
5876 - $results = json_decode($response_body, true);
5877 -
5878 - if (json_last_error() !== JSON_ERROR_NONE) {
5879 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5880 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5881 - // Store empty array for valid URLs
5882 - $this->current_valid_urls = [];
5883 - return '';
5884 - }
5885 -
5886 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5887 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5888 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5889 -
5890 - if (empty($results['matches'])) {
5891 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5892 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5893 - // Store empty array for valid URLs
5894 - $this->current_valid_urls = [];
5895 - return '';
5896 - }
5897 -
5898 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
5899 -
5900 - // Log first match details for debugging
5901 - if (!empty($results['matches'][0])) {
5902 - $first_match = $results['matches'][0];
5903 - //error_log("MXCHAT DEBUG: First match details:");
5904 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
5905 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5906 - if (isset($first_match['metadata'])) {
5907 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5908 - }
5909 - }
5910 -
5911 - // Initialize the final content
5912 - $content = '';
5913 - $matches_used = 0;
5914 - $matches_used_for_context = [];
5915 - $total_chunks_used = 0;
5916 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5917 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5918 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5919 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5920 -
5921 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5922 - // Use fresh options to ensure we get the latest setting value
5923 - $fresh_options = get_option('mxchat_options', []);
5924 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5925 -
5926 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
5927 - $url_groups = array();
5928 -
5929 - foreach ($results['matches'] as $index => $match) {
5930 - // Skip if similarity is below threshold
5931 - if ($match['score'] < $similarity_threshold) {
5932 - continue;
5933 - }
5934 -
5935 - $metadata = $match['metadata'] ?? array();
5936 - $source_url = $metadata['source_url'] ?? '';
5937 - $match_id = $match['id'] ?? '';
5938 -
5939 - // LAZY ROLE CHECK: Only check role for content we're actually considering
5940 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
5941 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5942 -
5943 - // Skip if user doesn't have access
5944 - if (!$has_access) {
5945 - continue;
5946 - }
5947 -
5948 - // Use a unique key for manual entries without a source URL
5949 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
5950 -
5951 - // Group by source URL (or unique key for manual entries)
5952 - if (!isset($url_groups[$group_key])) {
5953 - $url_groups[$group_key] = array(
5954 - 'source_url' => $source_url,
5955 - 'best_score' => 0,
5956 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
5957 - 'chunks' => array(),
5958 - 'single_text' => ''
5959 - );
5960 - }
5961 -
5962 - // Track best score for this group
5963 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
5964 - $url_groups[$group_key]['best_score'] = $match['score'];
5965 - }
5966 -
5967 - // Store chunk info or single text
5968 - if ($url_groups[$group_key]['is_chunked']) {
5969 - $url_groups[$group_key]['chunks'][] = array(
5970 - 'id' => $match_id,
5971 - 'score' => $match['score'],
5972 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
5973 - 'text' => $metadata['text'] ?? ''
5974 - );
5975 - } else {
5976 - // Non-chunked content - just store the text
5977 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
5978 - $url_groups[$group_key]['single_id'] = $match_id;
5979 - }
5980 - }
5981 -
5982 - // Sort URL groups by best score (highest first)
5983 - uasort($url_groups, function($a, $b) {
5984 - return $b['best_score'] <=> $a['best_score'];
5985 - });
5986 -
5987 - // Get RAG sources limit from options (default 6, min 3, max 10)
5988 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5989 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5990 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5991 -
5992 - // Take top N unique URLs based on user setting
5993 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5994 -
5995 - // Track which match IDs are actually used for context
5996 - foreach ($top_urls as $group) {
5997 - if ($group['is_chunked']) {
5998 - foreach ($group['chunks'] as $chunk) {
5999 - $matches_used_for_context[] = $chunk['id'];
6000 - }
6001 - } elseif (!empty($group['single_id'])) {
6002 - $matches_used_for_context[] = $group['single_id'];
6003 - }
6004 - }
6005 -
6006 - // Build content from top sources
6007 - foreach ($top_urls as $group_key => $group) {
6008 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6009 -
6010 - // Stop if we've hit the total chunk limit
6011 - if ($total_chunks_used >= $max_total_chunks) {
6012 - break;
6013 - }
6014 -
6015 - $full_text = '';
6016 - $chunks_in_this_source = 1; // Default for non-chunked content
6017 -
6018 - if ($group['is_chunked']) {
6019 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6020 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6021 -
6022 - // Fetch chunks for this URL with limit
6023 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6024 -
6025 - // If fetching all chunks fails, fall back to matched chunks
6026 - if (empty($full_text)) {
6027 - // Sort matched chunks by index and concatenate
6028 - usort($group['chunks'], function($a, $b) {
6029 - return $a['chunk_index'] <=> $b['chunk_index'];
6030 - });
6031 -
6032 - $chunk_texts = array();
6033 - $chunks_in_this_source = 0;
6034 - foreach ($group['chunks'] as $chunk) {
6035 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6036 - break;
6037 - }
6038 - $chunk_texts[] = $chunk['text'];
6039 - $chunks_in_this_source++;
6040 - }
6041 - $full_text = implode("\n\n", $chunk_texts);
6042 - }
6043 - } else {
6044 - $full_text = $group['single_text'];
6045 - $chunks_in_this_source = 1;
6046 - }
6047 -
6048 - if (!empty($full_text)) {
6049 - // Strip URLs from content if citation links are disabled
6050 - if (!$citation_links_enabled) {
6051 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6052 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6053 - }
6054 -
6055 - // Use numbered reference for URL-based entries, plain info label for manual entries
6056 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6057 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6058 - $matches_used++;
6059 - $content .= "## Reference " . $matches_used . " ##\n";
6060 - $content .= $full_text . "\n\n";
6061 -
6062 - // Only include citation URLs if citation links are enabled
6063 - if ($citation_links_enabled) {
6064 - $valid_urls[] = $source_url;
6065 - $content .= "URL: " . $source_url . "\n\n";
6066 - }
6067 - } else {
6068 - // Manual entry — no reference number, no citation
6069 - $content .= "## Information ##\n";
6070 - $content .= $full_text . "\n\n";
6071 - }
6072 -
6073 - // Extract any URLs from the text content itself (only if citation links enabled)
6074 - if ($citation_links_enabled) {
6075 - preg_match_all(
6076 - '#\bhttps?://[^\s<>"\']+#i',
6077 - $full_text,
6078 - $content_urls
6079 - );
6080 - if (!empty($content_urls[0])) {
6081 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6082 - }
6083 - }
6084 -
6085 - $total_chunks_used += $chunks_in_this_source;
6086 - }
6087 - }
6088 -
6089 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6090 - $all_matches = [];
6091 - foreach ($results['matches'] as $index => $match) {
6092 - if ($index >= 10) break; // Limit to top 10 for testing
6093 -
6094 - $match_id = $match['id'] ?? '';
6095 -
6096 - // Check role access for testing display (use cache if available)
6097 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6098 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6099 -
6100 - $source_display = '';
6101 - if (!empty($match['metadata']['source_url'])) {
6102 - $source_display = $match['metadata']['source_url'];
6103 - } else {
6104 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6105 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6106 - $source_display = substr(trim($content_preview), 0, 50) . '...';
6107 - }
6108 -
6109 - $match_id_for_display = $match['id'] ?? $index;
6110 -
6111 - // Check for chunk metadata in Pinecone
6112 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6113 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6114 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6115 -
6116 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6117 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6118 - $is_chunk = true;
6119 - }
6120 -
6121 - $all_matches[] = [
6122 - 'document_id' => $match_id_for_display,
6123 - 'similarity' => $match['score'],
6124 - 'similarity_percentage' => round($match['score'] * 100, 2),
6125 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6126 - 'source_display' => $source_display,
6127 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6128 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6129 - 'role_restriction' => $role_restriction,
6130 - 'has_access' => $has_access,
6131 - 'filtered_out' => !$has_access,
6132 - 'is_chunk' => $is_chunk,
6133 - 'chunk_index' => $chunk_index,
6134 - 'total_chunks' => $total_chunks
6135 - ];
6136 - }
6137 -
6138 - // Store for testing panel
6139 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6140 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6141 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6142 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6143 -
6144 - // NEW: Store unique valid URLs for validation
6145 - $this->current_valid_urls = array_unique($valid_urls);
6146 -
6147 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6148 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6149 -
6150 - // Add response guidelines
6151 - if ($matches_used === 0) {
6152 - $content = "No reference information was found for this query.\n\n";
6153 - } else {
6154 - // Build response guidelines based on citation links setting
6155 - $content .= "\n## Response Guidelines ##\n" .
6156 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6157 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6158 - "If you don't have specific information or are uncertain about any details, it's always " .
6159 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6160 - "When information is incomplete, let them know you are unsure.\n\n";
6161 -
6162 - // Only add hyperlink instructions if citation links are enabled
6163 - if ($citation_links_enabled) {
6164 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6165 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6166 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6167 - } else {
6168 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6169 - "Simply provide helpful answers based on the reference information without citing sources.";
6170 - }
6171 - }
6172 -
6173 - return trim($content);
6174 -}
6175 -
6176 -/**
6177 - * Get role restriction for a single vector (with caching)
6178 - */
6179 -private function get_single_vector_role($vector_id, $metadata = array()) {
6180 - global $wpdb;
6181 -
6182 - if (empty($vector_id)) {
6183 - return 'public';
6184 - }
6185 -
6186 - // Check cache first
6187 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6188 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6189 -
6190 - if ($cached_role !== false) {
6191 - return $cached_role;
6192 - }
6193 -
6194 - $role_restriction = 'public';
6195 -
6196 - // First try Pinecone metadata
6197 - if (!empty($metadata['role_restriction'])) {
6198 - $role_restriction = $metadata['role_restriction'];
6199 - } else {
6200 - // Check WordPress table for user-modified roles
6201 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6202 - $stored_role = $wpdb->get_var($wpdb->prepare(
6203 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6204 - $vector_id
6205 - ));
6206 -
6207 - if ($stored_role) {
6208 - $role_restriction = $stored_role;
6209 - }
6210 - }
6211 -
6212 - // Cache individual role for 1 hour
6213 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6214 -
6215 - return $role_restriction;
6216 -}
6217 -
6218 -/**
6219 - * Fetch and reassemble all chunks for a URL from Pinecone
6220 - *
6221 - * @param string $source_url The source URL to fetch chunks for
6222 - * @param array $bot_config Bot-specific Pinecone configuration
6223 - * @return string Reassembled content from all chunks
6224 - */
6225 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6226 - $api_key = $bot_config['api_key'] ?? '';
6227 - $host = $bot_config['host'] ?? '';
6228 - $namespace = $bot_config['namespace'] ?? '';
6229 -
6230 - if (empty($host) || empty($api_key)) {
6231 - $chunk_count = 0;
6232 - return '';
6233 - }
6234 -
6235 - $base_hash = md5($source_url);
6236 -
6237 - // Use Pinecone list API to find all chunk vectors with this prefix
6238 - $list_url = "https://{$host}/vectors/list";
6239 -
6240 - // Limit to max_chunks if specified, otherwise fetch up to 100
6241 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6242 -
6243 - $list_body = array(
6244 - 'prefix' => $base_hash . '_chunk_',
6245 - 'limit' => $fetch_limit
6246 - );
6247 -
6248 - if (!empty($namespace)) {
6249 - $list_body['namespace'] = $namespace;
6250 - }
6251 -
6252 - $list_response = wp_remote_post($list_url, array(
6253 - 'headers' => array(
6254 - 'Api-Key' => $api_key,
6255 - 'accept' => 'application/json',
6256 - 'content-type' => 'application/json'
6257 - ),
6258 - 'body' => wp_json_encode($list_body),
6259 - 'timeout' => 30
6260 - ));
6261 -
6262 - if (is_wp_error($list_response)) {
6263 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6264 - return '';
6265 - }
6266 -
6267 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6268 -
6269 - if (empty($list_data['vectors'])) {
6270 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6271 - return '';
6272 - }
6273 -
6274 - // Extract vector IDs
6275 - $vector_ids = array();
6276 - foreach ($list_data['vectors'] as $vector) {
6277 - if (isset($vector['id'])) {
6278 - $vector_ids[] = $vector['id'];
6279 - }
6280 - }
6281 -
6282 - if (empty($vector_ids)) {
6283 - return '';
6284 - }
6285 -
6286 - // Fetch all chunk content
6287 - $fetch_url = "https://{$host}/vectors/fetch";
6288 -
6289 - $fetch_body = array(
6290 - 'ids' => $vector_ids
6291 - );
6292 -
6293 - if (!empty($namespace)) {
6294 - $fetch_body['namespace'] = $namespace;
6295 - }
6296 -
6297 - $fetch_response = wp_remote_post($fetch_url, array(
6298 - 'headers' => array(
6299 - 'Api-Key' => $api_key,
6300 - 'accept' => 'application/json',
6301 - 'content-type' => 'application/json'
6302 - ),
6303 - 'body' => wp_json_encode($fetch_body),
6304 - 'timeout' => 30
6305 - ));
6306 -
6307 - if (is_wp_error($fetch_response)) {
6308 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6309 - return '';
6310 - }
6311 -
6312 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6313 -
6314 - if (empty($fetch_data['vectors'])) {
6315 - return '';
6316 - }
6317 -
6318 - // Sort chunks by index and reassemble
6319 - $chunks = array();
6320 - foreach ($fetch_data['vectors'] as $id => $vector) {
6321 - $metadata = $vector['metadata'] ?? array();
6322 - $chunk_index = $metadata['chunk_index'] ?? 0;
6323 - $text = $metadata['text'] ?? '';
6324 -
6325 - // Store chunk with its index
6326 - $chunks[$chunk_index] = $text;
6327 - }
6328 -
6329 - // Sort by chunk index
6330 - ksort($chunks);
6331 -
6332 - // Apply chunk limit if specified
6333 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6334 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6335 - }
6336 -
6337 - // Store actual chunk count
6338 - $chunk_count = count($chunks);
6339 -
6340 - // Reassemble content
6341 - return implode("\n\n", $chunks);
6342 -}
6343 -
6344 -/**
6345 - * Search for relevant content using OpenAI Vector Store (File Search)
6346 - *
6347 - * @param string $user_query The user's query text
6348 - * @param string $bot_id The bot ID
6349 - * @param array $vectorstore_config Vector Store configuration
6350 - * @return string Formatted context string with references
6351 - */
6352 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6353 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6354 - //error_log(" - bot_id: " . $bot_id);
6355 - //error_log(" - user_query length: " . strlen($user_query));
6356 -
6357 - // Get OpenAI API key
6358 - $mxchat_options = get_option('mxchat_options', array());
6359 - $api_key = $mxchat_options['api_key'] ?? '';
6360 -
6361 - // Reset vectorstore error tracking
6362 - $this->last_vectorstore_error = null;
6363 -
6364 - if (empty($api_key)) {
6365 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6366 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6367 - $this->current_valid_urls = [];
6368 - return '';
6369 - }
6370 -
6371 - // Get Vector Store configuration
6372 - if (empty($vectorstore_config)) {
6373 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6374 - }
6375 -
6376 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6377 - $max_results = $vectorstore_config['max_results'] ?? 5;
6378 -
6379 - if (empty($vectorstore_ids_string)) {
6380 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6381 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6382 - $this->current_valid_urls = [];
6383 - return '';
6384 - }
6385 -
6386 - // Parse Vector Store IDs
6387 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6388 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6389 -
6390 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6391 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6392 -
6393 - // Initialize similarity analysis storage
6394 - $this->last_similarity_analysis = [
6395 - 'knowledge_base_type' => 'OpenAI Vector Store',
6396 - 'bot_id' => $bot_id,
6397 - 'vectorstore_ids' => $vectorstore_ids,
6398 - 'top_matches' => [],
6399 - 'threshold_used' => 0,
6400 - 'total_checked' => 0
6401 - ];
6402 -
6403 - $valid_urls = [];
6404 -
6405 - // Get the selected model
6406 - $bot_options = $this->get_bot_options($bot_id);
6407 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6408 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6409 -
6410 - // Verify it's an OpenAI model
6411 - if (!$this->is_openai_chat_model($selected_model)) {
6412 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6413 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6414 - $this->current_valid_urls = [];
6415 - return '';
6416 - }
6417 -
6418 - // Use OpenAI Responses API with file_search tool
6419 - $request_body = array(
6420 - 'model' => $selected_model,
6421 - 'input' => $user_query,
6422 - 'tools' => array(
6423 - array(
6424 - 'type' => 'file_search',
6425 - 'vector_store_ids' => $vectorstore_ids,
6426 - 'max_num_results' => intval($max_results)
6427 - )
6428 - ),
6429 - 'include' => array('output[*].file_search_call.search_results')
6430 - );
6431 -
6432 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6433 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6434 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6435 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6436 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6437 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6438 -
6439 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6440 - 'headers' => array(
6441 - 'Authorization' => 'Bearer ' . $api_key,
6442 - 'Content-Type' => 'application/json'
6443 - ),
6444 - 'body' => wp_json_encode($request_body),
6445 - 'timeout' => 60
6446 - ));
6447 -
6448 - if (is_wp_error($response)) {
6449 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6450 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6451 - $this->current_valid_urls = [];
6452 - return '';
6453 - }
6454 -
6455 - $response_code = wp_remote_retrieve_response_code($response);
6456 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6457 -
6458 - $response_body = wp_remote_retrieve_body($response);
6459 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6460 -
6461 - if ($response_code !== 200) {
6462 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6463 - $api_error_detail = '';
6464 - $decoded_error = json_decode($response_body, true);
6465 - if (isset($decoded_error['error']['message'])) {
6466 - $api_error_detail = $decoded_error['error']['message'];
6467 - }
6468 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6469 - $this->current_valid_urls = [];
6470 - return '';
6471 - }
6472 - $result = json_decode($response_body, true);
6473 -
6474 - if (json_last_error() !== JSON_ERROR_NONE) {
6475 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6476 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6477 - $this->current_valid_urls = [];
6478 - return '';
6479 - }
6480 -
6481 - // Debug: Log the structure of the result
6482 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6483 - if (isset($result['output'])) {
6484 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6485 - foreach ($result['output'] as $idx => $out) {
6486 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6487 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6488 - }
6489 - } else {
6490 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6491 - }
6492 -
6493 - // Extract file search results from the response
6494 - $content = '';
6495 - $matches_used = 0;
6496 - $all_matches = [];
6497 -
6498 - // The Responses API returns output array with tool results
6499 - if (isset($result['output']) && is_array($result['output'])) {
6500 - foreach ($result['output'] as $output_item) {
6501 - // Look for file_search_call results
6502 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6503 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6504 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6505 -
6506 - // Check for search_results in the output item directly
6507 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6508 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6509 -
6510 - if (empty($search_results)) {
6511 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6512 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6513 - }
6514 -
6515 - foreach ($search_results as $index => $search_result) {
6516 - $filename = $search_result['filename'] ?? '';
6517 - $score = $search_result['score'] ?? 0;
6518 - $text_content = '';
6519 -
6520 - // Extract text content from the result
6521 - // The text can be directly on the result OR nested under content array
6522 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6523 - // Direct text field (OpenAI's actual format)
6524 - $text_content = $search_result['text'];
6525 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6526 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6527 - // Nested content array format
6528 - foreach ($search_result['content'] as $content_item) {
6529 - if (isset($content_item['text'])) {
6530 - $text_content .= $content_item['text'] . "\n";
6531 - }
6532 - }
6533 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6534 - } else {
6535 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6536 - }
6537 -
6538 - if (!empty($text_content)) {
6539 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6540 - $content .= trim($text_content) . "\n\n";
6541 -
6542 - if (!empty($filename)) {
6543 - $content .= "Source: " . $filename . "\n\n";
6544 - }
6545 -
6546 - // Extract URLs from content
6547 - preg_match_all(
6548 - '#\bhttps?://[^\s<>"\']+#i',
6549 - $text_content,
6550 - $content_urls
6551 - );
6552 - if (!empty($content_urls[0])) {
6553 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6554 - }
6555 -
6556 - $matches_used++;
6557 - }
6558 -
6559 - // Store for similarity analysis
6560 - $all_matches[] = [
6561 - 'document_id' => $filename ?: ('result_' . $index),
6562 - 'similarity' => $score,
6563 - 'similarity_percentage' => round($score * 100, 2),
6564 - 'above_threshold' => true,
6565 - 'source_display' => $filename,
6566 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6567 - 'used_for_context' => true,
6568 - 'role_restriction' => 'public',
6569 - 'has_access' => true,
6570 - 'filtered_out' => false
6571 - ];
6572 - }
6573 - }
6574 -
6575 - // Also check for message content with annotations (citations)
6576 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6577 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6578 - foreach ($output_item['content'] as $content_block) {
6579 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6580 - foreach ($content_block['annotations'] as $annotation) {
6581 - if (isset($annotation['filename'])) {
6582 - $filename = $annotation['filename'];
6583 - $score = $annotation['score'] ?? 0;
6584 - $text_content = '';
6585 -
6586 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6587 - foreach ($annotation['content'] as $ann_content) {
6588 - if (isset($ann_content['text'])) {
6589 - $text_content .= $ann_content['text'] . "\n";
6590 - }
6591 - }
6592 - }
6593 -
6594 - if (!empty($text_content) && $matches_used < $max_results) {
6595 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6596 - $content .= trim($text_content) . "\n\n";
6597 - $content .= "Source: " . $filename . "\n\n";
6598 -
6599 - preg_match_all(
6600 - '#\bhttps?://[^\s<>"\']+#i',
6601 - $text_content,
6602 - $content_urls
6603 - );
6604 - if (!empty($content_urls[0])) {
6605 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6606 - }
6607 -
6608 - $matches_used++;
6609 -
6610 - $all_matches[] = [
6611 - 'document_id' => $filename,
6612 - 'similarity' => $score,
6613 - 'similarity_percentage' => round($score * 100, 2),
6614 - 'above_threshold' => true,
6615 - 'source_display' => $filename,
6616 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6617 - 'used_for_context' => true,
6618 - 'role_restriction' => 'public',
6619 - 'has_access' => true,
6620 - 'filtered_out' => false
6621 - ];
6622 - }
6623 - }
6624 - }
6625 - }
6626 - }
6627 - }
6628 - }
6629 - }
6630 - }
6631 -
6632 - // Store for testing panel
6633 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6634 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6635 -
6636 - // Store unique valid URLs for validation
6637 - $this->current_valid_urls = array_unique($valid_urls);
6638 -
6639 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6640 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6641 -
6642 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6643 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6644 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6645 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6646 - if ($matches_used > 0) {
6647 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6648 - }
6649 -
6650 - // Check if citation links are enabled
6651 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6652 -
6653 - // Add response guidelines
6654 - if ($matches_used === 0) {
6655 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6656 - $content = "No reference information was found for this query.\n\n";
6657 - } else {
6658 - // Build response guidelines based on citation links setting
6659 - $content .= "\n## Response Guidelines ##\n" .
6660 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6661 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6662 - "If you don't have specific information or are uncertain about any details, it's always " .
6663 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6664 - "When information is incomplete, let them know you are unsure.\n\n";
6665 -
6666 - // Only add hyperlink instructions if citation links are enabled
6667 - if ($citation_links_enabled) {
6668 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6669 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6670 - } else {
6671 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6672 - "Simply provide helpful answers based on the reference information without citing sources.";
6673 - }
6674 - }
6675 -
6676 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6677 -
6678 - return trim($content);
6679 -}
6680 -
6681 -/**
6682 - * Check if the given model is an OpenAI chat model
6683 - *
6684 - * @param string $model The model ID
6685 - * @return bool True if it's an OpenAI model
6686 - */
6687 -private function is_openai_chat_model($model) {
6688 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6689 - foreach ($openai_prefixes as $prefix) {
6690 - if (strpos($model, $prefix) === 0) {
6691 - return true;
6692 - }
6693 - }
6694 - return false;
6695 -}
6696 -
6697 -/**
6698 - * Get bot-specific Vector Store configuration
6699 - *
6700 - * @param string $bot_id The bot ID
6701 - * @return array Configuration array
6702 - */
6703 -private function get_bot_vectorstore_config($bot_id = 'default') {
6704 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6705 -
6706 - // Default global settings
6707 - $default_config = array(
6708 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6709 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6710 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6711 - );
6712 -
6713 - // Allow multi-bot plugin to override with bot-specific settings
6714 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6715 -
6716 - // Preserve max_results from global settings if not set in bot config
6717 - if (!isset($bot_config['max_results'])) {
6718 - $bot_config['max_results'] = $default_config['max_results'];
6719 - }
6720 -
6721 - return $bot_config;
6722 -}
6723 -
6724 -private function mxchat_find_relevant_products($user_embedding) {
6725 - //error_log('MXChat Vector Search: Starting product search...');
6726 -
6727 - // Retrieve the add-on settings from the database
6728 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
6729 -
6730 - // Determine whether Pinecone is enabled
6731 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
6732 -
6733 - //error_log('Pinecone enabled flag: ' . $use_pinecone);
6734 -
6735 - if ($use_pinecone === 1) {
6736 - //error_log('MXChat Vector Search: Using Pinecone database for products');
6737 - return $this->find_relevant_products_pinecone($user_embedding);
6738 - } else {
6739 - //error_log('MXChat Vector Search: Using WordPress database for products');
6740 - return $this->find_relevant_products_wordpress($user_embedding);
6741 - }
6742 -}
6743 -private function find_relevant_products_wordpress($user_embedding) {
6744 - global $wpdb;
6745 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6746 -
6747 - if (!is_array($user_embedding)) {
6748 - return '';
6749 - }
6750 -
6751 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6752 - // results above the similarity threshold. Peak memory is bounded by
6753 - // $batch_size embedding rows plus a 3-element top list.
6754 - $batch_size = 250;
6755 - $similarity_threshold = 0.85;
6756 - $top_k = 3;
6757 - $top_results = [];
6758 - $offset = 0;
6759 -
6760 - do {
6761 - $batch = $wpdb->get_results($wpdb->prepare(
6762 - "SELECT id, embedding_vector
6763 - FROM {$system_prompt_table}
6764 - LIMIT %d OFFSET %d",
6765 - $batch_size,
6766 - $offset
6767 - ));
6768 -
6769 - if (empty($batch)) {
6770 - break;
6771 - }
6772 -
6773 - foreach ($batch as $row) {
6774 - $database_embedding = $row->embedding_vector
6775 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6776 - : null;
6777 -
6778 - if (!is_array($database_embedding)) {
6779 - unset($database_embedding);
6780 - continue;
6781 - }
6782 -
6783 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6784 - unset($database_embedding);
6785 -
6786 - if ($similarity < $similarity_threshold) {
6787 - continue;
6788 - }
6789 -
6790 - // Insert into bounded top-K (kept sorted descending)
6791 - if (count($top_results) < $top_k) {
6792 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6793 - usort($top_results, function ($a, $b) {
6794 - return $b['similarity'] <=> $a['similarity'];
6795 - });
6796 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6797 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6798 - usort($top_results, function ($a, $b) {
6799 - return $b['similarity'] <=> $a['similarity'];
6800 - });
6801 - }
6802 - }
6803 -
6804 - unset($batch);
6805 - $offset += $batch_size;
6806 - } while (true);
6807 -
6808 - if (empty($top_results)) {
6809 - return '';
6810 - }
6811 -
6812 - $content = '';
6813 - foreach ($top_results as $result) {
6814 - $chunk_content = $this->fetch_content_with_product_links($result['id']);
6815 - $content .= $chunk_content . "\n\n";
6816 - }
6817 -
6818 - return trim($content);
6819 -}
6820 -
6821 -
6822 -private function find_relevant_products_pinecone($user_embedding) {
6823 - //error_log('Starting Pinecone product search...');
6824 -
6825 - $options = get_option('mxchat_pinecone_addon_options', array());
6826 - $api_key = $options['mxchat_pinecone_api_key'] ?? '';
6827 - $host = $options['mxchat_pinecone_host'] ?? '';
6828 -
6829 - if (empty($host) || empty($api_key)) {
6830 - //error_log('Pinecone credentials not properly configured for product search');
6831 - return '';
6832 - }
6833 -
6834 - $similarity_threshold = 0.85;
6835 - $api_endpoint = "https://{$host}/query";
6836 -
6837 - $request_body = array(
6838 - 'vector' => $user_embedding,
6839 - 'topK' => 5,
6840 - 'includeMetadata' => true,
6841 - 'includeValues' => true,
6842 - 'filter' => array(
6843 - 'type' => 'product'
6844 - )
6845 - );
6846 -
6847 - //error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
6848 -
6849 - $response = wp_remote_post($api_endpoint, array(
6850 - 'headers' => array(
6851 - 'Api-Key' => $api_key,
6852 - 'accept' => 'application/json',
6853 - 'content-type' => 'application/json'
6854 - ),
6855 - 'body' => wp_json_encode($request_body),
6856 - 'timeout' => 30
6857 - ));
6858 -
6859 - if (is_wp_error($response)) {
6860 - //error_log('Pinecone product query error: ' . $response->get_error_message());
6861 - return '';
6862 - }
6863 -
6864 - $response_code = wp_remote_retrieve_response_code($response);
6865 - //error_log('Pinecone response code: ' . $response_code);
6866 -
6867 - if ($response_code !== 200) {
6868 - //error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
6869 - return '';
6870 - }
6871 -
6872 - $results = json_decode(wp_remote_retrieve_body($response), true);
6873 - //error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
6874 -
6875 - if (empty($results['matches'])) {
6876 - //error_log('No matches found in Pinecone response');
6877 - return '';
6878 - }
6879 -
6880 - $content = '';
6881 - foreach ($results['matches'] as $match) {
6882 - if ($match['score'] < $similarity_threshold) {
6883 - //error_log("Match below threshold: " . $match['score']);
6884 - continue;
6885 - }
6886 -
6887 - if (!empty($match['metadata']['text'])) {
6888 - $content .= $match['metadata']['text'];
6889 - if (!empty($match['metadata']['source_url'])) {
6890 - $content .= "\n\nFor more details, check out this product: " . esc_url($match['metadata']['source_url']);
6891 - }
6892 - $content .= "\n\n";
6893 - }
6894 - }
6895 -
6896 - return trim($content);
6897 -}
6898 -
6899 -
6900 344 private function fetch_content_with_product_links($most_relevant_id) {
6901 345 global $wpdb;
6902 346 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6903 347
@@ -6916,2808 +360,41 @@
6916 360
6917 361 return null;
6918 362 }
6919 363
6920 -/**
6921 - * Get system instructions for a specific bot or default
6922 - * Checks for multi-bot add-on and uses bot-specific instructions if available
6923 - * Automatically strips URLs if citation links are disabled
6924 - * Replaces {visitor_name} placeholder with actual visitor name if available
6925 - *
6926 - * @param string $bot_id The bot ID to get instructions for
6927 - * @param string $session_id Optional session ID to lookup visitor name
6928 - */
6929 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6930 - $instructions = '';
6931 364
6932 - // Check if multi-bot add-on is active
6933 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6934 - // Get bot-specific options from multi-bot add-on
6935 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6936 -
6937 - // If bot has custom system instructions, use those
6938 - if (!empty($bot_options['system_prompt_instructions'])) {
6939 - $instructions = $bot_options['system_prompt_instructions'];
6940 - }
365 + private function mxchat_generate_response($relevant_content, $api_key, $conversation_history) {
366 + if (!$relevant_content) {
367 + return "I'm sorry, I couldn't find relevant information on that topic.";
6941 368 }
6942 369
6943 - // Fall back to default system instructions
6944 - if (empty($instructions)) {
6945 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6946 - }
370 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6947 371
6948 - // Check if citation links are disabled - if so, strip URLs from instructions
6949 - $fresh_options = get_option('mxchat_options', []);
6950 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6951 -
6952 - if (!$citation_links_enabled && !empty($instructions)) {
6953 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
6954 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
6955 - }
6956 -
6957 - // Replace {visitor_name} placeholder with actual visitor name if available
6958 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
6959 - $name_option_key = "mxchat_name_{$session_id}";
6960 - $visitor_name = get_option($name_option_key, '');
6961 -
6962 - if (!empty($visitor_name)) {
6963 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
6964 - } else {
6965 - // Remove placeholder if no name is available
6966 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
6967 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
6968 - }
6969 - }
6970 -
6971 - // Allow developers to filter system instructions and process shortcodes
6972 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
6973 - $instructions = do_shortcode($instructions);
6974 -
6975 - return $instructions;
6976 -}
6977 -/**
6978 - * Get the current bot ID from session or request context
6979 - */
6980 -private function get_current_bot_id($session_id = '') {
6981 - // First, check if bot_id is passed in the current request
6982 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
6983 - return sanitize_key($_POST['bot_id']);
6984 - }
6985 -
6986 - // If not in POST, try to get it from session data
6987 - if (!empty($session_id)) {
6988 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
6989 - if (!empty($bot_id)) {
6990 - return $bot_id;
6991 - }
6992 - }
6993 -
6994 - // Fall back to default
6995 - return 'default';
6996 -}
6997 -private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $openrouter_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-5.1-chat-latest') {
6998 - try {
6999 - if (!$relevant_content) {
7000 - $error_response = [
7001 - 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7002 - 'error_code' => 'no_relevant_content'
7003 - ];
7004 -
7005 - if ($testing_data !== null) {
7006 - $error_response['testing_data'] = $testing_data;
7007 - }
7008 -
7009 - return $error_response;
7010 - }
7011 -
7012 - if (!is_array($conversation_history)) {
7013 - $conversation_history = array();
7014 - }
7015 -
7016 - // Check if this is an OpenRouter model
7017 - if ($selected_model === 'openrouter') {
7018 - // Get the actual OpenRouter model from options
7019 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7020 -
7021 - if (empty($openrouter_selected_model)) {
7022 - $error_response = [
7023 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7024 - 'error_code' => 'no_openrouter_model_selected'
7025 - ];
7026 - if ($testing_data !== null) {
7027 - $error_response['testing_data'] = $testing_data;
7028 - }
7029 - return $error_response;
7030 - }
7031 -
7032 - if (empty($openrouter_api_key)) {
7033 - $error_response = [
7034 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7035 - 'error_code' => 'missing_openrouter_api_key'
7036 - ];
7037 - if ($testing_data !== null) {
7038 - $error_response['testing_data'] = $testing_data;
7039 - }
7040 - return $error_response;
7041 - }
7042 -
7043 - if ($streaming) {
7044 - return $this->mxchat_generate_response_openrouter_stream(
7045 - $openrouter_selected_model,
7046 - $openrouter_api_key,
7047 - $conversation_history,
7048 - $relevant_content,
7049 - $session_id,
7050 - $testing_data
7051 - );
7052 - } else {
7053 - $response = $this->mxchat_generate_response_openrouter(
7054 - $openrouter_selected_model,
7055 - $openrouter_api_key,
7056 - $conversation_history,
7057 - $relevant_content
7058 - );
7059 - }
7060 -
7061 - if (is_array($response) && isset($response['error'])) {
7062 - if ($testing_data !== null) {
7063 - $response['testing_data'] = $testing_data;
7064 - }
7065 - return $response;
7066 - }
7067 -
7068 - return $response;
7069 - }
7070 -
7071 - // Extract model prefix to determine the provider
7072 - $model_parts = explode('-', $selected_model);
7073 - $provider = strtolower($model_parts[0]);
7074 -
7075 - // Handle model selection based on provider prefix
7076 - switch ($provider) {
7077 - case 'gemini':
7078 - if (empty($gemini_api_key)) {
7079 - $error_response = [
7080 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
7081 - 'error_code' => 'missing_gemini_api_key'
7082 - ];
7083 - if ($testing_data !== null) {
7084 - $error_response['testing_data'] = $testing_data;
7085 - }
7086 - return $error_response;
7087 - }
7088 - $response = $this->mxchat_generate_response_gemini(
7089 - $selected_model,
7090 - $gemini_api_key,
7091 - $conversation_history,
7092 - $relevant_content
7093 - );
7094 - break;
7095 -
7096 - case 'claude':
7097 - if (empty($claude_api_key)) {
7098 - $error_response = [
7099 - 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
7100 - 'error_code' => 'missing_claude_api_key'
7101 - ];
7102 - if ($testing_data !== null) {
7103 - $error_response['testing_data'] = $testing_data;
7104 - }
7105 - return $error_response;
7106 - }
7107 - if ($streaming) {
7108 - return $this->mxchat_generate_response_claude_stream(
7109 - $selected_model,
7110 - $claude_api_key,
7111 - $conversation_history,
7112 - $relevant_content,
7113 - $session_id,
7114 - $testing_data
7115 - );
7116 - } else {
7117 - $response = $this->mxchat_generate_response_claude(
7118 - $selected_model,
7119 - $claude_api_key,
7120 - $conversation_history,
7121 - $relevant_content
7122 - );
7123 - }
7124 - break;
7125 -
7126 - case 'grok':
7127 - if (empty($xai_api_key)) {
7128 - $error_response = [
7129 - 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
7130 - 'error_code' => 'missing_xai_api_key'
7131 - ];
7132 - if ($testing_data !== null) {
7133 - $error_response['testing_data'] = $testing_data;
7134 - }
7135 - return $error_response;
7136 - }
7137 - if ($streaming) {
7138 - return $this->mxchat_generate_response_xai_stream(
7139 - $selected_model,
7140 - $xai_api_key,
7141 - $conversation_history,
7142 - $relevant_content,
7143 - $session_id,
7144 - $testing_data
7145 - );
7146 - } else {
7147 - $response = $this->mxchat_generate_response_xai(
7148 - $selected_model,
7149 - $xai_api_key,
7150 - $conversation_history,
7151 - $relevant_content
7152 - );
7153 - }
7154 - break;
7155 -
7156 - case 'deepseek':
7157 - if (empty($deepseek_api_key)) {
7158 - $error_response = [
7159 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
7160 - 'error_code' => 'missing_deepseek_api_key'
7161 - ];
7162 - if ($testing_data !== null) {
7163 - $error_response['testing_data'] = $testing_data;
7164 - }
7165 - return $error_response;
7166 - }
7167 - if ($streaming) {
7168 - return $this->mxchat_generate_response_deepseek_stream(
7169 - $selected_model,
7170 - $deepseek_api_key,
7171 - $conversation_history,
7172 - $relevant_content,
7173 - $session_id,
7174 - $testing_data
7175 - );
7176 - } else {
7177 - $response = $this->mxchat_generate_response_deepseek(
7178 - $selected_model,
7179 - $deepseek_api_key,
7180 - $conversation_history,
7181 - $relevant_content
7182 - );
7183 - }
7184 - break;
7185 -
7186 - case 'custom':
7187 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7188 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7189 - if (empty($cp_base_url)) {
7190 - $error_response = [
7191 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7192 - 'error_code' => 'missing_custom_provider_base_url'
7193 - ];
7194 - if ($testing_data !== null) {
7195 - $error_response['testing_data'] = $testing_data;
7196 - }
7197 - return $error_response;
7198 - }
7199 - if ($streaming) {
7200 - return $this->mxchat_generate_response_custom_stream(
7201 - $selected_model,
7202 - $conversation_history,
7203 - $relevant_content,
7204 - $session_id,
7205 - $testing_data
7206 - );
7207 - } else {
7208 - $response = $this->mxchat_generate_response_custom(
7209 - $selected_model,
7210 - $conversation_history,
7211 - $relevant_content
7212 - );
7213 - }
7214 - break;
7215 -
7216 - case 'gpt':
7217 - case 'o1':
7218 - if (empty($api_key)) {
7219 - $error_response = [
7220 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7221 - 'error_code' => 'missing_openai_api_key'
7222 - ];
7223 - if ($testing_data !== null) {
7224 - $error_response['testing_data'] = $testing_data;
7225 - }
7226 - return $error_response;
7227 - }
7228 -
7229 - // Check if web search is enabled for this OpenAI model
7230 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7231 - // Models that don't support web search
7232 - $unsupported_web_search_models = array('gpt-4.1-nano');
7233 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7234 -
7235 - if ($web_search_enabled && $model_supports_web_search) {
7236 - // Use Responses API (required for some models, or when web search is enabled)
7237 - return $this->mxchat_generate_response_openai_web_search(
7238 - $selected_model,
7239 - $api_key,
7240 - $conversation_history,
7241 - $relevant_content,
7242 - $session_id,
7243 - $testing_data,
7244 - $streaming
7245 - );
7246 - } elseif ($streaming) {
7247 - return $this->mxchat_generate_response_openai_stream(
7248 - $selected_model,
7249 - $api_key,
7250 - $conversation_history,
7251 - $relevant_content,
7252 - $session_id,
7253 - $testing_data
7254 - );
7255 - } else {
7256 - $response = $this->mxchat_generate_response_openai(
7257 - $selected_model,
7258 - $api_key,
7259 - $conversation_history,
7260 - $relevant_content
7261 - );
7262 - }
7263 - break;
7264 -
7265 - default:
7266 - if (empty($api_key)) {
7267 - $error_response = [
7268 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7269 - 'error_code' => 'missing_openai_api_key'
7270 - ];
7271 - if ($testing_data !== null) {
7272 - $error_response['testing_data'] = $testing_data;
7273 - }
7274 - return $error_response;
7275 - }
7276 -
7277 - // Check if web search is enabled (default case also handles OpenAI models)
7278 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7279 - $unsupported_web_search_models = array('gpt-4.1-nano');
7280 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7281 -
7282 - if ($web_search_enabled && $model_supports_web_search) {
7283 - return $this->mxchat_generate_response_openai_web_search(
7284 - $selected_model,
7285 - $api_key,
7286 - $conversation_history,
7287 - $relevant_content,
7288 - $session_id,
7289 - $testing_data,
7290 - $streaming
7291 - );
7292 - } elseif ($streaming) {
7293 - return $this->mxchat_generate_response_openai_stream(
7294 - $selected_model,
7295 - $api_key,
7296 - $conversation_history,
7297 - $relevant_content,
7298 - $session_id,
7299 - $testing_data
7300 - );
7301 - } else {
7302 - $response = $this->mxchat_generate_response_openai(
7303 - $selected_model,
7304 - $api_key,
7305 - $conversation_history,
7306 - $relevant_content
7307 - );
7308 - }
7309 - break;
7310 - }
7311 -
7312 - if (is_array($response) && isset($response['error'])) {
7313 - if ($testing_data !== null) {
7314 - $response['testing_data'] = $testing_data;
7315 - }
7316 - return $response;
7317 - }
7318 -
7319 - return $response;
7320 -
7321 - } catch (Exception $e) {
7322 - $error_response = [
7323 - 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7324 - 'error_code' => 'system_exception',
7325 - 'exception_details' => $e->getMessage()
7326 - ];
7327 -
7328 - if ($testing_data !== null) {
7329 - $error_response['testing_data'] = $testing_data;
7330 - }
7331 -
7332 - return $error_response;
7333 - }
7334 -}
7335 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7336 - try {
7337 - $bot_id = $this->get_current_bot_id($session_id);
7338 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7339 -
7340 - if (!is_array($conversation_history)) {
7341 - $conversation_history = array();
7342 - }
7343 -
7344 - $formatted_conversation = array();
7345 -
7346 - $formatted_conversation[] = array(
7347 - 'role' => 'system',
7348 - 'content' => $system_prompt_instructions . " " . $relevant_content
7349 - );
7350 -
7351 - foreach ($conversation_history as $message) {
7352 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7353 - $role = $message['role'];
7354 - if ($role === 'bot' || $role === 'agent') {
7355 - $role = 'assistant';
7356 - }
7357 - if (!in_array($role, ['system', 'assistant', 'user'])) {
7358 - $role = 'user';
7359 - }
7360 - $formatted_conversation[] = array(
7361 - 'role' => $role,
7362 - 'content' => $message['content']
7363 - );
7364 - }
7365 - }
7366 -
7367 - if (headers_sent() || !function_exists('curl_init')) {
7368 - $regular_response = $this->mxchat_generate_response_openrouter(
7369 - $selected_model,
7370 - $openrouter_api_key,
7371 - $conversation_history,
7372 - $relevant_content
7373 - );
7374 -
7375 - // Save bot response to transcript
7376 - if (!empty($regular_response) && !empty($session_id)) {
7377 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7378 - }
7379 -
7380 - $response_data = [
7381 - 'text' => $regular_response,
7382 - 'html' => '',
7383 - 'session_id' => $session_id
7384 - ];
7385 -
7386 - if ($testing_data !== null) {
7387 - $response_data['testing_data'] = $testing_data;
7388 - }
7389 -
7390 - header('Content-Type: application/json');
7391 - echo json_encode($response_data);
7392 - return true;
7393 - }
7394 -
7395 - $body = json_encode([
7396 - 'model' => $selected_model,
7397 - 'messages' => $formatted_conversation,
7398 - 'temperature' => 1,
7399 - 'stream' => true
7400 - ]);
7401 -
7402 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
7403 - // inside WRITEFUNCTION on first byte of a successful upstream.
7404 -
7405 - $captured_status_code = 0;
7406 - $captured_body_pre_stream = '';
7407 - $full_response = '';
7408 - $stream_started = false;
7409 - $buffer = '';
7410 - $errno = 0;
7411 - $last_curl_error = '';
7412 - $http_code = 0;
7413 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7414 - $backoff_ms = array(0, 750, 2000);
7415 -
7416 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7417 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7418 - usleep($backoff_ms[$attempt] * 1000);
7419 - }
7420 -
7421 - $captured_status_code = 0;
7422 - $captured_body_pre_stream = '';
7423 - $full_response = '';
7424 - $stream_started = false;
7425 - $buffer = '';
7426 -
7427 - $ch = curl_init();
7428 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
7429 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7430 - curl_setopt($ch, CURLOPT_POST, true);
7431 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7432 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7433 - 'Content-Type: application/json',
7434 - 'Authorization: Bearer ' . $openrouter_api_key,
7435 - 'HTTP-Referer: ' . home_url(),
7436 - 'X-Title: ' . get_bloginfo('name')
7437 - ));
7438 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7439 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7440 -
7441 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7442 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7443 - $captured_status_code = (int) $m[1];
7444 - }
7445 - return strlen($header);
7446 - });
7447 -
7448 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7449 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7450 - $captured_body_pre_stream .= $data;
7451 - return strlen($data);
7452 - }
7453 -
7454 - if (!$this->streaming_headers_sent) {
7455 - $this->setup_streaming_headers();
7456 - }
7457 -
7458 - if (!$stream_started && $testing_data !== null) {
7459 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7460 - flush();
7461 - $stream_started = true;
7462 - }
7463 -
7464 - $buffer .= $data;
7465 - $lines = explode("\n", $buffer);
7466 - $buffer = array_pop($lines);
7467 -
7468 - foreach ($lines as $line) {
7469 - if (trim($line) === '') {
7470 - continue;
7471 - }
7472 - if (strpos($line, 'data: ') !== 0) {
7473 - continue;
7474 - }
7475 -
7476 - $json_str = substr($line, 6);
7477 -
7478 - if (trim($json_str) === '[DONE]') {
7479 - echo "data: [DONE]\n\n";
7480 - flush();
7481 - continue;
7482 - }
7483 -
7484 - $json = json_decode(trim($json_str), true);
7485 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7486 - $content = $json['choices'][0]['delta']['content'];
7487 - $full_response .= $content;
7488 -
7489 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7490 - flush();
7491 - }
7492 - }
7493 -
7494 - return strlen($data);
7495 - });
7496 -
7497 - $response = curl_exec($ch);
7498 - $errno = curl_errno($ch);
7499 - $last_curl_error = curl_error($ch);
7500 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7501 - curl_close($ch);
7502 -
7503 - if (!$errno && $http_code === 200) {
7504 - break;
7505 - }
7506 -
7507 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7508 - $can_retry = !$this->streaming_headers_sent
7509 - && ($attempt + 1) < $max_attempts
7510 - && $is_transient;
7511 -
7512 - if (defined('WP_DEBUG') && WP_DEBUG) {
7513 - error_log(sprintf(
7514 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7515 - $attempt + 1, $max_attempts, $http_code, $errno,
7516 - $is_transient ? 'yes' : 'no',
7517 - $can_retry ? 'Retrying.' : 'Giving up.'
7518 - ));
7519 - }
7520 -
7521 - if (!$can_retry) {
7522 - break;
7523 - }
7524 - }
7525 -
7526 - if (!$errno && $http_code === 200) {
7527 - if (!empty($full_response) && !empty($session_id)) {
7528 - $rag_context_for_storage = null;
7529 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7530 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7531 -
7532 - if ($has_rag_data || $has_action_data) {
7533 - $rag_context_for_storage = [];
7534 -
7535 - if ($has_rag_data) {
7536 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7537 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7538 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7539 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7540 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7541 - }
7542 -
7543 - if ($has_action_data) {
7544 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7545 - }
7546 - }
7547 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7548 - }
7549 - return true;
7550 - }
7551 -
7552 - return $this->mxchat_stream_emit_fallback(
7553 - 'openai',
7554 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7555 - $session_id,
7556 - $testing_data
7557 - );
7558 -
7559 - } catch (Exception $e) {
7560 - return $this->mxchat_stream_emit_fallback(
7561 - 'openai',
7562 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7563 - $session_id,
7564 - $testing_data
7565 - );
7566 - }
7567 -}
7568 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7569 - try {
7570 - $bot_id = $this->get_current_bot_id($session_id);
7571 -
7572 - // Get system prompt instructions using centralized function
7573 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7574 -
7575 - // Ensure conversation_history is an array
7576 - if (!is_array($conversation_history)) {
7577 - $conversation_history = array();
7578 - }
7579 -
7580 - // Format conversation history for OpenAI
7581 - $formatted_conversation = array();
7582 -
7583 - $formatted_conversation[] = array(
7584 - 'role' => 'system',
7585 - 'content' => $system_prompt_instructions . " " . $relevant_content
7586 - );
7587 -
7588 - foreach ($conversation_history as $message) {
7589 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7590 - $role = $message['role'];
7591 - if ($role === 'bot' || $role === 'agent') {
7592 - $role = 'assistant';
7593 - }
7594 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7595 - $role = 'user';
7596 - }
7597 - $formatted_conversation[] = array(
7598 - 'role' => $role,
7599 - 'content' => $message['content']
7600 - );
7601 - }
7602 - }
7603 -
7604 - // Check if we can actually stream
7605 - if (headers_sent() || !function_exists('curl_init')) {
7606 - // Fallback to regular response with testing data
7607 - $regular_response = $this->mxchat_generate_response_openai(
7608 - $selected_model,
7609 - $api_key,
7610 - $conversation_history,
7611 - $relevant_content
7612 - );
7613 -
7614 - // Save bot response to transcript
7615 - if (!empty($regular_response) && !empty($session_id)) {
7616 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7617 - }
7618 -
7619 - $response_data = [
7620 - 'text' => $regular_response,
7621 - 'html' => '',
7622 - 'session_id' => $session_id
7623 - ];
7624 -
7625 - if ($testing_data !== null) {
7626 - $response_data['testing_data'] = $testing_data;
7627 - }
7628 -
7629 - header('Content-Type: application/json');
7630 - echo json_encode($response_data);
7631 - return true;
7632 - }
7633 -
7634 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
7635 - $is_gpt5_model = (
7636 - strpos($selected_model, 'gpt-5') === 0 ||
7637 - $selected_model === 'gpt-5.2' ||
7638 - $selected_model === 'gpt-5.1-2025-11-13' ||
7639 - $selected_model === 'gpt-5' ||
7640 - $selected_model === 'gpt-5-mini' ||
7641 - $selected_model === 'gpt-5-nano'
7642 - );
7643 -
7644 - // Build request body with optimal settings for fast streaming
7645 - $request_body = [
7646 - 'model' => $selected_model,
7647 - 'messages' => $formatted_conversation,
7648 - 'temperature' => 1,
7649 - 'stream' => true
7650 - ];
7651 -
7652 - // Add reasoning_effort only for GPT-5 models that support it
7653 - // These chat models don't support reasoning_effort parameter
7654 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
7655 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
7656 - // GPT-5.1 uses 'low' instead of 'minimal'
7657 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7658 - $request_body['reasoning_effort'] = 'low';
7659 - } elseif ($selected_model === 'gpt-5.5') {
7660 - $request_body['reasoning_effort'] = 'none';
7661 - } elseif ($selected_model === 'gpt-5.4') {
7662 - $request_body['reasoning_effort'] = 'none';
7663 - } else {
7664 - $request_body['reasoning_effort'] = 'minimal';
7665 - }
7666 - }
7667 -
7668 - $body = json_encode($request_body);
7669 -
7670 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
7671 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
7672 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
7673 -
7674 - $captured_status_code = 0;
7675 - $captured_body_pre_stream = '';
7676 - $full_response = '';
7677 - $stream_started = false;
7678 - $buffer = '';
7679 - $errno = 0;
7680 - $last_curl_error = '';
7681 - $http_code = 0;
7682 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7683 - $backoff_ms = array(0, 750, 2000);
7684 -
7685 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7686 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7687 - usleep($backoff_ms[$attempt] * 1000);
7688 - }
7689 -
7690 - // Reset per-attempt capture state.
7691 - $captured_status_code = 0;
7692 - $captured_body_pre_stream = '';
7693 - $full_response = '';
7694 - $stream_started = false;
7695 - $buffer = '';
7696 -
7697 - $ch = curl_init();
7698 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
7699 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7700 - curl_setopt($ch, CURLOPT_POST, true);
7701 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7702 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7703 - 'Content-Type: application/json',
7704 - 'Authorization: Bearer ' . $api_key
7705 - ));
7706 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7707 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7708 -
7709 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
7710 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7711 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7712 - $captured_status_code = (int) $m[1];
7713 - }
7714 - return strlen($header);
7715 - });
7716 -
7717 - // Buffer control for real-time streaming
7718 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7719 - // V2 guard: if upstream returned non-200, buffer body for transient
7720 - // classification and DO NOT emit to client. Stream channel must NOT open.
7721 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7722 - $captured_body_pre_stream .= $data;
7723 - return strlen($data);
7724 - }
7725 -
7726 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
7727 - // After this point streaming_headers_sent === true → retry is structurally blocked.
7728 - if (!$this->streaming_headers_sent) {
7729 - $this->setup_streaming_headers();
7730 - }
7731 -
7732 - // Send testing data as the first event if available
7733 - if (!$stream_started && $testing_data !== null) {
7734 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7735 - flush();
7736 - $stream_started = true;
7737 - }
7738 -
7739 - // CRITICAL FIX: Append new data to buffer
7740 - $buffer .= $data;
7741 -
7742 - // Process complete lines only
7743 - $lines = explode("\n", $buffer);
7744 -
7745 - // CRITICAL FIX: Keep the last incomplete line in the buffer
7746 - $buffer = array_pop($lines);
7747 -
7748 - foreach ($lines as $line) {
7749 - if (trim($line) === '') {
7750 - continue;
7751 - }
7752 - if (strpos($line, 'data: ') !== 0) {
7753 - continue;
7754 - }
7755 -
7756 - $json_str = substr($line, 6);
7757 -
7758 - if (trim($json_str) === '[DONE]') {
7759 - echo "data: [DONE]\n\n";
7760 - flush();
7761 - continue;
7762 - }
7763 -
7764 - $json = json_decode(trim($json_str), true);
7765 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7766 - $content = $json['choices'][0]['delta']['content'];
7767 - $full_response .= $content;
7768 -
7769 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7770 - flush();
7771 - }
7772 - }
7773 -
7774 - return strlen($data);
7775 - });
7776 -
7777 - $response = curl_exec($ch);
7778 - $errno = curl_errno($ch);
7779 - $last_curl_error = curl_error($ch);
7780 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7781 - curl_close($ch);
7782 -
7783 - if (!$errno && $http_code === 200) {
7784 - break; // Happy path — WRITEFUNCTION already streamed everything.
7785 - }
7786 -
7787 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7788 - $can_retry = !$this->streaming_headers_sent
7789 - && ($attempt + 1) < $max_attempts
7790 - && $is_transient;
7791 -
7792 - if (defined('WP_DEBUG') && WP_DEBUG) {
7793 - error_log(sprintf(
7794 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7795 - $attempt + 1, $max_attempts, $http_code, $errno,
7796 - $is_transient ? 'yes' : 'no',
7797 - $can_retry ? 'Retrying.' : 'Giving up.'
7798 - ));
7799 - }
7800 -
7801 - if (!$can_retry) {
7802 - break;
7803 - }
7804 - }
7805 -
7806 - // Post-loop branch.
7807 - if (!$errno && $http_code === 200) {
7808 - // Happy path — save the complete response to maintain chat persistence.
7809 - if (!empty($full_response) && !empty($session_id)) {
7810 - $rag_context_for_storage = null;
7811 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7812 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7813 -
7814 - if ($has_rag_data || $has_action_data) {
7815 - $rag_context_for_storage = [];
7816 -
7817 - if ($has_rag_data) {
7818 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7819 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7820 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7821 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7822 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7823 - }
7824 -
7825 - if ($has_action_data) {
7826 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7827 - }
7828 - }
7829 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7830 - }
7831 -
7832 - return true;
7833 - }
7834 -
7835 - // Failure path — branch on whether SSE channel was opened.
7836 - return $this->mxchat_stream_emit_fallback(
7837 - 'openai',
7838 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7839 - $session_id,
7840 - $testing_data
7841 - );
7842 -
7843 - } catch (Exception $e) {
7844 - return $this->mxchat_stream_emit_fallback(
7845 - 'openai',
7846 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7847 - $session_id,
7848 - $testing_data
7849 - );
7850 - }
7851 -}
7852 -
7853 -/**
7854 - * Shared fallback emitter for streaming chat functions. Two outcomes:
7855 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
7856 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
7857 - * normal bot bubble. Transcript row is persisted.
7858 - * - streaming_headers_sent === false: SSE channel never opened (retries
7859 - * exhausted on initial connect). Emit a clean JSON response — the path
7860 - * the widget would normally hit if streaming wasn't even attempted.
7861 - *
7862 - * Used by all six *_stream functions after their per-attempt retry loop.
7863 - */
7864 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
7865 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
7866 -
7867 - if ($this->streaming_headers_sent) {
7868 - if ($is_error_array) {
7869 - echo "data: " . json_encode([
7870 - 'error' => true,
7871 - 'error_message' => $regular_response['error'],
7872 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7873 - 'text' => $regular_response['error'],
7874 - 'message' => $regular_response['error']
7875 - ]) . "\n\n";
7876 - echo "data: [DONE]\n\n";
7877 - flush();
7878 - return true;
7879 - }
7880 - $fallback_message = (string) $regular_response;
7881 - if (!empty($fallback_message) && !empty($session_id)) {
7882 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7883 - }
7884 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
7885 - echo "data: [DONE]\n\n";
7886 - flush();
7887 - return true;
7888 - }
7889 -
7890 - // SSE channel never opened — clean JSON fallback.
7891 - if ($is_error_array) {
7892 - header('Content-Type: application/json');
7893 - echo json_encode(array(
7894 - 'error' => true,
7895 - 'error_message' => $regular_response['error'],
7896 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7897 - 'text' => $regular_response['error'],
7898 - 'message' => $regular_response['error'],
7899 - ));
7900 - return true;
7901 - }
7902 -
7903 - $fallback_message = (string) $regular_response;
7904 - if (!empty($fallback_message) && !empty($session_id)) {
7905 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7906 - }
7907 - $response_data = array(
7908 - 'text' => $fallback_message,
7909 - 'html' => '',
7910 - 'session_id' => $session_id,
7911 - );
7912 - if ($testing_data !== null) {
7913 - $response_data['testing_data'] = $testing_data;
7914 - }
7915 - header('Content-Type: application/json');
7916 - echo json_encode($response_data);
7917 - return true;
7918 -}
7919 -
7920 -/**
7921 - * Resolve custom (OpenAI-compatible) provider config from settings.
7922 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
7923 - */
7924 -private function mxchat_resolve_custom_provider() {
7925 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
7926 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
7927 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
7928 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
7929 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
7930 -
7931 - $chat_url = $base_url . '/chat/completions';
7932 - if (!empty($api_version)) {
7933 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
7934 - }
7935 -
7936 - $headers = array('Content-Type: application/json');
7937 - if (!empty($api_key)) {
7938 - if ($auth_scheme === 'api-key') {
7939 - $headers[] = 'api-key: ' . $api_key;
7940 - } else {
7941 - $headers[] = 'Authorization: Bearer ' . $api_key;
7942 - }
7943 - }
7944 -
7945 - return array(
7946 - 'base_url' => $base_url,
7947 - 'api_key' => $api_key,
7948 - 'model' => $model !== '' ? $model : 'default',
7949 - 'auth_scheme' => $auth_scheme,
7950 - 'api_version' => $api_version,
7951 - 'chat_url' => $chat_url,
7952 - 'headers' => $headers,
7953 - );
7954 -}
7955 -
7956 -/**
7957 - * Streaming chat completion against an OpenAI-compatible custom provider
7958 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
7959 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
7960 - */
7961 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7962 - try {
7963 - $cfg = $this->mxchat_resolve_custom_provider();
7964 - if (empty($cfg['base_url'])) {
7965 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7966 - }
7967 -
7968 - $bot_id = $this->get_current_bot_id($session_id);
7969 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7970 - if (!is_array($conversation_history)) {
7971 - $conversation_history = array();
7972 - }
7973 -
7974 - $formatted_conversation = array();
7975 - $formatted_conversation[] = array(
7976 - 'role' => 'system',
7977 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7978 - );
7979 - foreach ($conversation_history as $message) {
7980 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7981 - $role = $message['role'];
7982 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7983 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7984 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
7985 - }
7986 - }
7987 -
7988 - if (headers_sent() || !function_exists('curl_init')) {
7989 - // No streaming capability — fall through to non-stream wrapper
7990 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7991 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
7992 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
7993 - }
7994 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7995 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7996 - header('Content-Type: application/json');
7997 - echo json_encode($response_data);
7998 - return true;
7999 - }
8000 -
8001 - $request_body = array(
8002 - 'model' => $cfg['model'],
8003 - 'messages' => $formatted_conversation,
8004 - 'stream' => true,
8005 - );
8006 - $body = json_encode($request_body);
8007 -
8008 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8009 -
8010 - $captured_status_code = 0;
8011 - $captured_body_pre_stream = '';
8012 - $full_response = '';
8013 - $stream_started = false;
8014 - $buffer = '';
8015 - $errno = 0;
8016 - $http_code = 0;
8017 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8018 - $backoff_ms = array(0, 750, 2000);
8019 -
8020 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8021 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8022 - usleep($backoff_ms[$attempt] * 1000);
8023 - }
8024 -
8025 - $captured_status_code = 0;
8026 - $captured_body_pre_stream = '';
8027 - $full_response = '';
8028 - $stream_started = false;
8029 - $buffer = '';
8030 -
8031 - $ch = curl_init();
8032 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8033 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8034 - curl_setopt($ch, CURLOPT_POST, true);
8035 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8036 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8037 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8038 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8039 -
8040 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8041 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8042 - $captured_status_code = (int) $m[1];
8043 - }
8044 - return strlen($header);
8045 - });
8046 -
8047 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8048 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8049 - $captured_body_pre_stream .= $data;
8050 - return strlen($data);
8051 - }
8052 -
8053 - if (!$this->streaming_headers_sent) {
8054 - $this->setup_streaming_headers();
8055 - }
8056 -
8057 - if (!$stream_started && $testing_data !== null) {
8058 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8059 - flush();
8060 - $stream_started = true;
8061 - }
8062 - $buffer .= $data;
8063 - $lines = explode("\n", $buffer);
8064 - $buffer = array_pop($lines);
8065 - foreach ($lines as $line) {
8066 - if (trim($line) === '') { continue; }
8067 - if (strpos($line, 'data: ') !== 0) { continue; }
8068 - $json_str = substr($line, 6);
8069 - if (trim($json_str) === '[DONE]') {
8070 - echo "data: [DONE]\n\n";
8071 - flush();
8072 - continue;
8073 - }
8074 - $json = json_decode(trim($json_str), true);
8075 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8076 - $content = $json['choices'][0]['delta']['content'];
8077 - $full_response .= $content;
8078 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8079 - flush();
8080 - }
8081 - }
8082 - return strlen($data);
8083 - });
8084 -
8085 - $response = curl_exec($ch);
8086 - $errno = curl_errno($ch);
8087 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8088 - curl_close($ch);
8089 -
8090 - if (!$errno && $http_code === 200) {
8091 - break;
8092 - }
8093 -
8094 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8095 - $can_retry = !$this->streaming_headers_sent
8096 - && ($attempt + 1) < $max_attempts
8097 - && $is_transient;
8098 -
8099 - if (defined('WP_DEBUG') && WP_DEBUG) {
8100 - error_log(sprintf(
8101 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8102 - $attempt + 1, $max_attempts, $http_code, $errno,
8103 - $is_transient ? 'yes' : 'no',
8104 - $can_retry ? 'Retrying.' : 'Giving up.'
8105 - ));
8106 - }
8107 -
8108 - if (!$can_retry) {
8109 - break;
8110 - }
8111 - }
8112 -
8113 - if (!$errno && $http_code === 200) {
8114 - if (!empty($full_response) && !empty($session_id)) {
8115 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8116 - }
8117 - return true;
8118 - }
8119 -
8120 - return $this->mxchat_stream_emit_fallback(
8121 - 'openai',
8122 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8123 - $session_id,
8124 - $testing_data
8125 - );
8126 -
8127 - } catch (Exception $e) {
8128 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8129 - }
8130 -}
8131 -
8132 -/**
8133 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8134 - * Returns string content on success, array['error'=>...] on failure.
8135 - */
8136 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8137 - $cfg = $this->mxchat_resolve_custom_provider();
8138 - if (empty($cfg['base_url'])) {
8139 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8140 - }
8141 -
8142 - $bot_id = $this->get_current_bot_id(null);
8143 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8144 - if (!is_array($conversation_history)) {
8145 - $conversation_history = array();
8146 - }
8147 -
8148 - $messages = array(array(
8149 - 'role' => 'system',
8150 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8151 - ));
8152 - foreach ($conversation_history as $message) {
8153 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8154 - $role = $message['role'];
8155 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8156 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8157 - $messages[] = array('role' => $role, 'content' => $message['content']);
8158 - }
8159 - }
8160 -
8161 - $headers_assoc = array('Content-Type' => 'application/json');
8162 - if (!empty($cfg['api_key'])) {
8163 - if ($cfg['auth_scheme'] === 'api-key') {
8164 - $headers_assoc['api-key'] = $cfg['api_key'];
8165 - } else {
8166 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8167 - }
8168 - }
8169 -
8170 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8171 - 'headers' => $headers_assoc,
8172 - 'body' => wp_json_encode(array(
8173 - 'model' => $cfg['model'],
8174 - 'messages' => $messages,
8175 - )),
8176 - 'timeout' => 120,
8177 - ), 'openai');
8178 -
8179 - if (is_wp_error($response)) {
8180 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8181 - }
8182 - $code = (int) wp_remote_retrieve_response_code($response);
8183 - if ($code < 200 || $code >= 300) {
8184 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8185 - }
8186 - $body = json_decode(wp_remote_retrieve_body($response), true);
8187 - if (isset($body['choices'][0]['message']['content'])) {
8188 - return (string) $body['choices'][0]['message']['content'];
8189 - }
8190 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8191 -}
8192 -
8193 -/**
8194 - * Generate response using OpenAI Responses API with web search tool
8195 - * This uses the newer Responses API which supports web search functionality
8196 - */
8197 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8198 - try {
8199 - $bot_id = $this->get_current_bot_id($session_id);
8200 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8201 -
8202 - if (!is_array($conversation_history)) {
8203 - $conversation_history = array();
8204 - }
8205 -
8206 - // Build the input for Responses API
8207 - // The Responses API uses a different format - we need to construct the input properly
8208 - $input_parts = [];
8209 -
8210 - // Add system instructions as context
8211 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8212 -
8213 - // Build conversation as input items for Responses API
8214 - foreach ($conversation_history as $message) {
8215 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8216 - $role = $message['role'];
8217 - if ($role === 'bot' || $role === 'agent') {
8218 - $role = 'assistant';
8219 - }
8220 - if (!in_array($role, ['assistant', 'user'])) {
8221 - $role = 'user';
8222 - }
8223 - $input_parts[] = [
8224 - 'type' => 'message',
8225 - 'role' => $role,
8226 - 'content' => $message['content']
8227 - ];
8228 - }
8229 - }
8230 -
8231 - // Build request body for Responses API
8232 - $request_body = [
8233 - 'model' => $selected_model,
8234 - 'input' => $input_parts,
8235 - 'instructions' => $system_context,
8236 - 'stream' => $streaming
8237 - ];
8238 -
8239 - // Only add web search tool if web search is enabled in settings
8240 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8241 - if ($web_search_enabled) {
8242 - $request_body['tools'] = [
8243 - ['type' => 'web_search']
8244 - ];
8245 - }
8246 -
8247 - // Add reasoning effort for supported models
8248 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8249 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8250 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8251 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8252 - $request_body['reasoning'] = ['effort' => 'low'];
8253 - } elseif ($selected_model === 'gpt-5.5') {
8254 - $request_body['reasoning'] = ['effort' => 'low'];
8255 - } elseif ($selected_model === 'gpt-5.4') {
8256 - $request_body['reasoning'] = ['effort' => 'low'];
8257 - }
8258 - }
8259 -
8260 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8261 -
8262 - if ($streaming) {
8263 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8264 - } else {
8265 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8266 - }
8267 -
8268 - } catch (Exception $e) {
8269 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8270 - return [
8271 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8272 - 'error_code' => 'web_search_exception'
8273 - ];
8274 - }
8275 -}
8276 -
8277 -/**
8278 - * Handle non-streaming web search response
8279 - */
8280 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8281 - $request_body['stream'] = false;
8282 -
8283 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8284 - 'headers' => array(
8285 - 'Authorization' => 'Bearer ' . $api_key,
8286 - 'Content-Type' => 'application/json'
8287 - ),
8288 - 'body' => json_encode($request_body),
8289 - 'timeout' => 90
8290 - ), 'openai');
8291 -
8292 - if (is_wp_error($response)) {
8293 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8294 - return [
8295 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8296 - 'error_code' => 'web_search_connection_error'
8297 - ];
8298 - }
8299 -
8300 - $response_code = wp_remote_retrieve_response_code($response);
8301 - $response_body = wp_remote_retrieve_body($response);
8302 -
8303 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8304 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8305 -
8306 - if ($response_code !== 200) {
8307 - $error_data = json_decode($response_body, true);
8308 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8309 - return [
8310 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8311 - 'error_code' => 'web_search_api_error'
8312 - ];
8313 - }
8314 -
8315 - $result = json_decode($response_body, true);
8316 -
8317 - if (json_last_error() !== JSON_ERROR_NONE) {
8318 - return [
8319 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8320 - 'error_code' => 'web_search_json_error'
8321 - ];
8322 - }
8323 -
8324 - // Extract the response text and citations from Responses API format
8325 - $output_text = '';
8326 - $citations = [];
8327 -
8328 - if (isset($result['output'])) {
8329 - foreach ($result['output'] as $output_item) {
8330 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8331 - foreach ($output_item['content'] as $content_item) {
8332 - if ($content_item['type'] === 'output_text') {
8333 - $output_text .= $content_item['text'];
8334 -
8335 - // Extract citations/annotations
8336 - if (isset($content_item['annotations'])) {
8337 - foreach ($content_item['annotations'] as $annotation) {
8338 - if ($annotation['type'] === 'url_citation') {
8339 - $citations[] = [
8340 - 'url' => $annotation['url'],
8341 - 'title' => $annotation['title'] ?? ''
8342 - ];
8343 - }
8344 - }
8345 - }
8346 - }
8347 - }
8348 - }
8349 - }
8350 - }
8351 -
8352 - // If we have citations, append them to the response
8353 - if (!empty($citations)) {
8354 - $output_text .= "\n\n**Sources:**\n";
8355 - $seen_urls = [];
8356 - foreach ($citations as $citation) {
8357 - if (!in_array($citation['url'], $seen_urls)) {
8358 - $seen_urls[] = $citation['url'];
8359 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8360 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8361 - }
8362 - }
8363 - }
8364 -
8365 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8366 - // which includes rag_context for the "sources" link in transcripts.
8367 -
8368 - return $output_text;
8369 -}
8370 -
8371 -/**
8372 - * Handle streaming web search response using Responses API
8373 - */
8374 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8375 - $request_body['stream'] = true;
8376 -
8377 - // Check if we can stream
8378 - if (headers_sent() || !function_exists('curl_init')) {
8379 - // Fallback to non-streaming
8380 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8381 - }
8382 -
8383 - // Setup streaming headers
8384 - $this->setup_streaming_headers();
8385 -
8386 - $ch = curl_init();
8387 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8388 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8389 - curl_setopt($ch, CURLOPT_POST, true);
8390 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8391 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8392 - 'Content-Type: application/json',
8393 - 'Authorization: Bearer ' . $api_key
8394 - ));
8395 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8396 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8397 -
8398 - $full_response = '';
8399 - $stream_started = false;
8400 - $buffer = '';
8401 - $citations = [];
8402 -
8403 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
8404 - // Send testing data as first event if available
8405 - if (!$stream_started && $testing_data !== null) {
8406 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8407 - flush();
8408 - $stream_started = true;
8409 - }
8410 -
8411 - $buffer .= $data;
8412 - $lines = explode("\n", $buffer);
8413 - $buffer = array_pop($lines);
8414 -
8415 - foreach ($lines as $line) {
8416 - if (trim($line) === '') continue;
8417 - if (strpos($line, 'data: ') !== 0) continue;
8418 -
8419 - $json_str = substr($line, 6);
8420 -
8421 - if (trim($json_str) === '[DONE]') {
8422 - // Append citations if we have any
8423 - if (!empty($citations)) {
8424 - $citation_text = "\n\n**Sources:**\n";
8425 - $seen_urls = [];
8426 - foreach ($citations as $citation) {
8427 - if (!in_array($citation['url'], $seen_urls)) {
8428 - $seen_urls[] = $citation['url'];
8429 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8430 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8431 - }
8432 - }
8433 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
8434 - $full_response .= $citation_text;
8435 - flush();
8436 - }
8437 - echo "data: [DONE]\n\n";
8438 - flush();
8439 - continue;
8440 - }
8441 -
8442 - $json = json_decode(trim($json_str), true);
8443 - if (!$json) continue;
8444 -
8445 - // Handle Responses API streaming events
8446 - // The format is different from Chat Completions
8447 - if (isset($json['type'])) {
8448 - switch ($json['type']) {
8449 - case 'response.output_text.delta':
8450 - // Text content delta
8451 - if (isset($json['delta'])) {
8452 - $content = $json['delta'];
8453 - $full_response .= $content;
8454 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8455 - flush();
8456 - }
8457 - break;
8458 -
8459 - case 'response.output_item.done':
8460 - // Check for citations in completed items
8461 - if (isset($json['item']['content'])) {
8462 - foreach ($json['item']['content'] as $content_item) {
8463 - if (isset($content_item['annotations'])) {
8464 - foreach ($content_item['annotations'] as $annotation) {
8465 - if ($annotation['type'] === 'url_citation') {
8466 - $citations[] = [
8467 - 'url' => $annotation['url'],
8468 - 'title' => $annotation['title'] ?? ''
8469 - ];
8470 - }
8471 - }
8472 - }
8473 - }
8474 - }
8475 - break;
8476 - }
8477 - }
8478 - }
8479 -
8480 - return strlen($data);
8481 - });
8482 -
8483 - $response = curl_exec($ch);
8484 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8485 -
8486 - if (curl_errno($ch) || $http_code !== 200) {
8487 - $curl_error = curl_error($ch);
8488 - curl_close($ch);
8489 -
8490 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
8491 -
8492 - return $this->mxchat_stream_emit_fallback(
8493 - 'web_search',
8494 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
8495 - $session_id,
8496 - $testing_data
8497 - );
8498 - }
8499 -
8500 - curl_close($ch);
8501 -
8502 - // Save the complete response with RAG context so the "sources" link
8503 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
8504 - if (!empty($full_response) && !empty($session_id)) {
8505 - $rag_context_for_storage = null;
8506 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8507 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8508 -
8509 - if ($has_rag_data || $has_action_data) {
8510 - $rag_context_for_storage = [];
8511 -
8512 - if ($has_rag_data) {
8513 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8514 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8515 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8516 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8517 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8518 - }
8519 -
8520 - if ($has_action_data) {
8521 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8522 - }
8523 - }
8524 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8525 - }
8526 -
8527 - return true;
8528 -}
8529 -
8530 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8531 - try {
8532 - // Get bot ID from session or request
8533 - $bot_id = $this->get_current_bot_id($session_id);
8534 -
8535 - // Get system prompt instructions using centralized function
8536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8537 - // Ensure conversation_history is an array
8538 - if (!is_array($conversation_history)) {
8539 - $conversation_history = array();
8540 - }
8541 -
8542 - // Clean and validate conversation history
8543 - foreach ($conversation_history as &$message) {
8544 - // Convert bot and agent roles to assistant
8545 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
8546 - $message['role'] = 'assistant';
8547 - }
8548 -
8549 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
8550 - if (!in_array($message['role'], ['assistant', 'user'])) {
8551 - $message['role'] = 'user';
8552 - }
8553 -
8554 - // Ensure content field exists
8555 - if (!isset($message['content']) || empty($message['content'])) {
8556 - $message['content'] = '';
8557 - }
8558 -
8559 - // Remove any unsupported fields
8560 - $message = array_intersect_key($message, array_flip(['role', 'content']));
8561 - }
8562 -
8563 - // Add relevant content as the latest user message
8564 - $conversation_history[] = [
8565 - 'role' => 'user',
8566 - 'content' => $relevant_content
8567 - ];
8568 -
8569 - // Prepare the request body with stream: true
8570 - $payload = [
8571 - 'model' => $selected_model,
8572 - 'messages' => $conversation_history,
8573 - 'max_tokens' => 1000,
8574 - 'temperature' => 0.8,
8575 - 'system' => $system_prompt_instructions,
8576 - 'stream' => true
8577 - ];
8578 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
8579 - $body = json_encode($payload);
8580 -
8581 - // Check if we can actually stream (headers not sent, etc.)
8582 - if (headers_sent() || !function_exists('curl_init')) {
8583 - // Fallback to regular response with testing data
8584 - //error_log("MxChat: Streaming not possible, falling back to regular response");
8585 - $regular_response = $this->mxchat_generate_response_claude(
8586 - $selected_model,
8587 - $claude_api_key,
8588 - array_slice($conversation_history, 0, -1), // Remove the added content
8589 - $relevant_content
8590 - );
8591 -
8592 - // Save bot response to transcript
8593 - if (!empty($regular_response) && !empty($session_id)) {
8594 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8595 - }
8596 -
8597 - // Return as JSON with testing data
8598 - $response_data = [
8599 - 'text' => $regular_response,
8600 - 'html' => '',
8601 - 'session_id' => $session_id
8602 - ];
8603 -
8604 - if ($testing_data !== null) {
8605 - $response_data['testing_data'] = $testing_data;
8606 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
8607 - }
8608 -
8609 - // Clear any streaming headers and send JSON
8610 - if (headers_sent() === false) {
8611 - header('Content-Type: application/json');
8612 - }
8613 - echo json_encode($response_data);
8614 - return true; // Indicate we handled the response
8615 - }
8616 -
8617 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8618 -
8619 - $captured_status_code = 0;
8620 - $captured_body_pre_stream = '';
8621 - $full_response = '';
8622 - $stream_started = false;
8623 - $buffer = '';
8624 - $errno = 0;
8625 - $http_code = 0;
8626 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8627 - $backoff_ms = array(0, 750, 2000);
8628 -
8629 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8630 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8631 - usleep($backoff_ms[$attempt] * 1000);
8632 - }
8633 -
8634 - $captured_status_code = 0;
8635 - $captured_body_pre_stream = '';
8636 - $full_response = '';
8637 - $stream_started = false;
8638 - $buffer = '';
8639 -
8640 - $ch = curl_init();
8641 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
8642 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8643 - curl_setopt($ch, CURLOPT_POST, true);
8644 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8645 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8646 - 'Content-Type: application/json',
8647 - 'x-api-key: ' . $claude_api_key,
8648 - 'anthropic-version: 2023-06-01'
8649 - ));
8650 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8651 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8652 -
8653 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8654 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8655 - $captured_status_code = (int) $m[1];
8656 - }
8657 - return strlen($header);
8658 - });
8659 -
8660 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8661 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8662 - $captured_body_pre_stream .= $data;
8663 - return strlen($data);
8664 - }
8665 -
8666 - if (!$this->streaming_headers_sent) {
8667 - $this->setup_streaming_headers();
8668 - }
8669 -
8670 - if (!$stream_started && $testing_data !== null) {
8671 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8672 - flush();
8673 - $stream_started = true;
8674 - }
8675 -
8676 - $buffer .= $data;
8677 - $lines = explode("\n", $buffer);
8678 - $buffer = array_pop($lines);
8679 -
8680 - foreach ($lines as $line) {
8681 - if (trim($line) === '') {
8682 - continue;
8683 - }
8684 -
8685 - if (strpos($line, 'event: ') === 0) {
8686 - continue;
8687 - }
8688 -
8689 - if (strpos($line, 'data: ') === 0) {
8690 - $json_str = substr($line, 6);
8691 -
8692 - $json = json_decode(trim($json_str), true);
8693 - if (json_last_error() !== JSON_ERROR_NONE) {
8694 - continue;
8695 - }
8696 -
8697 - if (isset($json['type'])) {
8698 - switch ($json['type']) {
8699 - case 'content_block_delta':
8700 - if (isset($json['delta']['text'])) {
8701 - $content = $json['delta']['text'];
8702 - $full_response .= $content;
8703 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8704 - flush();
8705 - }
8706 - break;
8707 -
8708 - case 'message_stop':
8709 - echo "data: [DONE]\n\n";
8710 - flush();
8711 - break;
8712 -
8713 - case 'error':
8714 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8715 - flush();
8716 - break;
8717 - }
8718 - }
8719 - }
8720 - }
8721 -
8722 - return strlen($data);
8723 - });
8724 -
8725 - $response = curl_exec($ch);
8726 - $errno = curl_errno($ch);
8727 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8728 - curl_close($ch);
8729 -
8730 - if (!$errno && $http_code === 200) {
8731 - break;
8732 - }
8733 -
8734 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
8735 - $can_retry = !$this->streaming_headers_sent
8736 - && ($attempt + 1) < $max_attempts
8737 - && $is_transient;
8738 -
8739 - if (defined('WP_DEBUG') && WP_DEBUG) {
8740 - error_log(sprintf(
8741 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8742 - $attempt + 1, $max_attempts, $http_code, $errno,
8743 - $is_transient ? 'yes' : 'no',
8744 - $can_retry ? 'Retrying.' : 'Giving up.'
8745 - ));
8746 - }
8747 -
8748 - if (!$can_retry) {
8749 - break;
8750 - }
8751 - }
8752 -
8753 - if ($errno || $http_code !== 200) {
8754 - return $this->mxchat_stream_emit_fallback(
8755 - 'anthropic',
8756 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content),
8757 - $session_id,
8758 - $testing_data
8759 - );
8760 - }
8761 -
8762 - // Save the complete response to maintain chat persistence
8763 - if (!empty($full_response) && !empty($session_id)) {
8764 - // Prepare RAG context for streaming response
8765 - $rag_context_for_storage = null;
8766 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8767 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8768 -
8769 - if ($has_rag_data || $has_action_data) {
8770 - $rag_context_for_storage = [];
8771 -
8772 - if ($has_rag_data) {
8773 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8774 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8775 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8776 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8777 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8778 - }
8779 -
8780 - if ($has_action_data) {
8781 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8782 - }
8783 - }
8784 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8785 - }
8786 -
8787 - return true; // Indicate streaming completed successfully
8788 -
8789 - } catch (Exception $e) {
8790 - return $this->mxchat_stream_emit_fallback(
8791 - 'anthropic',
8792 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content),
8793 - $session_id,
8794 - $testing_data
8795 - );
8796 - }
8797 -}
8798 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8799 - try {
8800 - // Get bot ID from session or request
8801 - $bot_id = $this->get_current_bot_id($session_id);
8802 -
8803 - // Get system prompt instructions using centralized function
8804 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8805 -
8806 - // Ensure conversation_history is an array
8807 - if (!is_array($conversation_history)) {
8808 - $conversation_history = array();
8809 - }
8810 -
8811 - // Format conversation history for X.AI (same as OpenAI format)
8812 - $formatted_conversation = array();
8813 -
8814 - $formatted_conversation[] = array(
8815 - 'role' => 'system',
8816 - 'content' => $system_prompt_instructions . " " . $relevant_content
8817 - );
8818 -
8819 - foreach ($conversation_history as $message) {
8820 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8821 - $role = $message['role'];
8822 - if ($role === 'bot' || $role === 'agent') {
8823 - $role = 'assistant';
8824 - }
8825 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8826 - $role = 'user';
8827 - }
8828 - $formatted_conversation[] = array(
8829 - 'role' => $role,
8830 - 'content' => $message['content']
8831 - );
8832 - }
8833 - }
8834 -
8835 - // Check if we can actually stream
8836 - if (headers_sent() || !function_exists('curl_init')) {
8837 - // Fallback to regular response with testing data
8838 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
8839 - $regular_response = $this->mxchat_generate_response_xai(
8840 - $selected_model,
8841 - $xai_api_key,
8842 - $conversation_history,
8843 - $relevant_content
8844 - );
8845 -
8846 - // Save bot response to transcript
8847 - if (!empty($regular_response) && !empty($session_id)) {
8848 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8849 - }
8850 -
8851 - $response_data = [
8852 - 'text' => $regular_response,
8853 - 'html' => '',
8854 - 'session_id' => $session_id
8855 - ];
8856 -
8857 - if ($testing_data !== null) {
8858 - $response_data['testing_data'] = $testing_data;
8859 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
8860 - }
8861 -
8862 - header('Content-Type: application/json');
8863 - echo json_encode($response_data);
8864 - return true;
8865 - }
8866 -
8867 - // Prepare the request body with stream: true
8868 - $body = json_encode([
8869 - 'model' => $selected_model,
8870 - 'messages' => $formatted_conversation,
8871 - 'temperature' => 0.8,
8872 - 'stream' => true
8873 - ]);
8874 -
8875 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8876 -
8877 - $captured_status_code = 0;
8878 - $captured_body_pre_stream = '';
8879 - $full_response = '';
8880 - $stream_started = false;
8881 - $buffer = '';
8882 - $errno = 0;
8883 - $http_code = 0;
8884 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8885 - $backoff_ms = array(0, 750, 2000);
8886 -
8887 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8888 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8889 - usleep($backoff_ms[$attempt] * 1000);
8890 - }
8891 -
8892 - $captured_status_code = 0;
8893 - $captured_body_pre_stream = '';
8894 - $full_response = '';
8895 - $stream_started = false;
8896 - $buffer = '';
8897 -
8898 - $ch = curl_init();
8899 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
8900 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8901 - curl_setopt($ch, CURLOPT_POST, true);
8902 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8903 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8904 - 'Content-Type: application/json',
8905 - 'Authorization: Bearer ' . $xai_api_key
8906 - ));
8907 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8908 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8909 -
8910 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8911 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8912 - $captured_status_code = (int) $m[1];
8913 - }
8914 - return strlen($header);
8915 - });
8916 -
8917 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8918 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8919 - $captured_body_pre_stream .= $data;
8920 - return strlen($data);
8921 - }
8922 -
8923 - if (!$this->streaming_headers_sent) {
8924 - $this->setup_streaming_headers();
8925 - }
8926 -
8927 - if (!$stream_started && $testing_data !== null) {
8928 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8929 - flush();
8930 - $stream_started = true;
8931 - }
8932 -
8933 - $buffer .= $data;
8934 - $lines = explode("\n", $buffer);
8935 - $buffer = array_pop($lines);
8936 -
8937 - foreach ($lines as $line) {
8938 - if (trim($line) === '') {
8939 - continue;
8940 - }
8941 - if (strpos($line, 'data: ') !== 0) {
8942 - continue;
8943 - }
8944 -
8945 - $json_str = substr($line, 6);
8946 -
8947 - if (trim($json_str) === '[DONE]') {
8948 - echo "data: [DONE]\n\n";
8949 - flush();
8950 - continue;
8951 - }
8952 -
8953 - $json = json_decode(trim($json_str), true);
8954 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8955 - $content = $json['choices'][0]['delta']['content'];
8956 - $full_response .= $content;
8957 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8958 - flush();
8959 - }
8960 - }
8961 -
8962 - return strlen($data);
8963 - });
8964 -
8965 - $response = curl_exec($ch);
8966 - $errno = curl_errno($ch);
8967 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8968 - curl_close($ch);
8969 -
8970 - if (!$errno && $http_code === 200) {
8971 - break;
8972 - }
8973 -
8974 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
8975 - $can_retry = !$this->streaming_headers_sent
8976 - && ($attempt + 1) < $max_attempts
8977 - && $is_transient;
8978 -
8979 - if (defined('WP_DEBUG') && WP_DEBUG) {
8980 - error_log(sprintf(
8981 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8982 - $attempt + 1, $max_attempts, $http_code, $errno,
8983 - $is_transient ? 'yes' : 'no',
8984 - $can_retry ? 'Retrying.' : 'Giving up.'
8985 - ));
8986 - }
8987 -
8988 - if (!$can_retry) {
8989 - break;
8990 - }
8991 - }
8992 -
8993 - if ($errno || $http_code !== 200) {
8994 - return $this->mxchat_stream_emit_fallback(
8995 - 'xai',
8996 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
8997 - $session_id,
8998 - $testing_data
8999 - );
9000 - }
9001 -
9002 - // Save the complete response to maintain chat persistence
9003 - if (!empty($full_response) && !empty($session_id)) {
9004 - // Prepare RAG context for streaming response
9005 - $rag_context_for_storage = null;
9006 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9007 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9008 -
9009 - if ($has_rag_data || $has_action_data) {
9010 - $rag_context_for_storage = [];
9011 -
9012 - if ($has_rag_data) {
9013 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9014 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9015 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9016 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9017 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9018 - }
9019 -
9020 - if ($has_action_data) {
9021 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9022 - }
9023 - }
9024 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9025 - }
9026 -
9027 - return true; // Indicate streaming completed successfully
9028 -
9029 - } catch (Exception $e) {
9030 - return $this->mxchat_stream_emit_fallback(
9031 - 'xai',
9032 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9033 - $session_id,
9034 - $testing_data
9035 - );
9036 - }
9037 -}
9038 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9039 - try {
9040 - // Get bot ID from session or request
9041 - $bot_id = $this->get_current_bot_id($session_id);
9042 -
9043 - // Get system prompt instructions using centralized function
9044 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9045 -
9046 - // Ensure conversation_history is an array
9047 - if (!is_array($conversation_history)) {
9048 - $conversation_history = array();
9049 - }
9050 -
9051 - // Format conversation history for DeepSeek
9052 - $formatted_conversation = array();
9053 -
9054 - $formatted_conversation[] = array(
9055 - 'role' => 'system',
9056 - 'content' => $system_prompt_instructions . " " . $relevant_content
9057 - );
9058 -
9059 - foreach ($conversation_history as $message) {
9060 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9061 - $role = $message['role'];
9062 - if ($role === 'bot' || $role === 'agent') {
9063 - $role = 'assistant';
9064 - }
9065 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9066 - $role = 'user';
9067 - }
9068 - $formatted_conversation[] = array(
9069 - 'role' => $role,
9070 - 'content' => $message['content']
9071 - );
9072 - }
9073 - }
9074 -
9075 - // Check if we can actually stream
9076 - if (headers_sent() || !function_exists('curl_init')) {
9077 - // Fallback to regular response with testing data
9078 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9079 - $regular_response = $this->mxchat_generate_response_deepseek(
9080 - $selected_model,
9081 - $deepseek_api_key,
9082 - $conversation_history,
9083 - $relevant_content
9084 - );
9085 -
9086 - // Save bot response to transcript
9087 - if (!empty($regular_response) && !empty($session_id)) {
9088 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9089 - }
9090 -
9091 - $response_data = [
9092 - 'text' => $regular_response,
9093 - 'html' => '',
9094 - 'session_id' => $session_id
9095 - ];
9096 -
9097 - if ($testing_data !== null) {
9098 - $response_data['testing_data'] = $testing_data;
9099 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
9100 - }
9101 -
9102 - header('Content-Type: application/json');
9103 - echo json_encode($response_data);
9104 - return true;
9105 - }
9106 -
9107 - // Prepare the request body with stream: true
9108 - $body = json_encode([
9109 - 'model' => $selected_model,
9110 - 'messages' => $formatted_conversation,
9111 - 'temperature' => 0.8,
9112 - 'stream' => true
9113 - ]);
9114 -
9115 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9116 -
9117 - $captured_status_code = 0;
9118 - $captured_body_pre_stream = '';
9119 - $full_response = '';
9120 - $stream_started = false;
9121 - $buffer = '';
9122 - $errno = 0;
9123 - $http_code = 0;
9124 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9125 - $backoff_ms = array(0, 750, 2000);
9126 -
9127 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9128 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9129 - usleep($backoff_ms[$attempt] * 1000);
9130 - }
9131 -
9132 - $captured_status_code = 0;
9133 - $captured_body_pre_stream = '';
9134 - $full_response = '';
9135 - $stream_started = false;
9136 - $buffer = '';
9137 -
9138 - $ch = curl_init();
9139 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9140 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9141 - curl_setopt($ch, CURLOPT_POST, true);
9142 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9143 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9144 - 'Content-Type: application/json',
9145 - 'Authorization: Bearer ' . $deepseek_api_key
9146 - ));
9147 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9148 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9149 -
9150 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9151 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9152 - $captured_status_code = (int) $m[1];
9153 - }
9154 - return strlen($header);
9155 - });
9156 -
9157 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9158 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9159 - $captured_body_pre_stream .= $data;
9160 - return strlen($data);
9161 - }
9162 -
9163 - if (!$this->streaming_headers_sent) {
9164 - $this->setup_streaming_headers();
9165 - }
9166 -
9167 - if (!$stream_started && $testing_data !== null) {
9168 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9169 - flush();
9170 - $stream_started = true;
9171 - }
9172 -
9173 - $buffer .= $data;
9174 - $lines = explode("\n", $buffer);
9175 - $buffer = array_pop($lines);
9176 -
9177 - foreach ($lines as $line) {
9178 - if (trim($line) === '') {
9179 - continue;
9180 - }
9181 - if (strpos($line, 'data: ') !== 0) {
9182 - continue;
9183 - }
9184 -
9185 - $json_str = substr($line, 6);
9186 -
9187 - if (trim($json_str) === '[DONE]') {
9188 - echo "data: [DONE]\n\n";
9189 - flush();
9190 - continue;
9191 - }
9192 -
9193 - $json = json_decode(trim($json_str), true);
9194 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9195 - $content = $json['choices'][0]['delta']['content'];
9196 - $full_response .= $content;
9197 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9198 - flush();
9199 - }
9200 - }
9201 -
9202 - return strlen($data);
9203 - });
9204 -
9205 - $response = curl_exec($ch);
9206 - $errno = curl_errno($ch);
9207 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9208 - curl_close($ch);
9209 -
9210 - if (!$errno && $http_code === 200) {
9211 - break;
9212 - }
9213 -
9214 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9215 - $can_retry = !$this->streaming_headers_sent
9216 - && ($attempt + 1) < $max_attempts
9217 - && $is_transient;
9218 -
9219 - if (defined('WP_DEBUG') && WP_DEBUG) {
9220 - error_log(sprintf(
9221 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9222 - $attempt + 1, $max_attempts, $http_code, $errno,
9223 - $is_transient ? 'yes' : 'no',
9224 - $can_retry ? 'Retrying.' : 'Giving up.'
9225 - ));
9226 - }
9227 -
9228 - if (!$can_retry) {
9229 - break;
9230 - }
9231 - }
9232 -
9233 - if ($errno || $http_code !== 200) {
9234 - return $this->mxchat_stream_emit_fallback(
9235 - 'openai',
9236 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9237 - $session_id,
9238 - $testing_data
9239 - );
9240 - }
9241 -
9242 - // Save the complete response to maintain chat persistence
9243 - if (!empty($full_response) && !empty($session_id)) {
9244 - // Prepare RAG context for streaming response
9245 - $rag_context_for_storage = null;
9246 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9247 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9248 -
9249 - if ($has_rag_data || $has_action_data) {
9250 - $rag_context_for_storage = [];
9251 -
9252 - if ($has_rag_data) {
9253 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9254 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9255 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9256 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9257 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9258 - }
9259 -
9260 - if ($has_action_data) {
9261 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9262 - }
9263 - }
9264 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9265 - }
9266 -
9267 - return true; // Indicate streaming completed successfully
9268 -
9269 - } catch (Exception $e) {
9270 - return $this->mxchat_stream_emit_fallback(
9271 - 'openai',
9272 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9273 - $session_id,
9274 - $testing_data
9275 - );
9276 - }
9277 -}
9278 -
9279 -
9280 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content) {
9281 - try {
9282 - if (!is_array($conversation_history)) {
9283 - $conversation_history = array();
9284 - }
9285 -
9286 - $bot_id = $this->get_current_bot_id('');
9287 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9288 -
9289 - $formatted_conversation = array();
9290 -
9291 - $formatted_conversation[] = array(
9292 - 'role' => 'system',
9293 - 'content' => $system_prompt_instructions . " " . $relevant_content
9294 - );
9295 -
9296 - foreach ($conversation_history as $message) {
9297 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9298 - $role = $message['role'];
9299 -
9300 - if ($role === 'bot' || $role === 'agent') {
9301 - $role = 'assistant';
9302 - }
9303 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9304 - $role = 'user';
9305 - }
9306 -
9307 - $formatted_conversation[] = array(
9308 - 'role' => $role,
9309 - 'content' => $message['content']
9310 - );
9311 - }
9312 - }
9313 -
9314 - $body = json_encode([
9315 - 'model' => $selected_model,
9316 - 'messages' => $formatted_conversation,
9317 - 'temperature' => 1,
9318 - ]);
9319 -
9320 - $args = [
9321 - 'body' => $body,
9322 - 'headers' => [
9323 - 'Content-Type' => 'application/json',
9324 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9325 - 'HTTP-Referer' => home_url(),
9326 - 'X-Title' => get_bloginfo('name'),
9327 - ],
9328 - 'timeout' => 60,
9329 - 'redirection' => 5,
9330 - 'blocking' => true,
9331 - 'httpversion' => '1.0',
9332 - 'sslverify' => true,
9333 - ];
9334 -
9335 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9336 -
9337 - if (is_wp_error($response)) {
9338 - $error_message = $response->get_error_message();
9339 - return [
9340 - 'error' => esc_html__('Connection error when contacting OpenRouter: ', 'mxchat') . esc_html($error_message),
9341 - 'error_code' => 'openrouter_connection_error',
9342 - 'provider' => 'openrouter'
9343 - ];
9344 - }
9345 -
9346 - $status_code = wp_remote_retrieve_response_code($response);
9347 - if ($status_code !== 200) {
9348 - $response_body = wp_remote_retrieve_body($response);
9349 - $decoded_response = json_decode($response_body, true);
9350 -
9351 - $error_message = isset($decoded_response['error']['message'])
9352 - ? $decoded_response['error']['message']
9353 - : 'HTTP Error ' . $status_code;
9354 -
9355 - return [
9356 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9357 - 'error_code' => 'openrouter_api_error',
9358 - 'provider' => 'openrouter',
9359 - 'status_code' => $status_code
9360 - ];
9361 - }
9362 -
9363 - $response_body = wp_remote_retrieve_body($response);
9364 - $decoded_response = json_decode($response_body, true);
9365 -
9366 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9367 - return trim($decoded_response['choices'][0]['message']['content']);
9368 - } else {
9369 - return [
9370 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9371 - 'error_code' => 'openrouter_response_format_error',
9372 - 'provider' => 'openrouter'
9373 - ];
9374 - }
9375 - } catch (Exception $e) {
9376 - return [
9377 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9378 - 'error_code' => 'openrouter_exception',
9379 - 'provider' => 'openrouter'
9380 - ];
9381 - }
9382 -}
9383 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
9384 -
9385 - // Get bot ID from session or request
9386 - $bot_id = $this->get_current_bot_id($session_id);
9387 -
9388 - // Get system prompt instructions using centralized function
9389 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9390 -
9391 - // Clean and validate conversation history
9392 - foreach ($conversation_history as &$message) {
9393 - // Convert bot and agent roles to assistant
9394 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
9395 - $message['role'] = 'assistant';
9396 - }
9397 -
9398 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
9399 - if (!in_array($message['role'], ['assistant', 'user'])) {
9400 - $message['role'] = 'user';
9401 - }
9402 -
9403 - // Ensure content field exists
9404 - if (!isset($message['content']) || empty($message['content'])) {
9405 - $message['content'] = '';
9406 - }
9407 -
9408 - // Remove any unsupported fields
9409 - $message = array_intersect_key($message, array_flip(['role', 'content']));
9410 - }
9411 -
9412 - // Add relevant content as the latest user message
9413 - $conversation_history[] = [
9414 - 'role' => 'user',
9415 - 'content' => $relevant_content
9416 - ];
9417 -
9418 - // Build request body
9419 - $payload = [
9420 - 'model' => $selected_model,
9421 - 'max_tokens' => 1000,
9422 - 'temperature' => 0.8,
9423 - 'messages' => $conversation_history,
9424 - 'system' => $system_prompt_instructions
9425 - ];
9426 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9427 - $body = json_encode($payload);
9428 -
9429 - // Set up API request
9430 - $args = [
9431 - 'body' => $body,
9432 - 'headers' => [
9433 - 'Content-Type' => 'application/json',
9434 - 'x-api-key' => $claude_api_key,
9435 - 'anthropic-version' => '2023-06-01'
9436 - ],
9437 - 'timeout' => 60,
9438 - 'redirection' => 5,
9439 - 'blocking' => true,
9440 - 'httpversion' => '1.0',
9441 - 'sslverify' => true,
9442 - ];
9443 -
9444 - // Make API request
9445 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
9446 -
9447 - // Check for WordPress errors
9448 - if (is_wp_error($response)) {
9449 - //error_log("Claude API request error: " . $response->get_error_message());
9450 - return "Sorry, there was an error connecting to the API.";
9451 - }
9452 -
9453 - // Check HTTP response code
9454 - $http_code = wp_remote_retrieve_response_code($response);
9455 - if ($http_code !== 200) {
9456 - $error_body = wp_remote_retrieve_body($response);
9457 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
9458 -
9459 - // Try to extract error message from response
9460 - $error_data = json_decode($error_body, true);
9461 - $error_message = isset($error_data['error']['message']) ?
9462 - $error_data['error']['message'] :
9463 - "HTTP error " . $http_code;
9464 -
9465 - return "Sorry, the API returned an error: " . $error_message;
9466 - }
9467 -
9468 - // Parse response
9469 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
9470 -
9471 - // Check for JSON decode errors
9472 - if (json_last_error() !== JSON_ERROR_NONE) {
9473 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
9474 - return "Sorry, there was an error processing the API response.";
9475 - }
9476 -
9477 - // Extract and validate response content. claude-fable-5 prepends a
9478 - // thinking block to content even with no thinking param — take the first
9479 - // TEXT block rather than content[0].
9480 - if (isset($response_body['content']) && is_array($response_body['content'])) {
9481 - foreach ($response_body['content'] as $block) {
9482 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
9483 - return trim($block['text']);
9484 - }
9485 - }
9486 - }
9487 -
9488 - // Log unexpected response format
9489 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
9490 - return "Sorry, I received an unexpected response format from the API.";
9491 -}
9492 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
9493 - try {
9494 - // Ensure conversation_history is an array
9495 - if (!is_array($conversation_history)) {
9496 - $conversation_history = array();
9497 - }
9498 -
9499 - // Get bot ID from session or request
9500 - $bot_id = $this->get_current_bot_id('');
9501 -
9502 - // Get system prompt instructions using centralized function
9503 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9504 -
9505 - // Create a new array for the formatted conversation
9506 - $formatted_conversation = array();
9507 -
9508 - // Add system message first
9509 - $formatted_conversation[] = array(
9510 - 'role' => 'system',
9511 - 'content' => $system_prompt_instructions . " " . $relevant_content
9512 - );
9513 -
9514 - // Add the rest of the conversation history
9515 - foreach ($conversation_history as $message) {
9516 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9517 - $role = $message['role'];
9518 -
9519 - // Convert roles to supported format
9520 - if ($role === 'bot' || $role === 'agent') {
9521 - $role = 'assistant';
9522 - }
9523 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9524 - $role = 'user';
9525 - }
9526 -
9527 - $formatted_conversation[] = array(
9528 - 'role' => $role,
9529 - 'content' => $message['content']
9530 - );
9531 - }
9532 - }
9533 -
9534 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
9535 - $is_gpt5_model = (
9536 - strpos($selected_model, 'gpt-5') === 0 ||
9537 - $selected_model === 'gpt-5.2' ||
9538 - $selected_model === 'gpt-5.1-2025-11-13' ||
9539 - $selected_model === 'gpt-5' ||
9540 - $selected_model === 'gpt-5-mini' ||
9541 - $selected_model === 'gpt-5-nano'
9542 - );
9543 -
9544 - // Build request body with optimal settings for fast responses
9545 - $request_body = [
9546 - 'model' => $selected_model,
9547 - 'messages' => $formatted_conversation,
9548 - 'temperature' => 1,
9549 - 'stream' => false
9550 - ];
9551 -
9552 - // Add reasoning_effort only for GPT-5 models that support it
9553 - // These chat models don't support reasoning_effort parameter
9554 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
9555 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
9556 - // GPT-5.1 uses 'low' instead of 'minimal'
9557 - if ($selected_model === 'gpt-5.1-2025-11-13') {
9558 - $request_body['reasoning_effort'] = 'low';
9559 - } elseif ($selected_model === 'gpt-5.5') {
9560 - $request_body['reasoning_effort'] = 'none';
9561 - } elseif ($selected_model === 'gpt-5.4') {
9562 - $request_body['reasoning_effort'] = 'none';
9563 - } else {
9564 - $request_body['reasoning_effort'] = 'minimal';
9565 - }
9566 - }
9567 -
9568 - $body = json_encode($request_body);
9569 -
9570 - $args = [
9571 - 'body' => $body,
9572 - 'headers' => [
9573 - 'Content-Type' => 'application/json',
9574 - 'Authorization' => 'Bearer ' . $api_key,
9575 - ],
9576 - 'timeout' => 60,
9577 - 'redirection' => 5,
9578 - 'blocking' => true,
9579 - 'httpversion' => '1.0',
9580 - 'sslverify' => true,
9581 - ];
9582 -
9583 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
9584 -
9585 - if (is_wp_error($response)) {
9586 - $error_message = $response->get_error_message();
9587 - return [
9588 - 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9589 - 'error_code' => 'openai_connection_error',
9590 - 'provider' => 'openai'
9591 - ];
9592 - }
9593 -
9594 - $status_code = wp_remote_retrieve_response_code($response);
9595 - if ($status_code !== 200) {
9596 - $response_body = wp_remote_retrieve_body($response);
9597 - $decoded_response = json_decode($response_body, true);
9598 -
9599 - $error_message = isset($decoded_response['error']['message'])
9600 - ? $decoded_response['error']['message']
9601 - : 'HTTP Error ' . $status_code;
9602 -
9603 - $error_type = isset($decoded_response['error']['type'])
9604 - ? $decoded_response['error']['type']
9605 - : 'unknown';
9606 -
9607 - // Handle specific error types
9608 - switch ($error_type) {
9609 - case 'invalid_request_error':
9610 - if (strpos($error_message, 'API key') !== false) {
9611 - return [
9612 - 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
9613 - 'error_code' => 'openai_invalid_api_key',
9614 - 'provider' => 'openai'
9615 - ];
9616 - }
9617 - break;
9618 -
9619 - case 'authentication_error':
9620 - return [
9621 - 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
9622 - 'error_code' => 'openai_auth_error',
9623 - 'provider' => 'openai'
9624 - ];
9625 -
9626 - case 'rate_limit_exceeded':
9627 - return [
9628 - 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
9629 - 'error_code' => 'openai_rate_limit',
9630 - 'provider' => 'openai'
9631 - ];
9632 -
9633 - case 'quota_exceeded':
9634 - return [
9635 - 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
9636 - 'error_code' => 'openai_quota_exceeded',
9637 - 'provider' => 'openai'
9638 - ];
9639 - }
9640 -
9641 - // Generic error fallback
9642 - return [
9643 - 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
9644 - 'error_code' => 'openai_api_error',
9645 - 'provider' => 'openai',
9646 - 'status_code' => $status_code
9647 - ];
9648 - }
9649 -
9650 - $response_body = wp_remote_retrieve_body($response);
9651 - $decoded_response = json_decode($response_body, true);
9652 -
9653 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9654 - return trim($decoded_response['choices'][0]['message']['content']);
9655 - } else {
9656 - return [
9657 - 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9658 - 'error_code' => 'openai_response_format_error',
9659 - 'provider' => 'openai'
9660 - ];
9661 - }
9662 - } catch (Exception $e) {
9663 - return [
9664 - 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9665 - 'error_code' => 'openai_exception',
9666 - 'provider' => 'openai'
9667 - ];
9668 - }
9669 -}
9670 -
9671 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9672 - try {
9673 - // Get bot ID from session or request
9674 - $bot_id = $this->get_current_bot_id($session_id);
9675 -
9676 - // Get system prompt instructions using centralized function
9677 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9678 -
9679 - // Add system prompt to relevant content
9680 372 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9681 373
9682 - // Prepend system instructions to the conversation history
9683 374 array_unshift($conversation_history, [
9684 375 'role' => 'system',
9685 376 'content' => "Here are your instructions: " . $content_with_instructions
9686 377 ]);
9687 378
9688 - // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
9689 379 foreach ($conversation_history as &$message) {
9690 380 if ($message['role'] === 'bot') {
9691 381 $message['role'] = 'assistant';
9692 - } elseif ($message['role'] === 'agent') {
9693 - // Tag the message as coming from a live agent
9694 - $message['role'] = 'assistant';
9695 - if (!isset($message['metadata'])) {
9696 - $message['metadata'] = ['source' => 'live_agent'];
9697 - }
9698 382 }
383 + }
9699 384
9700 - // Ensure all roles are valid
9701 - if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
9702 - $message['role'] = 'user'; // Default to 'user'
9703 - }
9704 - }
385 + $api_url = 'https://api.openai.com/v1/chat/completions';
9705 386
9706 - // Build the request body
9707 387 $body = json_encode([
9708 - 'model' => $selected_model,
388 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-3.5',
9709 389 'messages' => $conversation_history,
9710 - 'temperature' => 0.8,
9711 - 'stream' => false
9712 390 ]);
9713 391
9714 - // Set up the API request
9715 392 $args = [
9716 393 'body' => $body,
9717 394 'headers' => [
9718 395 'Content-Type' => 'application/json',
9719 - 'Authorization' => 'Bearer ' . $xai_api_key,
396 + 'Authorization' => 'Bearer ' . $api_key,
9720 397 ],
9721 398 'timeout' => 60,
9722 399 'redirection' => 5,
9723 400 'blocking' => true,
@@ -9724,592 +401,28 @@
9724 401 'httpversion' => '1.0',
9725 402 'sslverify' => true,
9726 403 ];
9727 404
9728 - // Make the API request
9729 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
405 + $response = wp_remote_post($api_url, $args);
9730 406
9731 - // Process the response
9732 407 if (is_wp_error($response)) {
9733 - $error_message = $response->get_error_message();
9734 - //error_log('X.AI API Error: ' . $error_message);
9735 - return [
9736 - 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
9737 - 'error_code' => 'xai_connection_error',
9738 - 'provider' => 'xai'
9739 - ];
408 + return "Sorry, there was an error processing your request.";
9740 409 }
9741 410
9742 - $status_code = wp_remote_retrieve_response_code($response);
9743 - if ($status_code !== 200) {
9744 - $response_body = wp_remote_retrieve_body($response);
9745 - $decoded_response = json_decode($response_body, true);
411 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
9746 412
9747 - // Log the full response for debugging
9748 - //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
9749 -
9750 - // Extract error message from X.AI's specific format
9751 - $error_message = '';
9752 -
9753 - // Check for direct error string (as seen in your logs)
9754 - if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
9755 - $error_message = $decoded_response['error'];
413 + if (isset($response_body['choices'][0]['message']['content'])) {
414 + if (isset($response_body['usage'])) {
415 + $prompt_tokens = $response_body['usage']['prompt_tokens'];
416 + $total_tokens = $response_body['usage']['total_tokens'];
9756 417 }
9757 - // Check for nested error object (OpenAI style)
9758 - elseif (isset($decoded_response['error']['message'])) {
9759 - $error_message = $decoded_response['error']['message'];
9760 - }
9761 - // Check for top-level message
9762 - elseif (isset($decoded_response['message'])) {
9763 - $error_message = $decoded_response['message'];
9764 - }
9765 - // Fallback
9766 - else {
9767 - $error_message = 'HTTP Error ' . $status_code;
9768 - }
9769 -
9770 - //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
9771 -
9772 - // Check for API key errors using string matching
9773 - if (stripos($error_message, 'api key') !== false ||
9774 - stripos($error_message, 'incorrect api key') !== false ||
9775 - stripos($error_message, 'invalid api key') !== false) {
9776 - return [
9777 - 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
9778 - 'error_code' => 'xai_invalid_api_key',
9779 - 'provider' => 'xai'
9780 - ];
9781 - }
9782 -
9783 - // Authentication errors
9784 - if ($status_code === 401 || $status_code === 403 ||
9785 - stripos($error_message, 'auth') !== false) {
9786 - return [
9787 - 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
9788 - 'error_code' => 'xai_auth_error',
9789 - 'provider' => 'xai'
9790 - ];
9791 - }
9792 -
9793 - // Model errors
9794 - if (stripos($error_message, 'model') !== false) {
9795 - return [
9796 - 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
9797 - 'error_code' => 'xai_invalid_model',
9798 - 'provider' => 'xai'
9799 - ];
9800 - }
9801 -
9802 - // Rate limit errors
9803 - if ($status_code === 429 ||
9804 - stripos($error_message, 'rate') !== false ||
9805 - stripos($error_message, 'limit') !== false) {
9806 - return [
9807 - 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
9808 - 'error_code' => 'xai_rate_limit',
9809 - 'provider' => 'xai'
9810 - ];
9811 - }
9812 -
9813 - // Quota errors
9814 - if (stripos($error_message, 'quota') !== false ||
9815 - stripos($error_message, 'billing') !== false) {
9816 - return [
9817 - 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
9818 - 'error_code' => 'xai_quota_exceeded',
9819 - 'provider' => 'xai'
9820 - ];
9821 - }
9822 -
9823 - // Server errors
9824 - if ($status_code >= 500) {
9825 - return [
9826 - 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
9827 - 'error_code' => 'xai_service_unavailable',
9828 - 'provider' => 'xai'
9829 - ];
9830 - }
9831 -
9832 - // Generic error fallback with the actual error message
9833 - return [
9834 - 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
9835 - 'error_code' => 'xai_api_error',
9836 - 'provider' => 'xai',
9837 - 'status_code' => $status_code
9838 - ];
9839 - }
9840 -
9841 - $response_body = wp_remote_retrieve_body($response);
9842 - $decoded_response = json_decode($response_body, true);
9843 -
9844 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9845 - return trim($decoded_response['choices'][0]['message']['content']);
418 + return trim($response_body['choices'][0]['message']['content']);
9846 419 } else {
9847 - //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
9848 - return [
9849 - 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
9850 - 'error_code' => 'xai_response_format_error',
9851 - 'provider' => 'xai'
9852 - ];
420 + return "Sorry, I couldn't process that request.";
9853 421 }
9854 -} catch (Exception $e) {
9855 - //error_log('X.AI Exception: ' . $e->getMessage());
9856 - return [
9857 - 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
9858 - 'error_code' => 'xai_exception',
9859 - 'provider' => 'xai'
9860 - ];
9861 422 }
9862 423
9863 424
9864 -}
9865 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
9866 - try {
9867 - // Ensure conversation_history is an array
9868 - if (!is_array($conversation_history)) {
9869 - $conversation_history = array();
9870 - }
9871 -
9872 - // Get bot ID from session or request
9873 - $bot_id = $this->get_current_bot_id($session_id);
9874 -
9875 - // Get system prompt instructions using centralized function
9876 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9877 -
9878 - // Create a new array for the formatted conversation
9879 - $formatted_conversation = array();
9880 -
9881 - // Add system message first
9882 - $formatted_conversation[] = array(
9883 - 'role' => 'system',
9884 - 'content' => $system_prompt_instructions . " " . $relevant_content
9885 - );
9886 -
9887 - // Add the rest of the conversation history
9888 - foreach ($conversation_history as $message) {
9889 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9890 - $role = $message['role'];
9891 -
9892 - // Convert roles to supported format
9893 - if ($role === 'bot' || $role === 'agent') {
9894 - $role = 'assistant';
9895 - }
9896 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9897 - $role = 'user';
9898 - }
9899 -
9900 - $formatted_conversation[] = array(
9901 - 'role' => $role,
9902 - 'content' => $message['content']
9903 - );
9904 - }
9905 - }
9906 -
9907 - $body = json_encode([
9908 - 'model' => $selected_model,
9909 - 'messages' => $formatted_conversation,
9910 - 'temperature' => 0.8,
9911 - 'stream' => false
9912 - ]);
9913 -
9914 - $args = [
9915 - 'body' => $body,
9916 - 'headers' => [
9917 - 'Content-Type' => 'application/json',
9918 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
9919 - ],
9920 - 'timeout' => 60,
9921 - 'redirection' => 5,
9922 - 'blocking' => true,
9923 - 'httpversion' => '1.0',
9924 - 'sslverify' => true,
9925 - ];
9926 -
9927 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
9928 -
9929 - if (is_wp_error($response)) {
9930 - $error_message = $response->get_error_message();
9931 - //error_log('DeepSeek API Error: ' . $error_message);
9932 - return [
9933 - 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
9934 - 'error_code' => 'deepseek_connection_error',
9935 - 'provider' => 'deepseek'
9936 - ];
9937 - }
9938 -
9939 - $status_code = wp_remote_retrieve_response_code($response);
9940 - if ($status_code !== 200) {
9941 - $response_body = wp_remote_retrieve_body($response);
9942 - $decoded_response = json_decode($response_body, true);
9943 -
9944 - $error_message = isset($decoded_response['error']['message'])
9945 - ? $decoded_response['error']['message']
9946 - : 'HTTP Error ' . $status_code;
9947 -
9948 - $error_type = isset($decoded_response['error']['type'])
9949 - ? $decoded_response['error']['type']
9950 - : 'unknown';
9951 -
9952 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
9953 -
9954 - // Handle specific error types
9955 - switch ($status_code) {
9956 - case 401:
9957 - return [
9958 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
9959 - 'error_code' => 'deepseek_auth_error',
9960 - 'provider' => 'deepseek'
9961 - ];
9962 -
9963 - case 400:
9964 - if (strpos($error_message, 'API key') !== false) {
9965 - return [
9966 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
9967 - 'error_code' => 'deepseek_invalid_api_key',
9968 - 'provider' => 'deepseek'
9969 - ];
9970 - }
9971 - break;
9972 -
9973 - case 429:
9974 - if (strpos($error_message, 'quota') !== false) {
9975 - return [
9976 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
9977 - 'error_code' => 'deepseek_quota_exceeded',
9978 - 'provider' => 'deepseek'
9979 - ];
9980 - } else {
9981 - return [
9982 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
9983 - 'error_code' => 'deepseek_rate_limit',
9984 - 'provider' => 'deepseek'
9985 - ];
9986 - }
9987 -
9988 - case 500:
9989 - case 502:
9990 - case 503:
9991 - case 504:
9992 - return [
9993 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
9994 - 'error_code' => 'deepseek_service_unavailable',
9995 - 'provider' => 'deepseek'
9996 - ];
9997 - }
9998 -
9999 - // Generic error fallback
10000 - return [
10001 - 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
10002 - 'error_code' => 'deepseek_api_error',
10003 - 'provider' => 'deepseek',
10004 - 'status_code' => $status_code
10005 - ];
10006 - }
10007 -
10008 - $response_body = wp_remote_retrieve_body($response);
10009 - $decoded_response = json_decode($response_body, true);
10010 -
10011 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10012 - return trim($decoded_response['choices'][0]['message']['content']);
10013 - } else {
10014 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10015 - return [
10016 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
10017 - 'error_code' => 'deepseek_response_format_error',
10018 - 'provider' => 'deepseek'
10019 - ];
10020 - }
10021 - } catch (Exception $e) {
10022 - //error_log('DeepSeek Exception: ' . $e->getMessage());
10023 - return [
10024 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
10025 - 'error_code' => 'deepseek_exception',
10026 - 'provider' => 'deepseek'
10027 - ];
10028 - }
10029 -}
10030 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
10031 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10032 - // Auto-rescue existing installs whose saved model is the dead ID.
10033 - if ($selected_model === 'gemini-3-pro-preview') {
10034 - $selected_model = 'gemini-3.1-pro-preview';
10035 - }
10036 - // Get bot ID from session or request
10037 - $bot_id = $this->get_current_bot_id($session_id);
10038 -
10039 - // Get system prompt instructions using centralized function
10040 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10041 -
10042 - // Add system prompt to relevant content
10043 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10044 -
10045 - // Format messages for Gemini API
10046 - $formatted_messages = [];
10047 -
10048 - // Add system message as the first user message with role prefix
10049 - // Note: Gemini doesn't have a dedicated system role, so we use a prefixed user message
10050 - $formatted_messages[] = [
10051 - 'role' => 'user',
10052 - 'parts' => [
10053 - ['text' => "[System Instructions] " . $content_with_instructions]
10054 - ]
10055 - ];
10056 -
10057 - // Add model response to acknowledge system instructions
10058 - $formatted_messages[] = [
10059 - 'role' => 'model',
10060 - 'parts' => [
10061 - ['text' => "I understand and will follow these instructions."]
10062 - ]
10063 - ];
10064 -
10065 - // Process the rest of the conversation history
10066 - $current_role = null;
10067 - $current_parts = [];
10068 -
10069 - foreach ($conversation_history as $message) {
10070 - // Skip the first system message as we already handled it
10071 - if ($message['role'] === 'system') {
10072 - continue;
10073 - }
10074 -
10075 - // Map roles to Gemini format
10076 - $gemini_role = '';
10077 - if ($message['role'] === 'user') {
10078 - $gemini_role = 'user';
10079 - } else if (in_array($message['role'], ['assistant', 'bot', 'agent'])) {
10080 - $gemini_role = 'model';
10081 - } else {
10082 - // Skip unsupported roles
10083 - continue;
10084 - }
10085 -
10086 - // If we have a new role, add the previous message
10087 - if ($current_role !== null && $current_role !== $gemini_role && !empty($current_parts)) {
10088 - $formatted_messages[] = [
10089 - 'role' => $current_role,
10090 - 'parts' => $current_parts
10091 - ];
10092 - $current_parts = [];
10093 - }
10094 -
10095 - // Set current role and add text to parts
10096 - $current_role = $gemini_role;
10097 - $current_parts[] = ['text' => $message['content']];
10098 - }
10099 -
10100 - // Add the last message if there's content
10101 - if ($current_role !== null && !empty($current_parts)) {
10102 - $formatted_messages[] = [
10103 - 'role' => $current_role,
10104 - 'parts' => $current_parts
10105 - ];
10106 - }
10107 -
10108 - // Build the request body
10109 - $body = json_encode([
10110 - 'contents' => $formatted_messages,
10111 - 'generationConfig' => [
10112 - 'temperature' => 0.7,
10113 - 'topP' => 0.95,
10114 - 'topK' => 40,
10115 - 'maxOutputTokens' => 8192,
10116 - ],
10117 - 'safetySettings' => [
10118 - [
10119 - 'category' => 'HARM_CATEGORY_HARASSMENT',
10120 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10121 - ],
10122 - [
10123 - 'category' => 'HARM_CATEGORY_HATE_SPEECH',
10124 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10125 - ],
10126 - [
10127 - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
10128 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10129 - ],
10130 - [
10131 - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
10132 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10133 - ]
10134 - ]
10135 - ]);
10136 -
10137 - // Prepare the API endpoint
10138 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10139 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10140 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10141 -
10142 - // Set up the API request
10143 - $args = [
10144 - 'body' => $body,
10145 - 'headers' => [
10146 - 'Content-Type' => 'application/json',
10147 - ],
10148 - 'timeout' => 60,
10149 - 'redirection' => 5,
10150 - 'blocking' => true,
10151 - 'httpversion' => '1.0',
10152 - 'sslverify' => true,
10153 - ];
10154 -
10155 - // Make the API request
10156 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10157 -
10158 - // Process the response
10159 - if (is_wp_error($response)) {
10160 - return "Sorry, there was an error processing your request: " . $response->get_error_message();
10161 - }
10162 -
10163 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
10164 -
10165 - // Handle potential errors in the response
10166 - if (isset($response_body['error'])) {
10167 - //error_log('Gemini API Error: ' . json_encode($response_body['error']));
10168 - return "Sorry, there was an error with the Gemini API: " .
10169 - (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
10170 - }
10171 -
10172 - // Extract the response text
10173 - if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
10174 - return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
10175 - } else {
10176 - //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
10177 - return "Sorry, I couldn't process that request. The response format was unexpected.";
10178 - }
10179 -}
10180 -
10181 -
10182 -public function test_streaming_request() {
10183 - $options = get_option('mxchat_options', []);
10184 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
10185 -
10186 - // Detect provider from model prefix
10187 - $provider = strtolower(explode('-', $model)[0]);
10188 -
10189 - $sample_prompt = 'Hello! Can you stream this response back to me?';
10190 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
10191 - $headers = [];
10192 - $body = [];
10193 - $url = '';
10194 - $api_key = '';
10195 -
10196 - switch ($provider) {
10197 - case 'gpt':
10198 - case 'o1':
10199 - $api_key = $options['api_key'] ?? '';
10200 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
10201 - $url = 'https://api.openai.com/v1/chat/completions';
10202 - $headers = [
10203 - 'Content-Type: application/json',
10204 - 'Authorization: Bearer ' . $api_key
10205 - ];
10206 - $body = [
10207 - 'model' => $model,
10208 - 'messages' => $messages,
10209 - 'stream' => true
10210 - ];
10211 - break;
10212 -
10213 - case 'claude':
10214 - $api_key = $options['claude_api_key'] ?? '';
10215 - if (empty($api_key)) return '❌ Missing API key for Claude';
10216 - $url = 'https://api.anthropic.com/v1/messages';
10217 - $headers = [
10218 - 'Content-Type: application/json',
10219 - 'x-api-key: ' . $api_key,
10220 - 'anthropic-version: 2023-06-01'
10221 - ];
10222 - $body = [
10223 - 'model' => $model,
10224 - 'messages' => $messages,
10225 - 'max_tokens' => 100,
10226 - 'stream' => true
10227 - ];
10228 - break;
10229 -
10230 - case 'grok':
10231 - $api_key = $options['xai_api_key'] ?? '';
10232 - if (empty($api_key)) return '❌ Missing API key for X.AI';
10233 - $url = 'https://api.x.ai/v1/chat/completions';
10234 - $headers = [
10235 - 'Content-Type: application/json',
10236 - 'Authorization: Bearer ' . $api_key
10237 - ];
10238 - $body = [
10239 - 'model' => $model,
10240 - 'messages' => $messages,
10241 - 'stream' => true
10242 - ];
10243 - break;
10244 -
10245 - case 'deepseek':
10246 - if (empty($deepseek_api_key)) {
10247 - $error_response = [
10248 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
10249 - 'error_code' => 'missing_deepseek_api_key'
10250 - ];
10251 - if ($testing_data !== null) {
10252 - $error_response['testing_data'] = $testing_data;
10253 - }
10254 - return $error_response;
10255 - }
10256 - if ($streaming) {
10257 - return $this->mxchat_generate_response_deepseek_stream(
10258 - $selected_model,
10259 - $deepseek_api_key,
10260 - $conversation_history,
10261 - $relevant_content,
10262 - $session_id,
10263 - $testing_data // Pass testing data
10264 - );
10265 - } else {
10266 - $response = $this->mxchat_generate_response_deepseek(
10267 - $selected_model,
10268 - $deepseek_api_key,
10269 - $conversation_history,
10270 - $relevant_content
10271 - );
10272 - }
10273 - break;
10274 -
10275 - case 'gemini':
10276 - $api_key = $options['gemini_api_key'] ?? '';
10277 - if (empty($api_key)) return '❌ Missing API key for Gemini';
10278 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
10279 - $headers = ['Content-Type: application/json'];
10280 - $body = [
10281 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
10282 - 'generationConfig' => ['temperature' => 0.7]
10283 - ];
10284 - break;
10285 -
10286 - default:
10287 - return '❌ Unsupported provider: ' . $provider;
10288 - }
10289 -
10290 - // Do the actual streaming test
10291 - $ch = curl_init($url);
10292 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
10293 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
10294 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10295 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
10296 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
10297 -
10298 - $response = curl_exec($ch);
10299 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
10300 - $error = curl_error($ch);
10301 - curl_close($ch);
10302 -
10303 - if ($error) return "❌ cURL error: $error";
10304 - if ($http_code !== 200) {
10305 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
10306 - return "❌ HTTP $http_code: $error_message";
10307 - }
10308 -
10309 - return true;
10310 -}
10311 -
10312 425 public function mxchat_dismiss_pre_chat_message() {
10313 426 // Get and sanitize the user identifier
10314 427 $user_id = $this->mxchat_get_user_identifier();
10315 428 $user_id = sanitize_key($user_id);
@@ -10320,30 +433,11 @@
10320 433
10321 434 wp_send_json_success();
10322 435 }
10323 436
10324 -public function mxchat_check_pre_chat_message_status() {
10325 - // Get and sanitize the user identifier
10326 - $user_id = $this->mxchat_get_user_identifier();
10327 - $user_id = sanitize_key($user_id);
10328 437
10329 - // Check if the transient exists (i.e., if the message was dismissed)
10330 - $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
10331 - $dismissed = get_transient($transient_key);
10332 438
10333 - // Log the result to see if it's being set correctly
10334 - //error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
10335 -
10336 - if ($dismissed) {
10337 - wp_send_json_success(['dismissed' => true]);
10338 - } else {
10339 - wp_send_json_success(['dismissed' => false]);
10340 - }
10341 -
10342 - wp_die();
10343 -}
10344 -
10345 -private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
439 + private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
10346 440 if (!is_array($vectorA) || !is_array($vectorB) || empty($vectorA) || empty($vectorB)) {
10347 441 return 0;
10348 442 }
10349 443
@@ -10363,1396 +457,111 @@
10363 457
10364 458 return $dotProduct / ($normA * $normB);
10365 459 }
10366 460
461 + public function mxchat_enqueue_scripts_styles() {
462 + // Define version numbers for the styles and scripts
463 + $chat_style_version = '1.0.8'; // Replace with your actual version
464 + $chat_script_version = '1.0.8'; // Replace with your actual version
10367 465
10368 -public function mxchat_enqueue_scripts_styles() {
10369 - // Fetch options from the database first to check loading strategy
10370 - $this->options = get_option('mxchat_options');
10371 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10372 -
10373 - // Always enqueue CSS immediately
10374 - wp_enqueue_style(
10375 - 'mxchat-chat-css',
10376 - plugin_dir_url(__FILE__) . '../css/chat-style.css',
10377 - array(),
10378 - MXCHAT_VERSION
10379 - );
10380 -
10381 - // Handle script loading based on strategy
10382 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10383 - // Enqueue the script normally
466 + // Correct path to the script file
10384 467 wp_enqueue_script(
10385 - 'mxchat-chat-js',
10386 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
10387 - array('jquery'),
10388 - MXCHAT_VERSION,
10389 - true
468 + 'mxchat-chat-js', // Handle for the script
469 + plugin_dir_url(__FILE__) . '../js/chat-script.js', // Correct path using __FILE__
470 + array('jquery'), // Dependencies
471 + $chat_script_version, // Version for cache busting
472 + true // Load script in footer
10390 473 );
10391 474
10392 - // Add defer attribute if strategy is 'defer'
10393 - if ($loading_strategy === 'defer') {
10394 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
10395 - }
10396 - } else {
10397 - // For delay or interaction-based loading, we'll use a custom loader
10398 - // Don't enqueue the main script - we'll load it dynamically
10399 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
10400 - }
475 + // Enqueue the CSS file similarly
476 + wp_enqueue_style(
477 + 'mxchat-chat-css', // Handle for the style
478 + plugin_dir_url(__FILE__) . '../css/chat-style.css', // Correct path using __FILE__
479 + array(), // No dependencies
480 + $chat_style_version // Version for cache busting
481 + );
10401 482
10402 - $prompts_options = get_option('mxchat_prompts_options', array());
483 + // Fetch options from the database
484 + $this->options = get_option('mxchat_options');
10403 485
10404 - // Check if AI theme is active - if so, skip inline colors in JavaScript
10405 - $theme_options = get_option('mxchat_theme_options', array());
10406 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10407 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10408 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
486 + // Prepare settings to pass to JavaScript
487 + $style_settings = array(
488 + 'ajax_url' => admin_url('admin-ajax.php'),
489 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'), // Nonce for security
490 + 'rate_limit_message' => 'Rate limit exceeded. Please try again later.',
491 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off'
492 + );
10409 493
10410 - // Prepare settings for JavaScript
10411 - $style_settings = array(
10412 - 'ajax_url' => admin_url('admin-ajax.php'),
10413 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
10414 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
10415 - // as a one-shot fallback for the first interaction on a fresh page load
10416 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
10417 - // but the widget refetches before each subsequent send.
10418 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10419 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10420 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10421 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10422 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10423 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10424 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10425 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10426 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10427 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10428 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10429 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10430 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10431 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10432 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10433 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10434 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10435 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10436 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10437 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10438 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10439 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10440 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10441 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10442 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
10443 - 'initial_email_state' => null, // Also fixed this undefined variable
10444 - 'skip_email_check' => true,
10445 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10446 - 'skip_inline_colors' => $skip_inline_colors,
10447 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10448 - );
10449 -
10450 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10451 - // print/transcript, satisfaction rating) come from the shared
10452 - // dynamic-settings method so this inline payload and the first-open
10453 - // refresh endpoint can never drift (plan-32db95).
10454 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
10455 -
10456 - // For normal/defer loading, use wp_localize_script
10457 - // For delayed loading, we store settings in a transient to be output inline
10458 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
494 + // Localize the script with necessary data
10459 495 wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10460 - } else {
10461 - // Store settings for the delayed loader to use
10462 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
10463 496 }
10464 -}
10465 497
10466 -/**
10467 - * Output the delayed script loader for performance optimization
10468 - */
10469 -public function mxchat_output_delayed_script_loader() {
10470 - $this->options = get_option('mxchat_options');
10471 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10472 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
10473 498
10474 - // Get the stored settings
10475 - $prompts_options = get_option('mxchat_prompts_options', array());
10476 - $theme_options = get_option('mxchat_theme_options', array());
10477 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10478 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10479 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10480 499
10481 - $style_settings = array(
10482 - 'ajax_url' => admin_url('admin-ajax.php'),
10483 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
10484 - // before each send. This inline value is a one-shot fallback for the first interaction.
10485 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10486 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10487 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10488 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10489 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10490 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10491 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10492 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10493 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10494 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10495 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10496 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10497 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10498 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10499 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10500 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10501 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10502 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10503 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10504 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10505 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10506 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10507 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10508 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10509 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
10510 - 'initial_email_state' => null,
10511 - 'skip_email_check' => true,
10512 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10513 - 'skip_inline_colors' => $skip_inline_colors,
10514 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10515 - );
500 + public function mxchat_reset_rate_limits() {
501 + global $wpdb;
10516 502
10517 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10518 - // print/transcript, satisfaction rating) come from the shared
10519 - // dynamic-settings method so this inline payload and the first-open
10520 - // refresh endpoint can never drift (plan-32db95).
10521 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
503 + // Define a cache key pattern for rate limits
504 + $cache_key_pattern = 'mxchat_chat_limit_%';
10522 505
10523 - // Determine delay time based on strategy
10524 - $delay_ms = 0;
10525 - switch ($loading_strategy) {
10526 - case 'delay_1s':
10527 - $delay_ms = 1000;
10528 - break;
10529 - case 'delay_3s':
10530 - $delay_ms = 3000;
10531 - break;
10532 - case 'delay_5s':
10533 - $delay_ms = 5000;
10534 - break;
10535 - }
506 + // Retrieve all option names matching the pattern
507 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
508 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
10536 509
10537 - ?>
10538 - <script type="text/javascript">
10539 - (function() {
10540 - var mxchatLoaded = false;
10541 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
10542 - window.mxchatChat = mxchatChat;
510 + // db call ok; no-cache ok
511 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- db call ok
512 + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
10543 513
10544 - function loadMxChatScript() {
10545 - if (mxchatLoaded) return;
10546 - mxchatLoaded = true;
10547 -
10548 - function appendChatScript() {
10549 - var script = document.createElement('script');
10550 - script.src = <?php echo wp_json_encode($script_url); ?>;
10551 - script.type = 'text/javascript';
10552 - document.body.appendChild(script);
10553 - }
10554 -
10555 - if (typeof jQuery !== 'undefined') {
10556 - appendChatScript();
10557 - } else {
10558 - var jq = document.createElement('script');
10559 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
10560 - jq.onload = appendChatScript;
10561 - document.body.appendChild(jq);
10562 - }
514 + // Clear the relevant cache entries
515 + foreach ($option_names as $option_name) {
516 + wp_cache_delete($option_name, 'options');
10563 517 }
10564 518
10565 - <?php if ($loading_strategy === 'on_interaction'): ?>
10566 - // Load on user interaction
10567 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
10568 - events.forEach(function(evt) {
10569 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
10570 - });
10571 - // Fallback: load after 8 seconds if no interaction
10572 - setTimeout(loadMxChatScript, 8000);
10573 - <?php else: ?>
10574 - // Load after specified delay
10575 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
10576 - <?php endif; ?>
10577 - })();
10578 - </script>
10579 - <?php
10580 -}
10581 -
10582 -/**
10583 - * Setup the cron jobs for rate limits with guard against multiple calls
10584 - */
10585 -public function setup_rate_limit_cron_jobs() {
10586 - // Add a guard to prevent multiple rapid calls
10587 - $last_setup = get_transient('mxchat_cron_setup_guard');
10588 - if ($last_setup && (time() - $last_setup) < 60) {
10589 - // Don't run again if we ran less than 60 seconds ago
10590 - return;
10591 - }
10592 -
10593 - // Set the guard
10594 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
10595 -
10596 - try {
10597 - // First, check if WordPress cron is disabled
10598 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
10599 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
10600 - $this->setup_fallback_rate_limit_system();
10601 - return;
10602 - }
10603 -
10604 - // Check if cron is already scheduled - if so, don't mess with it
10605 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
10606 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
10607 - return;
10608 - }
10609 -
10610 - // Clear any orphaned hooks (but don't loop indefinitely)
10611 - $hooks_to_clear = [
10612 - 'mxchat_reset_rate_limits',
10613 - 'mxchat_reset_hourly_rate_limits',
10614 - 'mxchat_reset_daily_rate_limits',
10615 - 'mxchat_reset_weekly_rate_limits',
10616 - 'mxchat_reset_monthly_rate_limits'
10617 - ];
10618 -
10619 - foreach ($hooks_to_clear as $hook) {
10620 - // Only clear a maximum of 3 instances to prevent infinite loops
10621 - $cleared = 0;
10622 - while (wp_next_scheduled($hook) && $cleared < 3) {
10623 - wp_clear_scheduled_hook($hook);
10624 - $cleared++;
10625 - }
10626 - }
10627 -
10628 - // Small delay after clearing
10629 - usleep(100000); // 0.1 seconds
10630 -
10631 - // Try to schedule the event
10632 - $initial_time = time() + 300; // Start in 5 minutes
10633 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
10634 -
10635 - if ($result === false) {
10636 - //error_log('MxChat: Failed to schedule cron, using fallback system');
10637 - $this->setup_fallback_rate_limit_system();
10638 - } else {
10639 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
10640 - }
10641 -
10642 - } catch (Exception $e) {
10643 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
10644 - $this->setup_fallback_rate_limit_system();
10645 - }
10646 -}
10647 -
10648 -/**
10649 - * Try alternative cron scheduling methods
10650 - */
10651 -private function try_alternative_cron_scheduling($initial_time) {
10652 - try {
10653 - // Method 1: Try with current time instead of future time
10654 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
10655 - if ($result1 !== false) {
10656 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
10657 - return true;
10658 - }
10659 -
10660 - // Method 2: Try with a different interval
10661 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
10662 - if ($result2 !== false) {
10663 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
10664 - return true;
10665 - }
10666 -
10667 - // Method 3: Try wp_schedule_single_event first, then recurring
10668 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
10669 - if ($result3 !== false) {
10670 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
10671 - // Schedule the next one manually in the handler
10672 - return true;
10673 - }
10674 -
10675 - return false;
10676 -
10677 - } catch (Exception $e) {
10678 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
10679 - return false;
10680 - }
10681 -}
10682 -
10683 -/**
10684 - * Enhanced fallback rate limit system
10685 - */
10686 -private function setup_fallback_rate_limit_system() {
10687 - // Set a flag to use database-based rate limit cleanup
10688 - update_option('mxchat_use_fallback_rate_limits', true);
10689 -
10690 - // Schedule a one-time check to happen on the next plugin load
10691 - update_option('mxchat_next_rate_limit_check', time() + 3600);
10692 -
10693 - // Also set up a more frequent fallback check (every 4 hours)
10694 - update_option('mxchat_fallback_check_interval', 4 * 3600);
10695 -
10696 - //error_log('MxChat: Fallback rate limit system activated');
10697 -}
10698 -
10699 -/**
10700 - * Enhanced fallback check method
10701 - */
10702 -public function check_fallback_rate_limits() {
10703 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10704 -
10705 - if (!$use_fallback) {
10706 - return; // Regular cron is working
10707 - }
10708 -
10709 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10710 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
10711 -
10712 - if (time() >= $next_check) {
10713 - //error_log('MxChat: Running fallback rate limit cleanup');
10714 - $this->mxchat_reset_rate_limits();
10715 -
10716 - // Schedule next check
10717 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
10718 - }
10719 -}
10720 -/**
10721 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
10722 - */
10723 -public function check_rate_limit() {
10724 - // Check if we need to run fallback cleanup
10725 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10726 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10727 -
10728 - if ($use_fallback && time() >= $next_check) {
10729 - $this->mxchat_reset_rate_limits();
10730 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
10731 - }
10732 -
10733 - // Get bot ID from current request context
10734 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10735 -
10736 - // Get bot-specific options (includes rate limits if overridden)
10737 - $bot_options = $this->get_bot_options($bot_id);
10738 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
10739 -
10740 - // Use bot-specific rate limits if available, otherwise fall back to default
10741 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
10742 -
10743 - // -------------------------------------------------------------------
10744 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
10745 - // it acts as a hard ceiling across all users + all roles. Default is
10746 - // 'unlimited' so existing installs are unchanged. Counter key drops
10747 - // both <role> and <user_id> segments — single pool per bot.
10748 - // -------------------------------------------------------------------
10749 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
10750 - ? $current_options['rate_limits_global']
10751 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
10752 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
10753 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
10754 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
10755 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10756 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
10757 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
10758 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
10759 - if ((int) $global_data['count'] === 0) {
10760 - $global_data['timestamp'] = time();
10761 - update_option($global_option, $global_data);
10762 - }
10763 - $now = time();
10764 - $ts = (int) $global_data['timestamp'];
10765 - $reset = false;
10766 - switch ($global_timeframe) {
10767 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
10768 - case 'daily': $reset = ($now - $ts) >= 86400; break;
10769 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
10770 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
10771 - }
10772 - if ($reset) {
10773 - $global_data = ['count' => 0, 'timestamp' => $now];
10774 - update_option($global_option, $global_data);
10775 - }
10776 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
10777 - $global_msg = !empty($global_cfg['message'])
10778 - ? $global_cfg['message']
10779 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
10780 - return [
10781 - 'error' => true,
10782 - 'message' => $this->process_rate_limit_message_html($global_msg),
10783 - ];
10784 - }
10785 - // Reserve the slot for this request. Per-role check below also increments
10786 - // its own counter — that is intentional, both ceilings apply independently.
10787 - $global_data['count']++;
10788 - update_option($global_option, $global_data);
10789 - }
10790 -
10791 - // Determine user role or if logged out
10792 - if (is_user_logged_in()) {
10793 - $user = wp_get_current_user();
10794 - $user_id = $user->ID;
10795 -
10796 - // Get the user's primary role using reset() to safely get the first element
10797 - $user_roles = $user->roles;
10798 -
10799 - // Safely get the first role regardless of array key structure
10800 - if (!empty($user_roles) && is_array($user_roles)) {
10801 - $role = reset($user_roles); // This safely gets the first element regardless of key
10802 - } else {
10803 - $role = 'subscriber'; // Default to subscriber if no role found
10804 - }
10805 - } else {
10806 - $role = 'logged_out';
10807 - // Use IP address for non-logged-in users
10808 - $user_id = $this->get_client_ip();
10809 - }
10810 -
10811 - // Check if rate limits are configured for this role
10812 - if (!isset($rate_limits_source[$role])) {
10813 - return true; // No limit set for this role
10814 - }
10815 -
10816 - $limit = $rate_limits_source[$role]['limit'];
10817 -
10818 - // If unlimited, return true immediately
10819 - if ($limit === 'unlimited') {
10820 - return true;
10821 - }
10822 -
10823 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
10824 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
10825 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
10826 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
10827 -
10828 - // Include bot_id in option name so each bot has separate rate limits
10829 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
10830 -
10831 - // Get the counter data
10832 - $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
10833 -
10834 - // If first request or counter reset needed, set the initial timestamp
10835 - if ($limit_data['count'] === 0) {
10836 - $limit_data['timestamp'] = time();
10837 - update_option($option_name, $limit_data);
10838 - }
10839 -
10840 - // Get the timeframe
10841 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
10842 - $rate_limits_source[$role]['timeframe'] : 'daily';
10843 -
10844 - // Check if the counter needs to be reset based on timeframe
10845 - $current_time = time();
10846 - $timestamp = $limit_data['timestamp'];
10847 - $should_reset = false;
10848 -
10849 - switch ($timeframe) {
10850 - case 'hourly':
10851 - $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
10852 - break;
10853 - case 'daily':
10854 - $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
10855 - break;
10856 - case 'weekly':
10857 - $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
10858 - break;
10859 - case 'monthly':
10860 - $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
10861 - break;
10862 - }
10863 -
10864 - // Reset the counter if the timeframe has passed
10865 - if ($should_reset) {
10866 - $limit_data = ['count' => 0, 'timestamp' => $current_time];
10867 - update_option($option_name, $limit_data);
10868 - }
10869 -
10870 - // Check if user has exceeded their limit
10871 - if ($limit_data['count'] >= intval($limit)) {
10872 - // Get the custom message for this role
10873 - $message = !empty($rate_limits_source[$role]['message'])
10874 - ? $rate_limits_source[$role]['message']
10875 - : __('Rate limit exceeded. Please try again later.', 'mxchat');
10876 -
10877 - // Add timeframe information to the message if placeholders exist
10878 - $timeframe_label = '';
10879 - switch ($timeframe) {
10880 - case 'hourly':
10881 - $timeframe_label = __('hour', 'mxchat');
10882 - break;
10883 - case 'daily':
10884 - $timeframe_label = __('day', 'mxchat');
10885 - break;
10886 - case 'weekly':
10887 - $timeframe_label = __('week', 'mxchat');
10888 - break;
10889 - case 'monthly':
10890 - $timeframe_label = __('month', 'mxchat');
10891 - break;
10892 - }
10893 -
10894 - // Replace placeholders in the message
10895 - $message = str_replace(
10896 - ['{limit}', '{count}', '{remaining}', '{timeframe}'],
10897 - [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
10898 - $message
10899 - );
10900 -
10901 - // Process HTML links in the message
10902 - $message = $this->process_rate_limit_message_html($message);
10903 -
10904 - // Return error with the processed message
10905 - return [
10906 - 'error' => true,
10907 - 'message' => $message
10908 - ];
10909 - }
10910 -
10911 - // Increment the counter
10912 - $limit_data['count']++;
10913 - update_option($option_name, $limit_data);
10914 -
10915 - return true;
10916 -}
10917 -
10918 -/**
10919 - * Enhanced rate limit reset with better error handling
10920 - */
10921 -public function mxchat_reset_rate_limits() {
10922 - try {
10923 - global $wpdb;
10924 - $all_options = get_option('mxchat_options', []);
10925 - $current_time = time();
10926 -
10927 - // Get rate limit options with a safer query and limit
10928 - $option_names = $wpdb->get_col(
10929 - $wpdb->prepare(
10930 - "SELECT option_name FROM {$wpdb->options}
10931 - WHERE option_name LIKE %s
10932 - LIMIT 1000",
10933 - 'mxchat_chat_limit_%'
10934 - )
10935 - );
10936 -
10937 - if (empty($option_names)) {
10938 - return;
10939 - }
10940 -
10941 - $processed_count = 0;
10942 - $max_processing_time = 30; // Maximum 30 seconds
10943 - $start_time = time();
10944 -
10945 - foreach ($option_names as $option_name) {
10946 - // Check processing time limit
10947 - if ((time() - $start_time) > $max_processing_time) {
10948 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
10949 - break;
10950 - }
10951 -
10952 - // Parse the option name more safely
10953 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
10954 - continue;
10955 - }
10956 -
10957 - $role_and_user = $matches[1] . '_' . $matches[2];
10958 - $parts = explode('_', $role_and_user);
10959 -
10960 - if (count($parts) < 2) {
10961 - continue;
10962 - }
10963 -
10964 - // Extract role (everything except the last part which is user ID)
10965 - $user_id_part = array_pop($parts);
10966 - $role = implode('_', $parts);
10967 -
10968 - // Skip if role doesn't exist in our settings
10969 - if (!isset($all_options['rate_limits'][$role])) {
10970 - // Clean up orphaned entries
10971 - delete_option($option_name);
10972 - continue;
10973 - }
10974 -
10975 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
10976 - $limit_data = get_option($option_name);
10977 -
10978 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
10979 - // Clean up invalid entries
10980 - delete_option($option_name);
10981 - continue;
10982 - }
10983 -
10984 - $timestamp = $limit_data['timestamp'];
10985 - $should_reset = false;
10986 -
10987 - // Determine if we should reset based on the timeframe
10988 - switch ($timeframe) {
10989 - case 'hourly':
10990 - $should_reset = ($current_time - $timestamp) >= 3600;
10991 - break;
10992 - case 'daily':
10993 - $should_reset = ($current_time - $timestamp) >= 86400;
10994 - break;
10995 - case 'weekly':
10996 - $should_reset = ($current_time - $timestamp) >= 604800;
10997 - break;
10998 - case 'monthly':
10999 - $should_reset = ($current_time - $timestamp) >= 2592000;
11000 - break;
11001 - }
11002 -
11003 - // Reset the counter if the timeframe has passed
11004 - if ($should_reset) {
11005 - delete_option($option_name);
11006 - wp_cache_delete($option_name, 'options');
11007 - $processed_count++;
11008 - }
11009 - }
11010 -
11011 - // Clean up any orphaned cache entries
519 + // Optionally, clear a general cache if you have one
11012 520 wp_cache_delete('mxchat_all_chat_limits', 'options');
11013 -
11014 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11015 -
11016 - } catch (Exception $e) {
11017 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11018 521 }
11019 -}
11020 522
11021 523
11022 -/**
11023 - * Process HTML links in rate limit messages
11024 - *
11025 - * @param string $message The rate limit message
11026 - * @return string The processed message with safe HTML links
11027 - */
11028 -private function process_rate_limit_message_html($message) {
11029 - // Return original message if empty
11030 - if (empty($message)) {
11031 - return $message;
524 +private function mxchat_fetch_woocommerce_products() {
525 + // Ensure WooCommerce is active
526 + if (!class_exists('WooCommerce')) {
527 + return [];
11032 528 }
11033 -
11034 - // First, convert markdown links to HTML
11035 - $message = $this->convert_markdown_links($message);
11036 -
11037 - // Then, auto-convert any remaining plain URLs to links
11038 - $message = $this->auto_link_urls($message);
11039 -
11040 - // Allow basic HTML tags for links and formatting
11041 - $allowed_tags = [
11042 - 'a' => [
11043 - 'href' => true,
11044 - 'target' => true,
11045 - 'rel' => true,
11046 - 'title' => true,
11047 - 'class' => true
11048 - ],
11049 - 'strong' => [],
11050 - 'em' => [],
11051 - 'br' => [],
11052 - 'b' => [],
11053 - 'i' => [],
11054 - 'span' => ['class' => true]
11055 - ];
11056 -
11057 - // Sanitize but allow the specified HTML tags
11058 - $processed_message = wp_kses($message, $allowed_tags);
11059 -
11060 - // If wp_kses stripped everything, return the original message as plain text
11061 - if (empty($processed_message) && !empty($message)) {
11062 - // Strip all HTML and return plain text as fallback
11063 - return wp_strip_all_tags($message);
11064 - }
11065 -
11066 - return $processed_message;
11067 -}
11068 529
11069 -/**
11070 - * Convert markdown links to HTML
11071 - *
11072 - * @param string $text The text to process
11073 - * @return string The text with markdown links converted to HTML
11074 - */
11075 -private function convert_markdown_links($text) {
11076 - // Return original text if empty
11077 - if (empty($text)) {
11078 - return $text;
11079 - }
11080 -
11081 - // Pattern to match markdown links: [text](url)
11082 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
11083 -
11084 - $processed_text = preg_replace_callback($pattern, function($matches) {
11085 - $link_text = $matches[1];
11086 - $url = $matches[2];
11087 -
11088 - // Clean up any trailing punctuation from the URL
11089 - $url = rtrim($url, '.,;:!?');
11090 -
11091 - // Sanitize the link text and URL
11092 - $safe_text = esc_html($link_text);
11093 - $safe_url = esc_url($url);
11094 -
11095 - // Create the HTML link
11096 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
11097 - }, $text);
11098 -
11099 - // If preg_replace_callback failed, return original text
11100 - if ($processed_text === null) {
11101 - return $text;
11102 - }
11103 -
11104 - return $processed_text;
11105 -}
530 + $args = array(
531 + 'post_type' => 'product',
532 + 'post_status' => 'publish',
533 + 'posts_per_page' => -1,
534 + );
11106 535
11107 -/**
11108 - * Auto-convert plain URLs to clickable links
11109 - *
11110 - * @param string $text The text to process
11111 - * @return string The text with URLs converted to links
11112 - */
11113 -private function auto_link_urls($text) {
11114 - // Return original text if empty
11115 - if (empty($text)) {
11116 - return $text;
11117 - }
11118 -
11119 - // Simple pattern that avoids complex lookbehinds
11120 - // This will match URLs that are not already inside href attributes or markdown links
11121 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
11122 -
11123 - $processed_text = preg_replace_callback($pattern, function($matches) {
11124 - $url = $matches[0];
11125 - // Clean up any trailing punctuation that might have been captured
11126 - $url = rtrim($url, '.,;:!?');
11127 -
11128 - // Add target="_blank" and rel="noopener noreferrer" for security
11129 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
11130 - }, $text);
11131 -
11132 - // If preg_replace_callback failed, return original text
11133 - if ($processed_text === null) {
11134 - return $text;
11135 - }
11136 -
11137 - return $processed_text;
11138 -}
536 + $products = get_posts($args);
537 + $product_data = [];
11139 538
539 + foreach ($products as $product) {
540 + $product_id = $product->ID;
541 + $product_obj = wc_get_product($product_id);
11140 542
11141 -// Helper function to get client IP address
11142 -private function get_client_ip() {
11143 - // Check for shared internet/ISP IP
11144 - if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
11145 - return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
543 + $product_data[] = array(
544 + 'id' => $product_id,
545 + 'name' => $product_obj->get_name(),
546 + 'description' => $product_obj->get_description(),
547 + 'short_description' => $product_obj->get_short_description(),
548 + 'url' => get_permalink($product_id),
549 + 'price' => $product_obj->get_regular_price(),
550 + 'sale_price' => $product_obj->get_sale_price(),
551 + 'stock_status' => $product_obj->get_stock_status(),
552 + 'sku' => $product_obj->get_sku(),
553 + 'in_stock' => $product_obj->is_in_stock(),
554 + 'total_sales' => $product_obj->get_total_sales(),
555 + );
11146 556 }
11147 -
11148 - // Check for IPs passing through proxies
11149 - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
11150 - // Use the first value in the comma-separated list
11151 - $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
11152 - return trim($forwarded_for[0]);
11153 - }
11154 -
11155 - if (!empty($_SERVER['REMOTE_ADDR'])) {
11156 - return sanitize_text_field($_SERVER['REMOTE_ADDR']);
11157 - }
11158 -
11159 - // Fallback
11160 - return 'unknown';
11161 -}
11162 557
11163 -/**
11164 - * AJAX handler to get system information for testing panel
11165 - */
11166 -/**
11167 - * AJAX handler to get system information for testing panel
11168 - */
11169 -public function mxchat_get_system_info() {
11170 - // Verify nonce for security
11171 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11172 - wp_send_json_error(['message' => 'Invalid nonce']);
11173 - return;
11174 - }
11175 -
11176 - // Only allow admin users
11177 - if (!current_user_can('administrator')) {
11178 - wp_send_json_error(['message' => 'Unauthorized']);
11179 - return;
11180 - }
11181 -
11182 - // Get system prompt from options
11183 - $system_prompt = isset($this->options['system_prompt_instructions'])
11184 - ? $this->options['system_prompt_instructions']
11185 - : 'No system prompt configured';
11186 -
11187 - // Get selected model
11188 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
11189 -
11190 - // Check if OpenRouter is being used
11191 - $is_openrouter = ($selected_model === 'openrouter');
11192 - $openrouter_model = '';
11193 -
11194 - if ($is_openrouter) {
11195 - // Get the actual OpenRouter model that's selected
11196 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11197 - ? $this->options['openrouter_selected_model']
11198 - : 'No OpenRouter model selected';
11199 -
11200 - // Update selected_model display to show both
11201 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11202 - }
11203 -
11204 - // Get API key status (just check if they exist, don't expose the keys)
11205 - $api_status = [];
11206 - $api_status['openai'] = !empty($this->options['api_key']);
11207 - $api_status['claude'] = !empty($this->options['claude_api_key']);
11208 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11209 - $api_status['xai'] = !empty($this->options['xai_api_key']);
11210 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11211 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11212 -
11213 - wp_send_json_success([
11214 - 'system_prompt' => $system_prompt,
11215 - 'selected_model' => $selected_model,
11216 - 'is_openrouter' => $is_openrouter,
11217 - 'openrouter_model' => $openrouter_model,
11218 - 'api_status' => $api_status
11219 - ]);
558 + return $product_data;
11220 559 }
11221 560
11222 -/**
11223 - * AJAX handler to get similarity threshold
11224 - */
11225 -public function mxchat_get_similarity_threshold() {
11226 - // Verify nonce for security
11227 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11228 - wp_send_json_error(['message' => 'Invalid nonce']);
11229 - return;
11230 - }
11231 -
11232 - // Only allow admin users
11233 - if (!current_user_can('administrator')) {
11234 - wp_send_json_error(['message' => 'Unauthorized']);
11235 - return;
11236 - }
11237 -
11238 - // Get similarity threshold from main options (default 35%)
11239 - $similarity_threshold = isset($this->options['similarity_threshold'])
11240 - ? ((int) $this->options['similarity_threshold']) / 100
11241 - : 0.35;
11242 -
11243 - wp_send_json_success([
11244 - 'threshold' => $similarity_threshold,
11245 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
11246 - ]);
11247 -}
11248 561
11249 -/**
11250 - * AJAX handler to get knowledge base status
11251 - */
11252 -public function mxchat_get_kb_status() {
11253 - // Verify nonce for security
11254 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11255 - wp_send_json_error(['message' => 'Invalid nonce']);
11256 - return;
11257 - }
11258 562
11259 - // Only allow admin users
11260 - if (!current_user_can('administrator')) {
11261 - wp_send_json_error(['message' => 'Unauthorized']);
11262 - return;
11263 - }
11264 563
11265 - // Check OpenAI Vector Store first (takes priority)
11266 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11267 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11268 -
11269 - if ($use_vectorstore) {
11270 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11271 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11272 -
11273 - $kb_info = [
11274 - 'type' => 'OpenAI Vector Store',
11275 - 'status' => 'Active',
11276 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11277 - ];
11278 -
11279 - wp_send_json_success($kb_info);
11280 - return;
11281 - }
11282 -
11283 - // Check Pinecone vs WordPress
11284 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
11285 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11286 -
11287 - $kb_info = [
11288 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11289 - 'status' => 'Active'
11290 - ];
11291 -
11292 - // Get document count
11293 - if ($use_pinecone) {
11294 - $kb_info['documents'] = 'Connected to Pinecone';
11295 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
11296 - } else {
11297 - // Count documents in WordPress database
11298 - global $wpdb;
11299 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11300 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11301 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11302 - }
11303 -
11304 - wp_send_json_success($kb_info);
11305 -}
11306 -
11307 -/**
11308 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
11309 - */
11310 -public function mxchat_start_fresh_session() {
11311 - // Verify nonce for security
11312 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11313 - wp_send_json_error(['message' => 'Invalid nonce']);
11314 - return;
11315 - }
11316 -
11317 - // Only allow admin users
11318 - if (!current_user_can('administrator')) {
11319 - wp_send_json_error(['message' => 'Unauthorized']);
11320 - return;
11321 - }
11322 -
11323 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
11324 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
11325 -
11326 - if (empty($old_session_id)) {
11327 - wp_send_json_error(['message' => 'Old session ID required']);
11328 - return;
11329 - }
11330 -
11331 - // If no new session ID provided, generate one
11332 - if (empty($new_session_id)) {
11333 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
11334 - }
11335 -
11336 - // Clear ALL data associated with the old session
11337 - $this->clear_complete_session_data($old_session_id);
11338 -
11339 - // Initialize the new session
11340 - $this->initialize_fresh_session($new_session_id);
11341 -
11342 - wp_send_json_success([
11343 - 'message' => 'Fresh session started successfully',
11344 - 'new_session_id' => $new_session_id,
11345 - 'old_session_id' => $old_session_id
11346 - ]);
11347 -}
11348 -
11349 -/**
11350 - * Clear ALL data associated with a session (ENHANCED)
11351 - */
11352 -private function clear_complete_session_data($session_id) {
11353 - // Clear chat history
11354 - delete_option("mxchat_history_{$session_id}");
11355 -
11356 - // Clear chat mode
11357 - delete_option("mxchat_mode_{$session_id}");
11358 -
11359 - // Clear any PDF/Word transients
11360 - $this->clear_pdf_transients($session_id);
11361 - if (method_exists($this, 'clear_word_transients')) {
11362 - $this->clear_word_transients($session_id);
11363 - }
11364 -
11365 - // Clear agent-related data
11366 - delete_option("mxchat_channel_{$session_id}");
11367 - delete_option("mxchat_agent_name_{$session_id}");
11368 - delete_option("mxchat_email_{$session_id}");
11369 -
11370 - // Clear any recommendation flow state
11371 - delete_option("mxchat_sr_flow_state_{$session_id}");
11372 -
11373 - // Clear any cached embeddings or context
11374 - delete_transient("mxchat_context_{$session_id}");
11375 - delete_transient("mxchat_last_query_{$session_id}");
11376 -
11377 - // Clear any testing data
11378 - delete_transient("mxchat_testing_data_{$session_id}");
11379 -
11380 - // Clear any rate limiting data for this session
11381 - delete_transient("mxchat_rate_limit_{$session_id}");
11382 -
11383 - // Clear any other session-specific transients
11384 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
11385 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
11386 - delete_transient("mxchat_include_word_in_context_{$session_id}");
11387 -
11388 - // Clear form addon state (pending forms and submitted forms)
11389 - delete_option("mxchat_pending_form_{$session_id}");
11390 - delete_option("mxchat_submitted_forms_{$session_id}");
11391 -
11392 - //error_log("MxChat: Cleared all data for session: {$session_id}");
11393 -}
11394 -
11395 -/**
11396 - * Initialize a fresh session with default data
11397 - */
11398 -private function initialize_fresh_session($session_id) {
11399 - // Set default chat mode
11400 - update_option("mxchat_mode_{$session_id}", 'ai');
11401 -
11402 - //error_log("MxChat: Initialized fresh session: {$session_id}");
11403 -}
11404 -
11405 -/**
11406 - * Helper method to clear Word document transients (if you have Word support)
11407 - */
11408 -private function clear_word_transients($session_id) {
11409 - delete_transient('mxchat_word_url_' . $session_id);
11410 - delete_transient('mxchat_word_filename_' . $session_id);
11411 - delete_transient('mxchat_word_embeddings_' . $session_id);
11412 - delete_transient('mxchat_include_word_in_context_' . $session_id);
11413 -}
11414 -
11415 -/**
11416 - * Simplified testing data capture method (CLEANED UP)
11417 - */
11418 -private function capture_testing_data($user_embedding, $message, $session_id) {
11419 - // Only capture for admin users
11420 - if (!current_user_can('administrator')) {
11421 - return null;
11422 - }
11423 -
11424 - $testing_data = [
11425 - 'query' => $message,
11426 - 'timestamp' => time(),
11427 - 'top_matches' => [],
11428 - 'action_matches' => [] // Add action matches
11429 - ];
11430 -
11431 - // Get similarity threshold
11432 - $similarity_threshold = isset($this->options['similarity_threshold'])
11433 - ? ((int) $this->options['similarity_threshold']) / 100
11434 - : 0.35;
11435 -
11436 - $testing_data['similarity_threshold'] = $similarity_threshold;
11437 -
11438 - // Use the real similarity analysis if available
11439 - if ($this->last_similarity_analysis !== null) {
11440 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
11441 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
11442 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
11443 - } else {
11444 - // Fallback: determine knowledge base type
11445 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
11446 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11447 -
11448 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
11449 - }
11450 -
11451 - // Include action analysis if available
11452 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
11453 - $testing_data['action_matches'] = $this->last_action_analysis;
11454 -
11455 - // Clear it after capturing to avoid stale data
11456 - $this->last_action_analysis = null;
11457 - }
11458 -
11459 - return $testing_data;
11460 -}
11461 -
11462 -
11463 -/**
11464 - * Track URL clicks from chatbot responses
11465 - */
11466 -public function mxchat_track_url_click() {
11467 - // Verify nonce for security
11468 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11469 - wp_send_json_error(['message' => 'Invalid nonce']);
11470 - wp_die();
11471 - }
11472 -
11473 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11474 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
11475 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
11476 -
11477 - if (empty($session_id) || empty($clicked_url)) {
11478 - wp_send_json_error(['message' => 'Missing required data']);
11479 - wp_die();
11480 - }
11481 -
11482 - global $wpdb;
11483 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11484 -
11485 - // Insert click tracking record
11486 - $wpdb->insert(
11487 - $table_name,
11488 - [
11489 - 'session_id' => $session_id,
11490 - 'clicked_url' => $clicked_url,
11491 - 'message_context' => $message_context,
11492 - 'click_timestamp' => current_time('mysql', 1),
11493 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
11494 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
11495 - ]
11496 - );
11497 -
11498 - wp_send_json_success(['message' => 'Click tracked']);
11499 - wp_die();
11500 -}
11501 -
11502 -/**
11503 - * Get URL click analytics for a session
11504 - */
11505 -public function mxchat_get_url_clicks($session_id) {
11506 - global $wpdb;
11507 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11508 -
11509 - $clicks = $wpdb->get_results($wpdb->prepare(
11510 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
11511 - $session_id
11512 - ));
11513 -
11514 - return $clicks;
11515 -}
11516 -/**
11517 - * Track the originating page where chat was started
11518 - */
11519 -public function mxchat_track_originating_page() {
11520 - // Verify nonce
11521 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11522 - wp_send_json_error(['message' => 'Invalid nonce']);
11523 - wp_die();
11524 - }
11525 -
11526 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11527 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
11528 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
11529 -
11530 - if (empty($session_id)) {
11531 - wp_send_json_error(['message' => 'Missing session ID']);
11532 - wp_die();
11533 - }
11534 -
11535 - global $wpdb;
11536 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
11537 -
11538 - // Check if we've already tracked for this session
11539 - $existing = $wpdb->get_var($wpdb->prepare(
11540 - "SELECT COUNT(*) FROM $table_name
11541 - WHERE session_id = %s
11542 - AND originating_page_url IS NOT NULL",
11543 - $session_id
11544 - ));
11545 -
11546 - if ($existing > 0) {
11547 - wp_send_json_success(['message' => 'Already tracked']);
11548 - wp_die();
11549 - }
11550 -
11551 - // Update the first message in this session with originating page info
11552 - $wpdb->query($wpdb->prepare(
11553 - "UPDATE $table_name
11554 - SET originating_page_url = %s,
11555 - originating_page_title = %s
11556 - WHERE session_id = %s
11557 - ORDER BY timestamp ASC
11558 - LIMIT 1",
11559 - $page_url,
11560 - $page_title,
11561 - $session_id
11562 - ));
11563 -
11564 - wp_send_json_success(['message' => 'Originating page tracked']);
11565 - wp_die();
11566 -}
11567 -
11568 -/**
11569 - * Validate and clean URLs from AI response
11570 - * Removes any URLs that aren't in the knowledge base
11571 - *
11572 - * @param string $response_text The AI-generated response
11573 - * @param array $valid_urls Array of URLs from the knowledge base
11574 - * @return string Cleaned response with invalid URLs removed/flagged
11575 - */
11576 -private function validate_and_clean_urls($response_text, $valid_urls) {
11577 - // DEBUG: Log what we're working with
11578 - //error_log("=== MxChat URL Validation Debug ===");
11579 - //error_log("Valid URLs count: " . count($valid_urls));
11580 - //error_log("Valid URLs: " . print_r($valid_urls, true));
11581 - //error_log("Response text length: " . strlen($response_text));
11582 - //error_log("Response text preview: " . substr($response_text, 0, 500));
11583 -
11584 - // If no valid URLs provided or empty response, return as-is
11585 - if (empty($valid_urls) || empty($response_text)) {
11586 - //error_log("Validation skipped - empty valid_urls or response");
11587 - return $response_text;
11588 - }
11589 -
11590 - // Extract all URLs from the AI response
11591 - // This regex matches http:// and https:// URLs
11592 - preg_match_all(
11593 - '#\bhttps?://[^\s<>"\')\]]+#i',
11594 - $response_text,
11595 - $matches
11596 - );
11597 -
11598 - // If no URLs found in response, return as-is
11599 - if (empty($matches[0])) {
11600 - //error_log("No URLs found in response");
11601 - return $response_text;
11602 - }
11603 -
11604 - $found_urls = $matches[0];
11605 - $cleaned_response = $response_text;
11606 - $removed_count = 0;
11607 -
11608 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
11609 - $normalized_valid_urls = array_map(function($url) {
11610 - // Remove trailing slash
11611 - $url = rtrim($url, '/');
11612 - // Remove URL fragments (#section)
11613 - $url = preg_replace('/#.*$/', '', $url);
11614 - // Remove trailing punctuation that might have been captured
11615 - $url = rtrim($url, '.,;:!?');
11616 - return $url;
11617 - }, $valid_urls);
11618 -
11619 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
11620 -
11621 - foreach ($found_urls as $found_url) {
11622 - // Clean up the found URL (remove trailing punctuation that might have been captured)
11623 - $clean_found_url = rtrim($found_url, '.,;:!?)');
11624 -
11625 - // DEBUG: Log each URL being checked
11626 - //error_log("Checking found URL: " . $found_url);
11627 -
11628 - // Normalize for comparison
11629 - $normalized_found = rtrim($clean_found_url, '/');
11630 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
11631 -
11632 - //error_log("Normalized found URL: " . $normalized_found);
11633 -
11634 - // Check if this URL exists in our valid URLs list
11635 - $is_valid = false;
11636 -
11637 - //error_log("Starting validation checks for: " . $normalized_found);
11638 -
11639 - // First, try exact match
11640 - if (in_array($normalized_found, $normalized_valid_urls)) {
11641 - $is_valid = true;
11642 - //error_log("EXACT MATCH FOUND");
11643 - } else {
11644 - //error_log("No exact match, checking variations...");
11645 - // If no exact match, check if it's a variation (with query params, etc.)
11646 - foreach ($normalized_valid_urls as $valid_url) {
11647 - //error_log(" Comparing against valid URL: " . $valid_url);
11648 -
11649 - // Check if the found URL starts with a valid URL (handles query params)
11650 - if (strpos($normalized_found, $valid_url) === 0) {
11651 - // Check what comes after the valid URL
11652 - $remainder = substr($normalized_found, strlen($valid_url));
11653 -
11654 - // Only valid if:
11655 - // 1. Exact match (remainder is empty)
11656 - // 2. Query params (starts with ?)
11657 - // 3. Fragment (starts with #)
11658 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
11659 - $is_valid = true;
11660 - //error_log(" MATCH: Found URL is valid variation of base URL");
11661 - break;
11662 - } else {
11663 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
11664 - }
11665 - }
11666 - // Also check the reverse (in case valid URL has query params)
11667 - if (strpos($valid_url, $normalized_found) === 0) {
11668 - $is_valid = true;
11669 - //error_log(" MATCH: Valid URL starts with found URL");
11670 - break;
11671 - }
11672 - }
11673 -
11674 - if (!$is_valid) {
11675 - //error_log("NO MATCH FOUND - URL should be removed");
11676 - }
11677 - }
11678 -
11679 - // If URL is not valid, remove it from the response
11680 - if (!$is_valid) {
11681 - // Log the removal for debugging
11682 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
11683 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
11684 -
11685 - $removed_count++;
11686 -
11687 - // Check if URL is part of a markdown link: [text](url)
11688 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
11689 - if (preg_match($markdown_pattern, $cleaned_response)) {
11690 - //error_log("Found markdown link, removing but keeping text");
11691 - // Remove the markdown link but keep the text
11692 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
11693 - }
11694 - // Check if URL is part of an HTML link: <a href="url">text</a>
11695 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
11696 - //error_log("Found HTML link, removing but keeping text");
11697 - // Remove the HTML link but keep the text
11698 - $link_text = $link_match[1];
11699 - $cleaned_response = preg_replace(
11700 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
11701 - $link_text,
11702 - $cleaned_response
11703 - );
11704 - }
11705 - // Otherwise just remove the bare URL
11706 - else {
11707 - //error_log("Removing bare URL");
11708 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
11709 - }
11710 - }
11711 - }
11712 -
11713 - // Log summary if any URLs were removed
11714 - if ($removed_count > 0) {
11715 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
11716 - } else {
11717 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
11718 - }
11719 -
11720 - // Clean up any double spaces or awkward punctuation left behind
11721 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
11722 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
11723 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
11724 -
11725 - //error_log("Final cleaned response: " . $cleaned_response);
11726 -
11727 - return trim($cleaned_response);
11728 -}
11729 -
11730 -/**
11731 - * AJAX handler to get current chat mode for a session
11732 - */
11733 -public function mxchat_get_current_chat_mode() {
11734 - // Verify nonce for security
11735 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11736 - wp_send_json_error(['message' => 'Invalid nonce']);
11737 - wp_die();
11738 - }
11739 -
11740 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11741 -
11742 - if (empty($session_id)) {
11743 - wp_send_json_error(['message' => 'Session ID missing']);
11744 - wp_die();
11745 - }
11746 -
11747 - // Get the current chat mode for this session
11748 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
11749 -
11750 - wp_send_json_success([
11751 - 'chat_mode' => $chat_mode
11752 - ]);
11753 - wp_die();
11754 -}
11755 564
11756 565
11757 566
11758 567 }