PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.1.9
MxChat – AI Chatbot & Content Generation for WordPress v2.1.9
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 +4688 -11267 3.2.62.1.9 View file →
@@ -1,11267 +1,4688 @@
1 -<?php
2 -if (!defined('ABSPATH')) {
3 - exit;
4 -}
5 -
6 -class MxChat_Integrator {
7 - private $options;
8 - private $prompts_options;
9 - 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 -
19 -/**
20 - * Setup streaming headers - call this right before actually streaming
21 - * This delays header setup to allow actions/forms to return JSON responses
22 - */
23 -private function setup_streaming_headers() {
24 - if ($this->streaming_headers_sent || headers_sent()) {
25 - return false;
26 - }
27 -
28 - // Disable output buffering
29 - while (ob_get_level()) {
30 - ob_end_flush();
31 - }
32 -
33 - // Set headers for SSE
34 - header('Content-Type: text/event-stream');
35 - header('Cache-Control: no-cache');
36 - header('Connection: keep-alive');
37 - header('X-Accel-Buffering: no');
38 -
39 - ob_implicit_flush(true);
40 - flush();
41 -
42 - $this->streaming_headers_sent = true;
43 - return true;
44 -}
45 -
46 -/**
47 - * Class constructor
48 - */
49 -public function __construct() {
50 - $this->options = get_option('mxchat_options');
51 - $this->prompts_options = get_option('mxchat_prompts_options', array());
52 - $this->chat_count = get_option('mxchat_chat_count', 0);
53 - $this->word_handler = new MXChat_Word_Handler($this->options);
54 -
55 - // Add all action hooks
56 - add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
57 - add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
58 - add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
59 - add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
60 - add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
61 -
62 - // Add the AJAX actions for checking if the pre-chat message was dismissed
63 - add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
64 - add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
65 - add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
66 - add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
67 - add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
68 - add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
69 -
70 - // Add REST API routes registration
71 - add_action('rest_api_init', array($this, 'register_routes'));
72 - add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
73 - add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
74 -
75 - // Rate limit action - notice we removed the old schedule setup
76 - add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
77 -
78 - // File upload and handling actions
79 - add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
80 - add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
81 - add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
82 - add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
83 -
84 - // Word document handling actions
85 - add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
86 - add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
87 - add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
88 - add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
89 - add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
90 - add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
91 -
92 - // Email handling actions
93 - add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
94 - add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
95 - add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
96 - add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
97 -
98 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
99 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
100 -
101 - // Testing panel AJAX actions
102 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
103 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
104 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
105 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
106 - // Add to your existing constructor, in the section with other AJAX actions:
107 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
108 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
109 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
110 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
111 - // Add chat mode checking actions
112 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
113 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
114 -
115 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
116 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
117 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
118 -
119 - // Auto-email transcript action
120 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
121 -
122 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
123 -
124 -
125 -}
126 -
127 -/**
128 - * Return a fresh nonce so cached pages can replace the stale one.
129 - */
130 -public function mxchat_refresh_nonce() {
131 - nocache_headers();
132 - wp_send_json_success(array('nonce' => wp_create_nonce('mxchat_chat_nonce')));
133 -}
134 -
135 -// In your core plugin's check_actions_for_addons method:
136 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
137 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
138 -
139 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
140 -
141 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
142 -
143 - return $result;
144 -}
145 -
146 - private function mxchat_increment_chat_count() {
147 - $chat_count = get_option('mxchat_chat_count', 0);
148 - $chat_count++;
149 - update_option('mxchat_chat_count', $chat_count);
150 - }
151 -
152 -function mxchat_fetch_conversation_history() {
153 - if (empty($_POST['session_id'])) {
154 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
155 - wp_die();
156 - }
157 -
158 - $session_id = sanitize_text_field($_POST['session_id']);
159 -
160 - // SECURITY FIX: Verify session ownership before retrieving data
161 - // If IP/user changed, signal frontend to reset session instead of blocking
162 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
163 -
164 - // Check if this session has an owner recorded
165 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
166 -
167 - // Update session owner if it changed (e.g. IP changed due to network switch)
168 - // The session ID itself is the authentication — if the client has it, they own it
169 - if (!$session_owner || $session_owner !== $current_user_identifier) {
170 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
171 - }
172 -
173 - $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
174 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
175 -
176 - if (empty($history)) {
177 - // Even if history is empty, return the chat mode
178 - wp_send_json_success([
179 - 'conversation' => [],
180 - 'chat_mode' => $chat_mode
181 - ]);
182 - wp_die();
183 - }
184 -
185 - wp_send_json_success([
186 - 'conversation' => $history,
187 - 'chat_mode' => $chat_mode
188 - ]);
189 - wp_die();
190 -}
191 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
192 - $history = get_option("mxchat_history_{$session_id}", []);
193 -
194 - // Check persistence setting - when OFF, only include messages from current page load
195 - $options = get_option('mxchat_options', []);
196 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
197 -
198 - // Filter history when persistence is OFF to match what the user sees
199 - if (!$persistence_enabled && $session_start_timestamp > 0) {
200 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
201 - // Include messages from this page load onwards
202 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
203 - });
204 - // Re-index array after filtering
205 - $history = array_values($history);
206 - }
207 -
208 - $formatted_history = [];
209 -
210 - // Adjusted for code-heavy conversations
211 - $max_tokens = 120000; // Context window size
212 - $reserved_tokens = 5000; // Space for system prompts + current query
213 - $current_token_count = 0;
214 -
215 - // Allowed HTML tags for content sanitization
216 - $allowed_tags = [
217 - 'pre' => ['class' => true],
218 - 'code' => ['class' => true],
219 - 'span' => ['class' => true],
220 - 'div' => ['class' => true],
221 - 'strong' => [],
222 - 'em' => []
223 - ];
224 -
225 - foreach (array_reverse($history) as $entry) {
226 - // Preserve code blocks while sanitizing other HTML
227 - $clean_content = wp_kses($entry['content'], $allowed_tags);
228 -
229 - // Detect code blocks in content
230 - $has_code = false;
231 -// Replace the HTML check with:
232 -// Allow messages that contain code blocks or are plain text
233 -if (strpos($clean_content, '<pre') === false &&
234 - strpos($clean_content, '<code') === false &&
235 - $clean_content !== strip_tags($entry['content'])) {
236 - continue;
237 -}
238 -
239 - // Skip entries that lost significant content during sanitization
240 - if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
241 - continue;
242 - }
243 -
244 - // More accurate token estimation (1 token ≈ 4 characters)
245 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
246 -
247 - // Check token budget with the new estimate
248 - if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
249 - // Try to fit partial content if it's the first entry
250 - if (empty($formatted_history)) {
251 - $clean_content = mb_substr($clean_content, 0, ($max_tokens - $reserved_tokens) * 4);
252 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
253 - } else {
254 - break;
255 - }
256 - }
257 -
258 - // Add to formatted history
259 - $formatted_history[] = [
260 - 'role' => $entry['role'],
261 - 'content' => $clean_content
262 - ];
263 -
264 - $current_token_count += $token_estimate;
265 - }
266 -
267 - // Reverse back to maintain chronological order
268 - $formatted_history = array_reverse($formatted_history);
269 -
270 - // Add system message about code context
271 - array_unshift($formatted_history, [
272 - 'role' => 'system',
273 - 'content' => 'Preserved code blocks are marked with [CODE BLOCK PRESERVED]. '
274 - . 'Maintain formatting and syntax highlighting when referencing code.'
275 - ]);
276 -
277 - return $formatted_history;
278 -}
279 -
280 -public function register_routes() {
281 - //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
282 -
283 - register_rest_route('mxchat/v1', '/stream', [
284 - 'methods' => 'GET',
285 - 'callback' => [$this, 'mxchat_stream_events'],
286 - 'permission_callback' => [$this, 'verify_chat_session'],
287 - ]);
288 -
289 - register_rest_route('mxchat/v1', '/agent-response', [
290 - 'methods' => 'POST',
291 - 'callback' => [$this, 'mxchat_handle_agent_response'],
292 - 'permission_callback' => [$this, 'verify_slack_request'],
293 - ]);
294 -
295 - register_rest_route('mxchat/v1', '/slack-interaction', [
296 - 'methods' => 'POST',
297 - 'callback' => [$this, 'handle_slack_interaction'],
298 - 'permission_callback' => [$this, 'verify_slack_request'],
299 - ]);
300 -
301 - register_rest_route('mxchat/v1', '/slack-messages', [
302 - 'methods' => 'POST',
303 - 'callback' => [$this, 'handle_slack_messages'],
304 - 'permission_callback' => [$this, 'verify_slack_request'],
305 - ]);
306 -
307 - // Telegram webhook endpoint
308 - register_rest_route('mxchat/v1', '/telegram-webhook', [
309 - 'methods' => 'POST',
310 - 'callback' => [$this, 'handle_telegram_webhook'],
311 - 'permission_callback' => [$this, 'verify_telegram_request'],
312 - ]);
313 -
314 - //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
315 -}
316 -
317 -/**
318 - * Verify valid chat session
319 - */
320 -public function verify_chat_session($request) {
321 - $session_id = $request->get_param('session_id');
322 - if (empty($session_id)) {
323 - //error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
324 - return false;
325 - }
326 -
327 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
328 - return $chat_mode === 'agent';
329 -}
330 -
331 -/**
332 - * Verify request is coming from Slack.
333 - *
334 - * @param WP_REST_Request $request
335 - * @return bool True if valid, false otherwise.
336 - */
337 -public function verify_slack_request($request) {
338 - // Get the Slack signing secret from your plugin options
339 - $valid_key = $this->options['live_agent_secret_key'] ?? '';
340 -
341 - if (empty($valid_key)) {
342 - //error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
343 - return false;
344 - }
345 -
346 - $timestamp = $request->get_header('X-Slack-Request-Timestamp');
347 - $slack_signature = $request->get_header('X-Slack-Signature');
348 -
349 - // Verify timestamp to prevent replay attacks
350 - if (abs(time() - intval($timestamp)) > 300) {
351 - //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
352 - return false;
353 - }
354 -
355 - // Get raw request body from the WP_REST_Request object
356 - // (php://input may already be consumed by WordPress at this point)
357 - $request_body = $request->get_body();
358 -
359 - // Create the signature base string
360 - $sig_basestring = "v0:{$timestamp}:{$request_body}";
361 -
362 - // Calculate expected signature
363 - $my_signature = 'v0=' . hash_hmac('sha256', $sig_basestring, $valid_key);
364 -
365 - // Compare signatures
366 - return hash_equals($my_signature, $slack_signature);
367 -}
368 -
369 -/**
370 - * Verify request is coming from Telegram.
371 - *
372 - * @param WP_REST_Request $request
373 - * @return bool True if valid, false otherwise.
374 - */
375 -public function verify_telegram_request($request) {
376 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
377 -
378 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
379 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
380 -
381 - if (empty($secret_token)) {
382 - // If no secret is configured, allow the request (for initial setup)
383 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
384 - return true;
385 - }
386 -
387 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
388 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
389 -
390 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
391 -
392 - if (empty($request_token)) {
393 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
394 - return false;
395 - }
396 -
397 - // Timing-safe comparison
398 - $result = hash_equals($secret_token, $request_token);
399 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
400 - return $result;
401 -}
402 -
403 -public function mxchat_stream_events(WP_REST_Request $request) {
404 - header('Content-Type: text/event-stream');
405 - header('Cache-Control: no-cache');
406 - header('Connection: keep-alive');
407 -
408 - $session_id = sanitize_text_field($request->get_param('session_id'));
409 - $last_seen_id = sanitize_text_field($request->get_param('last_seen_id')) ?: '';
410 -
411 - if (empty($session_id)) {
412 - echo esc_html__("event: error\ndata: ", 'mxchat') . esc_html__('Missing session_id', 'mxchat') . "\n\n";
413 - flush();
414 - exit;
415 - }
416 -
417 - $history = get_option("mxchat_history_{$session_id}", []);
418 -
419 - // Filter only new messages
420 - $new_messages = array_filter($history, function ($message) use ($last_seen_id) {
421 - return !empty($message['id']) && $message['id'] > $last_seen_id;
422 - });
423 -
424 - // Send new messages if available
425 - if (!empty($new_messages)) {
426 - echo esc_html__("event: newMessages\ndata: ", 'mxchat') . json_encode(array_values($new_messages)) . "\n\n";
427 - } else {
428 - // Keep the connection alive
429 - echo esc_html__("event: keepAlive\ndata: ", 'mxchat') . "{}\n\n";
430 - }
431 - flush();
432 - exit;
433 -}
434 -
435 -
436 -
437 -
438 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
439 - global $wpdb;
440 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
441 - //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
442 -
443 - // Check if this is the first message in a new session (before any other database operations)
444 - $is_new_session = false;
445 - if ($role === 'user') { // Only check for user messages, not bot responses
446 - $existing_messages = $wpdb->get_var($wpdb->prepare(
447 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
448 - $session_id
449 - ));
450 - $is_new_session = ($existing_messages == 0);
451 -
452 - // Log for debugging
453 - if ($is_new_session) {
454 - //error_log("[DEBUG] This is a NEW session - first message");
455 - }
456 - }
457 -
458 - // SECURITY FIX: Set session ownership for new sessions
459 - if ($is_new_session && $role === 'user') {
460 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
461 - $session_owner_key = "mxchat_session_owner_{$session_id}";
462 -
463 - // Only set ownership if not already set
464 - if (!get_option($session_owner_key)) {
465 - update_option($session_owner_key, $current_user_identifier, 'no');
466 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
467 - }
468 - }
469 -
470 - // 1) Extract agent name if present
471 - $agent_name = '';
472 - if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
473 - $agent_name = $matches[1];
474 - $message = str_replace("Agent: $agent_name - ", '', $message);
475 - $session_meta_key = "mxchat_agent_name_{$session_id}";
476 - if (empty(get_option($session_meta_key))) {
477 - update_option($session_meta_key, $agent_name);
478 - //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
479 - }
480 - }
481 -
482 - // 2) Generate unique message_id
483 - $message_id = uniqid();
484 - //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
485 -
486 - // 3) Determine user_id
487 - $user_id = is_user_logged_in() ? get_current_user_id() : 0;
488 -
489 - // 4) Determine user_identifier
490 - $user_identifier = $agent_name
491 - ? $agent_name
492 - : MxChat_User::mxchat_get_user_identifier();
493 -
494 - // 5) Determine displayed_name
495 - $user_email = MxChat_User::mxchat_get_user_email();
496 - $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
497 -
498 - // 6) Check for a saved email in wp_options
499 - $email_option_key = "mxchat_email_{$session_id}";
500 - $saved_email = get_option($email_option_key);
501 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
502 -
503 - // Check for a saved name in wp_options
504 - $name_option_key = "mxchat_name_{$session_id}";
505 - $saved_name = get_option($name_option_key);
506 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
507 -
508 - // If found, update DB user_email and user_name
509 - if ($saved_email || $saved_name) {
510 - $update_data = [];
511 - if ($saved_email) {
512 - $update_data['user_email'] = $saved_email;
513 - }
514 - if ($saved_name) {
515 - $update_data['user_name'] = $saved_name;
516 - }
517 -
518 - if (!empty($update_data)) {
519 - $update_res = $wpdb->update(
520 - $table_name,
521 - $update_data,
522 - ['session_id' => $session_id],
523 - array_fill(0, count($update_data), '%s'),
524 - ['%s']
525 - );
526 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
527 - }
528 - }
529 -
530 - // 7) Save to session history in wp_options
531 - $history_key = "mxchat_history_{$session_id}";
532 - $history = get_option($history_key, []);
533 - $history[] = [
534 - 'id' => $message_id,
535 - 'role' => $role,
536 - 'content' => $message,
537 - 'timestamp' => round(microtime(true) * 1000),
538 - 'agent_name' => $displayed_name,
539 - ];
540 - update_option($history_key, $history, 'no');
541 - //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
542 -
543 - // 8) Save the message to DB (INSERT)
544 - $insert_data = [
545 - 'user_id' => $user_id,
546 - 'user_identifier'=> $user_identifier,
547 - 'user_email' => $saved_email ?: $user_email,
548 - 'user_name' => $saved_name ?: '', // Add name to insert data
549 - 'session_id' => $session_id,
550 - 'role' => $role,
551 - 'message' => $message,
552 - 'timestamp' => current_time('mysql', 1),
553 - ];
554 -
555 - // IMPROVED: Handle originating page data
556 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
557 -
558 - if ($columns_exist) {
559 - if ($is_new_session && $role === 'user') {
560 - // For the first user message, set originating page data
561 -
562 - // First check if we have it from the parameter
563 - if ($originating_page && !empty($originating_page['url'])) {
564 - $insert_data['originating_page_url'] = $originating_page['url'];
565 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
566 -
567 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
568 - }
569 - // Otherwise check if it's stored in the instance property
570 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
571 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
572 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
573 -
574 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
575 -
576 - // Clear after using
577 - unset($this->pending_originating_page);
578 - }
579 - // Fallback to HTTP_REFERER if nothing else is available
580 - else if (isset($_SERVER['HTTP_REFERER'])) {
581 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
582 - $insert_data['originating_page_url'] = $referer_url;
583 -
584 - // Generate title from URL
585 - $parsed_url = parse_url($referer_url);
586 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
587 -
588 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
589 - $insert_data['originating_page_title'] = 'Homepage';
590 - } else {
591 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
592 - $insert_data['originating_page_title'] = ucwords(trim($title));
593 - }
594 -
595 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
596 - }
597 -
598 - // Store for this session so all messages have the same originating page
599 - if (!empty($insert_data['originating_page_url'])) {
600 - update_option("mxchat_originating_page_{$session_id}", [
601 - 'url' => $insert_data['originating_page_url'],
602 - 'title' => $insert_data['originating_page_title']
603 - ], 'no');
604 - }
605 - } else {
606 - // For subsequent messages in the session, use the stored originating page
607 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
608 - if ($stored_originating && !empty($stored_originating['url'])) {
609 - $insert_data['originating_page_url'] = $stored_originating['url'];
610 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
611 - }
612 - }
613 - }
614 -
615 - // Add RAG context if provided (for bot messages)
616 - if ($rag_context !== null && $role === 'bot') {
617 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
618 - if ($rag_context_column_exists) {
619 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
620 - }
621 - }
622 -
623 - $wpdb->insert($table_name, $insert_data);
624 - //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
625 -
626 - // 9) Send notification email if this is the first user message in a new session
627 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
628 - $this->send_new_chat_notification($session_id, array(
629 - 'identifier' => $user_identifier,
630 - 'email' => $saved_email ?: $user_email,
631 - 'ip' => $_SERVER['REMOTE_ADDR']
632 - ));
633 - }
634 -
635 - // 10) Schedule delayed transcript email if enabled and message is from user
636 - if ($wpdb->insert_id && $role === 'user') {
637 - $this->schedule_delayed_transcript_email($session_id);
638 - }
639 -
640 - //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
641 - return $message_id;
642 -}
643 -
644 -private function send_new_chat_notification($session_id, $user_info = array()) {
645 - $options = get_option('mxchat_transcripts_options');
646 -
647 - // Check if notifications are enabled
648 - if (empty($options['mxchat_enable_notifications'])) {
649 - return false;
650 - }
651 -
652 - // Get notification email
653 - $to = !empty($options['mxchat_notification_email']) ?
654 - $options['mxchat_notification_email'] :
655 - get_option('admin_email');
656 -
657 - if (!is_email($to)) {
658 - return false;
659 - }
660 -
661 - // Prepare email content
662 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
663 -
664 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
665 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
666 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
667 -
668 - $message = sprintf(
669 - "A new chat session has started on your website.\n\n" .
670 - "Session ID: %s\n" .
671 - "User: %s\n" .
672 - "Email: %s\n" .
673 - "IP Address: %s\n" .
674 - "Time: %s\n\n" .
675 - "View transcripts: %s",
676 - $session_id,
677 - $user_identifier,
678 - $user_email,
679 - $user_ip,
680 - current_time('mysql'),
681 - admin_url('admin.php?page=mxchat-transcripts')
682 - );
683 -
684 - // Send email
685 - return wp_mail($to, $subject, $message);
686 -}
687 -
688 -/**
689 - * Schedule delayed transcript email for a session
690 - * Reschedules if a new user message is received
691 - */
692 -private function schedule_delayed_transcript_email($session_id) {
693 - $options = get_option('mxchat_transcripts_options');
694 -
695 - // Check if auto-email is enabled
696 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
697 - return;
698 - }
699 -
700 - // Get notification email
701 - $email = !empty($options['mxchat_notification_email']) ?
702 - $options['mxchat_notification_email'] :
703 - get_option('admin_email');
704 -
705 - if (!is_email($email)) {
706 - return;
707 - }
708 -
709 - // Get delay in minutes (default 30)
710 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
711 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
712 -
713 - // Clear any existing scheduled event for this session
714 - $hook = 'mxchat_send_delayed_transcript';
715 - $args = array($session_id);
716 - $timestamp = wp_next_scheduled($hook, $args);
717 -
718 - if ($timestamp) {
719 - wp_unschedule_event($timestamp, $hook, $args);
720 - }
721 -
722 - // Schedule new event
723 - $schedule_time = time() + ($delay_minutes * 60);
724 - wp_schedule_single_event($schedule_time, $hook, $args);
725 -}
726 -
727 -/**
728 - * Check if chat messages contain contact information (email or phone number)
729 - *
730 - * @param array $messages Array of message objects with 'message' property
731 - * @param object|null $session_data Session data object with user_email property
732 - * @return bool True if contact info found, false otherwise
733 - */
734 -private function chat_contains_contact_info($messages, $session_data = null) {
735 - // Check if session already has a stored email
736 - if ($session_data && !empty($session_data->user_email)) {
737 - return true;
738 - }
739 -
740 - // Email regex pattern
741 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
742 -
743 - // Phone number patterns (covers various formats including international, WhatsApp style)
744 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
745 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
746 -
747 - // Only check user messages (not assistant responses)
748 - foreach ($messages as $msg) {
749 - if ($msg->role !== 'user') {
750 - continue;
751 - }
752 -
753 - $message_text = $msg->message;
754 -
755 - // Check for email
756 - if (preg_match($email_pattern, $message_text)) {
757 - return true;
758 - }
759 -
760 - // Check for phone number (must be at least 7 digits total to avoid false positives)
761 - if (preg_match($phone_pattern, $message_text, $matches)) {
762 - // Count actual digits to avoid matching short numbers
763 - $digits_only = preg_replace('/\D/', '', $matches[0]);
764 - if (strlen($digits_only) >= 7) {
765 - return true;
766 - }
767 - }
768 - }
769 -
770 - return false;
771 -}
772 -
773 -/**
774 - * Send the delayed transcript email with .txt attachment
775 - */
776 -public function mxchat_send_delayed_transcript($session_id) {
777 - global $wpdb;
778 -
779 - $options = get_option('mxchat_transcripts_options');
780 -
781 - // Get notification email
782 - $to = !empty($options['mxchat_notification_email']) ?
783 - $options['mxchat_notification_email'] :
784 - get_option('admin_email');
785 -
786 - if (!is_email($to)) {
787 - return false;
788 - }
789 -
790 - // Get all messages for this session
791 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
792 - $messages = $wpdb->get_results($wpdb->prepare(
793 - "SELECT role, message, timestamp FROM {$table_name}
794 - WHERE session_id = %s
795 - ORDER BY timestamp ASC",
796 - $session_id
797 - ));
798 -
799 - if (empty($messages)) {
800 - return false;
801 - }
802 -
803 - // Get session metadata
804 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
805 - $session_data = $wpdb->get_row($wpdb->prepare(
806 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
807 - $session_id
808 - ));
809 -
810 - // Check if contact info is required and if it's present
811 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
812 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
813 - // Contact info required but not found - skip sending
814 - return false;
815 - }
816 -
817 - // Build transcript content
818 - $transcript_content = "Chat Transcript\n";
819 - $transcript_content .= "================\n\n";
820 - $transcript_content .= "Session ID: " . $session_id . "\n";
821 -
822 - if ($session_data) {
823 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
824 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
825 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
826 - }
827 -
828 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
829 -
830 - // Add messages
831 - foreach ($messages as $msg) {
832 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
833 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
834 - $transcript_content .= $msg->message . "\n\n";
835 - }
836 -
837 - // Create temporary file for attachment using WP_Filesystem
838 - $upload_dir = wp_upload_dir();
839 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
840 - global $wp_filesystem;
841 - if (empty($wp_filesystem)) {
842 - require_once ABSPATH . 'wp-admin/includes/file.php';
843 - WP_Filesystem();
844 - }
845 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
846 -
847 - // Prepare email
848 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
849 -
850 - $message = "Please find attached the full chat transcript.\n\n";
851 - $message .= "Session ID: {$session_id}\n";
852 -
853 - if ($session_data) {
854 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
855 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
856 - }
857 -
858 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
859 -
860 - // Send email with attachment
861 - $attachments = array($temp_file);
862 - $result = wp_mail($to, $subject, $message, '', $attachments);
863 -
864 - // Clean up temporary file
865 - if (file_exists($temp_file)) {
866 - unlink($temp_file);
867 - }
868 -
869 - return $result;
870 -}
871 -
872 -
873 -
874 -public function mxchat_handle_save_email_and_response() {
875 - //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
876 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
877 -
878 - nocache_headers();
879 -
880 - // Validate nonce
881 - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
882 - //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
883 - wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
884 - wp_die();
885 - }
886 -
887 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
888 - $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
889 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
890 -
891 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
892 -
893 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
894 - //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
895 - wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
896 - wp_die();
897 - }
898 -
899 - // Validate name if provided (check if name field is enabled and name is required)
900 - $options = get_option('mxchat_options', []);
901 - $name_field_enabled = isset($options['enable_name_field']) &&
902 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
903 -
904 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
905 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
906 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
907 - wp_die();
908 - }
909 -
910 - // 1) Always store email in wp_options
911 - $email_option_key = "mxchat_email_{$session_id}";
912 - update_option($email_option_key, $email, 'no');
913 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
914 -
915 - // Store name in wp_options if provided
916 - if (!empty($name)) {
917 - $name_option_key = "mxchat_name_{$session_id}";
918 - update_option($name_option_key, $name, 'no');
919 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
920 - }
921 -
922 - // 2) (Optional) Also store in DB if a row already exists
923 - global $wpdb;
924 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
925 -
926 - // Make sure we have a valid placeholder in prepare
927 - $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", $session_id);
928 - $session_count = $wpdb->get_var($sql);
929 -
930 - //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
931 -
932 - if ($session_count) {
933 - // Update both user_email and user_name if row(s) exist
934 - if (!empty($name)) {
935 - $update_sql = $wpdb->prepare(
936 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
937 - $email,
938 - $name,
939 - $session_id
940 - );
941 - } else {
942 - $update_sql = $wpdb->prepare(
943 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
944 - $email,
945 - $session_id
946 - );
947 - }
948 - $wpdb->query($update_sql);
949 - //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
950 - } else {
951 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
952 - }
953 -
954 - // Provide success response (same as original)
955 - $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
956 - //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
957 - wp_send_json_success(['message' => $bot_message]);
958 - wp_die();
959 -}
960 -
961 -public function mxchat_check_email_provided() {
962 - //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
963 -
964 - nocache_headers();
965 -
966 - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
967 - //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
968 - wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
969 - }
970 -
971 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
972 - if (empty($session_id) || $session_id === 'null') {
973 - //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
974 - wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
975 - }
976 -
977 - // Check if the user is logged in
978 - if (is_user_logged_in()) {
979 - $current_user = wp_get_current_user();
980 - //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
981 -
982 - // Get user's display name for logged in users
983 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
984 - (!empty($current_user->first_name) ? $current_user->first_name : '');
985 -
986 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
987 - if (!empty($user_name)) {
988 - $response_data['name'] = $user_name;
989 - }
990 -
991 - wp_send_json_success($response_data);
992 - }
993 -
994 - // Check if name field is required
995 - $options = get_option('mxchat_options', []);
996 - $name_field_enabled = isset($options['enable_name_field']) &&
997 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
998 -
999 - $email_option_key = "mxchat_email_{$session_id}";
1000 - $stored_email = get_option($email_option_key, '');
1001 -
1002 - // Check for stored name
1003 - $name_option_key = "mxchat_name_{$session_id}";
1004 - $stored_name = get_option($name_option_key, '');
1005 -
1006 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1007 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1008 -
1009 - // Check if we have email and name (if name is required)
1010 - $has_required_info = !empty($stored_email);
1011 -
1012 - if ($name_field_enabled) {
1013 - $has_required_info = $has_required_info && !empty($stored_name);
1014 - }
1015 -
1016 - if ($has_required_info) {
1017 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1018 -
1019 - $response_data = ['email' => $stored_email];
1020 - if (!empty($stored_name)) {
1021 - $response_data['name'] = $stored_name;
1022 - }
1023 -
1024 - wp_send_json_success($response_data);
1025 - } else {
1026 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
1027 - wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1028 - }
1029 -}
1030 -
1031 -/**
1032 - * Send error response in appropriate format based on streaming mode
1033 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1034 - *
1035 - * @param string $error_message The error message to display
1036 - * @param string $error_code Optional error code for debugging
1037 - */
1038 -private function send_error_response($error_message, $error_code = 'api_error') {
1039 - if ($this->is_streaming) {
1040 - echo "data: " . json_encode([
1041 - 'error' => true,
1042 - 'error_message' => $error_message,
1043 - 'error_code' => $error_code,
1044 - 'text' => $error_message,
1045 - 'message' => $error_message
1046 - ]) . "\n\n";
1047 - echo "data: [DONE]\n\n";
1048 - flush();
1049 - } else {
1050 - wp_send_json_error([
1051 - 'error_message' => $error_message,
1052 - 'error_code' => $error_code
1053 - ]);
1054 - }
1055 - wp_die();
1056 -}
1057 -
1058 -public function mxchat_handle_chat_request() {
1059 - global $wpdb;
1060 -
1061 - // Debug: Log incoming bot_id
1062 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1063 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1064 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1065 -
1066 - // Get bot-specific options
1067 - $bot_options = $this->get_bot_options($bot_id);
1068 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1069 -
1070 - // Check if this is a streaming request
1071 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1072 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1073 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1074 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1075 -
1076 - // ADDED: Store streaming state in class property for use in private methods
1077 - $this->is_streaming = $is_streaming;
1078 -
1079 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1080 - // This allows actions/forms to return JSON responses without header conflicts
1081 -
1082 - // Check if MX Chat Moderation is active
1083 - if (class_exists('MX_Chat_Moderation')) {
1084 - // Get user email and IP
1085 - $user_email = '';
1086 - $user_ip = $_SERVER['REMOTE_ADDR'];
1087 -
1088 - // If user is logged in, get their email
1089 - if (is_user_logged_in()) {
1090 - $current_user = wp_get_current_user();
1091 - $user_email = $current_user->user_email;
1092 - }
1093 -
1094 - // Create ban handler instance
1095 - $ban_handler = new MX_Chat_Ban_Handler();
1096 -
1097 - // Check if user is banned by IP
1098 - if ($ban_handler->check_ban($user_ip, 'ip')) {
1099 - wp_send_json([
1100 - 'success' => false,
1101 - 'message' => esc_html__('Access denied. Your IP address has been banned.', 'mxchat'),
1102 - 'status' => 'banned'
1103 - ]);
1104 - wp_die();
1105 - }
1106 -
1107 - // If user is logged in, also check email
1108 - if (!empty($user_email) && $ban_handler->check_ban($user_email, 'email')) {
1109 - wp_send_json([
1110 - 'success' => false,
1111 - 'message' => esc_html__('Access denied. Your email address has been banned.', 'mxchat'),
1112 - 'status' => 'banned'
1113 - ]);
1114 - wp_die();
1115 - }
1116 - }
1117 -
1118 - $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1119 - $this->productCardHtml = '';
1120 -
1121 - // Get the actual WordPress user ID if logged in
1122 - $is_logged_in = is_user_logged_in();
1123 - if ($is_logged_in) {
1124 - $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1125 - } else {
1126 - // For logged-out users, use your existing identifier method
1127 - $user_id = $this->mxchat_get_user_identifier();
1128 - }
1129 -
1130 - // Get and sanitize the user identifier
1131 - $user_id = sanitize_key($user_id);
1132 -
1133 - // Check rate limit using new settings structure
1134 - $rate_limit_result = $this->check_rate_limit();
1135 -
1136 - if ($rate_limit_result !== true) {
1137 - wp_send_json([
1138 - 'success' => false,
1139 - 'message' => $rate_limit_result['message'],
1140 - 'status' => 'rate_limit_exceeded'
1141 - ]);
1142 - wp_die();
1143 - }
1144 -
1145 - // Rest of your existing code...
1146 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1147 -
1148 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1149 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1150 - // the frontend FormData.append() to stringify a null session_id into the literal
1151 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1152 - // ghost sessions that group every visitor's first message under one row.
1153 - if ($session_id === 'null' || $session_id === 'undefined') {
1154 - $session_id = '';
1155 - }
1156 -
1157 - if (empty($session_id)) {
1158 - wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1159 - wp_die();
1160 - }
1161 -
1162 - // Update session owner if it changed (e.g. IP changed due to network switch)
1163 - // The session ID itself is the authentication — if the client has it, they own it
1164 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1165 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1166 -
1167 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1168 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1169 - }
1170 -
1171 - // Validate and sanitize the incoming message
1172 - if (empty($_POST['message'])) {
1173 - wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1174 - wp_die();
1175 - }
1176 -
1177 -
1178 - // Track originating page for first message in session
1179 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1180 -
1181 - // Check if originating page columns exist
1182 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1183 -
1184 - if ($columns_exist) {
1185 - // Check if this session already has messages
1186 - $message_count = $wpdb->get_var($wpdb->prepare(
1187 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1188 - $session_id
1189 - ));
1190 -
1191 - // If this is the first message in the session
1192 - if ($message_count == 0) {
1193 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1194 - $originating_url = '';
1195 - $originating_title = '';
1196 -
1197 - // Try to get from POST data first (sent by JavaScript)
1198 - if (isset($_POST['current_page_url'])) {
1199 - $originating_url = esc_url_raw($_POST['current_page_url']);
1200 - $originating_title = isset($_POST['current_page_title'])
1201 - ? sanitize_text_field($_POST['current_page_title'])
1202 - : '';
1203 - }
1204 - // Fallback to HTTP_REFERER if not provided by JavaScript
1205 - else if (isset($_SERVER['HTTP_REFERER'])) {
1206 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1207 - }
1208 -
1209 - // Generate title if we have URL but no title
1210 - if ($originating_url && empty($originating_title)) {
1211 - $parsed_url = parse_url($originating_url);
1212 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1213 -
1214 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1215 - $originating_title = 'Homepage';
1216 - } else {
1217 - // Clean up the path to make a readable title
1218 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1219 - $originating_title = ucwords(trim($originating_title));
1220 - }
1221 - }
1222 -
1223 - // Store for later use when saving the message
1224 - $this->pending_originating_page = [
1225 - 'url' => $originating_url,
1226 - 'title' => $originating_title
1227 - ];
1228 - }
1229 - }
1230 -
1231 -
1232 -
1233 - // Get page context if provided
1234 - $page_context = null;
1235 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1236 - $page_context_raw = stripslashes($_POST['page_context']);
1237 - $page_context = json_decode($page_context_raw, true);
1238 -
1239 - // Validate page context structure
1240 - if (is_array($page_context) &&
1241 - isset($page_context['url']) &&
1242 - isset($page_context['title']) &&
1243 - isset($page_context['content'])) {
1244 -
1245 - // Sanitize page context
1246 - $page_context['url'] = esc_url_raw($page_context['url']);
1247 - $page_context['title'] = sanitize_text_field($page_context['title']);
1248 - $page_context['content'] = wp_kses_post($page_context['content']);
1249 - } else {
1250 - $page_context = null;
1251 - }
1252 - }
1253 -
1254 - // Modify the message sanitization to preserve PHP tags in code blocks
1255 - $allowed_tags = [
1256 - 'pre' => [],
1257 - 'code' => ['class' => true],
1258 - 'span' => ['class' => true],
1259 - 'div' => ['class' => true],
1260 - ];
1261 -
1262 - // First preserve code blocks
1263 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1264 - return htmlspecialchars_decode($matches[0]);
1265 - }, $_POST['message']);
1266 -
1267 - // Then apply sanitization
1268 - $message = wp_kses($message, $allowed_tags);
1269 -
1270 - // Preserve code blocks from markdown conversion
1271 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1272 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1273 -
1274 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1275 - // Always initialize testing data for admins (no toggle needed)
1276 - $testing_data = null;
1277 - if (current_user_can('administrator')) {
1278 - // For vision messages, use the original user message for the query display
1279 - $query_for_testing = $message;
1280 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1281 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1282 - }
1283 -
1284 - $testing_data = [
1285 - 'query' => $query_for_testing,
1286 - 'timestamp' => time(),
1287 - 'top_matches' => [],
1288 - 'action_matches' => [], // Initialize action matches array
1289 - 'page_context' => $page_context, // Include page context in testing data
1290 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1291 - 'bot_id' => $bot_id // Include bot ID in testing data
1292 - ];
1293 -
1294 - // Get similarity threshold from bot options or default options
1295 - $similarity_threshold = isset($current_options['similarity_threshold'])
1296 - ? ((int) $current_options['similarity_threshold']) / 100
1297 - : 0.35;
1298 -
1299 - $testing_data['similarity_threshold'] = $similarity_threshold;
1300 -
1301 - // Determine knowledge base type using bot-specific config
1302 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1303 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1304 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1305 - }
1306 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
1307 -
1308 - // Add debug before and after:
1309 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1310 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1311 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
1312 -
1313 -
1314 - // If the pre-processing returned a result (not the original message), use it directly
1315 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1316 - // Save the AI response
1317 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1318 -
1319 - // Save HTML content if provided
1320 - if (!empty($pre_processed_result['html'])) {
1321 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1322 - }
1323 -
1324 - // Add testing data if admin
1325 - $response_data = [
1326 - 'text' => $pre_processed_result['text'],
1327 - 'html' => $pre_processed_result['html'] ?? '',
1328 - 'session_id' => $session_id
1329 - ];
1330 -
1331 - if ($testing_data !== null) {
1332 - $response_data['testing_data'] = $testing_data;
1333 - }
1334 -
1335 - wp_send_json($response_data);
1336 - wp_die();
1337 - }
1338 -
1339 - // Save the user's message - handle vision processed messages differently
1340 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1341 - // For vision messages, save the original user message with image indicator
1342 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1343 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1344 - $image_count = intval($_POST['vision_images_count']);
1345 - $original_message .= " [{$image_count} image(s)]";
1346 - }
1347 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1348 - } else {
1349 - // Regular message - save as normal
1350 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1351 - }
1352 -
1353 -
1354 - if (is_email($message)) {
1355 - // Add the email to Loops
1356 - $this->add_email_to_loops($message);
1357 -
1358 - // Get the user's success message instruction using current_options
1359 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1360 -
1361 - // Set instruction for AI using the user's success message
1362 - $this->current_action_instruction = $user_success_message;
1363 -
1364 - // Clear the email capture transient since we got the email
1365 - delete_transient('mxchat_email_capture_' . $user_id);
1366 - }
1367 -
1368 - // Check if we're in an email capture flow but user hasn't provided email yet
1369 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1370 - // Check if the message contains an email (not the whole message being an email)
1371 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1372 - $extracted_email = $matches[0];
1373 -
1374 - // Add the extracted email to Loops
1375 - $this->add_email_to_loops($extracted_email);
1376 -
1377 - // Get the user's success message instruction using current_options
1378 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1379 -
1380 - // Set instruction for AI using the user's success message
1381 - $this->current_action_instruction = $user_success_message;
1382 -
1383 - // Clear the email capture transient since we got the email
1384 - delete_transient('mxchat_email_capture_' . $user_id);
1385 - }
1386 - // If no email found but we're in capture mode, remind them
1387 - else {
1388 - // Get the original instruction to remind them using current_options
1389 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1390 - $this->current_action_instruction = $original_instruction;
1391 - }
1392 - }
1393 -
1394 - $intent_info = '';
1395 -
1396 - // Check chat mode
1397 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1398 -
1399 - // Handle agent mode
1400 - // Handle agent mode
1401 - if ($chat_mode === 'agent') {
1402 - // First, check for switch intent before doing anything else
1403 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1404 -
1405 - // Capture action analysis for testing panel after intent check
1406 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1407 - $testing_data['action_matches'] = $this->last_action_analysis;
1408 - }
1409 -
1410 - // Around line 506, in the agent mode handling section:
1411 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1412 - // Update chat mode first
1413 - update_option("mxchat_mode_{$session_id}", 'ai');
1414 -
1415 - // Clear any existing PDF context to start fresh
1416 - $this->clear_pdf_transients($session_id);
1417 -
1418 - // Prepare clean switch response with explicit chat_mode
1419 - $response_data = [
1420 - 'text' => $this->fallbackResponse['text'],
1421 - 'html' => $this->fallbackResponse['html'] ?? '',
1422 - 'session_id' => $session_id,
1423 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1424 - ];
1425 -
1426 - if ($testing_data !== null) {
1427 - $response_data['testing_data'] = $testing_data;
1428 - }
1429 -
1430 - // Save the mode switch message
1431 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1432 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1433 -
1434 - // Send response and exit
1435 - wp_send_json($response_data);
1436 - wp_die();
1437 - } elseif (!$intent_matched) {
1438 - // No intent matched, handle live agent message
1439 - try {
1440 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1441 -
1442 - $agent_response = [
1443 - 'status' => 'waiting_for_agent',
1444 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1445 - ];
1446 -
1447 - if ($testing_data !== null) {
1448 - $agent_response['testing_data'] = $testing_data;
1449 - }
1450 -
1451 - wp_send_json_success($agent_response);
1452 - } catch (\Exception $e) {
1453 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1454 - }
1455 - wp_die();
1456 - }
1457 - }
1458 -
1459 - // Step 1: Check for new PDF URL in the message
1460 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1461 - $new_pdf_url = $matches[0];
1462 -
1463 - // Check if this is likely a PDF-related request
1464 - $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1465 - $is_pdf_request = false;
1466 -
1467 - foreach ($pdf_keywords as $keyword) {
1468 - if (stripos($message, $keyword) !== false) {
1469 - $is_pdf_request = true;
1470 - break;
1471 - }
1472 - }
1473 -
1474 - // If it looks like a PDF request or we're waiting for a PDF URL
1475 - if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1476 - // Validate HTTPS
1477 - if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1478 - // Extract filename from URL
1479 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1480 -
1481 - // Clear previous PDF transients
1482 - $this->clear_pdf_transients($session_id);
1483 -
1484 - // Process new PDF using current_options
1485 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
1486 - $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1487 -
1488 - if ($embeddings === 'too_many_pages') {
1489 - $error_text = sprintf(
1490 - $current_options['pdf_intent_error_text'] ??
1491 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1492 - $max_pages
1493 - );
1494 - $this->fallbackResponse['text'] = $error_text;
1495 - } elseif ($embeddings) {
1496 - // Store new PDF information
1497 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1498 -
1499 - // If the filename is generic, create a more descriptive one
1500 - if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1501 - strpos($pdf_filename, '.php') !== false) {
1502 - $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1503 - }
1504 -
1505 - set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1506 - set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1507 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1508 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1509 -
1510 - $success_text = $current_options['pdf_intent_success_text'] ??
1511 - esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1512 -
1513 - $pdf_response = [
1514 - 'success' => true,
1515 - 'message' => $success_text,
1516 - 'data' => [
1517 - 'filename' => $pdf_filename
1518 - ]
1519 - ];
1520 -
1521 - if ($testing_data !== null) {
1522 - $pdf_response['testing_data'] = $testing_data;
1523 - }
1524 -
1525 - wp_send_json($pdf_response);
1526 - wp_die();
1527 - } else {
1528 - $error_text = $current_options['pdf_intent_error_text'] ??
1529 - esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1530 - $this->fallbackResponse['text'] = $error_text;
1531 - }
1532 -
1533 - $pdf_error_response = [
1534 - 'success' => false,
1535 - 'message' => $this->fallbackResponse['text']
1536 - ];
1537 -
1538 - if ($testing_data !== null) {
1539 - $pdf_error_response['testing_data'] = $testing_data;
1540 - }
1541 -
1542 - wp_send_json($pdf_error_response);
1543 - wp_die();
1544 - }
1545 - }
1546 - }
1547 -
1548 -
1549 - // Step 2: Detect intent and handle intent-based responses
1550 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1551 -
1552 - // Capture action analysis for testing panel after intent check
1553 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1554 - $testing_data['action_matches'] = $this->last_action_analysis;
1555 - }
1556 -
1557 - // Step 3: Handle the intent result appropriately
1558 - if ($intent_result !== false) {
1559 - // Intent was matched - ALWAYS send as JSON response, never streaming
1560 -
1561 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1562 - // Intent returned a direct response array
1563 - $response_data = [
1564 - 'text' => $intent_result['text'] ?? '',
1565 - 'html' => $intent_result['html'] ?? '',
1566 - 'session_id' => $session_id
1567 - ];
1568 -
1569 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1570 - if (isset($intent_result['chat_mode'])) {
1571 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1572 - }
1573 -
1574 - if ($testing_data !== null) {
1575 - $response_data['testing_data'] = $testing_data;
1576 - }
1577 -
1578 - wp_send_json($response_data);
1579 - wp_die();
1580 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1581 - // Intent returned true and set fallbackResponse
1582 -
1583 - // SAVE TO TRANSCRIPT
1584 - if (!empty($this->fallbackResponse['text'])) {
1585 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1586 - }
1587 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1588 - if (!empty($this->fallbackResponse['html'])) {
1589 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1590 - }
1591 -
1592 - $response_data = [
1593 - 'text' => $this->fallbackResponse['text'] ?? '',
1594 - 'html' => $this->fallbackResponse['html'] ?? '',
1595 - 'session_id' => $session_id
1596 - ];
1597 -
1598 - if (isset($this->fallbackResponse['chat_mode'])) {
1599 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1600 - }
1601 -
1602 - if ($testing_data !== null) {
1603 - $response_data['testing_data'] = $testing_data;
1604 - }
1605 -
1606 - wp_send_json($response_data);
1607 - wp_die();
1608 - }
1609 - }
1610 -
1611 - // If we get here, no intent matched OR the intent didn't provide a usable response
1612 -
1613 - // Step 4: Generate AI response
1614 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1615 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
1616 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
1617 - $this->mxchat_increment_chat_count();
1618 -
1619 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1620 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
1621 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
1622 -
1623 - // Check if the embedding generation returned an error
1624 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1625 - $error_message = $user_message_embedding['error'];
1626 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
1627 -
1628 - // FIXED: Send error in appropriate format based on streaming mode
1629 - if ($is_streaming) {
1630 - echo "data: " . json_encode([
1631 - 'error' => true,
1632 - 'error_message' => $error_message,
1633 - 'error_code' => $error_code,
1634 - 'text' => $error_message,
1635 - 'message' => $error_message
1636 - ]) . "\n\n";
1637 - echo "data: [DONE]\n\n";
1638 - flush();
1639 - } else {
1640 - wp_send_json_error([
1641 - 'error_message' => $error_message,
1642 - 'error_code' => $error_code
1643 - ]);
1644 - }
1645 - wp_die();
1646 - }
1647 -
1648 - // Check if the embedding is valid
1649 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
1650 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
1651 -
1652 - // FIXED: Send error in appropriate format based on streaming mode
1653 - if ($is_streaming) {
1654 - echo "data: " . json_encode([
1655 - 'error' => true,
1656 - 'error_message' => $error_message,
1657 - 'error_code' => 'invalid_embedding',
1658 - 'text' => $error_message,
1659 - 'message' => $error_message
1660 - ]) . "\n\n";
1661 - echo "data: [DONE]\n\n";
1662 - flush();
1663 - } else {
1664 - wp_send_json_error([
1665 - 'error_message' => $error_message,
1666 - 'error_code' => 'invalid_embedding'
1667 - ]);
1668 - }
1669 - wp_die();
1670 - }
1671 -
1672 - // Build context with both knowledge base and PDF content if available
1673 - $context_content = "User asked: '{$message}'\n\n";
1674 -
1675 - // Add action instruction if present (add this right after the above line)
1676 - if (!empty($this->current_action_instruction)) {
1677 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
1678 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
1679 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
1680 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
1681 -
1682 - // Clear the instruction after using it
1683 - $this->current_action_instruction = null;
1684 - }
1685 -
1686 -
1687 - // Add page context if available and contextual awareness is enabled using current_options
1688 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
1689 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
1690 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
1691 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
1692 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
1693 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
1694 - }
1695 -
1696 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
1697 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
1698 -
1699 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
1700 - // Use fresh options to ensure we get the latest setting value
1701 - $fresh_options = get_option('mxchat_options', []);
1702 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
1703 -
1704 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
1705 - if ($citation_links_enabled && !empty($system_instructions)) {
1706 - preg_match_all(
1707 - '#\bhttps?://[^\s<>"\']+#i',
1708 - $system_instructions,
1709 - $system_instruction_urls
1710 - );
1711 -
1712 - if (!empty($system_instruction_urls[0])) {
1713 - // Merge with existing valid URLs
1714 - $this->current_valid_urls = array_merge(
1715 - $this->current_valid_urls,
1716 - $system_instruction_urls[0]
1717 - );
1718 - // Remove duplicates
1719 - $this->current_valid_urls = array_unique($this->current_valid_urls);
1720 -
1721 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
1722 - }
1723 - }
1724 -
1725 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
1726 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
1727 - // Update testing data with the REAL similarity analysis
1728 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
1729 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1730 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
1731 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
1732 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
1733 -}
1734 -// ===== END SIMILARITY DATA CAPTURE =====
1735 -
1736 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
1737 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
1738 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
1739 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
1740 -}
1741 -
1742 - if (!empty($relevant_content)) {
1743 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
1744 - } else {
1745 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
1746 - }
1747 -
1748 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
1749 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
1750 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
1751 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
1752 - foreach ($this->current_valid_urls as $url) {
1753 - $context_content .= "- " . $url . "\n";
1754 - }
1755 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
1756 - $context_content .= "===== END APPROVED URLS =====\n\n";
1757 - }
1758 -
1759 - // Check for and include PDF content
1760 - $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
1761 - $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
1762 - $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
1763 - if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
1764 - $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
1765 - if (!empty($relevant_pdf_pages)) {
1766 - $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
1767 - foreach ($relevant_pdf_pages as $page_data) {
1768 - $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
1769 - }
1770 - $context_content .= "\n";
1771 - }
1772 - }
1773 -
1774 - // Check for and include Word content
1775 - $word_url = get_transient('mxchat_word_url_' . $session_id);
1776 - $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
1777 - $word_filename = get_transient('mxchat_word_filename_' . $session_id);
1778 - if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
1779 - $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
1780 - if (!empty($relevant_word_chunks)) {
1781 - $context_content .= "Relevant content from Word document '{$word_filename}':\n";
1782 - foreach ($relevant_word_chunks as $chunk_data) {
1783 - $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
1784 - }
1785 - $context_content .= "\n";
1786 - }
1787 - }
1788 -
1789 - $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
1790 -
1791 - // Extract model from current options for bot-specific model support
1792 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
1793 -
1794 - $response = $this->mxchat_generate_response(
1795 - $context_content,
1796 - $current_options['api_key'] ?? $this->options['api_key'],
1797 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
1798 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
1799 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
1800 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
1801 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
1802 - $conversation_history,
1803 - $is_streaming,
1804 - $session_id,
1805 - $testing_data,
1806 - $selected_model
1807 - );
1808 -
1809 - // Handle streaming vs non-streaming responses
1810 - if ($is_streaming) {
1811 - // Check if streaming actually happened or if it fell back to regular response
1812 - if ($response === true) {
1813 - wp_die();
1814 - }
1815 - // If we get here, streaming fell back to regular response, continue
1816 - // But if there's an error, we need to send it as SSE format since headers are already set
1817 - if (is_array($response) && isset($response['error'])) {
1818 - $error_message = $response['error'];
1819 - $error_code = $response['error_code'] ?? 'api_error';
1820 - // Send error in SSE format that the client JS can handle
1821 - echo "data: " . json_encode([
1822 - 'error' => true,
1823 - 'error_message' => $error_message,
1824 - 'error_code' => $error_code,
1825 - 'text' => $error_message, // Also include as text for fallback handling
1826 - 'message' => $error_message
1827 - ]) . "\n\n";
1828 - echo "data: [DONE]\n\n";
1829 - flush();
1830 - wp_die();
1831 - }
1832 - }
1833 -
1834 - // Check if the response is an error array (non-streaming mode)
1835 - if (is_array($response) && isset($response['error'])) {
1836 - wp_send_json_error([
1837 - 'error_message' => $response['error'],
1838 - 'error_code' => $response['error_code'] ?? 'api_error'
1839 - ]);
1840 - wp_die();
1841 - }
1842 -
1843 - // DEBUG: Check what we have
1844 - //error_log("=== BEFORE URL VALIDATION ===");
1845 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
1846 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
1847 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
1848 -
1849 - // If we get here, the response is valid text - now validate URLs
1850 - if (!empty($this->current_valid_urls)) {
1851 - //error_log("CALLING validate_and_clean_urls");
1852 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
1853 - } else {
1854 - //error_log("SKIPPING validation - current_valid_urls is empty");
1855 - }
1856 - // ===== END URL VALIDATION =====
1857 -
1858 - // Prepare RAG context data for storage (only include documents used for context)
1859 - $rag_context_for_storage = null;
1860 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
1861 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
1862 -
1863 - if ($has_rag_data || $has_action_data) {
1864 - $rag_context_for_storage = [];
1865 -
1866 - // Add RAG/source data if available
1867 - if ($has_rag_data) {
1868 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
1869 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
1870 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
1871 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
1872 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
1873 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
1874 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
1875 - }
1876 -
1877 - // Add action analysis data if available
1878 - if ($has_action_data) {
1879 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
1880 - }
1881 - }
1882 -
1883 - // Save the cleaned response with RAG context
1884 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
1885 -
1886 - // Step 5: Save additional content if available
1887 - if (!empty($this->productCardHtml)) {
1888 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
1889 - }
1890 -
1891 - if (!empty($this->fallbackResponse['html'])) {
1892 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1893 - }
1894 -
1895 - // Step 6: Return the response
1896 - // DEBUG: Check if newlines exist in the response
1897 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
1898 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
1899 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
1900 -
1901 - $response_data = [
1902 - 'text' => $response,
1903 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1904 - 'session_id' => $session_id
1905 - ];
1906 -
1907 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
1908 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
1909 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
1910 - }
1911 -
1912 - // Also pass it as a top-level field so JS can show a better error message to admins
1913 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
1914 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
1915 - }
1916 -
1917 - // Always add testing data for admins (no toggle needed)
1918 - if ($testing_data !== null) {
1919 - $response_data['testing_data'] = $testing_data;
1920 - }
1921 -
1922 - wp_send_json($response_data);
1923 - wp_die();
1924 -}
1925 -
1926 -/**
1927 - * Get bot-specific options for multi-bot functionality
1928 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
1929 - */
1930 -// Also debug the bot options retrieval
1931 -private function get_bot_options($bot_id = 'default') {
1932 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
1933 -
1934 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
1935 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
1936 - return array();
1937 - }
1938 -
1939 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
1940 -
1941 - if (!empty($bot_options)) {
1942 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
1943 - if (isset($bot_options['similarity_threshold'])) {
1944 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
1945 - }
1946 - }
1947 -
1948 - return is_array($bot_options) ? $bot_options : array();
1949 -}
1950 -
1951 -/**
1952 - * Get bot-specific Pinecone configuration
1953 - * Used in the knowledge retrieval functions
1954 - */
1955 -// Also add debugging to your get_bot_pinecone_config function
1956 -private function get_bot_pinecone_config($bot_id = 'default') {
1957 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
1958 -
1959 - // If default bot or multi-bot add-on not active, use default Pinecone config
1960 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
1961 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
1962 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
1963 - $config = array(
1964 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
1965 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
1966 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
1967 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
1968 - );
1969 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
1970 - return $config;
1971 - }
1972 -
1973 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
1974 -
1975 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
1976 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
1977 -
1978 - if (!empty($bot_pinecone_config)) {
1979 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
1980 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
1981 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
1982 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
1983 - } else {
1984 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
1985 - }
1986 -
1987 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
1988 -}
1989 -
1990 -
1991 -// Updated function to check intents and invoke the callback function
1992 -private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
1993 - global $wpdb;
1994 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1995 -
1996 - // Get the current bot_id
1997 - $current_bot_id = $this->get_current_bot_id($session_id);
1998 -
1999 - // Generate the user embedding
2000 - $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2001 -
2002 - // Check if embedding generation returned an error
2003 - if (is_array($user_embedding) && isset($user_embedding['error'])) {
2004 - $error_message = $user_embedding['error'];
2005 - $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2006 -
2007 - // FIXED: Send error in appropriate format based on streaming mode
2008 - if ($this->is_streaming) {
2009 - echo "data: " . json_encode([
2010 - 'error' => true,
2011 - 'error_message' => $error_message,
2012 - 'error_code' => $error_code,
2013 - 'text' => $error_message,
2014 - 'message' => $error_message
2015 - ]) . "\n\n";
2016 - echo "data: [DONE]\n\n";
2017 - flush();
2018 - } else {
2019 - wp_send_json_error([
2020 - 'error_message' => $error_message,
2021 - 'error_code' => $error_code
2022 - ]);
2023 - }
2024 - wp_die();
2025 - }
2026 -
2027 - // Check if embedding is valid
2028 - if (!is_array($user_embedding) || empty($user_embedding)) {
2029 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2030 -
2031 - // FIXED: Send error in appropriate format based on streaming mode
2032 - if ($this->is_streaming) {
2033 - echo "data: " . json_encode([
2034 - 'error' => true,
2035 - 'error_message' => $error_message,
2036 - 'error_code' => 'invalid_embedding',
2037 - 'text' => $error_message,
2038 - 'message' => $error_message
2039 - ]) . "\n\n";
2040 - echo "data: [DONE]\n\n";
2041 - flush();
2042 - } else {
2043 - wp_send_json_error([
2044 - 'error_message' => $error_message,
2045 - 'error_code' => 'invalid_embedding'
2046 - ]);
2047 - }
2048 - wp_die();
2049 - }
2050 -
2051 - // Fetch intents from the database
2052 - $table_name = $wpdb->prefix . 'mxchat_intents';
2053 - if ($chat_mode === 'agent') {
2054 - $query = $wpdb->prepare(
2055 - "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2056 - 'mxchat_handle_switch_to_chatbot_intent'
2057 - );
2058 - $intents = $wpdb->get_results($query);
2059 - } else {
2060 - $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2061 - }
2062 -
2063 - if (empty($intents)) {
2064 - return false;
2065 - }
2066 -
2067 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2068 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2069 - $phrases_by_intent = [];
2070 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2071 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2072 - foreach ($all_phrases as $p) {
2073 - $phrases_by_intent[$p->intent_id][] = $p;
2074 - }
2075 - }
2076 -
2077 - $highest_similarity = -INF;
2078 - $matched_intent = null;
2079 -
2080 - // Array to store action analysis for testing panel
2081 - $action_analysis = [];
2082 -
2083 - foreach ($intents as $intent) {
2084 - // Additional check for enabled state
2085 - $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2086 - if (!$is_enabled) {
2087 - continue;
2088 - }
2089 -
2090 - // Check if this action is enabled for the current bot
2091 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2092 - continue;
2093 - }
2094 -
2095 - $best_similarity = -INF;
2096 - $matched_phrase_text = '';
2097 -
2098 - // Check legacy embedding vector (existing behavior)
2099 - $intent_embedding_serialized = $intent->embedding_vector;
2100 - $intent_embedding = $intent_embedding_serialized
2101 - ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2102 - : null;
2103 -
2104 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2105 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2106 - if ($legacy_similarity > $best_similarity) {
2107 - $best_similarity = $legacy_similarity;
2108 - $matched_phrase_text = 'legacy';
2109 - }
2110 - }
2111 -
2112 - // Check individual phrase vectors
2113 - if (isset($phrases_by_intent[$intent->id])) {
2114 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2115 - $phrase_embedding = $phrase_row->embedding_vector
2116 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2117 - : null;
2118 - if (!is_array($phrase_embedding)) {
2119 - continue;
2120 - }
2121 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2122 - if ($phrase_similarity > $best_similarity) {
2123 - $best_similarity = $phrase_similarity;
2124 - $matched_phrase_text = $phrase_row->phrase;
2125 - }
2126 - }
2127 - }
2128 -
2129 - // Skip if no valid embedding was found at all
2130 - if ($best_similarity === -INF) {
2131 - continue;
2132 - }
2133 -
2134 - $similarity = $best_similarity;
2135 - $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2136 -
2137 - // Store action analysis data for testing panel
2138 - $action_analysis[] = [
2139 - 'intent_label' => $intent->intent_label,
2140 - 'callback_function' => $intent->callback_function,
2141 - 'similarity' => round($similarity, 4),
2142 - 'similarity_percentage' => round($similarity * 100, 2),
2143 - 'threshold' => $intent_threshold,
2144 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2145 - 'above_threshold' => $similarity >= $intent_threshold,
2146 - 'matched_phrase' => $matched_phrase_text,
2147 - 'triggered' => false // Will be updated below if this intent is triggered
2148 - ];
2149 -
2150 - if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2151 - $highest_similarity = $similarity;
2152 - $matched_intent = $intent;
2153 - }
2154 - }
2155 -
2156 - // Mark the triggered action if any
2157 - if ($matched_intent) {
2158 - foreach ($action_analysis as &$action) {
2159 - if ($action['intent_label'] === $matched_intent->intent_label) {
2160 - $action['triggered'] = true;
2161 - break;
2162 - }
2163 - }
2164 - }
2165 -
2166 - // Sort actions by similarity (highest first) and store for testing panel
2167 - usort($action_analysis, function($a, $b) {
2168 - return $b['similarity'] <=> $a['similarity'];
2169 - });
2170 -
2171 - // Store action analysis for testing panel capture
2172 - $this->last_action_analysis = $action_analysis;
2173 -
2174 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2175 - if ($matched_intent) {
2176 - // If the callback is a method on this instance (core callback), call it directly
2177 - if (method_exists($this, $matched_intent->callback_function)) {
2178 - $callback_result = call_user_func(
2179 - [$this, $matched_intent->callback_function],
2180 - $message,
2181 - $user_id,
2182 - $session_id,
2183 - $matched_intent,
2184 - $user_context ?? null
2185 - );
2186 - } else {
2187 - // Otherwise, use apply_filters for add-on callbacks
2188 - $callback_result = apply_filters(
2189 - $matched_intent->callback_function,
2190 - false,
2191 - $message,
2192 - $user_id,
2193 - $session_id,
2194 - $matched_intent
2195 - );
2196 - }
2197 -
2198 - // Handle the callback result properly
2199 - if ($callback_result !== false) {
2200 - // If callback returned an array with chat_mode, use it directly
2201 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2202 - $this->fallbackResponse = $callback_result;
2203 - return $callback_result; // Return the full array
2204 - } else {
2205 - $this->fallbackResponse = $callback_result;
2206 - return true;
2207 - }
2208 - }
2209 - }
2210 -
2211 - return false;
2212 -}
2213 -
2214 -/**
2215 - * Check if an action is enabled for a specific bot
2216 - */
2217 -private function is_action_enabled_for_bot($intent, $bot_id) {
2218 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2219 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2220 - return true;
2221 - }
2222 -
2223 - $enabled_bots = json_decode($intent->enabled_bots, true);
2224 -
2225 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2226 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2227 - return true;
2228 - }
2229 -
2230 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2231 - // default-bot actions are testable from the admin panel
2232 - if ($bot_id === 'testing') {
2233 - $bot_id = 'default';
2234 - }
2235 -
2236 - // Check if the current bot is in the enabled bots list
2237 - return in_array($bot_id, $enabled_bots);
2238 -}
2239 -
2240 -// Helper function to clear PDF and Word document related transients
2241 -private function clear_pdf_transients($session_id) {
2242 - // PDF transients
2243 - delete_transient('mxchat_pdf_url_' . $session_id);
2244 - delete_transient('mxchat_pdf_embeddings_' . $session_id);
2245 - delete_transient('mxchat_include_pdf_in_context_' . $session_id);
2246 - delete_transient('mxchat_waiting_for_pdf_url_' . $session_id);
2247 -
2248 - // Word document transients
2249 - delete_transient('mxchat_word_url_' . $session_id);
2250 - delete_transient('mxchat_word_filename_' . $session_id);
2251 - delete_transient('mxchat_word_embeddings_' . $session_id);
2252 - delete_transient('mxchat_include_word_in_context_' . $session_id);
2253 - delete_transient('mxchat_waiting_for_word_' . $session_id);
2254 -}
2255 -
2256 -
2257 -
2258 -//verified good
2259 -public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2260 - // Get the user's original instruction/message
2261 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2262 -
2263 - // Set instruction for AI - just pass along what the user wanted to say
2264 - $this->current_action_instruction = $user_instruction;
2265 -
2266 - // Set the transient to track email capture flow
2267 - set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2268 -
2269 - // Return false to let the AI generate the response
2270 - return false;
2271 -}
2272 -
2273 -public function mxchat_generate_image($message, $user_id, $session_id) {
2274 - //error_log("Starting image generation for message: " . $message);
2275 -
2276 - // Prepare a prompt for OpenAI image generation
2277 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2278 -
2279 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2280 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2281 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2282 - $image_response = $this->mxchat_generate_custom_image($prompt);
2283 - } else {
2284 - // Use the existing OpenAI API key
2285 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2286 - // Call OpenAI GPT Image to generate an image
2287 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2288 - }
2289 -
2290 - // Check if the response contains an image URL
2291 - if (isset($image_response['imageUrl'])) {
2292 - $image_url = esc_url_raw($image_response['imageUrl']);
2293 -
2294 - // Construct the HTML with a CSS class instead of inline styles
2295 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2296 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2297 -
2298 - // Save the bot message with both text and HTML
2299 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2300 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2301 -
2302 - // Set the fallback response for the chat handler
2303 - $this->fallbackResponse = [
2304 - 'text' => $response_text,
2305 - 'html' => $response_html,
2306 - 'images' => [$image_url]
2307 - ];
2308 -
2309 - // For debugging/verification - Use json_encode to verify what's being set
2310 - //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
2311 -
2312 - // Return the response directly instead of relying on the property
2313 - return $this->fallbackResponse;
2314 - } else {
2315 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2316 -
2317 - // Save the error message
2318 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2319 -
2320 - // Set the fallback response for the chat handler
2321 - $this->fallbackResponse = [
2322 - 'text' => $response_text,
2323 - 'html' => '',
2324 - 'images' => []
2325 - ];
2326 -
2327 - //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2328 - //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
2329 -
2330 - // Return the response directly instead of relying on the property
2331 - return $this->fallbackResponse;
2332 - }
2333 -}
2334 -
2335 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2336 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2337 -
2338 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2339 - if (empty($gemini_api_key)) {
2340 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2341 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2342 - return ['text' => $response_text, 'html' => '', 'images' => []];
2343 - }
2344 -
2345 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2346 -
2347 - if (isset($image_response['imageUrl'])) {
2348 - $image_url = esc_url_raw($image_response['imageUrl']);
2349 -
2350 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2351 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2352 -
2353 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2354 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2355 -
2356 - $this->fallbackResponse = [
2357 - 'text' => $response_text,
2358 - 'html' => $response_html,
2359 - 'images' => [$image_url]
2360 - ];
2361 -
2362 - return $this->fallbackResponse;
2363 - } else {
2364 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2365 -
2366 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2367 -
2368 - $this->fallbackResponse = [
2369 - 'text' => $response_text,
2370 - 'html' => '',
2371 - 'images' => []
2372 - ];
2373 -
2374 - return $this->fallbackResponse;
2375 - }
2376 -}
2377 -
2378 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2379 - $extension = ($mime_type === 'image/jpeg') ? 'jpg' : 'png';
2380 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2381 - $decoded = base64_decode($base64_data);
2382 -
2383 - if ($decoded === false) {
2384 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2385 - }
2386 -
2387 - $upload = wp_upload_bits($filename, null, $decoded);
2388 -
2389 - if (!empty($upload['error'])) {
2390 - return new \WP_Error('upload_failed', $upload['error']);
2391 - }
2392 -
2393 - $attach_id = wp_insert_attachment([
2394 - 'post_mime_type' => $mime_type,
2395 - 'post_title' => $prefix,
2396 - 'post_content' => '',
2397 - 'post_status' => 'inherit',
2398 - ], $upload['file']);
2399 -
2400 - if (is_wp_error($attach_id)) {
2401 - return $attach_id;
2402 - }
2403 -
2404 - require_once ABSPATH . 'wp-admin/includes/image.php';
2405 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2406 - wp_update_attachment_metadata($attach_id, $metadata);
2407 -
2408 - return esc_url_raw(wp_get_attachment_url($attach_id));
2409 -}
2410 -
2411 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
2412 - $api_url = 'https://api.openai.com/v1/images/generations';
2413 - $body = json_encode([
2414 - 'prompt' => sanitize_text_field($prompt),
2415 - 'n' => 1,
2416 - 'size' => '1024x1024',
2417 - 'quality' => 'medium',
2418 - 'output_format' => 'png',
2419 - 'model' => sanitize_text_field($model),
2420 - ]);
2421 -
2422 - $args = [
2423 - 'body' => $body,
2424 - 'headers' => [
2425 - 'Content-Type' => 'application/json',
2426 - 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2427 - ],
2428 - 'method' => 'POST',
2429 - 'timeout' => absint($timeout),
2430 - ];
2431 -
2432 - $response = wp_remote_post($api_url, $args);
2433 -
2434 - if (is_wp_error($response)) {
2435 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2436 - }
2437 -
2438 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2439 -
2440 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2441 - if ($b64) {
2442 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2443 - if (is_wp_error($saved_url)) {
2444 - return ['error' => $saved_url->get_error_message()];
2445 - }
2446 - return ['imageUrl' => $saved_url];
2447 - } else {
2448 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2449 - }
2450 -}
2451 -
2452 -/**
2453 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2454 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2455 - */
2456 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2457 - $cfg = $this->mxchat_resolve_custom_provider();
2458 - if (empty($cfg['base_url'])) {
2459 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2460 - }
2461 - $url = $cfg['base_url'] . '/images/generations';
2462 - if (!empty($cfg['api_version'])) {
2463 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2464 - }
2465 - $body = wp_json_encode([
2466 - 'prompt' => sanitize_text_field($prompt),
2467 - 'n' => 1,
2468 - 'size' => '1024x1024',
2469 - 'model' => $cfg['model'],
2470 - ]);
2471 - $response = wp_remote_post($url, [
2472 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2473 - 'body' => $body,
2474 - 'method' => 'POST',
2475 - 'timeout' => absint($timeout),
2476 - ]);
2477 - if (is_wp_error($response)) {
2478 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2479 - }
2480 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2481 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2482 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2483 - if ($b64) {
2484 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2485 - if (is_wp_error($saved)) {
2486 - return ['error' => $saved->get_error_message()];
2487 - }
2488 - return ['imageUrl' => $saved];
2489 - }
2490 - $remote_url = $resp['data'][0]['url'] ?? null;
2491 - if ($remote_url) {
2492 - return ['imageUrl' => esc_url_raw($remote_url)];
2493 - }
2494 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2495 - return ['error' => esc_html($err_msg)];
2496 -}
2497 -
2498 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2499 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2500 -
2501 - $body = json_encode([
2502 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2503 - 'parameters' => [
2504 - 'sampleCount' => 1,
2505 - 'aspectRatio' => '1:1',
2506 - ],
2507 - ]);
2508 -
2509 - $args = [
2510 - 'body' => $body,
2511 - 'headers' => [
2512 - 'Content-Type' => 'application/json',
2513 - 'x-goog-api-key' => sanitize_text_field($api_key),
2514 - ],
2515 - 'method' => 'POST',
2516 - 'timeout' => absint($timeout),
2517 - ];
2518 -
2519 - $response = wp_remote_post($api_url, $args);
2520 -
2521 - if (is_wp_error($response)) {
2522 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2523 - }
2524 -
2525 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2526 -
2527 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2528 - if ($b64) {
2529 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2530 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2531 - if (is_wp_error($saved_url)) {
2532 - return ['error' => $saved_url->get_error_message()];
2533 - }
2534 - return ['imageUrl' => $saved_url];
2535 - } else {
2536 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2537 - }
2538 -}
2539 -
2540 -/**
2541 - * Handle web search requests.
2542 - *
2543 - * Sends the refined search query to the Brave Search API and uses the
2544 - * results to generate a conversational response with the AI model.
2545 - *
2546 - * @since 1.0.0
2547 - * @param string $message The user's search query.
2548 - * @param string $user_id The user identifier.
2549 - * @param string $session_id The current session ID.
2550 - * @return array Response array containing text with embedded HTML links
2551 - */
2552 -public function mxchat_handle_search_request($message, $user_id, $session_id) {
2553 - // Step 1: Interpret and refine the search query
2554 - $refined_search_query = $this->mxchat_interpret_search_query($message);
2555 - if (empty($refined_search_query)) {
2556 - return array(
2557 - 'text' => esc_html__('I apologize, but could you please rephrase your search request?', 'mxchat'),
2558 - 'html' => ''
2559 - );
2560 - }
2561 -
2562 - // Retrieve and validate API settings
2563 - $options = get_option('mxchat_options');
2564 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
2565 - $results_count = isset($options['brave_results_count']) ? absint($options['brave_results_count']) : 5;
2566 -
2567 - if (empty($api_key)) {
2568 - return array(
2569 - 'text' => esc_html__('Search functionality is temporarily unavailable. Please try again later.', 'mxchat'),
2570 - 'html' => ''
2571 - );
2572 - }
2573 -
2574 - // Build the API request URL
2575 - $api_url = add_query_arg(
2576 - array(
2577 - 'q' => rawurlencode($refined_search_query),
2578 - 'count' => $results_count,
2579 - 'text_decorations' => 'true',
2580 - 'rich_data' => 'true',
2581 - ),
2582 - 'https://api.search.brave.com/res/v1/web/search'
2583 - );
2584 -
2585 - // Attempt to retrieve cached results first
2586 - $transient_key = 'mxchat_search_' . md5($refined_search_query);
2587 - $results = get_transient($transient_key);
2588 -
2589 - if (false === $results) {
2590 - // SECURITY FIX: Changed to wp_safe_remote_get
2591 - $response = wp_safe_remote_get(
2592 - $api_url,
2593 - array(
2594 - 'headers' => array(
2595 - 'Accept' => 'application/json',
2596 - 'Accept-Encoding' => 'gzip',
2597 - 'X-Subscription-Token'=> $api_key,
2598 - ),
2599 - 'timeout' => 10,
2600 - )
2601 - );
2602 -
2603 - if (is_wp_error($response)) {
2604 - return array(
2605 - 'text' => esc_html__('I encountered an error while searching. Please try again.', 'mxchat'),
2606 - 'html' => ''
2607 - );
2608 - }
2609 -
2610 - $results = json_decode(wp_remote_retrieve_body($response), true);
2611 -
2612 - if (json_last_error() !== JSON_ERROR_NONE) {
2613 - return array(
2614 - 'text' => esc_html__('I received an invalid response from the search service.', 'mxchat'),
2615 - 'html' => ''
2616 - );
2617 - }
2618 -
2619 - // Cache results for one hour
2620 - set_transient($transient_key, $results, HOUR_IN_SECONDS);
2621 - }
2622 -
2623 - // Process results
2624 - if (!empty($results['web']['results']) && is_array($results['web']['results'])) {
2625 - // Create a more straightforward summary with HTML links
2626 - $search_results_text = '';
2627 -
2628 - // Add a simple intro
2629 - $search_results_text .= sprintf(
2630 - esc_html__("Here's what I found about '%s':", 'mxchat'),
2631 - esc_html($refined_search_query)
2632 - );
2633 -
2634 - // Add the top results with HTML links
2635 - foreach (array_slice($results['web']['results'], 0, 5) as $result) {
2636 - $title = isset($result['title']) ? wp_strip_all_tags($result['title']) : '';
2637 - $url = isset($result['url']) ? esc_url($result['url']) : '';
2638 - $description = isset($result['description']) ? wp_strip_all_tags($result['description']) : '';
2639 -
2640 - // Add a line break after the intro
2641 - $search_results_text .= '<br><br>';
2642 -
2643 - // Add title as a link
2644 - $search_results_text .= sprintf(
2645 - '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a><br>',
2646 - $url,
2647 - $title
2648 - );
2649 -
2650 - // Add a condensed description
2651 - $search_results_text .= sprintf("%s", $description);
2652 - }
2653 -
2654 - // Save to chat history
2655 - $this->mxchat_save_chat_message($session_id, 'bot', $search_results_text);
2656 -
2657 - // Return the formatted text with embedded HTML links
2658 - return array(
2659 - 'text' => $search_results_text,
2660 - 'html' => ''
2661 - );
2662 - } else {
2663 - return array(
2664 - 'text' => sprintf(
2665 - esc_html__('I searched for "%s" but couldn\'t find any relevant results. Would you like to try different search terms?', 'mxchat'),
2666 - esc_html($refined_search_query)
2667 - ),
2668 - 'html' => ''
2669 - );
2670 - }
2671 -}
2672 -
2673 -//very good
2674 -/**
2675 - * Handle image search requests from the chatbot
2676 - *
2677 - * @param string $message The user's search query
2678 - * @param int $user_id The user's ID
2679 - * @param string $session_id The chat session ID
2680 - * @return array Response array with text and HTML content
2681 - */
2682 -public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
2683 - // Step 1: Interpret the search query using the user's selected AI model
2684 - $refined_search_query = $this->mxchat_interpret_search_query($message);
2685 -
2686 - // If no query was interpreted, return a fallback message
2687 - if (empty($refined_search_query)) {
2688 - return array(
2689 - 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
2690 - 'html' => "",
2691 - );
2692 - }
2693 -
2694 - // Brave API URL
2695 - $api_url = 'https://api.search.brave.com/res/v1/images/search';
2696 -
2697 - // Retrieve Brave API settings
2698 - $options = get_option('mxchat_options');
2699 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
2700 -
2701 - if (empty($api_key)) {
2702 - return array(
2703 - 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
2704 - 'html' => "",
2705 - );
2706 - }
2707 -
2708 - $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
2709 - $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
2710 -
2711 - // Append query parameters based on settings
2712 - $api_url = add_query_arg([
2713 - 'q' => rawurlencode($refined_search_query),
2714 - 'count' => $image_count,
2715 - 'safesearch' => $safe_search,
2716 - ], $api_url);
2717 -
2718 - // Implement caching
2719 - $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
2720 - $body = get_transient($transient_key);
2721 -
2722 - if (false === $body) {
2723 - $args = [
2724 - 'headers' => [
2725 - 'Accept' => 'application/json',
2726 - 'Accept-Encoding' => 'gzip',
2727 - 'X-Subscription-Token' => $api_key,
2728 - ],
2729 - 'timeout' => 10,
2730 - ];
2731 -
2732 - // SECURITY FIX: Changed to wp_safe_remote_get
2733 - $response = wp_safe_remote_get($api_url, $args);
2734 -
2735 - if (is_wp_error($response)) {
2736 - return array(
2737 - 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
2738 - 'html' => "",
2739 - );
2740 - }
2741 -
2742 - $body = json_decode(wp_remote_retrieve_body($response), true);
2743 - set_transient($transient_key, $body, HOUR_IN_SECONDS);
2744 - }
2745 -
2746 - // Process the API response
2747 - if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
2748 - $html_output = '<div class="mxchat-image-gallery">';
2749 -
2750 - // Get the configured image count (1-6)
2751 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
2752 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
2753 -
2754 - // Use only the requested number of images
2755 - for ($i = 0; $i < $display_count; $i++) {
2756 - $image = $body['results'][$i];
2757 - $image_url = isset($image['url']) ? esc_url($image['url']) : '';
2758 - $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
2759 - $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
2760 -
2761 - if ($image_url && $thumbnail_url) {
2762 - $html_output .= '<div class="mxchat-image-item">';
2763 - $html_output .= '<strong class="mxchat-image-title">' . $title . '</strong>';
2764 - $html_output .= '<a href="' . $image_url . '" target="_blank" rel="noopener noreferrer" class="mxchat-image-link">';
2765 - $html_output .= '<img src="' . $thumbnail_url . '" alt="' . $title . '" class="mxchat-image-thumbnail">';
2766 - $html_output .= '</a></div>';
2767 - }
2768 - }
2769 -
2770 - $html_output .= '</div>';
2771 -
2772 - // Create response text
2773 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
2774 -
2775 - // Save both response text and HTML to chat history
2776 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2777 - $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
2778 -
2779 - // Return the combined response
2780 - return array(
2781 - 'text' => $response_text,
2782 - 'html' => $html_output,
2783 - );
2784 - } else {
2785 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
2786 -
2787 - // Save the error message to chat history
2788 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2789 -
2790 - return array(
2791 - 'text' => $response_text,
2792 - 'html' => "",
2793 - );
2794 - }
2795 -}
2796 -
2797 -/**
2798 - * Interpret the search query using the user's selected AI model
2799 - *
2800 - * @param string $user_query The original query from the user
2801 - * @return string The refined search query
2802 - */
2803 -public function mxchat_interpret_search_query($user_query) {
2804 - $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');
2805 -
2806 - // Get options and determine the selected model
2807 - $options = $this->options ?? get_option('mxchat_options');
2808 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
2809 -
2810 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
2811 - if ($selected_model === 'custom-provider') {
2812 - return $this->interpret_query_with_custom($user_query, $system_prompt);
2813 - }
2814 -
2815 - // Extract model prefix to determine the provider
2816 - $model_parts = explode('-', $selected_model);
2817 - $provider = strtolower($model_parts[0]);
2818 -
2819 - // Determine which API key to use based on the provider
2820 - switch ($provider) {
2821 - case 'gemini':
2822 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
2823 - if (empty($api_key)) {
2824 - return sanitize_text_field($user_query); // Default to original query if API key missing
2825 - }
2826 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
2827 -
2828 - case 'claude':
2829 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
2830 - if (empty($api_key)) {
2831 - return sanitize_text_field($user_query);
2832 - }
2833 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
2834 -
2835 - case 'grok':
2836 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
2837 - if (empty($api_key)) {
2838 - return sanitize_text_field($user_query);
2839 - }
2840 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
2841 -
2842 - case 'deepseek':
2843 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
2844 - if (empty($api_key)) {
2845 - return sanitize_text_field($user_query);
2846 - }
2847 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
2848 -
2849 - case 'gpt':
2850 - default:
2851 - // Default to OpenAI for custom models or unrecognized prefixes
2852 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
2853 - if (empty($api_key)) {
2854 - return sanitize_text_field($user_query);
2855 - }
2856 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
2857 - }
2858 -}
2859 -
2860 -/**
2861 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
2862 - * Uses the same base URL + auth scheme as the chat dispatcher.
2863 - */
2864 -private function interpret_query_with_custom($user_query, $system_prompt) {
2865 - $cfg = $this->mxchat_resolve_custom_provider();
2866 - if (empty($cfg['base_url'])) {
2867 - return sanitize_text_field($user_query);
2868 - }
2869 - $args = [
2870 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2871 - 'body' => wp_json_encode([
2872 - 'model' => $cfg['model'],
2873 - 'messages' => [
2874 - ['role' => 'system', 'content' => $system_prompt],
2875 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
2876 - ],
2877 - 'temperature' => 0.2,
2878 - 'max_tokens' => 20,
2879 - ]),
2880 - 'method' => 'POST',
2881 - 'timeout' => 15,
2882 - ];
2883 - $response = wp_remote_post($cfg['chat_url'], $args);
2884 - if (is_wp_error($response)) {
2885 - return sanitize_text_field($user_query);
2886 - }
2887 - $body = json_decode(wp_remote_retrieve_body($response), true);
2888 - return isset($body['choices'][0]['message']['content'])
2889 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
2890 - : sanitize_text_field($user_query);
2891 -}
2892 -
2893 -/**
2894 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
2895 - * into the assoc-array form wp_remote_post expects.
2896 - */
2897 -private function mxchat_custom_provider_assoc_headers($cfg) {
2898 - $headers = ['Content-Type' => 'application/json'];
2899 - if (!empty($cfg['api_key'])) {
2900 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
2901 - $headers['api-key'] = $cfg['api_key'];
2902 - } else {
2903 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
2904 - }
2905 - }
2906 - return $headers;
2907 -}
2908 -
2909 -/**
2910 - * Interpret query using OpenAI models
2911 - */
2912 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
2913 - $url = 'https://api.openai.com/v1/chat/completions';
2914 - $args = [
2915 - 'headers' => [
2916 - 'Authorization' => 'Bearer ' . $api_key,
2917 - 'Content-Type' => 'application/json',
2918 - ],
2919 - 'body' => wp_json_encode([
2920 - 'model' => $model,
2921 - 'messages' => [
2922 - ['role' => 'system', 'content' => $system_prompt],
2923 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
2924 - ],
2925 - 'temperature' => 0.2,
2926 - 'max_tokens' => 20,
2927 - ]),
2928 - 'method' => 'POST',
2929 - 'timeout' => 15,
2930 - ];
2931 -
2932 - $response = wp_remote_post($url, $args);
2933 - if (is_wp_error($response)) {
2934 - return sanitize_text_field($user_query);
2935 - }
2936 -
2937 - $body = json_decode(wp_remote_retrieve_body($response), true);
2938 - return isset($body['choices'][0]['message']['content'])
2939 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
2940 - : sanitize_text_field($user_query);
2941 -}
2942 -
2943 -/**
2944 - * Interpret query using Claude models
2945 - */
2946 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
2947 - $url = 'https://api.anthropic.com/v1/messages';
2948 -
2949 - $args = [
2950 - 'headers' => [
2951 - 'Content-Type' => 'application/json',
2952 - 'x-api-key' => $api_key,
2953 - 'anthropic-version' => '2023-06-01',
2954 - ],
2955 - 'body' => wp_json_encode([
2956 - 'model' => $model,
2957 - 'system' => $system_prompt,
2958 - 'messages' => [
2959 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
2960 - ],
2961 - 'max_tokens' => 20,
2962 - 'temperature' => 0.2,
2963 - ]),
2964 - 'method' => 'POST',
2965 - 'timeout' => 15,
2966 - ];
2967 -
2968 - $response = wp_remote_post($url, $args);
2969 - if (is_wp_error($response)) {
2970 - return sanitize_text_field($user_query);
2971 - }
2972 -
2973 - $body = json_decode(wp_remote_retrieve_body($response), true);
2974 - if (!empty($body['content'][0]['text'])) {
2975 - return sanitize_text_field(trim($body['content'][0]['text']));
2976 - }
2977 -
2978 - return sanitize_text_field($user_query);
2979 -}
2980 -
2981 -/**
2982 - * Interpret query using Gemini models
2983 - */
2984 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
2985 - // Use v1beta for preview models, v1 for stable models
2986 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
2987 -
2988 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
2989 -
2990 - $args = [
2991 - 'headers' => [
2992 - 'Content-Type' => 'application/json',
2993 - ],
2994 - 'body' => wp_json_encode([
2995 - 'contents' => [
2996 - [
2997 - 'role' => 'user',
2998 - 'parts' => [
2999 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3000 - ]
3001 - ]
3002 - ],
3003 - 'generationConfig' => [
3004 - 'temperature' => 0.2,
3005 - 'maxOutputTokens' => 20,
3006 - ],
3007 - ]),
3008 - 'method' => 'POST',
3009 - 'timeout' => 15,
3010 - ];
3011 -
3012 - $response = wp_remote_post($url, $args);
3013 - if (is_wp_error($response)) {
3014 - return sanitize_text_field($user_query);
3015 - }
3016 -
3017 - $body = json_decode(wp_remote_retrieve_body($response), true);
3018 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3019 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3020 - }
3021 -
3022 - return sanitize_text_field($user_query);
3023 -}
3024 -
3025 -/**
3026 - * Interpret query using X.AI (Grok) models
3027 - */
3028 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3029 - $url = 'https://api.xai.com/v1/chat/completions';
3030 -
3031 - $args = [
3032 - 'headers' => [
3033 - 'Content-Type' => 'application/json',
3034 - 'Authorization' => 'Bearer ' . $api_key,
3035 - ],
3036 - 'body' => wp_json_encode([
3037 - 'model' => $model,
3038 - 'messages' => [
3039 - ['role' => 'system', 'content' => $system_prompt],
3040 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3041 - ],
3042 - 'temperature' => 0.2,
3043 - 'max_tokens' => 20,
3044 - ]),
3045 - 'method' => 'POST',
3046 - 'timeout' => 15,
3047 - ];
3048 -
3049 - $response = wp_remote_post($url, $args);
3050 - if (is_wp_error($response)) {
3051 - return sanitize_text_field($user_query);
3052 - }
3053 -
3054 - $body = json_decode(wp_remote_retrieve_body($response), true);
3055 - if (isset($body['choices'][0]['message']['content'])) {
3056 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3057 - }
3058 -
3059 - return sanitize_text_field($user_query);
3060 -}
3061 -
3062 -/**
3063 - * Interpret query using DeepSeek models
3064 - */
3065 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3066 - $url = 'https://api.deepseek.com/v1/chat/completions';
3067 -
3068 - $args = [
3069 - 'headers' => [
3070 - 'Content-Type' => 'application/json',
3071 - 'Authorization' => 'Bearer ' . $api_key,
3072 - ],
3073 - 'body' => wp_json_encode([
3074 - 'model' => $model,
3075 - 'messages' => [
3076 - ['role' => 'system', 'content' => $system_prompt],
3077 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3078 - ],
3079 - 'temperature' => 0.2,
3080 - 'max_tokens' => 20,
3081 - ]),
3082 - 'method' => 'POST',
3083 - 'timeout' => 15,
3084 - ];
3085 -
3086 - $response = wp_remote_post($url, $args);
3087 - if (is_wp_error($response)) {
3088 - return sanitize_text_field($user_query);
3089 - }
3090 -
3091 - $body = json_decode(wp_remote_retrieve_body($response), true);
3092 - if (isset($body['choices'][0]['message']['content'])) {
3093 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3094 - }
3095 -
3096 - return sanitize_text_field($user_query);
3097 -}
3098 -
3099 -//very good
3100 -private function add_email_to_loops($email) {
3101 - // Sanitize the email
3102 - $email = sanitize_email($email);
3103 -
3104 - // Retrieve and sanitize options
3105 - $api_key = isset($this->options['loops_api_key']) ? sanitize_text_field($this->options['loops_api_key']) : '';
3106 - $mailing_list_id = isset($this->options['loops_mailing_list']) ? sanitize_text_field($this->options['loops_mailing_list']) : '';
3107 -
3108 - // Check for missing API key or mailing list ID
3109 - if (empty($api_key) || empty($mailing_list_id)) {
3110 - //error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
3111 - return;
3112 - }
3113 -
3114 - $data = array(
3115 - 'email' => $email,
3116 - 'subscribed' => true,
3117 - 'source' => __('MxChat AI Chatbot', 'mxchat'),
3118 - 'mailingLists' => array($mailing_list_id => true),
3119 - );
3120 -
3121 - $url = 'https://app.loops.so/api/v1/contacts/create';
3122 - $args = array(
3123 - 'body' => wp_json_encode($data),
3124 - 'headers' => array(
3125 - 'Authorization' => 'Bearer ' . $api_key,
3126 - 'Content-Type' => 'application/json',
3127 - ),
3128 - 'method' => 'POST',
3129 - 'timeout' => 45,
3130 - );
3131 -
3132 - $response = wp_remote_post($url, $args);
3133 -
3134 - // Handle errors in the API request
3135 - if (is_wp_error($response)) {
3136 - //error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
3137 - return;
3138 - }
3139 -
3140 - // Check for non-200 HTTP responses
3141 - $response_code = wp_remote_retrieve_response_code($response);
3142 - if ($response_code != 200) {
3143 - $response_body = wp_remote_retrieve_body($response);
3144 - //error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
3145 - }
3146 -}
3147 -
3148 -public function mxchat_handle_pdf_discussion($message, $user_id, $session_id) {
3149 - // Get the maximum number of pages allowed from admin settings
3150 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3151 -
3152 - // Retrieve options for dynamic texts
3153 - $trigger_text = $this->options['pdf_intent_trigger_text'] ?? __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat');
3154 - $success_text = $this->options['pdf_intent_success_text'] ?? __("I've processed the PDF. What questions do you have about it?", 'mxchat');
3155 - $error_text = $this->options['pdf_intent_error_text'] ?? __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
3156 -
3157 - // Check for explicit request for new PDF
3158 - $new_pdf_requested = stripos($message, 'new') !== false ||
3159 - stripos($message, 'another') !== false ||
3160 - stripos($message, 'different') !== false;
3161 -
3162 - // If user mentions adding/reading a PDF, set waiting flag
3163 - if (stripos($message, 'pdf') !== false ||
3164 - stripos($message, 'document') !== false ||
3165 - stripos($message, 'read') !== false) {
3166 - set_transient('mxchat_waiting_for_pdf_url_' . $session_id, true, HOUR_IN_SECONDS);
3167 - $this->fallbackResponse['text'] = $trigger_text;
3168 - return;
3169 - }
3170 -
3171 - // If we're waiting for a URL or user requested new PDF
3172 - if ($new_pdf_requested || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
3173 - if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
3174 - // Process URL... (rest of your existing URL processing code)
3175 - } else {
3176 - $this->fallbackResponse['text'] = $trigger_text;
3177 - }
3178 - return;
3179 - }
3180 -
3181 - // Default to proceeding with conversation if no specific PDF action is needed
3182 - $this->fallbackResponse['text'] = '';
3183 -}
3184 -
3185 -
3186 -/**
3187 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3188 - */
3189 -private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3190 - // CLEAR DEBUG LOGGING
3191 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3192 - //error_log("PDF Source: " . $pdf_source);
3193 - //error_log("Max Pages: " . $max_pages);
3194 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3195 -
3196 - // Check if Advanced Claude Toolbar is available and enabled
3197 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3198 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3199 -
3200 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3201 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3202 -
3203 - if ($claude_available && $claude_enabled) {
3204 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3205 -
3206 - // Attempt Claude processing first
3207 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3208 -
3209 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3210 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3211 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3212 -
3213 - // Log first page details for verification
3214 - if (isset($claude_result[0])) {
3215 - $first_page = $claude_result[0];
3216 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3217 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3218 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3219 - }
3220 -
3221 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3222 - return $claude_result;
3223 - } else {
3224 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3225 - //error_log("Claude result type: " . gettype($claude_result));
3226 - if (is_array($claude_result)) {
3227 - //error_log("Claude result count: " . count($claude_result));
3228 - }
3229 - }
3230 - }
3231 -
3232 - // Fallback to basic processing
3233 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3234 -
3235 - $upload_dir = wp_upload_dir();
3236 - $temp_file = null;
3237 -
3238 - try {
3239 - // Your existing basic processing code here...
3240 - // (I'll include the key parts with debug logging)
3241 -
3242 - if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3243 - //error_log("Downloading PDF from URL...");
3244 -
3245 - // SECURITY FIX: Validate URL before processing
3246 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3247 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3248 - return false;
3249 - }
3250 -
3251 - $temp_file = wp_tempnam($pdf_source);
3252 -
3253 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3254 - $response = wp_safe_remote_get($pdf_source, [
3255 - 'timeout' => 60,
3256 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3257 - ]);
3258 -
3259 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3260 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3261 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3262 - return false;
3263 - }
3264 -
3265 - global $wp_filesystem;
3266 - if (empty($wp_filesystem)) {
3267 - require_once ABSPATH . 'wp-admin/includes/file.php';
3268 - WP_Filesystem();
3269 - }
3270 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3271 - //error_log("✅ PDF downloaded successfully");
3272 - } else {
3273 - $temp_file = $pdf_source;
3274 - //error_log("Using local PDF file: " . $temp_file);
3275 - }
3276 -
3277 - // Parse PDF
3278 - //error_log("Parsing PDF with basic parser...");
3279 - mxchat_load_pdf_parser();
3280 - $parser = new \Smalot\PdfParser\Parser();
3281 - $pdf = $parser->parseFile($temp_file);
3282 - $pages = $pdf->getPages();
3283 -
3284 - //error_log("PDF contains " . count($pages) . " pages");
3285 -
3286 - if (count($pages) > $max_pages) {
3287 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3288 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3289 - unlink($temp_file);
3290 - }
3291 - return 'too_many_pages';
3292 - }
3293 -
3294 - $embeddings = [];
3295 - $processed_pages = 0;
3296 -
3297 - foreach ($pages as $page_number => $page) {
3298 - $text = $page->getText();
3299 -
3300 - if (empty(trim($text))) {
3301 - //error_log("Skipping empty page: " . ($page_number + 1));
3302 - continue;
3303 - }
3304 -
3305 - $text = $this->mxchat_clean_text($text);
3306 -
3307 - $embedding = $this->mxchat_generate_embedding(
3308 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3309 - $this->options['api_key']
3310 - );
3311 -
3312 - if ($embedding) {
3313 - $embeddings[] = [
3314 - 'page_number' => $page_number + 1,
3315 - 'embedding' => $embedding,
3316 - 'text' => $text,
3317 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3318 - 'processing_method' => 'basic_pdf_parser'
3319 - ];
3320 - $processed_pages++;
3321 - }
3322 - }
3323 -
3324 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3325 -
3326 - // Cleanup
3327 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3328 - unlink($temp_file);
3329 - }
3330 -
3331 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
3332 - return $embeddings;
3333 -
3334 - } catch (\Exception $e) {
3335 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
3336 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3337 - unlink($temp_file);
3338 - }
3339 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3340 - return false;
3341 - }
3342 -}
3343 -
3344 -
3345 -/**
3346 - * Validate PDF URL for security
3347 - * Prevents SSRF attacks by blocking dangerous URLs
3348 - */
3349 -
3350 -private function mxchat_is_safe_pdf_url($url) {
3351 - // Use WordPress core function for comprehensive validation
3352 - // This blocks localhost, private IPs, and reserved IP ranges
3353 - $validated_url = wp_http_validate_url($url);
3354 -
3355 - if ($validated_url === false) {
3356 - return false;
3357 - }
3358 -
3359 - // Additional check: only allow HTTP/HTTPS schemes
3360 - $parsed = parse_url($url);
3361 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3362 - return false;
3363 - }
3364 -
3365 - return true;
3366 -}
3367 -
3368 -
3369 -private function mxchat_clean_text($text) {
3370 - // Remove excessive whitespace
3371 - $text = preg_replace('/\s+/', ' ', $text);
3372 -
3373 - // Remove control characters except newlines and tabs
3374 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3375 -
3376 - // Normalize line endings
3377 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3378 -
3379 - // Trim whitespace
3380 - $text = trim($text);
3381 -
3382 - return $text;
3383 -}
3384 -
3385 -private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3386 - //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3387 -
3388 - $most_relevant = null;
3389 - $highest_similarity = -INF;
3390 -
3391 - foreach ($embeddings as $page_data) {
3392 - $similarity = $this->mxchat_calculate_cosine_similarity($query_embedding, $page_data['embedding']);
3393 -
3394 - if ($similarity > $highest_similarity) {
3395 - $highest_similarity = $similarity;
3396 - $most_relevant = $page_data['page_number'];
3397 - }
3398 - }
3399 -
3400 - if (!is_null($most_relevant)) {
3401 - $page_numbers = range(max(1, $most_relevant - 1), min(count($embeddings), $most_relevant + 1));
3402 - return array_filter($embeddings, function ($page) use ($page_numbers) {
3403 - return in_array($page['page_number'], $page_numbers);
3404 - });
3405 - }
3406 -
3407 - return [];
3408 -}
3409 -
3410 -
3411 -public function handle_pdf_upload() {
3412 - check_ajax_referer('mxchat_chat_nonce', 'nonce');
3413 -
3414 - if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3415 - wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3416 - return;
3417 - }
3418 -
3419 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3420 - $options = get_option('mxchat_options', array());
3421 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3422 -
3423 - if ($show_pdf_button !== 'on') {
3424 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3425 - return;
3426 - }
3427 -
3428 - $file = $_FILES['pdf_file'];
3429 - $session_id = sanitize_text_field($_POST['session_id']);
3430 - $original_filename = sanitize_text_field($file['name']);
3431 -
3432 - // Update session owner if it changed (e.g. IP changed due to network switch)
3433 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3434 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3435 -
3436 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3437 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3438 - }
3439 -
3440 - $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3441 - if ($file_type['type'] !== 'application/pdf') {
3442 - wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3443 - return;
3444 - }
3445 -
3446 - $upload_dir = wp_upload_dir();
3447 -
3448 - // SECURITY FIX: Generate random filename without exposing session_id
3449 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3450 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
3451 - $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3452 -
3453 - if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3454 - wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
3455 - return;
3456 - }
3457 -
3458 - $this->clear_pdf_transients($session_id);
3459 -
3460 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3461 - $embeddings = $this->fetch_and_split_pdf_pages($pdf_path, $max_pages);
3462 -
3463 - if ($embeddings === 'too_many_pages') {
3464 - unlink($pdf_path);
3465 - $error_message = sprintf(
3466 - $this->options['pdf_intent_error_text'] ??
3467 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
3468 - $max_pages
3469 - );
3470 - wp_send_json_error($error_message);
3471 - return;
3472 - }
3473 -
3474 - if ($embeddings === false || empty($embeddings)) {
3475 - unlink($pdf_path);
3476 - $error_message = $this->options['pdf_intent_error_text'] ??
3477 - esc_html__('The uploaded PDF appears to be empty or contains unsupported content.', 'mxchat');
3478 - wp_send_json_error($error_message);
3479 - return;
3480 - }
3481 -
3482 - if (!empty($embeddings)) {
3483 - // Store the mapping between session and the random filename
3484 - set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3485 - set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3486 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3487 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
3488 -
3489 - $success_message = $this->options['pdf_intent_success_text'] ??
3490 - esc_html__("I've processed the PDF. What questions do you have about it?", 'mxchat');
3491 -
3492 - wp_send_json_success([
3493 - 'message' => $success_message,
3494 - 'filename' => $original_filename
3495 - ]);
3496 - return;
3497 - }
3498 -
3499 - unlink($pdf_path);
3500 - $error_message = $this->options['pdf_intent_error_text'] ??
3501 - esc_html__('Sorry, I couldn\'t process the PDF. Please ensure it\'s a valid file.', 'mxchat');
3502 - wp_send_json_error($error_message);
3503 - return;
3504 -}
3505 -public function handle_pdf_remove() {
3506 - check_ajax_referer('mxchat_chat_nonce', 'nonce');
3507 -
3508 - if (empty($_POST['session_id'])) {
3509 - wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
3510 - wp_die();
3511 - }
3512 -
3513 - $session_id = sanitize_text_field($_POST['session_id']);
3514 - $pdf_path = get_transient('mxchat_pdf_url_' . $session_id);
3515 -
3516 - if ($pdf_path && file_exists($pdf_path)) {
3517 - unlink($pdf_path);
3518 - }
3519 -
3520 - $this->clear_pdf_transients($session_id);
3521 -
3522 - wp_send_json_success([
3523 - 'message' => esc_html__('PDF removed successfully.', 'mxchat')
3524 - ]);
3525 - wp_die();
3526 -}
3527 -
3528 -
3529 -function mxchat_fetch_new_messages() {
3530 - $session_id = sanitize_text_field($_POST['session_id']);
3531 - $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3532 - $persistence_enabled = $_POST['persistence_enabled'] === 'true';
3533 - $initial_timestamp = isset($_POST['initial_timestamp']) ? intval($_POST['initial_timestamp']) : 0;
3534 -
3535 - if (empty($session_id)) {
3536 - //error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
3537 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
3538 - wp_die();
3539 - }
3540 -
3541 - $history = get_option("mxchat_history_{$session_id}", []);
3542 -
3543 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
3544 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
3545 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
3546 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
3547 -
3548 - $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
3549 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
3550 -
3551 - // If persistence is enabled, show all new messages
3552 - if ($persistence_enabled) {
3553 - $has_id = !empty($message['id']);
3554 - $is_agent = $message['role'] === 'agent';
3555 -
3556 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
3557 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
3558 - $is_newer = true;
3559 - } else {
3560 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
3561 - }
3562 -
3563 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
3564 -
3565 - return $has_id && $is_newer && $is_agent;
3566 - }
3567 -
3568 - // If persistence is disabled, only show messages after initial timestamp
3569 - return !empty($message['id']) &&
3570 - $message['role'] === 'agent' &&
3571 - $message['timestamp'] > $initial_timestamp;
3572 - });
3573 -
3574 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
3575 -
3576 - // Include current chat mode so frontend can detect agent→AI transitions
3577 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3578 -
3579 - wp_send_json_success([
3580 - 'new_messages' => array_values($new_messages),
3581 - 'chat_mode' => $chat_mode
3582 - ]);
3583 - wp_die();
3584 -}
3585 -public function mxchat_live_agent_handover($message, $user_id, $session_id) {
3586 - // First check if live agents are available
3587 - $live_agent_available = $this->options['live_agent_status'] ?? 'off';
3588 - if ($live_agent_available !== 'on') {
3589 - $away_message = $this->options['live_agent_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
3590 - $this->fallbackResponse = [
3591 - 'text' => $away_message,
3592 - 'html' => '',
3593 - 'images' => [],
3594 - 'chat_mode' => 'ai'
3595 - ];
3596 - wp_send_json([
3597 - 'text' => $away_message,
3598 - 'html' => '',
3599 - 'chat_mode' => 'ai',
3600 - 'session_id' => $session_id
3601 - ]);
3602 - wp_die();
3603 - }
3604 -
3605 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
3606 -
3607 - if (empty($slack_bot_token)) {
3608 - return false;
3609 - }
3610 -
3611 - // Check if channel already exists for this session
3612 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
3613 -
3614 - if (empty($channel_id)) {
3615 - // Create new channel with session ID as name
3616 - $channel_name = $this->generate_channel_name($session_id);
3617 -
3618 - //error_log("Attempting to create channel: $channel_name");
3619 -
3620 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
3621 - 'headers' => [
3622 - 'Content-Type' => 'application/json',
3623 - 'Authorization' => 'Bearer ' . $slack_bot_token
3624 - ],
3625 - 'body' => json_encode([
3626 - 'name' => $channel_name,
3627 - 'is_private' => false // Public channel - anyone in workspace can join
3628 - ])
3629 - ]);
3630 -
3631 - if (!is_wp_error($response)) {
3632 - $response_body = wp_remote_retrieve_body($response);
3633 - $response_data = json_decode($response_body, true);
3634 -
3635 - //error_log("Channel creation response: " . $response_body);
3636 -
3637 - if (isset($response_data['ok']) && $response_data['ok']) {
3638 - $channel_id = $response_data['channel']['id'];
3639 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
3640 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
3641 - update_option("mxchat_channel_{$session_id}", $channel_id);
3642 -
3643 - // Auto-invite agents to the channel
3644 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
3645 -
3646 - if (!empty($agent_user_ids)) {
3647 - // Parse user IDs (one per line)
3648 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
3649 -
3650 - foreach ($user_ids as $user_id_to_invite) {
3651 - //error_log("Inviting user to channel: $user_id_to_invite");
3652 -
3653 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
3654 - 'headers' => [
3655 - 'Content-Type' => 'application/json',
3656 - 'Authorization' => 'Bearer ' . $slack_bot_token
3657 - ],
3658 - 'body' => json_encode([
3659 - 'channel' => $channel_id,
3660 - 'users' => $user_id_to_invite
3661 - ])
3662 - ]);
3663 -
3664 - if (!is_wp_error($invite_response)) {
3665 - $invite_body = wp_remote_retrieve_body($invite_response);
3666 - $invite_data = json_decode($invite_body, true);
3667 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
3668 -
3669 - if (isset($invite_data['ok']) && $invite_data['ok']) {
3670 - //error_log("Successfully invited user $user_id_to_invite to channel");
3671 - } else {
3672 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
3673 - }
3674 - } else {
3675 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
3676 - }
3677 - }
3678 - } else {
3679 - //error_log("No agent user IDs configured for auto-invite");
3680 - }
3681 - } else {
3682 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
3683 - }
3684 - } else {
3685 - //error_log("WP Error creating channel: " . $response->get_error_message());
3686 - }
3687 -
3688 - if (empty($channel_id)) {
3689 - return false; // Failed to create channel
3690 - }
3691 - }
3692 -
3693 - // Get recent chat history
3694 - $history = get_option("mxchat_history_{$session_id}", []);
3695 - $recent_history = array_slice($history, -5);
3696 -
3697 - // Format conversation context
3698 - $conversation_context = "";
3699 - if (!empty($recent_history)) {
3700 - $conversation_context = "*Recent Conversation:*\n";
3701 - foreach ($recent_history as $hist_message) {
3702 - $role_display = $hist_message['role'] === 'user' ? 'User' : 'AI';
3703 - $conversation_context .= ">{$role_display}: {$hist_message['content']}\n";
3704 - }
3705 - $conversation_context .= "\n";
3706 - }
3707 -
3708 - update_option("mxchat_mode_{$session_id}", 'agent');
3709 -
3710 - // Send message to channel
3711 - $channel_message = "🔔 *New Live Agent Request*\n\n";
3712 - $channel_message .= "*Session ID:* `{$session_id}`\n";
3713 - $channel_message .= "*User ID:* `{$user_id}`\n\n";
3714 -
3715 - if (!empty($conversation_context)) {
3716 - $channel_message .= $conversation_context;
3717 - }
3718 -
3719 - $channel_message .= "*Current Message:*\n{$message}\n\n";
3720 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
3721 -
3722 - wp_remote_post('https://slack.com/api/chat.postMessage', [
3723 - 'headers' => [
3724 - 'Content-Type' => 'application/json',
3725 - 'Authorization' => 'Bearer ' . $slack_bot_token
3726 - ],
3727 - 'body' => json_encode([
3728 - 'channel' => $channel_id,
3729 - 'text' => $channel_message,
3730 - 'mrkdwn' => true
3731 - ])
3732 - ]);
3733 -
3734 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
3735 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
3736 -
3737 - $this->fallbackResponse = [
3738 - 'text' => $success_message,
3739 - 'html' => '',
3740 - 'images' => [],
3741 - 'chat_mode' => 'agent'
3742 - ];
3743 -
3744 - wp_send_json([
3745 - 'success' => true,
3746 - 'text' => $success_message,
3747 - 'html' => '',
3748 - 'chat_mode' => 'agent',
3749 - 'session_id' => $session_id,
3750 - 'fallbackResponse' => $this->fallbackResponse
3751 - ]);
3752 - wp_die();
3753 -}
3754 -
3755 -private function generate_channel_name($session_id) {
3756 - $email = null;
3757 - $name = null;
3758 -
3759 - // 1. First priority: Check if user is logged in and get their info
3760 - if (is_user_logged_in()) {
3761 - $current_user = wp_get_current_user();
3762 - if (!empty($current_user->user_email)) {
3763 - $email = $current_user->user_email;
3764 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
3765 - }
3766 - if (!empty($current_user->display_name)) {
3767 - $name = $current_user->display_name;
3768 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
3769 - }
3770 - }
3771 -
3772 - // 2. Second priority: Check for saved email/name from "require email to chat" option
3773 - if (empty($email)) {
3774 - $email_option_key = "mxchat_email_{$session_id}";
3775 - $saved_email = get_option($email_option_key);
3776 - if (!empty($saved_email)) {
3777 - $email = $saved_email;
3778 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
3779 - }
3780 - }
3781 -
3782 - if (empty($name)) {
3783 - $name_option_key = "mxchat_name_{$session_id}";
3784 - $saved_name = get_option($name_option_key);
3785 - if (!empty($saved_name)) {
3786 - $name = $saved_name;
3787 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
3788 - }
3789 - }
3790 -
3791 - // 3. Third priority: Check existing chat transcript for email/name
3792 - if (empty($email) || empty($name)) {
3793 - global $wpdb;
3794 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
3795 - $existing_data = $wpdb->get_row($wpdb->prepare(
3796 - "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",
3797 - $session_id
3798 - ));
3799 -
3800 - if ($existing_data) {
3801 - if (empty($email) && !empty($existing_data->user_email)) {
3802 - $email = $existing_data->user_email;
3803 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
3804 - }
3805 - if (empty($name) && !empty($existing_data->user_name)) {
3806 - $name = $existing_data->user_name;
3807 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
3808 - }
3809 - }
3810 - }
3811 -
3812 - // 4. Generate channel name based on priority: Name > Email > Session ID
3813 - $channel_name = '';
3814 -
3815 - if (!empty($name)) {
3816 - // Convert name to valid Slack channel name
3817 - $base_name = strtolower(trim($name));
3818 - // Replace spaces and invalid characters
3819 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
3820 - $base_name = preg_replace('/\s+/', '-', $base_name);
3821 - $base_name = trim($base_name, '-');
3822 -
3823 - // Get last 4 characters of session ID for uniqueness
3824 - $session_suffix = substr($session_id, -4);
3825 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
3826 -
3827 - // Slack channel names have a 21 character limit
3828 - if (strlen($channel_name) > 21) {
3829 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
3830 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
3831 - $truncated_name = substr($base_name, 0, $available_space);
3832 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
3833 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
3834 - }
3835 -
3836 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
3837 -
3838 - } elseif (!empty($email)) {
3839 - // Convert email to valid Slack channel name (your existing logic)
3840 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
3841 - // Remove any remaining invalid characters
3842 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
3843 - // Ensure it doesn't end with a hyphen
3844 - $channel_name = rtrim($channel_name, '-');
3845 - // Slack channel names have a 21 character limit, so truncate if needed
3846 - if (strlen($channel_name) > 21) {
3847 - $channel_name = substr($channel_name, 0, 21);
3848 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
3849 - }
3850 -
3851 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
3852 -
3853 - } else {
3854 - // Fallback to session ID if no name or email found
3855 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
3856 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
3857 - }
3858 -
3859 - // Final validation - ensure channel name meets Slack requirements
3860 - if (strlen($channel_name) > 21) {
3861 - $channel_name = substr($channel_name, 0, 21);
3862 - $channel_name = rtrim($channel_name, '-');
3863 - }
3864 -
3865 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
3866 - return $channel_name;
3867 -}
3868 -
3869 -/**
3870 - * Telegram Live Agent Handover
3871 - * Creates a forum topic in the Telegram group and notifies agents
3872 - */
3873 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
3874 - // Check if Telegram agents are available
3875 - $telegram_available = $this->options['telegram_status'] ?? 'off';
3876 - if ($telegram_available !== 'on') {
3877 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
3878 - $this->fallbackResponse = [
3879 - 'text' => $away_message,
3880 - 'html' => '',
3881 - 'images' => [],
3882 - 'chat_mode' => 'ai'
3883 - ];
3884 - wp_send_json([
3885 - 'text' => $away_message,
3886 - 'html' => '',
3887 - 'chat_mode' => 'ai',
3888 - 'session_id' => $session_id
3889 - ]);
3890 - wp_die();
3891 - }
3892 -
3893 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
3894 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
3895 -
3896 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
3897 - return false;
3898 - }
3899 -
3900 - // Check if topic already exists for this session
3901 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
3902 -
3903 - if (empty($topic_id)) {
3904 - // Generate topic name
3905 - $topic_name = $this->generate_telegram_topic_name($session_id);
3906 -
3907 - // Random icon color (Telegram forum topic colors)
3908 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
3909 - $icon_color = $icon_colors[array_rand($icon_colors)];
3910 -
3911 - // Create forum topic
3912 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
3913 - 'headers' => ['Content-Type' => 'application/json'],
3914 - 'body' => json_encode([
3915 - 'chat_id' => $telegram_group_id,
3916 - 'name' => $topic_name,
3917 - 'icon_color' => $icon_color
3918 - ])
3919 - ]);
3920 -
3921 - if (!is_wp_error($response)) {
3922 - $response_body = wp_remote_retrieve_body($response);
3923 - $response_data = json_decode($response_body, true);
3924 -
3925 - if (isset($response_data['ok']) && $response_data['ok']) {
3926 - $topic_id = $response_data['result']['message_thread_id'];
3927 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
3928 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
3929 - }
3930 - }
3931 -
3932 - if (empty($topic_id)) {
3933 - return false; // Failed to create topic
3934 - }
3935 - }
3936 -
3937 - // Get recent chat history
3938 - $history = get_option("mxchat_history_{$session_id}", []);
3939 - $recent_history = array_slice($history, -5);
3940 -
3941 - // Format conversation context for Telegram (HTML format)
3942 - $conversation_context = "";
3943 - if (!empty($recent_history)) {
3944 - $conversation_context = "<b>Recent Conversation:</b>\n";
3945 - foreach ($recent_history as $hist_message) {
3946 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
3947 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
3948 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
3949 - }
3950 - $conversation_context .= "\n";
3951 - }
3952 -
3953 - // Get user info
3954 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
3955 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
3956 -
3957 - // Update session mode
3958 - update_option("mxchat_mode_{$session_id}", 'agent');
3959 -
3960 - // Send initial message to topic
3961 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
3962 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
3963 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
3964 - $topic_message .= "<b>User:</b> {$user_name}\n";
3965 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
3966 -
3967 - if (!empty($conversation_context)) {
3968 - $topic_message .= $conversation_context;
3969 - }
3970 -
3971 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
3972 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
3973 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
3974 -
3975 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
3976 - 'headers' => ['Content-Type' => 'application/json'],
3977 - 'body' => json_encode([
3978 - 'chat_id' => $telegram_group_id,
3979 - 'message_thread_id' => $topic_id,
3980 - 'text' => $topic_message,
3981 - 'parse_mode' => 'HTML'
3982 - ])
3983 - ]);
3984 -
3985 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
3986 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
3987 -
3988 - $this->fallbackResponse = [
3989 - 'text' => $success_message,
3990 - 'html' => '',
3991 - 'images' => [],
3992 - 'chat_mode' => 'agent'
3993 - ];
3994 -
3995 - wp_send_json([
3996 - 'success' => true,
3997 - 'text' => $success_message,
3998 - 'html' => '',
3999 - 'chat_mode' => 'agent',
4000 - 'session_id' => $session_id,
4001 - 'fallbackResponse' => $this->fallbackResponse
4002 - ]);
4003 - wp_die();
4004 -}
4005 -
4006 -/**
4007 - * Generate topic name for Telegram forum
4008 - */
4009 -private function generate_telegram_topic_name($session_id) {
4010 - $name = null;
4011 - $email = null;
4012 -
4013 - // Check logged in user
4014 - if (is_user_logged_in()) {
4015 - $current_user = wp_get_current_user();
4016 - if (!empty($current_user->display_name)) {
4017 - $name = $current_user->display_name;
4018 - }
4019 - if (!empty($current_user->user_email)) {
4020 - $email = $current_user->user_email;
4021 - }
4022 - }
4023 -
4024 - // Check session data
4025 - if (empty($name)) {
4026 - $name = get_option("mxchat_name_{$session_id}");
4027 - }
4028 - if (empty($email)) {
4029 - $email = get_option("mxchat_email_{$session_id}");
4030 - }
4031 -
4032 - // Generate topic name
4033 - $session_suffix = substr($session_id, -6);
4034 -
4035 - if (!empty($name)) {
4036 - // Clean name for topic (max 128 chars in Telegram)
4037 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4038 - $clean_name = trim($clean_name);
4039 - if (strlen($clean_name) > 50) {
4040 - $clean_name = substr($clean_name, 0, 50);
4041 - }
4042 - return "Chat - {$clean_name} ({$session_suffix})";
4043 - } elseif (!empty($email)) {
4044 - // Use email prefix
4045 - $email_prefix = explode('@', $email)[0];
4046 - if (strlen($email_prefix) > 30) {
4047 - $email_prefix = substr($email_prefix, 0, 30);
4048 - }
4049 - return "Chat - {$email_prefix} ({$session_suffix})";
4050 - }
4051 -
4052 - return "Chat - {$session_suffix}";
4053 -}
4054 -
4055 -/**
4056 - * Send user message to Telegram agent
4057 - */
4058 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4059 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4060 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4061 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4062 -
4063 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4064 - return false;
4065 - }
4066 -
4067 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4068 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4069 -
4070 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4071 - 'headers' => ['Content-Type' => 'application/json'],
4072 - 'body' => json_encode([
4073 - 'chat_id' => $group_id,
4074 - 'message_thread_id' => $topic_id,
4075 - 'text' => $user_message,
4076 - 'parse_mode' => 'HTML'
4077 - ])
4078 - ]);
4079 -
4080 - return !is_wp_error($response);
4081 -}
4082 -
4083 -/**
4084 - * Handle incoming Telegram webhook
4085 - */
4086 -public function handle_telegram_webhook(WP_REST_Request $request) {
4087 - $body = $request->get_body();
4088 - $data = json_decode($body, true);
4089 -
4090 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4091 -
4092 - // Handle message events from forum topics
4093 - if (isset($data['message'])) {
4094 - $message_data = $data['message'];
4095 -
4096 - // Skip if not from a forum topic
4097 - if (!isset($message_data['message_thread_id'])) {
4098 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4099 - return new WP_REST_Response(['ok' => true]);
4100 - }
4101 -
4102 - // Skip bot messages
4103 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4104 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4105 - return new WP_REST_Response(['ok' => true]);
4106 - }
4107 -
4108 - $chat_id = $message_data['chat']['id'] ?? '';
4109 - $topic_id = $message_data['message_thread_id'];
4110 - $message_text = $message_data['text'] ?? '';
4111 - $message_id = $message_data['message_id'] ?? '';
4112 - $from = $message_data['from'] ?? [];
4113 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4114 - if (empty($agent_name)) {
4115 - $agent_name = $from['username'] ?? 'Agent';
4116 - }
4117 -
4118 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4119 -
4120 - // Skip empty messages
4121 - if (empty($message_text)) {
4122 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4123 - return new WP_REST_Response(['ok' => true]);
4124 - }
4125 -
4126 - // Find session ID by topic ID - cast to string for comparison
4127 - global $wpdb;
4128 - $topic_id_str = strval($topic_id);
4129 - $session_option = $wpdb->get_var(
4130 - $wpdb->prepare(
4131 - "SELECT option_name FROM {$wpdb->options}
4132 - WHERE option_name LIKE %s
4133 - AND option_value = %s",
4134 - 'mxchat_telegram_topic_%',
4135 - $topic_id_str
4136 - )
4137 - );
4138 -
4139 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4140 -
4141 - if ($session_option) {
4142 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4143 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4144 -
4145 - // Verify the group ID matches
4146 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4147 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4148 -
4149 - if (strval($stored_group_id) != strval($chat_id)) {
4150 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4151 - return new WP_REST_Response(['ok' => true]);
4152 - }
4153 -
4154 - // Check for closure commands
4155 - $lower_text = strtolower(trim($message_text));
4156 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4157 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4158 - // End the live agent session
4159 - update_option("mxchat_mode_{$session_id}", 'ai');
4160 -
4161 - // Save disconnect message
4162 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4163 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4164 -
4165 - // Notify in Telegram
4166 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4167 - if (!empty($telegram_bot_token)) {
4168 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4169 - 'headers' => ['Content-Type' => 'application/json'],
4170 - 'body' => json_encode([
4171 - 'chat_id' => $chat_id,
4172 - 'message_thread_id' => $topic_id,
4173 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4174 - 'parse_mode' => 'HTML'
4175 - ])
4176 - ]);
4177 -
4178 - // Optionally close the topic
4179 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4180 - 'headers' => ['Content-Type' => 'application/json'],
4181 - 'body' => json_encode([
4182 - 'chat_id' => $chat_id,
4183 - 'message_thread_id' => $topic_id
4184 - ])
4185 - ]);
4186 - }
4187 -
4188 - return new WP_REST_Response(['ok' => true]);
4189 - }
4190 -
4191 - // Deduplicate messages
4192 - $message_key = md5($session_id . $message_id . $message_text);
4193 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4194 -
4195 - if (in_array($message_key, $processed_messages)) {
4196 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4197 - return new WP_REST_Response(['ok' => true]);
4198 - }
4199 -
4200 - $processed_messages[] = $message_key;
4201 - if (count($processed_messages) > 50) {
4202 - $processed_messages = array_slice($processed_messages, -50);
4203 - }
4204 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4205 -
4206 - // Save the agent message - format with agent name prefix for proper parsing
4207 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4208 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4209 -
4210 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4211 -
4212 - // Verify the message was saved to history
4213 - $history = get_option("mxchat_history_{$session_id}", []);
4214 - $last_message = end($history);
4215 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4216 -
4217 - // Send confirmation back to Telegram
4218 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4219 - if (!empty($telegram_bot_token)) {
4220 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4221 - if (!get_transient($confirm_key)) {
4222 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4223 - 'headers' => ['Content-Type' => 'application/json'],
4224 - 'body' => json_encode([
4225 - 'chat_id' => $chat_id,
4226 - 'message_thread_id' => $topic_id,
4227 - 'text' => "✅ <i>Message sent to user</i>",
4228 - 'parse_mode' => 'HTML',
4229 - 'reply_to_message_id' => $message_id
4230 - ])
4231 - ]);
4232 - set_transient($confirm_key, true, 300);
4233 - }
4234 - }
4235 - } else {
4236 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4237 - }
4238 - } else {
4239 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4240 - }
4241 -
4242 - return new WP_REST_Response(['ok' => true]);
4243 -}
4244 -
4245 -public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4246 - // Check if this is a Telegram agent session
4247 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4248 - if (!empty($telegram_topic_id)) {
4249 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4250 - }
4251 -
4252 - // Otherwise, try Slack
4253 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4254 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4255 -
4256 - if (empty($slack_bot_token) || empty($channel_id)) {
4257 - return false;
4258 - }
4259 -
4260 - $user_message = "💬 *User:* {$message}";
4261 -
4262 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
4263 - 'headers' => [
4264 - 'Content-Type' => 'application/json',
4265 - 'Authorization' => 'Bearer ' . $slack_bot_token
4266 - ],
4267 - 'body' => json_encode([
4268 - 'channel' => $channel_id,
4269 - 'text' => $user_message,
4270 - 'mrkdwn' => true
4271 - ])
4272 - ]);
4273 -
4274 - return !is_wp_error($response);
4275 -}
4276 -public function handle_slack_interaction(WP_REST_Request $request) {
4277 - //error_log('Received Slack interaction');
4278 -
4279 - $payload = json_decode($request->get_param('payload'), true);
4280 - //error_log('Payload: ' . print_r($payload, true));
4281 -
4282 - // Handle button click
4283 - if ($payload['type'] === 'block_actions' && $payload['actions'][0]['action_id'] === 'reply_to_user') {
4284 - $session_id = $payload['actions'][0]['value'];
4285 - $trigger_id = $payload['trigger_id'];
4286 -
4287 - // Get Bot Token from settings
4288 - $slack_token = $this->options['live_agent_bot_token'] ?? '';
4289 -
4290 - if (empty($slack_token)) {
4291 - //error_log('Slack Bot Token not configured');
4292 - return new WP_REST_Response(['error' => esc_html__('Bot token not configured', 'mxchat')], 400);
4293 - }
4294 - $response = wp_remote_post('https://slack.com/api/views.open', [
4295 - 'headers' => [
4296 - 'Content-Type' => 'application/json',
4297 - 'Authorization' => 'Bearer ' . $slack_token
4298 - ],
4299 - 'body' => json_encode([
4300 - 'trigger_id' => $trigger_id,
4301 - 'view' => [
4302 - 'type' => 'modal',
4303 - 'callback_id' => 'reply_modal',
4304 - 'title' => [
4305 - 'type' => 'plain_text',
4306 - 'text' => __('Reply to User', 'mxchat')
4307 - ],
4308 - 'submit' => [
4309 - 'type' => 'plain_text',
4310 - 'text' => __('Send', 'mxchat')
4311 - ],
4312 - 'close' => [
4313 - 'type' => 'plain_text',
4314 - 'text' => __('Cancel', 'mxchat')
4315 - ],
4316 - 'blocks' => [
4317 - [
4318 - 'type' => 'input',
4319 - 'block_id' => 'reply_block',
4320 - 'label' => [
4321 - 'type' => 'plain_text',
4322 - 'text' => sprintf(__('Reply to session: %s', 'mxchat'), $session_id)
4323 - ],
4324 - 'element' => [
4325 - 'type' => 'plain_text_input',
4326 - 'action_id' => 'message',
4327 - 'multiline' => true,
4328 - 'placeholder' => [
4329 - 'type' => 'plain_text',
4330 - 'text' => __('Type your message here...', 'mxchat')
4331 - ]
4332 - ]
4333 - ]
4334 - ],
4335 - 'private_metadata' => $session_id
4336 - ]
4337 - ])
4338 - ]);
4339 -
4340 - //error_log('Views.open response: ' . print_r($response, true));
4341 -
4342 - // Return immediate acknowledgment
4343 - return new WP_REST_Response(['ok' => true]);
4344 - }
4345 -
4346 - // Handle modal submission
4347 -// Handle modal submission
4348 -if ($payload['type'] === 'view_submission') {
4349 - $session_id = $payload['view']['private_metadata'];
4350 - $message = $payload['view']['state']['values']['reply_block']['message']['value'];
4351 -
4352 - // Save the message (keep the message_id but don't include in response)
4353 - $this->mxchat_save_chat_message($session_id, 'agent', $message);
4354 -
4355 - // Keep the original response format for Slack
4356 - return new WP_REST_Response([
4357 - 'response_action' => 'clear'
4358 - ]);
4359 -}
4360 -
4361 - // Default acknowledgment
4362 - return new WP_REST_Response(['ok' => true]);
4363 -}
4364 -public function mxchat_handle_agent_response(WP_REST_Request $request) {
4365 - //error_log('Received agent response request');
4366 - //error_log('Request data: ' . print_r($request->get_params(), true));
4367 - // //error_log('Raw body: ' . file_get_contents('php://input'));
4368 -
4369 - // Get the data from Slack's slash command format
4370 - $command_text = $request->get_param('text');
4371 - // //error_log('Command text: ' . $command_text);
4372 -
4373 - if (empty($command_text)) {
4374 - //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
4375 - return new WP_REST_Response([
4376 - 'error' => esc_html__('Command text is required. Format: /reply session_id message', 'mxchat')
4377 - ], 400);
4378 - }
4379 -
4380 - // Split the command text into session_id and message
4381 - $parts = explode(' ', $command_text, 2);
4382 - if (count($parts) !== 2) {
4383 - //error_log('Agent response error: Invalid command format');
4384 - return new WP_REST_Response([
4385 - 'error' => esc_html__('Invalid format. Use: /reply session_id message', 'mxchat')
4386 - ], 400);
4387 - }
4388 -
4389 - $session_id = sanitize_text_field($parts[0]);
4390 - $message = sanitize_text_field($parts[1]);
4391 -
4392 - //error_log("Processing agent response - Session ID: $session_id, Message: $message");
4393 -
4394 - // Save the message
4395 - $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
4396 -
4397 - if (!$message_id) {
4398 - // //error_log('Failed to save agent message');
4399 - return new WP_REST_Response([
4400 - 'error' => esc_html__('Failed to save message', 'mxchat')
4401 - ], 500);
4402 - }
4403 -
4404 - // Return success response in Slack's expected format
4405 - return new WP_REST_Response([
4406 - 'response_type' => 'in_channel',
4407 - 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4408 - ], 200);
4409 -}
4410 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4411 - // Update mode to AI
4412 - update_option("mxchat_mode_{$session_id}", 'ai');
4413 -
4414 - // Clear any existing PDF context to start fresh
4415 - $this->clear_pdf_transients($session_id);
4416 -
4417 - // Set the response with explicit chat_mode
4418 - $this->fallbackResponse = [
4419 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4420 - 'html' => '',
4421 - 'images' => [],
4422 - 'chat_mode' => 'ai' // Ensure this is set
4423 - ];
4424 -
4425 - // Return the complete response array instead of just true
4426 - return $this->fallbackResponse;
4427 -}
4428 -
4429 -public function handle_slack_messages(WP_REST_Request $request) {
4430 - // Log the incoming request for debugging
4431 - //error_log('Slack events request received: ' . $request->get_body());
4432 -
4433 - $body = $request->get_body();
4434 - $data = json_decode($body, true);
4435 -
4436 - // Handle Slack URL verification
4437 - if (isset($data['type']) && $data['type'] === 'url_verification') {
4438 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
4439 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
4440 - }
4441 -
4442 - // IMPORTANT: Handle Slack's event deduplication
4443 - if (isset($data['event_id'])) {
4444 - $event_id = $data['event_id'];
4445 - $processed_events = get_transient('mxchat_slack_events') ?: [];
4446 -
4447 - // Check if we've already processed this event
4448 - if (in_array($event_id, $processed_events)) {
4449 - //error_log("Duplicate event detected: $event_id");
4450 - return new WP_REST_Response(['ok' => true]);
4451 - }
4452 -
4453 - // Add this event to processed list
4454 - $processed_events[] = $event_id;
4455 - // Keep only last 100 events to prevent memory issues
4456 - if (count($processed_events) > 100) {
4457 - $processed_events = array_slice($processed_events, -100);
4458 - }
4459 - // Store for 1 hour
4460 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
4461 - }
4462 -
4463 - // Handle message events
4464 - if (isset($data['event']) && $data['event']['type'] === 'message') {
4465 - $event = $data['event'];
4466 -
4467 - // Skip bot messages and messages with subtypes (like bot_message)
4468 - if (isset($event['bot_id']) || isset($event['subtype'])) {
4469 - return new WP_REST_Response(['ok' => true]);
4470 - }
4471 -
4472 - // Additional check: Skip if this is a threaded reply to our confirmation
4473 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
4474 - return new WP_REST_Response(['ok' => true]);
4475 - }
4476 -
4477 - $channel_id = $event['channel'];
4478 - $message_text = $event['text'] ?? '';
4479 - $message_ts = $event['ts'] ?? '';
4480 -
4481 - // Find session ID by looking for matching channel
4482 - global $wpdb;
4483 - $session_option = $wpdb->get_var(
4484 - $wpdb->prepare(
4485 - "SELECT option_name FROM {$wpdb->options}
4486 - WHERE option_name LIKE 'mxchat_channel_%'
4487 - AND option_value = %s",
4488 - $channel_id
4489 - )
4490 - );
4491 -
4492 - if ($session_option) {
4493 - $session_id = str_replace('mxchat_channel_', '', $session_option);
4494 -
4495 - // Create a unique key for this specific message
4496 - $message_key = md5($session_id . $message_ts . $message_text);
4497 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
4498 -
4499 - // Check if we've already processed this exact message
4500 - if (in_array($message_key, $processed_messages)) {
4501 - //error_log("Duplicate message detected for session $session_id");
4502 - return new WP_REST_Response(['ok' => true]);
4503 - }
4504 -
4505 - // Add to processed messages
4506 - $processed_messages[] = $message_key;
4507 - // Keep only last 50 messages per session
4508 - if (count($processed_messages) > 50) {
4509 - $processed_messages = array_slice($processed_messages, -50);
4510 - }
4511 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4512 -
4513 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4514 -
4515 - // Handle agent ending the chat — transfer back to AI
4516 - // Format: "!endchat" or "!endchat <custom message to user>"
4517 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
4518 - update_option("mxchat_mode_{$session_id}", 'ai');
4519 -
4520 - // Extract custom message after !endchat, or use empty string
4521 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
4522 -
4523 - // Send the agent's custom farewell message if provided
4524 - if (!empty($custom_message)) {
4525 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
4526 - }
4527 -
4528 - // Confirm in Slack channel
4529 - if (!empty($slack_bot_token)) {
4530 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4531 - 'headers' => [
4532 - 'Content-Type' => 'application/json',
4533 - 'Authorization' => 'Bearer ' . $slack_bot_token
4534 - ],
4535 - 'body' => json_encode([
4536 - 'channel' => $channel_id,
4537 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
4538 - 'mrkdwn' => true
4539 - ])
4540 - ]);
4541 - }
4542 -
4543 - return new WP_REST_Response(['ok' => true]);
4544 - }
4545 -
4546 - // Save the agent message
4547 - $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4548 -
4549 - // Send confirmation back to Slack (only once)
4550 - if (!empty($slack_bot_token)) {
4551 - // Use a transient to prevent duplicate confirmations
4552 - $confirm_key = 'mxchat_confirm_' . $message_key;
4553 - if (!get_transient($confirm_key)) {
4554 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4555 - 'headers' => [
4556 - 'Content-Type' => 'application/json',
4557 - 'Authorization' => 'Bearer ' . $slack_bot_token
4558 - ],
4559 - 'body' => json_encode([
4560 - 'channel' => $channel_id,
4561 - 'text' => "✅ _Message sent to user_",
4562 - 'thread_ts' => $event['ts'] // Reply in thread
4563 - ])
4564 - ]);
4565 - // Set transient to prevent duplicate confirmations
4566 - set_transient($confirm_key, true, 300); // 5 minutes
4567 - }
4568 - }
4569 - }
4570 - }
4571 -
4572 - return new WP_REST_Response(['ok' => true]);
4573 -}
4574 -
4575 -// For the word upload handler
4576 -public function mxchat_handle_word_upload() {
4577 - // Delegate to word handler
4578 - $this->word_handler->mxchat_handle_word_upload();
4579 -}
4580 -
4581 -// For the word removal handler
4582 -public function mxchat_handle_word_remove() {
4583 - // Delegate to word handler
4584 - $this->word_handler->mxchat_handle_word_remove();
4585 -}
4586 -
4587 -// For the word status check
4588 -public function mxchat_check_word_status() {
4589 - // Delegate to word handler
4590 - $this->word_handler->mxchat_check_word_status();
4591 -}
4592 -
4593 -
4594 -private function mxchat_get_user_identifier() {
4595 - return MxChat_User::mxchat_get_user_identifier();
4596 -}
4597 -
4598 -private function mxchat_generate_embedding($text, $api_key) {
4599 - try {
4600 - // Get options and selected model
4601 - $options = get_option('mxchat_options');
4602 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4603 -
4604 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
4605 - // Off by default so existing sites see byte-identical behavior.
4606 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
4607 - return $this->mxchat_generate_embedding_custom($text);
4608 - }
4609 -
4610 - // Determine endpoint and API key based on model
4611 - if (strpos($selected_model, 'voyage') === 0) {
4612 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
4613 - $api_key = $options['voyage_api_key'] ?? '';
4614 -
4615 - // Check if Voyage API key is missing
4616 - if (empty($api_key)) {
4617 - //error_log('Voyage API key is missing');
4618 - return [
4619 - 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
4620 - 'error_code' => 'missing_voyage_api_key'
4621 - ];
4622 - }
4623 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
4624 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
4625 - $api_key = $options['gemini_api_key'] ?? '';
4626 -
4627 - // Check if Gemini API key is missing
4628 - if (empty($api_key)) {
4629 - //error_log('Gemini API key is missing');
4630 - return [
4631 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
4632 - 'error_code' => 'missing_gemini_api_key'
4633 - ];
4634 - }
4635 - } else {
4636 - $endpoint = 'https://api.openai.com/v1/embeddings';
4637 - // Use the passed API key for OpenAI
4638 -
4639 - // Check if OpenAI API key is missing
4640 - if (empty($api_key)) {
4641 - //error_log('OpenAI API key is missing');
4642 - return [
4643 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
4644 - 'error_code' => 'missing_openai_api_key'
4645 - ];
4646 - }
4647 - }
4648 -
4649 - // Check if text is empty
4650 - if (empty($text)) {
4651 - //error_log('Empty text provided for embedding generation');
4652 - return [
4653 - 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
4654 - 'error_code' => 'empty_embedding_text'
4655 - ];
4656 - }
4657 -
4658 - // Prepare request body based on provider
4659 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4660 - // Gemini API format
4661 - $request_body = [
4662 - 'model' => 'models/' . $selected_model,
4663 - 'content' => [
4664 - 'parts' => [
4665 - ['text' => $text]
4666 - ]
4667 - ],
4668 - 'outputDimensionality' => 1536
4669 - ];
4670 -
4671 - // Prepare headers for Gemini (API key as query parameter)
4672 - $endpoint .= '?key=' . $api_key;
4673 - $headers = [
4674 - 'Content-Type' => 'application/json'
4675 - ];
4676 - } else {
4677 - // OpenAI/Voyage API format
4678 - $request_body = [
4679 - 'input' => $text,
4680 - 'model' => $selected_model
4681 - ];
4682 -
4683 - // Add output_dimension for voyage-3-large
4684 - if ($selected_model === 'voyage-3-large') {
4685 - $request_body['output_dimension'] = 2048;
4686 - }
4687 -
4688 - // Prepare headers for OpenAI/Voyage
4689 - $headers = [
4690 - 'Content-Type' => 'application/json',
4691 - 'Authorization' => 'Bearer ' . $api_key
4692 - ];
4693 - }
4694 -
4695 - // Prepare request arguments
4696 - $args = [
4697 - 'body' => wp_json_encode($request_body),
4698 - 'headers' => $headers,
4699 - 'timeout' => 60,
4700 - 'redirection' => 5,
4701 - 'blocking' => true,
4702 - 'httpversion' => '1.0',
4703 - 'sslverify' => true,
4704 - ];
4705 -
4706 - // Make the request
4707 - $response = wp_remote_post($endpoint, $args);
4708 -
4709 - // Handle WordPress errors
4710 - if (is_wp_error($response)) {
4711 - $error_message = $response->get_error_message();
4712 - //error_log('Embedding Generation Error: ' . $error_message);
4713 - return [
4714 - 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
4715 - 'error_code' => 'embedding_connection_error'
4716 - ];
4717 - }
4718 -
4719 - // Check HTTP status code
4720 - $status_code = wp_remote_retrieve_response_code($response);
4721 - if ($status_code !== 200) {
4722 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
4723 -
4724 - $error_message = isset($response_body['error']['message'])
4725 - ? $response_body['error']['message']
4726 - : 'HTTP Error ' . $status_code;
4727 -
4728 - $error_type = isset($response_body['error']['type'])
4729 - ? $response_body['error']['type']
4730 - : 'unknown';
4731 -
4732 - //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
4733 -
4734 - // Handle specific error types
4735 - switch ($error_type) {
4736 - case 'invalid_request_error':
4737 - if (strpos($error_message, 'API key') !== false) {
4738 - return [
4739 - 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
4740 - 'error_code' => 'embedding_invalid_api_key'
4741 - ];
4742 - }
4743 - break;
4744 -
4745 - case 'authentication_error':
4746 - return [
4747 - 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
4748 - 'error_code' => 'embedding_auth_error'
4749 - ];
4750 -
4751 - case 'rate_limit_exceeded':
4752 - return [
4753 - 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
4754 - 'error_code' => 'embedding_rate_limit'
4755 - ];
4756 -
4757 - case 'quota_exceeded':
4758 - return [
4759 - 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
4760 - 'error_code' => 'embedding_quota_exceeded'
4761 - ];
4762 - }
4763 -
4764 - // Generic error fallback
4765 - return [
4766 - 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
4767 - 'error_code' => 'embedding_api_error',
4768 - 'status_code' => $status_code
4769 - ];
4770 - }
4771 -
4772 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
4773 -
4774 - // Handle different response formats based on provider
4775 - if (strpos($selected_model, 'gemini-embedding') === 0) {
4776 - // Gemini API response format
4777 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
4778 - return $response_body['embedding']['values'];
4779 - } else {
4780 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
4781 - return [
4782 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
4783 - 'error_code' => 'invalid_gemini_embedding_response'
4784 - ];
4785 - }
4786 - } else {
4787 - // OpenAI/Voyage API response format
4788 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
4789 - return $response_body['data'][0]['embedding'];
4790 - } else {
4791 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
4792 - return [
4793 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
4794 - 'error_code' => 'invalid_embedding_response'
4795 - ];
4796 - }
4797 - }
4798 - } catch (Exception $e) {
4799 - //error_log('Embedding Exception: ' . $e->getMessage());
4800 - return [
4801 - 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
4802 - 'error_code' => 'embedding_exception'
4803 - ];
4804 - }
4805 -}
4806 -
4807 -
4808 -/**
4809 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
4810 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
4811 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
4812 - */
4813 -private function mxchat_generate_embedding_custom($text) {
4814 - if (empty($text)) {
4815 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
4816 - }
4817 - $cfg = $this->mxchat_resolve_custom_provider();
4818 - if (empty($cfg['base_url'])) {
4819 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
4820 - }
4821 -
4822 - $options = get_option('mxchat_options');
4823 - $embed_url = $cfg['base_url'] . '/embeddings';
4824 - if (!empty($cfg['api_version'])) {
4825 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
4826 - }
4827 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
4828 - ? trim((string) $options['custom_provider_embedding_model'])
4829 - : $cfg['model'];
4830 -
4831 - $response = wp_remote_post($embed_url, [
4832 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
4833 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
4834 - 'timeout' => 60,
4835 - ]);
4836 - if (is_wp_error($response)) {
4837 - return [
4838 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
4839 - 'error_code' => 'embedding_custom_connection_error',
4840 - ];
4841 - }
4842 - $status = wp_remote_retrieve_response_code($response);
4843 - $body = json_decode(wp_remote_retrieve_body($response), true);
4844 - if ($status !== 200) {
4845 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
4846 - return [
4847 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
4848 - 'error_code' => 'embedding_custom_api_error',
4849 - 'status_code' => $status,
4850 - ];
4851 - }
4852 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
4853 - return $body['data'][0]['embedding'];
4854 - }
4855 - return [
4856 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
4857 - 'error_code' => 'embedding_custom_invalid_response',
4858 - ];
4859 -}
4860 -
4861 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
4862 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
4863 -
4864 - // Check for OpenAI Vector Store first (takes priority when enabled)
4865 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
4866 -
4867 - if ($bot_vectorstore_config['use_vectorstore']) {
4868 - // Get current model to verify it's an OpenAI model
4869 - $bot_options = $this->get_bot_options($bot_id);
4870 - $mxchat_options = get_option('mxchat_options', array());
4871 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
4872 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
4873 -
4874 - if ($this->is_openai_chat_model($selected_model)) {
4875 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
4876 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
4877 - } else {
4878 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
4879 - }
4880 - }
4881 -
4882 - // Get bot-specific Pinecone configuration
4883 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
4884 -
4885 - // Debug: Log the Pinecone configuration
4886 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
4887 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
4888 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
4889 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
4890 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
4891 -
4892 - // Determine whether to use Pinecone based on bot configuration
4893 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
4894 -
4895 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
4896 -
4897 - if ($use_pinecone) {
4898 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
4899 - } else {
4900 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
4901 - }
4902 -}
4903 -
4904 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
4905 - global $wpdb;
4906 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
4907 - // Initialize similarity analysis storage
4908 - $this->last_similarity_analysis = [
4909 - 'knowledge_base_type' => 'WordPress Database',
4910 - 'bot_id' => $bot_id,
4911 - 'top_matches' => [],
4912 - 'threshold_used' => 0,
4913 - 'total_checked' => 0
4914 - ];
4915 -
4916 - // NEW: Initialize valid URLs array
4917 - $valid_urls = [];
4918 -
4919 - // Get bot-specific options for similarity threshold
4920 - $bot_options = $this->get_bot_options($bot_id);
4921 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
4922 -
4923 - // Get knowledge manager instance for role checking
4924 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
4925 -
4926 - // Get base similarity threshold from bot options or default options
4927 - $similarity_threshold = isset($current_options['similarity_threshold'])
4928 - ? ((int) $current_options['similarity_threshold']) / 100
4929 - : 0.35;
4930 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
4931 -
4932 - // Precompute bot_filter once, outside the streaming loop
4933 - $bot_filter = '';
4934 - if ($bot_id !== 'default') {
4935 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
4936 - if ($column_exists) {
4937 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
4938 - }
4939 - }
4940 -
4941 - // ===== STREAMING TOP-K PASS =====
4942 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
4943 - // - top 10 by raw similarity (for the testing/debug display panel)
4944 - // - candidates above threshold with access (capped) for context assembly
4945 - // This bounds peak memory regardless of knowledge base size and avoids loading
4946 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
4947 - $batch_size = 250;
4948 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
4949 - $top_display = [];
4950 - $candidates = [];
4951 - $total_checked = 0;
4952 - $offset = 0;
4953 -
4954 - do {
4955 - $batch = $wpdb->get_results($wpdb->prepare(
4956 - "SELECT id, embedding_vector, source_url, role_restriction
4957 - FROM {$system_prompt_table}
4958 - WHERE 1=1 {$bot_filter}
4959 - LIMIT %d OFFSET %d",
4960 - $batch_size,
4961 - $offset
4962 - ));
4963 -
4964 - if (empty($batch)) {
4965 - break;
4966 - }
4967 -
4968 - foreach ($batch as $row) {
4969 - $database_embedding = $row->embedding_vector
4970 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
4971 - : null;
4972 -
4973 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
4974 - unset($database_embedding);
4975 - continue;
4976 - }
4977 -
4978 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
4979 - unset($database_embedding);
4980 -
4981 - $role_restriction = $row->role_restriction ?? 'public';
4982 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
4983 - $source_url = $row->source_url ?? '';
4984 -
4985 - // Maintain top 10 display buffer (insert-if-beats-worst)
4986 - if (count($top_display) < 10) {
4987 - $top_display[] = [
4988 - 'id' => $row->id,
4989 - 'similarity' => $similarity,
4990 - 'source_url' => $source_url,
4991 - 'role_restriction' => $role_restriction,
4992 - 'has_access' => $has_access,
4993 - ];
4994 - usort($top_display, function ($a, $b) {
4995 - return $b['similarity'] <=> $a['similarity'];
4996 - });
4997 - } elseif ($similarity > $top_display[9]['similarity']) {
4998 - $top_display[9] = [
4999 - 'id' => $row->id,
5000 - 'similarity' => $similarity,
5001 - 'source_url' => $source_url,
5002 - 'role_restriction' => $role_restriction,
5003 - 'has_access' => $has_access,
5004 - ];
5005 - usort($top_display, function ($a, $b) {
5006 - return $b['similarity'] <=> $a['similarity'];
5007 - });
5008 - }
5009 -
5010 - // Track candidates for context assembly (above threshold + has access)
5011 - if ($similarity >= $similarity_threshold && $has_access) {
5012 - $candidates[] = [
5013 - 'id' => $row->id,
5014 - 'similarity' => $similarity,
5015 - 'source_url' => $source_url,
5016 - ];
5017 - }
5018 -
5019 - $total_checked++;
5020 - }
5021 -
5022 - unset($batch);
5023 -
5024 - // Trim candidates periodically to cap memory during long scans
5025 - if (count($candidates) > $max_candidates) {
5026 - usort($candidates, function ($a, $b) {
5027 - return $b['similarity'] <=> $a['similarity'];
5028 - });
5029 - $candidates = array_slice($candidates, 0, $max_candidates);
5030 - }
5031 -
5032 - $offset += $batch_size;
5033 - } while (true);
5034 -
5035 - if ($total_checked === 0) {
5036 - $this->current_valid_urls = [];
5037 - return '';
5038 - }
5039 -
5040 - // Final candidates sort (best first)
5041 - if (count($candidates) > 1) {
5042 - usort($candidates, function ($a, $b) {
5043 - return $b['similarity'] <=> $a['similarity'];
5044 - });
5045 - }
5046 -
5047 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5048 - // Gather unique IDs we actually need (top_display + candidates) and pull
5049 - // article_content in bounded IN() batches. This avoids loading content for
5050 - // every row during the similarity scan.
5051 - $needed_ids = [];
5052 - foreach ($top_display as $item) {
5053 - $needed_ids[$item['id']] = true;
5054 - }
5055 - foreach ($candidates as $item) {
5056 - $needed_ids[$item['id']] = true;
5057 - }
5058 - $needed_ids = array_keys($needed_ids);
5059 -
5060 - $content_map = [];
5061 - if (!empty($needed_ids)) {
5062 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5063 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5064 - $rows = $wpdb->get_results($wpdb->prepare(
5065 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5066 - ...$chunk_ids
5067 - ));
5068 - foreach ($rows as $r) {
5069 - $content_map[$r->id] = $r->article_content;
5070 - }
5071 - unset($rows);
5072 - }
5073 - }
5074 -
5075 - // Build the all_similarities display array from the top 10
5076 - $all_similarities = [];
5077 - foreach ($top_display as $item) {
5078 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5079 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5080 - $is_chunk = $parsed_for_display['is_chunked'];
5081 - $chunk_meta = $parsed_for_display['metadata'];
5082 -
5083 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5084 - $source_display = $item['source_url'];
5085 - } else {
5086 - $content_preview = strip_tags($article_content_for_parse);
5087 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5088 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5089 - }
5090 -
5091 - $all_similarities[] = [
5092 - 'document_id' => $item['id'],
5093 - 'similarity' => $item['similarity'],
5094 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5095 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5096 - 'source_display' => $source_display,
5097 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5098 - 'used_for_context' => false,
5099 - 'role_restriction' => $item['role_restriction'],
5100 - 'has_access' => $item['has_access'],
5101 - 'filtered_out' => !$item['has_access'],
5102 - 'is_chunk' => $is_chunk,
5103 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5104 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5105 - ];
5106 - }
5107 -
5108 - // Build url_groups from candidates for chunk reassembly
5109 - $url_groups = array();
5110 - foreach ($candidates as $cand) {
5111 - $article_content = $content_map[$cand['id']] ?? '';
5112 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5113 - $is_chunked = $parsed['is_chunked'];
5114 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5115 - $text_content = $parsed['text'];
5116 -
5117 - $source_url = $cand['source_url'];
5118 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5119 -
5120 - if (!isset($url_groups[$group_key])) {
5121 - $url_groups[$group_key] = array(
5122 - 'source_url' => $source_url,
5123 - 'best_score' => 0,
5124 - 'is_chunked' => $is_chunked,
5125 - 'chunks' => array(),
5126 - 'single_text' => '',
5127 - 'single_id' => null
5128 - );
5129 - }
5130 -
5131 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5132 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5133 - }
5134 -
5135 - if ($is_chunked) {
5136 - $url_groups[$group_key]['is_chunked'] = true;
5137 - $url_groups[$group_key]['chunks'][] = array(
5138 - 'id' => $cand['id'],
5139 - 'score' => $cand['similarity'],
5140 - 'chunk_index' => $chunk_index,
5141 - 'text' => $text_content
5142 - );
5143 - } else {
5144 - $url_groups[$group_key]['single_text'] = $text_content;
5145 - $url_groups[$group_key]['single_id'] = $cand['id'];
5146 - }
5147 - }
5148 -
5149 - // Sort ALL similarities for testing display (highest first)
5150 - usort($all_similarities, function ($a, $b) {
5151 - return $b['similarity'] <=> $a['similarity'];
5152 - });
5153 -
5154 - // Sort URL groups by best score (highest first)
5155 - uasort($url_groups, function($a, $b) {
5156 - return $b['best_score'] <=> $a['best_score'];
5157 - });
5158 -
5159 - // Get RAG sources limit from options (default 6, min 3, max 10)
5160 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5161 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5162 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5163 -
5164 - // Take top N unique URLs based on user setting
5165 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5166 -
5167 - // Track which document IDs are used for context
5168 - $used_document_ids = [];
5169 - foreach ($top_urls as $group) {
5170 - if ($group['is_chunked']) {
5171 - foreach ($group['chunks'] as $chunk) {
5172 - $used_document_ids[] = $chunk['id'];
5173 - }
5174 - } elseif ($group['single_id']) {
5175 - $used_document_ids[] = $group['single_id'];
5176 - }
5177 - }
5178 -
5179 - // Update the all_similarities array to mark which were actually used
5180 - foreach ($all_similarities as &$similarity_item) {
5181 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5182 - }
5183 -
5184 - // Store top 10 for testing panel
5185 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5186 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5187 -
5188 - // Initialize final content
5189 - $content = '';
5190 - $matches_used = 0;
5191 - $total_chunks_used = 0;
5192 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5193 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5194 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5195 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5196 -
5197 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5198 - // Use fresh options to ensure we get the latest setting value
5199 - $fresh_options = get_option('mxchat_options', []);
5200 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5201 -
5202 - // Build content from top sources
5203 - foreach ($top_urls as $group_key => $group) {
5204 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5205 -
5206 - // Stop if we've hit the total chunk limit
5207 - if ($total_chunks_used >= $max_total_chunks) {
5208 - break;
5209 - }
5210 -
5211 - $full_text = '';
5212 - $chunks_in_this_source = 1; // Default for non-chunked content
5213 -
5214 - if ($group['is_chunked']) {
5215 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5216 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5217 -
5218 - // Fetch chunks for this URL with limit
5219 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5220 -
5221 - // If fetching all chunks fails, fall back to matched chunks
5222 - if (empty($full_text)) {
5223 - // Sort matched chunks by index and concatenate
5224 - usort($group['chunks'], function($a, $b) {
5225 - return $a['chunk_index'] <=> $b['chunk_index'];
5226 - });
5227 -
5228 - $chunk_texts = array();
5229 - $chunks_in_this_source = 0;
5230 - foreach ($group['chunks'] as $chunk) {
5231 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5232 - break;
5233 - }
5234 - $chunk_texts[] = $chunk['text'];
5235 - $chunks_in_this_source++;
5236 - }
5237 - $full_text = implode("\n\n", $chunk_texts);
5238 - }
5239 - } else {
5240 - $full_text = $group['single_text'];
5241 - $chunks_in_this_source = 1;
5242 - }
5243 -
5244 - if (!empty($full_text)) {
5245 - // Strip URLs from content if citation links are disabled
5246 - if (!$citation_links_enabled) {
5247 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5248 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5249 - }
5250 -
5251 - // Use numbered reference for URL-based entries, plain info label for manual entries
5252 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5253 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5254 - $matches_used++;
5255 - $content .= "## Reference " . $matches_used . " ##\n";
5256 - $content .= $full_text . "\n\n";
5257 -
5258 - // Only include citation URLs if citation links are enabled
5259 - if ($citation_links_enabled) {
5260 - $valid_urls[] = $source_url;
5261 - $content .= "URL: " . $source_url . "\n\n";
5262 - }
5263 - } else {
5264 - // Manual entry — no reference number, no citation
5265 - $content .= "## Information ##\n";
5266 - $content .= $full_text . "\n\n";
5267 - }
5268 -
5269 - // Extract any URLs from the text content itself (only if citation links enabled)
5270 - if ($citation_links_enabled) {
5271 - preg_match_all(
5272 - '#\bhttps?://[^\s<>"\']+#i',
5273 - $full_text,
5274 - $content_urls
5275 - );
5276 - if (!empty($content_urls[0])) {
5277 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5278 - }
5279 - }
5280 -
5281 - $total_chunks_used += $chunks_in_this_source;
5282 - }
5283 - }
5284 -
5285 - // NEW: Store unique valid URLs for validation
5286 - $this->current_valid_urls = array_unique($valid_urls);
5287 -
5288 - // Store sources and chunks counts for testing/transcript display
5289 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5290 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5291 -
5292 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5293 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5294 -
5295 - // Add response guidelines
5296 - if (empty($top_urls)) {
5297 - $content = "No reference information was found for this query.\n\n";
5298 - } else {
5299 - // Build response guidelines based on citation links setting
5300 - $content .= "\n## Response Guidelines ##\n" .
5301 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5302 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5303 - "If you don't have specific information or are uncertain about any details, it's always " .
5304 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5305 - "When information is incomplete, let them know you are unsure.\n\n";
5306 -
5307 - // Only add hyperlink instructions if citation links are enabled
5308 - if ($citation_links_enabled) {
5309 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5310 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5311 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5312 - } else {
5313 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5314 - "Simply provide helpful answers based on the reference information without citing sources.";
5315 - }
5316 - }
5317 -
5318 - return trim($content);
5319 -}
5320 -
5321 -/**
5322 - * Fetch and reassemble chunks for a URL from WordPress database
5323 - *
5324 - * @param string $source_url The source URL to fetch chunks for
5325 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5326 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5327 - * @return string Reassembled content from chunks
5328 - */
5329 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5330 - global $wpdb;
5331 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5332 -
5333 - // Fetch all rows with this source_url
5334 - $rows = $wpdb->get_results($wpdb->prepare(
5335 - "SELECT article_content FROM {$table}
5336 - WHERE source_url = %s
5337 - ORDER BY id ASC",
5338 - $source_url
5339 - ));
5340 -
5341 - if (empty($rows)) {
5342 - $chunk_count = 0;
5343 - return '';
5344 - }
5345 -
5346 - // Parse and sort chunks by index
5347 - $chunks = array();
5348 - foreach ($rows as $row) {
5349 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5350 -
5351 - if ($parsed['is_chunked']) {
5352 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5353 - $chunks[$chunk_index] = $parsed['text'];
5354 - } else {
5355 - // Non-chunked content - just return it
5356 - $chunks[] = $parsed['text'];
5357 - }
5358 - }
5359 -
5360 - // Sort by chunk index
5361 - ksort($chunks);
5362 -
5363 - // Apply chunk limit if specified
5364 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5365 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5366 - }
5367 -
5368 - // Store actual chunk count
5369 - $chunk_count = count($chunks);
5370 -
5371 - // Reassemble content
5372 - return implode("\n\n", $chunks);
5373 -}
5374 -
5375 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5376 - global $wpdb;
5377 -
5378 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5379 - //error_log(" - bot_id: " . $bot_id);
5380 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5381 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5382 -
5383 - // Use bot-specific config or fall back to default
5384 - if ($bot_config === null) {
5385 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5386 - }
5387 -
5388 - $api_key = $bot_config['api_key'] ?? '';
5389 - $host = $bot_config['host'] ?? '';
5390 - $namespace = $bot_config['namespace'] ?? '';
5391 -
5392 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5393 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5394 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5395 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5396 -
5397 - // Initialize similarity analysis storage
5398 - $this->last_similarity_analysis = [
5399 - 'knowledge_base_type' => 'Pinecone',
5400 - 'bot_id' => $bot_id,
5401 - 'namespace' => $namespace,
5402 - 'top_matches' => [],
5403 - 'threshold_used' => 0,
5404 - 'total_checked' => 0
5405 - ];
5406 -
5407 - // NEW: Initialize valid URLs array
5408 - $valid_urls = [];
5409 -
5410 - if (empty($host) || empty($api_key)) {
5411 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5412 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5413 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5414 - // Store empty array for valid URLs since we can't proceed
5415 - $this->current_valid_urls = [];
5416 - return '';
5417 - }
5418 -
5419 - // Get knowledge manager instance for role checking
5420 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5421 -
5422 - // Get the similarity threshold from the bot options or main options
5423 - $bot_options = $this->get_bot_options($bot_id);
5424 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
5425 -
5426 - $similarity_threshold = isset($current_options['similarity_threshold'])
5427 - ? ((int) $current_options['similarity_threshold']) / 100
5428 - : 0.35;
5429 -
5430 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5431 -
5432 - // Prepare the query request for Pinecone
5433 - $api_endpoint = "https://{$host}/query";
5434 -
5435 - $request_body = array(
5436 - 'vector' => $user_embedding,
5437 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
5438 - 'includeMetadata' => true,
5439 - 'includeValues' => true
5440 - );
5441 -
5442 - // Add namespace if specified for this bot
5443 - if (!empty($namespace)) {
5444 - $request_body['namespace'] = $namespace;
5445 - }
5446 -
5447 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5448 - //error_log(" - Endpoint: " . $api_endpoint);
5449 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5450 -
5451 - $response = wp_remote_post($api_endpoint, array(
5452 - 'headers' => array(
5453 - 'Api-Key' => $api_key,
5454 - 'accept' => 'application/json',
5455 - 'content-type' => 'application/json'
5456 - ),
5457 - 'body' => wp_json_encode($request_body),
5458 - 'timeout' => 30
5459 - ));
5460 -
5461 - if (is_wp_error($response)) {
5462 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5463 - // Store empty array for valid URLs
5464 - $this->current_valid_urls = [];
5465 - return '';
5466 - }
5467 -
5468 - $response_code = wp_remote_retrieve_response_code($response);
5469 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5470 -
5471 - if ($response_code !== 200) {
5472 - $response_body = wp_remote_retrieve_body($response);
5473 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5474 - // Store empty array for valid URLs
5475 - $this->current_valid_urls = [];
5476 - return '';
5477 - }
5478 -
5479 - // ADD DETAILED DEBUG SECTION HERE
5480 - $response_body = wp_remote_retrieve_body($response);
5481 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5482 -
5483 - $results = json_decode($response_body, true);
5484 -
5485 - if (json_last_error() !== JSON_ERROR_NONE) {
5486 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5487 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5488 - // Store empty array for valid URLs
5489 - $this->current_valid_urls = [];
5490 - return '';
5491 - }
5492 -
5493 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5494 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5495 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5496 -
5497 - if (empty($results['matches'])) {
5498 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5499 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5500 - // Store empty array for valid URLs
5501 - $this->current_valid_urls = [];
5502 - return '';
5503 - }
5504 -
5505 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
5506 -
5507 - // Log first match details for debugging
5508 - if (!empty($results['matches'][0])) {
5509 - $first_match = $results['matches'][0];
5510 - //error_log("MXCHAT DEBUG: First match details:");
5511 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
5512 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5513 - if (isset($first_match['metadata'])) {
5514 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5515 - }
5516 - }
5517 -
5518 - // Initialize the final content
5519 - $content = '';
5520 - $matches_used = 0;
5521 - $matches_used_for_context = [];
5522 - $total_chunks_used = 0;
5523 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5524 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5525 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5526 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5527 -
5528 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5529 - // Use fresh options to ensure we get the latest setting value
5530 - $fresh_options = get_option('mxchat_options', []);
5531 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5532 -
5533 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
5534 - $url_groups = array();
5535 -
5536 - foreach ($results['matches'] as $index => $match) {
5537 - // Skip if similarity is below threshold
5538 - if ($match['score'] < $similarity_threshold) {
5539 - continue;
5540 - }
5541 -
5542 - $metadata = $match['metadata'] ?? array();
5543 - $source_url = $metadata['source_url'] ?? '';
5544 - $match_id = $match['id'] ?? '';
5545 -
5546 - // LAZY ROLE CHECK: Only check role for content we're actually considering
5547 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
5548 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5549 -
5550 - // Skip if user doesn't have access
5551 - if (!$has_access) {
5552 - continue;
5553 - }
5554 -
5555 - // Use a unique key for manual entries without a source URL
5556 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
5557 -
5558 - // Group by source URL (or unique key for manual entries)
5559 - if (!isset($url_groups[$group_key])) {
5560 - $url_groups[$group_key] = array(
5561 - 'source_url' => $source_url,
5562 - 'best_score' => 0,
5563 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
5564 - 'chunks' => array(),
5565 - 'single_text' => ''
5566 - );
5567 - }
5568 -
5569 - // Track best score for this group
5570 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
5571 - $url_groups[$group_key]['best_score'] = $match['score'];
5572 - }
5573 -
5574 - // Store chunk info or single text
5575 - if ($url_groups[$group_key]['is_chunked']) {
5576 - $url_groups[$group_key]['chunks'][] = array(
5577 - 'id' => $match_id,
5578 - 'score' => $match['score'],
5579 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
5580 - 'text' => $metadata['text'] ?? ''
5581 - );
5582 - } else {
5583 - // Non-chunked content - just store the text
5584 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
5585 - $url_groups[$group_key]['single_id'] = $match_id;
5586 - }
5587 - }
5588 -
5589 - // Sort URL groups by best score (highest first)
5590 - uasort($url_groups, function($a, $b) {
5591 - return $b['best_score'] <=> $a['best_score'];
5592 - });
5593 -
5594 - // Get RAG sources limit from options (default 6, min 3, max 10)
5595 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5596 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5597 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5598 -
5599 - // Take top N unique URLs based on user setting
5600 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5601 -
5602 - // Track which match IDs are actually used for context
5603 - foreach ($top_urls as $group) {
5604 - if ($group['is_chunked']) {
5605 - foreach ($group['chunks'] as $chunk) {
5606 - $matches_used_for_context[] = $chunk['id'];
5607 - }
5608 - } elseif (!empty($group['single_id'])) {
5609 - $matches_used_for_context[] = $group['single_id'];
5610 - }
5611 - }
5612 -
5613 - // Build content from top sources
5614 - foreach ($top_urls as $group_key => $group) {
5615 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5616 -
5617 - // Stop if we've hit the total chunk limit
5618 - if ($total_chunks_used >= $max_total_chunks) {
5619 - break;
5620 - }
5621 -
5622 - $full_text = '';
5623 - $chunks_in_this_source = 1; // Default for non-chunked content
5624 -
5625 - if ($group['is_chunked']) {
5626 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5627 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5628 -
5629 - // Fetch chunks for this URL with limit
5630 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
5631 -
5632 - // If fetching all chunks fails, fall back to matched chunks
5633 - if (empty($full_text)) {
5634 - // Sort matched chunks by index and concatenate
5635 - usort($group['chunks'], function($a, $b) {
5636 - return $a['chunk_index'] <=> $b['chunk_index'];
5637 - });
5638 -
5639 - $chunk_texts = array();
5640 - $chunks_in_this_source = 0;
5641 - foreach ($group['chunks'] as $chunk) {
5642 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5643 - break;
5644 - }
5645 - $chunk_texts[] = $chunk['text'];
5646 - $chunks_in_this_source++;
5647 - }
5648 - $full_text = implode("\n\n", $chunk_texts);
5649 - }
5650 - } else {
5651 - $full_text = $group['single_text'];
5652 - $chunks_in_this_source = 1;
5653 - }
5654 -
5655 - if (!empty($full_text)) {
5656 - // Strip URLs from content if citation links are disabled
5657 - if (!$citation_links_enabled) {
5658 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5659 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5660 - }
5661 -
5662 - // Use numbered reference for URL-based entries, plain info label for manual entries
5663 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5664 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5665 - $matches_used++;
5666 - $content .= "## Reference " . $matches_used . " ##\n";
5667 - $content .= $full_text . "\n\n";
5668 -
5669 - // Only include citation URLs if citation links are enabled
5670 - if ($citation_links_enabled) {
5671 - $valid_urls[] = $source_url;
5672 - $content .= "URL: " . $source_url . "\n\n";
5673 - }
5674 - } else {
5675 - // Manual entry — no reference number, no citation
5676 - $content .= "## Information ##\n";
5677 - $content .= $full_text . "\n\n";
5678 - }
5679 -
5680 - // Extract any URLs from the text content itself (only if citation links enabled)
5681 - if ($citation_links_enabled) {
5682 - preg_match_all(
5683 - '#\bhttps?://[^\s<>"\']+#i',
5684 - $full_text,
5685 - $content_urls
5686 - );
5687 - if (!empty($content_urls[0])) {
5688 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5689 - }
5690 - }
5691 -
5692 - $total_chunks_used += $chunks_in_this_source;
5693 - }
5694 - }
5695 -
5696 - // Process ALL matches for testing data (top 10) - with role checking for testing display
5697 - $all_matches = [];
5698 - foreach ($results['matches'] as $index => $match) {
5699 - if ($index >= 10) break; // Limit to top 10 for testing
5700 -
5701 - $match_id = $match['id'] ?? '';
5702 -
5703 - // Check role access for testing display (use cache if available)
5704 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
5705 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5706 -
5707 - $source_display = '';
5708 - if (!empty($match['metadata']['source_url'])) {
5709 - $source_display = $match['metadata']['source_url'];
5710 - } else {
5711 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
5712 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5713 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5714 - }
5715 -
5716 - $match_id_for_display = $match['id'] ?? $index;
5717 -
5718 - // Check for chunk metadata in Pinecone
5719 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
5720 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
5721 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
5722 -
5723 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
5724 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
5725 - $is_chunk = true;
5726 - }
5727 -
5728 - $all_matches[] = [
5729 - 'document_id' => $match_id_for_display,
5730 - 'similarity' => $match['score'],
5731 - 'similarity_percentage' => round($match['score'] * 100, 2),
5732 - 'above_threshold' => $match['score'] >= $similarity_threshold,
5733 - 'source_display' => $source_display,
5734 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
5735 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
5736 - 'role_restriction' => $role_restriction,
5737 - 'has_access' => $has_access,
5738 - 'filtered_out' => !$has_access,
5739 - 'is_chunk' => $is_chunk,
5740 - 'chunk_index' => $chunk_index,
5741 - 'total_chunks' => $total_chunks
5742 - ];
5743 - }
5744 -
5745 - // Store for testing panel
5746 - $this->last_similarity_analysis['top_matches'] = $all_matches;
5747 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
5748 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5749 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5750 -
5751 - // NEW: Store unique valid URLs for validation
5752 - $this->current_valid_urls = array_unique($valid_urls);
5753 -
5754 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5755 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5756 -
5757 - // Add response guidelines
5758 - if ($matches_used === 0) {
5759 - $content = "No reference information was found for this query.\n\n";
5760 - } else {
5761 - // Build response guidelines based on citation links setting
5762 - $content .= "\n## Response Guidelines ##\n" .
5763 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5764 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5765 - "If you don't have specific information or are uncertain about any details, it's always " .
5766 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5767 - "When information is incomplete, let them know you are unsure.\n\n";
5768 -
5769 - // Only add hyperlink instructions if citation links are enabled
5770 - if ($citation_links_enabled) {
5771 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5772 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5773 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5774 - } else {
5775 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5776 - "Simply provide helpful answers based on the reference information without citing sources.";
5777 - }
5778 - }
5779 -
5780 - return trim($content);
5781 -}
5782 -
5783 -/**
5784 - * Get role restriction for a single vector (with caching)
5785 - */
5786 -private function get_single_vector_role($vector_id, $metadata = array()) {
5787 - global $wpdb;
5788 -
5789 - if (empty($vector_id)) {
5790 - return 'public';
5791 - }
5792 -
5793 - // Check cache first
5794 - $cache_key = 'mxchat_vector_role_' . $vector_id;
5795 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
5796 -
5797 - if ($cached_role !== false) {
5798 - return $cached_role;
5799 - }
5800 -
5801 - $role_restriction = 'public';
5802 -
5803 - // First try Pinecone metadata
5804 - if (!empty($metadata['role_restriction'])) {
5805 - $role_restriction = $metadata['role_restriction'];
5806 - } else {
5807 - // Check WordPress table for user-modified roles
5808 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
5809 - $stored_role = $wpdb->get_var($wpdb->prepare(
5810 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
5811 - $vector_id
5812 - ));
5813 -
5814 - if ($stored_role) {
5815 - $role_restriction = $stored_role;
5816 - }
5817 - }
5818 -
5819 - // Cache individual role for 1 hour
5820 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
5821 -
5822 - return $role_restriction;
5823 -}
5824 -
5825 -/**
5826 - * Fetch and reassemble all chunks for a URL from Pinecone
5827 - *
5828 - * @param string $source_url The source URL to fetch chunks for
5829 - * @param array $bot_config Bot-specific Pinecone configuration
5830 - * @return string Reassembled content from all chunks
5831 - */
5832 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
5833 - $api_key = $bot_config['api_key'] ?? '';
5834 - $host = $bot_config['host'] ?? '';
5835 - $namespace = $bot_config['namespace'] ?? '';
5836 -
5837 - if (empty($host) || empty($api_key)) {
5838 - $chunk_count = 0;
5839 - return '';
5840 - }
5841 -
5842 - $base_hash = md5($source_url);
5843 -
5844 - // Use Pinecone list API to find all chunk vectors with this prefix
5845 - $list_url = "https://{$host}/vectors/list";
5846 -
5847 - // Limit to max_chunks if specified, otherwise fetch up to 100
5848 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
5849 -
5850 - $list_body = array(
5851 - 'prefix' => $base_hash . '_chunk_',
5852 - 'limit' => $fetch_limit
5853 - );
5854 -
5855 - if (!empty($namespace)) {
5856 - $list_body['namespace'] = $namespace;
5857 - }
5858 -
5859 - $list_response = wp_remote_post($list_url, array(
5860 - 'headers' => array(
5861 - 'Api-Key' => $api_key,
5862 - 'accept' => 'application/json',
5863 - 'content-type' => 'application/json'
5864 - ),
5865 - 'body' => wp_json_encode($list_body),
5866 - 'timeout' => 30
5867 - ));
5868 -
5869 - if (is_wp_error($list_response)) {
5870 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
5871 - return '';
5872 - }
5873 -
5874 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
5875 -
5876 - if (empty($list_data['vectors'])) {
5877 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
5878 - return '';
5879 - }
5880 -
5881 - // Extract vector IDs
5882 - $vector_ids = array();
5883 - foreach ($list_data['vectors'] as $vector) {
5884 - if (isset($vector['id'])) {
5885 - $vector_ids[] = $vector['id'];
5886 - }
5887 - }
5888 -
5889 - if (empty($vector_ids)) {
5890 - return '';
5891 - }
5892 -
5893 - // Fetch all chunk content
5894 - $fetch_url = "https://{$host}/vectors/fetch";
5895 -
5896 - $fetch_body = array(
5897 - 'ids' => $vector_ids
5898 - );
5899 -
5900 - if (!empty($namespace)) {
5901 - $fetch_body['namespace'] = $namespace;
5902 - }
5903 -
5904 - $fetch_response = wp_remote_post($fetch_url, array(
5905 - 'headers' => array(
5906 - 'Api-Key' => $api_key,
5907 - 'accept' => 'application/json',
5908 - 'content-type' => 'application/json'
5909 - ),
5910 - 'body' => wp_json_encode($fetch_body),
5911 - 'timeout' => 30
5912 - ));
5913 -
5914 - if (is_wp_error($fetch_response)) {
5915 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
5916 - return '';
5917 - }
5918 -
5919 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
5920 -
5921 - if (empty($fetch_data['vectors'])) {
5922 - return '';
5923 - }
5924 -
5925 - // Sort chunks by index and reassemble
5926 - $chunks = array();
5927 - foreach ($fetch_data['vectors'] as $id => $vector) {
5928 - $metadata = $vector['metadata'] ?? array();
5929 - $chunk_index = $metadata['chunk_index'] ?? 0;
5930 - $text = $metadata['text'] ?? '';
5931 -
5932 - // Store chunk with its index
5933 - $chunks[$chunk_index] = $text;
5934 - }
5935 -
5936 - // Sort by chunk index
5937 - ksort($chunks);
5938 -
5939 - // Apply chunk limit if specified
5940 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5941 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5942 - }
5943 -
5944 - // Store actual chunk count
5945 - $chunk_count = count($chunks);
5946 -
5947 - // Reassemble content
5948 - return implode("\n\n", $chunks);
5949 -}
5950 -
5951 -/**
5952 - * Search for relevant content using OpenAI Vector Store (File Search)
5953 - *
5954 - * @param string $user_query The user's query text
5955 - * @param string $bot_id The bot ID
5956 - * @param array $vectorstore_config Vector Store configuration
5957 - * @return string Formatted context string with references
5958 - */
5959 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
5960 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
5961 - //error_log(" - bot_id: " . $bot_id);
5962 - //error_log(" - user_query length: " . strlen($user_query));
5963 -
5964 - // Get OpenAI API key
5965 - $mxchat_options = get_option('mxchat_options', array());
5966 - $api_key = $mxchat_options['api_key'] ?? '';
5967 -
5968 - // Reset vectorstore error tracking
5969 - $this->last_vectorstore_error = null;
5970 -
5971 - if (empty($api_key)) {
5972 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
5973 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
5974 - $this->current_valid_urls = [];
5975 - return '';
5976 - }
5977 -
5978 - // Get Vector Store configuration
5979 - if (empty($vectorstore_config)) {
5980 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5981 - }
5982 -
5983 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
5984 - $max_results = $vectorstore_config['max_results'] ?? 5;
5985 -
5986 - if (empty($vectorstore_ids_string)) {
5987 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
5988 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
5989 - $this->current_valid_urls = [];
5990 - return '';
5991 - }
5992 -
5993 - // Parse Vector Store IDs
5994 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
5995 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
5996 -
5997 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
5998 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
5999 -
6000 - // Initialize similarity analysis storage
6001 - $this->last_similarity_analysis = [
6002 - 'knowledge_base_type' => 'OpenAI Vector Store',
6003 - 'bot_id' => $bot_id,
6004 - 'vectorstore_ids' => $vectorstore_ids,
6005 - 'top_matches' => [],
6006 - 'threshold_used' => 0,
6007 - 'total_checked' => 0
6008 - ];
6009 -
6010 - $valid_urls = [];
6011 -
6012 - // Get the selected model
6013 - $bot_options = $this->get_bot_options($bot_id);
6014 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6015 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6016 -
6017 - // Verify it's an OpenAI model
6018 - if (!$this->is_openai_chat_model($selected_model)) {
6019 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6020 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6021 - $this->current_valid_urls = [];
6022 - return '';
6023 - }
6024 -
6025 - // Use OpenAI Responses API with file_search tool
6026 - $request_body = array(
6027 - 'model' => $selected_model,
6028 - 'input' => $user_query,
6029 - 'tools' => array(
6030 - array(
6031 - 'type' => 'file_search',
6032 - 'vector_store_ids' => $vectorstore_ids,
6033 - 'max_num_results' => intval($max_results)
6034 - )
6035 - ),
6036 - 'include' => array('output[*].file_search_call.search_results')
6037 - );
6038 -
6039 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6040 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6041 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6042 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6043 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6044 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6045 -
6046 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6047 - 'headers' => array(
6048 - 'Authorization' => 'Bearer ' . $api_key,
6049 - 'Content-Type' => 'application/json'
6050 - ),
6051 - 'body' => wp_json_encode($request_body),
6052 - 'timeout' => 60
6053 - ));
6054 -
6055 - if (is_wp_error($response)) {
6056 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6057 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6058 - $this->current_valid_urls = [];
6059 - return '';
6060 - }
6061 -
6062 - $response_code = wp_remote_retrieve_response_code($response);
6063 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6064 -
6065 - $response_body = wp_remote_retrieve_body($response);
6066 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6067 -
6068 - if ($response_code !== 200) {
6069 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6070 - $api_error_detail = '';
6071 - $decoded_error = json_decode($response_body, true);
6072 - if (isset($decoded_error['error']['message'])) {
6073 - $api_error_detail = $decoded_error['error']['message'];
6074 - }
6075 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6076 - $this->current_valid_urls = [];
6077 - return '';
6078 - }
6079 - $result = json_decode($response_body, true);
6080 -
6081 - if (json_last_error() !== JSON_ERROR_NONE) {
6082 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6083 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6084 - $this->current_valid_urls = [];
6085 - return '';
6086 - }
6087 -
6088 - // Debug: Log the structure of the result
6089 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6090 - if (isset($result['output'])) {
6091 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6092 - foreach ($result['output'] as $idx => $out) {
6093 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6094 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6095 - }
6096 - } else {
6097 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6098 - }
6099 -
6100 - // Extract file search results from the response
6101 - $content = '';
6102 - $matches_used = 0;
6103 - $all_matches = [];
6104 -
6105 - // The Responses API returns output array with tool results
6106 - if (isset($result['output']) && is_array($result['output'])) {
6107 - foreach ($result['output'] as $output_item) {
6108 - // Look for file_search_call results
6109 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6110 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6111 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6112 -
6113 - // Check for search_results in the output item directly
6114 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6115 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6116 -
6117 - if (empty($search_results)) {
6118 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6119 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6120 - }
6121 -
6122 - foreach ($search_results as $index => $search_result) {
6123 - $filename = $search_result['filename'] ?? '';
6124 - $score = $search_result['score'] ?? 0;
6125 - $text_content = '';
6126 -
6127 - // Extract text content from the result
6128 - // The text can be directly on the result OR nested under content array
6129 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6130 - // Direct text field (OpenAI's actual format)
6131 - $text_content = $search_result['text'];
6132 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6133 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6134 - // Nested content array format
6135 - foreach ($search_result['content'] as $content_item) {
6136 - if (isset($content_item['text'])) {
6137 - $text_content .= $content_item['text'] . "\n";
6138 - }
6139 - }
6140 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6141 - } else {
6142 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6143 - }
6144 -
6145 - if (!empty($text_content)) {
6146 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6147 - $content .= trim($text_content) . "\n\n";
6148 -
6149 - if (!empty($filename)) {
6150 - $content .= "Source: " . $filename . "\n\n";
6151 - }
6152 -
6153 - // Extract URLs from content
6154 - preg_match_all(
6155 - '#\bhttps?://[^\s<>"\']+#i',
6156 - $text_content,
6157 - $content_urls
6158 - );
6159 - if (!empty($content_urls[0])) {
6160 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6161 - }
6162 -
6163 - $matches_used++;
6164 - }
6165 -
6166 - // Store for similarity analysis
6167 - $all_matches[] = [
6168 - 'document_id' => $filename ?: ('result_' . $index),
6169 - 'similarity' => $score,
6170 - 'similarity_percentage' => round($score * 100, 2),
6171 - 'above_threshold' => true,
6172 - 'source_display' => $filename,
6173 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6174 - 'used_for_context' => true,
6175 - 'role_restriction' => 'public',
6176 - 'has_access' => true,
6177 - 'filtered_out' => false
6178 - ];
6179 - }
6180 - }
6181 -
6182 - // Also check for message content with annotations (citations)
6183 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6184 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6185 - foreach ($output_item['content'] as $content_block) {
6186 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6187 - foreach ($content_block['annotations'] as $annotation) {
6188 - if (isset($annotation['filename'])) {
6189 - $filename = $annotation['filename'];
6190 - $score = $annotation['score'] ?? 0;
6191 - $text_content = '';
6192 -
6193 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6194 - foreach ($annotation['content'] as $ann_content) {
6195 - if (isset($ann_content['text'])) {
6196 - $text_content .= $ann_content['text'] . "\n";
6197 - }
6198 - }
6199 - }
6200 -
6201 - if (!empty($text_content) && $matches_used < $max_results) {
6202 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6203 - $content .= trim($text_content) . "\n\n";
6204 - $content .= "Source: " . $filename . "\n\n";
6205 -
6206 - preg_match_all(
6207 - '#\bhttps?://[^\s<>"\']+#i',
6208 - $text_content,
6209 - $content_urls
6210 - );
6211 - if (!empty($content_urls[0])) {
6212 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6213 - }
6214 -
6215 - $matches_used++;
6216 -
6217 - $all_matches[] = [
6218 - 'document_id' => $filename,
6219 - 'similarity' => $score,
6220 - 'similarity_percentage' => round($score * 100, 2),
6221 - 'above_threshold' => true,
6222 - 'source_display' => $filename,
6223 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6224 - 'used_for_context' => true,
6225 - 'role_restriction' => 'public',
6226 - 'has_access' => true,
6227 - 'filtered_out' => false
6228 - ];
6229 - }
6230 - }
6231 - }
6232 - }
6233 - }
6234 - }
6235 - }
6236 - }
6237 - }
6238 -
6239 - // Store for testing panel
6240 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6241 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6242 -
6243 - // Store unique valid URLs for validation
6244 - $this->current_valid_urls = array_unique($valid_urls);
6245 -
6246 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6247 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6248 -
6249 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6250 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6251 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6252 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6253 - if ($matches_used > 0) {
6254 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6255 - }
6256 -
6257 - // Check if citation links are enabled
6258 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6259 -
6260 - // Add response guidelines
6261 - if ($matches_used === 0) {
6262 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6263 - $content = "No reference information was found for this query.\n\n";
6264 - } else {
6265 - // Build response guidelines based on citation links setting
6266 - $content .= "\n## Response Guidelines ##\n" .
6267 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6268 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6269 - "If you don't have specific information or are uncertain about any details, it's always " .
6270 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6271 - "When information is incomplete, let them know you are unsure.\n\n";
6272 -
6273 - // Only add hyperlink instructions if citation links are enabled
6274 - if ($citation_links_enabled) {
6275 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6276 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6277 - } else {
6278 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6279 - "Simply provide helpful answers based on the reference information without citing sources.";
6280 - }
6281 - }
6282 -
6283 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6284 -
6285 - return trim($content);
6286 -}
6287 -
6288 -/**
6289 - * Check if the given model is an OpenAI chat model
6290 - *
6291 - * @param string $model The model ID
6292 - * @return bool True if it's an OpenAI model
6293 - */
6294 -private function is_openai_chat_model($model) {
6295 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6296 - foreach ($openai_prefixes as $prefix) {
6297 - if (strpos($model, $prefix) === 0) {
6298 - return true;
6299 - }
6300 - }
6301 - return false;
6302 -}
6303 -
6304 -/**
6305 - * Get bot-specific Vector Store configuration
6306 - *
6307 - * @param string $bot_id The bot ID
6308 - * @return array Configuration array
6309 - */
6310 -private function get_bot_vectorstore_config($bot_id = 'default') {
6311 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6312 -
6313 - // Default global settings
6314 - $default_config = array(
6315 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6316 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6317 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6318 - );
6319 -
6320 - // Allow multi-bot plugin to override with bot-specific settings
6321 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6322 -
6323 - // Preserve max_results from global settings if not set in bot config
6324 - if (!isset($bot_config['max_results'])) {
6325 - $bot_config['max_results'] = $default_config['max_results'];
6326 - }
6327 -
6328 - return $bot_config;
6329 -}
6330 -
6331 -private function mxchat_find_relevant_products($user_embedding) {
6332 - //error_log('MXChat Vector Search: Starting product search...');
6333 -
6334 - // Retrieve the add-on settings from the database
6335 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
6336 -
6337 - // Determine whether Pinecone is enabled
6338 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
6339 -
6340 - //error_log('Pinecone enabled flag: ' . $use_pinecone);
6341 -
6342 - if ($use_pinecone === 1) {
6343 - //error_log('MXChat Vector Search: Using Pinecone database for products');
6344 - return $this->find_relevant_products_pinecone($user_embedding);
6345 - } else {
6346 - //error_log('MXChat Vector Search: Using WordPress database for products');
6347 - return $this->find_relevant_products_wordpress($user_embedding);
6348 - }
6349 -}
6350 -private function find_relevant_products_wordpress($user_embedding) {
6351 - global $wpdb;
6352 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6353 -
6354 - if (!is_array($user_embedding)) {
6355 - return '';
6356 - }
6357 -
6358 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6359 - // results above the similarity threshold. Peak memory is bounded by
6360 - // $batch_size embedding rows plus a 3-element top list.
6361 - $batch_size = 250;
6362 - $similarity_threshold = 0.85;
6363 - $top_k = 3;
6364 - $top_results = [];
6365 - $offset = 0;
6366 -
6367 - do {
6368 - $batch = $wpdb->get_results($wpdb->prepare(
6369 - "SELECT id, embedding_vector
6370 - FROM {$system_prompt_table}
6371 - LIMIT %d OFFSET %d",
6372 - $batch_size,
6373 - $offset
6374 - ));
6375 -
6376 - if (empty($batch)) {
6377 - break;
6378 - }
6379 -
6380 - foreach ($batch as $row) {
6381 - $database_embedding = $row->embedding_vector
6382 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6383 - : null;
6384 -
6385 - if (!is_array($database_embedding)) {
6386 - unset($database_embedding);
6387 - continue;
6388 - }
6389 -
6390 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6391 - unset($database_embedding);
6392 -
6393 - if ($similarity < $similarity_threshold) {
6394 - continue;
6395 - }
6396 -
6397 - // Insert into bounded top-K (kept sorted descending)
6398 - if (count($top_results) < $top_k) {
6399 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6400 - usort($top_results, function ($a, $b) {
6401 - return $b['similarity'] <=> $a['similarity'];
6402 - });
6403 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6404 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6405 - usort($top_results, function ($a, $b) {
6406 - return $b['similarity'] <=> $a['similarity'];
6407 - });
6408 - }
6409 - }
6410 -
6411 - unset($batch);
6412 - $offset += $batch_size;
6413 - } while (true);
6414 -
6415 - if (empty($top_results)) {
6416 - return '';
6417 - }
6418 -
6419 - $content = '';
6420 - foreach ($top_results as $result) {
6421 - $chunk_content = $this->fetch_content_with_product_links($result['id']);
6422 - $content .= $chunk_content . "\n\n";
6423 - }
6424 -
6425 - return trim($content);
6426 -}
6427 -
6428 -
6429 -private function find_relevant_products_pinecone($user_embedding) {
6430 - //error_log('Starting Pinecone product search...');
6431 -
6432 - $options = get_option('mxchat_pinecone_addon_options', array());
6433 - $api_key = $options['mxchat_pinecone_api_key'] ?? '';
6434 - $host = $options['mxchat_pinecone_host'] ?? '';
6435 -
6436 - if (empty($host) || empty($api_key)) {
6437 - //error_log('Pinecone credentials not properly configured for product search');
6438 - return '';
6439 - }
6440 -
6441 - $similarity_threshold = 0.85;
6442 - $api_endpoint = "https://{$host}/query";
6443 -
6444 - $request_body = array(
6445 - 'vector' => $user_embedding,
6446 - 'topK' => 5,
6447 - 'includeMetadata' => true,
6448 - 'includeValues' => true,
6449 - 'filter' => array(
6450 - 'type' => 'product'
6451 - )
6452 - );
6453 -
6454 - //error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
6455 -
6456 - $response = wp_remote_post($api_endpoint, array(
6457 - 'headers' => array(
6458 - 'Api-Key' => $api_key,
6459 - 'accept' => 'application/json',
6460 - 'content-type' => 'application/json'
6461 - ),
6462 - 'body' => wp_json_encode($request_body),
6463 - 'timeout' => 30
6464 - ));
6465 -
6466 - if (is_wp_error($response)) {
6467 - //error_log('Pinecone product query error: ' . $response->get_error_message());
6468 - return '';
6469 - }
6470 -
6471 - $response_code = wp_remote_retrieve_response_code($response);
6472 - //error_log('Pinecone response code: ' . $response_code);
6473 -
6474 - if ($response_code !== 200) {
6475 - //error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
6476 - return '';
6477 - }
6478 -
6479 - $results = json_decode(wp_remote_retrieve_body($response), true);
6480 - //error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
6481 -
6482 - if (empty($results['matches'])) {
6483 - //error_log('No matches found in Pinecone response');
6484 - return '';
6485 - }
6486 -
6487 - $content = '';
6488 - foreach ($results['matches'] as $match) {
6489 - if ($match['score'] < $similarity_threshold) {
6490 - //error_log("Match below threshold: " . $match['score']);
6491 - continue;
6492 - }
6493 -
6494 - if (!empty($match['metadata']['text'])) {
6495 - $content .= $match['metadata']['text'];
6496 - if (!empty($match['metadata']['source_url'])) {
6497 - $content .= "\n\nFor more details, check out this product: " . esc_url($match['metadata']['source_url']);
6498 - }
6499 - $content .= "\n\n";
6500 - }
6501 - }
6502 -
6503 - return trim($content);
6504 -}
6505 -
6506 -
6507 -private function fetch_content_with_product_links($most_relevant_id) {
6508 - global $wpdb;
6509 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6510 -
6511 - // Fetch the article content and associated product URL
6512 - $query = $wpdb->prepare("SELECT article_content, source_url FROM {$system_prompt_table} WHERE id = %d", $most_relevant_id);
6513 - $result = $wpdb->get_row($query);
6514 -
6515 - if ($result) {
6516 - // Append the product link to the content if available
6517 - $content = $result->article_content;
6518 - if (!empty($result->source_url)) {
6519 - $content .= "\n\nFor more details, check out this product: " . esc_url($result->source_url);
6520 - }
6521 - return $content;
6522 - }
6523 -
6524 - return null;
6525 -}
6526 -
6527 -/**
6528 - * Get system instructions for a specific bot or default
6529 - * Checks for multi-bot add-on and uses bot-specific instructions if available
6530 - * Automatically strips URLs if citation links are disabled
6531 - * Replaces {visitor_name} placeholder with actual visitor name if available
6532 - *
6533 - * @param string $bot_id The bot ID to get instructions for
6534 - * @param string $session_id Optional session ID to lookup visitor name
6535 - */
6536 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6537 - $instructions = '';
6538 -
6539 - // Check if multi-bot add-on is active
6540 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6541 - // Get bot-specific options from multi-bot add-on
6542 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6543 -
6544 - // If bot has custom system instructions, use those
6545 - if (!empty($bot_options['system_prompt_instructions'])) {
6546 - $instructions = $bot_options['system_prompt_instructions'];
6547 - }
6548 - }
6549 -
6550 - // Fall back to default system instructions
6551 - if (empty($instructions)) {
6552 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6553 - }
6554 -
6555 - // Check if citation links are disabled - if so, strip URLs from instructions
6556 - $fresh_options = get_option('mxchat_options', []);
6557 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6558 -
6559 - if (!$citation_links_enabled && !empty($instructions)) {
6560 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
6561 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
6562 - }
6563 -
6564 - // Replace {visitor_name} placeholder with actual visitor name if available
6565 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
6566 - $name_option_key = "mxchat_name_{$session_id}";
6567 - $visitor_name = get_option($name_option_key, '');
6568 -
6569 - if (!empty($visitor_name)) {
6570 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
6571 - } else {
6572 - // Remove placeholder if no name is available
6573 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
6574 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
6575 - }
6576 - }
6577 -
6578 - // Allow developers to filter system instructions and process shortcodes
6579 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
6580 - $instructions = do_shortcode($instructions);
6581 -
6582 - return $instructions;
6583 -}
6584 -/**
6585 - * Get the current bot ID from session or request context
6586 - */
6587 -private function get_current_bot_id($session_id = '') {
6588 - // First, check if bot_id is passed in the current request
6589 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
6590 - return sanitize_key($_POST['bot_id']);
6591 - }
6592 -
6593 - // If not in POST, try to get it from session data
6594 - if (!empty($session_id)) {
6595 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
6596 - if (!empty($bot_id)) {
6597 - return $bot_id;
6598 - }
6599 - }
6600 -
6601 - // Fall back to default
6602 - return 'default';
6603 -}
6604 -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') {
6605 - try {
6606 - if (!$relevant_content) {
6607 - $error_response = [
6608 - 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
6609 - 'error_code' => 'no_relevant_content'
6610 - ];
6611 -
6612 - if ($testing_data !== null) {
6613 - $error_response['testing_data'] = $testing_data;
6614 - }
6615 -
6616 - return $error_response;
6617 - }
6618 -
6619 - if (!is_array($conversation_history)) {
6620 - $conversation_history = array();
6621 - }
6622 -
6623 - // Check if this is an OpenRouter model
6624 - if ($selected_model === 'openrouter') {
6625 - // Get the actual OpenRouter model from options
6626 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
6627 -
6628 - if (empty($openrouter_selected_model)) {
6629 - $error_response = [
6630 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
6631 - 'error_code' => 'no_openrouter_model_selected'
6632 - ];
6633 - if ($testing_data !== null) {
6634 - $error_response['testing_data'] = $testing_data;
6635 - }
6636 - return $error_response;
6637 - }
6638 -
6639 - if (empty($openrouter_api_key)) {
6640 - $error_response = [
6641 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
6642 - 'error_code' => 'missing_openrouter_api_key'
6643 - ];
6644 - if ($testing_data !== null) {
6645 - $error_response['testing_data'] = $testing_data;
6646 - }
6647 - return $error_response;
6648 - }
6649 -
6650 - if ($streaming) {
6651 - return $this->mxchat_generate_response_openrouter_stream(
6652 - $openrouter_selected_model,
6653 - $openrouter_api_key,
6654 - $conversation_history,
6655 - $relevant_content,
6656 - $session_id,
6657 - $testing_data
6658 - );
6659 - } else {
6660 - $response = $this->mxchat_generate_response_openrouter(
6661 - $openrouter_selected_model,
6662 - $openrouter_api_key,
6663 - $conversation_history,
6664 - $relevant_content
6665 - );
6666 - }
6667 -
6668 - if (is_array($response) && isset($response['error'])) {
6669 - if ($testing_data !== null) {
6670 - $response['testing_data'] = $testing_data;
6671 - }
6672 - return $response;
6673 - }
6674 -
6675 - return $response;
6676 - }
6677 -
6678 - // Extract model prefix to determine the provider
6679 - $model_parts = explode('-', $selected_model);
6680 - $provider = strtolower($model_parts[0]);
6681 -
6682 - // Handle model selection based on provider prefix
6683 - switch ($provider) {
6684 - case 'gemini':
6685 - if (empty($gemini_api_key)) {
6686 - $error_response = [
6687 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
6688 - 'error_code' => 'missing_gemini_api_key'
6689 - ];
6690 - if ($testing_data !== null) {
6691 - $error_response['testing_data'] = $testing_data;
6692 - }
6693 - return $error_response;
6694 - }
6695 - $response = $this->mxchat_generate_response_gemini(
6696 - $selected_model,
6697 - $gemini_api_key,
6698 - $conversation_history,
6699 - $relevant_content
6700 - );
6701 - break;
6702 -
6703 - case 'claude':
6704 - if (empty($claude_api_key)) {
6705 - $error_response = [
6706 - 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
6707 - 'error_code' => 'missing_claude_api_key'
6708 - ];
6709 - if ($testing_data !== null) {
6710 - $error_response['testing_data'] = $testing_data;
6711 - }
6712 - return $error_response;
6713 - }
6714 - if ($streaming) {
6715 - return $this->mxchat_generate_response_claude_stream(
6716 - $selected_model,
6717 - $claude_api_key,
6718 - $conversation_history,
6719 - $relevant_content,
6720 - $session_id,
6721 - $testing_data
6722 - );
6723 - } else {
6724 - $response = $this->mxchat_generate_response_claude(
6725 - $selected_model,
6726 - $claude_api_key,
6727 - $conversation_history,
6728 - $relevant_content
6729 - );
6730 - }
6731 - break;
6732 -
6733 - case 'grok':
6734 - if (empty($xai_api_key)) {
6735 - $error_response = [
6736 - 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
6737 - 'error_code' => 'missing_xai_api_key'
6738 - ];
6739 - if ($testing_data !== null) {
6740 - $error_response['testing_data'] = $testing_data;
6741 - }
6742 - return $error_response;
6743 - }
6744 - if ($streaming) {
6745 - return $this->mxchat_generate_response_xai_stream(
6746 - $selected_model,
6747 - $xai_api_key,
6748 - $conversation_history,
6749 - $relevant_content,
6750 - $session_id,
6751 - $testing_data
6752 - );
6753 - } else {
6754 - $response = $this->mxchat_generate_response_xai(
6755 - $selected_model,
6756 - $xai_api_key,
6757 - $conversation_history,
6758 - $relevant_content
6759 - );
6760 - }
6761 - break;
6762 -
6763 - case 'deepseek':
6764 - if (empty($deepseek_api_key)) {
6765 - $error_response = [
6766 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
6767 - 'error_code' => 'missing_deepseek_api_key'
6768 - ];
6769 - if ($testing_data !== null) {
6770 - $error_response['testing_data'] = $testing_data;
6771 - }
6772 - return $error_response;
6773 - }
6774 - if ($streaming) {
6775 - return $this->mxchat_generate_response_deepseek_stream(
6776 - $selected_model,
6777 - $deepseek_api_key,
6778 - $conversation_history,
6779 - $relevant_content,
6780 - $session_id,
6781 - $testing_data
6782 - );
6783 - } else {
6784 - $response = $this->mxchat_generate_response_deepseek(
6785 - $selected_model,
6786 - $deepseek_api_key,
6787 - $conversation_history,
6788 - $relevant_content
6789 - );
6790 - }
6791 - break;
6792 -
6793 - case 'custom':
6794 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
6795 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
6796 - if (empty($cp_base_url)) {
6797 - $error_response = [
6798 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
6799 - 'error_code' => 'missing_custom_provider_base_url'
6800 - ];
6801 - if ($testing_data !== null) {
6802 - $error_response['testing_data'] = $testing_data;
6803 - }
6804 - return $error_response;
6805 - }
6806 - if ($streaming) {
6807 - return $this->mxchat_generate_response_custom_stream(
6808 - $selected_model,
6809 - $conversation_history,
6810 - $relevant_content,
6811 - $session_id,
6812 - $testing_data
6813 - );
6814 - } else {
6815 - $response = $this->mxchat_generate_response_custom(
6816 - $selected_model,
6817 - $conversation_history,
6818 - $relevant_content
6819 - );
6820 - }
6821 - break;
6822 -
6823 - case 'gpt':
6824 - case 'o1':
6825 - if (empty($api_key)) {
6826 - $error_response = [
6827 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
6828 - 'error_code' => 'missing_openai_api_key'
6829 - ];
6830 - if ($testing_data !== null) {
6831 - $error_response['testing_data'] = $testing_data;
6832 - }
6833 - return $error_response;
6834 - }
6835 -
6836 - // Check if web search is enabled for this OpenAI model
6837 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
6838 - // Models that don't support web search
6839 - $unsupported_web_search_models = array('gpt-4.1-nano');
6840 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
6841 -
6842 - if ($web_search_enabled && $model_supports_web_search) {
6843 - // Use Responses API (required for some models, or when web search is enabled)
6844 - return $this->mxchat_generate_response_openai_web_search(
6845 - $selected_model,
6846 - $api_key,
6847 - $conversation_history,
6848 - $relevant_content,
6849 - $session_id,
6850 - $testing_data,
6851 - $streaming
6852 - );
6853 - } elseif ($streaming) {
6854 - return $this->mxchat_generate_response_openai_stream(
6855 - $selected_model,
6856 - $api_key,
6857 - $conversation_history,
6858 - $relevant_content,
6859 - $session_id,
6860 - $testing_data
6861 - );
6862 - } else {
6863 - $response = $this->mxchat_generate_response_openai(
6864 - $selected_model,
6865 - $api_key,
6866 - $conversation_history,
6867 - $relevant_content
6868 - );
6869 - }
6870 - break;
6871 -
6872 - default:
6873 - if (empty($api_key)) {
6874 - $error_response = [
6875 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
6876 - 'error_code' => 'missing_openai_api_key'
6877 - ];
6878 - if ($testing_data !== null) {
6879 - $error_response['testing_data'] = $testing_data;
6880 - }
6881 - return $error_response;
6882 - }
6883 -
6884 - // Check if web search is enabled (default case also handles OpenAI models)
6885 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
6886 - $unsupported_web_search_models = array('gpt-4.1-nano');
6887 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
6888 -
6889 - if ($web_search_enabled && $model_supports_web_search) {
6890 - return $this->mxchat_generate_response_openai_web_search(
6891 - $selected_model,
6892 - $api_key,
6893 - $conversation_history,
6894 - $relevant_content,
6895 - $session_id,
6896 - $testing_data,
6897 - $streaming
6898 - );
6899 - } elseif ($streaming) {
6900 - return $this->mxchat_generate_response_openai_stream(
6901 - $selected_model,
6902 - $api_key,
6903 - $conversation_history,
6904 - $relevant_content,
6905 - $session_id,
6906 - $testing_data
6907 - );
6908 - } else {
6909 - $response = $this->mxchat_generate_response_openai(
6910 - $selected_model,
6911 - $api_key,
6912 - $conversation_history,
6913 - $relevant_content
6914 - );
6915 - }
6916 - break;
6917 - }
6918 -
6919 - if (is_array($response) && isset($response['error'])) {
6920 - if ($testing_data !== null) {
6921 - $response['testing_data'] = $testing_data;
6922 - }
6923 - return $response;
6924 - }
6925 -
6926 - return $response;
6927 -
6928 - } catch (Exception $e) {
6929 - $error_response = [
6930 - 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
6931 - 'error_code' => 'system_exception',
6932 - 'exception_details' => $e->getMessage()
6933 - ];
6934 -
6935 - if ($testing_data !== null) {
6936 - $error_response['testing_data'] = $testing_data;
6937 - }
6938 -
6939 - return $error_response;
6940 - }
6941 -}
6942 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
6943 - try {
6944 - $bot_id = $this->get_current_bot_id($session_id);
6945 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
6946 -
6947 - if (!is_array($conversation_history)) {
6948 - $conversation_history = array();
6949 - }
6950 -
6951 - $formatted_conversation = array();
6952 -
6953 - $formatted_conversation[] = array(
6954 - 'role' => 'system',
6955 - 'content' => $system_prompt_instructions . " " . $relevant_content
6956 - );
6957 -
6958 - foreach ($conversation_history as $message) {
6959 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
6960 - $role = $message['role'];
6961 - if ($role === 'bot' || $role === 'agent') {
6962 - $role = 'assistant';
6963 - }
6964 - if (!in_array($role, ['system', 'assistant', 'user'])) {
6965 - $role = 'user';
6966 - }
6967 - $formatted_conversation[] = array(
6968 - 'role' => $role,
6969 - 'content' => $message['content']
6970 - );
6971 - }
6972 - }
6973 -
6974 - if (headers_sent() || !function_exists('curl_init')) {
6975 - $regular_response = $this->mxchat_generate_response_openrouter(
6976 - $selected_model,
6977 - $openrouter_api_key,
6978 - $conversation_history,
6979 - $relevant_content
6980 - );
6981 -
6982 - // Save bot response to transcript
6983 - if (!empty($regular_response) && !empty($session_id)) {
6984 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
6985 - }
6986 -
6987 - $response_data = [
6988 - 'text' => $regular_response,
6989 - 'html' => '',
6990 - 'session_id' => $session_id
6991 - ];
6992 -
6993 - if ($testing_data !== null) {
6994 - $response_data['testing_data'] = $testing_data;
6995 - }
6996 -
6997 - header('Content-Type: application/json');
6998 - echo json_encode($response_data);
6999 - return true;
7000 - }
7001 -
7002 - $body = json_encode([
7003 - 'model' => $selected_model,
7004 - 'messages' => $formatted_conversation,
7005 - 'temperature' => 1,
7006 - 'stream' => true
7007 - ]);
7008 -
7009 - // Setup streaming headers now that we know we're actually streaming
7010 - $this->setup_streaming_headers();
7011 -
7012 - $ch = curl_init();
7013 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
7014 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7015 - curl_setopt($ch, CURLOPT_POST, true);
7016 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7017 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7018 - 'Content-Type: application/json',
7019 - 'Authorization: Bearer ' . $openrouter_api_key,
7020 - 'HTTP-Referer: ' . home_url(),
7021 - 'X-Title: ' . get_bloginfo('name')
7022 - ));
7023 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7024 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7025 -
7026 - $full_response = '';
7027 - $stream_started = false;
7028 - $buffer = '';
7029 -
7030 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
7031 - if (!$stream_started && $testing_data !== null) {
7032 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7033 - flush();
7034 - $stream_started = true;
7035 - }
7036 -
7037 - $buffer .= $data;
7038 - $lines = explode("\n", $buffer);
7039 - $buffer = array_pop($lines);
7040 -
7041 - foreach ($lines as $line) {
7042 - if (trim($line) === '') {
7043 - continue;
7044 - }
7045 -
7046 - if (strpos($line, 'data: ') !== 0) {
7047 - continue;
7048 - }
7049 -
7050 - $json_str = substr($line, 6);
7051 -
7052 - if (trim($json_str) === '[DONE]') {
7053 - echo "data: [DONE]\n\n";
7054 - flush();
7055 - continue;
7056 - }
7057 -
7058 - $json = json_decode(trim($json_str), true);
7059 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7060 - $content = $json['choices'][0]['delta']['content'];
7061 - $full_response .= $content;
7062 -
7063 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7064 - flush();
7065 - }
7066 - }
7067 -
7068 - return strlen($data);
7069 - });
7070 -
7071 - $response = curl_exec($ch);
7072 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7073 -
7074 - if (curl_errno($ch) || $http_code !== 200) {
7075 - curl_close($ch);
7076 -
7077 - $regular_response = $this->mxchat_generate_response_openrouter(
7078 - $selected_model,
7079 - $openrouter_api_key,
7080 - $conversation_history,
7081 - $relevant_content
7082 - );
7083 -
7084 - $response_data = [
7085 - 'text' => $regular_response,
7086 - 'html' => '',
7087 - 'session_id' => $session_id
7088 - ];
7089 -
7090 - if ($testing_data !== null) {
7091 - $response_data['testing_data'] = $testing_data;
7092 - }
7093 -
7094 - header('Content-Type: application/json');
7095 - echo json_encode($response_data);
7096 - return true;
7097 - }
7098 -
7099 - curl_close($ch);
7100 -
7101 - if (!empty($full_response) && !empty($session_id)) {
7102 - // Prepare RAG context for streaming response
7103 - $rag_context_for_storage = null;
7104 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7105 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7106 -
7107 - if ($has_rag_data || $has_action_data) {
7108 - $rag_context_for_storage = [];
7109 -
7110 - if ($has_rag_data) {
7111 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7112 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7113 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7114 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7115 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7116 - }
7117 -
7118 - if ($has_action_data) {
7119 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7120 - }
7121 - }
7122 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7123 - }
7124 -
7125 - return true;
7126 -
7127 - } catch (Exception $e) {
7128 - $regular_response = $this->mxchat_generate_response_openrouter(
7129 - $selected_model,
7130 - $openrouter_api_key,
7131 - $conversation_history,
7132 - $relevant_content
7133 - );
7134 -
7135 - $response_data = [
7136 - 'text' => $regular_response,
7137 - 'html' => '',
7138 - 'session_id' => $session_id
7139 - ];
7140 -
7141 - if ($testing_data !== null) {
7142 - $response_data['testing_data'] = $testing_data;
7143 - }
7144 -
7145 - header('Content-Type: application/json');
7146 - echo json_encode($response_data);
7147 - return true;
7148 - }
7149 -}
7150 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7151 - try {
7152 - $bot_id = $this->get_current_bot_id($session_id);
7153 -
7154 - // Get system prompt instructions using centralized function
7155 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7156 -
7157 - // Ensure conversation_history is an array
7158 - if (!is_array($conversation_history)) {
7159 - $conversation_history = array();
7160 - }
7161 -
7162 - // Format conversation history for OpenAI
7163 - $formatted_conversation = array();
7164 -
7165 - $formatted_conversation[] = array(
7166 - 'role' => 'system',
7167 - 'content' => $system_prompt_instructions . " " . $relevant_content
7168 - );
7169 -
7170 - foreach ($conversation_history as $message) {
7171 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7172 - $role = $message['role'];
7173 - if ($role === 'bot' || $role === 'agent') {
7174 - $role = 'assistant';
7175 - }
7176 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7177 - $role = 'user';
7178 - }
7179 - $formatted_conversation[] = array(
7180 - 'role' => $role,
7181 - 'content' => $message['content']
7182 - );
7183 - }
7184 - }
7185 -
7186 - // Check if we can actually stream
7187 - if (headers_sent() || !function_exists('curl_init')) {
7188 - // Fallback to regular response with testing data
7189 - $regular_response = $this->mxchat_generate_response_openai(
7190 - $selected_model,
7191 - $api_key,
7192 - $conversation_history,
7193 - $relevant_content
7194 - );
7195 -
7196 - // Save bot response to transcript
7197 - if (!empty($regular_response) && !empty($session_id)) {
7198 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7199 - }
7200 -
7201 - $response_data = [
7202 - 'text' => $regular_response,
7203 - 'html' => '',
7204 - 'session_id' => $session_id
7205 - ];
7206 -
7207 - if ($testing_data !== null) {
7208 - $response_data['testing_data'] = $testing_data;
7209 - }
7210 -
7211 - header('Content-Type: application/json');
7212 - echo json_encode($response_data);
7213 - return true;
7214 - }
7215 -
7216 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
7217 - $is_gpt5_model = (
7218 - strpos($selected_model, 'gpt-5') === 0 ||
7219 - $selected_model === 'gpt-5.2' ||
7220 - $selected_model === 'gpt-5.1-2025-11-13' ||
7221 - $selected_model === 'gpt-5' ||
7222 - $selected_model === 'gpt-5-mini' ||
7223 - $selected_model === 'gpt-5-nano'
7224 - );
7225 -
7226 - // Build request body with optimal settings for fast streaming
7227 - $request_body = [
7228 - 'model' => $selected_model,
7229 - 'messages' => $formatted_conversation,
7230 - 'temperature' => 1,
7231 - 'stream' => true
7232 - ];
7233 -
7234 - // Add reasoning_effort only for GPT-5 models that support it
7235 - // These chat models don't support reasoning_effort parameter
7236 - $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');
7237 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
7238 - // GPT-5.1 uses 'low' instead of 'minimal'
7239 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7240 - $request_body['reasoning_effort'] = 'low';
7241 - } elseif ($selected_model === 'gpt-5.5') {
7242 - $request_body['reasoning_effort'] = 'none';
7243 - } elseif ($selected_model === 'gpt-5.4') {
7244 - $request_body['reasoning_effort'] = 'none';
7245 - } else {
7246 - $request_body['reasoning_effort'] = 'minimal';
7247 - }
7248 - }
7249 -
7250 - $body = json_encode($request_body);
7251 -
7252 - // Setup streaming headers now that we know we're actually streaming
7253 - $this->setup_streaming_headers();
7254 -
7255 - // Use cURL for streaming support
7256 - $ch = curl_init();
7257 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
7258 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7259 - curl_setopt($ch, CURLOPT_POST, true);
7260 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7261 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7262 - 'Content-Type: application/json',
7263 - 'Authorization: Bearer ' . $api_key
7264 - ));
7265 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7266 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7267 -
7268 - $full_response = ''; // Accumulate full response for saving
7269 - $stream_started = false;
7270 - $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
7271 -
7272 - // Buffer control for real-time streaming
7273 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
7274 - // Send testing data as the first event if available
7275 - if (!$stream_started && $testing_data !== null) {
7276 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7277 - flush();
7278 - $stream_started = true;
7279 - }
7280 -
7281 - // CRITICAL FIX: Append new data to buffer
7282 - $buffer .= $data;
7283 -
7284 - // Process complete lines only
7285 - $lines = explode("\n", $buffer);
7286 -
7287 - // CRITICAL FIX: Keep the last incomplete line in the buffer
7288 - // The last element might be incomplete, so keep it in buffer
7289 - $buffer = array_pop($lines);
7290 -
7291 - foreach ($lines as $line) {
7292 - // Skip empty lines
7293 - if (trim($line) === '') {
7294 - continue;
7295 - }
7296 -
7297 - // Only process lines that start with "data: "
7298 - if (strpos($line, 'data: ') !== 0) {
7299 - continue;
7300 - }
7301 -
7302 - $json_str = substr($line, 6); // Remove 'data: ' prefix
7303 -
7304 - if (trim($json_str) === '[DONE]') {
7305 - echo "data: [DONE]\n\n";
7306 - flush();
7307 - continue;
7308 - }
7309 -
7310 - // Try to decode JSON
7311 - $json = json_decode(trim($json_str), true);
7312 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7313 - $content = $json['choices'][0]['delta']['content'];
7314 - $full_response .= $content; // Accumulate the full response
7315 -
7316 - // Send as SSE format
7317 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7318 - flush();
7319 - }
7320 - }
7321 -
7322 - return strlen($data);
7323 - });
7324 -
7325 - $response = curl_exec($ch);
7326 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7327 -
7328 - if (curl_errno($ch) || $http_code !== 200) {
7329 - $curl_error = curl_error($ch);
7330 - curl_close($ch);
7331 -
7332 - // Fallback to regular response
7333 - $regular_response = $this->mxchat_generate_response_openai(
7334 - $selected_model,
7335 - $api_key,
7336 - $conversation_history,
7337 - $relevant_content
7338 - );
7339 -
7340 - // FIXED: Check if regular response returned an error
7341 - if (is_array($regular_response) && isset($regular_response['error'])) {
7342 - // Send error in SSE format since we're in streaming mode
7343 - echo "data: " . json_encode([
7344 - 'error' => true,
7345 - 'error_message' => $regular_response['error'],
7346 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7347 - 'text' => $regular_response['error'],
7348 - 'message' => $regular_response['error']
7349 - ]) . "\n\n";
7350 - echo "data: [DONE]\n\n";
7351 - flush();
7352 - return true;
7353 - }
7354 -
7355 - $response_data = [
7356 - 'text' => $regular_response,
7357 - 'html' => '',
7358 - 'session_id' => $session_id
7359 - ];
7360 -
7361 - if ($testing_data !== null) {
7362 - $response_data['testing_data'] = $testing_data;
7363 - }
7364 -
7365 - header('Content-Type: application/json');
7366 - echo json_encode($response_data);
7367 - return true;
7368 - }
7369 -
7370 - curl_close($ch);
7371 -
7372 - // Save the complete response to maintain chat persistence
7373 - if (!empty($full_response) && !empty($session_id)) {
7374 - // Prepare RAG context for streaming response
7375 - $rag_context_for_storage = null;
7376 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7377 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7378 -
7379 - if ($has_rag_data || $has_action_data) {
7380 - $rag_context_for_storage = [];
7381 -
7382 - if ($has_rag_data) {
7383 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7384 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7385 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7386 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7387 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7388 - }
7389 -
7390 - if ($has_action_data) {
7391 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7392 - }
7393 - }
7394 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7395 - }
7396 -
7397 - return true; // Indicate streaming completed successfully
7398 -
7399 - } catch (Exception $e) {
7400 - // Fallback to regular response
7401 - $regular_response = $this->mxchat_generate_response_openai(
7402 - $selected_model,
7403 - $api_key,
7404 - $conversation_history,
7405 - $relevant_content
7406 - );
7407 -
7408 - // FIXED: Check if regular response returned an error
7409 - if (is_array($regular_response) && isset($regular_response['error'])) {
7410 - // Send error in SSE format since we're in streaming mode
7411 - echo "data: " . json_encode([
7412 - 'error' => true,
7413 - 'error_message' => $regular_response['error'],
7414 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7415 - 'text' => $regular_response['error'],
7416 - 'message' => $regular_response['error']
7417 - ]) . "\n\n";
7418 - echo "data: [DONE]\n\n";
7419 - flush();
7420 - return true;
7421 - }
7422 -
7423 - $response_data = [
7424 - 'text' => $regular_response,
7425 - 'html' => '',
7426 - 'session_id' => $session_id
7427 - ];
7428 -
7429 - if ($testing_data !== null) {
7430 - $response_data['testing_data'] = $testing_data;
7431 - }
7432 -
7433 - header('Content-Type: application/json');
7434 - echo json_encode($response_data);
7435 - return true;
7436 - }
7437 -}
7438 -
7439 -/**
7440 - * Resolve custom (OpenAI-compatible) provider config from settings.
7441 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
7442 - */
7443 -private function mxchat_resolve_custom_provider() {
7444 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
7445 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
7446 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
7447 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
7448 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
7449 -
7450 - $chat_url = $base_url . '/chat/completions';
7451 - if (!empty($api_version)) {
7452 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
7453 - }
7454 -
7455 - $headers = array('Content-Type: application/json');
7456 - if (!empty($api_key)) {
7457 - if ($auth_scheme === 'api-key') {
7458 - $headers[] = 'api-key: ' . $api_key;
7459 - } else {
7460 - $headers[] = 'Authorization: Bearer ' . $api_key;
7461 - }
7462 - }
7463 -
7464 - return array(
7465 - 'base_url' => $base_url,
7466 - 'api_key' => $api_key,
7467 - 'model' => $model !== '' ? $model : 'default',
7468 - 'auth_scheme' => $auth_scheme,
7469 - 'api_version' => $api_version,
7470 - 'chat_url' => $chat_url,
7471 - 'headers' => $headers,
7472 - );
7473 -}
7474 -
7475 -/**
7476 - * Streaming chat completion against an OpenAI-compatible custom provider
7477 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
7478 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
7479 - */
7480 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7481 - try {
7482 - $cfg = $this->mxchat_resolve_custom_provider();
7483 - if (empty($cfg['base_url'])) {
7484 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7485 - }
7486 -
7487 - $bot_id = $this->get_current_bot_id($session_id);
7488 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7489 - if (!is_array($conversation_history)) {
7490 - $conversation_history = array();
7491 - }
7492 -
7493 - $formatted_conversation = array();
7494 - $formatted_conversation[] = array(
7495 - 'role' => 'system',
7496 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7497 - );
7498 - foreach ($conversation_history as $message) {
7499 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7500 - $role = $message['role'];
7501 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7502 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7503 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
7504 - }
7505 - }
7506 -
7507 - if (headers_sent() || !function_exists('curl_init')) {
7508 - // No streaming capability — fall through to non-stream wrapper
7509 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7510 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
7511 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
7512 - }
7513 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7514 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7515 - header('Content-Type: application/json');
7516 - echo json_encode($response_data);
7517 - return true;
7518 - }
7519 -
7520 - $request_body = array(
7521 - 'model' => $cfg['model'],
7522 - 'messages' => $formatted_conversation,
7523 - 'stream' => true,
7524 - );
7525 - $body = json_encode($request_body);
7526 -
7527 - $this->setup_streaming_headers();
7528 -
7529 - $ch = curl_init();
7530 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
7531 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7532 - curl_setopt($ch, CURLOPT_POST, true);
7533 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7534 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
7535 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7536 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
7537 -
7538 - $full_response = '';
7539 - $stream_started = false;
7540 - $buffer = '';
7541 -
7542 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
7543 - if (!$stream_started && $testing_data !== null) {
7544 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
7545 - flush();
7546 - $stream_started = true;
7547 - }
7548 - $buffer .= $data;
7549 - $lines = explode("\n", $buffer);
7550 - $buffer = array_pop($lines);
7551 - foreach ($lines as $line) {
7552 - if (trim($line) === '') { continue; }
7553 - if (strpos($line, 'data: ') !== 0) { continue; }
7554 - $json_str = substr($line, 6);
7555 - if (trim($json_str) === '[DONE]') {
7556 - echo "data: [DONE]\n\n";
7557 - flush();
7558 - continue;
7559 - }
7560 - $json = json_decode(trim($json_str), true);
7561 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7562 - $content = $json['choices'][0]['delta']['content'];
7563 - $full_response .= $content;
7564 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
7565 - flush();
7566 - }
7567 - }
7568 - return strlen($data);
7569 - });
7570 -
7571 - $response = curl_exec($ch);
7572 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7573 -
7574 - if (curl_errno($ch) || $http_code !== 200) {
7575 - $curl_error = curl_error($ch);
7576 - curl_close($ch);
7577 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7578 - if (is_array($regular) && isset($regular['error'])) {
7579 - echo "data: " . json_encode(array(
7580 - 'error' => true,
7581 - 'error_message' => $regular['error'],
7582 - 'error_code' => $regular['error_code'] ?? 'custom_provider_error',
7583 - 'text' => $regular['error'],
7584 - 'message' => $regular['error'],
7585 - )) . "\n\n";
7586 - echo "data: [DONE]\n\n";
7587 - flush();
7588 - return true;
7589 - }
7590 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7591 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7592 - header('Content-Type: application/json');
7593 - echo json_encode($response_data);
7594 - return true;
7595 - }
7596 -
7597 - curl_close($ch);
7598 -
7599 - if (!empty($full_response) && !empty($session_id)) {
7600 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
7601 - }
7602 - return true;
7603 -
7604 - } catch (Exception $e) {
7605 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
7606 - }
7607 -}
7608 -
7609 -/**
7610 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
7611 - * Returns string content on success, array['error'=>...] on failure.
7612 - */
7613 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
7614 - $cfg = $this->mxchat_resolve_custom_provider();
7615 - if (empty($cfg['base_url'])) {
7616 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7617 - }
7618 -
7619 - $bot_id = $this->get_current_bot_id(null);
7620 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
7621 - if (!is_array($conversation_history)) {
7622 - $conversation_history = array();
7623 - }
7624 -
7625 - $messages = array(array(
7626 - 'role' => 'system',
7627 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7628 - ));
7629 - foreach ($conversation_history as $message) {
7630 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7631 - $role = $message['role'];
7632 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7633 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7634 - $messages[] = array('role' => $role, 'content' => $message['content']);
7635 - }
7636 - }
7637 -
7638 - $headers_assoc = array('Content-Type' => 'application/json');
7639 - if (!empty($cfg['api_key'])) {
7640 - if ($cfg['auth_scheme'] === 'api-key') {
7641 - $headers_assoc['api-key'] = $cfg['api_key'];
7642 - } else {
7643 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
7644 - }
7645 - }
7646 -
7647 - $response = wp_remote_post($cfg['chat_url'], array(
7648 - 'headers' => $headers_assoc,
7649 - 'body' => wp_json_encode(array(
7650 - 'model' => $cfg['model'],
7651 - 'messages' => $messages,
7652 - )),
7653 - 'timeout' => 120,
7654 - ));
7655 -
7656 - if (is_wp_error($response)) {
7657 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
7658 - }
7659 - $code = (int) wp_remote_retrieve_response_code($response);
7660 - if ($code < 200 || $code >= 300) {
7661 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
7662 - }
7663 - $body = json_decode(wp_remote_retrieve_body($response), true);
7664 - if (isset($body['choices'][0]['message']['content'])) {
7665 - return (string) $body['choices'][0]['message']['content'];
7666 - }
7667 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
7668 -}
7669 -
7670 -/**
7671 - * Generate response using OpenAI Responses API with web search tool
7672 - * This uses the newer Responses API which supports web search functionality
7673 - */
7674 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
7675 - try {
7676 - $bot_id = $this->get_current_bot_id($session_id);
7677 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7678 -
7679 - if (!is_array($conversation_history)) {
7680 - $conversation_history = array();
7681 - }
7682 -
7683 - // Build the input for Responses API
7684 - // The Responses API uses a different format - we need to construct the input properly
7685 - $input_parts = [];
7686 -
7687 - // Add system instructions as context
7688 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
7689 -
7690 - // Build conversation as input items for Responses API
7691 - foreach ($conversation_history as $message) {
7692 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7693 - $role = $message['role'];
7694 - if ($role === 'bot' || $role === 'agent') {
7695 - $role = 'assistant';
7696 - }
7697 - if (!in_array($role, ['assistant', 'user'])) {
7698 - $role = 'user';
7699 - }
7700 - $input_parts[] = [
7701 - 'type' => 'message',
7702 - 'role' => $role,
7703 - 'content' => $message['content']
7704 - ];
7705 - }
7706 - }
7707 -
7708 - // Build request body for Responses API
7709 - $request_body = [
7710 - 'model' => $selected_model,
7711 - 'input' => $input_parts,
7712 - 'instructions' => $system_context,
7713 - 'stream' => $streaming
7714 - ];
7715 -
7716 - // Only add web search tool if web search is enabled in settings
7717 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7718 - if ($web_search_enabled) {
7719 - $request_body['tools'] = [
7720 - ['type' => 'web_search']
7721 - ];
7722 - }
7723 -
7724 - // Add reasoning effort for supported models
7725 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
7726 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
7727 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
7728 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7729 - $request_body['reasoning'] = ['effort' => 'low'];
7730 - } elseif ($selected_model === 'gpt-5.5') {
7731 - $request_body['reasoning'] = ['effort' => 'low'];
7732 - } elseif ($selected_model === 'gpt-5.4') {
7733 - $request_body['reasoning'] = ['effort' => 'low'];
7734 - }
7735 - }
7736 -
7737 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
7738 -
7739 - if ($streaming) {
7740 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
7741 - } else {
7742 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
7743 - }
7744 -
7745 - } catch (Exception $e) {
7746 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
7747 - return [
7748 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
7749 - 'error_code' => 'web_search_exception'
7750 - ];
7751 - }
7752 -}
7753 -
7754 -/**
7755 - * Handle non-streaming web search response
7756 - */
7757 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
7758 - $request_body['stream'] = false;
7759 -
7760 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
7761 - 'headers' => array(
7762 - 'Authorization' => 'Bearer ' . $api_key,
7763 - 'Content-Type' => 'application/json'
7764 - ),
7765 - 'body' => json_encode($request_body),
7766 - 'timeout' => 90
7767 - ));
7768 -
7769 - if (is_wp_error($response)) {
7770 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
7771 - return [
7772 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
7773 - 'error_code' => 'web_search_connection_error'
7774 - ];
7775 - }
7776 -
7777 - $response_code = wp_remote_retrieve_response_code($response);
7778 - $response_body = wp_remote_retrieve_body($response);
7779 -
7780 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
7781 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
7782 -
7783 - if ($response_code !== 200) {
7784 - $error_data = json_decode($response_body, true);
7785 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
7786 - return [
7787 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
7788 - 'error_code' => 'web_search_api_error'
7789 - ];
7790 - }
7791 -
7792 - $result = json_decode($response_body, true);
7793 -
7794 - if (json_last_error() !== JSON_ERROR_NONE) {
7795 - return [
7796 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
7797 - 'error_code' => 'web_search_json_error'
7798 - ];
7799 - }
7800 -
7801 - // Extract the response text and citations from Responses API format
7802 - $output_text = '';
7803 - $citations = [];
7804 -
7805 - if (isset($result['output'])) {
7806 - foreach ($result['output'] as $output_item) {
7807 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
7808 - foreach ($output_item['content'] as $content_item) {
7809 - if ($content_item['type'] === 'output_text') {
7810 - $output_text .= $content_item['text'];
7811 -
7812 - // Extract citations/annotations
7813 - if (isset($content_item['annotations'])) {
7814 - foreach ($content_item['annotations'] as $annotation) {
7815 - if ($annotation['type'] === 'url_citation') {
7816 - $citations[] = [
7817 - 'url' => $annotation['url'],
7818 - 'title' => $annotation['title'] ?? ''
7819 - ];
7820 - }
7821 - }
7822 - }
7823 - }
7824 - }
7825 - }
7826 - }
7827 - }
7828 -
7829 - // If we have citations, append them to the response
7830 - if (!empty($citations)) {
7831 - $output_text .= "\n\n**Sources:**\n";
7832 - $seen_urls = [];
7833 - foreach ($citations as $citation) {
7834 - if (!in_array($citation['url'], $seen_urls)) {
7835 - $seen_urls[] = $citation['url'];
7836 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
7837 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
7838 - }
7839 - }
7840 - }
7841 -
7842 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
7843 - // which includes rag_context for the "sources" link in transcripts.
7844 -
7845 - return $output_text;
7846 -}
7847 -
7848 -/**
7849 - * Handle streaming web search response using Responses API
7850 - */
7851 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
7852 - $request_body['stream'] = true;
7853 -
7854 - // Check if we can stream
7855 - if (headers_sent() || !function_exists('curl_init')) {
7856 - // Fallback to non-streaming
7857 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
7858 - }
7859 -
7860 - // Setup streaming headers
7861 - $this->setup_streaming_headers();
7862 -
7863 - $ch = curl_init();
7864 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
7865 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7866 - curl_setopt($ch, CURLOPT_POST, true);
7867 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
7868 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7869 - 'Content-Type: application/json',
7870 - 'Authorization: Bearer ' . $api_key
7871 - ));
7872 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7873 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
7874 -
7875 - $full_response = '';
7876 - $stream_started = false;
7877 - $buffer = '';
7878 - $citations = [];
7879 -
7880 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
7881 - // Send testing data as first event if available
7882 - if (!$stream_started && $testing_data !== null) {
7883 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7884 - flush();
7885 - $stream_started = true;
7886 - }
7887 -
7888 - $buffer .= $data;
7889 - $lines = explode("\n", $buffer);
7890 - $buffer = array_pop($lines);
7891 -
7892 - foreach ($lines as $line) {
7893 - if (trim($line) === '') continue;
7894 - if (strpos($line, 'data: ') !== 0) continue;
7895 -
7896 - $json_str = substr($line, 6);
7897 -
7898 - if (trim($json_str) === '[DONE]') {
7899 - // Append citations if we have any
7900 - if (!empty($citations)) {
7901 - $citation_text = "\n\n**Sources:**\n";
7902 - $seen_urls = [];
7903 - foreach ($citations as $citation) {
7904 - if (!in_array($citation['url'], $seen_urls)) {
7905 - $seen_urls[] = $citation['url'];
7906 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
7907 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
7908 - }
7909 - }
7910 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
7911 - $full_response .= $citation_text;
7912 - flush();
7913 - }
7914 - echo "data: [DONE]\n\n";
7915 - flush();
7916 - continue;
7917 - }
7918 -
7919 - $json = json_decode(trim($json_str), true);
7920 - if (!$json) continue;
7921 -
7922 - // Handle Responses API streaming events
7923 - // The format is different from Chat Completions
7924 - if (isset($json['type'])) {
7925 - switch ($json['type']) {
7926 - case 'response.output_text.delta':
7927 - // Text content delta
7928 - if (isset($json['delta'])) {
7929 - $content = $json['delta'];
7930 - $full_response .= $content;
7931 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7932 - flush();
7933 - }
7934 - break;
7935 -
7936 - case 'response.output_item.done':
7937 - // Check for citations in completed items
7938 - if (isset($json['item']['content'])) {
7939 - foreach ($json['item']['content'] as $content_item) {
7940 - if (isset($content_item['annotations'])) {
7941 - foreach ($content_item['annotations'] as $annotation) {
7942 - if ($annotation['type'] === 'url_citation') {
7943 - $citations[] = [
7944 - 'url' => $annotation['url'],
7945 - 'title' => $annotation['title'] ?? ''
7946 - ];
7947 - }
7948 - }
7949 - }
7950 - }
7951 - }
7952 - break;
7953 - }
7954 - }
7955 - }
7956 -
7957 - return strlen($data);
7958 - });
7959 -
7960 - $response = curl_exec($ch);
7961 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7962 -
7963 - if (curl_errno($ch) || $http_code !== 200) {
7964 - $curl_error = curl_error($ch);
7965 - curl_close($ch);
7966 -
7967 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
7968 -
7969 - // Fallback to non-streaming
7970 - $fallback_response = $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
7971 -
7972 - if (is_array($fallback_response) && isset($fallback_response['error'])) {
7973 - echo "data: " . json_encode([
7974 - 'error' => true,
7975 - 'error_message' => $fallback_response['error'],
7976 - 'error_code' => $fallback_response['error_code'] ?? 'web_search_error'
7977 - ]) . "\n\n";
7978 - echo "data: [DONE]\n\n";
7979 - flush();
7980 - return true;
7981 - }
7982 -
7983 - $response_data = [
7984 - 'text' => $fallback_response,
7985 - 'html' => '',
7986 - 'session_id' => $session_id
7987 - ];
7988 - if ($testing_data !== null) {
7989 - $response_data['testing_data'] = $testing_data;
7990 - }
7991 - header('Content-Type: application/json');
7992 - echo json_encode($response_data);
7993 - return true;
7994 - }
7995 -
7996 - curl_close($ch);
7997 -
7998 - // Save the complete response with RAG context so the "sources" link
7999 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
8000 - if (!empty($full_response) && !empty($session_id)) {
8001 - $rag_context_for_storage = null;
8002 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8003 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8004 -
8005 - if ($has_rag_data || $has_action_data) {
8006 - $rag_context_for_storage = [];
8007 -
8008 - if ($has_rag_data) {
8009 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8010 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8011 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8012 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8013 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8014 - }
8015 -
8016 - if ($has_action_data) {
8017 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8018 - }
8019 - }
8020 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8021 - }
8022 -
8023 - return true;
8024 -}
8025 -
8026 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8027 - try {
8028 - // Get bot ID from session or request
8029 - $bot_id = $this->get_current_bot_id($session_id);
8030 -
8031 - // Get system prompt instructions using centralized function
8032 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8033 - // Ensure conversation_history is an array
8034 - if (!is_array($conversation_history)) {
8035 - $conversation_history = array();
8036 - }
8037 -
8038 - // Clean and validate conversation history
8039 - foreach ($conversation_history as &$message) {
8040 - // Convert bot and agent roles to assistant
8041 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
8042 - $message['role'] = 'assistant';
8043 - }
8044 -
8045 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
8046 - if (!in_array($message['role'], ['assistant', 'user'])) {
8047 - $message['role'] = 'user';
8048 - }
8049 -
8050 - // Ensure content field exists
8051 - if (!isset($message['content']) || empty($message['content'])) {
8052 - $message['content'] = '';
8053 - }
8054 -
8055 - // Remove any unsupported fields
8056 - $message = array_intersect_key($message, array_flip(['role', 'content']));
8057 - }
8058 -
8059 - // Add relevant content as the latest user message
8060 - $conversation_history[] = [
8061 - 'role' => 'user',
8062 - 'content' => $relevant_content
8063 - ];
8064 -
8065 - // Prepare the request body with stream: true
8066 - $body = json_encode([
8067 - 'model' => $selected_model,
8068 - 'messages' => $conversation_history,
8069 - 'max_tokens' => 1000,
8070 - 'temperature' => 0.8,
8071 - 'system' => $system_prompt_instructions,
8072 - 'stream' => true
8073 - ]);
8074 -
8075 - // Check if we can actually stream (headers not sent, etc.)
8076 - if (headers_sent() || !function_exists('curl_init')) {
8077 - // Fallback to regular response with testing data
8078 - //error_log("MxChat: Streaming not possible, falling back to regular response");
8079 - $regular_response = $this->mxchat_generate_response_claude(
8080 - $selected_model,
8081 - $claude_api_key,
8082 - array_slice($conversation_history, 0, -1), // Remove the added content
8083 - $relevant_content
8084 - );
8085 -
8086 - // Save bot response to transcript
8087 - if (!empty($regular_response) && !empty($session_id)) {
8088 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8089 - }
8090 -
8091 - // Return as JSON with testing data
8092 - $response_data = [
8093 - 'text' => $regular_response,
8094 - 'html' => '',
8095 - 'session_id' => $session_id
8096 - ];
8097 -
8098 - if ($testing_data !== null) {
8099 - $response_data['testing_data'] = $testing_data;
8100 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
8101 - }
8102 -
8103 - // Clear any streaming headers and send JSON
8104 - if (headers_sent() === false) {
8105 - header('Content-Type: application/json');
8106 - }
8107 - echo json_encode($response_data);
8108 - return true; // Indicate we handled the response
8109 - }
8110 -
8111 - // Setup streaming headers now that we know we're actually streaming
8112 - $this->setup_streaming_headers();
8113 -
8114 - // Use cURL for streaming support
8115 - $ch = curl_init();
8116 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
8117 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8118 - curl_setopt($ch, CURLOPT_POST, true);
8119 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8120 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8121 - 'Content-Type: application/json',
8122 - 'x-api-key: ' . $claude_api_key,
8123 - 'anthropic-version: 2023-06-01'
8124 - ));
8125 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8126 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8127 -
8128 - $full_response = ''; // Accumulate full response for saving
8129 - $stream_started = false;
8130 - $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
8131 -
8132 - // Buffer control for real-time streaming
8133 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
8134 - // Send testing data as the first event if available
8135 - if (!$stream_started && $testing_data !== null) {
8136 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8137 - flush();
8138 - $stream_started = true;
8139 - //error_log("MxChat Testing: Sent testing data in Claude stream");
8140 - }
8141 -
8142 - // CRITICAL FIX: Append new data to buffer
8143 - $buffer .= $data;
8144 -
8145 - // Process complete lines only
8146 - $lines = explode("\n", $buffer);
8147 -
8148 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8149 - // The last element might be incomplete, so keep it in buffer
8150 - $buffer = array_pop($lines);
8151 -
8152 - foreach ($lines as $line) {
8153 - if (trim($line) === '') {
8154 - continue;
8155 - }
8156 -
8157 - // Claude uses event: and data: format
8158 - if (strpos($line, 'event: ') === 0) {
8159 - // Store the event type for the next data line
8160 - continue;
8161 - }
8162 -
8163 - if (strpos($line, 'data: ') === 0) {
8164 - $json_str = substr($line, 6); // Remove 'data: ' prefix
8165 -
8166 - $json = json_decode(trim($json_str), true);
8167 - if (json_last_error() !== JSON_ERROR_NONE) {
8168 - continue;
8169 - }
8170 -
8171 - // Handle different event types
8172 - if (isset($json['type'])) {
8173 - switch ($json['type']) {
8174 - case 'content_block_delta':
8175 - if (isset($json['delta']['text'])) {
8176 - $content = $json['delta']['text'];
8177 - $full_response .= $content; // Accumulate
8178 - // Send as SSE format compatible with your frontend
8179 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8180 - flush();
8181 - }
8182 - break;
8183 -
8184 - case 'message_stop':
8185 - echo "data: [DONE]\n\n";
8186 - flush();
8187 - break;
8188 -
8189 - case 'error':
8190 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8191 - flush();
8192 - break;
8193 - }
8194 - }
8195 - }
8196 - }
8197 -
8198 - return strlen($data);
8199 - });
8200 -
8201 - $response = curl_exec($ch);
8202 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8203 -
8204 - if (curl_errno($ch)) {
8205 - curl_close($ch);
8206 - throw new Exception('cURL Error: ' . curl_error($ch));
8207 - }
8208 -
8209 - curl_close($ch);
8210 -
8211 - if ($http_code !== 200) {
8212 - // Fallback to regular response
8213 - //error_log("MxChat: Claude streaming failed with HTTP $http_code, falling back");
8214 - $regular_response = $this->mxchat_generate_response_claude(
8215 - $selected_model,
8216 - $claude_api_key,
8217 - array_slice($conversation_history, 0, -1), // Remove the added content
8218 - $relevant_content
8219 - );
8220 -
8221 - // FIXED: Check if regular response returned an error
8222 - if (is_array($regular_response) && isset($regular_response['error'])) {
8223 - // Send error in SSE format since we're in streaming mode
8224 - echo "data: " . json_encode([
8225 - 'error' => true,
8226 - 'error_message' => $regular_response['error'],
8227 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8228 - 'text' => $regular_response['error'],
8229 - 'message' => $regular_response['error']
8230 - ]) . "\n\n";
8231 - echo "data: [DONE]\n\n";
8232 - flush();
8233 - return true;
8234 - }
8235 -
8236 - $response_data = [
8237 - 'text' => $regular_response,
8238 - 'html' => '',
8239 - 'session_id' => $session_id
8240 - ];
8241 -
8242 - if ($testing_data !== null) {
8243 - $response_data['testing_data'] = $testing_data;
8244 - //error_log("MxChat Testing: Added testing data to Claude error fallback");
8245 - }
8246 -
8247 - header('Content-Type: application/json');
8248 - echo json_encode($response_data);
8249 - return true;
8250 - }
8251 -
8252 - // Save the complete response to maintain chat persistence
8253 - if (!empty($full_response) && !empty($session_id)) {
8254 - // Prepare RAG context for streaming response
8255 - $rag_context_for_storage = null;
8256 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8257 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8258 -
8259 - if ($has_rag_data || $has_action_data) {
8260 - $rag_context_for_storage = [];
8261 -
8262 - if ($has_rag_data) {
8263 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8264 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8265 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8266 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8267 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8268 - }
8269 -
8270 - if ($has_action_data) {
8271 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8272 - }
8273 - }
8274 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8275 - }
8276 -
8277 - return true; // Indicate streaming completed successfully
8278 -
8279 - } catch (Exception $e) {
8280 - //error_log("MxChat Claude streaming exception: " . $e->getMessage());
8281 -
8282 - // Fallback to regular response on exception
8283 - $regular_response = $this->mxchat_generate_response_claude(
8284 - $selected_model,
8285 - $claude_api_key,
8286 - $conversation_history,
8287 - $relevant_content
8288 - );
8289 -
8290 - // FIXED: Check if regular response returned an error
8291 - if (is_array($regular_response) && isset($regular_response['error'])) {
8292 - // Send error in SSE format since we're in streaming mode
8293 - echo "data: " . json_encode([
8294 - 'error' => true,
8295 - 'error_message' => $regular_response['error'],
8296 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8297 - 'text' => $regular_response['error'],
8298 - 'message' => $regular_response['error']
8299 - ]) . "\n\n";
8300 - echo "data: [DONE]\n\n";
8301 - flush();
8302 - return true;
8303 - }
8304 -
8305 - $response_data = [
8306 - 'text' => $regular_response,
8307 - 'html' => '',
8308 - 'session_id' => $session_id
8309 - ];
8310 -
8311 - if ($testing_data !== null) {
8312 - $response_data['testing_data'] = $testing_data;
8313 - //error_log("MxChat Testing: Added testing data to Claude exception fallback");
8314 - }
8315 -
8316 - header('Content-Type: application/json');
8317 - echo json_encode($response_data);
8318 - return true;
8319 - }
8320 -}
8321 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8322 - try {
8323 - // Get bot ID from session or request
8324 - $bot_id = $this->get_current_bot_id($session_id);
8325 -
8326 - // Get system prompt instructions using centralized function
8327 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8328 -
8329 - // Ensure conversation_history is an array
8330 - if (!is_array($conversation_history)) {
8331 - $conversation_history = array();
8332 - }
8333 -
8334 - // Format conversation history for X.AI (same as OpenAI format)
8335 - $formatted_conversation = array();
8336 -
8337 - $formatted_conversation[] = array(
8338 - 'role' => 'system',
8339 - 'content' => $system_prompt_instructions . " " . $relevant_content
8340 - );
8341 -
8342 - foreach ($conversation_history as $message) {
8343 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8344 - $role = $message['role'];
8345 - if ($role === 'bot' || $role === 'agent') {
8346 - $role = 'assistant';
8347 - }
8348 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8349 - $role = 'user';
8350 - }
8351 - $formatted_conversation[] = array(
8352 - 'role' => $role,
8353 - 'content' => $message['content']
8354 - );
8355 - }
8356 - }
8357 -
8358 - // Check if we can actually stream
8359 - if (headers_sent() || !function_exists('curl_init')) {
8360 - // Fallback to regular response with testing data
8361 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
8362 - $regular_response = $this->mxchat_generate_response_xai(
8363 - $selected_model,
8364 - $xai_api_key,
8365 - $conversation_history,
8366 - $relevant_content
8367 - );
8368 -
8369 - // Save bot response to transcript
8370 - if (!empty($regular_response) && !empty($session_id)) {
8371 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8372 - }
8373 -
8374 - $response_data = [
8375 - 'text' => $regular_response,
8376 - 'html' => '',
8377 - 'session_id' => $session_id
8378 - ];
8379 -
8380 - if ($testing_data !== null) {
8381 - $response_data['testing_data'] = $testing_data;
8382 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
8383 - }
8384 -
8385 - header('Content-Type: application/json');
8386 - echo json_encode($response_data);
8387 - return true;
8388 - }
8389 -
8390 - // Prepare the request body with stream: true
8391 - $body = json_encode([
8392 - 'model' => $selected_model,
8393 - 'messages' => $formatted_conversation,
8394 - 'temperature' => 0.8,
8395 - 'stream' => true
8396 - ]);
8397 -
8398 - // Setup streaming headers now that we know we're actually streaming
8399 - $this->setup_streaming_headers();
8400 -
8401 - // Use cURL for streaming support
8402 - $ch = curl_init();
8403 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
8404 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8405 - curl_setopt($ch, CURLOPT_POST, true);
8406 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8407 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8408 - 'Content-Type: application/json',
8409 - 'Authorization: Bearer ' . $xai_api_key
8410 - ));
8411 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8412 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8413 -
8414 - $full_response = ''; // Accumulate full response for saving
8415 - $stream_started = false;
8416 - $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
8417 -
8418 - // Buffer control for real-time streaming
8419 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
8420 - // Send testing data as the first event if available
8421 - if (!$stream_started && $testing_data !== null) {
8422 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8423 - flush();
8424 - $stream_started = true;
8425 - //error_log("MxChat Testing: Sent testing data in X.AI stream");
8426 - }
8427 -
8428 - // CRITICAL FIX: Append new data to buffer
8429 - $buffer .= $data;
8430 -
8431 - // Process complete lines only
8432 - $lines = explode("\n", $buffer);
8433 -
8434 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8435 - // The last element might be incomplete, so keep it in buffer
8436 - $buffer = array_pop($lines);
8437 -
8438 - foreach ($lines as $line) {
8439 - // Skip empty lines
8440 - if (trim($line) === '') {
8441 - continue;
8442 - }
8443 -
8444 - // Only process lines that start with "data: "
8445 - if (strpos($line, 'data: ') !== 0) {
8446 - continue;
8447 - }
8448 -
8449 - $json_str = substr($line, 6); // Remove 'data: ' prefix
8450 -
8451 - if (trim($json_str) === '[DONE]') {
8452 - echo "data: [DONE]\n\n";
8453 - flush();
8454 - continue;
8455 - }
8456 -
8457 - // Try to decode JSON
8458 - $json = json_decode(trim($json_str), true);
8459 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8460 - $content = $json['choices'][0]['delta']['content'];
8461 - $full_response .= $content; // Accumulate
8462 - // Send as SSE format
8463 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8464 - flush();
8465 - }
8466 - }
8467 -
8468 - return strlen($data);
8469 - });
8470 -
8471 - $response = curl_exec($ch);
8472 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8473 -
8474 - if (curl_errno($ch) || $http_code !== 200) {
8475 - curl_close($ch);
8476 -
8477 - // Fallback to regular response
8478 - //error_log("MxChat: X.AI streaming failed, falling back");
8479 - $regular_response = $this->mxchat_generate_response_xai(
8480 - $selected_model,
8481 - $xai_api_key,
8482 - $conversation_history,
8483 - $relevant_content
8484 - );
8485 -
8486 - $response_data = [
8487 - 'text' => $regular_response,
8488 - 'html' => '',
8489 - 'session_id' => $session_id
8490 - ];
8491 -
8492 - if ($testing_data !== null) {
8493 - $response_data['testing_data'] = $testing_data;
8494 - //error_log("MxChat Testing: Added testing data to X.AI error fallback");
8495 - }
8496 -
8497 - header('Content-Type: application/json');
8498 - echo json_encode($response_data);
8499 - return true;
8500 - }
8501 -
8502 - curl_close($ch);
8503 -
8504 - // Save the complete response to maintain chat persistence
8505 - if (!empty($full_response) && !empty($session_id)) {
8506 - // Prepare RAG context for streaming response
8507 - $rag_context_for_storage = null;
8508 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8509 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8510 -
8511 - if ($has_rag_data || $has_action_data) {
8512 - $rag_context_for_storage = [];
8513 -
8514 - if ($has_rag_data) {
8515 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8516 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8517 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8518 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8519 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8520 - }
8521 -
8522 - if ($has_action_data) {
8523 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8524 - }
8525 - }
8526 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8527 - }
8528 -
8529 - return true; // Indicate streaming completed successfully
8530 -
8531 - } catch (Exception $e) {
8532 - //error_log("MxChat X.AI streaming exception: " . $e->getMessage());
8533 -
8534 - // Fallback to regular response
8535 - $regular_response = $this->mxchat_generate_response_xai(
8536 - $selected_model,
8537 - $xai_api_key,
8538 - $conversation_history,
8539 - $relevant_content
8540 - );
8541 -
8542 - $response_data = [
8543 - 'text' => $regular_response,
8544 - 'html' => '',
8545 - 'session_id' => $session_id
8546 - ];
8547 -
8548 - if ($testing_data !== null) {
8549 - $response_data['testing_data'] = $testing_data;
8550 - //error_log("MxChat Testing: Added testing data to X.AI exception fallback");
8551 - }
8552 -
8553 - header('Content-Type: application/json');
8554 - echo json_encode($response_data);
8555 - return true;
8556 - }
8557 -}
8558 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8559 - try {
8560 - // Get bot ID from session or request
8561 - $bot_id = $this->get_current_bot_id($session_id);
8562 -
8563 - // Get system prompt instructions using centralized function
8564 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8565 -
8566 - // Ensure conversation_history is an array
8567 - if (!is_array($conversation_history)) {
8568 - $conversation_history = array();
8569 - }
8570 -
8571 - // Format conversation history for DeepSeek
8572 - $formatted_conversation = array();
8573 -
8574 - $formatted_conversation[] = array(
8575 - 'role' => 'system',
8576 - 'content' => $system_prompt_instructions . " " . $relevant_content
8577 - );
8578 -
8579 - foreach ($conversation_history as $message) {
8580 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8581 - $role = $message['role'];
8582 - if ($role === 'bot' || $role === 'agent') {
8583 - $role = 'assistant';
8584 - }
8585 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8586 - $role = 'user';
8587 - }
8588 - $formatted_conversation[] = array(
8589 - 'role' => $role,
8590 - 'content' => $message['content']
8591 - );
8592 - }
8593 - }
8594 -
8595 - // Check if we can actually stream
8596 - if (headers_sent() || !function_exists('curl_init')) {
8597 - // Fallback to regular response with testing data
8598 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
8599 - $regular_response = $this->mxchat_generate_response_deepseek(
8600 - $selected_model,
8601 - $deepseek_api_key,
8602 - $conversation_history,
8603 - $relevant_content
8604 - );
8605 -
8606 - // Save bot response to transcript
8607 - if (!empty($regular_response) && !empty($session_id)) {
8608 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8609 - }
8610 -
8611 - $response_data = [
8612 - 'text' => $regular_response,
8613 - 'html' => '',
8614 - 'session_id' => $session_id
8615 - ];
8616 -
8617 - if ($testing_data !== null) {
8618 - $response_data['testing_data'] = $testing_data;
8619 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
8620 - }
8621 -
8622 - header('Content-Type: application/json');
8623 - echo json_encode($response_data);
8624 - return true;
8625 - }
8626 -
8627 - // Prepare the request body with stream: true
8628 - $body = json_encode([
8629 - 'model' => $selected_model,
8630 - 'messages' => $formatted_conversation,
8631 - 'temperature' => 0.8,
8632 - 'stream' => true
8633 - ]);
8634 -
8635 - // Setup streaming headers now that we know we're actually streaming
8636 - $this->setup_streaming_headers();
8637 -
8638 - // Use cURL for streaming support
8639 - $ch = curl_init();
8640 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
8641 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8642 - curl_setopt($ch, CURLOPT_POST, true);
8643 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8644 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8645 - 'Content-Type: application/json',
8646 - 'Authorization: Bearer ' . $deepseek_api_key
8647 - ));
8648 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8649 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8650 -
8651 - $full_response = ''; // Accumulate full response for saving
8652 - $stream_started = false;
8653 - $buffer = ''; // CRITICAL: Add persistent buffer for incomplete chunks
8654 -
8655 - // Buffer control for real-time streaming
8656 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, $testing_data) {
8657 - // Send testing data as the first event if available
8658 - if (!$stream_started && $testing_data !== null) {
8659 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8660 - flush();
8661 - $stream_started = true;
8662 - //error_log("MxChat Testing: Sent testing data in DeepSeek stream");
8663 - }
8664 -
8665 - // CRITICAL FIX: Append new data to buffer
8666 - $buffer .= $data;
8667 -
8668 - // Process complete lines only
8669 - $lines = explode("\n", $buffer);
8670 -
8671 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8672 - // The last element might be incomplete, so keep it in buffer
8673 - $buffer = array_pop($lines);
8674 -
8675 - foreach ($lines as $line) {
8676 - // Skip empty lines
8677 - if (trim($line) === '') {
8678 - continue;
8679 - }
8680 -
8681 - // Only process lines that start with "data: "
8682 - if (strpos($line, 'data: ') !== 0) {
8683 - continue;
8684 - }
8685 -
8686 - $json_str = substr($line, 6); // Remove 'data: ' prefix
8687 -
8688 - if (trim($json_str) === '[DONE]') {
8689 - echo "data: [DONE]\n\n";
8690 - flush();
8691 - continue;
8692 - }
8693 -
8694 - // Try to decode JSON
8695 - $json = json_decode(trim($json_str), true);
8696 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8697 - $content = $json['choices'][0]['delta']['content'];
8698 - $full_response .= $content; // Accumulate the full response
8699 -
8700 - // Send as SSE format
8701 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8702 - flush();
8703 - }
8704 - }
8705 -
8706 - return strlen($data);
8707 - });
8708 -
8709 - $response = curl_exec($ch);
8710 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8711 -
8712 - if (curl_errno($ch) || $http_code !== 200) {
8713 - $curl_error = curl_error($ch);
8714 - curl_close($ch);
8715 -
8716 - // Log the specific error for debugging
8717 - //error_log("MxChat: DeepSeek streaming failed - HTTP: $http_code, cURL: $curl_error");
8718 -
8719 - // Fallback to regular response
8720 - $regular_response = $this->mxchat_generate_response_deepseek(
8721 - $selected_model,
8722 - $deepseek_api_key,
8723 - $conversation_history,
8724 - $relevant_content
8725 - );
8726 -
8727 - // Handle error response from regular function
8728 - if (is_array($regular_response) && isset($regular_response['error'])) {
8729 - if ($testing_data !== null) {
8730 - $regular_response['testing_data'] = $testing_data;
8731 - }
8732 - header('Content-Type: application/json');
8733 - echo json_encode($regular_response);
8734 - return true;
8735 - }
8736 -
8737 - $response_data = [
8738 - 'text' => $regular_response,
8739 - 'html' => '',
8740 - 'session_id' => $session_id
8741 - ];
8742 -
8743 - if ($testing_data !== null) {
8744 - $response_data['testing_data'] = $testing_data;
8745 - //error_log("MxChat Testing: Added testing data to DeepSeek error fallback");
8746 - }
8747 -
8748 - header('Content-Type: application/json');
8749 - echo json_encode($response_data);
8750 - return true;
8751 - }
8752 -
8753 - curl_close($ch);
8754 -
8755 - // Save the complete response to maintain chat persistence
8756 - if (!empty($full_response) && !empty($session_id)) {
8757 - // Prepare RAG context for streaming response
8758 - $rag_context_for_storage = null;
8759 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8760 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8761 -
8762 - if ($has_rag_data || $has_action_data) {
8763 - $rag_context_for_storage = [];
8764 -
8765 - if ($has_rag_data) {
8766 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8767 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8768 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8769 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8770 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8771 - }
8772 -
8773 - if ($has_action_data) {
8774 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8775 - }
8776 - }
8777 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8778 - }
8779 -
8780 - return true; // Indicate streaming completed successfully
8781 -
8782 - } catch (Exception $e) {
8783 - //error_log("MxChat DeepSeek streaming exception: " . $e->getMessage());
8784 -
8785 - // Fallback to regular response
8786 - $regular_response = $this->mxchat_generate_response_deepseek(
8787 - $selected_model,
8788 - $deepseek_api_key,
8789 - $conversation_history,
8790 - $relevant_content
8791 - );
8792 -
8793 - // Handle error response from regular function
8794 - if (is_array($regular_response) && isset($regular_response['error'])) {
8795 - if ($testing_data !== null) {
8796 - $regular_response['testing_data'] = $testing_data;
8797 - }
8798 - header('Content-Type: application/json');
8799 - echo json_encode($regular_response);
8800 - return true;
8801 - }
8802 -
8803 - $response_data = [
8804 - 'text' => $regular_response,
8805 - 'html' => '',
8806 - 'session_id' => $session_id
8807 - ];
8808 -
8809 - if ($testing_data !== null) {
8810 - $response_data['testing_data'] = $testing_data;
8811 - //error_log("MxChat Testing: Added testing data to DeepSeek exception fallback");
8812 - }
8813 -
8814 - header('Content-Type: application/json');
8815 - echo json_encode($response_data);
8816 - return true;
8817 - }
8818 -}
8819 -
8820 -
8821 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content) {
8822 - try {
8823 - if (!is_array($conversation_history)) {
8824 - $conversation_history = array();
8825 - }
8826 -
8827 - $bot_id = $this->get_current_bot_id('');
8828 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8829 -
8830 - $formatted_conversation = array();
8831 -
8832 - $formatted_conversation[] = array(
8833 - 'role' => 'system',
8834 - 'content' => $system_prompt_instructions . " " . $relevant_content
8835 - );
8836 -
8837 - foreach ($conversation_history as $message) {
8838 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8839 - $role = $message['role'];
8840 -
8841 - if ($role === 'bot' || $role === 'agent') {
8842 - $role = 'assistant';
8843 - }
8844 - if (!in_array($role, ['system', 'assistant', 'user'])) {
8845 - $role = 'user';
8846 - }
8847 -
8848 - $formatted_conversation[] = array(
8849 - 'role' => $role,
8850 - 'content' => $message['content']
8851 - );
8852 - }
8853 - }
8854 -
8855 - $body = json_encode([
8856 - 'model' => $selected_model,
8857 - 'messages' => $formatted_conversation,
8858 - 'temperature' => 1,
8859 - ]);
8860 -
8861 - $args = [
8862 - 'body' => $body,
8863 - 'headers' => [
8864 - 'Content-Type' => 'application/json',
8865 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
8866 - 'HTTP-Referer' => home_url(),
8867 - 'X-Title' => get_bloginfo('name'),
8868 - ],
8869 - 'timeout' => 60,
8870 - 'redirection' => 5,
8871 - 'blocking' => true,
8872 - 'httpversion' => '1.0',
8873 - 'sslverify' => true,
8874 - ];
8875 -
8876 - $response = wp_remote_post('https://openrouter.ai/api/v1/chat/completions', $args);
8877 -
8878 - if (is_wp_error($response)) {
8879 - $error_message = $response->get_error_message();
8880 - return [
8881 - 'error' => esc_html__('Connection error when contacting OpenRouter: ', 'mxchat') . esc_html($error_message),
8882 - 'error_code' => 'openrouter_connection_error',
8883 - 'provider' => 'openrouter'
8884 - ];
8885 - }
8886 -
8887 - $status_code = wp_remote_retrieve_response_code($response);
8888 - if ($status_code !== 200) {
8889 - $response_body = wp_remote_retrieve_body($response);
8890 - $decoded_response = json_decode($response_body, true);
8891 -
8892 - $error_message = isset($decoded_response['error']['message'])
8893 - ? $decoded_response['error']['message']
8894 - : 'HTTP Error ' . $status_code;
8895 -
8896 - return [
8897 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
8898 - 'error_code' => 'openrouter_api_error',
8899 - 'provider' => 'openrouter',
8900 - 'status_code' => $status_code
8901 - ];
8902 - }
8903 -
8904 - $response_body = wp_remote_retrieve_body($response);
8905 - $decoded_response = json_decode($response_body, true);
8906 -
8907 - if (isset($decoded_response['choices'][0]['message']['content'])) {
8908 - return trim($decoded_response['choices'][0]['message']['content']);
8909 - } else {
8910 - return [
8911 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
8912 - 'error_code' => 'openrouter_response_format_error',
8913 - 'provider' => 'openrouter'
8914 - ];
8915 - }
8916 - } catch (Exception $e) {
8917 - return [
8918 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
8919 - 'error_code' => 'openrouter_exception',
8920 - 'provider' => 'openrouter'
8921 - ];
8922 - }
8923 -}
8924 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
8925 -
8926 - // Get bot ID from session or request
8927 - $bot_id = $this->get_current_bot_id($session_id);
8928 -
8929 - // Get system prompt instructions using centralized function
8930 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8931 -
8932 - // Clean and validate conversation history
8933 - foreach ($conversation_history as &$message) {
8934 - // Convert bot and agent roles to assistant
8935 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
8936 - $message['role'] = 'assistant';
8937 - }
8938 -
8939 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
8940 - if (!in_array($message['role'], ['assistant', 'user'])) {
8941 - $message['role'] = 'user';
8942 - }
8943 -
8944 - // Ensure content field exists
8945 - if (!isset($message['content']) || empty($message['content'])) {
8946 - $message['content'] = '';
8947 - }
8948 -
8949 - // Remove any unsupported fields
8950 - $message = array_intersect_key($message, array_flip(['role', 'content']));
8951 - }
8952 -
8953 - // Add relevant content as the latest user message
8954 - $conversation_history[] = [
8955 - 'role' => 'user',
8956 - 'content' => $relevant_content
8957 - ];
8958 -
8959 - // Build request body
8960 - $body = json_encode([
8961 - 'model' => $selected_model,
8962 - 'max_tokens' => 1000,
8963 - 'temperature' => 0.8,
8964 - 'messages' => $conversation_history,
8965 - 'system' => $system_prompt_instructions
8966 - ]);
8967 -
8968 - // Set up API request
8969 - $args = [
8970 - 'body' => $body,
8971 - 'headers' => [
8972 - 'Content-Type' => 'application/json',
8973 - 'x-api-key' => $claude_api_key,
8974 - 'anthropic-version' => '2023-06-01'
8975 - ],
8976 - 'timeout' => 60,
8977 - 'redirection' => 5,
8978 - 'blocking' => true,
8979 - 'httpversion' => '1.0',
8980 - 'sslverify' => true,
8981 - ];
8982 -
8983 - // Make API request
8984 - $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
8985 -
8986 - // Check for WordPress errors
8987 - if (is_wp_error($response)) {
8988 - //error_log("Claude API request error: " . $response->get_error_message());
8989 - return "Sorry, there was an error connecting to the API.";
8990 - }
8991 -
8992 - // Check HTTP response code
8993 - $http_code = wp_remote_retrieve_response_code($response);
8994 - if ($http_code !== 200) {
8995 - $error_body = wp_remote_retrieve_body($response);
8996 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
8997 -
8998 - // Try to extract error message from response
8999 - $error_data = json_decode($error_body, true);
9000 - $error_message = isset($error_data['error']['message']) ?
9001 - $error_data['error']['message'] :
9002 - "HTTP error " . $http_code;
9003 -
9004 - return "Sorry, the API returned an error: " . $error_message;
9005 - }
9006 -
9007 - // Parse response
9008 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
9009 -
9010 - // Check for JSON decode errors
9011 - if (json_last_error() !== JSON_ERROR_NONE) {
9012 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
9013 - return "Sorry, there was an error processing the API response.";
9014 - }
9015 -
9016 - // Extract and validate response content
9017 - if (isset($response_body['content']) &&
9018 - is_array($response_body['content']) &&
9019 - !empty($response_body['content']) &&
9020 - isset($response_body['content'][0]['text'])) {
9021 - return trim($response_body['content'][0]['text']);
9022 - }
9023 -
9024 - // Log unexpected response format
9025 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
9026 - return "Sorry, I received an unexpected response format from the API.";
9027 -}
9028 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
9029 - try {
9030 - // Ensure conversation_history is an array
9031 - if (!is_array($conversation_history)) {
9032 - $conversation_history = array();
9033 - }
9034 -
9035 - // Get bot ID from session or request
9036 - $bot_id = $this->get_current_bot_id('');
9037 -
9038 - // Get system prompt instructions using centralized function
9039 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9040 -
9041 - // Create a new array for the formatted conversation
9042 - $formatted_conversation = array();
9043 -
9044 - // Add system message first
9045 - $formatted_conversation[] = array(
9046 - 'role' => 'system',
9047 - 'content' => $system_prompt_instructions . " " . $relevant_content
9048 - );
9049 -
9050 - // Add the rest of the conversation history
9051 - foreach ($conversation_history as $message) {
9052 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9053 - $role = $message['role'];
9054 -
9055 - // Convert roles to supported format
9056 - if ($role === 'bot' || $role === 'agent') {
9057 - $role = 'assistant';
9058 - }
9059 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9060 - $role = 'user';
9061 - }
9062 -
9063 - $formatted_conversation[] = array(
9064 - 'role' => $role,
9065 - 'content' => $message['content']
9066 - );
9067 - }
9068 - }
9069 -
9070 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
9071 - $is_gpt5_model = (
9072 - strpos($selected_model, 'gpt-5') === 0 ||
9073 - $selected_model === 'gpt-5.2' ||
9074 - $selected_model === 'gpt-5.1-2025-11-13' ||
9075 - $selected_model === 'gpt-5' ||
9076 - $selected_model === 'gpt-5-mini' ||
9077 - $selected_model === 'gpt-5-nano'
9078 - );
9079 -
9080 - // Build request body with optimal settings for fast responses
9081 - $request_body = [
9082 - 'model' => $selected_model,
9083 - 'messages' => $formatted_conversation,
9084 - 'temperature' => 1,
9085 - 'stream' => false
9086 - ];
9087 -
9088 - // Add reasoning_effort only for GPT-5 models that support it
9089 - // These chat models don't support reasoning_effort parameter
9090 - $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');
9091 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
9092 - // GPT-5.1 uses 'low' instead of 'minimal'
9093 - if ($selected_model === 'gpt-5.1-2025-11-13') {
9094 - $request_body['reasoning_effort'] = 'low';
9095 - } elseif ($selected_model === 'gpt-5.5') {
9096 - $request_body['reasoning_effort'] = 'none';
9097 - } elseif ($selected_model === 'gpt-5.4') {
9098 - $request_body['reasoning_effort'] = 'none';
9099 - } else {
9100 - $request_body['reasoning_effort'] = 'minimal';
9101 - }
9102 - }
9103 -
9104 - $body = json_encode($request_body);
9105 -
9106 - $args = [
9107 - 'body' => $body,
9108 - 'headers' => [
9109 - 'Content-Type' => 'application/json',
9110 - 'Authorization' => 'Bearer ' . $api_key,
9111 - ],
9112 - 'timeout' => 60,
9113 - 'redirection' => 5,
9114 - 'blocking' => true,
9115 - 'httpversion' => '1.0',
9116 - 'sslverify' => true,
9117 - ];
9118 -
9119 - $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
9120 -
9121 - if (is_wp_error($response)) {
9122 - $error_message = $response->get_error_message();
9123 - return [
9124 - 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9125 - 'error_code' => 'openai_connection_error',
9126 - 'provider' => 'openai'
9127 - ];
9128 - }
9129 -
9130 - $status_code = wp_remote_retrieve_response_code($response);
9131 - if ($status_code !== 200) {
9132 - $response_body = wp_remote_retrieve_body($response);
9133 - $decoded_response = json_decode($response_body, true);
9134 -
9135 - $error_message = isset($decoded_response['error']['message'])
9136 - ? $decoded_response['error']['message']
9137 - : 'HTTP Error ' . $status_code;
9138 -
9139 - $error_type = isset($decoded_response['error']['type'])
9140 - ? $decoded_response['error']['type']
9141 - : 'unknown';
9142 -
9143 - // Handle specific error types
9144 - switch ($error_type) {
9145 - case 'invalid_request_error':
9146 - if (strpos($error_message, 'API key') !== false) {
9147 - return [
9148 - 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
9149 - 'error_code' => 'openai_invalid_api_key',
9150 - 'provider' => 'openai'
9151 - ];
9152 - }
9153 - break;
9154 -
9155 - case 'authentication_error':
9156 - return [
9157 - 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
9158 - 'error_code' => 'openai_auth_error',
9159 - 'provider' => 'openai'
9160 - ];
9161 -
9162 - case 'rate_limit_exceeded':
9163 - return [
9164 - 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
9165 - 'error_code' => 'openai_rate_limit',
9166 - 'provider' => 'openai'
9167 - ];
9168 -
9169 - case 'quota_exceeded':
9170 - return [
9171 - 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
9172 - 'error_code' => 'openai_quota_exceeded',
9173 - 'provider' => 'openai'
9174 - ];
9175 - }
9176 -
9177 - // Generic error fallback
9178 - return [
9179 - 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
9180 - 'error_code' => 'openai_api_error',
9181 - 'provider' => 'openai',
9182 - 'status_code' => $status_code
9183 - ];
9184 - }
9185 -
9186 - $response_body = wp_remote_retrieve_body($response);
9187 - $decoded_response = json_decode($response_body, true);
9188 -
9189 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9190 - return trim($decoded_response['choices'][0]['message']['content']);
9191 - } else {
9192 - return [
9193 - 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9194 - 'error_code' => 'openai_response_format_error',
9195 - 'provider' => 'openai'
9196 - ];
9197 - }
9198 - } catch (Exception $e) {
9199 - return [
9200 - 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9201 - 'error_code' => 'openai_exception',
9202 - 'provider' => 'openai'
9203 - ];
9204 - }
9205 -}
9206 -
9207 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9208 - try {
9209 - // Get bot ID from session or request
9210 - $bot_id = $this->get_current_bot_id($session_id);
9211 -
9212 - // Get system prompt instructions using centralized function
9213 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9214 -
9215 - // Add system prompt to relevant content
9216 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9217 -
9218 - // Prepend system instructions to the conversation history
9219 - array_unshift($conversation_history, [
9220 - 'role' => 'system',
9221 - 'content' => "Here are your instructions: " . $content_with_instructions
9222 - ]);
9223 -
9224 - // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
9225 - foreach ($conversation_history as &$message) {
9226 - if ($message['role'] === 'bot') {
9227 - $message['role'] = 'assistant';
9228 - } elseif ($message['role'] === 'agent') {
9229 - // Tag the message as coming from a live agent
9230 - $message['role'] = 'assistant';
9231 - if (!isset($message['metadata'])) {
9232 - $message['metadata'] = ['source' => 'live_agent'];
9233 - }
9234 - }
9235 -
9236 - // Ensure all roles are valid
9237 - if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
9238 - $message['role'] = 'user'; // Default to 'user'
9239 - }
9240 - }
9241 -
9242 - // Build the request body
9243 - $body = json_encode([
9244 - 'model' => $selected_model,
9245 - 'messages' => $conversation_history,
9246 - 'temperature' => 0.8,
9247 - 'stream' => false
9248 - ]);
9249 -
9250 - // Set up the API request
9251 - $args = [
9252 - 'body' => $body,
9253 - 'headers' => [
9254 - 'Content-Type' => 'application/json',
9255 - 'Authorization' => 'Bearer ' . $xai_api_key,
9256 - ],
9257 - 'timeout' => 60,
9258 - 'redirection' => 5,
9259 - 'blocking' => true,
9260 - 'httpversion' => '1.0',
9261 - 'sslverify' => true,
9262 - ];
9263 -
9264 - // Make the API request
9265 - $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
9266 -
9267 - // Process the response
9268 - if (is_wp_error($response)) {
9269 - $error_message = $response->get_error_message();
9270 - //error_log('X.AI API Error: ' . $error_message);
9271 - return [
9272 - 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
9273 - 'error_code' => 'xai_connection_error',
9274 - 'provider' => 'xai'
9275 - ];
9276 - }
9277 -
9278 - $status_code = wp_remote_retrieve_response_code($response);
9279 - if ($status_code !== 200) {
9280 - $response_body = wp_remote_retrieve_body($response);
9281 - $decoded_response = json_decode($response_body, true);
9282 -
9283 - // Log the full response for debugging
9284 - //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
9285 -
9286 - // Extract error message from X.AI's specific format
9287 - $error_message = '';
9288 -
9289 - // Check for direct error string (as seen in your logs)
9290 - if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
9291 - $error_message = $decoded_response['error'];
9292 - }
9293 - // Check for nested error object (OpenAI style)
9294 - elseif (isset($decoded_response['error']['message'])) {
9295 - $error_message = $decoded_response['error']['message'];
9296 - }
9297 - // Check for top-level message
9298 - elseif (isset($decoded_response['message'])) {
9299 - $error_message = $decoded_response['message'];
9300 - }
9301 - // Fallback
9302 - else {
9303 - $error_message = 'HTTP Error ' . $status_code;
9304 - }
9305 -
9306 - //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
9307 -
9308 - // Check for API key errors using string matching
9309 - if (stripos($error_message, 'api key') !== false ||
9310 - stripos($error_message, 'incorrect api key') !== false ||
9311 - stripos($error_message, 'invalid api key') !== false) {
9312 - return [
9313 - 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
9314 - 'error_code' => 'xai_invalid_api_key',
9315 - 'provider' => 'xai'
9316 - ];
9317 - }
9318 -
9319 - // Authentication errors
9320 - if ($status_code === 401 || $status_code === 403 ||
9321 - stripos($error_message, 'auth') !== false) {
9322 - return [
9323 - 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
9324 - 'error_code' => 'xai_auth_error',
9325 - 'provider' => 'xai'
9326 - ];
9327 - }
9328 -
9329 - // Model errors
9330 - if (stripos($error_message, 'model') !== false) {
9331 - return [
9332 - 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
9333 - 'error_code' => 'xai_invalid_model',
9334 - 'provider' => 'xai'
9335 - ];
9336 - }
9337 -
9338 - // Rate limit errors
9339 - if ($status_code === 429 ||
9340 - stripos($error_message, 'rate') !== false ||
9341 - stripos($error_message, 'limit') !== false) {
9342 - return [
9343 - 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
9344 - 'error_code' => 'xai_rate_limit',
9345 - 'provider' => 'xai'
9346 - ];
9347 - }
9348 -
9349 - // Quota errors
9350 - if (stripos($error_message, 'quota') !== false ||
9351 - stripos($error_message, 'billing') !== false) {
9352 - return [
9353 - 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
9354 - 'error_code' => 'xai_quota_exceeded',
9355 - 'provider' => 'xai'
9356 - ];
9357 - }
9358 -
9359 - // Server errors
9360 - if ($status_code >= 500) {
9361 - return [
9362 - 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
9363 - 'error_code' => 'xai_service_unavailable',
9364 - 'provider' => 'xai'
9365 - ];
9366 - }
9367 -
9368 - // Generic error fallback with the actual error message
9369 - return [
9370 - 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
9371 - 'error_code' => 'xai_api_error',
9372 - 'provider' => 'xai',
9373 - 'status_code' => $status_code
9374 - ];
9375 - }
9376 -
9377 - $response_body = wp_remote_retrieve_body($response);
9378 - $decoded_response = json_decode($response_body, true);
9379 -
9380 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9381 - return trim($decoded_response['choices'][0]['message']['content']);
9382 - } else {
9383 - //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
9384 - return [
9385 - 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
9386 - 'error_code' => 'xai_response_format_error',
9387 - 'provider' => 'xai'
9388 - ];
9389 - }
9390 -} catch (Exception $e) {
9391 - //error_log('X.AI Exception: ' . $e->getMessage());
9392 - return [
9393 - 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
9394 - 'error_code' => 'xai_exception',
9395 - 'provider' => 'xai'
9396 - ];
9397 -}
9398 -
9399 -
9400 -}
9401 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
9402 - try {
9403 - // Ensure conversation_history is an array
9404 - if (!is_array($conversation_history)) {
9405 - $conversation_history = array();
9406 - }
9407 -
9408 - // Get bot ID from session or request
9409 - $bot_id = $this->get_current_bot_id($session_id);
9410 -
9411 - // Get system prompt instructions using centralized function
9412 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9413 -
9414 - // Create a new array for the formatted conversation
9415 - $formatted_conversation = array();
9416 -
9417 - // Add system message first
9418 - $formatted_conversation[] = array(
9419 - 'role' => 'system',
9420 - 'content' => $system_prompt_instructions . " " . $relevant_content
9421 - );
9422 -
9423 - // Add the rest of the conversation history
9424 - foreach ($conversation_history as $message) {
9425 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9426 - $role = $message['role'];
9427 -
9428 - // Convert roles to supported format
9429 - if ($role === 'bot' || $role === 'agent') {
9430 - $role = 'assistant';
9431 - }
9432 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9433 - $role = 'user';
9434 - }
9435 -
9436 - $formatted_conversation[] = array(
9437 - 'role' => $role,
9438 - 'content' => $message['content']
9439 - );
9440 - }
9441 - }
9442 -
9443 - $body = json_encode([
9444 - 'model' => $selected_model,
9445 - 'messages' => $formatted_conversation,
9446 - 'temperature' => 0.8,
9447 - 'stream' => false
9448 - ]);
9449 -
9450 - $args = [
9451 - 'body' => $body,
9452 - 'headers' => [
9453 - 'Content-Type' => 'application/json',
9454 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
9455 - ],
9456 - 'timeout' => 60,
9457 - 'redirection' => 5,
9458 - 'blocking' => true,
9459 - 'httpversion' => '1.0',
9460 - 'sslverify' => true,
9461 - ];
9462 -
9463 - $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
9464 -
9465 - if (is_wp_error($response)) {
9466 - $error_message = $response->get_error_message();
9467 - //error_log('DeepSeek API Error: ' . $error_message);
9468 - return [
9469 - 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
9470 - 'error_code' => 'deepseek_connection_error',
9471 - 'provider' => 'deepseek'
9472 - ];
9473 - }
9474 -
9475 - $status_code = wp_remote_retrieve_response_code($response);
9476 - if ($status_code !== 200) {
9477 - $response_body = wp_remote_retrieve_body($response);
9478 - $decoded_response = json_decode($response_body, true);
9479 -
9480 - $error_message = isset($decoded_response['error']['message'])
9481 - ? $decoded_response['error']['message']
9482 - : 'HTTP Error ' . $status_code;
9483 -
9484 - $error_type = isset($decoded_response['error']['type'])
9485 - ? $decoded_response['error']['type']
9486 - : 'unknown';
9487 -
9488 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
9489 -
9490 - // Handle specific error types
9491 - switch ($status_code) {
9492 - case 401:
9493 - return [
9494 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
9495 - 'error_code' => 'deepseek_auth_error',
9496 - 'provider' => 'deepseek'
9497 - ];
9498 -
9499 - case 400:
9500 - if (strpos($error_message, 'API key') !== false) {
9501 - return [
9502 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
9503 - 'error_code' => 'deepseek_invalid_api_key',
9504 - 'provider' => 'deepseek'
9505 - ];
9506 - }
9507 - break;
9508 -
9509 - case 429:
9510 - if (strpos($error_message, 'quota') !== false) {
9511 - return [
9512 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
9513 - 'error_code' => 'deepseek_quota_exceeded',
9514 - 'provider' => 'deepseek'
9515 - ];
9516 - } else {
9517 - return [
9518 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
9519 - 'error_code' => 'deepseek_rate_limit',
9520 - 'provider' => 'deepseek'
9521 - ];
9522 - }
9523 -
9524 - case 500:
9525 - case 502:
9526 - case 503:
9527 - case 504:
9528 - return [
9529 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
9530 - 'error_code' => 'deepseek_service_unavailable',
9531 - 'provider' => 'deepseek'
9532 - ];
9533 - }
9534 -
9535 - // Generic error fallback
9536 - return [
9537 - 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
9538 - 'error_code' => 'deepseek_api_error',
9539 - 'provider' => 'deepseek',
9540 - 'status_code' => $status_code
9541 - ];
9542 - }
9543 -
9544 - $response_body = wp_remote_retrieve_body($response);
9545 - $decoded_response = json_decode($response_body, true);
9546 -
9547 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9548 - return trim($decoded_response['choices'][0]['message']['content']);
9549 - } else {
9550 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
9551 - return [
9552 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
9553 - 'error_code' => 'deepseek_response_format_error',
9554 - 'provider' => 'deepseek'
9555 - ];
9556 - }
9557 - } catch (Exception $e) {
9558 - //error_log('DeepSeek Exception: ' . $e->getMessage());
9559 - return [
9560 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
9561 - 'error_code' => 'deepseek_exception',
9562 - 'provider' => 'deepseek'
9563 - ];
9564 - }
9565 -}
9566 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
9567 - // Get bot ID from session or request
9568 - $bot_id = $this->get_current_bot_id($session_id);
9569 -
9570 - // Get system prompt instructions using centralized function
9571 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9572 -
9573 - // Add system prompt to relevant content
9574 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9575 -
9576 - // Format messages for Gemini API
9577 - $formatted_messages = [];
9578 -
9579 - // Add system message as the first user message with role prefix
9580 - // Note: Gemini doesn't have a dedicated system role, so we use a prefixed user message
9581 - $formatted_messages[] = [
9582 - 'role' => 'user',
9583 - 'parts' => [
9584 - ['text' => "[System Instructions] " . $content_with_instructions]
9585 - ]
9586 - ];
9587 -
9588 - // Add model response to acknowledge system instructions
9589 - $formatted_messages[] = [
9590 - 'role' => 'model',
9591 - 'parts' => [
9592 - ['text' => "I understand and will follow these instructions."]
9593 - ]
9594 - ];
9595 -
9596 - // Process the rest of the conversation history
9597 - $current_role = null;
9598 - $current_parts = [];
9599 -
9600 - foreach ($conversation_history as $message) {
9601 - // Skip the first system message as we already handled it
9602 - if ($message['role'] === 'system') {
9603 - continue;
9604 - }
9605 -
9606 - // Map roles to Gemini format
9607 - $gemini_role = '';
9608 - if ($message['role'] === 'user') {
9609 - $gemini_role = 'user';
9610 - } else if (in_array($message['role'], ['assistant', 'bot', 'agent'])) {
9611 - $gemini_role = 'model';
9612 - } else {
9613 - // Skip unsupported roles
9614 - continue;
9615 - }
9616 -
9617 - // If we have a new role, add the previous message
9618 - if ($current_role !== null && $current_role !== $gemini_role && !empty($current_parts)) {
9619 - $formatted_messages[] = [
9620 - 'role' => $current_role,
9621 - 'parts' => $current_parts
9622 - ];
9623 - $current_parts = [];
9624 - }
9625 -
9626 - // Set current role and add text to parts
9627 - $current_role = $gemini_role;
9628 - $current_parts[] = ['text' => $message['content']];
9629 - }
9630 -
9631 - // Add the last message if there's content
9632 - if ($current_role !== null && !empty($current_parts)) {
9633 - $formatted_messages[] = [
9634 - 'role' => $current_role,
9635 - 'parts' => $current_parts
9636 - ];
9637 - }
9638 -
9639 - // Build the request body
9640 - $body = json_encode([
9641 - 'contents' => $formatted_messages,
9642 - 'generationConfig' => [
9643 - 'temperature' => 0.7,
9644 - 'topP' => 0.95,
9645 - 'topK' => 40,
9646 - 'maxOutputTokens' => 8192,
9647 - ],
9648 - 'safetySettings' => [
9649 - [
9650 - 'category' => 'HARM_CATEGORY_HARASSMENT',
9651 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
9652 - ],
9653 - [
9654 - 'category' => 'HARM_CATEGORY_HATE_SPEECH',
9655 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
9656 - ],
9657 - [
9658 - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
9659 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
9660 - ],
9661 - [
9662 - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
9663 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
9664 - ]
9665 - ]
9666 - ]);
9667 -
9668 - // Prepare the API endpoint
9669 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
9670 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
9671 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
9672 -
9673 - // Set up the API request
9674 - $args = [
9675 - 'body' => $body,
9676 - 'headers' => [
9677 - 'Content-Type' => 'application/json',
9678 - ],
9679 - 'timeout' => 60,
9680 - 'redirection' => 5,
9681 - 'blocking' => true,
9682 - 'httpversion' => '1.0',
9683 - 'sslverify' => true,
9684 - ];
9685 -
9686 - // Make the API request
9687 - $response = wp_remote_post($api_endpoint, $args);
9688 -
9689 - // Process the response
9690 - if (is_wp_error($response)) {
9691 - return "Sorry, there was an error processing your request: " . $response->get_error_message();
9692 - }
9693 -
9694 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
9695 -
9696 - // Handle potential errors in the response
9697 - if (isset($response_body['error'])) {
9698 - //error_log('Gemini API Error: ' . json_encode($response_body['error']));
9699 - return "Sorry, there was an error with the Gemini API: " .
9700 - (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
9701 - }
9702 -
9703 - // Extract the response text
9704 - if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
9705 - return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
9706 - } else {
9707 - //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
9708 - return "Sorry, I couldn't process that request. The response format was unexpected.";
9709 - }
9710 -}
9711 -
9712 -
9713 -public function test_streaming_request() {
9714 - $options = get_option('mxchat_options', []);
9715 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
9716 -
9717 - // Detect provider from model prefix
9718 - $provider = strtolower(explode('-', $model)[0]);
9719 -
9720 - $sample_prompt = 'Hello! Can you stream this response back to me?';
9721 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
9722 - $headers = [];
9723 - $body = [];
9724 - $url = '';
9725 - $api_key = '';
9726 -
9727 - switch ($provider) {
9728 - case 'gpt':
9729 - case 'o1':
9730 - $api_key = $options['api_key'] ?? '';
9731 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
9732 - $url = 'https://api.openai.com/v1/chat/completions';
9733 - $headers = [
9734 - 'Content-Type: application/json',
9735 - 'Authorization: Bearer ' . $api_key
9736 - ];
9737 - $body = [
9738 - 'model' => $model,
9739 - 'messages' => $messages,
9740 - 'stream' => true
9741 - ];
9742 - break;
9743 -
9744 - case 'claude':
9745 - $api_key = $options['claude_api_key'] ?? '';
9746 - if (empty($api_key)) return '❌ Missing API key for Claude';
9747 - $url = 'https://api.anthropic.com/v1/messages';
9748 - $headers = [
9749 - 'Content-Type: application/json',
9750 - 'x-api-key: ' . $api_key,
9751 - 'anthropic-version: 2023-06-01'
9752 - ];
9753 - $body = [
9754 - 'model' => $model,
9755 - 'messages' => $messages,
9756 - 'max_tokens' => 100,
9757 - 'stream' => true
9758 - ];
9759 - break;
9760 -
9761 - case 'grok':
9762 - $api_key = $options['xai_api_key'] ?? '';
9763 - if (empty($api_key)) return '❌ Missing API key for X.AI';
9764 - $url = 'https://api.x.ai/v1/chat/completions';
9765 - $headers = [
9766 - 'Content-Type: application/json',
9767 - 'Authorization: Bearer ' . $api_key
9768 - ];
9769 - $body = [
9770 - 'model' => $model,
9771 - 'messages' => $messages,
9772 - 'stream' => true
9773 - ];
9774 - break;
9775 -
9776 - case 'deepseek':
9777 - if (empty($deepseek_api_key)) {
9778 - $error_response = [
9779 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
9780 - 'error_code' => 'missing_deepseek_api_key'
9781 - ];
9782 - if ($testing_data !== null) {
9783 - $error_response['testing_data'] = $testing_data;
9784 - }
9785 - return $error_response;
9786 - }
9787 - if ($streaming) {
9788 - return $this->mxchat_generate_response_deepseek_stream(
9789 - $selected_model,
9790 - $deepseek_api_key,
9791 - $conversation_history,
9792 - $relevant_content,
9793 - $session_id,
9794 - $testing_data // Pass testing data
9795 - );
9796 - } else {
9797 - $response = $this->mxchat_generate_response_deepseek(
9798 - $selected_model,
9799 - $deepseek_api_key,
9800 - $conversation_history,
9801 - $relevant_content
9802 - );
9803 - }
9804 - break;
9805 -
9806 - case 'gemini':
9807 - $api_key = $options['gemini_api_key'] ?? '';
9808 - if (empty($api_key)) return '❌ Missing API key for Gemini';
9809 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
9810 - $headers = ['Content-Type: application/json'];
9811 - $body = [
9812 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
9813 - 'generationConfig' => ['temperature' => 0.7]
9814 - ];
9815 - break;
9816 -
9817 - default:
9818 - return '❌ Unsupported provider: ' . $provider;
9819 - }
9820 -
9821 - // Do the actual streaming test
9822 - $ch = curl_init($url);
9823 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
9824 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
9825 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
9826 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
9827 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9828 -
9829 - $response = curl_exec($ch);
9830 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9831 - $error = curl_error($ch);
9832 - curl_close($ch);
9833 -
9834 - if ($error) return "❌ cURL error: $error";
9835 - if ($http_code !== 200) {
9836 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
9837 - return "❌ HTTP $http_code: $error_message";
9838 - }
9839 -
9840 - return true;
9841 -}
9842 -
9843 -public function mxchat_dismiss_pre_chat_message() {
9844 - // Get and sanitize the user identifier
9845 - $user_id = $this->mxchat_get_user_identifier();
9846 - $user_id = sanitize_key($user_id);
9847 -
9848 - // Set a transient to track that the user has dismissed the pre-chat message
9849 - $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
9850 - set_transient($transient_key, true, DAY_IN_SECONDS);
9851 -
9852 - wp_send_json_success();
9853 -}
9854 -
9855 -public function mxchat_check_pre_chat_message_status() {
9856 - // Get and sanitize the user identifier
9857 - $user_id = $this->mxchat_get_user_identifier();
9858 - $user_id = sanitize_key($user_id);
9859 -
9860 - // Check if the transient exists (i.e., if the message was dismissed)
9861 - $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
9862 - $dismissed = get_transient($transient_key);
9863 -
9864 - // Log the result to see if it's being set correctly
9865 - //error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
9866 -
9867 - if ($dismissed) {
9868 - wp_send_json_success(['dismissed' => true]);
9869 - } else {
9870 - wp_send_json_success(['dismissed' => false]);
9871 - }
9872 -
9873 - wp_die();
9874 -}
9875 -
9876 -private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
9877 - if (!is_array($vectorA) || !is_array($vectorB) || empty($vectorA) || empty($vectorB)) {
9878 - return 0;
9879 - }
9880 -
9881 - $dotProduct = array_sum(array_map(function ($a, $b) {
9882 - return $a * $b;
9883 - }, $vectorA, $vectorB));
9884 - $normA = sqrt(array_sum(array_map(function ($a) {
9885 - return $a * $a;
9886 - }, $vectorA)));
9887 - $normB = sqrt(array_sum(array_map(function ($b) {
9888 - return $b * $b;
9889 - }, $vectorB)));
9890 -
9891 - if ($normA == 0 || $normB == 0) {
9892 - return 0;
9893 - }
9894 -
9895 - return $dotProduct / ($normA * $normB);
9896 - }
9897 -
9898 -
9899 -public function mxchat_enqueue_scripts_styles() {
9900 - // Fetch options from the database first to check loading strategy
9901 - $this->options = get_option('mxchat_options');
9902 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
9903 -
9904 - // Always enqueue CSS immediately
9905 - wp_enqueue_style(
9906 - 'mxchat-chat-css',
9907 - plugin_dir_url(__FILE__) . '../css/chat-style.css',
9908 - array(),
9909 - MXCHAT_VERSION
9910 - );
9911 -
9912 - // Handle script loading based on strategy
9913 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
9914 - // Enqueue the script normally
9915 - wp_enqueue_script(
9916 - 'mxchat-chat-js',
9917 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
9918 - array('jquery'),
9919 - MXCHAT_VERSION,
9920 - true
9921 - );
9922 -
9923 - // Add defer attribute if strategy is 'defer'
9924 - if ($loading_strategy === 'defer') {
9925 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
9926 - }
9927 - } else {
9928 - // For delay or interaction-based loading, we'll use a custom loader
9929 - // Don't enqueue the main script - we'll load it dynamically
9930 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
9931 - }
9932 -
9933 - $prompts_options = get_option('mxchat_prompts_options', array());
9934 -
9935 - // Check if AI theme is active - if so, skip inline colors in JavaScript
9936 - $theme_options = get_option('mxchat_theme_options', array());
9937 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
9938 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
9939 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
9940 -
9941 - // Prepare settings for JavaScript
9942 - $style_settings = array(
9943 - 'ajax_url' => admin_url('admin-ajax.php'),
9944 - 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
9945 - 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest',
9946 - 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
9947 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
9948 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
9949 - 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
9950 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
9951 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
9952 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
9953 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
9954 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
9955 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
9956 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
9957 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
9958 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
9959 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
9960 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
9961 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
9962 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
9963 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
9964 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
9965 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
9966 - 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
9967 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
9968 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
9969 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
9970 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
9971 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
9972 - 'initial_email_state' => null, // Also fixed this undefined variable
9973 - 'skip_email_check' => true,
9974 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
9975 - 'skip_inline_colors' => $skip_inline_colors,
9976 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
9977 - 'print_button_enabled' => $this->options['print_button_enabled'] ?? 'on',
9978 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
9979 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
9980 - 'satisfaction_rating_enabled' => apply_filters(
9981 - 'mxchat_satisfaction_rating_enabled',
9982 - ($this->options['satisfaction_rating_enabled'] ?? 'off') === 'on'
9983 - ),
9984 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($this->options['satisfaction_rating_idle_seconds'] ?? 60))),
9985 - 'satisfaction_rating_copy' => array(
9986 - 'question' => !empty($this->options['satisfaction_rating_question']) ? esc_html($this->options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
9987 - 'helpful' => esc_html__('Helpful', 'mxchat'),
9988 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
9989 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
9990 - 'thanks' => !empty($this->options['satisfaction_rating_thanks']) ? esc_html($this->options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
9991 - 'placeholder' => !empty($this->options['satisfaction_rating_placeholder']) ? esc_html($this->options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
9992 - 'send' => esc_html__('Send', 'mxchat'),
9993 - 'skip' => esc_html__('Skip', 'mxchat'),
9994 - 'saved' => !empty($this->options['satisfaction_rating_saved']) ? esc_html($this->options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
9995 - ),
9996 - );
9997 -
9998 - // For normal/defer loading, use wp_localize_script
9999 - // For delayed loading, we store settings in a transient to be output inline
10000 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10001 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10002 - } else {
10003 - // Store settings for the delayed loader to use
10004 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
10005 - }
10006 -}
10007 -
10008 -/**
10009 - * Output the delayed script loader for performance optimization
10010 - */
10011 -public function mxchat_output_delayed_script_loader() {
10012 - $this->options = get_option('mxchat_options');
10013 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10014 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
10015 -
10016 - // Get the stored settings
10017 - $prompts_options = get_option('mxchat_prompts_options', array());
10018 - $theme_options = get_option('mxchat_theme_options', array());
10019 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10020 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10021 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10022 -
10023 - $style_settings = array(
10024 - 'ajax_url' => admin_url('admin-ajax.php'),
10025 - 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
10026 - 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest',
10027 - 'enable_streaming_toggle' => isset($this->options['enable_streaming_toggle']) ? $this->options['enable_streaming_toggle'] : 'on',
10028 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10029 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10030 - 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
10031 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10032 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10033 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10034 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10035 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10036 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10037 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10038 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10039 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10040 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10041 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10042 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10043 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10044 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10045 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10046 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10047 - 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
10048 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10049 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10050 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10051 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10052 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
10053 - 'initial_email_state' => null,
10054 - 'skip_email_check' => true,
10055 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10056 - 'skip_inline_colors' => $skip_inline_colors,
10057 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10058 - 'print_button_enabled' => $this->options['print_button_enabled'] ?? 'on',
10059 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
10060 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
10061 - 'satisfaction_rating_enabled' => apply_filters(
10062 - 'mxchat_satisfaction_rating_enabled',
10063 - ($this->options['satisfaction_rating_enabled'] ?? 'off') === 'on'
10064 - ),
10065 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($this->options['satisfaction_rating_idle_seconds'] ?? 60))),
10066 - 'satisfaction_rating_copy' => array(
10067 - 'question' => !empty($this->options['satisfaction_rating_question']) ? esc_html($this->options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
10068 - 'helpful' => esc_html__('Helpful', 'mxchat'),
10069 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
10070 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
10071 - 'thanks' => !empty($this->options['satisfaction_rating_thanks']) ? esc_html($this->options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
10072 - 'placeholder' => !empty($this->options['satisfaction_rating_placeholder']) ? esc_html($this->options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
10073 - 'send' => esc_html__('Send', 'mxchat'),
10074 - 'skip' => esc_html__('Skip', 'mxchat'),
10075 - 'saved' => !empty($this->options['satisfaction_rating_saved']) ? esc_html($this->options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
10076 - ),
10077 - );
10078 -
10079 - // Determine delay time based on strategy
10080 - $delay_ms = 0;
10081 - switch ($loading_strategy) {
10082 - case 'delay_1s':
10083 - $delay_ms = 1000;
10084 - break;
10085 - case 'delay_3s':
10086 - $delay_ms = 3000;
10087 - break;
10088 - case 'delay_5s':
10089 - $delay_ms = 5000;
10090 - break;
10091 - }
10092 -
10093 - ?>
10094 - <script type="text/javascript">
10095 - (function() {
10096 - var mxchatLoaded = false;
10097 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
10098 - window.mxchatChat = mxchatChat;
10099 -
10100 - function loadMxChatScript() {
10101 - if (mxchatLoaded) return;
10102 - mxchatLoaded = true;
10103 -
10104 - function appendChatScript() {
10105 - var script = document.createElement('script');
10106 - script.src = <?php echo wp_json_encode($script_url); ?>;
10107 - script.type = 'text/javascript';
10108 - document.body.appendChild(script);
10109 - }
10110 -
10111 - if (typeof jQuery !== 'undefined') {
10112 - appendChatScript();
10113 - } else {
10114 - var jq = document.createElement('script');
10115 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
10116 - jq.onload = appendChatScript;
10117 - document.body.appendChild(jq);
10118 - }
10119 - }
10120 -
10121 - <?php if ($loading_strategy === 'on_interaction'): ?>
10122 - // Load on user interaction
10123 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
10124 - events.forEach(function(evt) {
10125 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
10126 - });
10127 - // Fallback: load after 8 seconds if no interaction
10128 - setTimeout(loadMxChatScript, 8000);
10129 - <?php else: ?>
10130 - // Load after specified delay
10131 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
10132 - <?php endif; ?>
10133 - })();
10134 - </script>
10135 - <?php
10136 -}
10137 -
10138 -/**
10139 - * Setup the cron jobs for rate limits with guard against multiple calls
10140 - */
10141 -public function setup_rate_limit_cron_jobs() {
10142 - // Add a guard to prevent multiple rapid calls
10143 - $last_setup = get_transient('mxchat_cron_setup_guard');
10144 - if ($last_setup && (time() - $last_setup) < 60) {
10145 - // Don't run again if we ran less than 60 seconds ago
10146 - return;
10147 - }
10148 -
10149 - // Set the guard
10150 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
10151 -
10152 - try {
10153 - // First, check if WordPress cron is disabled
10154 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
10155 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
10156 - $this->setup_fallback_rate_limit_system();
10157 - return;
10158 - }
10159 -
10160 - // Check if cron is already scheduled - if so, don't mess with it
10161 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
10162 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
10163 - return;
10164 - }
10165 -
10166 - // Clear any orphaned hooks (but don't loop indefinitely)
10167 - $hooks_to_clear = [
10168 - 'mxchat_reset_rate_limits',
10169 - 'mxchat_reset_hourly_rate_limits',
10170 - 'mxchat_reset_daily_rate_limits',
10171 - 'mxchat_reset_weekly_rate_limits',
10172 - 'mxchat_reset_monthly_rate_limits'
10173 - ];
10174 -
10175 - foreach ($hooks_to_clear as $hook) {
10176 - // Only clear a maximum of 3 instances to prevent infinite loops
10177 - $cleared = 0;
10178 - while (wp_next_scheduled($hook) && $cleared < 3) {
10179 - wp_clear_scheduled_hook($hook);
10180 - $cleared++;
10181 - }
10182 - }
10183 -
10184 - // Small delay after clearing
10185 - usleep(100000); // 0.1 seconds
10186 -
10187 - // Try to schedule the event
10188 - $initial_time = time() + 300; // Start in 5 minutes
10189 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
10190 -
10191 - if ($result === false) {
10192 - //error_log('MxChat: Failed to schedule cron, using fallback system');
10193 - $this->setup_fallback_rate_limit_system();
10194 - } else {
10195 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
10196 - }
10197 -
10198 - } catch (Exception $e) {
10199 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
10200 - $this->setup_fallback_rate_limit_system();
10201 - }
10202 -}
10203 -
10204 -/**
10205 - * Try alternative cron scheduling methods
10206 - */
10207 -private function try_alternative_cron_scheduling($initial_time) {
10208 - try {
10209 - // Method 1: Try with current time instead of future time
10210 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
10211 - if ($result1 !== false) {
10212 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
10213 - return true;
10214 - }
10215 -
10216 - // Method 2: Try with a different interval
10217 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
10218 - if ($result2 !== false) {
10219 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
10220 - return true;
10221 - }
10222 -
10223 - // Method 3: Try wp_schedule_single_event first, then recurring
10224 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
10225 - if ($result3 !== false) {
10226 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
10227 - // Schedule the next one manually in the handler
10228 - return true;
10229 - }
10230 -
10231 - return false;
10232 -
10233 - } catch (Exception $e) {
10234 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
10235 - return false;
10236 - }
10237 -}
10238 -
10239 -/**
10240 - * Enhanced fallback rate limit system
10241 - */
10242 -private function setup_fallback_rate_limit_system() {
10243 - // Set a flag to use database-based rate limit cleanup
10244 - update_option('mxchat_use_fallback_rate_limits', true);
10245 -
10246 - // Schedule a one-time check to happen on the next plugin load
10247 - update_option('mxchat_next_rate_limit_check', time() + 3600);
10248 -
10249 - // Also set up a more frequent fallback check (every 4 hours)
10250 - update_option('mxchat_fallback_check_interval', 4 * 3600);
10251 -
10252 - //error_log('MxChat: Fallback rate limit system activated');
10253 -}
10254 -
10255 -/**
10256 - * Enhanced fallback check method
10257 - */
10258 -public function check_fallback_rate_limits() {
10259 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10260 -
10261 - if (!$use_fallback) {
10262 - return; // Regular cron is working
10263 - }
10264 -
10265 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10266 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
10267 -
10268 - if (time() >= $next_check) {
10269 - //error_log('MxChat: Running fallback rate limit cleanup');
10270 - $this->mxchat_reset_rate_limits();
10271 -
10272 - // Schedule next check
10273 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
10274 - }
10275 -}
10276 -/**
10277 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
10278 - */
10279 -public function check_rate_limit() {
10280 - // Check if we need to run fallback cleanup
10281 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10282 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10283 -
10284 - if ($use_fallback && time() >= $next_check) {
10285 - $this->mxchat_reset_rate_limits();
10286 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
10287 - }
10288 -
10289 - // Get bot ID from current request context
10290 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10291 -
10292 - // Get bot-specific options (includes rate limits if overridden)
10293 - $bot_options = $this->get_bot_options($bot_id);
10294 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
10295 -
10296 - // Use bot-specific rate limits if available, otherwise fall back to default
10297 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
10298 -
10299 - // Determine user role or if logged out
10300 - if (is_user_logged_in()) {
10301 - $user = wp_get_current_user();
10302 - $user_id = $user->ID;
10303 -
10304 - // Get the user's primary role using reset() to safely get the first element
10305 - $user_roles = $user->roles;
10306 -
10307 - // Safely get the first role regardless of array key structure
10308 - if (!empty($user_roles) && is_array($user_roles)) {
10309 - $role = reset($user_roles); // This safely gets the first element regardless of key
10310 - } else {
10311 - $role = 'subscriber'; // Default to subscriber if no role found
10312 - }
10313 - } else {
10314 - $role = 'logged_out';
10315 - // Use IP address for non-logged-in users
10316 - $user_id = $this->get_client_ip();
10317 - }
10318 -
10319 - // Check if rate limits are configured for this role
10320 - if (!isset($rate_limits_source[$role])) {
10321 - return true; // No limit set for this role
10322 - }
10323 -
10324 - $limit = $rate_limits_source[$role]['limit'];
10325 -
10326 - // If unlimited, return true immediately
10327 - if ($limit === 'unlimited') {
10328 - return true;
10329 - }
10330 -
10331 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
10332 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
10333 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
10334 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
10335 -
10336 - // Include bot_id in option name so each bot has separate rate limits
10337 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
10338 -
10339 - // Get the counter data
10340 - $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
10341 -
10342 - // If first request or counter reset needed, set the initial timestamp
10343 - if ($limit_data['count'] === 0) {
10344 - $limit_data['timestamp'] = time();
10345 - update_option($option_name, $limit_data);
10346 - }
10347 -
10348 - // Get the timeframe
10349 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
10350 - $rate_limits_source[$role]['timeframe'] : 'daily';
10351 -
10352 - // Check if the counter needs to be reset based on timeframe
10353 - $current_time = time();
10354 - $timestamp = $limit_data['timestamp'];
10355 - $should_reset = false;
10356 -
10357 - switch ($timeframe) {
10358 - case 'hourly':
10359 - $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
10360 - break;
10361 - case 'daily':
10362 - $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
10363 - break;
10364 - case 'weekly':
10365 - $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
10366 - break;
10367 - case 'monthly':
10368 - $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
10369 - break;
10370 - }
10371 -
10372 - // Reset the counter if the timeframe has passed
10373 - if ($should_reset) {
10374 - $limit_data = ['count' => 0, 'timestamp' => $current_time];
10375 - update_option($option_name, $limit_data);
10376 - }
10377 -
10378 - // Check if user has exceeded their limit
10379 - if ($limit_data['count'] >= intval($limit)) {
10380 - // Get the custom message for this role
10381 - $message = !empty($rate_limits_source[$role]['message'])
10382 - ? $rate_limits_source[$role]['message']
10383 - : __('Rate limit exceeded. Please try again later.', 'mxchat');
10384 -
10385 - // Add timeframe information to the message if placeholders exist
10386 - $timeframe_label = '';
10387 - switch ($timeframe) {
10388 - case 'hourly':
10389 - $timeframe_label = __('hour', 'mxchat');
10390 - break;
10391 - case 'daily':
10392 - $timeframe_label = __('day', 'mxchat');
10393 - break;
10394 - case 'weekly':
10395 - $timeframe_label = __('week', 'mxchat');
10396 - break;
10397 - case 'monthly':
10398 - $timeframe_label = __('month', 'mxchat');
10399 - break;
10400 - }
10401 -
10402 - // Replace placeholders in the message
10403 - $message = str_replace(
10404 - ['{limit}', '{count}', '{remaining}', '{timeframe}'],
10405 - [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
10406 - $message
10407 - );
10408 -
10409 - // Process HTML links in the message
10410 - $message = $this->process_rate_limit_message_html($message);
10411 -
10412 - // Return error with the processed message
10413 - return [
10414 - 'error' => true,
10415 - 'message' => $message
10416 - ];
10417 - }
10418 -
10419 - // Increment the counter
10420 - $limit_data['count']++;
10421 - update_option($option_name, $limit_data);
10422 -
10423 - return true;
10424 -}
10425 -
10426 -/**
10427 - * Enhanced rate limit reset with better error handling
10428 - */
10429 -public function mxchat_reset_rate_limits() {
10430 - try {
10431 - global $wpdb;
10432 - $all_options = get_option('mxchat_options', []);
10433 - $current_time = time();
10434 -
10435 - // Get rate limit options with a safer query and limit
10436 - $option_names = $wpdb->get_col(
10437 - $wpdb->prepare(
10438 - "SELECT option_name FROM {$wpdb->options}
10439 - WHERE option_name LIKE %s
10440 - LIMIT 1000",
10441 - 'mxchat_chat_limit_%'
10442 - )
10443 - );
10444 -
10445 - if (empty($option_names)) {
10446 - return;
10447 - }
10448 -
10449 - $processed_count = 0;
10450 - $max_processing_time = 30; // Maximum 30 seconds
10451 - $start_time = time();
10452 -
10453 - foreach ($option_names as $option_name) {
10454 - // Check processing time limit
10455 - if ((time() - $start_time) > $max_processing_time) {
10456 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
10457 - break;
10458 - }
10459 -
10460 - // Parse the option name more safely
10461 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
10462 - continue;
10463 - }
10464 -
10465 - $role_and_user = $matches[1] . '_' . $matches[2];
10466 - $parts = explode('_', $role_and_user);
10467 -
10468 - if (count($parts) < 2) {
10469 - continue;
10470 - }
10471 -
10472 - // Extract role (everything except the last part which is user ID)
10473 - $user_id_part = array_pop($parts);
10474 - $role = implode('_', $parts);
10475 -
10476 - // Skip if role doesn't exist in our settings
10477 - if (!isset($all_options['rate_limits'][$role])) {
10478 - // Clean up orphaned entries
10479 - delete_option($option_name);
10480 - continue;
10481 - }
10482 -
10483 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
10484 - $limit_data = get_option($option_name);
10485 -
10486 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
10487 - // Clean up invalid entries
10488 - delete_option($option_name);
10489 - continue;
10490 - }
10491 -
10492 - $timestamp = $limit_data['timestamp'];
10493 - $should_reset = false;
10494 -
10495 - // Determine if we should reset based on the timeframe
10496 - switch ($timeframe) {
10497 - case 'hourly':
10498 - $should_reset = ($current_time - $timestamp) >= 3600;
10499 - break;
10500 - case 'daily':
10501 - $should_reset = ($current_time - $timestamp) >= 86400;
10502 - break;
10503 - case 'weekly':
10504 - $should_reset = ($current_time - $timestamp) >= 604800;
10505 - break;
10506 - case 'monthly':
10507 - $should_reset = ($current_time - $timestamp) >= 2592000;
10508 - break;
10509 - }
10510 -
10511 - // Reset the counter if the timeframe has passed
10512 - if ($should_reset) {
10513 - delete_option($option_name);
10514 - wp_cache_delete($option_name, 'options');
10515 - $processed_count++;
10516 - }
10517 - }
10518 -
10519 - // Clean up any orphaned cache entries
10520 - wp_cache_delete('mxchat_all_chat_limits', 'options');
10521 -
10522 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
10523 -
10524 - } catch (Exception $e) {
10525 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
10526 - }
10527 -}
10528 -
10529 -
10530 -/**
10531 - * Process HTML links in rate limit messages
10532 - *
10533 - * @param string $message The rate limit message
10534 - * @return string The processed message with safe HTML links
10535 - */
10536 -private function process_rate_limit_message_html($message) {
10537 - // Return original message if empty
10538 - if (empty($message)) {
10539 - return $message;
10540 - }
10541 -
10542 - // First, convert markdown links to HTML
10543 - $message = $this->convert_markdown_links($message);
10544 -
10545 - // Then, auto-convert any remaining plain URLs to links
10546 - $message = $this->auto_link_urls($message);
10547 -
10548 - // Allow basic HTML tags for links and formatting
10549 - $allowed_tags = [
10550 - 'a' => [
10551 - 'href' => true,
10552 - 'target' => true,
10553 - 'rel' => true,
10554 - 'title' => true,
10555 - 'class' => true
10556 - ],
10557 - 'strong' => [],
10558 - 'em' => [],
10559 - 'br' => [],
10560 - 'b' => [],
10561 - 'i' => [],
10562 - 'span' => ['class' => true]
10563 - ];
10564 -
10565 - // Sanitize but allow the specified HTML tags
10566 - $processed_message = wp_kses($message, $allowed_tags);
10567 -
10568 - // If wp_kses stripped everything, return the original message as plain text
10569 - if (empty($processed_message) && !empty($message)) {
10570 - // Strip all HTML and return plain text as fallback
10571 - return wp_strip_all_tags($message);
10572 - }
10573 -
10574 - return $processed_message;
10575 -}
10576 -
10577 -/**
10578 - * Convert markdown links to HTML
10579 - *
10580 - * @param string $text The text to process
10581 - * @return string The text with markdown links converted to HTML
10582 - */
10583 -private function convert_markdown_links($text) {
10584 - // Return original text if empty
10585 - if (empty($text)) {
10586 - return $text;
10587 - }
10588 -
10589 - // Pattern to match markdown links: [text](url)
10590 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
10591 -
10592 - $processed_text = preg_replace_callback($pattern, function($matches) {
10593 - $link_text = $matches[1];
10594 - $url = $matches[2];
10595 -
10596 - // Clean up any trailing punctuation from the URL
10597 - $url = rtrim($url, '.,;:!?');
10598 -
10599 - // Sanitize the link text and URL
10600 - $safe_text = esc_html($link_text);
10601 - $safe_url = esc_url($url);
10602 -
10603 - // Create the HTML link
10604 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
10605 - }, $text);
10606 -
10607 - // If preg_replace_callback failed, return original text
10608 - if ($processed_text === null) {
10609 - return $text;
10610 - }
10611 -
10612 - return $processed_text;
10613 -}
10614 -
10615 -/**
10616 - * Auto-convert plain URLs to clickable links
10617 - *
10618 - * @param string $text The text to process
10619 - * @return string The text with URLs converted to links
10620 - */
10621 -private function auto_link_urls($text) {
10622 - // Return original text if empty
10623 - if (empty($text)) {
10624 - return $text;
10625 - }
10626 -
10627 - // Simple pattern that avoids complex lookbehinds
10628 - // This will match URLs that are not already inside href attributes or markdown links
10629 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
10630 -
10631 - $processed_text = preg_replace_callback($pattern, function($matches) {
10632 - $url = $matches[0];
10633 - // Clean up any trailing punctuation that might have been captured
10634 - $url = rtrim($url, '.,;:!?');
10635 -
10636 - // Add target="_blank" and rel="noopener noreferrer" for security
10637 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
10638 - }, $text);
10639 -
10640 - // If preg_replace_callback failed, return original text
10641 - if ($processed_text === null) {
10642 - return $text;
10643 - }
10644 -
10645 - return $processed_text;
10646 -}
10647 -
10648 -
10649 -// Helper function to get client IP address
10650 -private function get_client_ip() {
10651 - // Check for shared internet/ISP IP
10652 - if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
10653 - return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
10654 - }
10655 -
10656 - // Check for IPs passing through proxies
10657 - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
10658 - // Use the first value in the comma-separated list
10659 - $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
10660 - return trim($forwarded_for[0]);
10661 - }
10662 -
10663 - if (!empty($_SERVER['REMOTE_ADDR'])) {
10664 - return sanitize_text_field($_SERVER['REMOTE_ADDR']);
10665 - }
10666 -
10667 - // Fallback
10668 - return 'unknown';
10669 -}
10670 -
10671 -/**
10672 - * AJAX handler to get system information for testing panel
10673 - */
10674 -/**
10675 - * AJAX handler to get system information for testing panel
10676 - */
10677 -public function mxchat_get_system_info() {
10678 - // Verify nonce for security
10679 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
10680 - wp_send_json_error(['message' => 'Invalid nonce']);
10681 - return;
10682 - }
10683 -
10684 - // Only allow admin users
10685 - if (!current_user_can('administrator')) {
10686 - wp_send_json_error(['message' => 'Unauthorized']);
10687 - return;
10688 - }
10689 -
10690 - // Get system prompt from options
10691 - $system_prompt = isset($this->options['system_prompt_instructions'])
10692 - ? $this->options['system_prompt_instructions']
10693 - : 'No system prompt configured';
10694 -
10695 - // Get selected model
10696 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
10697 -
10698 - // Check if OpenRouter is being used
10699 - $is_openrouter = ($selected_model === 'openrouter');
10700 - $openrouter_model = '';
10701 -
10702 - if ($is_openrouter) {
10703 - // Get the actual OpenRouter model that's selected
10704 - $openrouter_model = isset($this->options['openrouter_selected_model'])
10705 - ? $this->options['openrouter_selected_model']
10706 - : 'No OpenRouter model selected';
10707 -
10708 - // Update selected_model display to show both
10709 - $selected_model = 'OpenRouter: ' . $openrouter_model;
10710 - }
10711 -
10712 - // Get API key status (just check if they exist, don't expose the keys)
10713 - $api_status = [];
10714 - $api_status['openai'] = !empty($this->options['api_key']);
10715 - $api_status['claude'] = !empty($this->options['claude_api_key']);
10716 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
10717 - $api_status['xai'] = !empty($this->options['xai_api_key']);
10718 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
10719 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
10720 -
10721 - wp_send_json_success([
10722 - 'system_prompt' => $system_prompt,
10723 - 'selected_model' => $selected_model,
10724 - 'is_openrouter' => $is_openrouter,
10725 - 'openrouter_model' => $openrouter_model,
10726 - 'api_status' => $api_status
10727 - ]);
10728 -}
10729 -
10730 -/**
10731 - * AJAX handler to get similarity threshold
10732 - */
10733 -public function mxchat_get_similarity_threshold() {
10734 - // Verify nonce for security
10735 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
10736 - wp_send_json_error(['message' => 'Invalid nonce']);
10737 - return;
10738 - }
10739 -
10740 - // Only allow admin users
10741 - if (!current_user_can('administrator')) {
10742 - wp_send_json_error(['message' => 'Unauthorized']);
10743 - return;
10744 - }
10745 -
10746 - // Get similarity threshold from main options (default 35%)
10747 - $similarity_threshold = isset($this->options['similarity_threshold'])
10748 - ? ((int) $this->options['similarity_threshold']) / 100
10749 - : 0.35;
10750 -
10751 - wp_send_json_success([
10752 - 'threshold' => $similarity_threshold,
10753 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
10754 - ]);
10755 -}
10756 -
10757 -/**
10758 - * AJAX handler to get knowledge base status
10759 - */
10760 -public function mxchat_get_kb_status() {
10761 - // Verify nonce for security
10762 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
10763 - wp_send_json_error(['message' => 'Invalid nonce']);
10764 - return;
10765 - }
10766 -
10767 - // Only allow admin users
10768 - if (!current_user_can('administrator')) {
10769 - wp_send_json_error(['message' => 'Unauthorized']);
10770 - return;
10771 - }
10772 -
10773 - // Check OpenAI Vector Store first (takes priority)
10774 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
10775 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
10776 -
10777 - if ($use_vectorstore) {
10778 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
10779 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
10780 -
10781 - $kb_info = [
10782 - 'type' => 'OpenAI Vector Store',
10783 - 'status' => 'Active',
10784 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
10785 - ];
10786 -
10787 - wp_send_json_success($kb_info);
10788 - return;
10789 - }
10790 -
10791 - // Check Pinecone vs WordPress
10792 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
10793 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
10794 -
10795 - $kb_info = [
10796 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
10797 - 'status' => 'Active'
10798 - ];
10799 -
10800 - // Get document count
10801 - if ($use_pinecone) {
10802 - $kb_info['documents'] = 'Connected to Pinecone';
10803 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
10804 - } else {
10805 - // Count documents in WordPress database
10806 - global $wpdb;
10807 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
10808 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
10809 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
10810 - }
10811 -
10812 - wp_send_json_success($kb_info);
10813 -}
10814 -
10815 -/**
10816 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
10817 - */
10818 -public function mxchat_start_fresh_session() {
10819 - // Verify nonce for security
10820 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
10821 - wp_send_json_error(['message' => 'Invalid nonce']);
10822 - return;
10823 - }
10824 -
10825 - // Only allow admin users
10826 - if (!current_user_can('administrator')) {
10827 - wp_send_json_error(['message' => 'Unauthorized']);
10828 - return;
10829 - }
10830 -
10831 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
10832 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
10833 -
10834 - if (empty($old_session_id)) {
10835 - wp_send_json_error(['message' => 'Old session ID required']);
10836 - return;
10837 - }
10838 -
10839 - // If no new session ID provided, generate one
10840 - if (empty($new_session_id)) {
10841 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
10842 - }
10843 -
10844 - // Clear ALL data associated with the old session
10845 - $this->clear_complete_session_data($old_session_id);
10846 -
10847 - // Initialize the new session
10848 - $this->initialize_fresh_session($new_session_id);
10849 -
10850 - wp_send_json_success([
10851 - 'message' => 'Fresh session started successfully',
10852 - 'new_session_id' => $new_session_id,
10853 - 'old_session_id' => $old_session_id
10854 - ]);
10855 -}
10856 -
10857 -/**
10858 - * Clear ALL data associated with a session (ENHANCED)
10859 - */
10860 -private function clear_complete_session_data($session_id) {
10861 - // Clear chat history
10862 - delete_option("mxchat_history_{$session_id}");
10863 -
10864 - // Clear chat mode
10865 - delete_option("mxchat_mode_{$session_id}");
10866 -
10867 - // Clear any PDF/Word transients
10868 - $this->clear_pdf_transients($session_id);
10869 - if (method_exists($this, 'clear_word_transients')) {
10870 - $this->clear_word_transients($session_id);
10871 - }
10872 -
10873 - // Clear agent-related data
10874 - delete_option("mxchat_channel_{$session_id}");
10875 - delete_option("mxchat_agent_name_{$session_id}");
10876 - delete_option("mxchat_email_{$session_id}");
10877 -
10878 - // Clear any recommendation flow state
10879 - delete_option("mxchat_sr_flow_state_{$session_id}");
10880 -
10881 - // Clear any cached embeddings or context
10882 - delete_transient("mxchat_context_{$session_id}");
10883 - delete_transient("mxchat_last_query_{$session_id}");
10884 -
10885 - // Clear any testing data
10886 - delete_transient("mxchat_testing_data_{$session_id}");
10887 -
10888 - // Clear any rate limiting data for this session
10889 - delete_transient("mxchat_rate_limit_{$session_id}");
10890 -
10891 - // Clear any other session-specific transients
10892 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
10893 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
10894 - delete_transient("mxchat_include_word_in_context_{$session_id}");
10895 -
10896 - // Clear form addon state (pending forms and submitted forms)
10897 - delete_option("mxchat_pending_form_{$session_id}");
10898 - delete_option("mxchat_submitted_forms_{$session_id}");
10899 -
10900 - //error_log("MxChat: Cleared all data for session: {$session_id}");
10901 -}
10902 -
10903 -/**
10904 - * Initialize a fresh session with default data
10905 - */
10906 -private function initialize_fresh_session($session_id) {
10907 - // Set default chat mode
10908 - update_option("mxchat_mode_{$session_id}", 'ai');
10909 -
10910 - //error_log("MxChat: Initialized fresh session: {$session_id}");
10911 -}
10912 -
10913 -/**
10914 - * Helper method to clear Word document transients (if you have Word support)
10915 - */
10916 -private function clear_word_transients($session_id) {
10917 - delete_transient('mxchat_word_url_' . $session_id);
10918 - delete_transient('mxchat_word_filename_' . $session_id);
10919 - delete_transient('mxchat_word_embeddings_' . $session_id);
10920 - delete_transient('mxchat_include_word_in_context_' . $session_id);
10921 -}
10922 -
10923 -/**
10924 - * Simplified testing data capture method (CLEANED UP)
10925 - */
10926 -private function capture_testing_data($user_embedding, $message, $session_id) {
10927 - // Only capture for admin users
10928 - if (!current_user_can('administrator')) {
10929 - return null;
10930 - }
10931 -
10932 - $testing_data = [
10933 - 'query' => $message,
10934 - 'timestamp' => time(),
10935 - 'top_matches' => [],
10936 - 'action_matches' => [] // Add action matches
10937 - ];
10938 -
10939 - // Get similarity threshold
10940 - $similarity_threshold = isset($this->options['similarity_threshold'])
10941 - ? ((int) $this->options['similarity_threshold']) / 100
10942 - : 0.35;
10943 -
10944 - $testing_data['similarity_threshold'] = $similarity_threshold;
10945 -
10946 - // Use the real similarity analysis if available
10947 - if ($this->last_similarity_analysis !== null) {
10948 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
10949 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
10950 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
10951 - } else {
10952 - // Fallback: determine knowledge base type
10953 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
10954 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
10955 -
10956 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
10957 - }
10958 -
10959 - // Include action analysis if available
10960 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
10961 - $testing_data['action_matches'] = $this->last_action_analysis;
10962 -
10963 - // Clear it after capturing to avoid stale data
10964 - $this->last_action_analysis = null;
10965 - }
10966 -
10967 - return $testing_data;
10968 -}
10969 -
10970 -
10971 -/**
10972 - * Track URL clicks from chatbot responses
10973 - */
10974 -public function mxchat_track_url_click() {
10975 - // Verify nonce for security
10976 - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
10977 - wp_send_json_error(['message' => 'Invalid nonce']);
10978 - wp_die();
10979 - }
10980 -
10981 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
10982 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
10983 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
10984 -
10985 - if (empty($session_id) || empty($clicked_url)) {
10986 - wp_send_json_error(['message' => 'Missing required data']);
10987 - wp_die();
10988 - }
10989 -
10990 - global $wpdb;
10991 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
10992 -
10993 - // Insert click tracking record
10994 - $wpdb->insert(
10995 - $table_name,
10996 - [
10997 - 'session_id' => $session_id,
10998 - 'clicked_url' => $clicked_url,
10999 - 'message_context' => $message_context,
11000 - 'click_timestamp' => current_time('mysql', 1),
11001 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
11002 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
11003 - ]
11004 - );
11005 -
11006 - wp_send_json_success(['message' => 'Click tracked']);
11007 - wp_die();
11008 -}
11009 -
11010 -/**
11011 - * Get URL click analytics for a session
11012 - */
11013 -public function mxchat_get_url_clicks($session_id) {
11014 - global $wpdb;
11015 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11016 -
11017 - $clicks = $wpdb->get_results($wpdb->prepare(
11018 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
11019 - $session_id
11020 - ));
11021 -
11022 - return $clicks;
11023 -}
11024 -/**
11025 - * Track the originating page where chat was started
11026 - */
11027 -public function mxchat_track_originating_page() {
11028 - // Verify nonce
11029 - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11030 - wp_send_json_error(['message' => 'Invalid nonce']);
11031 - wp_die();
11032 - }
11033 -
11034 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11035 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
11036 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
11037 -
11038 - if (empty($session_id)) {
11039 - wp_send_json_error(['message' => 'Missing session ID']);
11040 - wp_die();
11041 - }
11042 -
11043 - global $wpdb;
11044 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
11045 -
11046 - // Check if we've already tracked for this session
11047 - $existing = $wpdb->get_var($wpdb->prepare(
11048 - "SELECT COUNT(*) FROM $table_name
11049 - WHERE session_id = %s
11050 - AND originating_page_url IS NOT NULL",
11051 - $session_id
11052 - ));
11053 -
11054 - if ($existing > 0) {
11055 - wp_send_json_success(['message' => 'Already tracked']);
11056 - wp_die();
11057 - }
11058 -
11059 - // Update the first message in this session with originating page info
11060 - $wpdb->query($wpdb->prepare(
11061 - "UPDATE $table_name
11062 - SET originating_page_url = %s,
11063 - originating_page_title = %s
11064 - WHERE session_id = %s
11065 - ORDER BY timestamp ASC
11066 - LIMIT 1",
11067 - $page_url,
11068 - $page_title,
11069 - $session_id
11070 - ));
11071 -
11072 - wp_send_json_success(['message' => 'Originating page tracked']);
11073 - wp_die();
11074 -}
11075 -
11076 -/**
11077 - * Validate and clean URLs from AI response
11078 - * Removes any URLs that aren't in the knowledge base
11079 - *
11080 - * @param string $response_text The AI-generated response
11081 - * @param array $valid_urls Array of URLs from the knowledge base
11082 - * @return string Cleaned response with invalid URLs removed/flagged
11083 - */
11084 -private function validate_and_clean_urls($response_text, $valid_urls) {
11085 - // DEBUG: Log what we're working with
11086 - //error_log("=== MxChat URL Validation Debug ===");
11087 - //error_log("Valid URLs count: " . count($valid_urls));
11088 - //error_log("Valid URLs: " . print_r($valid_urls, true));
11089 - //error_log("Response text length: " . strlen($response_text));
11090 - //error_log("Response text preview: " . substr($response_text, 0, 500));
11091 -
11092 - // If no valid URLs provided or empty response, return as-is
11093 - if (empty($valid_urls) || empty($response_text)) {
11094 - //error_log("Validation skipped - empty valid_urls or response");
11095 - return $response_text;
11096 - }
11097 -
11098 - // Extract all URLs from the AI response
11099 - // This regex matches http:// and https:// URLs
11100 - preg_match_all(
11101 - '#\bhttps?://[^\s<>"\')\]]+#i',
11102 - $response_text,
11103 - $matches
11104 - );
11105 -
11106 - // If no URLs found in response, return as-is
11107 - if (empty($matches[0])) {
11108 - //error_log("No URLs found in response");
11109 - return $response_text;
11110 - }
11111 -
11112 - $found_urls = $matches[0];
11113 - $cleaned_response = $response_text;
11114 - $removed_count = 0;
11115 -
11116 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
11117 - $normalized_valid_urls = array_map(function($url) {
11118 - // Remove trailing slash
11119 - $url = rtrim($url, '/');
11120 - // Remove URL fragments (#section)
11121 - $url = preg_replace('/#.*$/', '', $url);
11122 - // Remove trailing punctuation that might have been captured
11123 - $url = rtrim($url, '.,;:!?');
11124 - return $url;
11125 - }, $valid_urls);
11126 -
11127 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
11128 -
11129 - foreach ($found_urls as $found_url) {
11130 - // Clean up the found URL (remove trailing punctuation that might have been captured)
11131 - $clean_found_url = rtrim($found_url, '.,;:!?)');
11132 -
11133 - // DEBUG: Log each URL being checked
11134 - //error_log("Checking found URL: " . $found_url);
11135 -
11136 - // Normalize for comparison
11137 - $normalized_found = rtrim($clean_found_url, '/');
11138 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
11139 -
11140 - //error_log("Normalized found URL: " . $normalized_found);
11141 -
11142 - // Check if this URL exists in our valid URLs list
11143 - $is_valid = false;
11144 -
11145 - //error_log("Starting validation checks for: " . $normalized_found);
11146 -
11147 - // First, try exact match
11148 - if (in_array($normalized_found, $normalized_valid_urls)) {
11149 - $is_valid = true;
11150 - //error_log("EXACT MATCH FOUND");
11151 - } else {
11152 - //error_log("No exact match, checking variations...");
11153 - // If no exact match, check if it's a variation (with query params, etc.)
11154 - foreach ($normalized_valid_urls as $valid_url) {
11155 - //error_log(" Comparing against valid URL: " . $valid_url);
11156 -
11157 - // Check if the found URL starts with a valid URL (handles query params)
11158 - if (strpos($normalized_found, $valid_url) === 0) {
11159 - // Check what comes after the valid URL
11160 - $remainder = substr($normalized_found, strlen($valid_url));
11161 -
11162 - // Only valid if:
11163 - // 1. Exact match (remainder is empty)
11164 - // 2. Query params (starts with ?)
11165 - // 3. Fragment (starts with #)
11166 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
11167 - $is_valid = true;
11168 - //error_log(" MATCH: Found URL is valid variation of base URL");
11169 - break;
11170 - } else {
11171 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
11172 - }
11173 - }
11174 - // Also check the reverse (in case valid URL has query params)
11175 - if (strpos($valid_url, $normalized_found) === 0) {
11176 - $is_valid = true;
11177 - //error_log(" MATCH: Valid URL starts with found URL");
11178 - break;
11179 - }
11180 - }
11181 -
11182 - if (!$is_valid) {
11183 - //error_log("NO MATCH FOUND - URL should be removed");
11184 - }
11185 - }
11186 -
11187 - // If URL is not valid, remove it from the response
11188 - if (!$is_valid) {
11189 - // Log the removal for debugging
11190 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
11191 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
11192 -
11193 - $removed_count++;
11194 -
11195 - // Check if URL is part of a markdown link: [text](url)
11196 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
11197 - if (preg_match($markdown_pattern, $cleaned_response)) {
11198 - //error_log("Found markdown link, removing but keeping text");
11199 - // Remove the markdown link but keep the text
11200 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
11201 - }
11202 - // Check if URL is part of an HTML link: <a href="url">text</a>
11203 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
11204 - //error_log("Found HTML link, removing but keeping text");
11205 - // Remove the HTML link but keep the text
11206 - $link_text = $link_match[1];
11207 - $cleaned_response = preg_replace(
11208 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
11209 - $link_text,
11210 - $cleaned_response
11211 - );
11212 - }
11213 - // Otherwise just remove the bare URL
11214 - else {
11215 - //error_log("Removing bare URL");
11216 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
11217 - }
11218 - }
11219 - }
11220 -
11221 - // Log summary if any URLs were removed
11222 - if ($removed_count > 0) {
11223 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
11224 - } else {
11225 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
11226 - }
11227 -
11228 - // Clean up any double spaces or awkward punctuation left behind
11229 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
11230 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
11231 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
11232 -
11233 - //error_log("Final cleaned response: " . $cleaned_response);
11234 -
11235 - return trim($cleaned_response);
11236 -}
11237 -
11238 -/**
11239 - * AJAX handler to get current chat mode for a session
11240 - */
11241 -public function mxchat_get_current_chat_mode() {
11242 - // Verify nonce for security
11243 - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
11244 - wp_send_json_error(['message' => 'Invalid nonce']);
11245 - wp_die();
11246 - }
11247 -
11248 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11249 -
11250 - if (empty($session_id)) {
11251 - wp_send_json_error(['message' => 'Session ID missing']);
11252 - wp_die();
11253 - }
11254 -
11255 - // Get the current chat mode for this session
11256 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
11257 -
11258 - wp_send_json_success([
11259 - 'chat_mode' => $chat_mode
11260 - ]);
11261 - wp_die();
11262 -}
11263 -
11264 -
11265 -
11266 -}
11267 -?>
1 +<?php
2 +if (!defined('ABSPATH')) {
3 + exit;
4 +}
5 +
6 +class MxChat_Integrator {
7 + private $options;
8 + private $prompts_options;
9 + private $chat_count;
10 + private $fallbackResponse;
11 + private $productCardHtml;
12 + private $word_handler;
13 +
14 +/**
15 + * Setup the cron jobs for rate limits
16 + */
17 +public function setup_rate_limit_cron_jobs() {
18 + // Clear previous schedules
19 + wp_clear_scheduled_hook('mxchat_reset_rate_limits');
20 + wp_clear_scheduled_hook('mxchat_reset_hourly_rate_limits');
21 + wp_clear_scheduled_hook('mxchat_reset_daily_rate_limits');
22 + wp_clear_scheduled_hook('mxchat_reset_weekly_rate_limits');
23 + wp_clear_scheduled_hook('mxchat_reset_monthly_rate_limits');
24 +
25 + // Schedule the main rate limit reset check (runs hourly)
26 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
27 + wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
28 + }
29 +}
30 +
31 +/**
32 + * Class constructor
33 + */
34 +public function __construct() {
35 + $this->options = get_option('mxchat_options');
36 + $this->prompts_options = get_option('mxchat_prompts_options', array());
37 + $this->chat_count = get_option('mxchat_chat_count', 0);
38 + $this->word_handler = new MXChat_Word_Handler($this->options);
39 +
40 + // Setup the cron jobs for rate limits
41 + $this->setup_rate_limit_cron_jobs();
42 +
43 + // Add all action hooks
44 + add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
45 + add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
46 + add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
47 + add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
48 + add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
49 +
50 + // Add the AJAX actions for checking if the pre-chat message was dismissed
51 + add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
52 + add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
53 + add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
54 + add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
55 + add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
56 + add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
57 +
58 + // Add REST API routes registration
59 + add_action('rest_api_init', array($this, 'register_routes'));
60 + add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
61 + add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
62 +
63 + // Rate limit action - notice we removed the old schedule setup
64 + add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
65 +
66 + // File upload and handling actions
67 + add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
68 + add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
69 + add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
70 + add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
71 +
72 + // Word document handling actions
73 + add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
74 + add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
75 + add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
76 + add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
77 + add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
78 + add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
79 +
80 + // Email handling actions
81 + add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
82 + add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
83 + add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
84 + add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
85 +}
86 +
87 +
88 + private function mxchat_increment_chat_count() {
89 + $chat_count = get_option('mxchat_chat_count', 0);
90 + $chat_count++;
91 + update_option('mxchat_chat_count', $chat_count);
92 + }
93 +
94 +function mxchat_fetch_conversation_history() {
95 + if (empty($_POST['session_id'])) {
96 + wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
97 + wp_die();
98 + }
99 +
100 + $session_id = sanitize_text_field($_POST['session_id']);
101 + $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
102 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
103 +
104 + if (empty($history)) {
105 + // Even if history is empty, return the chat mode
106 + wp_send_json_success([
107 + 'conversation' => [],
108 + 'chat_mode' => $chat_mode
109 + ]);
110 + wp_die();
111 + }
112 +
113 + wp_send_json_success([
114 + 'conversation' => $history,
115 + 'chat_mode' => $chat_mode
116 + ]);
117 + wp_die();
118 +}
119 +private function mxchat_fetch_conversation_history_for_ajax($session_id) {
120 + $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history based on session ID
121 + $formatted_history = [];
122 +
123 + // Format the history to align with the expected structure for OpenAI
124 + foreach ($history as $entry) {
125 + $formatted_history[] = [
126 + 'role' => $entry['role'], // Ensure this matches 'user' or 'assistant'
127 + 'content' => $entry['content']
128 + ];
129 + }
130 +
131 + return $formatted_history;
132 +}
133 +
134 +
135 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
136 + $history = get_option("mxchat_history_{$session_id}", []);
137 + $formatted_history = [];
138 +
139 + // Adjusted for code-heavy conversations
140 + $max_tokens = 120000; // Context window size
141 + $reserved_tokens = 5000; // Space for system prompts + current query
142 + $current_token_count = 0;
143 +
144 + // Allowed HTML tags for content sanitization
145 + $allowed_tags = [
146 + 'pre' => ['class' => true],
147 + 'code' => ['class' => true],
148 + 'span' => ['class' => true],
149 + 'div' => ['class' => true],
150 + 'strong' => [],
151 + 'em' => []
152 + ];
153 +
154 + foreach (array_reverse($history) as $entry) {
155 + // Preserve code blocks while sanitizing other HTML
156 + $clean_content = wp_kses($entry['content'], $allowed_tags);
157 +
158 + // Detect code blocks in content
159 + $has_code = false;
160 +// Replace the HTML check with:
161 +// Allow messages that contain code blocks or are plain text
162 +if (strpos($clean_content, '<pre') === false &&
163 + strpos($clean_content, '<code') === false &&
164 + $clean_content !== strip_tags($entry['content'])) {
165 + continue;
166 +}
167 +
168 + // Skip entries that lost significant content during sanitization
169 + if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
170 + continue;
171 + }
172 +
173 + // More accurate token estimation (1 token ≈ 4 characters)
174 + $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
175 +
176 + // Check token budget with the new estimate
177 + if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
178 + // Try to fit partial content if it's the first entry
179 + if (empty($formatted_history)) {
180 + $clean_content = mb_substr($clean_content, 0, ($max_tokens - $reserved_tokens) * 4);
181 + $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
182 + } else {
183 + break;
184 + }
185 + }
186 +
187 + // Add to formatted history
188 + $formatted_history[] = [
189 + 'role' => $entry['role'],
190 + 'content' => $clean_content
191 + ];
192 +
193 + $current_token_count += $token_estimate;
194 + }
195 +
196 + // Reverse back to maintain chronological order
197 + $formatted_history = array_reverse($formatted_history);
198 +
199 + // Add system message about code context
200 + array_unshift($formatted_history, [
201 + 'role' => 'system',
202 + 'content' => 'Preserved code blocks are marked with [CODE BLOCK PRESERVED]. '
203 + . 'Maintain formatting and syntax highlighting when referencing code.'
204 + ]);
205 +
206 + return $formatted_history;
207 +}
208 +
209 +public function register_routes() {
210 + //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
211 +
212 + register_rest_route('mxchat/v1', '/stream', [
213 + 'methods' => 'GET',
214 + 'callback' => [$this, 'mxchat_stream_events'],
215 + 'permission_callback' => [$this, 'verify_chat_session'],
216 + ]);
217 +
218 + register_rest_route('mxchat/v1', '/agent-response', [
219 + 'methods' => 'POST',
220 + 'callback' => [$this, 'mxchat_handle_agent_response'],
221 + 'permission_callback' => [$this, 'verify_slack_request'],
222 + ]);
223 +
224 + register_rest_route('mxchat/v1', '/slack-interaction', [
225 + 'methods' => 'POST',
226 + 'callback' => [$this, 'handle_slack_interaction'],
227 + 'permission_callback' => [$this, 'verify_slack_request'],
228 + ]);
229 +
230 + //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
231 +}
232 +
233 +/**
234 + * Verify valid chat session
235 + */
236 +public function verify_chat_session($request) {
237 + $session_id = $request->get_param('session_id');
238 + if (empty($session_id)) {
239 + //error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
240 + return false;
241 + }
242 +
243 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
244 + return $chat_mode === 'agent';
245 +}
246 +
247 +/**
248 + * Verify request is coming from Slack.
249 + *
250 + * @param WP_REST_Request $request
251 + * @return bool True if valid, false otherwise.
252 + */
253 +public function verify_slack_request($request) {
254 + // Get the Slack signing secret from your plugin options
255 + $valid_key = $this->options['live_agent_secret_key'] ?? '';
256 +
257 + if (empty($valid_key)) {
258 + //error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
259 + return false;
260 + }
261 +
262 + $timestamp = $request->get_header('X-Slack-Request-Timestamp');
263 + $slack_signature = $request->get_header('X-Slack-Signature');
264 +
265 + // Verify timestamp to prevent replay attacks
266 + if (abs(time() - intval($timestamp)) > 300) {
267 + //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
268 + return false;
269 + }
270 +
271 + // Get raw request body
272 + $request_body = file_get_contents('php://input');
273 +
274 + // Create the signature base string
275 + $sig_basestring = "v0:{$timestamp}:{$request_body}";
276 +
277 + // Calculate expected signature
278 + $my_signature = 'v0=' . hash_hmac('sha256', $sig_basestring, $valid_key);
279 +
280 + // Compare signatures
281 + return hash_equals($my_signature, $slack_signature);
282 +}
283 +
284 +public function mxchat_stream_events(WP_REST_Request $request) {
285 + header('Content-Type: text/event-stream');
286 + header('Cache-Control: no-cache');
287 + header('Connection: keep-alive');
288 +
289 + $session_id = sanitize_text_field($request->get_param('session_id'));
290 + $last_seen_id = sanitize_text_field($request->get_param('last_seen_id')) ?: '';
291 +
292 + if (empty($session_id)) {
293 + echo esc_html__("event: error\ndata: ", 'mxchat') . esc_html__('Missing session_id', 'mxchat') . "\n\n";
294 + flush();
295 + exit;
296 + }
297 +
298 + $history = get_option("mxchat_history_{$session_id}", []);
299 +
300 + // Filter only new messages
301 + $new_messages = array_filter($history, function ($message) use ($last_seen_id) {
302 + return !empty($message['id']) && $message['id'] > $last_seen_id;
303 + });
304 +
305 + // Send new messages if available
306 + if (!empty($new_messages)) {
307 + echo esc_html__("event: newMessages\ndata: ", 'mxchat') . json_encode(array_values($new_messages)) . "\n\n";
308 + } else {
309 + // Keep the connection alive
310 + echo esc_html__("event: keepAlive\ndata: ", 'mxchat') . "{}\n\n";
311 + }
312 + flush();
313 + exit;
314 +}
315 +
316 +
317 +
318 +
319 +private function mxchat_save_chat_message($session_id, $role, $message) {
320 + global $wpdb;
321 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
322 + //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
323 +
324 + // Check if this is the first message in a new session (before any other database operations)
325 + $is_new_session = false;
326 + if ($role === 'user') { // Only check for user messages, not bot responses
327 + $existing_messages = $wpdb->get_var($wpdb->prepare(
328 + "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
329 + $session_id
330 + ));
331 + $is_new_session = ($existing_messages == 0);
332 + }
333 +
334 + // 1) Extract agent name if present
335 + $agent_name = '';
336 + if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
337 + $agent_name = $matches[1];
338 + $message = str_replace("Agent: $agent_name - ", '', $message);
339 + $session_meta_key = "mxchat_agent_name_{$session_id}";
340 + if (empty(get_option($session_meta_key))) {
341 + update_option($session_meta_key, $agent_name);
342 + //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
343 + }
344 + }
345 + // 2) Generate unique message_id
346 + $message_id = uniqid();
347 + //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
348 + // 3) Determine user_id
349 + $user_id = is_user_logged_in() ? get_current_user_id() : 0;
350 + // 4) Determine user_identifier
351 + $user_identifier = $agent_name
352 + ? $agent_name
353 + : MxChat_User::mxchat_get_user_identifier();
354 + // 5) Determine displayed_name
355 + $user_email = MxChat_User::mxchat_get_user_email();
356 + $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
357 + // 6) Check for a saved email in wp_options
358 + $email_option_key = "mxchat_email_{$session_id}";
359 + $saved_email = get_option($email_option_key);
360 + //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
361 + // If found, update DB user_email
362 + if ($saved_email) {
363 + $update_res = $wpdb->update(
364 + $table_name,
365 + ['user_email' => $saved_email],
366 + ['session_id' => $session_id],
367 + ['%s'],
368 + ['%s']
369 + );
370 + //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email update for session_id {$session_id}. update_res: {$update_res}");
371 + }
372 + // 7) Save to session history in wp_options
373 + $history_key = "mxchat_history_{$session_id}";
374 + $history = get_option($history_key, []);
375 + $history[] = [
376 + 'id' => $message_id,
377 + 'role' => $role,
378 + 'content' => $message,
379 + 'timestamp' => round(microtime(true) * 1000),
380 + 'agent_name' => $displayed_name,
381 + ];
382 + update_option($history_key, $history, 'no');
383 + //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
384 + // 8) Save the message to DB (INSERT)
385 + $insert_data = [
386 + 'user_id' => $user_id,
387 + 'user_identifier'=> $user_identifier,
388 + 'user_email' => $saved_email ?: $user_email,
389 + 'session_id' => $session_id,
390 + 'role' => $role,
391 + 'message' => $message,
392 + 'timestamp' => current_time('mysql', 1),
393 + ];
394 + $wpdb->insert($table_name, $insert_data);
395 + //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
396 +
397 + // 9) Send notification email if this is the first user message in a new session
398 + if ($wpdb->insert_id && $is_new_session && $role === 'user') {
399 + $this->send_new_chat_notification($session_id, array(
400 + 'identifier' => $user_identifier,
401 + 'email' => $saved_email ?: $user_email,
402 + 'ip' => $_SERVER['REMOTE_ADDR']
403 + ));
404 + }
405 +
406 + //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
407 + return $message_id;
408 +}
409 +private function send_new_chat_notification($session_id, $user_info = array()) {
410 + $options = get_option('mxchat_transcripts_options');
411 +
412 + // Check if notifications are enabled
413 + if (empty($options['mxchat_enable_notifications'])) {
414 + return false;
415 + }
416 +
417 + // Get notification email
418 + $to = !empty($options['mxchat_notification_email']) ?
419 + $options['mxchat_notification_email'] :
420 + get_option('admin_email');
421 +
422 + if (!is_email($to)) {
423 + return false;
424 + }
425 +
426 + // Prepare email content
427 + $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
428 +
429 + $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
430 + $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
431 + $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
432 +
433 + $message = sprintf(
434 + "A new chat session has started on your website.\n\n" .
435 + "Session ID: %s\n" .
436 + "User: %s\n" .
437 + "Email: %s\n" .
438 + "IP Address: %s\n" .
439 + "Time: %s\n\n" .
440 + "View transcripts: %s",
441 + $session_id,
442 + $user_identifier,
443 + $user_email,
444 + $user_ip,
445 + current_time('mysql'),
446 + admin_url('admin.php?page=mxchat-transcripts')
447 + );
448 +
449 + // Send email
450 + return wp_mail($to, $subject, $message);
451 +}
452 +
453 +public function mxchat_handle_save_email_and_response() {
454 + //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
455 +
456 + // Validate nonce
457 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
458 + //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
459 + wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
460 + wp_die();
461 + }
462 +
463 + $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
464 + $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
465 +
466 + //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}");
467 +
468 + if (empty($session_id) || empty($email)) {
469 + //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
470 + wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
471 + wp_die();
472 + }
473 +
474 + // 1) Always store in wp_options
475 + $option_key = "mxchat_email_{$session_id}";
476 + update_option($option_key, $email);
477 + //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$option_key} => {$email}");
478 +
479 + // 2) (Optional) Also store in DB if a row already exists
480 + global $wpdb;
481 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
482 +
483 + // Make sure we have a valid placeholder in prepare
484 + $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", $session_id);
485 + $session_count = $wpdb->get_var($sql);
486 +
487 + //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
488 +
489 + if ($session_count) {
490 + // Update user_email if row(s) exist
491 + $update_sql = $wpdb->prepare(
492 + "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
493 + $email,
494 + $session_id
495 + );
496 + $wpdb->query($update_sql);
497 + //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
498 + } else {
499 + //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email is only in wp_options.");
500 + }
501 +
502 + // Provide success response
503 + $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
504 + //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
505 + wp_send_json_success(['message' => $bot_message]);
506 + wp_die();
507 +}
508 +
509 +public function mxchat_check_email_provided() {
510 + //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
511 +
512 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
513 + //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
514 + wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
515 + }
516 +
517 + $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
518 + if (empty($session_id)) {
519 + //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
520 + wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
521 + }
522 +
523 + // Check if the user is logged in
524 + if (is_user_logged_in()) {
525 + $current_user = wp_get_current_user();
526 + //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
527 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
528 + }
529 +
530 + $option_key = "mxchat_email_{$session_id}";
531 + $stored_email = get_option($option_key, '');
532 +
533 + //error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
534 +
535 + if (!empty($stored_email)) {
536 + //error_log("[DEBUG] mxchat_check_email_provided -> Email found, returning success");
537 + wp_send_json_success(['email' => $stored_email]);
538 + } else {
539 + //error_log("[DEBUG] mxchat_check_email_provided -> No email found, returning error");
540 + wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
541 + }
542 +}
543 +
544 +// Add this to your plugin's main PHP file
545 +public function mxchat_check_new_messages() {
546 + if (!isset($_POST['session_id']) || !isset($_POST['last_seen_id'])) {
547 + wp_send_json_error(['message' => 'Missing required parameters']);
548 + wp_die();
549 + }
550 +
551 + $session_id = sanitize_text_field($_POST['session_id']);
552 + $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
553 +
554 + // Get chat history
555 + $history = get_option("mxchat_history_{$session_id}", []);
556 +
557 + if (empty($history)) {
558 + wp_send_json_success([
559 + 'hasNewMessages' => false,
560 + 'new_messages' => []
561 + ]);
562 + wp_die();
563 + }
564 +
565 + // Filter new messages
566 + $new_messages = array_filter($history, function($message) use ($last_seen_id) {
567 + return isset($message['id']) && $message['id'] > $last_seen_id;
568 + });
569 +
570 + // Sort by ID to ensure proper order
571 + usort($new_messages, function($a, $b) {
572 + return $a['id'] <=> $b['id'];
573 + });
574 +
575 + wp_send_json_success([
576 + 'hasNewMessages' => !empty($new_messages),
577 + 'new_messages' => array_values($new_messages),
578 + 'latestMessageId' => end($new_messages)['id'] ?? $last_seen_id
579 + ]);
580 + wp_die();
581 +}
582 +
583 +public function mxchat_handle_chat_request() {
584 + global $wpdb;
585 +
586 +
587 + // Check if MX Chat Moderation is active
588 + if (class_exists('MX_Chat_Moderation')) {
589 + // Get user email and IP
590 + $user_email = '';
591 + $user_ip = $_SERVER['REMOTE_ADDR'];
592 +
593 + // If user is logged in, get their email
594 + if (is_user_logged_in()) {
595 + $current_user = wp_get_current_user();
596 + $user_email = $current_user->user_email;
597 + }
598 +
599 + // Create ban handler instance
600 + $ban_handler = new MX_Chat_Ban_Handler();
601 +
602 + // Check if user is banned by IP
603 + if ($ban_handler->check_ban($user_ip, 'ip')) {
604 + wp_send_json([
605 + 'success' => false,
606 + 'message' => esc_html__('Access denied. Your IP address has been banned.', 'mxchat'),
607 + 'status' => 'banned'
608 + ]);
609 + wp_die();
610 + }
611 +
612 + // If user is logged in, also check email
613 + if (!empty($user_email) && $ban_handler->check_ban($user_email, 'email')) {
614 + wp_send_json([
615 + 'success' => false,
616 + 'message' => esc_html__('Access denied. Your email address has been banned.', 'mxchat'),
617 + 'status' => 'banned'
618 + ]);
619 + wp_die();
620 + }
621 + }
622 +
623 +$this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
624 +$this->productCardHtml = '';
625 +
626 +// Get the actual WordPress user ID if logged in
627 +$is_logged_in = is_user_logged_in();
628 +if ($is_logged_in) {
629 + $user_id = get_current_user_id(); // This will get the actual WordPress user ID
630 +} else {
631 + // For logged-out users, use your existing identifier method
632 + $user_id = $this->mxchat_get_user_identifier();
633 +}
634 +
635 +// Get and sanitize the user identifier
636 +$user_id = sanitize_key($user_id);
637 +
638 +// Check rate limit using new settings structure
639 +$rate_limit_result = $this->check_rate_limit();
640 +
641 +// Add this at the start of your rate limit checking in mxchat_handle_chat_request()
642 +//error_log('MXChat Rate Limit: Starting rate limit check in handle_chat_request()');
643 +
644 +// Then right after checking the result:
645 +if ($rate_limit_result !== true) {
646 + //error_log('MXChat Rate Limit: Rate limit exceeded, returning error');
647 + wp_send_json([
648 + 'success' => false,
649 + 'message' => $rate_limit_result['message'],
650 + 'status' => 'rate_limit_exceeded'
651 + ]);
652 + wp_die();
653 +} else {
654 + //error_log('MXChat Rate Limit: Check passed successfully');
655 +}
656 +
657 + // Rest of your existing code...
658 + $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
659 + //error_log("Session ID: $session_id");
660 +
661 + if (empty($session_id)) {
662 + //error_log("Error: Session ID is missing.");
663 + wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
664 + wp_die();
665 + }
666 +
667 + // Validate and sanitize the incoming message
668 + if (empty($_POST['message'])) {
669 + //error_log("Error: No message received.");
670 + wp_send_json_error(esc_html__('No message received.', 'mxchat'));
671 + wp_die();
672 + }
673 +
674 +
675 +// Modify the message sanitization to preserve PHP tags in code blocks
676 +$allowed_tags = [
677 + 'pre' => [],
678 + 'code' => ['class' => true],
679 + 'span' => ['class' => true],
680 + 'div' => ['class' => true],
681 +];
682 +
683 +// First preserve code blocks
684 +$message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
685 + return htmlspecialchars_decode($matches[0]);
686 +}, $_POST['message']);
687 +
688 +// Then apply sanitization
689 +$message = wp_kses($message, $allowed_tags);
690 +
691 +// Decode code blocks
692 +$message = preg_replace_callback('/(&lt;pre&gt;&lt;code.*?&gt;.*?&lt;\/code&gt;&lt;\/pre&gt;)/s', function($matches) {
693 + return htmlspecialchars_decode($matches[1]);
694 +}, $message);
695 +
696 +$message = trim($message);
697 +
698 +// Preserve code blocks from markdown conversion
699 +$message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
700 +
701 +// Check if any add-ons want to pre-process this message (for web search etc.)
702 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
703 +
704 +// If the pre-processing returned a result (not the original message), use it directly
705 +if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
706 + // Save the AI response
707 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
708 +
709 + // Save HTML content if provided
710 + if (!empty($pre_processed_result['html'])) {
711 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
712 + }
713 +
714 + // Return the response
715 + wp_send_json([
716 + 'text' => $pre_processed_result['text'],
717 + 'html' => $pre_processed_result['html'] ?? '',
718 + 'session_id' => $session_id
719 + ]);
720 + wp_die();
721 +}
722 +
723 + // Save the user's message
724 + $this->mxchat_save_chat_message($session_id, 'user', $message);
725 +
726 + // Check if the message is an email address
727 + if (is_email($message)) {
728 + // Add the email to Loops
729 + $this->add_email_to_loops($message);
730 +
731 + // Send success response
732 + $response_message = $this->options['email_capture_response'] ??
733 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
734 +
735 + wp_send_json([
736 + 'success' => true,
737 + 'status' => 'email_captured',
738 + 'message' => $response_message
739 + ]);
740 + wp_die();
741 + }
742 +
743 + $intent_info = '';
744 +
745 + // Check chat mode
746 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
747 + //error_log("Chat Mode: $chat_mode");
748 +
749 + // Handle agent mode
750 + if ($chat_mode === 'agent') {
751 + // First, check for switch intent before doing anything else
752 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
753 +
754 + // If we matched an intent and it's the switch intent, handle it
755 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
756 + //error_log("Switch to chatbot intent detected");
757 +
758 + // Update chat mode first
759 + update_option("mxchat_mode_{$session_id}", 'ai');
760 +
761 + // Clear any existing PDF context to start fresh
762 + $this->clear_pdf_transients($session_id);
763 +
764 + // Prepare clean switch response
765 + $response_data = [
766 + 'text' => $this->fallbackResponse['text'],
767 + 'html' => '',
768 + 'session_id' => $session_id,
769 + 'chat_mode' => 'ai'
770 + ];
771 +
772 + // Save the mode switch message
773 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
774 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
775 +
776 + // Send response and exit
777 + wp_send_json($response_data);
778 + wp_die();
779 + } elseif (!$intent_matched) {
780 + // No intent matched, handle live agent message
781 + try {
782 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
783 + //error_log("Message sent to agent.");
784 +
785 + wp_send_json_success([
786 + 'status' => 'waiting_for_agent',
787 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
788 + ]);
789 + } catch (\Exception $e) {
790 + //error_log("Error sending message to agent: " . $e->getMessage());
791 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
792 + }
793 + wp_die();
794 + }
795 + }
796 +
797 + // Step 1: Check for new PDF URL in the message
798 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
799 + $new_pdf_url = $matches[0];
800 +
801 + // Check if this is likely a PDF-related request
802 + $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
803 + $is_pdf_request = false;
804 +
805 + foreach ($pdf_keywords as $keyword) {
806 + if (stripos($message, $keyword) !== false) {
807 + $is_pdf_request = true;
808 + break;
809 + }
810 + }
811 +
812 + // If it looks like a PDF request or we're waiting for a PDF URL
813 + if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
814 + // Validate HTTPS
815 + if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
816 + // Extract filename from URL
817 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
818 +
819 + // Clear previous PDF transients
820 + $this->clear_pdf_transients($session_id);
821 +
822 + // Process new PDF
823 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
824 + $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
825 +
826 + if ($embeddings === 'too_many_pages') {
827 + $error_text = sprintf(
828 + $this->options['pdf_intent_error_text'] ??
829 + esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
830 + $max_pages
831 + );
832 + $this->fallbackResponse['text'] = $error_text;
833 + } elseif ($embeddings) {
834 + // Store new PDF information
835 + // Create a more meaningful filename from URL
836 + $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
837 +
838 + // If the filename is generic (like results_download.php), create a more descriptive one
839 + if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
840 + strpos($pdf_filename, '.php') !== false) {
841 + // Create a timestamp-based name
842 + $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
843 + }
844 +
845 + set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
846 + set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
847 + set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
848 + set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
849 +
850 + $success_text = $this->options['pdf_intent_success_text'] ??
851 + esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
852 +
853 + // Return success with filename for UI update
854 + wp_send_json([
855 + 'success' => true,
856 + 'message' => $success_text,
857 + 'data' => [
858 + 'filename' => $pdf_filename
859 + ]
860 + ]);
861 + wp_die();
862 + } else {
863 + $error_text = $this->options['pdf_intent_error_text'] ??
864 + esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
865 + $this->fallbackResponse['text'] = $error_text;
866 + }
867 +
868 + wp_send_json([
869 + 'success' => false,
870 + 'message' => $this->fallbackResponse['text']
871 + ]);
872 + wp_die();
873 + }
874 + }
875 + }
876 +
877 +
878 +// Add this before the intent check section (before Step 2) in mxchat_handle_chat_request
879 +// Check if there's an active recommendation flow session
880 +$flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
881 +if (!empty($flow_state) && isset($flow_state['flow_id'])) {
882 + //error_log('MXCHAT DEBUG: Detected active recommendation flow, routing directly');
883 +
884 + // Create a dummy intent object that matches the original intent
885 + $dummy_intent = new stdClass();
886 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
887 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
888 +
889 + // Call the recommendation flow handler directly
890 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
891 +
892 + // If the handler returned a response, send it
893 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
894 + // Save the bot's response to the chat history
895 + if (!empty($response_data['text'])) {
896 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
897 + }
898 + if (!empty($response_data['html'])) {
899 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
900 + }
901 +
902 + // Send the response
903 + wp_send_json($response_data);
904 + wp_die();
905 + }
906 +
907 + // If we reach here, the flow handler didn't provide a usable response
908 + // We'll continue with regular processing
909 + //error_log('MXCHAT DEBUG: Recommendation flow handler did not provide a usable response');
910 +}
911 +
912 + // Step 2: Detect intent and handle intent-based responses
913 +$intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
914 +//error_log("Intent Result Type: " . gettype($intent_result));
915 +
916 +// Step 3: Handle the intent result appropriately
917 +if ($intent_result !== false) {
918 + // The intent was matched and handled
919 + //error_log("Intent was matched and handled.");
920 +
921 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
922 + // Intent returned a direct response array
923 + //error_log("Intent returned a direct response.");
924 + $response_data = [
925 + 'text' => $intent_result['text'] ?? '',
926 + 'html' => $intent_result['html'] ?? '',
927 + 'session_id' => $session_id
928 + ];
929 +
930 + wp_send_json($response_data);
931 + wp_die();
932 + }
933 + else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
934 + // Intent returned true and set fallbackResponse
935 + //error_log("Intent returned true with fallbackResponse set.");
936 + $response_data = [
937 + 'text' => $this->fallbackResponse['text'] ?? '',
938 + 'html' => $this->fallbackResponse['html'] ?? '',
939 + 'session_id' => $session_id
940 + ];
941 +
942 + wp_send_json($response_data);
943 + wp_die();
944 + }
945 +
946 + // Intent was matched but no usable response was provided
947 + // This shouldn't happen with proper intent implementation
948 + //error_log("Warning: Intent matched but no response provided.");
949 +}
950 +
951 + // If we get here, no intent matched OR the intent didn't provide a usable response
952 + //error_log("No matching intent or usable response. Generating AI response.");
953 +
954 + // Step 4: Generate AI response
955 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
956 + $this->mxchat_increment_chat_count();
957 +
958 + // Generate embedding for the user's query
959 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
960 +
961 + // Check if the embedding generation returned an error
962 + if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
963 + $error_message = $user_message_embedding['error'];
964 + $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
965 +
966 + //error_log("Embedding error for session $session_id: $error_message (Code: $error_code)");
967 +
968 + // Important: Structure the error data correctly for wp_send_json_error
969 + wp_send_json_error([
970 + 'error_message' => $error_message,
971 + 'error_code' => $error_code
972 + ]);
973 + wp_die();
974 + }
975 +
976 + // Check if the embedding is valid
977 + if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
978 + //error_log("Failed to generate message embedding for session $session_id");
979 + wp_send_json_error([
980 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
981 + 'error_code' => 'invalid_embedding'
982 + ]);
983 + wp_die();
984 + }
985 +
986 + // Build context with both knowledge base and PDF content if available
987 + $context_content = "User asked: '{$message}'\n\n";
988 +
989 +
990 + // Get relevant content from knowledge base
991 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
992 + if (!empty($relevant_content)) {
993 + $context_content .= "Relevant content from knowledge database:\n" . $relevant_content . "\n\n";
994 + }
995 +
996 +
997 + // Check for and include PDF content
998 + $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
999 + $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
1000 + $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
1001 + if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
1002 + $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
1003 + if (!empty($relevant_pdf_pages)) {
1004 + $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
1005 + foreach ($relevant_pdf_pages as $page_data) {
1006 + $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
1007 + }
1008 + $context_content .= "\n";
1009 + }
1010 + }
1011 +
1012 + // Check for and include Word content
1013 + $word_url = get_transient('mxchat_word_url_' . $session_id);
1014 + $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
1015 + $word_filename = get_transient('mxchat_word_filename_' . $session_id);
1016 + if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
1017 + $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
1018 + if (!empty($relevant_word_chunks)) {
1019 + $context_content .= "Relevant content from Word document '{$word_filename}':\n";
1020 + foreach ($relevant_word_chunks as $chunk_data) {
1021 + $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
1022 + }
1023 + $context_content .= "\n";
1024 + }
1025 + }
1026 +
1027 + $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
1028 +
1029 + // Generate the response using the full context
1030 + $response = $this->mxchat_generate_response(
1031 + $context_content,
1032 + $this->options['api_key'],
1033 + $this->options['xai_api_key'],
1034 + $this->options['claude_api_key'],
1035 + $this->options['deepseek_api_key'],
1036 + $this->options['gemini_api_key'],
1037 + $conversation_history
1038 + );
1039 +
1040 + // Check if the response is an error array
1041 + if (is_array($response) && isset($response['error'])) {
1042 + //error_log("AI Response Error: " . $response['error'] . " (Code: " . ($response['error_code'] ?? 'unknown') . ")");
1043 +
1044 + // Send a user-friendly error message
1045 + wp_send_json_error([
1046 + 'error_message' => $response['error'],
1047 + 'error_code' => $response['error_code'] ?? 'api_error'
1048 + ]);
1049 + wp_die();
1050 + }
1051 +
1052 + // If we get here, the response is valid text
1053 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1054 +
1055 + // Step 5: Save additional content if available
1056 + if (!empty($this->productCardHtml)) {
1057 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
1058 + }
1059 +
1060 + if (!empty($this->fallbackResponse['html'])) {
1061 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1062 + }
1063 +
1064 + // Step 6: Return the response
1065 + $response_data = [
1066 + 'text' => $response,
1067 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1068 + 'session_id' => $session_id
1069 + ];
1070 +
1071 + wp_send_json($response_data);
1072 + wp_die();
1073 +}
1074 +
1075 +// Updated function to check intents and invoke the callback function
1076 +private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
1077 + global $wpdb;
1078 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1079 +
1080 + //error_log('🔍 MXCHAT DEBUG: Intent Check Started ==================');
1081 + //error_log("🔍 MXCHAT DEBUG: Message: '$message'");
1082 + //error_log("🔍 MXCHAT DEBUG: Chat Mode: $chat_mode");
1083 +
1084 + // Generate the user embedding
1085 + //error_log('🔄 MXCHAT DEBUG: Generating user embedding');
1086 + $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1087 +
1088 + // Check if embedding generation returned an error
1089 + if (is_array($user_embedding) && isset($user_embedding['error'])) {
1090 + $error_message = $user_embedding['error'];
1091 + $error_code = $user_embedding['error_code'] ?? 'embedding_error';
1092 +
1093 + //error_log("❌ MXCHAT DEBUG: Embedding error: $error_message (Code: $error_code)");
1094 +
1095 + // Send the error to the frontend
1096 + wp_send_json_error([
1097 + 'error_message' => $error_message,
1098 + 'error_code' => $error_code
1099 + ]);
1100 + wp_die();
1101 + }
1102 +
1103 + // Check if embedding is valid (not an error and is an array)
1104 + if (!is_array($user_embedding) || empty($user_embedding)) {
1105 + //error_log('❌ MXCHAT DEBUG: Failed to generate user embedding');
1106 +
1107 + // Send a generic error to the frontend
1108 + wp_send_json_error([
1109 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1110 + 'error_code' => 'invalid_embedding'
1111 + ]);
1112 + wp_die();
1113 + }
1114 +
1115 + //error_log('✅ MXCHAT DEBUG: User embedding generated successfully');
1116 +
1117 + // Fetch intents from the database
1118 + $table_name = $wpdb->prefix . 'mxchat_intents';
1119 + if ($chat_mode === 'agent') {
1120 + //error_log('🔍 MXCHAT DEBUG: Agent mode - fetching only chatbot switch intent');
1121 + $query = $wpdb->prepare(
1122 + "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
1123 + 'mxchat_handle_switch_to_chatbot_intent'
1124 + );
1125 + $intents = $wpdb->get_results($query);
1126 + } else {
1127 + //error_log('🔍 MXCHAT DEBUG: AI mode - fetching all enabled intents');
1128 + // Only fetch enabled intents (either explicitly enabled with 1 or implicitly enabled with NULL for backward compatibility)
1129 + $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
1130 + }
1131 +
1132 + //error_log('🔍 MXCHAT DEBUG: Found ' . count($intents) . ' enabled intents to check');
1133 +
1134 + if (empty($intents)) {
1135 + //error_log('❌ MXCHAT DEBUG: No enabled intents found in database');
1136 + return false;
1137 + }
1138 +
1139 + $highest_similarity = -INF;
1140 + $matched_intent = null;
1141 +
1142 + //error_log('📊 MXCHAT DEBUG: Intent Similarity Scores ==================');
1143 + foreach ($intents as $intent) {
1144 + //error_log("🔄 MXCHAT DEBUG: Checking intent: '{$intent->intent_label}' (callback: {$intent->callback_function})");
1145 +
1146 + // Additional check for enabled state in case database structure was modified
1147 + $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
1148 + if (!$is_enabled) {
1149 + //error_log("⚠️ MXCHAT DEBUG: Skipping disabled intent: {$intent->intent_label}");
1150 + continue;
1151 + }
1152 +
1153 + $intent_embedding_serialized = $intent->embedding_vector;
1154 + $intent_embedding = $intent_embedding_serialized
1155 + ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
1156 + : null;
1157 +
1158 + if (!is_array($intent_embedding)) {
1159 + //error_log("❌ MXCHAT DEBUG: Invalid embedding for intent: {$intent->intent_label}");
1160 + continue;
1161 + }
1162 +
1163 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
1164 + $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
1165 +
1166 + //error_log("📊 MXCHAT DEBUG: Intent '{$intent->intent_label}' similarity: {$similarity}, threshold: {$intent_threshold}");
1167 +
1168 + if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
1169 + $highest_similarity = $similarity;
1170 + $matched_intent = $intent;
1171 + //error_log("✅ MXCHAT DEBUG: New best match: '{$intent->intent_label}' with similarity {$similarity}");
1172 + }
1173 + }
1174 + //error_log('📊 MXCHAT DEBUG: End Intent Scores ==================');
1175 +
1176 + if ($matched_intent) {
1177 + //error_log("🎯 MXCHAT DEBUG: Final Intent Match: '{$matched_intent->intent_label}'");
1178 + //error_log("🔄 MXCHAT DEBUG: Invoking callback: {$matched_intent->callback_function}");
1179 +
1180 + // If the callback is a method on this instance (core callback), call it directly
1181 + if (method_exists($this, $matched_intent->callback_function)) {
1182 + //error_log('🔍 MXCHAT DEBUG: Using direct method call for core callback');
1183 + $callback_result = call_user_func(
1184 + [$this, $matched_intent->callback_function],
1185 + $message,
1186 + $user_id,
1187 + $session_id,
1188 + $matched_intent,
1189 + $user_context
1190 + );
1191 + } else {
1192 + //error_log('🔍 MXCHAT DEBUG: Using apply_filters for add-on callback');
1193 + // Otherwise, use apply_filters for add-on callbacks
1194 + $callback_result = apply_filters(
1195 + $matched_intent->callback_function,
1196 + false, // default return value
1197 + $message,
1198 + $user_id,
1199 + $session_id,
1200 + $matched_intent
1201 + );
1202 + }
1203 +
1204 + //error_log('🔍 MXCHAT DEBUG: Callback result type: ' . gettype($callback_result));
1205 + if ($callback_result !== false) {
1206 + //error_log('✅ MXCHAT DEBUG: Intent handled successfully');
1207 + $this->fallbackResponse = $callback_result;
1208 + return true;
1209 + }
1210 + //error_log('❌ MXCHAT DEBUG: Callback returned false');
1211 + } else {
1212 + //error_log('❌ MXCHAT DEBUG: No matching intent found');
1213 + }
1214 +
1215 + //error_log('🔍 MXCHAT DEBUG: Intent Check Completed ==================');
1216 + return false;
1217 +}
1218 +
1219 +// Helper function to clear PDF and Word document related transients
1220 +private function clear_pdf_transients($session_id) {
1221 + // PDF transients
1222 + delete_transient('mxchat_pdf_url_' . $session_id);
1223 + delete_transient('mxchat_pdf_embeddings_' . $session_id);
1224 + delete_transient('mxchat_include_pdf_in_context_' . $session_id);
1225 + delete_transient('mxchat_waiting_for_pdf_url_' . $session_id);
1226 +
1227 + // Word document transients
1228 + delete_transient('mxchat_word_url_' . $session_id);
1229 + delete_transient('mxchat_word_filename_' . $session_id);
1230 + delete_transient('mxchat_word_embeddings_' . $session_id);
1231 + delete_transient('mxchat_include_word_in_context_' . $session_id);
1232 + delete_transient('mxchat_waiting_for_word_' . $session_id);
1233 +}
1234 +
1235 +
1236 +
1237 +//verified good
1238 +public function mxchat_handle_email_capture($message, $user_id, $session_id) {
1239 + // Log the message safely
1240 + //error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
1241 +
1242 + // Initiate email capture flow
1243 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
1244 +
1245 + set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
1246 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1247 +
1248 + // Respond to the user
1249 + wp_send_json(['message' => $response]);
1250 + wp_die();
1251 +}
1252 +
1253 +public function mxchat_generate_image($message, $user_id, $session_id) {
1254 + //error_log("Starting image generation for message: " . $message);
1255 +
1256 + // Prepare a prompt for DALL-E
1257 + $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
1258 +
1259 + // Use the existing OpenAI API key
1260 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1261 +
1262 + // Call DALL-E to generate an image
1263 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1264 +
1265 + // Check if the response contains an image URL
1266 + if (isset($image_response['imageUrl'])) {
1267 + $image_url = esc_url_raw($image_response['imageUrl']);
1268 +
1269 + // Construct the HTML with a CSS class instead of inline styles
1270 + $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
1271 + $response_text = esc_html__('Here is the image I generated:', 'mxchat');
1272 +
1273 + // Save the bot message with both text and HTML
1274 + $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1275 + $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
1276 +
1277 + // Set the fallback response for the chat handler
1278 + $this->fallbackResponse = [
1279 + 'text' => $response_text,
1280 + 'html' => $response_html,
1281 + 'images' => [$image_url]
1282 + ];
1283 +
1284 + // For debugging/verification - Use json_encode to verify what's being set
1285 + //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
1286 +
1287 + // Return the response directly instead of relying on the property
1288 + return $this->fallbackResponse;
1289 + } else {
1290 + $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
1291 +
1292 + // Save the error message
1293 + $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1294 +
1295 + // Set the fallback response for the chat handler
1296 + $this->fallbackResponse = [
1297 + 'text' => $response_text,
1298 + 'html' => '',
1299 + 'images' => []
1300 + ];
1301 +
1302 + //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
1303 + //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
1304 +
1305 + // Return the response directly instead of relying on the property
1306 + return $this->fallbackResponse;
1307 + }
1308 +}
1309 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
1310 + $api_url = 'https://api.openai.com/v1/images/generations';
1311 + $body = json_encode([
1312 + 'prompt' => sanitize_text_field($prompt),
1313 + 'n' => 1,
1314 + 'size' => '1024x1024',
1315 + 'model' => sanitize_text_field($model),
1316 + ]);
1317 +
1318 + $args = [
1319 + 'body' => $body,
1320 + 'headers' => [
1321 + 'Content-Type' => 'application/json',
1322 + 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
1323 + ],
1324 + 'method' => 'POST',
1325 + 'timeout' => absint($timeout),
1326 + ];
1327 +
1328 + $response = wp_remote_post($api_url, $args);
1329 +
1330 + if (is_wp_error($response)) {
1331 + //error_log("DALL-E request failed: " . $response->get_error_message());
1332 + return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
1333 + }
1334 +
1335 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
1336 +
1337 + if (isset($response_body['data'][0]['url'])) {
1338 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
1339 + } else {
1340 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
1341 + return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
1342 + }
1343 +}
1344 +
1345 +/**
1346 + * Handle web search requests.
1347 + *
1348 + * Sends the refined search query to the Brave Search API and uses the
1349 + * results to generate a conversational response with the AI model.
1350 + *
1351 + * @since 1.0.0
1352 + * @param string $message The user's search query.
1353 + * @param string $user_id The user identifier.
1354 + * @param string $session_id The current session ID.
1355 + * @return array Response array containing text with embedded HTML links
1356 + */
1357 +public function mxchat_handle_search_request($message, $user_id, $session_id) {
1358 + // Step 1: Interpret and refine the search query
1359 + $refined_search_query = $this->mxchat_interpret_search_query($message);
1360 + if (empty($refined_search_query)) {
1361 + return array(
1362 + 'text' => esc_html__('I apologize, but could you please rephrase your search request?', 'mxchat'),
1363 + 'html' => ''
1364 + );
1365 + }
1366 +
1367 + // Retrieve and validate API settings
1368 + $options = get_option('mxchat_options');
1369 + $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
1370 + $results_count = isset($options['brave_results_count']) ? absint($options['brave_results_count']) : 5;
1371 +
1372 + if (empty($api_key)) {
1373 + return array(
1374 + 'text' => esc_html__('Search functionality is temporarily unavailable. Please try again later.', 'mxchat'),
1375 + 'html' => ''
1376 + );
1377 + }
1378 +
1379 + // Build the API request URL
1380 + $api_url = add_query_arg(
1381 + array(
1382 + 'q' => rawurlencode($refined_search_query),
1383 + 'count' => $results_count,
1384 + 'text_decorations' => 'true',
1385 + 'rich_data' => 'true',
1386 + ),
1387 + 'https://api.search.brave.com/res/v1/web/search'
1388 + );
1389 +
1390 + // Attempt to retrieve cached results first
1391 + $transient_key = 'mxchat_search_' . md5($refined_search_query);
1392 + $results = get_transient($transient_key);
1393 +
1394 + if (false === $results) {
1395 + // Fetch new results from the Brave Search API
1396 + $response = wp_remote_get(
1397 + $api_url,
1398 + array(
1399 + 'headers' => array(
1400 + 'Accept' => 'application/json',
1401 + 'Accept-Encoding' => 'gzip',
1402 + 'X-Subscription-Token'=> $api_key,
1403 + ),
1404 + 'timeout' => 10,
1405 + )
1406 + );
1407 +
1408 + if (is_wp_error($response)) {
1409 + return array(
1410 + 'text' => esc_html__('I encountered an error while searching. Please try again.', 'mxchat'),
1411 + 'html' => ''
1412 + );
1413 + }
1414 +
1415 + $results = json_decode(wp_remote_retrieve_body($response), true);
1416 +
1417 + if (json_last_error() !== JSON_ERROR_NONE) {
1418 + return array(
1419 + 'text' => esc_html__('I received an invalid response from the search service.', 'mxchat'),
1420 + 'html' => ''
1421 + );
1422 + }
1423 +
1424 + // Cache results for one hour
1425 + set_transient($transient_key, $results, HOUR_IN_SECONDS);
1426 + }
1427 +
1428 + // Process results
1429 + if (!empty($results['web']['results']) && is_array($results['web']['results'])) {
1430 + // Create a more straightforward summary with HTML links
1431 + $search_results_text = '';
1432 +
1433 + // Add a simple intro
1434 + $search_results_text .= sprintf(
1435 + esc_html__("Here's what I found about '%s':", 'mxchat'),
1436 + esc_html($refined_search_query)
1437 + );
1438 +
1439 + // Add the top results with HTML links
1440 + foreach (array_slice($results['web']['results'], 0, 5) as $result) {
1441 + $title = isset($result['title']) ? wp_strip_all_tags($result['title']) : '';
1442 + $url = isset($result['url']) ? esc_url($result['url']) : '';
1443 + $description = isset($result['description']) ? wp_strip_all_tags($result['description']) : '';
1444 +
1445 + // Add a line break after the intro
1446 + $search_results_text .= '<br><br>';
1447 +
1448 + // Add title as a link
1449 + $search_results_text .= sprintf(
1450 + '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a><br>',
1451 + $url,
1452 + $title
1453 + );
1454 +
1455 + // Add a condensed description
1456 + $search_results_text .= sprintf("%s", $description);
1457 + }
1458 +
1459 + // Save to chat history
1460 + $this->mxchat_save_chat_message($session_id, 'bot', $search_results_text);
1461 +
1462 + // Return the formatted text with embedded HTML links
1463 + return array(
1464 + 'text' => $search_results_text,
1465 + 'html' => ''
1466 + );
1467 + } else {
1468 + return array(
1469 + 'text' => sprintf(
1470 + esc_html__('I searched for "%s" but couldn\'t find any relevant results. Would you like to try different search terms?', 'mxchat'),
1471 + esc_html($refined_search_query)
1472 + ),
1473 + 'html' => ''
1474 + );
1475 + }
1476 +}
1477 +/**
1478 + * Format search results into a natural text summary.
1479 + *
1480 + * @since 1.0.0
1481 + * @param array $results The search results from the API.
1482 + * @param string $query The original search query.
1483 + * @return string The text summary of the top results.
1484 + */
1485 +private function format_search_results( $results, $query ) {
1486 + $summary = sprintf(
1487 + esc_html__( 'Here are the most relevant results for "%s":', 'mxchat' ),
1488 + esc_html( $query )
1489 + ) . "\n\n";
1490 +
1491 + $max_results = min( count( $results ), 3 );
1492 + for ( $i = 0; $i < $max_results; $i++ ) {
1493 + $result = $results[ $i ];
1494 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1495 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1496 +
1497 + // Append title and description to the summary
1498 + $summary .= sprintf(
1499 + "%s\n%s\n\n",
1500 + esc_html( $title ),
1501 + esc_html( $description )
1502 + );
1503 + }
1504 +
1505 + return $summary;
1506 +}
1507 +
1508 +/**
1509 + * Generate HTML markup for search results.
1510 + *
1511 + * @since 1.0.0
1512 + * @param array $results The search results from the API.
1513 + * @param string $query The user-refined query.
1514 + * @return string The HTML markup for displaying the results.
1515 + */
1516 +private function generate_search_results_html( $results, $query ) {
1517 + ob_start();
1518 + ?>
1519 + <div class="mxchat-search-results">
1520 + <?php foreach ( $results as $result ) :
1521 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1522 + $url = isset( $result['url'] ) ? esc_url( $result['url'] ) : '#';
1523 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1524 + $favicon = isset( $result['meta_url']['favicon'] ) ? esc_url( $result['meta_url']['favicon'] ) : '';
1525 + $thumbnail = isset( $result['thumbnail']['src'] ) ? esc_url( $result['thumbnail']['src'] ) : '';
1526 + $domain = parse_url( $url, PHP_URL_HOST );
1527 + ?>
1528 + <div class="mxchat-search-item">
1529 + <div class="mxchat-search-header">
1530 + <?php if ( $favicon ) : ?>
1531 + <img
1532 + src="<?php echo esc_url( $favicon ); ?>"
1533 + class="mxchat-site-icon"
1534 + alt="<?php echo esc_attr__( 'Site icon', 'mxchat' ); ?>"
1535 + width="16"
1536 + height="16"
1537 + />
1538 + <?php endif; ?>
1539 + <div class="mxchat-site-url"><?php echo esc_html( $domain ); ?></div>
1540 + </div>
1541 +
1542 + <div class="mxchat-search-content">
1543 + <h3 class="mxchat-search-title">
1544 + <a href="<?php echo esc_url( $url ); ?>"
1545 + target="_blank"
1546 + rel="noopener noreferrer"
1547 + >
1548 + <?php echo esc_html( $title ); ?>
1549 + </a>
1550 + </h3>
1551 +
1552 + <?php if ( $thumbnail ) : ?>
1553 + <div class="mxchat-search-thumbnail">
1554 + <img
1555 + src="<?php echo esc_url( $thumbnail ); ?>"
1556 + alt="<?php echo esc_attr__( 'Thumbnail image', 'mxchat' ); ?>"
1557 + loading="lazy"
1558 + />
1559 + </div>
1560 + <?php endif; ?>
1561 +
1562 + <div class="mxchat-search-description">
1563 + <?php echo esc_html( $description ); ?>
1564 + </div>
1565 + </div>
1566 + </div>
1567 + <?php endforeach; ?>
1568 + </div>
1569 + <?php
1570 + return ob_get_clean();
1571 +}
1572 +
1573 +
1574 +//very good
1575 +/**
1576 + * Handle image search requests from the chatbot
1577 + *
1578 + * @param string $message The user's search query
1579 + * @param int $user_id The user's ID
1580 + * @param string $session_id The chat session ID
1581 + * @return array Response array with text and HTML content
1582 + */
1583 +public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
1584 + // Step 1: Interpret the search query using the user's selected AI model
1585 + $refined_search_query = $this->mxchat_interpret_search_query($message);
1586 +
1587 + // If no query was interpreted, return a fallback message
1588 + if (empty($refined_search_query)) {
1589 + return array(
1590 + 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
1591 + 'html' => "",
1592 + );
1593 + }
1594 +
1595 + // Brave API URL
1596 + $api_url = 'https://api.search.brave.com/res/v1/images/search';
1597 +
1598 + // Retrieve Brave API settings
1599 + $options = get_option('mxchat_options');
1600 + $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
1601 +
1602 + if (empty($api_key)) {
1603 + return array(
1604 + 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
1605 + 'html' => "",
1606 + );
1607 + }
1608 +
1609 + $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
1610 + $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
1611 +
1612 + // Append query parameters based on settings
1613 + $api_url = add_query_arg([
1614 + 'q' => rawurlencode($refined_search_query),
1615 + 'count' => $image_count,
1616 + 'safesearch' => $safe_search,
1617 + ], $api_url);
1618 +
1619 + // Implement caching
1620 + $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
1621 + $body = get_transient($transient_key);
1622 +
1623 + if (false === $body) {
1624 + $args = [
1625 + 'headers' => [
1626 + 'Accept' => 'application/json',
1627 + 'Accept-Encoding' => 'gzip',
1628 + 'X-Subscription-Token' => $api_key,
1629 + ],
1630 + 'timeout' => 10,
1631 + ];
1632 +
1633 + $response = wp_remote_get($api_url, $args);
1634 +
1635 + if (is_wp_error($response)) {
1636 + return array(
1637 + 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
1638 + 'html' => "",
1639 + );
1640 + }
1641 +
1642 + $body = json_decode(wp_remote_retrieve_body($response), true);
1643 + set_transient($transient_key, $body, HOUR_IN_SECONDS);
1644 + }
1645 +
1646 + // Process the API response
1647 + if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
1648 + $html_output = '<div class="mxchat-image-gallery">';
1649 +
1650 + // Get the configured image count (1-6)
1651 + $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
1652 + $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
1653 +
1654 + // Use only the requested number of images
1655 + for ($i = 0; $i < $display_count; $i++) {
1656 + $image = $body['results'][$i];
1657 + $image_url = isset($image['url']) ? esc_url($image['url']) : '';
1658 + $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
1659 + $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
1660 +
1661 + if ($image_url && $thumbnail_url) {
1662 + $html_output .= '<div class="mxchat-image-item">';
1663 + $html_output .= '<strong class="mxchat-image-title">' . $title . '</strong>';
1664 + $html_output .= '<a href="' . $image_url . '" target="_blank" rel="noopener noreferrer" class="mxchat-image-link">';
1665 + $html_output .= '<img src="' . $thumbnail_url . '" alt="' . $title . '" class="mxchat-image-thumbnail">';
1666 + $html_output .= '</a></div>';
1667 + }
1668 + }
1669 +
1670 + $html_output .= '</div>';
1671 +
1672 + // Create response text
1673 + $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
1674 +
1675 + // Save both response text and HTML to chat history
1676 + $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1677 + $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
1678 +
1679 + // Return the combined response
1680 + return array(
1681 + 'text' => $response_text,
1682 + 'html' => $html_output,
1683 + );
1684 + } else {
1685 + $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
1686 +
1687 + // Save the error message to chat history
1688 + $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1689 +
1690 + return array(
1691 + 'text' => $response_text,
1692 + 'html' => "",
1693 + );
1694 + }
1695 +}
1696 +
1697 +/**
1698 + * Interpret the search query using the user's selected AI model
1699 + *
1700 + * @param string $user_query The original query from the user
1701 + * @return string The refined search query
1702 + */
1703 +public function mxchat_interpret_search_query($user_query) {
1704 + $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');
1705 +
1706 + // Get options and determine the selected model
1707 + $options = $this->options ?? get_option('mxchat_options');
1708 + $selected_model = isset($options['model']) ? $options['model'] : 'gpt-4o';
1709 +
1710 + // Extract model prefix to determine the provider
1711 + $model_parts = explode('-', $selected_model);
1712 + $provider = strtolower($model_parts[0]);
1713 +
1714 + // Determine which API key to use based on the provider
1715 + switch ($provider) {
1716 + case 'gemini':
1717 + $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
1718 + if (empty($api_key)) {
1719 + return sanitize_text_field($user_query); // Default to original query if API key missing
1720 + }
1721 + return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
1722 +
1723 + case 'claude':
1724 + $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
1725 + if (empty($api_key)) {
1726 + return sanitize_text_field($user_query);
1727 + }
1728 + return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
1729 +
1730 + case 'grok':
1731 + $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
1732 + if (empty($api_key)) {
1733 + return sanitize_text_field($user_query);
1734 + }
1735 + return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
1736 +
1737 + case 'deepseek':
1738 + $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
1739 + if (empty($api_key)) {
1740 + return sanitize_text_field($user_query);
1741 + }
1742 + return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
1743 +
1744 + case 'gpt':
1745 + default:
1746 + // Default to OpenAI for custom models or unrecognized prefixes
1747 + $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
1748 + if (empty($api_key)) {
1749 + return sanitize_text_field($user_query);
1750 + }
1751 + return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
1752 + }
1753 +}
1754 +
1755 +/**
1756 + * Interpret query using OpenAI models
1757 + */
1758 +private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-4o') {
1759 + $url = 'https://api.openai.com/v1/chat/completions';
1760 + $args = [
1761 + 'headers' => [
1762 + 'Authorization' => 'Bearer ' . $api_key,
1763 + 'Content-Type' => 'application/json',
1764 + ],
1765 + 'body' => wp_json_encode([
1766 + 'model' => $model,
1767 + 'messages' => [
1768 + ['role' => 'system', 'content' => $system_prompt],
1769 + ['role' => 'user', 'content' => sanitize_text_field($user_query)],
1770 + ],
1771 + 'temperature' => 0.2,
1772 + 'max_tokens' => 20,
1773 + ]),
1774 + 'method' => 'POST',
1775 + 'timeout' => 15,
1776 + ];
1777 +
1778 + $response = wp_remote_post($url, $args);
1779 + if (is_wp_error($response)) {
1780 + return sanitize_text_field($user_query);
1781 + }
1782 +
1783 + $body = json_decode(wp_remote_retrieve_body($response), true);
1784 + return isset($body['choices'][0]['message']['content'])
1785 + ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
1786 + : sanitize_text_field($user_query);
1787 +}
1788 +
1789 +/**
1790 + * Interpret query using Claude models
1791 + */
1792 +private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
1793 + $url = 'https://api.anthropic.com/v1/messages';
1794 +
1795 + $args = [
1796 + 'headers' => [
1797 + 'Content-Type' => 'application/json',
1798 + 'x-api-key' => $api_key,
1799 + 'anthropic-version' => '2023-06-01',
1800 + ],
1801 + 'body' => wp_json_encode([
1802 + 'model' => $model,
1803 + 'system' => $system_prompt,
1804 + 'messages' => [
1805 + ['role' => 'user', 'content' => sanitize_text_field($user_query)]
1806 + ],
1807 + 'max_tokens' => 20,
1808 + 'temperature' => 0.2,
1809 + ]),
1810 + 'method' => 'POST',
1811 + 'timeout' => 15,
1812 + ];
1813 +
1814 + $response = wp_remote_post($url, $args);
1815 + if (is_wp_error($response)) {
1816 + return sanitize_text_field($user_query);
1817 + }
1818 +
1819 + $body = json_decode(wp_remote_retrieve_body($response), true);
1820 + if (!empty($body['content'][0]['text'])) {
1821 + return sanitize_text_field(trim($body['content'][0]['text']));
1822 + }
1823 +
1824 + return sanitize_text_field($user_query);
1825 +}
1826 +
1827 +/**
1828 + * Interpret query using Gemini models
1829 + */
1830 +private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
1831 + // Strip "gemini-" prefix for the API
1832 + $model_version = str_replace('gemini-', '', $model);
1833 +
1834 + $url = "https://generativelanguage.googleapis.com/v1/models/$model_version:generateContent?key=" . urlencode($api_key);
1835 +
1836 + $args = [
1837 + 'headers' => [
1838 + 'Content-Type' => 'application/json',
1839 + ],
1840 + 'body' => wp_json_encode([
1841 + 'contents' => [
1842 + [
1843 + 'role' => 'user',
1844 + 'parts' => [
1845 + ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
1846 + ]
1847 + ]
1848 + ],
1849 + 'generationConfig' => [
1850 + 'temperature' => 0.2,
1851 + 'maxOutputTokens' => 20,
1852 + ],
1853 + ]),
1854 + 'method' => 'POST',
1855 + 'timeout' => 15,
1856 + ];
1857 +
1858 + $response = wp_remote_post($url, $args);
1859 + if (is_wp_error($response)) {
1860 + return sanitize_text_field($user_query);
1861 + }
1862 +
1863 + $body = json_decode(wp_remote_retrieve_body($response), true);
1864 + if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
1865 + return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
1866 + }
1867 +
1868 + return sanitize_text_field($user_query);
1869 +}
1870 +
1871 +/**
1872 + * Interpret query using X.AI (Grok) models
1873 + */
1874 +private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
1875 + $url = 'https://api.xai.com/v1/chat/completions';
1876 +
1877 + $args = [
1878 + 'headers' => [
1879 + 'Content-Type' => 'application/json',
1880 + 'Authorization' => 'Bearer ' . $api_key,
1881 + ],
1882 + 'body' => wp_json_encode([
1883 + 'model' => $model,
1884 + 'messages' => [
1885 + ['role' => 'system', 'content' => $system_prompt],
1886 + ['role' => 'user', 'content' => sanitize_text_field($user_query)],
1887 + ],
1888 + 'temperature' => 0.2,
1889 + 'max_tokens' => 20,
1890 + ]),
1891 + 'method' => 'POST',
1892 + 'timeout' => 15,
1893 + ];
1894 +
1895 + $response = wp_remote_post($url, $args);
1896 + if (is_wp_error($response)) {
1897 + return sanitize_text_field($user_query);
1898 + }
1899 +
1900 + $body = json_decode(wp_remote_retrieve_body($response), true);
1901 + if (isset($body['choices'][0]['message']['content'])) {
1902 + return sanitize_text_field(trim($body['choices'][0]['message']['content']));
1903 + }
1904 +
1905 + return sanitize_text_field($user_query);
1906 +}
1907 +
1908 +/**
1909 + * Interpret query using DeepSeek models
1910 + */
1911 +private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
1912 + $url = 'https://api.deepseek.com/v1/chat/completions';
1913 +
1914 + $args = [
1915 + 'headers' => [
1916 + 'Content-Type' => 'application/json',
1917 + 'Authorization' => 'Bearer ' . $api_key,
1918 + ],
1919 + 'body' => wp_json_encode([
1920 + 'model' => $model,
1921 + 'messages' => [
1922 + ['role' => 'system', 'content' => $system_prompt],
1923 + ['role' => 'user', 'content' => sanitize_text_field($user_query)],
1924 + ],
1925 + 'temperature' => 0.2,
1926 + 'max_tokens' => 20,
1927 + ]),
1928 + 'method' => 'POST',
1929 + 'timeout' => 15,
1930 + ];
1931 +
1932 + $response = wp_remote_post($url, $args);
1933 + if (is_wp_error($response)) {
1934 + return sanitize_text_field($user_query);
1935 + }
1936 +
1937 + $body = json_decode(wp_remote_retrieve_body($response), true);
1938 + if (isset($body['choices'][0]['message']['content'])) {
1939 + return sanitize_text_field(trim($body['choices'][0]['message']['content']));
1940 + }
1941 +
1942 + return sanitize_text_field($user_query);
1943 +}
1944 +
1945 +
1946 +
1947 +private function find_product_in_message($message) {
1948 + global $wpdb;
1949 +
1950 + // Get embedding for the search query
1951 + $query_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1952 +
1953 + // Check if embedding generation returned an error
1954 + if (is_array($query_embedding) && isset($query_embedding['error'])) {
1955 + $error_message = $query_embedding['error'];
1956 + $error_code = $query_embedding['error_code'] ?? 'embedding_error';
1957 +
1958 + //error_log("Product search embedding error: $error_message (Code: $error_code)");
1959 +
1960 + // Set a user-friendly fallback response
1961 + $this->fallbackResponse['text'] = esc_html__("I'm having trouble processing your product search. Please try again later or contact support if this persists.", 'mxchat');
1962 +
1963 + // Also store the technical error for admin users
1964 + $this->fallbackResponse['admin_error'] = $error_message;
1965 + $this->fallbackResponse['error_code'] = $error_code;
1966 +
1967 + return null;
1968 + }
1969 +
1970 + // Check if embedding is valid
1971 + if (!is_array($query_embedding) || empty($query_embedding)) {
1972 + //error_log("Failed to generate embedding for product search");
1973 + $this->fallbackResponse['text'] = esc_html__("I couldn't process your product search. Please try again with different wording.", 'mxchat');
1974 + return null;
1975 + }
1976 +
1977 + // Get relevant content as string
1978 + $relevant_content = $this->mxchat_find_relevant_products($query_embedding);
1979 + if (empty($relevant_content)) {
1980 + // Return null to indicate no results and set fallback response
1981 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Could you please be more specific about the product you're looking for?", 'mxchat');
1982 + return null;
1983 + }
1984 +
1985 +
1986 + // Extract product URLs from the content
1987 + preg_match_all('/https?:\/\/[^\s<>"\']+?\/product\/[^\s<>"\']+/', $relevant_content, $matches);
1988 +
1989 + if (!empty($matches[0])) {
1990 + // Try each URL found
1991 + foreach ($matches[0] as $url) {
1992 + // Clean the URL
1993 + $url = rtrim($url, '/."\']');
1994 +
1995 + // Get the product slug
1996 + $path = parse_url($url, PHP_URL_PATH);
1997 + $slug = basename(rtrim($path, '/'));
1998 +
1999 + // Find product by slug
2000 + $args = array(
2001 + 'post_type' => 'product',
2002 + 'post_status' => 'publish',
2003 + 'name' => $slug,
2004 + 'posts_per_page' => 1
2005 + );
2006 +
2007 + $products = get_posts($args);
2008 +
2009 + if (!empty($products)) {
2010 + $product_id = $products[0]->ID;
2011 + $product = wc_get_product($product_id);
2012 +
2013 + if ($product && $product->is_purchasable()) {
2014 + return $product_id;
2015 + }
2016 + }
2017 + }
2018 + }
2019 +
2020 + // Fallback: Look for product names in the content
2021 + $products = wc_get_products([
2022 + 'status' => 'publish',
2023 + 'limit' => -1,
2024 + 'return' => 'all'
2025 + ]);
2026 +
2027 + foreach ($products as $product) {
2028 + $name = $product->get_name();
2029 + if (stripos($relevant_content, $name) !== false) {
2030 + if ($product->is_purchasable()) {
2031 + return $product->get_id();
2032 + }
2033 + }
2034 + }
2035 +
2036 + // If no product is found after all checks, set the fallback response
2037 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Try to be more specific", 'mxchat');
2038 + return null;
2039 +}
2040 +
2041 +// New method to handle intent responses
2042 +private function generate_intent_response($context_content, $session_id) {
2043 + // Convert the context array to a structured string for the AI
2044 + $context_string = $this->format_intent_context($context_content);
2045 + // Generate AI response using the context
2046 + $response = $this->mxchat_generate_response(
2047 + $context_string,
2048 + $this->options['api_key'],
2049 + $this->options['xai_api_key'],
2050 + $this->options['claude_api_key'],
2051 + $this->options['deepseek_api_key'],
2052 + $this->options['gemini_api_key'], // Added Gemini API key
2053 + $this->mxchat_fetch_conversation_history_for_ai($session_id)
2054 + );
2055 + $this->fallbackResponse['text'] = $response;
2056 + return true;
2057 +}
2058 +
2059 +// Helper method to format intent context
2060 +private function format_intent_context($context) {
2061 + $context_string = esc_html__("INTENT CONTEXT:\n", 'mxchat');
2062 +
2063 + switch ($context['intent']) {
2064 + case 'add_to_cart':
2065 + if ($context['status'] === 'success') {
2066 + $context_string .= esc_html__("Action: Successfully added product to cart\n", 'mxchat');
2067 + $context_string .= sprintf(esc_html__("Product: %s\n", 'mxchat'), $context['product']['name']);
2068 + $context_string .= esc_html__("Available actions: ", 'mxchat') . implode(', ', $context['available_actions']) . "\n";
2069 + $context_string .= sprintf(esc_html__("Cart URL: %s\n", 'mxchat'), $context['cart_url']);
2070 + $context_string .= esc_html__("\nPlease inform the user of the successful addition and their available options.", 'mxchat');
2071 + } else {
2072 + $context_string .= esc_html__("Action: Failed to add product to cart\n", 'mxchat');
2073 + $context_string .= sprintf(esc_html__("Reason: %s\n", 'mxchat'), $context['reason']);
2074 + switch ($context['reason']) {
2075 + case 'woocommerce_not_available':
2076 + $context_string .= esc_html__("\nPlease inform the user that shopping features are not available.", 'mxchat');
2077 + break;
2078 + case 'no_product_context':
2079 + $context_string .= esc_html__("\nPlease ask the user to specify which product they want to add.", 'mxchat');
2080 + break;
2081 + case 'product_not_found':
2082 + $context_string .= esc_html__("\nPlease inform the user that the product couldn't be found and ask them to try again.", 'mxchat');
2083 + break;
2084 + case 'add_to_cart_failed':
2085 + $context_string .= esc_html__("\nPlease apologize to the user and suggest they try again or ask for assistance.", 'mxchat');
2086 + break;
2087 + }
2088 + }
2089 + break;
2090 + }
2091 +
2092 + return $context_string;
2093 +}
2094 +
2095 +
2096 +//very good
2097 +private function add_email_to_loops($email) {
2098 + // Sanitize the email
2099 + $email = sanitize_email($email);
2100 +
2101 + // Retrieve and sanitize options
2102 + $api_key = isset($this->options['loops_api_key']) ? sanitize_text_field($this->options['loops_api_key']) : '';
2103 + $mailing_list_id = isset($this->options['loops_mailing_list']) ? sanitize_text_field($this->options['loops_mailing_list']) : '';
2104 +
2105 + // Check for missing API key or mailing list ID
2106 + if (empty($api_key) || empty($mailing_list_id)) {
2107 + //error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
2108 + return;
2109 + }
2110 +
2111 + $data = array(
2112 + 'email' => $email,
2113 + 'subscribed' => true,
2114 + 'source' => __('MxChat AI Chatbot', 'mxchat'),
2115 + 'mailingLists' => array($mailing_list_id => true),
2116 + );
2117 +
2118 + $url = 'https://app.loops.so/api/v1/contacts/create';
2119 + $args = array(
2120 + 'body' => wp_json_encode($data),
2121 + 'headers' => array(
2122 + 'Authorization' => 'Bearer ' . $api_key,
2123 + 'Content-Type' => 'application/json',
2124 + ),
2125 + 'method' => 'POST',
2126 + 'timeout' => 45,
2127 + );
2128 +
2129 + $response = wp_remote_post($url, $args);
2130 +
2131 + // Handle errors in the API request
2132 + if (is_wp_error($response)) {
2133 + //error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
2134 + return;
2135 + }
2136 +
2137 + // Check for non-200 HTTP responses
2138 + $response_code = wp_remote_retrieve_response_code($response);
2139 + if ($response_code != 200) {
2140 + $response_body = wp_remote_retrieve_body($response);
2141 + //error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
2142 + }
2143 +}
2144 +
2145 +public function mxchat_handle_pdf_discussion($message, $user_id, $session_id) {
2146 + // Get the maximum number of pages allowed from admin settings
2147 + $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
2148 +
2149 + // Retrieve options for dynamic texts
2150 + $trigger_text = $this->options['pdf_intent_trigger_text'] ?? __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat');
2151 + $success_text = $this->options['pdf_intent_success_text'] ?? __("I've processed the PDF. What questions do you have about it?", 'mxchat');
2152 + $error_text = $this->options['pdf_intent_error_text'] ?? __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
2153 +
2154 + // Check for explicit request for new PDF
2155 + $new_pdf_requested = stripos($message, 'new') !== false ||
2156 + stripos($message, 'another') !== false ||
2157 + stripos($message, 'different') !== false;
2158 +
2159 + // If user mentions adding/reading a PDF, set waiting flag
2160 + if (stripos($message, 'pdf') !== false ||
2161 + stripos($message, 'document') !== false ||
2162 + stripos($message, 'read') !== false) {
2163 + set_transient('mxchat_waiting_for_pdf_url_' . $session_id, true, HOUR_IN_SECONDS);
2164 + $this->fallbackResponse['text'] = $trigger_text;
2165 + return;
2166 + }
2167 +
2168 + // If we're waiting for a URL or user requested new PDF
2169 + if ($new_pdf_requested || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
2170 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
2171 + // Process URL... (rest of your existing URL processing code)
2172 + } else {
2173 + $this->fallbackResponse['text'] = $trigger_text;
2174 + }
2175 + return;
2176 + }
2177 +
2178 + // Default to proceeding with conversation if no specific PDF action is needed
2179 + $this->fallbackResponse['text'] = '';
2180 +}
2181 +
2182 +
2183 +private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
2184 + $upload_dir = wp_upload_dir();
2185 + $temp_file = null;
2186 +
2187 + try {
2188 + // Handle URL vs local file
2189 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
2190 + // Validate and download the file from URL
2191 + $temp_file = wp_tempnam($pdf_source); // Safe temporary file name
2192 + $response = wp_remote_get($pdf_source, ['timeout' => 60]);
2193 +
2194 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
2195 + //error_log(esc_html__("Failed to download PDF. Error: ", 'mxchat') . print_r($response, true));
2196 + return false;
2197 + }
2198 +
2199 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
2200 +
2201 + // Validate that the downloaded file is a PDF
2202 + $mime_type = mime_content_type($temp_file);
2203 + if ($mime_type !== 'application/pdf') {
2204 + //error_log(esc_html__("Invalid MIME type detected for PDF: ", 'mxchat') . $mime_type);
2205 + unlink($temp_file);
2206 + return false;
2207 + }
2208 + } else {
2209 + // For local files, use the provided path directly
2210 + $temp_file = $pdf_source;
2211 + }
2212 +
2213 + // Parse and process the PDF
2214 + $parser = new \Smalot\PdfParser\Parser();
2215 + $pdf = $parser->parseFile($temp_file);
2216 + $pages = $pdf->getPages();
2217 +
2218 + if (count($pages) > $max_pages) {
2219 + //error_log(esc_html__("PDF exceeds the maximum allowed pages: ", 'mxchat') . count($pages));
2220 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
2221 + unlink($temp_file);
2222 + }
2223 + return esc_html__('too_many_pages', 'mxchat');
2224 + }
2225 +
2226 + $embeddings = [];
2227 + foreach ($pages as $page_number => $page) {
2228 + $text = $page->getText();
2229 +
2230 + // Ensure text is non-empty before generating embeddings
2231 + if (empty(trim($text))) {
2232 + //error_log(esc_html__("Skipping empty page: ", 'mxchat') . ($page_number + 1));
2233 + continue;
2234 + }
2235 +
2236 + $embedding = $this->mxchat_generate_embedding(
2237 + esc_html__("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
2238 + $this->options['api_key']
2239 + );
2240 +
2241 + if ($embedding) {
2242 + $embeddings[] = [
2243 + 'page_number' => $page_number + 1,
2244 + 'embedding' => $embedding,
2245 + 'text' => $text,
2246 + ];
2247 + } else {
2248 + //error_log(esc_html__("Failed to generate embedding for page ", 'mxchat') . ($page_number + 1));
2249 + }
2250 + }
2251 +
2252 + // Clean up downloaded file if it was from URL
2253 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
2254 + unlink($temp_file);
2255 + }
2256 +
2257 + return $embeddings;
2258 +
2259 + } catch (\Exception $e) {
2260 + // //error_log(esc_html__("Error parsing or processing PDF: ", 'mxchat') . $e->getMessage());
2261 +
2262 + // Cleanup in case of exception
2263 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
2264 + unlink($temp_file);
2265 + }
2266 +
2267 + return false;
2268 + }
2269 +}
2270 +private function find_relevant_pdf_pages($query_embedding, $embeddings) {
2271 + //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
2272 +
2273 + $most_relevant = null;
2274 + $highest_similarity = -INF;
2275 +
2276 + foreach ($embeddings as $page_data) {
2277 + $similarity = $this->mxchat_calculate_cosine_similarity($query_embedding, $page_data['embedding']);
2278 +
2279 + if ($similarity > $highest_similarity) {
2280 + $highest_similarity = $similarity;
2281 + $most_relevant = $page_data['page_number'];
2282 + }
2283 + }
2284 +
2285 + if (!is_null($most_relevant)) {
2286 + $page_numbers = range(max(1, $most_relevant - 1), min(count($embeddings), $most_relevant + 1));
2287 + return array_filter($embeddings, function ($page) use ($page_numbers) {
2288 + return in_array($page['page_number'], $page_numbers);
2289 + });
2290 + }
2291 +
2292 + return [];
2293 +}
2294 +// Add this to your class
2295 +public function handle_pdf_upload() {
2296 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
2297 +
2298 + if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
2299 + wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
2300 + return;
2301 + }
2302 +
2303 + $file = $_FILES['pdf_file'];
2304 + $session_id = sanitize_text_field($_POST['session_id']);
2305 + $original_filename = sanitize_text_field($file['name']);
2306 +
2307 + $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
2308 + if ($file_type['type'] !== 'application/pdf') {
2309 + wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
2310 + return;
2311 + }
2312 +
2313 + $upload_dir = wp_upload_dir();
2314 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
2315 + $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
2316 +
2317 + if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
2318 + wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
2319 + return;
2320 + }
2321 +
2322 + $this->clear_pdf_transients($session_id);
2323 +
2324 + $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
2325 + $embeddings = $this->fetch_and_split_pdf_pages($pdf_path, $max_pages);
2326 +
2327 + if ($embeddings === 'too_many_pages') {
2328 + unlink($pdf_path);
2329 + $error_message = sprintf(
2330 + $this->options['pdf_intent_error_text'] ??
2331 + esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
2332 + $max_pages
2333 + );
2334 + wp_send_json_error($error_message);
2335 + return;
2336 + }
2337 +
2338 + if ($embeddings === false || empty($embeddings)) {
2339 + unlink($pdf_path);
2340 + $error_message = $this->options['pdf_intent_error_text'] ??
2341 + esc_html__('The uploaded PDF appears to be empty or contains unsupported content.', 'mxchat');
2342 + wp_send_json_error($error_message);
2343 + return;
2344 + }
2345 +
2346 + if (!empty($embeddings)) {
2347 + set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
2348 + set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
2349 + set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
2350 + set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
2351 +
2352 + $success_message = $this->options['pdf_intent_success_text'] ??
2353 + esc_html__("I've processed the PDF. What questions do you have about it?", 'mxchat');
2354 +
2355 + wp_send_json_success([
2356 + 'message' => $success_message,
2357 + 'filename' => $original_filename
2358 + ]);
2359 + return;
2360 + }
2361 +
2362 + unlink($pdf_path);
2363 + $error_message = $this->options['pdf_intent_error_text'] ??
2364 + esc_html__('Sorry, I couldn\'t process the PDF. Please ensure it\'s a valid file.', 'mxchat');
2365 + wp_send_json_error($error_message);
2366 + return;
2367 +}
2368 +public function handle_pdf_remove() {
2369 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
2370 +
2371 + if (empty($_POST['session_id'])) {
2372 + wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
2373 + wp_die();
2374 + }
2375 +
2376 + $session_id = sanitize_text_field($_POST['session_id']);
2377 + $pdf_path = get_transient('mxchat_pdf_url_' . $session_id);
2378 +
2379 + if ($pdf_path && file_exists($pdf_path)) {
2380 + unlink($pdf_path);
2381 + }
2382 +
2383 + $this->clear_pdf_transients($session_id);
2384 +
2385 + wp_send_json_success([
2386 + 'message' => esc_html__('PDF removed successfully.', 'mxchat')
2387 + ]);
2388 + wp_die();
2389 +}
2390 +
2391 +
2392 +
2393 +
2394 +function mxchat_fetch_new_messages() {
2395 + $session_id = sanitize_text_field($_POST['session_id']);
2396 + $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
2397 + $persistence_enabled = $_POST['persistence_enabled'] === 'true';
2398 + $initial_timestamp = isset($_POST['initial_timestamp']) ? intval($_POST['initial_timestamp']) : 0;
2399 +
2400 + if (empty($session_id)) {
2401 + //error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
2402 + wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
2403 + wp_die();
2404 + }
2405 +
2406 + $history = get_option("mxchat_history_{$session_id}", []);
2407 +
2408 + $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
2409 + // If persistence is enabled, show all new messages
2410 + if ($persistence_enabled) {
2411 + return !empty($message['id']) &&
2412 + strcmp($message['id'], $last_seen_id) > 0 &&
2413 + $message['role'] === 'agent';
2414 + }
2415 +
2416 + // If persistence is disabled, only show messages after initial timestamp
2417 + return !empty($message['id']) &&
2418 + $message['role'] === 'agent' &&
2419 + $message['timestamp'] > $initial_timestamp;
2420 + });
2421 +
2422 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
2423 +
2424 + wp_send_json_success([
2425 + 'new_messages' => array_values($new_messages)
2426 + ]);
2427 + wp_die();
2428 +}
2429 +
2430 +
2431 +public function mxchat_live_agent_handover($message, $user_id, $session_id) {
2432 + // First check if live agents are available
2433 + $live_agent_available = $this->options['live_agent_status'] ?? 'off';
2434 + if ($live_agent_available !== 'on') {
2435 + $away_message = $this->options['live_agent_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
2436 + $this->fallbackResponse = [
2437 + 'text' => $away_message,
2438 + 'html' => '',
2439 + 'images' => [],
2440 + 'chat_mode' => 'ai'
2441 + ];
2442 + wp_send_json([
2443 + 'text' => $away_message,
2444 + 'html' => '',
2445 + 'chat_mode' => 'ai',
2446 + 'session_id' => $session_id
2447 + ]);
2448 + wp_die();
2449 + }
2450 +
2451 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2452 + if (empty($slack_webhook_url)) {
2453 + return false;
2454 + }
2455 +
2456 + // Get recent chat history (last 5 messages)
2457 + $history = get_option("mxchat_history_{$session_id}", []);
2458 + $recent_history = array_slice($history, -5); // Get last 5 messages
2459 +
2460 + // Format conversation history
2461 + $conversation_context = "";
2462 + if (!empty($recent_history)) {
2463 + $conversation_context = "*Recent Conversation:*\n";
2464 + foreach ($recent_history as $hist_message) {
2465 + $role_display = $hist_message['role'] === 'user' ? 'User' : 'AI';
2466 + $conversation_context .= ">{$role_display}: {$hist_message['content']}\n";
2467 + }
2468 + $conversation_context .= "\n";
2469 + }
2470 +
2471 + update_option("mxchat_mode_{$session_id}", 'agent');
2472 +
2473 + $webhook_data = [
2474 + 'blocks' => [
2475 + [
2476 + 'type' => 'header',
2477 + 'text' => [
2478 + 'type' => 'plain_text',
2479 + 'text' => '🔔 New Live Agent Request',
2480 + 'emoji' => true
2481 + ]
2482 + ],
2483 + [
2484 + 'type' => 'section',
2485 + 'fields' => [
2486 + [
2487 + 'type' => 'mrkdwn',
2488 + 'text' => sprintf('*User ID:*\n`%s`', $user_id)
2489 + ],
2490 + [
2491 + 'type' => 'mrkdwn',
2492 + 'text' => sprintf('*Session ID:*\n`%s`', $session_id)
2493 + ]
2494 + ]
2495 + ]
2496 + ]
2497 + ];
2498 +
2499 + // Add conversation history if exists
2500 + if (!empty($conversation_context)) {
2501 + $webhook_data['blocks'][] = [
2502 + 'type' => 'section',
2503 + 'text' => [
2504 + 'type' => 'mrkdwn',
2505 + 'text' => $conversation_context
2506 + ]
2507 + ];
2508 + }
2509 +
2510 + // Add the current message
2511 + $webhook_data['blocks'][] = [
2512 + 'type' => 'section',
2513 + 'text' => [
2514 + 'type' => 'mrkdwn',
2515 + 'text' => sprintf('*Current Message:*\n%s', $message)
2516 + ]
2517 + ];
2518 +
2519 + // Add the reply button
2520 + $webhook_data['blocks'][] = [
2521 + 'type' => 'actions',
2522 + 'elements' => [
2523 + [
2524 + 'type' => 'button',
2525 + 'text' => [
2526 + 'type' => 'plain_text',
2527 + 'text' => '✍️ Reply',
2528 + 'emoji' => true
2529 + ],
2530 + 'value' => $session_id,
2531 + 'action_id' => 'reply_to_user',
2532 + 'style' => 'primary'
2533 + ]
2534 + ]
2535 + ];
2536 +
2537 + $response = wp_remote_post($slack_webhook_url, [
2538 + 'body' => json_encode($webhook_data),
2539 + 'headers' => [
2540 + 'Content-Type' => 'application/json',
2541 + ],
2542 + ]);
2543 +
2544 + if (is_wp_error($response)) {
2545 + return false;
2546 + }
2547 +
2548 + $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
2549 + $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
2550 +
2551 + $this->fallbackResponse = [
2552 + 'text' => $success_message,
2553 + 'html' => '',
2554 + 'images' => [],
2555 + 'chat_mode' => 'agent'
2556 + ];
2557 +
2558 + wp_send_json([
2559 + 'success' => true,
2560 + 'text' => $success_message,
2561 + 'html' => '',
2562 + 'chat_mode' => 'agent',
2563 + 'session_id' => $session_id,
2564 + 'fallbackResponse' => $this->fallbackResponse
2565 + ]);
2566 + wp_die();
2567 +}
2568 +public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
2569 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2570 +
2571 + if (empty($slack_webhook_url)) {
2572 + //error_log(esc_html__('Slack Webhook URL is not configured.', 'mxchat'));
2573 + return false;
2574 + }
2575 +
2576 + $webhook_data = [
2577 + 'blocks' => [
2578 + [
2579 + 'type' => 'header',
2580 + 'text' => [
2581 + 'type' => 'plain_text',
2582 + 'text' => esc_html__('📩 New Chat Message', 'mxchat'),
2583 + 'emoji' => true
2584 + ]
2585 + ],
2586 + [
2587 + 'type' => 'section',
2588 + 'fields' => [
2589 + [
2590 + 'type' => 'mrkdwn',
2591 + 'text' => sprintf(esc_html__('*User ID:*\n`%s`', 'mxchat'), $user_id)
2592 + ],
2593 + [
2594 + 'type' => 'mrkdwn',
2595 + 'text' => sprintf(esc_html__('*Session ID:*\n`%s`', 'mxchat'), $session_id)
2596 + ]
2597 + ]
2598 + ],
2599 + [
2600 + 'type' => 'section',
2601 + 'text' => [
2602 + 'type' => 'mrkdwn',
2603 + 'text' => sprintf(esc_html__('*Message:*\n%s', 'mxchat'), $message)
2604 + ]
2605 + ],
2606 + [
2607 + 'type' => 'actions',
2608 + 'elements' => [
2609 + [
2610 + 'type' => 'button',
2611 + 'text' => [
2612 + 'type' => 'plain_text',
2613 + 'text' => esc_html__('✍️ Reply', 'mxchat'),
2614 + 'emoji' => true
2615 + ],
2616 + 'value' => $session_id,
2617 + 'action_id' => 'reply_to_user',
2618 + 'style' => 'primary'
2619 + ]
2620 + ]
2621 + ]
2622 + ]
2623 + ];
2624 +
2625 + $response = wp_remote_post($slack_webhook_url, [
2626 + 'body' => json_encode($webhook_data),
2627 + 'headers' => [
2628 + 'Content-Type' => 'application/json',
2629 + ],
2630 + ]);
2631 +
2632 + if (is_wp_error($response)) {
2633 + //error_log(esc_html__('Error sending message to Slack: ', 'mxchat') . $response->get_error_message());
2634 + return false;
2635 + }
2636 +
2637 + //error_log(esc_html__('Message sent to Slack successfully.', 'mxchat'));
2638 + return true;
2639 +}
2640 +public function handle_slack_interaction(WP_REST_Request $request) {
2641 + //error_log('Received Slack interaction');
2642 +
2643 + $payload = json_decode($request->get_param('payload'), true);
2644 + //error_log('Payload: ' . print_r($payload, true));
2645 +
2646 + // Handle button click
2647 + if ($payload['type'] === 'block_actions' && $payload['actions'][0]['action_id'] === 'reply_to_user') {
2648 + $session_id = $payload['actions'][0]['value'];
2649 + $trigger_id = $payload['trigger_id'];
2650 +
2651 + // Get Bot Token from settings
2652 + $slack_token = $this->options['live_agent_bot_token'] ?? '';
2653 +
2654 + if (empty($slack_token)) {
2655 + //error_log('Slack Bot Token not configured');
2656 + return new WP_REST_Response(['error' => esc_html__('Bot token not configured', 'mxchat')], 400);
2657 + }
2658 + $response = wp_remote_post('https://slack.com/api/views.open', [
2659 + 'headers' => [
2660 + 'Content-Type' => 'application/json',
2661 + 'Authorization' => 'Bearer ' . $slack_token
2662 + ],
2663 + 'body' => json_encode([
2664 + 'trigger_id' => $trigger_id,
2665 + 'view' => [
2666 + 'type' => 'modal',
2667 + 'callback_id' => 'reply_modal',
2668 + 'title' => [
2669 + 'type' => 'plain_text',
2670 + 'text' => __('Reply to User', 'mxchat')
2671 + ],
2672 + 'submit' => [
2673 + 'type' => 'plain_text',
2674 + 'text' => __('Send', 'mxchat')
2675 + ],
2676 + 'close' => [
2677 + 'type' => 'plain_text',
2678 + 'text' => __('Cancel', 'mxchat')
2679 + ],
2680 + 'blocks' => [
2681 + [
2682 + 'type' => 'input',
2683 + 'block_id' => 'reply_block',
2684 + 'label' => [
2685 + 'type' => 'plain_text',
2686 + 'text' => sprintf(__('Reply to session: %s', 'mxchat'), $session_id)
2687 + ],
2688 + 'element' => [
2689 + 'type' => 'plain_text_input',
2690 + 'action_id' => 'message',
2691 + 'multiline' => true,
2692 + 'placeholder' => [
2693 + 'type' => 'plain_text',
2694 + 'text' => __('Type your message here...', 'mxchat')
2695 + ]
2696 + ]
2697 + ]
2698 + ],
2699 + 'private_metadata' => $session_id
2700 + ]
2701 + ])
2702 + ]);
2703 +
2704 + //error_log('Views.open response: ' . print_r($response, true));
2705 +
2706 + // Return immediate acknowledgment
2707 + return new WP_REST_Response(['ok' => true]);
2708 + }
2709 +
2710 + // Handle modal submission
2711 +// Handle modal submission
2712 +if ($payload['type'] === 'view_submission') {
2713 + $session_id = $payload['view']['private_metadata'];
2714 + $message = $payload['view']['state']['values']['reply_block']['message']['value'];
2715 +
2716 + // Save the message (keep the message_id but don't include in response)
2717 + $this->mxchat_save_chat_message($session_id, 'agent', $message);
2718 +
2719 + // Keep the original response format for Slack
2720 + return new WP_REST_Response([
2721 + 'response_action' => 'clear'
2722 + ]);
2723 +}
2724 +
2725 + // Default acknowledgment
2726 + return new WP_REST_Response(['ok' => true]);
2727 +}
2728 +
2729 +public function mxchat_handle_agent_response(WP_REST_Request $request) {
2730 + //error_log('Received agent response request');
2731 + //error_log('Request data: ' . print_r($request->get_params(), true));
2732 + // //error_log('Raw body: ' . file_get_contents('php://input'));
2733 +
2734 + // Get the data from Slack's slash command format
2735 + $command_text = $request->get_param('text');
2736 + // //error_log('Command text: ' . $command_text);
2737 +
2738 + if (empty($command_text)) {
2739 + //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
2740 + return new WP_REST_Response([
2741 + 'error' => esc_html__('Command text is required. Format: /reply session_id message', 'mxchat')
2742 + ], 400);
2743 + }
2744 +
2745 + // Split the command text into session_id and message
2746 + $parts = explode(' ', $command_text, 2);
2747 + if (count($parts) !== 2) {
2748 + //error_log('Agent response error: Invalid command format');
2749 + return new WP_REST_Response([
2750 + 'error' => esc_html__('Invalid format. Use: /reply session_id message', 'mxchat')
2751 + ], 400);
2752 + }
2753 +
2754 + $session_id = sanitize_text_field($parts[0]);
2755 + $message = sanitize_text_field($parts[1]);
2756 +
2757 + //error_log("Processing agent response - Session ID: $session_id, Message: $message");
2758 +
2759 + // Save the message
2760 + $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
2761 +
2762 + if (!$message_id) {
2763 + // //error_log('Failed to save agent message');
2764 + return new WP_REST_Response([
2765 + 'error' => esc_html__('Failed to save message', 'mxchat')
2766 + ], 500);
2767 + }
2768 +
2769 + // Return success response in Slack's expected format
2770 + return new WP_REST_Response([
2771 + 'response_type' => 'in_channel',
2772 + 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
2773 + ], 200);
2774 +}
2775 +
2776 +
2777 +public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
2778 + //error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
2779 +
2780 + // Just update mode to AI
2781 + update_option("mxchat_mode_{$session_id}", 'ai');
2782 +
2783 + // Initialize states
2784 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2785 + $this->productCardHtml = '';
2786 +
2787 + // Set the response message
2788 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
2789 +
2790 + return true; // Intent was handled
2791 +}
2792 +
2793 +
2794 +
2795 +
2796 +// For the word upload handler
2797 +public function mxchat_handle_word_upload() {
2798 + // Delegate to word handler
2799 + $this->word_handler->mxchat_handle_word_upload();
2800 +}
2801 +
2802 +// For the word removal handler
2803 +public function mxchat_handle_word_remove() {
2804 + // Delegate to word handler
2805 + $this->word_handler->mxchat_handle_word_remove();
2806 +}
2807 +
2808 +// For the word status check
2809 +public function mxchat_check_word_status() {
2810 + // Delegate to word handler
2811 + $this->word_handler->mxchat_check_word_status();
2812 +}
2813 +
2814 +
2815 +private function mxchat_get_user_identifier() {
2816 + return MxChat_User::mxchat_get_user_identifier();
2817 +}
2818 +
2819 +private function mxchat_generate_embedding($text, $api_key) {
2820 + try {
2821 + // Get options and selected model
2822 + $options = get_option('mxchat_options');
2823 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2824 +
2825 + // Determine endpoint and API key based on model
2826 + if (strpos($selected_model, 'voyage') === 0) {
2827 + $endpoint = 'https://api.voyageai.com/v1/embeddings';
2828 + $api_key = $options['voyage_api_key'] ?? '';
2829 +
2830 + // Check if Voyage API key is missing
2831 + if (empty($api_key)) {
2832 + //error_log('Voyage API key is missing');
2833 + return [
2834 + 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
2835 + 'error_code' => 'missing_voyage_api_key'
2836 + ];
2837 + }
2838 + } else {
2839 + $endpoint = 'https://api.openai.com/v1/embeddings';
2840 + // Use the passed API key for OpenAI
2841 +
2842 + // Check if OpenAI API key is missing
2843 + if (empty($api_key)) {
2844 + //error_log('OpenAI API key is missing');
2845 + return [
2846 + 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
2847 + 'error_code' => 'missing_openai_api_key'
2848 + ];
2849 + }
2850 + }
2851 +
2852 + // Check if text is empty
2853 + if (empty($text)) {
2854 + //error_log('Empty text provided for embedding generation');
2855 + return [
2856 + 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
2857 + 'error_code' => 'empty_embedding_text'
2858 + ];
2859 + }
2860 +
2861 + // Prepare request body with conditional output_dimension
2862 + $request_body = [
2863 + 'input' => $text,
2864 + 'model' => $selected_model
2865 + ];
2866 +
2867 + // Add output_dimension for voyage-3-large
2868 + if ($selected_model === 'voyage-3-large') {
2869 + $request_body['output_dimension'] = 2048;
2870 + }
2871 +
2872 + // Prepare request arguments
2873 + $args = [
2874 + 'body' => wp_json_encode($request_body),
2875 + 'headers' => [
2876 + 'Content-Type' => 'application/json',
2877 + 'Authorization' => 'Bearer ' . $api_key,
2878 + ],
2879 + 'timeout' => 60,
2880 + 'redirection' => 5,
2881 + 'blocking' => true,
2882 + 'httpversion' => '1.0',
2883 + 'sslverify' => true,
2884 + ];
2885 +
2886 + // Make the request
2887 + $response = wp_remote_post($endpoint, $args);
2888 +
2889 + // Handle WordPress errors
2890 + if (is_wp_error($response)) {
2891 + $error_message = $response->get_error_message();
2892 + //error_log('Embedding Generation Error: ' . $error_message);
2893 + return [
2894 + 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
2895 + 'error_code' => 'embedding_connection_error'
2896 + ];
2897 + }
2898 +
2899 + // Check HTTP status code
2900 + $status_code = wp_remote_retrieve_response_code($response);
2901 + if ($status_code !== 200) {
2902 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
2903 +
2904 + $error_message = isset($response_body['error']['message'])
2905 + ? $response_body['error']['message']
2906 + : 'HTTP Error ' . $status_code;
2907 +
2908 + $error_type = isset($response_body['error']['type'])
2909 + ? $response_body['error']['type']
2910 + : 'unknown';
2911 +
2912 + //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
2913 +
2914 + // Handle specific error types
2915 + switch ($error_type) {
2916 + case 'invalid_request_error':
2917 + if (strpos($error_message, 'API key') !== false) {
2918 + return [
2919 + 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
2920 + 'error_code' => 'embedding_invalid_api_key'
2921 + ];
2922 + }
2923 + break;
2924 +
2925 + case 'authentication_error':
2926 + return [
2927 + 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
2928 + 'error_code' => 'embedding_auth_error'
2929 + ];
2930 +
2931 + case 'rate_limit_exceeded':
2932 + return [
2933 + 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
2934 + 'error_code' => 'embedding_rate_limit'
2935 + ];
2936 +
2937 + case 'quota_exceeded':
2938 + return [
2939 + 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
2940 + 'error_code' => 'embedding_quota_exceeded'
2941 + ];
2942 + }
2943 +
2944 + // Generic error fallback
2945 + return [
2946 + 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
2947 + 'error_code' => 'embedding_api_error',
2948 + 'status_code' => $status_code
2949 + ];
2950 + }
2951 +
2952 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
2953 +
2954 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
2955 + return $response_body['data'][0]['embedding'];
2956 + } else {
2957 + //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
2958 + return [
2959 + 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
2960 + 'error_code' => 'invalid_embedding_response'
2961 + ];
2962 + }
2963 + } catch (Exception $e) {
2964 + //error_log('Embedding Exception: ' . $e->getMessage());
2965 + return [
2966 + 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
2967 + 'error_code' => 'embedding_exception'
2968 + ];
2969 + }
2970 +}
2971 +
2972 +
2973 +private function mxchat_find_relevant_content($user_embedding) {
2974 + //error_log('MXChat Vector Search: Starting content search...');
2975 +
2976 + // Retrieve the add-on settings from the database.
2977 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
2978 +
2979 + // Determine whether Pinecone is enabled.
2980 + // We expect the sanitized setting to be a string '1' if enabled, otherwise '0'.
2981 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
2982 +
2983 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
2984 +
2985 + if ($use_pinecone === 1) {
2986 + //error_log('MXChat Vector Search: Using Pinecone database');
2987 + return $this->find_relevant_content_pinecone($user_embedding);
2988 + } else {
2989 + //error_log('MXChat Vector Search: Using WordPress database');
2990 + return $this->find_relevant_content_wordpress($user_embedding);
2991 + }
2992 +}
2993 +
2994 +
2995 +private function find_relevant_content_wordpress($user_embedding) {
2996 + global $wpdb;
2997 + $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
2998 + $cache_key = 'mxchat_system_prompt_embeddings';
2999 + $batch_size = 500;
3000 +
3001 + // Log start of matching process
3002 + //error_log('[MXCHAT] Starting similarity matching process');
3003 +
3004 + // Retrieve embeddings from cache or database
3005 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3006 + if ($embeddings === false) {
3007 + //error_log('[MXCHAT] Cache miss - loading embeddings from database');
3008 + $embeddings = [];
3009 + $offset = 0;
3010 +
3011 + // Load in batches and build cache
3012 + do {
3013 + $query = $wpdb->prepare(
3014 + "SELECT id, embedding_vector
3015 + FROM {$system_prompt_table}
3016 + LIMIT %d OFFSET %d",
3017 + $batch_size,
3018 + $offset
3019 + );
3020 +
3021 + $batch = $wpdb->get_results($query);
3022 + if (empty($batch)) {
3023 + break;
3024 + }
3025 +
3026 + $embeddings = array_merge($embeddings, $batch);
3027 + $offset += $batch_size;
3028 +
3029 + // Free memory
3030 + unset($batch);
3031 +
3032 + } while (true);
3033 +
3034 + if (empty($embeddings)) {
3035 + //error_log('[MXCHAT] No embeddings found in database');
3036 + return ''; // Return an empty string if no embeddings found
3037 + }
3038 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3039 + //error_log('[MXCHAT] Cached ' . count($embeddings) . ' embeddings');
3040 + } else {
3041 + //error_log('[MXCHAT] Using ' . count($embeddings) . ' cached embeddings');
3042 + }
3043 +
3044 + // Initialize array to store relevant results with similarity scores
3045 + $relevant_results = [];
3046 +
3047 + // Get the similarity threshold from the main options array only
3048 + $main_options = get_option('mxchat_options', []);
3049 + $similarity_threshold = isset($main_options['similarity_threshold'])
3050 + ? ((int) $main_options['similarity_threshold']) / 100
3051 + : 0.8; // Default to 80%
3052 +
3053 + //error_log('[MXCHAT] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
3054 +
3055 + // Iterate through embeddings to calculate similarity
3056 + foreach ($embeddings as $embedding) {
3057 + $database_embedding = $embedding->embedding_vector
3058 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3059 + : null;
3060 + if (is_array($database_embedding) && is_array($user_embedding)) {
3061 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
3062 +
3063 + // Log each similarity score over 0.5 to reduce log spam
3064 + if ($similarity > 0.1) {
3065 + //error_log(sprintf('[MXCHAT] ID: %d | Similarity Score: %.4f', $embedding->id, $similarity));
3066 + }
3067 +
3068 + $relevant_results[] = [
3069 + 'id' => $embedding->id,
3070 + 'similarity' => $similarity
3071 + ];
3072 + }
3073 + // Free memory
3074 + unset($database_embedding);
3075 + }
3076 +
3077 + // Filter and sort relevant results by similarity
3078 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3079 + return $result['similarity'] >= $similarity_threshold;
3080 + });
3081 + usort($relevant_results, function ($a, $b) {
3082 + return $b['similarity'] <=> $a['similarity'];
3083 + });
3084 +
3085 + // Log number of results that met threshold
3086 + //error_log('[MXCHAT] ' . count($relevant_results) . ' results met the similarity threshold');
3087 +
3088 + // Limit to the top 5 results
3089 + $top_results = array_slice($relevant_results, 0, 5);
3090 +
3091 + // Log the top matches
3092 + //error_log('[MXCHAT] Top matching results:');
3093 + foreach ($top_results as $index => $result) {
3094 +
3095 + }
3096 +
3097 + // Initialize the final content
3098 + $content = '';
3099 +
3100 + // Fetch and combine content for the top results
3101 + foreach ($top_results as $result) {
3102 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3103 + // Check if the content is PDF-related and add surrounding pages
3104 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
3105 + //error_log('[MXCHAT] ID ' . $result['id'] . ' is PDF content, adding surrounding pages');
3106 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
3107 + "SELECT id, article_content FROM {$system_prompt_table}
3108 + WHERE id IN (
3109 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
3110 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
3111 + )",
3112 + $result['id'],
3113 + $result['id']
3114 + ));
3115 + // Add previous content if it exists
3116 + if (!empty($surrounding_content[0])) {
3117 + $content .= $surrounding_content[0]->article_content . "\n\n";
3118 + }
3119 + // Add the main chunk content
3120 + $content .= $chunk_content . "\n\n";
3121 + // Add next content if it exists
3122 + if (!empty($surrounding_content[1])) {
3123 + $content .= $surrounding_content[1]->article_content . "\n\n";
3124 + }
3125 + } else {
3126 + // For non-PDF content, add directly
3127 + $content .= $chunk_content . "\n\n";
3128 + }
3129 + }
3130 +
3131 + // Log content length
3132 + //error_log('[MXCHAT] Retrieved content length: ' . strlen(trim($content)) . ' characters');
3133 +
3134 + return trim($content);
3135 +}
3136 +
3137 +
3138 +private function find_relevant_content_pinecone($user_embedding) {
3139 + $options = get_option('mxchat_pinecone_addon_options', array());
3140 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
3141 + $host = $options['mxchat_pinecone_host'] ?? '';
3142 +
3143 + if (empty($host) || empty($api_key)) {
3144 + //error_log('[MXCHAT Debug] Pinecone credentials not properly configured');
3145 + return '';
3146 + }
3147 +
3148 + // Get the similarity threshold from the main options array only
3149 + $main_options = get_option('mxchat_options', []);
3150 + $similarity_threshold = isset($main_options['similarity_threshold'])
3151 + ? ((int) $main_options['similarity_threshold']) / 100
3152 + : 0.8; // Default to 80%
3153 +
3154 + //error_log('[MXCHAT Debug] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
3155 +
3156 + // Prepare the query request for Pinecone
3157 + $api_endpoint = "https://{$host}/query";
3158 +
3159 + //error_log('[MXCHAT Debug] Querying Pinecone at: ' . $api_endpoint);
3160 +
3161 + $request_body = array(
3162 + 'vector' => $user_embedding,
3163 + 'topK' => 5,
3164 + 'includeMetadata' => true,
3165 + 'includeValues' => true
3166 + );
3167 +
3168 + $response = wp_remote_post($api_endpoint, array(
3169 + 'headers' => array(
3170 + 'Api-Key' => $api_key,
3171 + 'accept' => 'application/json',
3172 + 'content-type' => 'application/json'
3173 + ),
3174 + 'body' => wp_json_encode($request_body),
3175 + 'timeout' => 30
3176 + ));
3177 +
3178 + if (is_wp_error($response)) {
3179 + //error_log('[MXCHAT Debug] Pinecone query error: ' . $response->get_error_message());
3180 + return '';
3181 + }
3182 +
3183 + $response_code = wp_remote_retrieve_response_code($response);
3184 + if ($response_code !== 200) {
3185 + //error_log('[MXCHAT Debug] Pinecone API error: ' . wp_remote_retrieve_body($response));
3186 + return '';
3187 + }
3188 +
3189 + $results = json_decode(wp_remote_retrieve_body($response), true);
3190 + if (empty($results['matches'])) {
3191 + //error_log('[MXCHAT Debug] No matches found in Pinecone response');
3192 + return '';
3193 + }
3194 +
3195 + //error_log('[MXCHAT Debug] Found ' . count($results['matches']) . ' matches in Pinecone');
3196 +
3197 + // Initialize the final content
3198 + $content = '';
3199 + $matches_above_threshold = 0;
3200 +
3201 + // Process each match
3202 + foreach ($results['matches'] as $index => $match) {
3203 + // Log score for each match
3204 +
3205 + // Skip if similarity is below threshold
3206 + if ($match['score'] < $similarity_threshold) {
3207 + continue;
3208 + }
3209 +
3210 + $matches_above_threshold++;
3211 +
3212 + if (!empty($match['metadata']['text']) && !empty($match['metadata']['source_url'])) {
3213 + // Add content with citation
3214 + $content .= $match['metadata']['text'] . "\n";
3215 + $content .= "Source: " . $match['metadata']['source_url'] . "\n\n";
3216 + }
3217 + }
3218 +
3219 + //error_log('[MXCHAT Debug] Total matches used (above threshold): ' . $matches_above_threshold);
3220 + //error_log('[MXCHAT Debug] Content length returned: ' . strlen(trim($content)) . ' characters');
3221 +
3222 + return trim($content);
3223 +}
3224 +
3225 +private function mxchat_find_relevant_products($user_embedding) {
3226 + //error_log('MXChat Vector Search: Starting product search...');
3227 +
3228 + // Retrieve the add-on settings from the database
3229 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
3230 +
3231 + // Determine whether Pinecone is enabled
3232 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
3233 +
3234 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
3235 +
3236 + if ($use_pinecone === 1) {
3237 + //error_log('MXChat Vector Search: Using Pinecone database for products');
3238 + return $this->find_relevant_products_pinecone($user_embedding);
3239 + } else {
3240 + //error_log('MXChat Vector Search: Using WordPress database for products');
3241 + return $this->find_relevant_products_wordpress($user_embedding);
3242 + }
3243 +}
3244 +
3245 +private function find_relevant_products_wordpress($user_embedding) {
3246 + global $wpdb;
3247 + $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3248 + $cache_key = 'mxchat_system_prompt_embeddings';
3249 + $batch_size = 500;
3250 +
3251 + // Original WordPress database search logic
3252 + // [Previous implementation remains the same]
3253 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3254 + if ($embeddings === false) {
3255 + $embeddings = [];
3256 + $offset = 0;
3257 +
3258 + do {
3259 + $query = $wpdb->prepare(
3260 + "SELECT id, embedding_vector
3261 + FROM {$system_prompt_table}
3262 + LIMIT %d OFFSET %d",
3263 + $batch_size,
3264 + $offset
3265 + );
3266 +
3267 + $batch = $wpdb->get_results($query);
3268 + if (empty($batch)) {
3269 + break;
3270 + }
3271 +
3272 + $embeddings = array_merge($embeddings, $batch);
3273 + $offset += $batch_size;
3274 +
3275 + unset($batch);
3276 +
3277 + } while (true);
3278 +
3279 + if (empty($embeddings)) {
3280 + return '';
3281 + }
3282 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3283 + }
3284 +
3285 + $relevant_results = [];
3286 + foreach ($embeddings as $embedding) {
3287 + $database_embedding = $embedding->embedding_vector
3288 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3289 + : null;
3290 + if (is_array($database_embedding) && is_array($user_embedding)) {
3291 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
3292 + $relevant_results[] = [
3293 + 'id' => $embedding->id,
3294 + 'similarity' => $similarity
3295 + ];
3296 + }
3297 + unset($database_embedding);
3298 + }
3299 +
3300 + // Use fixed threshold for products
3301 + $similarity_threshold = 0.85;
3302 +
3303 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3304 + return $result['similarity'] >= $similarity_threshold;
3305 + });
3306 + usort($relevant_results, function ($a, $b) {
3307 + return $b['similarity'] <=> $a['similarity'];
3308 + });
3309 +
3310 + $top_results = array_slice($relevant_results, 0, 5);
3311 + $content = '';
3312 +
3313 + foreach ($top_results as $result) {
3314 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
3315 + $content .= $chunk_content . "\n\n";
3316 + }
3317 +
3318 + return trim($content);
3319 +}
3320 +
3321 +// Modified search function with correct filter syntax
3322 +private function find_relevant_products_pinecone($user_embedding) {
3323 + //error_log('Starting Pinecone product search...');
3324 +
3325 + $options = get_option('mxchat_pinecone_addon_options', array());
3326 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
3327 + $host = $options['mxchat_pinecone_host'] ?? '';
3328 +
3329 + if (empty($host) || empty($api_key)) {
3330 + //error_log('Pinecone credentials not properly configured for product search');
3331 + return '';
3332 + }
3333 +
3334 + $similarity_threshold = 0.85;
3335 + $api_endpoint = "https://{$host}/query";
3336 +
3337 + $request_body = array(
3338 + 'vector' => $user_embedding,
3339 + 'topK' => 5,
3340 + 'includeMetadata' => true,
3341 + 'includeValues' => true,
3342 + 'filter' => array(
3343 + 'type' => 'product'
3344 + )
3345 + );
3346 +
3347 + //error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
3348 +
3349 + $response = wp_remote_post($api_endpoint, array(
3350 + 'headers' => array(
3351 + 'Api-Key' => $api_key,
3352 + 'accept' => 'application/json',
3353 + 'content-type' => 'application/json'
3354 + ),
3355 + 'body' => wp_json_encode($request_body),
3356 + 'timeout' => 30
3357 + ));
3358 +
3359 + if (is_wp_error($response)) {
3360 + //error_log('Pinecone product query error: ' . $response->get_error_message());
3361 + return '';
3362 + }
3363 +
3364 + $response_code = wp_remote_retrieve_response_code($response);
3365 + //error_log('Pinecone response code: ' . $response_code);
3366 +
3367 + if ($response_code !== 200) {
3368 + //error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
3369 + return '';
3370 + }
3371 +
3372 + $results = json_decode(wp_remote_retrieve_body($response), true);
3373 + //error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
3374 +
3375 + if (empty($results['matches'])) {
3376 + //error_log('No matches found in Pinecone response');
3377 + return '';
3378 + }
3379 +
3380 + $content = '';
3381 + foreach ($results['matches'] as $match) {
3382 + if ($match['score'] < $similarity_threshold) {
3383 + //error_log("Match below threshold: " . $match['score']);
3384 + continue;
3385 + }
3386 +
3387 + if (!empty($match['metadata']['text'])) {
3388 + $content .= $match['metadata']['text'];
3389 + if (!empty($match['metadata']['source_url'])) {
3390 + $content .= "\n\nFor more details, check out this product: " . esc_url($match['metadata']['source_url']);
3391 + }
3392 + $content .= "\n\n";
3393 + }
3394 + }
3395 +
3396 + return trim($content);
3397 +}
3398 +
3399 +
3400 +private function fetch_content_with_product_links($most_relevant_id) {
3401 + global $wpdb;
3402 + $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3403 +
3404 + // Fetch the article content and associated product URL
3405 + $query = $wpdb->prepare("SELECT article_content, source_url FROM {$system_prompt_table} WHERE id = %d", $most_relevant_id);
3406 + $result = $wpdb->get_row($query);
3407 +
3408 + if ($result) {
3409 + // Append the product link to the content if available
3410 + $content = $result->article_content;
3411 + if (!empty($result->source_url)) {
3412 + $content .= "\n\nFor more details, check out this product: " . esc_url($result->source_url);
3413 + }
3414 + return $content;
3415 + }
3416 +
3417 + return null;
3418 +}
3419 +
3420 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history) {
3421 + try {
3422 + if (!$relevant_content) {
3423 + return [
3424 + 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
3425 + 'error_code' => 'no_relevant_content'
3426 + ];
3427 + }
3428 +
3429 + // Ensure conversation_history is an array
3430 + if (!is_array($conversation_history)) {
3431 + $conversation_history = array();
3432 + }
3433 +
3434 + // Get selected model with default fallback
3435 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
3436 +
3437 + // Extract model prefix to determine the provider
3438 + $model_parts = explode('-', $selected_model);
3439 + $provider = strtolower($model_parts[0]);
3440 +
3441 + // Handle model selection based on provider prefix
3442 + switch ($provider) {
3443 + case 'gemini':
3444 + if (empty($gemini_api_key)) {
3445 + return [
3446 + 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
3447 + 'error_code' => 'missing_gemini_api_key'
3448 + ];
3449 + }
3450 + $response = $this->mxchat_generate_response_gemini(
3451 + $selected_model,
3452 + $gemini_api_key,
3453 + $conversation_history,
3454 + $relevant_content
3455 + );
3456 + break;
3457 +
3458 + case 'claude':
3459 + if (empty($claude_api_key)) {
3460 + return [
3461 + 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
3462 + 'error_code' => 'missing_claude_api_key'
3463 + ];
3464 + }
3465 + $response = $this->mxchat_generate_response_claude(
3466 + $selected_model,
3467 + $claude_api_key,
3468 + $conversation_history,
3469 + $relevant_content
3470 + );
3471 + break;
3472 +
3473 + case 'grok':
3474 + if (empty($xai_api_key)) {
3475 + return [
3476 + 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
3477 + 'error_code' => 'missing_xai_api_key'
3478 + ];
3479 + }
3480 + $response = $this->mxchat_generate_response_xai(
3481 + $selected_model,
3482 + $xai_api_key,
3483 + $conversation_history,
3484 + $relevant_content
3485 + );
3486 + break;
3487 +
3488 + case 'deepseek':
3489 + if (empty($deepseek_api_key)) {
3490 + return [
3491 + 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
3492 + 'error_code' => 'missing_deepseek_api_key'
3493 + ];
3494 + }
3495 + $response = $this->mxchat_generate_response_deepseek(
3496 + $selected_model,
3497 + $deepseek_api_key,
3498 + $conversation_history,
3499 + $relevant_content
3500 + );
3501 + break;
3502 +
3503 + case 'gpt':
3504 + if (empty($api_key)) {
3505 + return [
3506 + 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
3507 + 'error_code' => 'missing_openai_api_key'
3508 + ];
3509 + }
3510 + $response = $this->mxchat_generate_response_openai(
3511 + $selected_model,
3512 + $api_key,
3513 + $conversation_history,
3514 + $relevant_content
3515 + );
3516 + break;
3517 +
3518 + default:
3519 + // Default to OpenAI for custom models or unrecognized prefixes
3520 + if (empty($api_key)) {
3521 + return [
3522 + 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
3523 + 'error_code' => 'missing_openai_api_key'
3524 + ];
3525 + }
3526 + $response = $this->mxchat_generate_response_openai(
3527 + $selected_model,
3528 + $api_key,
3529 + $conversation_history,
3530 + $relevant_content
3531 + );
3532 + break;
3533 + }
3534 +
3535 + // Check if the response is an error array from the provider-specific function
3536 + if (is_array($response) && isset($response['error'])) {
3537 + return $response; // Pass through the error
3538 + }
3539 +
3540 + return $response;
3541 +
3542 + } catch (Exception $e) {
3543 + //error_log('MXChat Error: ' . $e->getMessage());
3544 + return [
3545 + 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
3546 + 'error_code' => 'system_exception',
3547 + 'exception_details' => $e->getMessage()
3548 + ];
3549 + }
3550 +}
3551 +
3552 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
3553 + try {
3554 + // Ensure conversation_history is an array
3555 + if (!is_array($conversation_history)) {
3556 + $conversation_history = array();
3557 + }
3558 +
3559 + // Get system prompt instructions from options
3560 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3561 +
3562 + // Create a new array for the formatted conversation
3563 + $formatted_conversation = array();
3564 +
3565 + // Add system message first
3566 + $formatted_conversation[] = array(
3567 + 'role' => 'system',
3568 + 'content' => $system_prompt_instructions . " " . $relevant_content
3569 + );
3570 +
3571 + // Add the rest of the conversation history
3572 + foreach ($conversation_history as $message) {
3573 + if (is_array($message) && isset($message['role']) && isset($message['content'])) {
3574 + $role = $message['role'];
3575 +
3576 + // Convert roles to supported format
3577 + if ($role === 'bot' || $role === 'agent') {
3578 + $role = 'assistant';
3579 + }
3580 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3581 + $role = 'user';
3582 + }
3583 +
3584 + $formatted_conversation[] = array(
3585 + 'role' => $role,
3586 + 'content' => $message['content']
3587 + );
3588 + }
3589 + }
3590 +
3591 + $body = json_encode([
3592 + 'model' => $selected_model,
3593 + 'messages' => $formatted_conversation,
3594 + 'temperature' => 0.8,
3595 + 'stream' => false
3596 + ]);
3597 +
3598 + $args = [
3599 + 'body' => $body,
3600 + 'headers' => [
3601 + 'Content-Type' => 'application/json',
3602 + 'Authorization' => 'Bearer ' . $deepseek_api_key,
3603 + ],
3604 + 'timeout' => 60,
3605 + 'redirection' => 5,
3606 + 'blocking' => true,
3607 + 'httpversion' => '1.0',
3608 + 'sslverify' => true,
3609 + ];
3610 +
3611 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
3612 +
3613 + if (is_wp_error($response)) {
3614 + $error_message = $response->get_error_message();
3615 + //error_log('DeepSeek API Error: ' . $error_message);
3616 + return [
3617 + 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
3618 + 'error_code' => 'deepseek_connection_error',
3619 + 'provider' => 'deepseek'
3620 + ];
3621 + }
3622 +
3623 + $status_code = wp_remote_retrieve_response_code($response);
3624 + if ($status_code !== 200) {
3625 + $response_body = wp_remote_retrieve_body($response);
3626 + $decoded_response = json_decode($response_body, true);
3627 +
3628 + $error_message = isset($decoded_response['error']['message'])
3629 + ? $decoded_response['error']['message']
3630 + : 'HTTP Error ' . $status_code;
3631 +
3632 + $error_type = isset($decoded_response['error']['type'])
3633 + ? $decoded_response['error']['type']
3634 + : 'unknown';
3635 +
3636 + //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
3637 +
3638 + // Handle specific error types
3639 + switch ($status_code) {
3640 + case 401:
3641 + return [
3642 + 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
3643 + 'error_code' => 'deepseek_auth_error',
3644 + 'provider' => 'deepseek'
3645 + ];
3646 +
3647 + case 400:
3648 + if (strpos($error_message, 'API key') !== false) {
3649 + return [
3650 + 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
3651 + 'error_code' => 'deepseek_invalid_api_key',
3652 + 'provider' => 'deepseek'
3653 + ];
3654 + }
3655 + break;
3656 +
3657 + case 429:
3658 + if (strpos($error_message, 'quota') !== false) {
3659 + return [
3660 + 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
3661 + 'error_code' => 'deepseek_quota_exceeded',
3662 + 'provider' => 'deepseek'
3663 + ];
3664 + } else {
3665 + return [
3666 + 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
3667 + 'error_code' => 'deepseek_rate_limit',
3668 + 'provider' => 'deepseek'
3669 + ];
3670 + }
3671 +
3672 + case 500:
3673 + case 502:
3674 + case 503:
3675 + case 504:
3676 + return [
3677 + 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
3678 + 'error_code' => 'deepseek_service_unavailable',
3679 + 'provider' => 'deepseek'
3680 + ];
3681 + }
3682 +
3683 + // Generic error fallback
3684 + return [
3685 + 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
3686 + 'error_code' => 'deepseek_api_error',
3687 + 'provider' => 'deepseek',
3688 + 'status_code' => $status_code
3689 + ];
3690 + }
3691 +
3692 + $response_body = wp_remote_retrieve_body($response);
3693 + $decoded_response = json_decode($response_body, true);
3694 +
3695 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3696 + return trim($decoded_response['choices'][0]['message']['content']);
3697 + } else {
3698 + //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
3699 + return [
3700 + 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
3701 + 'error_code' => 'deepseek_response_format_error',
3702 + 'provider' => 'deepseek'
3703 + ];
3704 + }
3705 + } catch (Exception $e) {
3706 + //error_log('DeepSeek Exception: ' . $e->getMessage());
3707 + return [
3708 + 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
3709 + 'error_code' => 'deepseek_exception',
3710 + 'provider' => 'deepseek'
3711 + ];
3712 + }
3713 +}
3714 +
3715 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
3716 + try {
3717 + // Ensure conversation_history is an array
3718 + if (!is_array($conversation_history)) {
3719 + $conversation_history = array();
3720 + }
3721 +
3722 + // Get system prompt instructions from options
3723 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3724 +
3725 + // Create a new array for the formatted conversation
3726 + $formatted_conversation = array();
3727 +
3728 + // Add system message first
3729 + $formatted_conversation[] = array(
3730 + 'role' => 'system',
3731 + 'content' => $system_prompt_instructions . " " . $relevant_content
3732 + );
3733 +
3734 + // Add the rest of the conversation history
3735 + foreach ($conversation_history as $message) {
3736 + if (is_array($message) && isset($message['role']) && isset($message['content'])) {
3737 + $role = $message['role'];
3738 +
3739 + // Convert roles to supported format
3740 + if ($role === 'bot' || $role === 'agent') {
3741 + $role = 'assistant';
3742 + }
3743 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3744 + $role = 'user';
3745 + }
3746 +
3747 + $formatted_conversation[] = array(
3748 + 'role' => $role,
3749 + 'content' => $message['content']
3750 + );
3751 + }
3752 + }
3753 +
3754 + $body = json_encode([
3755 + 'model' => $selected_model,
3756 + 'messages' => $formatted_conversation,
3757 + 'temperature' => 0.8,
3758 + 'stream' => false
3759 + ]);
3760 +
3761 + $args = [
3762 + 'body' => $body,
3763 + 'headers' => [
3764 + 'Content-Type' => 'application/json',
3765 + 'Authorization' => 'Bearer ' . $api_key,
3766 + ],
3767 + 'timeout' => 60,
3768 + 'redirection' => 5,
3769 + 'blocking' => true,
3770 + 'httpversion' => '1.0',
3771 + 'sslverify' => true,
3772 + ];
3773 +
3774 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
3775 +
3776 + if (is_wp_error($response)) {
3777 + $error_message = $response->get_error_message();
3778 + //error_log('OpenAI API Error: ' . $error_message);
3779 + return [
3780 + 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
3781 + 'error_code' => 'openai_connection_error',
3782 + 'provider' => 'openai'
3783 + ];
3784 + }
3785 +
3786 + $status_code = wp_remote_retrieve_response_code($response);
3787 + if ($status_code !== 200) {
3788 + $response_body = wp_remote_retrieve_body($response);
3789 + $decoded_response = json_decode($response_body, true);
3790 +
3791 + $error_message = isset($decoded_response['error']['message'])
3792 + ? $decoded_response['error']['message']
3793 + : 'HTTP Error ' . $status_code;
3794 +
3795 + $error_type = isset($decoded_response['error']['type'])
3796 + ? $decoded_response['error']['type']
3797 + : 'unknown';
3798 +
3799 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
3800 +
3801 + // Handle specific error types
3802 + switch ($error_type) {
3803 + case 'invalid_request_error':
3804 + if (strpos($error_message, 'API key') !== false) {
3805 + return [
3806 + 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
3807 + 'error_code' => 'openai_invalid_api_key',
3808 + 'provider' => 'openai'
3809 + ];
3810 + }
3811 + break;
3812 +
3813 + case 'authentication_error':
3814 + return [
3815 + 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
3816 + 'error_code' => 'openai_auth_error',
3817 + 'provider' => 'openai'
3818 + ];
3819 +
3820 + case 'rate_limit_exceeded':
3821 + return [
3822 + 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
3823 + 'error_code' => 'openai_rate_limit',
3824 + 'provider' => 'openai'
3825 + ];
3826 +
3827 + case 'quota_exceeded':
3828 + return [
3829 + 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
3830 + 'error_code' => 'openai_quota_exceeded',
3831 + 'provider' => 'openai'
3832 + ];
3833 + }
3834 +
3835 + // Generic error fallback
3836 + return [
3837 + 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
3838 + 'error_code' => 'openai_api_error',
3839 + 'provider' => 'openai',
3840 + 'status_code' => $status_code
3841 + ];
3842 + }
3843 +
3844 + $response_body = wp_remote_retrieve_body($response);
3845 + $decoded_response = json_decode($response_body, true);
3846 +
3847 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3848 + return trim($decoded_response['choices'][0]['message']['content']);
3849 + } else {
3850 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
3851 + return [
3852 + 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
3853 + 'error_code' => 'openai_response_format_error',
3854 + 'provider' => 'openai'
3855 + ];
3856 + }
3857 + } catch (Exception $e) {
3858 + //error_log('OpenAI Exception: ' . $e->getMessage());
3859 + return [
3860 + 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
3861 + 'error_code' => 'openai_exception',
3862 + 'provider' => 'openai'
3863 + ];
3864 + }
3865 +}
3866 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
3867 + try {
3868 + // Get system prompt instructions from options
3869 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3870 +
3871 + // Add system prompt to relevant content
3872 + $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
3873 +
3874 + // Prepend system instructions to the conversation history
3875 + array_unshift($conversation_history, [
3876 + 'role' => 'system',
3877 + 'content' => "Here are your instructions: " . $content_with_instructions
3878 + ]);
3879 +
3880 + // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
3881 + foreach ($conversation_history as &$message) {
3882 + if ($message['role'] === 'bot') {
3883 + $message['role'] = 'assistant';
3884 + } elseif ($message['role'] === 'agent') {
3885 + // Tag the message as coming from a live agent
3886 + $message['role'] = 'assistant';
3887 + if (!isset($message['metadata'])) {
3888 + $message['metadata'] = ['source' => 'live_agent'];
3889 + }
3890 + }
3891 +
3892 + // Ensure all roles are valid
3893 + if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
3894 + $message['role'] = 'user'; // Default to 'user'
3895 + }
3896 + }
3897 +
3898 + // Build the request body
3899 + $body = json_encode([
3900 + 'model' => $selected_model,
3901 + 'messages' => $conversation_history,
3902 + 'temperature' => 0.8,
3903 + 'stream' => false
3904 + ]);
3905 +
3906 + // Set up the API request
3907 + $args = [
3908 + 'body' => $body,
3909 + 'headers' => [
3910 + 'Content-Type' => 'application/json',
3911 + 'Authorization' => 'Bearer ' . $xai_api_key,
3912 + ],
3913 + 'timeout' => 60,
3914 + 'redirection' => 5,
3915 + 'blocking' => true,
3916 + 'httpversion' => '1.0',
3917 + 'sslverify' => true,
3918 + ];
3919 +
3920 + // Make the API request
3921 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
3922 +
3923 + // Process the response
3924 + if (is_wp_error($response)) {
3925 + $error_message = $response->get_error_message();
3926 + //error_log('X.AI API Error: ' . $error_message);
3927 + return [
3928 + 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
3929 + 'error_code' => 'xai_connection_error',
3930 + 'provider' => 'xai'
3931 + ];
3932 + }
3933 +
3934 + $status_code = wp_remote_retrieve_response_code($response);
3935 + if ($status_code !== 200) {
3936 + $response_body = wp_remote_retrieve_body($response);
3937 + $decoded_response = json_decode($response_body, true);
3938 +
3939 + // Log the full response for debugging
3940 + //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
3941 +
3942 + // Extract error message from X.AI's specific format
3943 + $error_message = '';
3944 +
3945 + // Check for direct error string (as seen in your logs)
3946 + if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
3947 + $error_message = $decoded_response['error'];
3948 + }
3949 + // Check for nested error object (OpenAI style)
3950 + elseif (isset($decoded_response['error']['message'])) {
3951 + $error_message = $decoded_response['error']['message'];
3952 + }
3953 + // Check for top-level message
3954 + elseif (isset($decoded_response['message'])) {
3955 + $error_message = $decoded_response['message'];
3956 + }
3957 + // Fallback
3958 + else {
3959 + $error_message = 'HTTP Error ' . $status_code;
3960 + }
3961 +
3962 + //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
3963 +
3964 + // Check for API key errors using string matching
3965 + if (stripos($error_message, 'api key') !== false ||
3966 + stripos($error_message, 'incorrect api key') !== false ||
3967 + stripos($error_message, 'invalid api key') !== false) {
3968 + return [
3969 + 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
3970 + 'error_code' => 'xai_invalid_api_key',
3971 + 'provider' => 'xai'
3972 + ];
3973 + }
3974 +
3975 + // Authentication errors
3976 + if ($status_code === 401 || $status_code === 403 ||
3977 + stripos($error_message, 'auth') !== false) {
3978 + return [
3979 + 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
3980 + 'error_code' => 'xai_auth_error',
3981 + 'provider' => 'xai'
3982 + ];
3983 + }
3984 +
3985 + // Model errors
3986 + if (stripos($error_message, 'model') !== false) {
3987 + return [
3988 + 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
3989 + 'error_code' => 'xai_invalid_model',
3990 + 'provider' => 'xai'
3991 + ];
3992 + }
3993 +
3994 + // Rate limit errors
3995 + if ($status_code === 429 ||
3996 + stripos($error_message, 'rate') !== false ||
3997 + stripos($error_message, 'limit') !== false) {
3998 + return [
3999 + 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
4000 + 'error_code' => 'xai_rate_limit',
4001 + 'provider' => 'xai'
4002 + ];
4003 + }
4004 +
4005 + // Quota errors
4006 + if (stripos($error_message, 'quota') !== false ||
4007 + stripos($error_message, 'billing') !== false) {
4008 + return [
4009 + 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
4010 + 'error_code' => 'xai_quota_exceeded',
4011 + 'provider' => 'xai'
4012 + ];
4013 + }
4014 +
4015 + // Server errors
4016 + if ($status_code >= 500) {
4017 + return [
4018 + 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
4019 + 'error_code' => 'xai_service_unavailable',
4020 + 'provider' => 'xai'
4021 + ];
4022 + }
4023 +
4024 + // Generic error fallback with the actual error message
4025 + return [
4026 + 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
4027 + 'error_code' => 'xai_api_error',
4028 + 'provider' => 'xai',
4029 + 'status_code' => $status_code
4030 + ];
4031 + }
4032 +
4033 + $response_body = wp_remote_retrieve_body($response);
4034 + $decoded_response = json_decode($response_body, true);
4035 +
4036 + if (isset($decoded_response['choices'][0]['message']['content'])) {
4037 + return trim($decoded_response['choices'][0]['message']['content']);
4038 + } else {
4039 + //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
4040 + return [
4041 + 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
4042 + 'error_code' => 'xai_response_format_error',
4043 + 'provider' => 'xai'
4044 + ];
4045 + }
4046 +} catch (Exception $e) {
4047 + //error_log('X.AI Exception: ' . $e->getMessage());
4048 + return [
4049 + 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
4050 + 'error_code' => 'xai_exception',
4051 + 'provider' => 'xai'
4052 + ];
4053 +}
4054 +}
4055 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
4056 + // Get system prompt instructions from options
4057 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4058 +
4059 + // Clean and validate conversation history
4060 + foreach ($conversation_history as &$message) {
4061 + // Convert bot and agent roles to assistant
4062 + if ($message['role'] === 'bot' || $message['role'] === 'agent') {
4063 + $message['role'] = 'assistant';
4064 + }
4065 +
4066 + // Remove unsupported roles - Claude only supports 'assistant' and 'user'
4067 + if (!in_array($message['role'], ['assistant', 'user'])) {
4068 + $message['role'] = 'user';
4069 + }
4070 +
4071 + // Ensure content field exists
4072 + if (!isset($message['content']) || empty($message['content'])) {
4073 + $message['content'] = '';
4074 + }
4075 +
4076 + // Remove any unsupported fields
4077 + $message = array_intersect_key($message, array_flip(['role', 'content']));
4078 + }
4079 +
4080 + // Add relevant content as the latest user message
4081 + $conversation_history[] = [
4082 + 'role' => 'user',
4083 + 'content' => $relevant_content
4084 + ];
4085 +
4086 + // Build request body
4087 + $body = json_encode([
4088 + 'model' => $selected_model,
4089 + 'max_tokens' => 1000,
4090 + 'temperature' => 0.8,
4091 + 'messages' => $conversation_history,
4092 + 'system' => $system_prompt_instructions
4093 + ]);
4094 +
4095 + // Set up API request
4096 + $args = [
4097 + 'body' => $body,
4098 + 'headers' => [
4099 + 'Content-Type' => 'application/json',
4100 + 'x-api-key' => $claude_api_key,
4101 + 'anthropic-version' => '2023-06-01'
4102 + ],
4103 + 'timeout' => 60,
4104 + 'redirection' => 5,
4105 + 'blocking' => true,
4106 + 'httpversion' => '1.0',
4107 + 'sslverify' => true,
4108 + ];
4109 +
4110 + // Make API request
4111 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
4112 +
4113 + // Check for WordPress errors
4114 + if (is_wp_error($response)) {
4115 + //error_log("Claude API request error: " . $response->get_error_message());
4116 + return "Sorry, there was an error connecting to the API.";
4117 + }
4118 +
4119 + // Check HTTP response code
4120 + $http_code = wp_remote_retrieve_response_code($response);
4121 + if ($http_code !== 200) {
4122 + $error_body = wp_remote_retrieve_body($response);
4123 + //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
4124 +
4125 + // Try to extract error message from response
4126 + $error_data = json_decode($error_body, true);
4127 + $error_message = isset($error_data['error']['message']) ?
4128 + $error_data['error']['message'] :
4129 + "HTTP error " . $http_code;
4130 +
4131 + return "Sorry, the API returned an error: " . $error_message;
4132 + }
4133 +
4134 + // Parse response
4135 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
4136 +
4137 + // Check for JSON decode errors
4138 + if (json_last_error() !== JSON_ERROR_NONE) {
4139 + //error_log("Claude API JSON decode error: " . json_last_error_msg());
4140 + return "Sorry, there was an error processing the API response.";
4141 + }
4142 +
4143 + // Extract and validate response content
4144 + if (isset($response_body['content']) &&
4145 + is_array($response_body['content']) &&
4146 + !empty($response_body['content']) &&
4147 + isset($response_body['content'][0]['text'])) {
4148 + return trim($response_body['content'][0]['text']);
4149 + }
4150 +
4151 + // Log unexpected response format
4152 + //error_log("Claude API unexpected response format: " . print_r($response_body, true));
4153 + return "Sorry, I received an unexpected response format from the API.";
4154 +}
4155 +private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
4156 + // Get system prompt instructions from options
4157 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
4158 +
4159 + // Add system prompt to relevant content
4160 + $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
4161 +
4162 + // Format messages for Gemini API
4163 + $formatted_messages = [];
4164 +
4165 + // Add system message as the first user message with role prefix
4166 + // Note: Gemini doesn't have a dedicated system role, so we use a prefixed user message
4167 + $formatted_messages[] = [
4168 + 'role' => 'user',
4169 + 'parts' => [
4170 + ['text' => "[System Instructions] " . $content_with_instructions]
4171 + ]
4172 + ];
4173 +
4174 + // Add model response to acknowledge system instructions
4175 + $formatted_messages[] = [
4176 + 'role' => 'model',
4177 + 'parts' => [
4178 + ['text' => "I understand and will follow these instructions."]
4179 + ]
4180 + ];
4181 +
4182 + // Process the rest of the conversation history
4183 + $current_role = null;
4184 + $current_parts = [];
4185 +
4186 + foreach ($conversation_history as $message) {
4187 + // Skip the first system message as we already handled it
4188 + if ($message['role'] === 'system') {
4189 + continue;
4190 + }
4191 +
4192 + // Map roles to Gemini format
4193 + $gemini_role = '';
4194 + if ($message['role'] === 'user') {
4195 + $gemini_role = 'user';
4196 + } else if (in_array($message['role'], ['assistant', 'bot', 'agent'])) {
4197 + $gemini_role = 'model';
4198 + } else {
4199 + // Skip unsupported roles
4200 + continue;
4201 + }
4202 +
4203 + // If we have a new role, add the previous message
4204 + if ($current_role !== null && $current_role !== $gemini_role && !empty($current_parts)) {
4205 + $formatted_messages[] = [
4206 + 'role' => $current_role,
4207 + 'parts' => $current_parts
4208 + ];
4209 + $current_parts = [];
4210 + }
4211 +
4212 + // Set current role and add text to parts
4213 + $current_role = $gemini_role;
4214 + $current_parts[] = ['text' => $message['content']];
4215 + }
4216 +
4217 + // Add the last message if there's content
4218 + if ($current_role !== null && !empty($current_parts)) {
4219 + $formatted_messages[] = [
4220 + 'role' => $current_role,
4221 + 'parts' => $current_parts
4222 + ];
4223 + }
4224 +
4225 + // Build the request body
4226 + $body = json_encode([
4227 + 'contents' => $formatted_messages,
4228 + 'generationConfig' => [
4229 + 'temperature' => 0.7,
4230 + 'topP' => 0.95,
4231 + 'topK' => 40,
4232 + 'maxOutputTokens' => 8192,
4233 + ],
4234 + 'safetySettings' => [
4235 + [
4236 + 'category' => 'HARM_CATEGORY_HARASSMENT',
4237 + 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
4238 + ],
4239 + [
4240 + 'category' => 'HARM_CATEGORY_HATE_SPEECH',
4241 + 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
4242 + ],
4243 + [
4244 + 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
4245 + 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
4246 + ],
4247 + [
4248 + 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
4249 + 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
4250 + ]
4251 + ]
4252 + ]);
4253 +
4254 + // Prepare the API endpoint
4255 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
4256 +
4257 + // Set up the API request
4258 + $args = [
4259 + 'body' => $body,
4260 + 'headers' => [
4261 + 'Content-Type' => 'application/json',
4262 + ],
4263 + 'timeout' => 60,
4264 + 'redirection' => 5,
4265 + 'blocking' => true,
4266 + 'httpversion' => '1.0',
4267 + 'sslverify' => true,
4268 + ];
4269 +
4270 + // Make the API request
4271 + $response = wp_remote_post($api_endpoint, $args);
4272 +
4273 + // Process the response
4274 + if (is_wp_error($response)) {
4275 + return "Sorry, there was an error processing your request: " . $response->get_error_message();
4276 + }
4277 +
4278 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
4279 +
4280 + // Handle potential errors in the response
4281 + if (isset($response_body['error'])) {
4282 + //error_log('Gemini API Error: ' . json_encode($response_body['error']));
4283 + return "Sorry, there was an error with the Gemini API: " .
4284 + (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
4285 + }
4286 +
4287 + // Extract the response text
4288 + if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
4289 + return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
4290 + } else {
4291 + //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
4292 + return "Sorry, I couldn't process that request. The response format was unexpected.";
4293 + }
4294 +}
4295 +
4296 +
4297 +
4298 +public function mxchat_dismiss_pre_chat_message() {
4299 + // Get and sanitize the user identifier
4300 + $user_id = $this->mxchat_get_user_identifier();
4301 + $user_id = sanitize_key($user_id);
4302 +
4303 + // Set a transient to track that the user has dismissed the pre-chat message
4304 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
4305 + set_transient($transient_key, true, DAY_IN_SECONDS);
4306 +
4307 + wp_send_json_success();
4308 +}
4309 +
4310 +public function mxchat_check_pre_chat_message_status() {
4311 + // Get and sanitize the user identifier
4312 + $user_id = $this->mxchat_get_user_identifier();
4313 + $user_id = sanitize_key($user_id);
4314 +
4315 + // Check if the transient exists (i.e., if the message was dismissed)
4316 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
4317 + $dismissed = get_transient($transient_key);
4318 +
4319 + // Log the result to see if it's being set correctly
4320 + //error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
4321 +
4322 + if ($dismissed) {
4323 + wp_send_json_success(['dismissed' => true]);
4324 + } else {
4325 + wp_send_json_success(['dismissed' => false]);
4326 + }
4327 +
4328 + wp_die();
4329 +}
4330 +
4331 +private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
4332 + if (!is_array($vectorA) || !is_array($vectorB) || empty($vectorA) || empty($vectorB)) {
4333 + return 0;
4334 + }
4335 +
4336 + $dotProduct = array_sum(array_map(function ($a, $b) {
4337 + return $a * $b;
4338 + }, $vectorA, $vectorB));
4339 + $normA = sqrt(array_sum(array_map(function ($a) {
4340 + return $a * $a;
4341 + }, $vectorA)));
4342 + $normB = sqrt(array_sum(array_map(function ($b) {
4343 + return $b * $b;
4344 + }, $vectorB)));
4345 +
4346 + if ($normA == 0 || $normB == 0) {
4347 + return 0;
4348 + }
4349 +
4350 + return $dotProduct / ($normA * $normB);
4351 + }
4352 +
4353 +public function mxchat_enqueue_scripts_styles() {
4354 + // Define version numbers for the styles and scripts
4355 + $chat_style_version = '2.1.9';
4356 + $chat_script_version = '2.1.9';
4357 +
4358 + // Enqueue the script
4359 + wp_enqueue_script(
4360 + 'mxchat-chat-js',
4361 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
4362 + array('jquery'),
4363 + $chat_script_version,
4364 + true
4365 + );
4366 +
4367 + // Enqueue the CSS
4368 + wp_enqueue_style(
4369 + 'mxchat-chat-css',
4370 + plugin_dir_url(__FILE__) . '../css/chat-style.css',
4371 + array(),
4372 + $chat_style_version
4373 + );
4374 +
4375 + // Fetch options from the database
4376 + $this->options = get_option('mxchat_options');
4377 + $prompts_options = get_option('mxchat_prompts_options', array());
4378 +
4379 + // Prepare settings for JavaScript
4380 + $style_settings = array(
4381 + 'ajax_url' => admin_url('admin-ajax.php'),
4382 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
4383 + 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
4384 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
4385 + 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
4386 + 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
4387 + 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
4388 + 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
4389 + 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
4390 + 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
4391 + 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
4392 + 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
4393 + 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
4394 + 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
4395 + 'icon_color' => $this->options['icon_color'] ?? '#fff',
4396 + 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
4397 + 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
4398 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off', // Example for consistency
4399 +
4400 + 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
4401 + 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
4402 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
4403 + 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
4404 + 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
4405 + 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
4406 +
4407 + 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
4408 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
4409 + );
4410 +
4411 + // Pass the settings to the script
4412 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
4413 +}
4414 +
4415 +
4416 +// Modify the mxchat_reset_rate_limits function to handle different timeframes
4417 +public function mxchat_reset_rate_limits() {
4418 + global $wpdb;
4419 + $all_options = get_option('mxchat_options', []);
4420 + $current_time = time();
4421 +
4422 + // Get all rate limit options
4423 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
4424 +
4425 + foreach ($option_names as $option_name) {
4426 + // Parse the option name to extract role and user ID
4427 + // Format: mxchat_chat_limit_ROLE_USERID or mxchat_chat_limit_logged_out_IP
4428 + $parts = explode('_', $option_name);
4429 +
4430 + // Skip if the option name doesn't match our expected format
4431 + if (count($parts) < 4) {
4432 + continue;
4433 + }
4434 +
4435 + // Extract role (may be multiple parts like 'shop_manager')
4436 + $role_parts = array_slice($parts, 3, -1); // Get all parts between 'mxchat_chat_limit_' and the last part (user ID)
4437 + $role = implode('_', $role_parts);
4438 +
4439 + // Skip if role doesn't exist in our settings
4440 + if (!isset($all_options['rate_limits'][$role])) {
4441 + continue;
4442 + }
4443 +
4444 + $timeframe = $all_options['rate_limits'][$role]['timeframe'];
4445 + $limit_data = get_option($option_name);
4446 +
4447 + if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
4448 + continue;
4449 + }
4450 +
4451 + $timestamp = $limit_data['timestamp'];
4452 + $should_reset = false;
4453 +
4454 + // Determine if we should reset based on the timeframe
4455 + switch ($timeframe) {
4456 + case 'hourly':
4457 + $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
4458 + break;
4459 + case 'daily':
4460 + $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
4461 + break;
4462 + case 'weekly':
4463 + $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
4464 + break;
4465 + case 'monthly':
4466 + $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
4467 + break;
4468 + }
4469 +
4470 + // Reset the counter if the timeframe has passed
4471 + if ($should_reset) {
4472 + delete_option($option_name);
4473 + wp_cache_delete($option_name, 'options');
4474 + }
4475 + }
4476 +
4477 + // Clean up any orphaned entries
4478 + wp_cache_delete('mxchat_all_chat_limits', 'options');
4479 +}
4480 +
4481 +private function mxchat_fetch_woocommerce_products() {
4482 + // Ensure WooCommerce is active
4483 + if (!class_exists('WooCommerce')) {
4484 + return [];
4485 + }
4486 +
4487 + $args = array(
4488 + 'post_type' => 'product',
4489 + 'post_status' => 'publish',
4490 + 'posts_per_page' => -1,
4491 + );
4492 +
4493 + $products = get_posts($args);
4494 + $product_data = [];
4495 +
4496 + foreach ($products as $product) {
4497 + $product_id = $product->ID;
4498 + $product_obj = wc_get_product($product_id);
4499 +
4500 + $product_data[] = array(
4501 + 'id' => $product_id,
4502 + 'name' => $product_obj->get_name(),
4503 + 'description' => $product_obj->get_description(),
4504 + 'short_description' => $product_obj->get_short_description(),
4505 + 'url' => get_permalink($product_id),
4506 + 'price' => $product_obj->get_regular_price(),
4507 + 'sale_price' => $product_obj->get_sale_price(),
4508 + 'stock_status' => $product_obj->get_stock_status(),
4509 + 'sku' => $product_obj->get_sku(),
4510 + 'in_stock' => $product_obj->is_in_stock(),
4511 + 'total_sales' => $product_obj->get_total_sales(),
4512 + );
4513 + }
4514 +
4515 + return $product_data;
4516 +}
4517 +
4518 +/**
4519 + * Check if the current user has exceeded their rate limit based on role
4520 + *
4521 + * @return true|array True if limit not exceeded, or array with error message if exceeded
4522 + */
4523 +public function check_rate_limit() {
4524 + $all_options = get_option('mxchat_options', []);
4525 + //error_log('MXChat Rate Limit: Starting check');
4526 + //error_log('MXChat Rate Limit: Options: ' . print_r($all_options, true));
4527 +
4528 + // Determine user role or if logged out
4529 + if (is_user_logged_in()) {
4530 + $user = wp_get_current_user();
4531 + $user_id = $user->ID;
4532 +
4533 + // Get the user's primary role using reset() to safely get the first element
4534 + $user_roles = $user->roles;
4535 +
4536 + // Safely get the first role regardless of array key structure
4537 + if (!empty($user_roles) && is_array($user_roles)) {
4538 + $role = reset($user_roles); // This safely gets the first element regardless of key
4539 + } else {
4540 + $role = 'subscriber'; // Default to subscriber if no role found
4541 + }
4542 +
4543 + //error_log('MXChat Rate Limit: User ID: ' . $user_id . ', Role: ' . $role);
4544 + } else {
4545 + $role = 'logged_out';
4546 + // Use IP address for non-logged-in users
4547 + $user_id = $this->get_client_ip();
4548 + //error_log('MXChat Rate Limit: Logged out user IP: ' . $user_id);
4549 + }
4550 +
4551 + // Check if rate limits are configured for this role
4552 + if (!isset($all_options['rate_limits'][$role])) {
4553 + //error_log('MXChat Rate Limit: No rate limit configured for role: ' . $role);
4554 + return true; // No limit set for this role
4555 + }
4556 +
4557 + $limit = $all_options['rate_limits'][$role]['limit'];
4558 + //error_log('MXChat Rate Limit: Limit for role ' . $role . ': ' . $limit);
4559 +
4560 + // If unlimited, return true immediately
4561 + if ($limit === 'unlimited') {
4562 + //error_log('MXChat Rate Limit: Unlimited setting, no limit applied');
4563 + return true;
4564 + }
4565 +
4566 + // Get the option name for this user/role
4567 + $option_name = 'mxchat_chat_limit_' . $role . '_' . $user_id;
4568 + //error_log('MXChat Rate Limit: Option name: ' . $option_name);
4569 +
4570 + // Get the counter data
4571 + $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
4572 + //error_log('MXChat Rate Limit: Current limit data: ' . print_r($limit_data, true));
4573 +
4574 + // If first request or counter reset needed, set the initial timestamp
4575 + if ($limit_data['count'] === 0) {
4576 + $limit_data['timestamp'] = time();
4577 + update_option($option_name, $limit_data);
4578 + //error_log('MXChat Rate Limit: First request, initialized timestamp');
4579 + }
4580 +
4581 + // Get the timeframe
4582 + $timeframe = isset($all_options['rate_limits'][$role]['timeframe']) ?
4583 + $all_options['rate_limits'][$role]['timeframe'] : 'daily';
4584 + //error_log('MXChat Rate Limit: Timeframe: ' . $timeframe);
4585 +
4586 + // Check if the counter needs to be reset based on timeframe
4587 + $current_time = time();
4588 + $timestamp = $limit_data['timestamp'];
4589 + $should_reset = false;
4590 +
4591 + switch ($timeframe) {
4592 + case 'hourly':
4593 + $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
4594 + break;
4595 + case 'daily':
4596 + $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
4597 + break;
4598 + case 'weekly':
4599 + $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
4600 + break;
4601 + case 'monthly':
4602 + $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
4603 + break;
4604 + }
4605 +
4606 + //error_log('MXChat Rate Limit: Current time: ' . $current_time . ', Last timestamp: ' . $timestamp);
4607 + //error_log('MXChat Rate Limit: Time elapsed: ' . ($current_time - $timestamp) . ' seconds');
4608 + //error_log('MXChat Rate Limit: Should reset: ' . ($should_reset ? 'Yes' : 'No'));
4609 +
4610 + // Reset the counter if the timeframe has passed
4611 + if ($should_reset) {
4612 + $limit_data = ['count' => 0, 'timestamp' => $current_time];
4613 + update_option($option_name, $limit_data);
4614 + //error_log('MXChat Rate Limit: Reset counter to 0');
4615 + }
4616 +
4617 + // Check if user has exceeded their limit
4618 + if ($limit_data['count'] >= intval($limit)) {
4619 + // Get the custom message for this role
4620 + $message = !empty($all_options['rate_limits'][$role]['message'])
4621 + ? $all_options['rate_limits'][$role]['message']
4622 + : __('Rate limit exceeded. Please try again later.', 'mxchat');
4623 +
4624 + // Add timeframe information to the message if placeholders exist
4625 + $timeframe_label = '';
4626 + switch ($timeframe) {
4627 + case 'hourly':
4628 + $timeframe_label = __('hour', 'mxchat');
4629 + break;
4630 + case 'daily':
4631 + $timeframe_label = __('day', 'mxchat');
4632 + break;
4633 + case 'weekly':
4634 + $timeframe_label = __('week', 'mxchat');
4635 + break;
4636 + case 'monthly':
4637 + $timeframe_label = __('month', 'mxchat');
4638 + break;
4639 + }
4640 +
4641 + // Replace placeholders in the message
4642 + $message = str_replace(
4643 + ['{limit}', '{count}', '{remaining}', '{timeframe}'],
4644 + [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
4645 + $message
4646 + );
4647 +
4648 + //error_log('MXChat Rate Limit: Limit exceeded. Message: ' . $message);
4649 +
4650 + // Return error with the custom message
4651 + return [
4652 + 'error' => true,
4653 + 'message' => $message
4654 + ];
4655 + }
4656 +
4657 + // Increment the counter
4658 + $limit_data['count']++;
4659 + update_option($option_name, $limit_data);
4660 + //error_log('MXChat Rate Limit: Incremented counter to ' . $limit_data['count']);
4661 +
4662 + return true;
4663 +}
4664 +
4665 +// Helper function to get client IP address
4666 +private function get_client_ip() {
4667 + // Check for shared internet/ISP IP
4668 + if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
4669 + return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
4670 + }
4671 +
4672 + // Check for IPs passing through proxies
4673 + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
4674 + // Use the first value in the comma-separated list
4675 + $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
4676 + return trim($forwarded_for[0]);
4677 + }
4678 +
4679 + if (!empty($_SERVER['REMOTE_ADDR'])) {
4680 + return sanitize_text_field($_SERVER['REMOTE_ADDR']);
4681 + }
4682 +
4683 + // Fallback
4684 + return 'unknown';
4685 +}
4686 +
4687 +}
4688 +?>