PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.0.7
MxChat – AI Chatbot & Content Generation for WordPress v2.0.7
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-integrator.php +1563 -9782 3.2.92.0.7 View file →
@@ -9,286 +9,50 @@
9 9 private $chat_count;
10 10 private $fallbackResponse;
11 11 private $productCardHtml;
12 12 private $word_handler;
13 - private $last_similarity_analysis = null;
14 - private $current_valid_urls = [];
15 - private $last_vectorstore_error = null;
16 - private $is_streaming = false; // ADDED: Track if current request is streaming
17 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
18 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
19 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
20 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
21 13
22 -/**
23 - * Setup streaming headers - call this right before actually streaming
24 - * This delays header setup to allow actions/forms to return JSON responses
25 - */
26 -/**
27 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
28 - *
29 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
30 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
31 - * specific "overloaded" / "rate limit" body string. Returns immediately on
32 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
33 - *
34 - * Drop-in replacement for wp_remote_post — returns the same shape
35 - * (WP_Error or response array) so the caller's existing error-handling
36 - * code path is unchanged.
37 - *
38 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
39 - * paths (the *_response_openai / *_response_claude / etc functions).
40 - * For the *_stream variants, the cURL initial-connect happens inside a
41 - * read-chunks loop — retrying there safely (without re-emitting partial
42 - * stream chunks to the client) is a separate problem. Streaming paths
43 - * are NOT wrapped in this build; tracked as a follow-on.
44 - *
45 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
46 - * (default true). When false, behavior is identical to plain wp_remote_post.
47 - */
48 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
49 - $opts = is_array($this->options ?? null) ? $this->options : array();
50 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
51 - (string) $opts['auto_retry_on_transient_error'] !== '0';
52 -
53 - if (!$enabled) {
54 - return wp_remote_post($url, $args);
55 - }
56 -
57 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
58 - $last_response = null;
59 -
60 - foreach ($backoffs as $i => $delay_ms) {
61 - if ($delay_ms > 0) {
62 - usleep($delay_ms * 1000);
63 - }
64 - $response = wp_remote_post($url, $args);
65 - $last_response = $response;
66 -
67 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
68 - return $response;
69 - }
70 -
71 - if (defined('WP_DEBUG') && WP_DEBUG) {
72 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
73 - : (int) wp_remote_retrieve_response_code($response);
74 - error_log(sprintf(
75 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
76 - $provider_hint ?: 'unknown',
77 - $i + 1,
78 - $code_for_log,
79 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
80 - ));
81 - }
82 - }
83 -
84 - return $last_response;
85 -}
86 -
87 -/**
88 - * Returns true if a wp_remote_post response represents a TRANSIENT
89 - * provider error worth retrying. Conservative — only retries on signals
90 - * that are very likely to clear within a few seconds.
91 - *
92 - * Transient signals:
93 - * - WP_Error with timeout / connection / dns / ssl
94 - * - HTTP 429, 502, 503, 504
95 - * - Provider-specific overload bodies (gemini "overloaded", openai
96 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
97 - *
98 - * NOT transient (return false — fail-fast):
99 - * - 200/2xx (success)
100 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
101 - * budget; the user needs to fix something)
102 - * - Any other 4xx (assume permanent unless explicitly listed above)
103 - * - 5xx other than the four listed above (e.g. 500 generic server error
104 - * is often a malformed request on our side, not a transient outage)
105 - */
106 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
107 - if (is_wp_error($response)) {
108 - $code = $response->get_error_code();
109 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
110 - || stripos((string) $response->get_error_message(), 'timed out') !== false
111 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
112 - }
113 -
114 - $status = (int) wp_remote_retrieve_response_code($response);
115 - if (in_array($status, array(429, 502, 503, 504), true)) {
116 - return true;
117 - }
118 - if ($status >= 200 && $status < 300) {
119 - return false;
120 - }
121 - // Permanent 4xx that should fail fast — even with no body.
122 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
123 - return false;
124 - }
125 -
126 - // Provider-specific body inspection for the cases where the upstream
127 - // returns 200 with an error envelope (gemini does this for overload).
128 - $body = (string) wp_remote_retrieve_body($response);
129 - if ($body === '') {
130 - return false;
131 - }
132 - $lower = strtolower($body);
133 - $hint = strtolower((string) $provider_hint);
134 -
135 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
136 - || strpos($lower, 'high demand') !== false
137 - || strpos($lower, 'model is overloaded') !== false)) {
138 - return true;
139 - }
140 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
141 - || strpos($lower, '"type":"server_error"') !== false
142 - || strpos($lower, '"code":"server_error"') !== false)) {
143 - return true;
144 - }
145 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
146 - || strpos($lower, 'overloaded_error') !== false)) {
147 - return true;
148 - }
149 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
150 - return true;
151 - }
152 -
153 - return false;
154 -}
155 -
156 -/**
157 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
158 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
159 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
160 - * collect status separately from a plain wp_remote_post array shape, so the
161 - * non-streaming helper above can't be called directly. This delegate keeps
162 - * the classification rules identical across both paths.
163 - */
164 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
165 - if ($curl_errno) {
166 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
167 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
168 - return in_array($curl_errno, array(
169 - CURLE_OPERATION_TIMEDOUT,
170 - CURLE_COULDNT_CONNECT,
171 - CURLE_COULDNT_RESOLVE_HOST,
172 - CURLE_SSL_CONNECT_ERROR,
173 - CURLE_GOT_NOTHING,
174 - CURLE_SEND_ERROR,
175 - CURLE_RECV_ERROR,
176 - ), true);
177 - }
178 -
179 - $status = (int) $http_code;
180 - if (in_array($status, array(429, 502, 503, 504), true)) {
181 - return true;
182 - }
183 - if ($status >= 200 && $status < 300) {
184 - return false;
185 - }
186 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
187 - return false;
188 - }
189 -
190 - $body = (string) $body;
191 - if ($body === '') {
192 - return false;
193 - }
194 - $lower = strtolower($body);
195 - $hint = strtolower((string) $provider_hint);
196 -
197 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
198 - || strpos($lower, 'high demand') !== false
199 - || strpos($lower, 'model is overloaded') !== false)) {
200 - return true;
201 - }
202 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
203 - || strpos($lower, '"type":"server_error"') !== false
204 - || strpos($lower, '"code":"server_error"') !== false)) {
205 - return true;
206 - }
207 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
208 - || strpos($lower, 'overloaded_error') !== false)) {
209 - return true;
210 - }
211 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
212 - return true;
213 - }
214 -
215 - return false;
216 -}
217 -
218 -/**
219 - * Whether transient-error auto-retry is enabled in admin settings.
220 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
221 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
222 - */
223 -private function mxchat_retry_enabled() {
224 - $opts = is_array($this->options ?? null) ? $this->options : array();
225 - return !isset($opts['auto_retry_on_transient_error']) ||
226 - (string) $opts['auto_retry_on_transient_error'] !== '0';
227 -}
228 -
229 -private function setup_streaming_headers() {
230 - if ($this->streaming_headers_sent || headers_sent()) {
231 - return false;
232 - }
233 -
234 - // Disable output buffering
235 - while (ob_get_level()) {
236 - ob_end_flush();
237 - }
238 -
239 - // Set headers for SSE
240 - header('Content-Type: text/event-stream');
241 - header('Cache-Control: no-cache');
242 - header('Connection: keep-alive');
243 - header('X-Accel-Buffering: no');
244 -
245 - ob_implicit_flush(true);
246 - flush();
247 -
248 - $this->streaming_headers_sent = true;
249 - return true;
250 -}
251 -
252 -/**
253 - * Class constructor
254 - */
255 14 public function __construct() {
256 15 $this->options = get_option('mxchat_options');
257 16 $this->prompts_options = get_option('mxchat_prompts_options', array());
17 +
258 18 $this->chat_count = get_option('mxchat_chat_count', 0);
259 19 $this->word_handler = new MXChat_Word_Handler($this->options);
260 -
261 - // Add all action hooks
20 +
262 21 add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
263 22 add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
264 23 add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
24 +
265 25 add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
266 26 add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
267 -
268 27 // Add the AJAX actions for checking if the pre-chat message was dismissed
269 28 add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
270 29 add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
30 +
271 31 add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
272 32 add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
33 +
273 34 add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
274 35 add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
275 -
36 +
37 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
38 + wp_schedule_event(time(), 'daily', 'mxchat_reset_rate_limits');
39 + }
40 +
276 41 // Add REST API routes registration
277 42 add_action('rest_api_init', array($this, 'register_routes'));
43 +
278 44 add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
279 45 add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
280 -
281 - // Rate limit action - notice we removed the old schedule setup
46 +
282 47 add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
283 -
284 - // File upload and handling actions
48 +
285 49 add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
286 50 add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
287 51 add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
288 52 add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
289 -
290 - // Word document handling actions
53 +
54 + // Add these with your other add_action hooks
291 55 add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
292 56 add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
293 57 add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
294 58 add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
@@ -293,128 +57,16 @@
293 57 add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
294 58 add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
295 59 add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
296 60 add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
297 -
298 - // Email handling actions
61 +
299 62 add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
300 63 add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
301 64 add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
302 65 add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
303 -
304 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
305 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
306 -
307 - // Testing panel AJAX actions
308 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
309 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
310 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
311 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
312 - // Add to your existing constructor, in the section with other AJAX actions:
313 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
314 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
315 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
316 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
317 - // Add chat mode checking actions
318 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
319 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
320 -
321 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
322 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
323 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
324 -
325 - // Auto-email transcript action
326 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
327 -
328 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
329 -
330 -
331 66 }
332 67
333 -/**
334 - * Return a fresh nonce so cached pages can replace the stale one.
335 - * With `with_settings`, also returns the current behavior-gate settings so
336 - * the widget can correct stale inline-localized values (plan-32db95).
337 - */
338 -public function mxchat_refresh_nonce() {
339 - nocache_headers();
340 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
341 - if (!empty($_REQUEST['with_settings'])) {
342 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
343 - }
344 - wp_send_json_success($payload);
345 -}
346 68
347 -/**
348 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
349 - *
350 - * Every widget setting ships inline in page HTML via wp_localize_script, so
351 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
352 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
353 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
354 - * caches PHP can reach; this payload covers the rest — the widget requests
355 - * it on first open (via the nonce-refresh endpoints) and merges it over
356 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
357 - * nonce uses.
358 - *
359 - * Behavior gates + labels ONLY — colors stay inline because they're also
360 - * server-inline-styled, and a runtime swap would visibly flash.
361 - *
362 - * Both wp_localize_script blocks merge this exact array, so the inline and
363 - * refreshed payloads cannot drift.
364 - *
365 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
366 - * instead of trusting the instance copy.
367 - * @return array
368 - */
369 -public function get_dynamic_widget_settings($fresh = false) {
370 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
371 - if (!is_array($options)) {
372 - $options = array();
373 - }
374 - return array(
375 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
376 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
377 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
378 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
379 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
380 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
381 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
382 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
383 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
384 - // scalars to string, and (string) false === '' — which the widget's
385 - // old gate read as enabled (plan-4bba64). The filter keeps its
386 - // boolean contract; only the emitted value is stringified.
387 - 'satisfaction_rating_enabled' => apply_filters(
388 - 'mxchat_satisfaction_rating_enabled',
389 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
390 - ) ? 'on' : 'off',
391 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
392 - 'satisfaction_rating_copy' => array(
393 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
394 - 'helpful' => esc_html__('Helpful', 'mxchat'),
395 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
396 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
397 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
398 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
399 - 'send' => esc_html__('Send', 'mxchat'),
400 - 'skip' => esc_html__('Skip', 'mxchat'),
401 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
402 - ),
403 - );
404 -}
405 -
406 -// In your core plugin's check_actions_for_addons method:
407 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
408 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
409 -
410 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
411 -
412 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
413 -
414 - return $result;
415 -}
416 -
417 69 private function mxchat_increment_chat_count() {
418 70 $chat_count = get_option('mxchat_chat_count', 0);
419 71 $chat_count++;
420 72 update_option('mxchat_chat_count', $chat_count);
@@ -426,22 +78,8 @@
426 78 wp_die();
427 79 }
428 80
429 81 $session_id = sanitize_text_field($_POST['session_id']);
430 -
431 - // SECURITY FIX: Verify session ownership before retrieving data
432 - // If IP/user changed, signal frontend to reset session instead of blocking
433 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
434 -
435 - // Check if this session has an owner recorded
436 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
437 -
438 - // Update session owner if it changed (e.g. IP changed due to network switch)
439 - // The session ID itself is the authentication — if the client has it, they own it
440 - if (!$session_owner || $session_owner !== $current_user_identifier) {
441 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
442 - }
443 -
444 82 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
445 83 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
446 84
447 85 if (empty($history)) {
@@ -458,25 +96,26 @@
458 96 'chat_mode' => $chat_mode
459 97 ]);
460 98 wp_die();
461 99 }
462 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
463 - $history = get_option("mxchat_history_{$session_id}", []);
100 +private function mxchat_fetch_conversation_history_for_ajax($session_id) {
101 + $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history based on session ID
102 + $formatted_history = [];
464 103
465 - // Check persistence setting - when OFF, only include messages from current page load
466 - $options = get_option('mxchat_options', []);
467 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
104 + // Format the history to align with the expected structure for OpenAI
105 + foreach ($history as $entry) {
106 + $formatted_history[] = [
107 + 'role' => $entry['role'], // Ensure this matches 'user' or 'assistant'
108 + 'content' => $entry['content']
109 + ];
110 + }
468 111
469 - // Filter history when persistence is OFF to match what the user sees
470 - if (!$persistence_enabled && $session_start_timestamp > 0) {
471 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
472 - // Include messages from this page load onwards
473 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
474 - });
475 - // Re-index array after filtering
476 - $history = array_values($history);
477 - }
112 + return $formatted_history;
113 +}
478 114
115 +
116 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
117 + $history = get_option("mxchat_history_{$session_id}", []);
479 118 $formatted_history = [];
480 119
481 120 // Adjusted for code-heavy conversations
482 121 $max_tokens = 120000; // Context window size
@@ -511,9 +150,9 @@
511 150 if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
512 151 continue;
513 152 }
514 153
515 - // More accurate token estimation (1 token ≈ 4 characters)
154 + // More accurate token estimation (1 token ≈ 4 characters)
516 155 $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
517 156
518 157 // Check token budget with the new estimate
519 158 if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
@@ -550,17 +189,8 @@
550 189
551 190 public function register_routes() {
552 191 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
553 192
554 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
555 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
556 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
557 - register_rest_route('mxchat/v1', '/nonce', [
558 - 'methods' => 'GET',
559 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
560 - 'permission_callback' => '__return_true',
561 - ]);
562 -
563 193 register_rest_route('mxchat/v1', '/stream', [
564 194 'methods' => 'GET',
565 195 'callback' => [$this, 'mxchat_stream_events'],
566 196 'permission_callback' => [$this, 'verify_chat_session'],
@@ -576,112 +206,13 @@
576 206 'methods' => 'POST',
577 207 'callback' => [$this, 'handle_slack_interaction'],
578 208 'permission_callback' => [$this, 'verify_slack_request'],
579 209 ]);
580 -
581 - register_rest_route('mxchat/v1', '/slack-messages', [
582 - 'methods' => 'POST',
583 - 'callback' => [$this, 'handle_slack_messages'],
584 - 'permission_callback' => [$this, 'verify_slack_request'],
585 - ]);
586 210
587 - // Telegram webhook endpoint
588 - register_rest_route('mxchat/v1', '/telegram-webhook', [
589 - 'methods' => 'POST',
590 - 'callback' => [$this, 'handle_telegram_webhook'],
591 - 'permission_callback' => [$this, 'verify_telegram_request'],
592 - ]);
593 -
594 211 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
595 212 }
596 213
597 214 /**
598 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
599 - * caches it for the session and includes it on every chat-send / stream-send /
600 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
601 - * we eliminate the entire class of "first-message Access denied" failures that
602 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
603 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
604 - * lives in the HTML body.
605 - *
606 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
607 - * client browser can't be used to flood the nonce-issuance path.
608 - *
609 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
610 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
611 - * backwards-compat window so cached pages still in users' browsers don't break
612 - * mid-session.
613 - *
614 - * @since 3.2.7
615 - */
616 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
617 - $ip = '';
618 - if (!empty($_SERVER['REMOTE_ADDR'])) {
619 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
620 - }
621 - if ($ip !== '') {
622 - // Best-effort rate limit. WP transients with sub-second TTL are racy
623 - // (parallel bursts can squeak through before set_transient completes);
624 - // we use 2s to make the gate slightly more reliable. Real production
625 - // rate-limiting at sub-second granularity needs Redis or DB row locks
626 - // — out of scope for this endpoint, which is already cheap.
627 - $key = 'mxchat_nonce_rl_' . md5($ip);
628 - if (get_transient($key)) {
629 - return new WP_REST_Response(array(
630 - 'error' => 'rate_limited',
631 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
632 - ), 429);
633 - }
634 - set_transient($key, 1, 2);
635 - }
636 -
637 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
638 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
639 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
640 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
641 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
642 - if ( ! is_user_logged_in() ) {
643 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
644 - if ( $maybe_uid ) {
645 - wp_set_current_user( $maybe_uid );
646 - }
647 - }
648 -
649 - $payload = array(
650 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
651 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
652 - );
653 -
654 - // plan-32db95: the widget's first-open refresh asks for current behavior
655 - // settings in the same round-trip, so stale inline-localized values on
656 - // cached pages get corrected without a second request. All values in
657 - // this payload already ship in public page HTML — nothing sensitive.
658 - if ($request->get_param('with_settings')) {
659 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
660 - }
661 -
662 - return new WP_REST_Response($payload, 200);
663 -}
664 -
665 -/**
666 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
667 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
668 - * action (inline-localized in older cached HTML). The legacy acceptance is
669 - * a 30-day backwards-compat window — to be removed in a follow-up release
670 - * after 2026-06-27.
671 - *
672 - * @param string $posted_nonce
673 - * @return bool
674 - */
675 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
676 - if (!is_string($posted_nonce) || $posted_nonce === '') {
677 - return false;
678 - }
679 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
680 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
681 -}
682 -
683 -/**
684 215 * Verify valid chat session
685 216 */
686 217 public function verify_chat_session($request) {
687 218 $session_id = $request->get_param('session_id');
@@ -717,11 +248,10 @@
717 248 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
718 249 return false;
719 250 }
720 251
721 - // Get raw request body from the WP_REST_Request object
722 - // (php://input may already be consumed by WordPress at this point)
723 - $request_body = $request->get_body();
252 + // Get raw request body
253 + $request_body = file_get_contents('php://input');
724 254
725 255 // Create the signature base string
726 256 $sig_basestring = "v0:{$timestamp}:{$request_body}";
727 257
@@ -730,43 +260,8 @@
730 260
731 261 // Compare signatures
732 262 return hash_equals($my_signature, $slack_signature);
733 263 }
734 -
735 -/**
736 - * Verify request is coming from Telegram.
737 - *
738 - * @param WP_REST_Request $request
739 - * @return bool True if valid, false otherwise.
740 - */
741 -public function verify_telegram_request($request) {
742 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
743 -
744 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
745 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
746 -
747 - if (empty($secret_token)) {
748 - // If no secret is configured, allow the request (for initial setup)
749 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
750 - return true;
751 - }
752 -
753 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
754 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
755 -
756 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
757 -
758 - if (empty($request_token)) {
759 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
760 - return false;
761 - }
762 -
763 - // Timing-safe comparison
764 - $result = hash_equals($secret_token, $request_token);
765 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
766 - return $result;
767 -}
768 -
769 264 public function mxchat_stream_events(WP_REST_Request $request) {
770 265 header('Content-Type: text/event-stream');
771 266 header('Cache-Control: no-cache');
772 267 header('Connection: keep-alive');
@@ -800,45 +295,20 @@
800 295
801 296
802 297
803 298
804 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
299 +private function mxchat_save_chat_message($session_id, $role, $message) {
805 300 global $wpdb;
301 +
806 302 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
807 303 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
808 -
809 - // Check if this is the first message in a new session (before any other database operations)
810 - $is_new_session = false;
811 - if ($role === 'user') { // Only check for user messages, not bot responses
812 - $existing_messages = $wpdb->get_var($wpdb->prepare(
813 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
814 - $session_id
815 - ));
816 - $is_new_session = ($existing_messages == 0);
817 -
818 - // Log for debugging
819 - if ($is_new_session) {
820 - //error_log("[DEBUG] This is a NEW session - first message");
821 - }
822 - }
823 -
824 - // SECURITY FIX: Set session ownership for new sessions
825 - if ($is_new_session && $role === 'user') {
826 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
827 - $session_owner_key = "mxchat_session_owner_{$session_id}";
828 -
829 - // Only set ownership if not already set
830 - if (!get_option($session_owner_key)) {
831 - update_option($session_owner_key, $current_user_identifier, 'no');
832 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
833 - }
834 - }
835 -
304 +
836 305 // 1) Extract agent name if present
837 306 $agent_name = '';
838 307 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
839 308 $agent_name = $matches[1];
840 309 $message = str_replace("Agent: $agent_name - ", '', $message);
310 +
841 311 $session_meta_key = "mxchat_agent_name_{$session_id}";
842 312 if (empty(get_option($session_meta_key))) {
843 313 update_option($session_meta_key, $agent_name);
844 314 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
@@ -843,57 +313,42 @@
843 313 update_option($session_meta_key, $agent_name);
844 314 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
845 315 }
846 316 }
847 -
317 +
848 318 // 2) Generate unique message_id
849 319 $message_id = uniqid();
850 320 //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
851 -
321 +
852 322 // 3) Determine user_id
853 323 $user_id = is_user_logged_in() ? get_current_user_id() : 0;
854 -
324 +
855 325 // 4) Determine user_identifier
856 326 $user_identifier = $agent_name
857 327 ? $agent_name
858 328 : MxChat_User::mxchat_get_user_identifier();
859 -
329 +
860 330 // 5) Determine displayed_name
861 331 $user_email = MxChat_User::mxchat_get_user_email();
862 332 $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
863 -
333 +
864 334 // 6) Check for a saved email in wp_options
865 335 $email_option_key = "mxchat_email_{$session_id}";
866 336 $saved_email = get_option($email_option_key);
867 337 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
868 -
869 - // Check for a saved name in wp_options
870 - $name_option_key = "mxchat_name_{$session_id}";
871 - $saved_name = get_option($name_option_key);
872 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
873 -
874 - // If found, update DB user_email and user_name
875 - if ($saved_email || $saved_name) {
876 - $update_data = [];
877 - if ($saved_email) {
878 - $update_data['user_email'] = $saved_email;
879 - }
880 - if ($saved_name) {
881 - $update_data['user_name'] = $saved_name;
882 - }
883 -
884 - if (!empty($update_data)) {
885 - $update_res = $wpdb->update(
886 - $table_name,
887 - $update_data,
888 - ['session_id' => $session_id],
889 - array_fill(0, count($update_data), '%s'),
890 - ['%s']
891 - );
892 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
893 - }
338 +
339 + // If found, update DB user_email
340 + if ($saved_email) {
341 + $update_res = $wpdb->update(
342 + $table_name,
343 + ['user_email' => $saved_email],
344 + ['session_id' => $session_id],
345 + ['%s'],
346 + ['%s']
347 + );
348 + //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email update for session_id {$session_id}. update_res: {$update_res}");
894 349 }
895 -
350 +
896 351 // 7) Save to session history in wp_options
897 352 $history_key = "mxchat_history_{$session_id}";
898 353 $history = get_option($history_key, []);
899 354 $history[] = [
@@ -902,352 +357,34 @@
902 357 'content' => $message,
903 358 'timestamp' => round(microtime(true) * 1000),
904 359 'agent_name' => $displayed_name,
905 360 ];
906 - update_option($history_key, $history, 'no');
361 + update_option($history_key, $history);
907 362 //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
908 -
363 +
909 364 // 8) Save the message to DB (INSERT)
910 365 $insert_data = [
911 366 'user_id' => $user_id,
912 367 'user_identifier'=> $user_identifier,
913 368 'user_email' => $saved_email ?: $user_email,
914 - 'user_name' => $saved_name ?: '', // Add name to insert data
915 369 'session_id' => $session_id,
916 370 'role' => $role,
917 371 'message' => $message,
918 372 'timestamp' => current_time('mysql', 1),
919 373 ];
920 -
921 - // IMPROVED: Handle originating page data
922 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
923 -
924 - if ($columns_exist) {
925 - if ($is_new_session && $role === 'user') {
926 - // For the first user message, set originating page data
927 -
928 - // First check if we have it from the parameter
929 - if ($originating_page && !empty($originating_page['url'])) {
930 - $insert_data['originating_page_url'] = $originating_page['url'];
931 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
932 -
933 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
934 - }
935 - // Otherwise check if it's stored in the instance property
936 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
937 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
938 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
939 -
940 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
941 -
942 - // Clear after using (= null, not unset(): unset() undeclares the property
943 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
944 - $this->pending_originating_page = null;
945 - }
946 - // Fallback to HTTP_REFERER if nothing else is available
947 - else if (isset($_SERVER['HTTP_REFERER'])) {
948 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
949 - $insert_data['originating_page_url'] = $referer_url;
950 -
951 - // Generate title from URL
952 - $parsed_url = parse_url($referer_url);
953 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
954 -
955 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
956 - $insert_data['originating_page_title'] = 'Homepage';
957 - } else {
958 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
959 - $insert_data['originating_page_title'] = ucwords(trim($title));
960 - }
961 -
962 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
963 - }
964 -
965 - // Store for this session so all messages have the same originating page
966 - if (!empty($insert_data['originating_page_url'])) {
967 - update_option("mxchat_originating_page_{$session_id}", [
968 - 'url' => $insert_data['originating_page_url'],
969 - 'title' => $insert_data['originating_page_title']
970 - ], 'no');
971 - }
972 - } else {
973 - // For subsequent messages in the session, use the stored originating page
974 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
975 - if ($stored_originating && !empty($stored_originating['url'])) {
976 - $insert_data['originating_page_url'] = $stored_originating['url'];
977 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
978 - }
979 - }
980 - }
981 -
982 - // Add RAG context if provided (for bot messages)
983 - if ($rag_context !== null && $role === 'bot') {
984 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
985 - if ($rag_context_column_exists) {
986 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
987 - }
988 - }
989 -
990 374 $wpdb->insert($table_name, $insert_data);
991 375 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
992 -
993 - // 9) Send notification email if this is the first user message in a new session
994 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
995 - $this->send_new_chat_notification($session_id, array(
996 - 'identifier' => $user_identifier,
997 - 'email' => $saved_email ?: $user_email,
998 - 'ip' => $_SERVER['REMOTE_ADDR']
999 - ));
1000 - }
1001 -
1002 - // 10) Schedule delayed transcript email if enabled and message is from user
1003 - if ($wpdb->insert_id && $role === 'user') {
1004 - $this->schedule_delayed_transcript_email($session_id);
1005 - }
1006 -
376 +
1007 377 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1008 378 return $message_id;
1009 379 }
1010 380
1011 -private function send_new_chat_notification($session_id, $user_info = array()) {
1012 - $options = get_option('mxchat_transcripts_options');
1013 -
1014 - // Check if notifications are enabled
1015 - if (empty($options['mxchat_enable_notifications'])) {
1016 - return false;
1017 - }
1018 -
1019 - // Get notification email
1020 - $to = !empty($options['mxchat_notification_email']) ?
1021 - $options['mxchat_notification_email'] :
1022 - get_option('admin_email');
1023 -
1024 - if (!is_email($to)) {
1025 - return false;
1026 - }
1027 -
1028 - // Prepare email content
1029 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1030 -
1031 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1032 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1033 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1034 -
1035 - $message = sprintf(
1036 - "A new chat session has started on your website.\n\n" .
1037 - "Session ID: %s\n" .
1038 - "User: %s\n" .
1039 - "Email: %s\n" .
1040 - "IP Address: %s\n" .
1041 - "Time: %s\n\n" .
1042 - "View transcripts: %s",
1043 - $session_id,
1044 - $user_identifier,
1045 - $user_email,
1046 - $user_ip,
1047 - current_time('mysql'),
1048 - admin_url('admin.php?page=mxchat-transcripts')
1049 - );
1050 -
1051 - // Send email
1052 - return wp_mail($to, $subject, $message);
1053 -}
1054 -
1055 -/**
1056 - * Schedule delayed transcript email for a session
1057 - * Reschedules if a new user message is received
1058 - */
1059 -private function schedule_delayed_transcript_email($session_id) {
1060 - $options = get_option('mxchat_transcripts_options');
1061 -
1062 - // Check if auto-email is enabled
1063 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1064 - return;
1065 - }
1066 -
1067 - // Get notification email
1068 - $email = !empty($options['mxchat_notification_email']) ?
1069 - $options['mxchat_notification_email'] :
1070 - get_option('admin_email');
1071 -
1072 - if (!is_email($email)) {
1073 - return;
1074 - }
1075 -
1076 - // Get delay in minutes (default 30)
1077 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1078 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1079 -
1080 - // Clear any existing scheduled event for this session
1081 - $hook = 'mxchat_send_delayed_transcript';
1082 - $args = array($session_id);
1083 - $timestamp = wp_next_scheduled($hook, $args);
1084 -
1085 - if ($timestamp) {
1086 - wp_unschedule_event($timestamp, $hook, $args);
1087 - }
1088 -
1089 - // Schedule new event
1090 - $schedule_time = time() + ($delay_minutes * 60);
1091 - wp_schedule_single_event($schedule_time, $hook, $args);
1092 -}
1093 -
1094 -/**
1095 - * Check if chat messages contain contact information (email or phone number)
1096 - *
1097 - * @param array $messages Array of message objects with 'message' property
1098 - * @param object|null $session_data Session data object with user_email property
1099 - * @return bool True if contact info found, false otherwise
1100 - */
1101 -private function chat_contains_contact_info($messages, $session_data = null) {
1102 - // Check if session already has a stored email
1103 - if ($session_data && !empty($session_data->user_email)) {
1104 - return true;
1105 - }
1106 -
1107 - // Email regex pattern
1108 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1109 -
1110 - // Phone number patterns (covers various formats including international, WhatsApp style)
1111 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1112 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1113 -
1114 - // Only check user messages (not assistant responses)
1115 - foreach ($messages as $msg) {
1116 - if ($msg->role !== 'user') {
1117 - continue;
1118 - }
1119 -
1120 - $message_text = $msg->message;
1121 -
1122 - // Check for email
1123 - if (preg_match($email_pattern, $message_text)) {
1124 - return true;
1125 - }
1126 -
1127 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1128 - if (preg_match($phone_pattern, $message_text, $matches)) {
1129 - // Count actual digits to avoid matching short numbers
1130 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1131 - if (strlen($digits_only) >= 7) {
1132 - return true;
1133 - }
1134 - }
1135 - }
1136 -
1137 - return false;
1138 -}
1139 -
1140 -/**
1141 - * Send the delayed transcript email with .txt attachment
1142 - */
1143 -public function mxchat_send_delayed_transcript($session_id) {
1144 - global $wpdb;
1145 -
1146 - $options = get_option('mxchat_transcripts_options');
1147 -
1148 - // Get notification email
1149 - $to = !empty($options['mxchat_notification_email']) ?
1150 - $options['mxchat_notification_email'] :
1151 - get_option('admin_email');
1152 -
1153 - if (!is_email($to)) {
1154 - return false;
1155 - }
1156 -
1157 - // Get all messages for this session
1158 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1159 - $messages = $wpdb->get_results($wpdb->prepare(
1160 - "SELECT role, message, timestamp FROM {$table_name}
1161 - WHERE session_id = %s
1162 - ORDER BY timestamp ASC",
1163 - $session_id
1164 - ));
1165 -
1166 - if (empty($messages)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get session metadata
1171 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1172 - $session_data = $wpdb->get_row($wpdb->prepare(
1173 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1174 - $session_id
1175 - ));
1176 -
1177 - // Check if contact info is required and if it's present
1178 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1179 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1180 - // Contact info required but not found - skip sending
1181 - return false;
1182 - }
1183 -
1184 - // Build transcript content
1185 - $transcript_content = "Chat Transcript\n";
1186 - $transcript_content .= "================\n\n";
1187 - $transcript_content .= "Session ID: " . $session_id . "\n";
1188 -
1189 - if ($session_data) {
1190 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1191 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1192 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1193 - }
1194 -
1195 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1196 -
1197 - // Add messages
1198 - foreach ($messages as $msg) {
1199 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1200 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1201 - $transcript_content .= $msg->message . "\n\n";
1202 - }
1203 -
1204 - // Create temporary file for attachment using WP_Filesystem
1205 - $upload_dir = wp_upload_dir();
1206 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1207 - global $wp_filesystem;
1208 - if (empty($wp_filesystem)) {
1209 - require_once ABSPATH . 'wp-admin/includes/file.php';
1210 - WP_Filesystem();
1211 - }
1212 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1213 -
1214 - // Prepare email
1215 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1216 -
1217 - $message = "Please find attached the full chat transcript.\n\n";
1218 - $message .= "Session ID: {$session_id}\n";
1219 -
1220 - if ($session_data) {
1221 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1222 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1223 - }
1224 -
1225 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1226 -
1227 - // Send email with attachment
1228 - $attachments = array($temp_file);
1229 - $result = wp_mail($to, $subject, $message, '', $attachments);
1230 -
1231 - // Clean up temporary file
1232 - if (file_exists($temp_file)) {
1233 - unlink($temp_file);
1234 - }
1235 -
1236 - return $result;
1237 -}
1238 -
1239 -
1240 -
1241 381 public function mxchat_handle_save_email_and_response() {
1242 382 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1243 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1244 383
1245 - nocache_headers();
1246 -
1247 384 // Validate nonce
1248 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1249 - //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
385 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
386 + error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1250 387 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1251 388 wp_die();
1252 389 }
1253 390
@@ -1252,41 +389,22 @@
1252 389 }
1253 390
1254 391 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1255 392 $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1256 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1257 393
1258 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
394 + //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}");
1259 395
1260 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
396 + if (empty($session_id) || empty($email)) {
1261 397 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1262 398 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1263 399 wp_die();
1264 400 }
1265 401
1266 - // Validate name if provided (check if name field is enabled and name is required)
1267 - $options = get_option('mxchat_options', []);
1268 - $name_field_enabled = isset($options['enable_name_field']) &&
1269 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1270 -
1271 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1272 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1273 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1274 - wp_die();
1275 - }
402 + // 1) Always store in wp_options
403 + $option_key = "mxchat_email_{$session_id}";
404 + update_option($option_key, $email);
405 + //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$option_key} => {$email}");
1276 406
1277 - // 1) Always store email in wp_options
1278 - $email_option_key = "mxchat_email_{$session_id}";
1279 - update_option($email_option_key, $email, 'no');
1280 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1281 -
1282 - // Store name in wp_options if provided
1283 - if (!empty($name)) {
1284 - $name_option_key = "mxchat_name_{$session_id}";
1285 - update_option($name_option_key, $name, 'no');
1286 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1287 - }
1288 -
1289 407 // 2) (Optional) Also store in DB if a row already exists
1290 408 global $wpdb;
1291 409 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1292 410
@@ -1296,30 +414,21 @@
1296 414
1297 415 //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1298 416
1299 417 if ($session_count) {
1300 - // Update both user_email and user_name if row(s) exist
1301 - if (!empty($name)) {
1302 - $update_sql = $wpdb->prepare(
1303 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1304 - $email,
1305 - $name,
1306 - $session_id
1307 - );
1308 - } else {
1309 - $update_sql = $wpdb->prepare(
1310 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1311 - $email,
1312 - $session_id
1313 - );
1314 - }
418 + // Update user_email if row(s) exist
419 + $update_sql = $wpdb->prepare(
420 + "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
421 + $email,
422 + $session_id
423 + );
1315 424 $wpdb->query($update_sql);
1316 425 //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1317 426 } else {
1318 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
427 + //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email is only in wp_options.");
1319 428 }
1320 429
1321 - // Provide success response (same as original)
430 + // Provide success response
1322 431 $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1323 432 //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1324 433 wp_send_json_success(['message' => $bot_message]);
1325 434 wp_die();
@@ -1327,17 +436,15 @@
1327 436
1328 437 public function mxchat_check_email_provided() {
1329 438 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1330 439
1331 - nocache_headers();
1332 -
1333 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
440 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1334 441 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1335 442 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1336 443 }
1337 444
1338 445 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1339 - if (empty($session_id) || $session_id === 'null') {
446 + if (empty($session_id)) {
1340 447 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1341 448 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1342 449 }
1343 450
@@ -1344,82 +451,105 @@
1344 451 // Check if the user is logged in
1345 452 if (is_user_logged_in()) {
1346 453 $current_user = wp_get_current_user();
1347 454 //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1348 -
1349 - // Get user's display name for logged in users
1350 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1351 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1352 -
1353 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1354 - if (!empty($user_name)) {
1355 - $response_data['name'] = $user_name;
1356 - }
1357 -
1358 - wp_send_json_success($response_data);
455 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
1359 456 }
1360 457
1361 - // Check if name field is required
1362 - $options = get_option('mxchat_options', []);
1363 - $name_field_enabled = isset($options['enable_name_field']) &&
1364 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
458 + $option_key = "mxchat_email_{$session_id}";
459 + $stored_email = get_option($option_key, '');
1365 460
1366 - $email_option_key = "mxchat_email_{$session_id}";
1367 - $stored_email = get_option($email_option_key, '');
1368 -
1369 - // Check for stored name
1370 - $name_option_key = "mxchat_name_{$session_id}";
1371 - $stored_name = get_option($name_option_key, '');
461 + //error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
1372 462
1373 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1374 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
463 + if (!empty($stored_email)) {
464 + //error_log("[DEBUG] mxchat_check_email_provided -> Email found, returning success");
465 + wp_send_json_success(['email' => $stored_email]);
466 + } else {
467 + //error_log("[DEBUG] mxchat_check_email_provided -> No email found, returning error");
468 + wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
469 + }
470 +}
1375 471
1376 - // Check if we have email and name (if name is required)
1377 - $has_required_info = !empty($stored_email);
1378 -
1379 - if ($name_field_enabled) {
1380 - $has_required_info = $has_required_info && !empty($stored_name);
472 +
473 +// First, add this helper function to get the highest rate limit for a user's roles
474 +private function get_user_role_rate_limit($user_id) {
475 + //error_log(esc_html__("Checking rate limit for user ID: ", 'mxchat') . $user_id);
476 +
477 + if (!$user_id) {
478 + //error_log(esc_html__("No user ID provided, using logged-out limit: ", 'mxchat') . ($this->options['rate_limit_logged_out'] ?? '10'));
479 + return $this->options['rate_limit_logged_out'] ?? '10';
1381 480 }
1382 481
1383 - if ($has_required_info) {
1384 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1385 -
1386 - $response_data = ['email' => $stored_email];
1387 - if (!empty($stored_name)) {
1388 - $response_data['name'] = $stored_name;
482 + $user = get_userdata($user_id);
483 + if (!$user || !$user->roles) {
484 + //error_log(esc_html__("No user data or roles found, using logged-out limit: ", 'mxchat') . ($this->options['rate_limit_logged_out'] ?? '10'));
485 + return $this->options['rate_limit_logged_out'] ?? '10';
486 + }
487 +
488 + //error_log(esc_html__("User roles: ", 'mxchat') . print_r($user->roles, true));
489 + //error_log(esc_html__("Available role rate limits: ", 'mxchat') . print_r($this->options['role_rate_limits'] ?? [], true));
490 +
491 + $max_limit = 0;
492 + foreach ($user->roles as $role) {
493 + //error_log(esc_html__("Checking limit for role: ", 'mxchat') . $role);
494 + if (isset($this->options['role_rate_limits'][$role])) {
495 + $role_limit = $this->options['role_rate_limits'][$role];
496 + //error_log(esc_html__("Found limit for role ", 'mxchat') . $role . esc_html__(": ", 'mxchat') . $role_limit);
497 +
498 + if ($role_limit === 'unlimited') {
499 + //error_log(esc_html__("Returning unlimited for role: ", 'mxchat') . $role);
500 + return 'unlimited';
501 + }
502 +
503 + $max_limit = max($max_limit, (int)$role_limit);
504 + //error_log(esc_html__("Current max limit: ", 'mxchat') . $max_limit);
505 + } else {
506 + //error_log(esc_html__("No limit found for role: ", 'mxchat') . $role);
1389 507 }
1390 -
1391 - wp_send_json_success($response_data);
1392 - } else {
1393 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
1394 - wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1395 508 }
509 +
510 + $final_limit = $max_limit > 0 ? (string)$max_limit : '100';
511 + //error_log(esc_html__("Final rate limit: ", 'mxchat') . $final_limit);
512 + return $final_limit;
1396 513 }
1397 514
1398 -/**
1399 - * Send error response in appropriate format based on streaming mode
1400 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1401 - *
1402 - * @param string $error_message The error message to display
1403 - * @param string $error_code Optional error code for debugging
1404 - */
1405 -private function send_error_response($error_message, $error_code = 'api_error') {
1406 - if ($this->is_streaming) {
1407 - echo "data: " . json_encode([
1408 - 'error' => true,
1409 - 'error_message' => $error_message,
1410 - 'error_code' => $error_code,
1411 - 'text' => $error_message,
1412 - 'message' => $error_message
1413 - ]) . "\n\n";
1414 - echo "data: [DONE]\n\n";
1415 - flush();
1416 - } else {
1417 - wp_send_json_error([
1418 - 'error_message' => $error_message,
1419 - 'error_code' => $error_code
515 +
516 +// Add this to your plugin's main PHP file
517 +public function mxchat_check_new_messages() {
518 + if (!isset($_POST['session_id']) || !isset($_POST['last_seen_id'])) {
519 + wp_send_json_error(['message' => 'Missing required parameters']);
520 + wp_die();
521 + }
522 +
523 + $session_id = sanitize_text_field($_POST['session_id']);
524 + $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
525 +
526 + // Get chat history
527 + $history = get_option("mxchat_history_{$session_id}", []);
528 +
529 + if (empty($history)) {
530 + wp_send_json_success([
531 + 'hasNewMessages' => false,
532 + 'new_messages' => []
1420 533 ]);
534 + wp_die();
1421 535 }
536 +
537 + // Filter new messages
538 + $new_messages = array_filter($history, function($message) use ($last_seen_id) {
539 + return isset($message['id']) && $message['id'] > $last_seen_id;
540 + });
541 +
542 + // Sort by ID to ensure proper order
543 + usort($new_messages, function($a, $b) {
544 + return $a['id'] <=> $b['id'];
545 + });
546 +
547 + wp_send_json_success([
548 + 'hasNewMessages' => !empty($new_messages),
549 + 'new_messages' => array_values($new_messages),
550 + 'latestMessageId' => end($new_messages)['id'] ?? $last_seen_id
551 + ]);
1422 552 wp_die();
1423 553 }
1424 554
1425 555 public function mxchat_handle_chat_request() {
@@ -1424,30 +554,10 @@
1424 554
1425 555 public function mxchat_handle_chat_request() {
1426 556 global $wpdb;
1427 557
1428 - // Debug: Log incoming bot_id
1429 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1430 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1431 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1432 -
1433 - // Get bot-specific options
1434 - $bot_options = $this->get_bot_options($bot_id);
1435 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1436 558
1437 - // Check if this is a streaming request
1438 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1439 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1440 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1441 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1442 -
1443 - // ADDED: Store streaming state in class property for use in private methods
1444 - $this->is_streaming = $is_streaming;
1445 -
1446 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1447 - // This allows actions/forms to return JSON responses without header conflicts
1448 -
1449 - // Check if MX Chat Moderation is active
559 + // Check if MX Chat Moderation is active
1450 560 if (class_exists('MX_Chat_Moderation')) {
1451 561 // Get user email and IP
1452 562 $user_email = '';
1453 563 $user_ip = $_SERVER['REMOTE_ADDR'];
@@ -1481,12 +591,14 @@
1481 591 wp_die();
1482 592 }
1483 593 }
1484 594
595 +
596 + // Reset fallback response at the start of each request
1485 597 $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1486 598 $this->productCardHtml = '';
1487 599
1488 - // Get the actual WordPress user ID if logged in
600 + // Get the actual WordPress user ID if logged in
1489 601 $is_logged_in = is_user_logged_in();
1490 602 if ($is_logged_in) {
1491 603 $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1492 604 } else {
@@ -1496,336 +608,201 @@
1496 608
1497 609 // Get and sanitize the user identifier
1498 610 $user_id = sanitize_key($user_id);
1499 611
1500 - // Check rate limit using new settings structure
1501 - $rate_limit_result = $this->check_rate_limit();
612 + // Determine if user is logged in
613 + $is_logged_in = is_user_logged_in();
614 + //error_log("User logged in status: " . ($is_logged_in ? 'true' : 'false'));
1502 615
1503 - if ($rate_limit_result !== true) {
1504 - wp_send_json([
1505 - 'success' => false,
1506 - 'message' => $rate_limit_result['message'],
1507 - 'status' => 'rate_limit_exceeded'
1508 - ]);
1509 - wp_die();
616 + // Get rate limit based on user status
617 + // Get rate limit based on user status
618 + $rate_limit = $is_logged_in
619 + ? $this->get_user_role_rate_limit($user_id)
620 + : ($this->options['rate_limit_logged_out'] ?? '10');
621 +
622 + //error_log("Selected rate limit: " . $rate_limit);
623 +
624 + // Rest of your code remains the same
625 + // If rate limit is 'unlimited', skip rate limiting checks
626 + if ($rate_limit !== 'unlimited') {
627 + // Convert rate limit to integer
628 + $rate_limit = intval($rate_limit);
629 + // Setup rate limiting
630 + $rate_limit_transient_key = 'mxchat_chat_limit_' . $user_id;
631 + $chat_count = get_transient($rate_limit_transient_key);
632 + if ($chat_count === false) {
633 + // Initialize new counter if none exists
634 + $chat_count = 0;
635 + }
636 + // Check if user has exceeded their rate limit
637 + if ($chat_count >= $rate_limit) {
638 + // Get custom rate limit message or use default
639 + $rate_limit_message = isset($this->options['rate_limit_message'])
640 + ? $this->options['rate_limit_message']
641 + : esc_html__('Rate limit exceeded. Please try again later.', 'mxchat');
642 + // Replace placeholder if it exists in the message
643 + $rate_limit_message = str_replace(
644 + array('{limit}', '{count}', '{remaining}'),
645 + array($rate_limit, $chat_count, max(0, $rate_limit - $chat_count)),
646 + $rate_limit_message
647 + );
648 + wp_send_json([
649 + 'success' => false,
650 + 'message' => $rate_limit_message,
651 + 'status' => 'rate_limit_exceeded',
652 + 'limit' => $rate_limit,
653 + 'count' => $chat_count
654 + ]);
655 + wp_die();
656 + }
657 + // Increment the counter
658 + $chat_count++;
659 + // Store the updated count with 24-hour expiration
660 + set_transient($rate_limit_transient_key, $chat_count, DAY_IN_SECONDS);
1510 661 }
1511 662
1512 663 // Rest of your existing code...
1513 664 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
665 + //error_log("Session ID: $session_id");
1514 666
1515 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1516 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1517 - // the frontend FormData.append() to stringify a null session_id into the literal
1518 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1519 - // ghost sessions that group every visitor's first message under one row.
1520 - if ($session_id === 'null' || $session_id === 'undefined') {
1521 - $session_id = '';
1522 - }
1523 -
1524 667 if (empty($session_id)) {
668 + //error_log("Error: Session ID is missing.");
1525 669 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1526 670 wp_die();
1527 671 }
1528 672
1529 - // Update session owner if it changed (e.g. IP changed due to network switch)
1530 - // The session ID itself is the authentication — if the client has it, they own it
1531 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1532 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1533 -
1534 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1535 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1536 - }
1537 -
1538 673 // Validate and sanitize the incoming message
1539 674 if (empty($_POST['message'])) {
675 + //error_log("Error: No message received.");
1540 676 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1541 677 wp_die();
1542 678 }
1543 -
1544 -
1545 - // Track originating page for first message in session
1546 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1547 679
1548 - // Check if originating page columns exist
1549 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1550 680
1551 - if ($columns_exist) {
1552 - // Check if this session already has messages
1553 - $message_count = $wpdb->get_var($wpdb->prepare(
1554 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1555 - $session_id
1556 - ));
1557 -
1558 - // If this is the first message in the session
1559 - if ($message_count == 0) {
1560 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1561 - $originating_url = '';
1562 - $originating_title = '';
1563 -
1564 - // Try to get from POST data first (sent by JavaScript)
1565 - if (isset($_POST['current_page_url'])) {
1566 - $originating_url = esc_url_raw($_POST['current_page_url']);
1567 - $originating_title = isset($_POST['current_page_title'])
1568 - ? sanitize_text_field($_POST['current_page_title'])
1569 - : '';
1570 - }
1571 - // Fallback to HTTP_REFERER if not provided by JavaScript
1572 - else if (isset($_SERVER['HTTP_REFERER'])) {
1573 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1574 - }
1575 -
1576 - // Generate title if we have URL but no title
1577 - if ($originating_url && empty($originating_title)) {
1578 - $parsed_url = parse_url($originating_url);
1579 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1580 -
1581 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1582 - $originating_title = 'Homepage';
1583 - } else {
1584 - // Clean up the path to make a readable title
1585 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1586 - $originating_title = ucwords(trim($originating_title));
1587 - }
1588 - }
1589 -
1590 - // Store for later use when saving the message
1591 - $this->pending_originating_page = [
1592 - 'url' => $originating_url,
1593 - 'title' => $originating_title
1594 - ];
1595 - }
1596 - }
1597 -
1598 -
681 +// Modify the message sanitization to preserve PHP tags in code blocks
682 +$allowed_tags = [
683 + 'pre' => [],
684 + 'code' => ['class' => true],
685 + 'span' => ['class' => true],
686 + 'div' => ['class' => true],
687 +];
1599 688
1600 - // Get page context if provided
1601 - $page_context = null;
1602 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1603 - $page_context_raw = stripslashes($_POST['page_context']);
1604 - $page_context = json_decode($page_context_raw, true);
1605 -
1606 - // Validate page context structure
1607 - if (is_array($page_context) &&
1608 - isset($page_context['url']) &&
1609 - isset($page_context['title']) &&
1610 - isset($page_context['content'])) {
1611 -
1612 - // Sanitize page context
1613 - $page_context['url'] = esc_url_raw($page_context['url']);
1614 - $page_context['title'] = sanitize_text_field($page_context['title']);
1615 - $page_context['content'] = wp_kses_post($page_context['content']);
1616 - } else {
1617 - $page_context = null;
1618 - }
1619 - }
689 +// First preserve code blocks
690 +$message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
691 + return htmlspecialchars_decode($matches[0]);
692 +}, $_POST['message']);
1620 693
1621 - // Modify the message sanitization to preserve PHP tags in code blocks
1622 - $allowed_tags = [
1623 - 'pre' => [],
1624 - 'code' => ['class' => true],
1625 - 'span' => ['class' => true],
1626 - 'div' => ['class' => true],
1627 - ];
694 +// Then apply sanitization
695 +$message = wp_kses($message, $allowed_tags);
1628 696
1629 - // First preserve code blocks
1630 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1631 - return htmlspecialchars_decode($matches[0]);
1632 - }, $_POST['message']);
697 +// Decode code blocks
698 +$message = preg_replace_callback('/(&lt;pre&gt;&lt;code.*?&gt;.*?&lt;\/code&gt;&lt;\/pre&gt;)/s', function($matches) {
699 + return htmlspecialchars_decode($matches[1]);
700 +}, $message);
1633 701
1634 - // Then apply sanitization
1635 - $message = wp_kses($message, $allowed_tags);
702 +$message = trim($message);
1636 703
1637 - // Preserve code blocks from markdown conversion
1638 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1639 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
704 +// Preserve code blocks from markdown conversion
705 +$message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1640 706
1641 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1642 - // Always initialize testing data for admins (no toggle needed)
1643 - $testing_data = null;
1644 - if (current_user_can('administrator')) {
1645 - // For vision messages, use the original user message for the query display
1646 - $query_for_testing = $message;
1647 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1648 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1649 - }
1650 -
1651 - $testing_data = [
1652 - 'query' => $query_for_testing,
1653 - 'timestamp' => time(),
1654 - 'top_matches' => [],
1655 - 'action_matches' => [], // Initialize action matches array
1656 - 'page_context' => $page_context, // Include page context in testing data
1657 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1658 - 'bot_id' => $bot_id // Include bot ID in testing data
1659 - ];
1660 -
1661 - // Get similarity threshold from bot options or default options
1662 - $similarity_threshold = isset($current_options['similarity_threshold'])
1663 - ? ((int) $current_options['similarity_threshold']) / 100
1664 - : 0.35;
1665 -
1666 - $testing_data['similarity_threshold'] = $similarity_threshold;
1667 -
1668 - // Determine knowledge base type using bot-specific config
1669 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1670 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1671 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1672 - }
1673 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
707 +// Check if any add-ons want to pre-process this message (for web search etc.)
708 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1674 709
1675 - // Add debug before and after:
1676 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1677 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1678 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
710 +// If the pre-processing returned a result (not the original message), use it directly
711 +if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
712 + // Save the AI response
713 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
714 +
715 + // Save HTML content if provided
716 + if (!empty($pre_processed_result['html'])) {
717 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
718 + }
719 +
720 + // Return the response
721 + wp_send_json([
722 + 'text' => $pre_processed_result['text'],
723 + 'html' => $pre_processed_result['html'] ?? '',
724 + 'session_id' => $session_id
725 + ]);
726 + wp_die();
727 +}
1679 728
729 + // Save the user's message
730 + $this->mxchat_save_chat_message($session_id, 'user', $message);
1680 731
1681 - // If the pre-processing returned a result (not the original message), use it directly
1682 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1683 - // Save the AI response
1684 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1685 -
1686 - // Save HTML content if provided
1687 - if (!empty($pre_processed_result['html'])) {
1688 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1689 - }
1690 -
1691 - // Add testing data if admin
1692 - $response_data = [
1693 - 'text' => $pre_processed_result['text'],
1694 - 'html' => $pre_processed_result['html'] ?? '',
1695 - 'session_id' => $session_id
1696 - ];
1697 -
1698 - if ($testing_data !== null) {
1699 - $response_data['testing_data'] = $testing_data;
1700 - }
1701 -
1702 - wp_send_json($response_data);
1703 - wp_die();
1704 - }
732 + // Check if the message is an email address
733 + if (is_email($message)) {
734 + // Add the email to Loops
735 + $this->add_email_to_loops($message);
1705 736
1706 - // Save the user's message - handle vision processed messages differently
1707 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1708 - // For vision messages, save the original user message with image indicator
1709 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1710 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1711 - $image_count = intval($_POST['vision_images_count']);
1712 - $original_message .= " [{$image_count} image(s)]";
1713 - }
1714 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1715 - } else {
1716 - // Regular message - save as normal
1717 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1718 - }
737 + // Send success response
738 + $response_message = $this->options['email_capture_response'] ??
739 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
1719 740
1720 -
1721 - if (is_email($message)) {
1722 - // Add the email to Loops
1723 - $this->add_email_to_loops($message);
1724 -
1725 - // Get the user's success message instruction using current_options
1726 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1727 -
1728 - // Set instruction for AI using the user's success message
1729 - $this->current_action_instruction = $user_success_message;
1730 -
1731 - // Clear the email capture transient since we got the email
1732 - delete_transient('mxchat_email_capture_' . $user_id);
1733 - }
1734 -
1735 - // Check if we're in an email capture flow but user hasn't provided email yet
1736 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1737 - // Check if the message contains an email (not the whole message being an email)
1738 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1739 - $extracted_email = $matches[0];
1740 -
1741 - // Add the extracted email to Loops
1742 - $this->add_email_to_loops($extracted_email);
1743 -
1744 - // Get the user's success message instruction using current_options
1745 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1746 -
1747 - // Set instruction for AI using the user's success message
1748 - $this->current_action_instruction = $user_success_message;
1749 -
1750 - // Clear the email capture transient since we got the email
1751 - delete_transient('mxchat_email_capture_' . $user_id);
1752 - }
1753 - // If no email found but we're in capture mode, remind them
1754 - else {
1755 - // Get the original instruction to remind them using current_options
1756 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1757 - $this->current_action_instruction = $original_instruction;
1758 - }
1759 - }
741 + wp_send_json([
742 + 'success' => true,
743 + 'status' => 'email_captured',
744 + 'message' => $response_message
745 + ]);
746 + wp_die();
747 + }
1760 748
1761 - $intent_info = '';
749 + $intent_info = '';
1762 750
1763 - // Check chat mode
1764 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
751 + // Check chat mode
752 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
753 + //error_log("Chat Mode: $chat_mode");
1765 754
1766 - // Handle agent mode
1767 755 // Handle agent mode
1768 - if ($chat_mode === 'agent') {
1769 - // First, check for switch intent before doing anything else
1770 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
756 + if ($chat_mode === 'agent') {
757 + // First, check for switch intent before doing anything else
758 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1771 759
1772 - // Capture action analysis for testing panel after intent check
1773 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1774 - $testing_data['action_matches'] = $this->last_action_analysis;
1775 - }
1776 -
1777 - // Around line 506, in the agent mode handling section:
1778 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1779 - // Update chat mode first
1780 - update_option("mxchat_mode_{$session_id}", 'ai');
1781 -
1782 - // Clear any existing PDF context to start fresh
1783 - $this->clear_pdf_transients($session_id);
1784 -
1785 - // Prepare clean switch response with explicit chat_mode
1786 - $response_data = [
1787 - 'text' => $this->fallbackResponse['text'],
1788 - 'html' => $this->fallbackResponse['html'] ?? '',
1789 - 'session_id' => $session_id,
1790 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1791 - ];
1792 -
1793 - if ($testing_data !== null) {
1794 - $response_data['testing_data'] = $testing_data;
1795 - }
1796 -
1797 - // Save the mode switch message
1798 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1799 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1800 -
1801 - // Send response and exit
1802 - wp_send_json($response_data);
1803 - wp_die();
1804 - } elseif (!$intent_matched) {
1805 - // No intent matched, handle live agent message
1806 - try {
1807 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
760 + // If we matched an intent and it's the switch intent, handle it
761 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
762 + //error_log("Switch to chatbot intent detected");
1808 763
1809 - $agent_response = [
1810 - 'status' => 'waiting_for_agent',
1811 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1812 - ];
1813 -
1814 - if ($testing_data !== null) {
1815 - $agent_response['testing_data'] = $testing_data;
1816 - }
764 + // Update chat mode first
765 + update_option("mxchat_mode_{$session_id}", 'ai');
1817 766
1818 - wp_send_json_success($agent_response);
1819 - } catch (\Exception $e) {
1820 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1821 - }
1822 - wp_die();
767 + // Clear any existing PDF context to start fresh
768 + $this->clear_pdf_transients($session_id);
769 +
770 + // Prepare clean switch response
771 + $response_data = [
772 + 'text' => $this->fallbackResponse['text'],
773 + 'html' => '',
774 + 'session_id' => $session_id,
775 + 'chat_mode' => 'ai'
776 + ];
777 +
778 + // Save the mode switch message
779 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
780 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
781 +
782 + // Send response and exit
783 + wp_send_json($response_data);
784 + wp_die();
785 + } elseif (!$intent_matched) {
786 + // No intent matched, handle live agent message
787 + try {
788 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
789 + //error_log("Message sent to agent.");
790 +
791 + wp_send_json_success([
792 + 'status' => 'waiting_for_agent',
793 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
794 + ]);
795 + } catch (\Exception $e) {
796 + //error_log("Error sending message to agent: " . $e->getMessage());
797 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1823 798 }
799 + wp_die();
1824 800 }
801 + }
1825 802
1826 803 // Step 1: Check for new PDF URL in the message
1827 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
804 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1828 805 $new_pdf_url = $matches[0];
1829 806
1830 807 // Check if this is likely a PDF-related request
1831 808 $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
@@ -1847,15 +824,15 @@
1847 824
1848 825 // Clear previous PDF transients
1849 826 $this->clear_pdf_transients($session_id);
1850 827
1851 - // Process new PDF using current_options
1852 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
828 + // Process new PDF
829 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
1853 830 $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1854 831
1855 832 if ($embeddings === 'too_many_pages') {
1856 833 $error_text = sprintf(
1857 - $current_options['pdf_intent_error_text'] ??
834 + $this->options['pdf_intent_error_text'] ??
1858 835 esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1859 836 $max_pages
1860 837 );
1861 838 $this->fallbackResponse['text'] = $error_text;
@@ -1860,13 +837,15 @@
1860 837 );
1861 838 $this->fallbackResponse['text'] = $error_text;
1862 839 } elseif ($embeddings) {
1863 840 // Store new PDF information
841 + // Create a more meaningful filename from URL
1864 842 $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1865 843
1866 - // If the filename is generic, create a more descriptive one
844 + // If the filename is generic (like results_download.php), create a more descriptive one
1867 845 if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1868 846 strpos($pdf_filename, '.php') !== false) {
847 + // Create a timestamp-based name
1869 848 $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1870 849 }
1871 850
1872 851 set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
@@ -1873,257 +852,77 @@
1873 852 set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1874 853 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1875 854 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1876 855
1877 - $success_text = $current_options['pdf_intent_success_text'] ??
856 + $success_text = $this->options['pdf_intent_success_text'] ??
1878 857 esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1879 858
1880 - $pdf_response = [
859 + // Return success with filename for UI update
860 + wp_send_json([
1881 861 'success' => true,
1882 862 'message' => $success_text,
1883 863 'data' => [
1884 864 'filename' => $pdf_filename
1885 865 ]
1886 - ];
1887 -
1888 - if ($testing_data !== null) {
1889 - $pdf_response['testing_data'] = $testing_data;
1890 - }
1891 -
1892 - wp_send_json($pdf_response);
866 + ]);
1893 867 wp_die();
1894 868 } else {
1895 - $error_text = $current_options['pdf_intent_error_text'] ??
869 + $error_text = $this->options['pdf_intent_error_text'] ??
1896 870 esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1897 871 $this->fallbackResponse['text'] = $error_text;
1898 872 }
1899 873
1900 - $pdf_error_response = [
874 + wp_send_json([
1901 875 'success' => false,
1902 876 'message' => $this->fallbackResponse['text']
1903 - ];
1904 -
1905 - if ($testing_data !== null) {
1906 - $pdf_error_response['testing_data'] = $testing_data;
1907 - }
1908 -
1909 - wp_send_json($pdf_error_response);
877 + ]);
1910 878 wp_die();
1911 879 }
1912 880 }
1913 881 }
1914 882
883 + // Step 2: Detect intent and handle intent-based responses
884 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
885 + //error_log("Intent Matched: " . ($intent_matched ? "Yes" : "No"));
1915 886
1916 - // Step 2: Detect intent and handle intent-based responses
1917 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
887 + // Step 3: If intent is matched and handled, respond immediately
888 + if ($intent_matched && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
889 + //error_log("Intent response triggered.");
890 + $response_data = [
891 + 'text' => $this->fallbackResponse['text'],
892 + 'html' => $this->fallbackResponse['html'],
893 + 'session_id' => $session_id
894 + ];
895 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text'] . $this->fallbackResponse['html']);
896 + wp_send_json($response_data);
897 + wp_die();
898 + }
1918 899
1919 - // Capture action analysis for testing panel after intent check
1920 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1921 - $testing_data['action_matches'] = $this->last_action_analysis;
1922 - }
900 + // If no intent matched or product not found, proceed with AI response
901 + //error_log("No matching intent or fallback. Generating AI response.");
1923 902
1924 - // Step 3: Handle the intent result appropriately
1925 - if ($intent_result !== false) {
1926 - // Intent was matched - ALWAYS send as JSON response, never streaming
1927 -
1928 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1929 - // Intent returned a direct response array
1930 - $response_data = [
1931 - 'text' => $intent_result['text'] ?? '',
1932 - 'html' => $intent_result['html'] ?? '',
1933 - 'session_id' => $session_id
1934 - ];
903 + // Step 4: Generate AI response
904 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
905 + $this->mxchat_increment_chat_count();
1935 906
1936 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1937 - if (isset($intent_result['chat_mode'])) {
1938 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1939 - }
907 + // Generate embedding for the user's query
908 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
909 + if (!is_array($user_message_embedding)) {
910 + //error_log("Failed to generate message embedding for session $session_id");
911 + wp_send_json_error(esc_html__('Error processing your message.', 'mxchat'));
912 + wp_die();
913 + }
1940 914
1941 - if ($testing_data !== null) {
1942 - $response_data['testing_data'] = $testing_data;
1943 - }
915 + // Build context with both knowledge base and PDF content if available
916 + $context_content = "User asked: '{$message}'\n\n";
1944 917
1945 - wp_send_json($response_data);
1946 - wp_die();
1947 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1948 - // Intent returned true and set fallbackResponse
918 + // Get relevant content from knowledge base
919 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
920 + if (!empty($relevant_content)) {
921 + $context_content .= "Relevant content from knowledge database:\n" . $relevant_content . "\n\n";
922 + }
1949 923
1950 - // SAVE TO TRANSCRIPT
1951 - if (!empty($this->fallbackResponse['text'])) {
1952 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1953 - }
1954 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1955 - if (!empty($this->fallbackResponse['html'])) {
1956 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1957 - }
1958 924
1959 - $response_data = [
1960 - 'text' => $this->fallbackResponse['text'] ?? '',
1961 - 'html' => $this->fallbackResponse['html'] ?? '',
1962 - 'session_id' => $session_id
1963 - ];
1964 -
1965 - if (isset($this->fallbackResponse['chat_mode'])) {
1966 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1967 - }
1968 -
1969 - if ($testing_data !== null) {
1970 - $response_data['testing_data'] = $testing_data;
1971 - }
1972 -
1973 - wp_send_json($response_data);
1974 - wp_die();
1975 - }
1976 - }
1977 -
1978 - // If we get here, no intent matched OR the intent didn't provide a usable response
1979 -
1980 - // Step 4: Generate AI response
1981 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1982 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
1983 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
1984 - $this->mxchat_increment_chat_count();
1985 -
1986 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
1987 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
1988 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
1989 -
1990 - // Check if the embedding generation returned an error
1991 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
1992 - $error_message = $user_message_embedding['error'];
1993 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
1994 -
1995 - // FIXED: Send error in appropriate format based on streaming mode
1996 - if ($is_streaming) {
1997 - echo "data: " . json_encode([
1998 - 'error' => true,
1999 - 'error_message' => $error_message,
2000 - 'error_code' => $error_code,
2001 - 'text' => $error_message,
2002 - 'message' => $error_message
2003 - ]) . "\n\n";
2004 - echo "data: [DONE]\n\n";
2005 - flush();
2006 - } else {
2007 - wp_send_json_error([
2008 - 'error_message' => $error_message,
2009 - 'error_code' => $error_code
2010 - ]);
2011 - }
2012 - wp_die();
2013 - }
2014 -
2015 - // Check if the embedding is valid
2016 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2017 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2018 -
2019 - // FIXED: Send error in appropriate format based on streaming mode
2020 - if ($is_streaming) {
2021 - echo "data: " . json_encode([
2022 - 'error' => true,
2023 - 'error_message' => $error_message,
2024 - 'error_code' => 'invalid_embedding',
2025 - 'text' => $error_message,
2026 - 'message' => $error_message
2027 - ]) . "\n\n";
2028 - echo "data: [DONE]\n\n";
2029 - flush();
2030 - } else {
2031 - wp_send_json_error([
2032 - 'error_message' => $error_message,
2033 - 'error_code' => 'invalid_embedding'
2034 - ]);
2035 - }
2036 - wp_die();
2037 - }
2038 -
2039 - // Build context with both knowledge base and PDF content if available
2040 - $context_content = "User asked: '{$message}'\n\n";
2041 -
2042 - // Add action instruction if present (add this right after the above line)
2043 - if (!empty($this->current_action_instruction)) {
2044 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2045 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2046 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2047 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2048 -
2049 - // Clear the instruction after using it
2050 - $this->current_action_instruction = null;
2051 - }
2052 -
2053 -
2054 - // Add page context if available and contextual awareness is enabled using current_options
2055 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2056 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2057 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2058 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2059 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2060 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2061 - }
2062 -
2063 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2064 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2065 -
2066 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2067 - // Use fresh options to ensure we get the latest setting value
2068 - $fresh_options = get_option('mxchat_options', []);
2069 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2070 -
2071 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2072 - if ($citation_links_enabled && !empty($system_instructions)) {
2073 - preg_match_all(
2074 - '#\bhttps?://[^\s<>"\']+#i',
2075 - $system_instructions,
2076 - $system_instruction_urls
2077 - );
2078 -
2079 - if (!empty($system_instruction_urls[0])) {
2080 - // Merge with existing valid URLs
2081 - $this->current_valid_urls = array_merge(
2082 - $this->current_valid_urls,
2083 - $system_instruction_urls[0]
2084 - );
2085 - // Remove duplicates
2086 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2087 -
2088 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2089 - }
2090 - }
2091 -
2092 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2093 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2094 - // Update testing data with the REAL similarity analysis
2095 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2096 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2097 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2098 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2099 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2100 -}
2101 -// ===== END SIMILARITY DATA CAPTURE =====
2102 -
2103 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2104 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2105 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2106 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2107 -}
2108 -
2109 - if (!empty($relevant_content)) {
2110 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2111 - } else {
2112 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2113 - }
2114 -
2115 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2116 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2117 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2118 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2119 - foreach ($this->current_valid_urls as $url) {
2120 - $context_content .= "- " . $url . "\n";
2121 - }
2122 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2123 - $context_content .= "===== END APPROVED URLS =====\n\n";
2124 - }
2125 -
2126 925 // Check for and include PDF content
2127 926 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2128 927 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2129 928 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2154,408 +953,129 @@
2154 953 }
2155 954
2156 955 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2157 956
2158 - // Extract model from current options for bot-specific model support
2159 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2160 -
2161 - $response = $this->mxchat_generate_response(
2162 - $context_content,
2163 - $current_options['api_key'] ?? $this->options['api_key'],
2164 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2165 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2166 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2167 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2168 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2169 - $conversation_history,
2170 - $is_streaming,
2171 - $session_id,
2172 - $testing_data,
2173 - $selected_model
2174 - );
2175 -
2176 - // Handle streaming vs non-streaming responses
2177 - if ($is_streaming) {
2178 - // Check if streaming actually happened or if it fell back to regular response
2179 - if ($response === true) {
2180 - wp_die();
2181 - }
2182 - // If we get here, streaming fell back to regular response, continue
2183 - // But if there's an error, we need to send it as SSE format since headers are already set
2184 - if (is_array($response) && isset($response['error'])) {
2185 - $error_message = $response['error'];
2186 - $error_code = $response['error_code'] ?? 'api_error';
2187 - // Send error in SSE format that the client JS can handle
2188 - echo "data: " . json_encode([
2189 - 'error' => true,
2190 - 'error_message' => $error_message,
2191 - 'error_code' => $error_code,
2192 - 'text' => $error_message, // Also include as text for fallback handling
2193 - 'message' => $error_message
2194 - ]) . "\n\n";
2195 - echo "data: [DONE]\n\n";
2196 - flush();
2197 - wp_die();
2198 - }
2199 - }
957 + // Generate the response using the full context
958 + $response = $this->mxchat_generate_response(
959 + $context_content,
960 + $this->options['api_key'],
961 + $this->options['xai_api_key'],
962 + $this->options['claude_api_key'],
963 + $this->options['deepseek_api_key'],
964 + $conversation_history
965 + );
2200 966
2201 - // Check if the response is an error array (non-streaming mode)
2202 - if (is_array($response) && isset($response['error'])) {
2203 - wp_send_json_error([
2204 - 'error_message' => $response['error'],
2205 - 'error_code' => $response['error_code'] ?? 'api_error'
2206 - ]);
2207 - wp_die();
2208 - }
2209 -
2210 - // DEBUG: Check what we have
2211 - //error_log("=== BEFORE URL VALIDATION ===");
2212 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2213 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2214 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2215 -
2216 - // If we get here, the response is valid text - now validate URLs
2217 - if (!empty($this->current_valid_urls)) {
2218 - //error_log("CALLING validate_and_clean_urls");
2219 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2220 - } else {
2221 - //error_log("SKIPPING validation - current_valid_urls is empty");
2222 - }
2223 - // ===== END URL VALIDATION =====
967 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2224 968
2225 - // Prepare RAG context data for storage (only include documents used for context)
2226 - $rag_context_for_storage = null;
2227 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2228 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
969 + // Step 5: Save additional content if available
970 + if (!empty($this->productCardHtml)) {
971 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
972 + }
2229 973
2230 - if ($has_rag_data || $has_action_data) {
2231 - $rag_context_for_storage = [];
974 + if (!empty($this->fallbackResponse['html'])) {
975 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
976 + }
2232 977
2233 - // Add RAG/source data if available
2234 - if ($has_rag_data) {
2235 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2236 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2237 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2238 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2239 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2240 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2241 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2242 - }
978 + // Step 6: Return the response
979 + $response_data = [
980 + 'text' => $response,
981 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
982 + 'session_id' => $session_id
983 + ];
2243 984
2244 - // Add action analysis data if available
2245 - if ($has_action_data) {
2246 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2247 - }
2248 - }
2249 -
2250 - // Save the cleaned response with RAG context
2251 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2252 -
2253 - // Step 5: Save additional content if available
2254 - if (!empty($this->productCardHtml)) {
2255 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2256 - }
2257 -
2258 - if (!empty($this->fallbackResponse['html'])) {
2259 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2260 - }
2261 -
2262 - // Step 6: Return the response
2263 - // DEBUG: Check if newlines exist in the response
2264 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2265 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2266 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2267 -
2268 - $response_data = [
2269 - 'text' => $response,
2270 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2271 - 'session_id' => $session_id
2272 - ];
2273 -
2274 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2275 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2276 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2277 - }
2278 -
2279 - // Also pass it as a top-level field so JS can show a better error message to admins
2280 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2281 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2282 - }
2283 -
2284 - // Always add testing data for admins (no toggle needed)
2285 - if ($testing_data !== null) {
2286 - $response_data['testing_data'] = $testing_data;
2287 - }
2288 -
2289 - wp_send_json($response_data);
2290 - wp_die();
985 + wp_send_json($response_data);
986 + wp_die();
2291 987 }
2292 988
2293 -/**
2294 - * Get bot-specific options for multi-bot functionality
2295 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2296 - */
2297 -// Also debug the bot options retrieval
2298 -private function get_bot_options($bot_id = 'default') {
2299 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2300 -
2301 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2302 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2303 - return array();
2304 - }
2305 -
2306 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2307 -
2308 - if (!empty($bot_options)) {
2309 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2310 - if (isset($bot_options['similarity_threshold'])) {
2311 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2312 - }
2313 - }
2314 -
2315 - return is_array($bot_options) ? $bot_options : array();
2316 -}
2317 -
2318 -/**
2319 - * Get bot-specific Pinecone configuration
2320 - * Used in the knowledge retrieval functions
2321 - */
2322 -// Also add debugging to your get_bot_pinecone_config function
2323 -private function get_bot_pinecone_config($bot_id = 'default') {
2324 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2325 -
2326 - // If default bot or multi-bot add-on not active, use default Pinecone config
2327 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2328 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2329 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2330 - $config = array(
2331 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2332 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2333 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2334 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2335 - );
2336 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2337 - return $config;
2338 - }
2339 -
2340 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2341 -
2342 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2343 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2344 -
2345 - if (!empty($bot_pinecone_config)) {
2346 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2347 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2348 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2349 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2350 - } else {
2351 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2352 - }
2353 -
2354 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2355 -}
2356 -
2357 -
2358 -// Updated function to check intents and invoke the callback function
989 +// New function to check intents and invoke the callback function
2359 990 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2360 991 global $wpdb;
2361 992 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2362 993
2363 - // Get the current bot_id
2364 - $current_bot_id = $this->get_current_bot_id($session_id);
994 + //error_log('🔍 MXCHAT DEBUG: Intent Check Started ==================');
995 + //error_log("🔍 MXCHAT DEBUG: Message: '$message'");
996 + //error_log("🔍 MXCHAT DEBUG: Chat Mode: $chat_mode");
2365 997
2366 998 // Generate the user embedding
999 + //error_log('🔄 MXCHAT DEBUG: Generating user embedding');
2367 1000 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2368 -
2369 - // Check if embedding generation returned an error
2370 - if (is_array($user_embedding) && isset($user_embedding['error'])) {
2371 - $error_message = $user_embedding['error'];
2372 - $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2373 -
2374 - // FIXED: Send error in appropriate format based on streaming mode
2375 - if ($this->is_streaming) {
2376 - echo "data: " . json_encode([
2377 - 'error' => true,
2378 - 'error_message' => $error_message,
2379 - 'error_code' => $error_code,
2380 - 'text' => $error_message,
2381 - 'message' => $error_message
2382 - ]) . "\n\n";
2383 - echo "data: [DONE]\n\n";
2384 - flush();
2385 - } else {
2386 - wp_send_json_error([
2387 - 'error_message' => $error_message,
2388 - 'error_code' => $error_code
2389 - ]);
2390 - }
2391 - wp_die();
1001 + if (!is_array($user_embedding)) {
1002 + //error_log('❌ MXCHAT DEBUG: Failed to generate user embedding');
1003 + return false;
2392 1004 }
2393 -
2394 - // Check if embedding is valid
2395 - if (!is_array($user_embedding) || empty($user_embedding)) {
2396 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2397 -
2398 - // FIXED: Send error in appropriate format based on streaming mode
2399 - if ($this->is_streaming) {
2400 - echo "data: " . json_encode([
2401 - 'error' => true,
2402 - 'error_message' => $error_message,
2403 - 'error_code' => 'invalid_embedding',
2404 - 'text' => $error_message,
2405 - 'message' => $error_message
2406 - ]) . "\n\n";
2407 - echo "data: [DONE]\n\n";
2408 - flush();
2409 - } else {
2410 - wp_send_json_error([
2411 - 'error_message' => $error_message,
2412 - 'error_code' => 'invalid_embedding'
2413 - ]);
2414 - }
2415 - wp_die();
2416 - }
2417 -
1005 + //error_log('✅ MXCHAT DEBUG: User embedding generated successfully');
1006 +
2418 1007 // Fetch intents from the database
2419 1008 $table_name = $wpdb->prefix . 'mxchat_intents';
2420 1009 if ($chat_mode === 'agent') {
1010 + //error_log('🔍 MXCHAT DEBUG: Agent mode - fetching only chatbot switch intent');
2421 1011 $query = $wpdb->prepare(
2422 - "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
1012 + "SELECT * FROM $table_name WHERE callback_function = %s",
2423 1013 'mxchat_handle_switch_to_chatbot_intent'
2424 1014 );
2425 1015 $intents = $wpdb->get_results($query);
2426 1016 } else {
2427 - $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
1017 + //error_log('🔍 MXCHAT DEBUG: AI mode - fetching all intents');
1018 + $intents = $wpdb->get_results("SELECT * FROM $table_name");
2428 1019 }
2429 -
1020 +
1021 + //error_log('🔍 MXCHAT DEBUG: Found ' . count($intents) . ' intents to check');
1022 +
2430 1023 if (empty($intents)) {
1024 + //error_log('❌ MXCHAT DEBUG: No intents found in database');
2431 1025 return false;
2432 1026 }
2433 -
2434 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2435 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2436 - $phrases_by_intent = [];
2437 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2438 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2439 - foreach ($all_phrases as $p) {
2440 - $phrases_by_intent[$p->intent_id][] = $p;
2441 - }
2442 - }
2443 -
1027 +
2444 1028 $highest_similarity = -INF;
2445 1029 $matched_intent = null;
2446 -
2447 - // Array to store action analysis for testing panel
2448 - $action_analysis = [];
2449 -
1030 +
1031 + //error_log('📊 MXCHAT DEBUG: Intent Similarity Scores ==================');
2450 1032 foreach ($intents as $intent) {
2451 - // Additional check for enabled state
2452 - $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2453 - if (!$is_enabled) {
2454 - continue;
2455 - }
2456 -
2457 - // Check if this action is enabled for the current bot
2458 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2459 - continue;
2460 - }
2461 -
2462 - $best_similarity = -INF;
2463 - $matched_phrase_text = '';
2464 -
2465 - // Check legacy embedding vector (existing behavior)
1033 + //error_log("🔄 MXCHAT DEBUG: Checking intent: '{$intent->intent_label}' (callback: {$intent->callback_function})");
1034 +
2466 1035 $intent_embedding_serialized = $intent->embedding_vector;
2467 1036 $intent_embedding = $intent_embedding_serialized
2468 1037 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2469 1038 : null;
2470 -
2471 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2472 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2473 - if ($legacy_similarity > $best_similarity) {
2474 - $best_similarity = $legacy_similarity;
2475 - $matched_phrase_text = 'legacy';
2476 - }
2477 - }
2478 -
2479 - // Check individual phrase vectors
2480 - if (isset($phrases_by_intent[$intent->id])) {
2481 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2482 - $phrase_embedding = $phrase_row->embedding_vector
2483 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2484 - : null;
2485 - if (!is_array($phrase_embedding)) {
2486 - continue;
2487 - }
2488 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2489 - if ($phrase_similarity > $best_similarity) {
2490 - $best_similarity = $phrase_similarity;
2491 - $matched_phrase_text = $phrase_row->phrase;
2492 - }
2493 - }
2494 - }
2495 -
2496 - // Skip if no valid embedding was found at all
2497 - if ($best_similarity === -INF) {
1039 +
1040 + if (!is_array($intent_embedding)) {
1041 + //error_log("❌ MXCHAT DEBUG: Invalid embedding for intent: {$intent->intent_label}");
2498 1042 continue;
2499 1043 }
2500 -
2501 - $similarity = $best_similarity;
1044 +
1045 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2502 1046 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2503 -
2504 - // Store action analysis data for testing panel
2505 - $action_analysis[] = [
2506 - 'intent_label' => $intent->intent_label,
2507 - 'callback_function' => $intent->callback_function,
2508 - 'similarity' => round($similarity, 4),
2509 - 'similarity_percentage' => round($similarity * 100, 2),
2510 - 'threshold' => $intent_threshold,
2511 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2512 - 'above_threshold' => $similarity >= $intent_threshold,
2513 - 'matched_phrase' => $matched_phrase_text,
2514 - 'triggered' => false // Will be updated below if this intent is triggered
2515 - ];
2516 -
1047 +
1048 +
2517 1049 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2518 1050 $highest_similarity = $similarity;
2519 1051 $matched_intent = $intent;
1052 + //error_log("✅ MXCHAT DEBUG: New best match: '{$intent->intent_label}' with similarity {$similarity}");
2520 1053 }
2521 1054 }
1055 + //error_log('📊 MXCHAT DEBUG: End Intent Scores ==================');
2522 1056
2523 - // Mark the triggered action if any
2524 1057 if ($matched_intent) {
2525 - foreach ($action_analysis as &$action) {
2526 - if ($action['intent_label'] === $matched_intent->intent_label) {
2527 - $action['triggered'] = true;
2528 - break;
2529 - }
2530 - }
2531 - }
2532 -
2533 - // Sort actions by similarity (highest first) and store for testing panel
2534 - usort($action_analysis, function($a, $b) {
2535 - return $b['similarity'] <=> $a['similarity'];
2536 - });
2537 -
2538 - // Store action analysis for testing panel capture
2539 - $this->last_action_analysis = $action_analysis;
2540 -
2541 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2542 - if ($matched_intent) {
1058 + //error_log("🎯 MXCHAT DEBUG: Final Intent Match: '{$matched_intent->intent_label}'");
1059 + //error_log("🔄 MXCHAT DEBUG: Invoking callback: {$matched_intent->callback_function}");
1060 +
2543 1061 // If the callback is a method on this instance (core callback), call it directly
2544 1062 if (method_exists($this, $matched_intent->callback_function)) {
1063 + //error_log('🔍 MXCHAT DEBUG: Using direct method call for core callback');
2545 1064 $callback_result = call_user_func(
2546 - [$this, $matched_intent->callback_function],
2547 - $message,
2548 - $user_id,
2549 - $session_id,
2550 - $matched_intent,
2551 - $user_context ?? null
2552 - );
1065 + [$this, $matched_intent->callback_function],
1066 + $message,
1067 + $user_id,
1068 + $session_id,
1069 + $matched_intent,
1070 + $user_context // Add user context
1071 + );
2553 1072 } else {
1073 + //error_log('🔍 MXCHAT DEBUG: Using apply_filters for add-on callback');
2554 1074 // Otherwise, use apply_filters for add-on callbacks
2555 1075 $callback_result = apply_filters(
2556 1076 $matched_intent->callback_function,
2557 - false,
1077 + false, // default return value
2558 1078 $message,
2559 1079 $user_id,
2560 1080 $session_id,
2561 1081 $matched_intent
@@ -2561,50 +1081,24 @@
2561 1081 $matched_intent
2562 1082 );
2563 1083 }
2564 1084
2565 - // Handle the callback result properly
1085 + //error_log('🔍 MXCHAT DEBUG: Callback result type: ' . gettype($callback_result));
2566 1086 if ($callback_result !== false) {
2567 - // If callback returned an array with chat_mode, use it directly
2568 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2569 - $this->fallbackResponse = $callback_result;
2570 - return $callback_result; // Return the full array
2571 - } else {
2572 - $this->fallbackResponse = $callback_result;
2573 - return true;
2574 - }
1087 + //error_log('✅ MXCHAT DEBUG: Intent handled successfully');
1088 + $this->fallbackResponse = $callback_result;
1089 + return true;
2575 1090 }
1091 + //error_log('❌ MXCHAT DEBUG: Callback returned false');
1092 + } else {
1093 + //error_log('❌ MXCHAT DEBUG: No matching intent found');
2576 1094 }
2577 1095
1096 + //error_log('🔍 MXCHAT DEBUG: Intent Check Completed ==================');
2578 1097 return false;
2579 1098 }
2580 1099
2581 -/**
2582 - * Check if an action is enabled for a specific bot
2583 - */
2584 -private function is_action_enabled_for_bot($intent, $bot_id) {
2585 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2586 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2587 - return true;
2588 - }
2589 1100
2590 - $enabled_bots = json_decode($intent->enabled_bots, true);
2591 -
2592 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2593 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2594 - return true;
2595 - }
2596 -
2597 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2598 - // default-bot actions are testable from the admin panel
2599 - if ($bot_id === 'testing') {
2600 - $bot_id = 'default';
2601 - }
2602 -
2603 - // Check if the current bot is in the enabled bots list
2604 - return in_array($bot_id, $enabled_bots);
2605 -}
2606 -
2607 1101 // Helper function to clear PDF and Word document related transients
2608 1102 private function clear_pdf_transients($session_id) {
2609 1103 // PDF transients
2610 1104 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2623,177 +1117,80 @@
2623 1117
2624 1118
2625 1119 //verified good
2626 1120 public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2627 - // Get the user's original instruction/message
2628 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2629 -
2630 - // Set instruction for AI - just pass along what the user wanted to say
2631 - $this->current_action_instruction = $user_instruction;
2632 -
2633 - // Set the transient to track email capture flow
2634 - set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2635 -
2636 - // Return false to let the AI generate the response
2637 - return false;
2638 -}
1121 + // Log the message safely
1122 + //error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
2639 1123
2640 -public function mxchat_generate_image($message, $user_id, $session_id) {
2641 - //error_log("Starting image generation for message: " . $message);
1124 + // Initiate email capture flow
1125 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
2642 1126
2643 - // Prepare a prompt for OpenAI image generation
2644 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
1127 + set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
1128 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2645 1129
2646 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2647 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2648 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2649 - $image_response = $this->mxchat_generate_custom_image($prompt);
2650 - } else {
2651 - // Use the existing OpenAI API key
2652 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2653 - // Call OpenAI GPT Image to generate an image
2654 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2655 - }
2656 -
2657 - // Check if the response contains an image URL
2658 - if (isset($image_response['imageUrl'])) {
2659 - $image_url = esc_url_raw($image_response['imageUrl']);
2660 -
2661 - // Construct the HTML with a CSS class instead of inline styles
2662 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2663 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2664 -
2665 - // Save the bot message with both text and HTML
2666 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2667 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2668 -
2669 - // Set the fallback response for the chat handler
2670 - $this->fallbackResponse = [
2671 - 'text' => $response_text,
2672 - 'html' => $response_html,
2673 - 'images' => [$image_url]
2674 - ];
2675 -
2676 - // For debugging/verification - Use json_encode to verify what's being set
2677 - //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
2678 -
2679 - // Return the response directly instead of relying on the property
2680 - return $this->fallbackResponse;
2681 - } else {
2682 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2683 -
2684 - // Save the error message
2685 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2686 -
2687 - // Set the fallback response for the chat handler
2688 - $this->fallbackResponse = [
2689 - 'text' => $response_text,
2690 - 'html' => '',
2691 - 'images' => []
2692 - ];
2693 -
2694 - //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2695 - //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
2696 -
2697 - // Return the response directly instead of relying on the property
2698 - return $this->fallbackResponse;
2699 - }
1130 + // Respond to the user
1131 + wp_send_json(['message' => $response]);
1132 + wp_die();
2700 1133 }
2701 1134
2702 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
1135 +//very good
1136 +public function mxchat_generate_image($message, $user_id, $session_id) {
1137 + // Prepare a prompt for DALL-E
2703 1138 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2704 1139
2705 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2706 - if (empty($gemini_api_key)) {
2707 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2708 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2709 - return ['text' => $response_text, 'html' => '', 'images' => []];
2710 - }
1140 + // Use the existing OpenAI API key
1141 + $openai_api_key = sanitize_text_field($this->options['api_key']);
2711 1142
2712 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
1143 + // Call DALL-E to generate an image
1144 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
2713 1145
1146 + // Check if the response contains an image URL
2714 1147 if (isset($image_response['imageUrl'])) {
2715 1148 $image_url = esc_url_raw($image_response['imageUrl']);
2716 1149
1150 + // Construct the HTML with a CSS class instead of inline styles
2717 1151 $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
1152 +
2718 1153 $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2719 -
2720 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2721 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2722 -
2723 - $this->fallbackResponse = [
2724 - 'text' => $response_text,
2725 - 'html' => $response_html,
2726 - 'images' => [$image_url]
2727 - ];
2728 -
2729 - return $this->fallbackResponse;
2730 1154 } else {
2731 1155 $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2732 -
2733 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2734 -
2735 - $this->fallbackResponse = [
2736 - 'text' => $response_text,
2737 - 'html' => '',
2738 - 'images' => []
2739 - ];
2740 -
2741 - return $this->fallbackResponse;
1156 + $response_html = '';
1157 + //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2742 1158 }
2743 -}
2744 1159
2745 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2746 - $extension = ($mime_type === 'image/jpeg') ? 'jpg' : 'png';
2747 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2748 - $decoded = base64_decode($base64_data);
1160 + // Where you save the AI response
1161 + if (!empty($response)) {
1162 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1163 + }
1164 +
1165 + // Similarly, when returning the response data
1166 + $response_data = [
1167 + 'text' => empty($response) ? null : $response, // Use null instead of empty string
1168 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1169 + 'session_id' => $session_id
1170 + ];
2749 1171
2750 - if ($decoded === false) {
2751 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2752 - }
2753 -
2754 - $upload = wp_upload_bits($filename, null, $decoded);
2755 -
2756 - if (!empty($upload['error'])) {
2757 - return new \WP_Error('upload_failed', $upload['error']);
2758 - }
2759 -
2760 - $attach_id = wp_insert_attachment([
2761 - 'post_mime_type' => $mime_type,
2762 - 'post_title' => $prefix,
2763 - 'post_content' => '',
2764 - 'post_status' => 'inherit',
2765 - ], $upload['file']);
2766 -
2767 - if (is_wp_error($attach_id)) {
2768 - return $attach_id;
2769 - }
2770 -
2771 - require_once ABSPATH . 'wp-admin/includes/image.php';
2772 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2773 - wp_update_attachment_metadata($attach_id, $metadata);
2774 -
2775 - return esc_url_raw(wp_get_attachment_url($attach_id));
1172 + // Send the JSON response
1173 + header('Content-Type: application/json; charset=' . get_option('blog_charset'));
1174 + echo json_encode($response_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
1175 + wp_die();
2776 1176 }
2777 -
2778 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1177 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2779 1178 $api_url = 'https://api.openai.com/v1/images/generations';
2780 1179 $body = json_encode([
2781 - 'prompt' => sanitize_text_field($prompt),
2782 - 'n' => 1,
2783 - 'size' => '1024x1024',
2784 - 'quality' => 'medium',
2785 - 'output_format' => 'png',
2786 - 'model' => sanitize_text_field($model),
1180 + 'prompt' => sanitize_text_field($prompt),
1181 + 'n' => 1,
1182 + 'size' => '1024x1024',
1183 + 'model' => sanitize_text_field($model),
2787 1184 ]);
2788 1185
2789 1186 $args = [
2790 - 'body' => $body,
1187 + 'body' => $body,
2791 1188 'headers' => [
2792 - 'Content-Type' => 'application/json',
1189 + 'Content-Type' => 'application/json',
2793 1190 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2794 1191 ],
2795 - 'method' => 'POST',
1192 + 'method' => 'POST',
2796 1193 'timeout' => absint($timeout),
2797 1194 ];
2798 1195
2799 1196 $response = wp_remote_post($api_url, $args);
@@ -2798,151 +1195,61 @@
2798 1195
2799 1196 $response = wp_remote_post($api_url, $args);
2800 1197
2801 1198 if (is_wp_error($response)) {
1199 + //error_log("DALL-E request failed: " . $response->get_error_message());
2802 1200 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2803 1201 }
2804 1202
2805 1203 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2806 1204
2807 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2808 - if ($b64) {
2809 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2810 - if (is_wp_error($saved_url)) {
2811 - return ['error' => $saved_url->get_error_message()];
2812 - }
2813 - return ['imageUrl' => $saved_url];
1205 + if (isset($response_body['data'][0]['url'])) {
1206 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2814 1207 } else {
1208 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2815 1209 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2816 1210 }
2817 1211 }
2818 1212
2819 1213 /**
2820 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2821 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2822 - */
2823 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2824 - $cfg = $this->mxchat_resolve_custom_provider();
2825 - if (empty($cfg['base_url'])) {
2826 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2827 - }
2828 - $url = $cfg['base_url'] . '/images/generations';
2829 - if (!empty($cfg['api_version'])) {
2830 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2831 - }
2832 - $body = wp_json_encode([
2833 - 'prompt' => sanitize_text_field($prompt),
2834 - 'n' => 1,
2835 - 'size' => '1024x1024',
2836 - 'model' => $cfg['model'],
2837 - ]);
2838 - $response = wp_remote_post($url, [
2839 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2840 - 'body' => $body,
2841 - 'method' => 'POST',
2842 - 'timeout' => absint($timeout),
2843 - ]);
2844 - if (is_wp_error($response)) {
2845 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2846 - }
2847 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2848 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2849 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2850 - if ($b64) {
2851 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2852 - if (is_wp_error($saved)) {
2853 - return ['error' => $saved->get_error_message()];
2854 - }
2855 - return ['imageUrl' => $saved];
2856 - }
2857 - $remote_url = $resp['data'][0]['url'] ?? null;
2858 - if ($remote_url) {
2859 - return ['imageUrl' => esc_url_raw($remote_url)];
2860 - }
2861 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2862 - return ['error' => esc_html($err_msg)];
2863 -}
2864 -
2865 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2866 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2867 -
2868 - $body = json_encode([
2869 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2870 - 'parameters' => [
2871 - 'sampleCount' => 1,
2872 - 'aspectRatio' => '1:1',
2873 - ],
2874 - ]);
2875 -
2876 - $args = [
2877 - 'body' => $body,
2878 - 'headers' => [
2879 - 'Content-Type' => 'application/json',
2880 - 'x-goog-api-key' => sanitize_text_field($api_key),
2881 - ],
2882 - 'method' => 'POST',
2883 - 'timeout' => absint($timeout),
2884 - ];
2885 -
2886 - $response = wp_remote_post($api_url, $args);
2887 -
2888 - if (is_wp_error($response)) {
2889 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2890 - }
2891 -
2892 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2893 -
2894 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2895 - if ($b64) {
2896 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2897 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2898 - if (is_wp_error($saved_url)) {
2899 - return ['error' => $saved_url->get_error_message()];
2900 - }
2901 - return ['imageUrl' => $saved_url];
2902 - } else {
2903 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2904 - }
2905 -}
2906 -
2907 -/**
2908 1214 * Handle web search requests.
2909 1215 *
2910 - * Sends the refined search query to the Brave Search API and uses the
2911 - * results to generate a conversational response with the AI model.
1216 + * Sends the refined search query to the Brave Search API and displays neatly formatted,
1217 + * styled search results. Results are cached for performance.
2912 1218 *
2913 1219 * @since 1.0.0
2914 1220 * @param string $message The user's search query.
2915 1221 * @param string $user_id The user identifier.
2916 1222 * @param string $session_id The current session ID.
2917 - * @return array Response array containing text with embedded HTML links
1223 + * @return void
2918 1224 */
2919 -public function mxchat_handle_search_request($message, $user_id, $session_id) {
1225 +public function mxchat_handle_search_request( $message, $user_id, $session_id ) {
2920 1226 // Step 1: Interpret and refine the search query
2921 - $refined_search_query = $this->mxchat_interpret_search_query($message);
2922 - if (empty($refined_search_query)) {
2923 - return array(
2924 - 'text' => esc_html__('I apologize, but could you please rephrase your search request?', 'mxchat'),
2925 - 'html' => ''
1227 + $refined_search_query = $this->mxchat_interpret_search_query( $message );
1228 +
1229 + if ( empty( $refined_search_query ) ) {
1230 + $this->fallbackResponse = array(
1231 + 'text' => esc_html__( 'I apologize, but could you please rephrase your search request?', 'mxchat' ),
2926 1232 );
1233 + return;
2927 1234 }
2928 -
1235 +
2929 1236 // Retrieve and validate API settings
2930 - $options = get_option('mxchat_options');
2931 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
2932 - $results_count = isset($options['brave_results_count']) ? absint($options['brave_results_count']) : 5;
2933 -
2934 - if (empty($api_key)) {
2935 - return array(
2936 - 'text' => esc_html__('Search functionality is temporarily unavailable. Please try again later.', 'mxchat'),
2937 - 'html' => ''
1237 + $options = get_option( 'mxchat_options' );
1238 + $api_key = isset( $options['brave_api_key'] ) ? sanitize_text_field( $options['brave_api_key'] ) : '';
1239 + $results_count = isset( $options['brave_results_count'] ) ? absint( $options['brave_results_count'] ) : 5;
1240 +
1241 + if ( empty( $api_key ) ) {
1242 + $this->fallbackResponse = array(
1243 + 'text' => esc_html__( 'Search functionality is temporarily unavailable. Please try again later.', 'mxchat' ),
2938 1244 );
1245 + return;
2939 1246 }
2940 -
1247 +
2941 1248 // Build the API request URL
2942 1249 $api_url = add_query_arg(
2943 1250 array(
2944 - 'q' => rawurlencode($refined_search_query),
1251 + 'q' => rawurlencode( $refined_search_query ),
2945 1252 'count' => $results_count,
2946 1253 'text_decorations' => 'true',
2947 1254 'rich_data' => 'true',
2948 1255 ),
@@ -2947,16 +1254,16 @@
2947 1254 'rich_data' => 'true',
2948 1255 ),
2949 1256 'https://api.search.brave.com/res/v1/web/search'
2950 1257 );
2951 -
1258 +
2952 1259 // Attempt to retrieve cached results first
2953 - $transient_key = 'mxchat_search_' . md5($refined_search_query);
2954 - $results = get_transient($transient_key);
2955 -
2956 - if (false === $results) {
2957 - // SECURITY FIX: Changed to wp_safe_remote_get
2958 - $response = wp_safe_remote_get(
1260 + $transient_key = 'mxchat_search_' . md5( $refined_search_query );
1261 + $results = get_transient( $transient_key );
1262 +
1263 + if ( false === $results ) {
1264 + // Fetch new results from the Brave Search API
1265 + $response = wp_remote_get(
2959 1266 $api_url,
2960 1267 array(
2961 1268 'headers' => array(
2962 1269 'Accept' => 'application/json',
@@ -2965,98 +1272,162 @@
2965 1272 ),
2966 1273 'timeout' => 10,
2967 1274 )
2968 1275 );
2969 -
2970 - if (is_wp_error($response)) {
2971 - return array(
2972 - 'text' => esc_html__('I encountered an error while searching. Please try again.', 'mxchat'),
2973 - 'html' => ''
1276 +
1277 + if ( is_wp_error( $response ) ) {
1278 + $this->fallbackResponse = array(
1279 + 'text' => esc_html__( 'I encountered an error while searching. Please try again.', 'mxchat' ),
2974 1280 );
1281 + return;
2975 1282 }
2976 -
2977 - $results = json_decode(wp_remote_retrieve_body($response), true);
2978 -
2979 - if (json_last_error() !== JSON_ERROR_NONE) {
2980 - return array(
2981 - 'text' => esc_html__('I received an invalid response from the search service.', 'mxchat'),
2982 - 'html' => ''
1283 +
1284 + $results = json_decode( wp_remote_retrieve_body( $response ), true );
1285 +
1286 + if ( json_last_error() !== JSON_ERROR_NONE ) {
1287 + $this->fallbackResponse = array(
1288 + 'text' => esc_html__( 'I received an invalid response from the search service.', 'mxchat' ),
2983 1289 );
1290 + return;
2984 1291 }
2985 -
1292 +
2986 1293 // Cache results for one hour
2987 - set_transient($transient_key, $results, HOUR_IN_SECONDS);
1294 + set_transient( $transient_key, $results, HOUR_IN_SECONDS );
2988 1295 }
2989 -
2990 - // Process results
2991 - if (!empty($results['web']['results']) && is_array($results['web']['results'])) {
2992 - // Create a more straightforward summary with HTML links
2993 - $search_results_text = '';
2994 -
2995 - // Add a simple intro
2996 - $search_results_text .= sprintf(
2997 - esc_html__("Here's what I found about '%s':", 'mxchat'),
2998 - esc_html($refined_search_query)
1296 +
1297 + // Process and display results
1298 + if ( ! empty( $results['web']['results'] ) && is_array( $results['web']['results'] ) ) {
1299 + $html = $this->generate_search_results_html( $results['web']['results'], $refined_search_query );
1300 +
1301 + // Only return HTML (no large text summary)
1302 + $this->fallbackResponse = array(
1303 + 'html' => $html,
2999 1304 );
3000 -
3001 - // Add the top results with HTML links
3002 - foreach (array_slice($results['web']['results'], 0, 5) as $result) {
3003 - $title = isset($result['title']) ? wp_strip_all_tags($result['title']) : '';
3004 - $url = isset($result['url']) ? esc_url($result['url']) : '';
3005 - $description = isset($result['description']) ? wp_strip_all_tags($result['description']) : '';
3006 -
3007 - // Add a line break after the intro
3008 - $search_results_text .= '<br><br>';
3009 -
3010 - // Add title as a link
3011 - $search_results_text .= sprintf(
3012 - '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a><br>',
3013 - $url,
3014 - $title
3015 - );
3016 -
3017 - // Add a condensed description
3018 - $search_results_text .= sprintf("%s", $description);
3019 - }
3020 -
1305 +
3021 1306 // Save to chat history
3022 - $this->mxchat_save_chat_message($session_id, 'bot', $search_results_text);
3023 -
3024 - // Return the formatted text with embedded HTML links
3025 - return array(
3026 - 'text' => $search_results_text,
3027 - 'html' => ''
3028 - );
1307 + $this->mxchat_save_chat_message( $session_id, 'bot', $html );
3029 1308 } else {
3030 - return array(
1309 + $this->fallbackResponse = array(
3031 1310 'text' => sprintf(
3032 - esc_html__('I searched for "%s" but couldn\'t find any relevant results. Would you like to try different search terms?', 'mxchat'),
3033 - esc_html($refined_search_query)
1311 + esc_html__( 'I couldn\'t find any relevant results for "%s". Would you like to try different search terms?', 'mxchat' ),
1312 + esc_html( $refined_search_query )
3034 1313 ),
3035 - 'html' => ''
3036 1314 );
3037 1315 }
3038 1316 }
3039 1317
3040 -//very good
1318 +
3041 1319 /**
3042 - * Handle image search requests from the chatbot
1320 + * Format search results into a natural text summary.
3043 1321 *
3044 - * @param string $message The user's search query
3045 - * @param int $user_id The user's ID
3046 - * @param string $session_id The chat session ID
3047 - * @return array Response array with text and HTML content
1322 + * @since 1.0.0
1323 + * @param array $results The search results from the API.
1324 + * @param string $query The original search query.
1325 + * @return string The text summary of the top results.
3048 1326 */
1327 +private function format_search_results( $results, $query ) {
1328 + $summary = sprintf(
1329 + esc_html__( 'Here are the most relevant results for "%s":', 'mxchat' ),
1330 + esc_html( $query )
1331 + ) . "\n\n";
1332 +
1333 + $max_results = min( count( $results ), 3 );
1334 + for ( $i = 0; $i < $max_results; $i++ ) {
1335 + $result = $results[ $i ];
1336 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1337 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1338 +
1339 + // Append title and description to the summary
1340 + $summary .= sprintf(
1341 + "%s\n%s\n\n",
1342 + esc_html( $title ),
1343 + esc_html( $description )
1344 + );
1345 + }
1346 +
1347 + return $summary;
1348 +}
1349 +
1350 +/**
1351 + * Generate HTML markup for search results.
1352 + *
1353 + * @since 1.0.0
1354 + * @param array $results The search results from the API.
1355 + * @param string $query The user-refined query.
1356 + * @return string The HTML markup for displaying the results.
1357 + */
1358 +private function generate_search_results_html( $results, $query ) {
1359 + ob_start();
1360 + ?>
1361 + <div class="mxchat-search-results">
1362 + <?php foreach ( $results as $result ) :
1363 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1364 + $url = isset( $result['url'] ) ? esc_url( $result['url'] ) : '#';
1365 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1366 + $favicon = isset( $result['meta_url']['favicon'] ) ? esc_url( $result['meta_url']['favicon'] ) : '';
1367 + $thumbnail = isset( $result['thumbnail']['src'] ) ? esc_url( $result['thumbnail']['src'] ) : '';
1368 + $domain = parse_url( $url, PHP_URL_HOST );
1369 + ?>
1370 + <div class="mxchat-search-item">
1371 + <div class="mxchat-search-header">
1372 + <?php if ( $favicon ) : ?>
1373 + <img
1374 + src="<?php echo esc_url( $favicon ); ?>"
1375 + class="mxchat-site-icon"
1376 + alt="<?php echo esc_attr__( 'Site icon', 'mxchat' ); ?>"
1377 + width="16"
1378 + height="16"
1379 + />
1380 + <?php endif; ?>
1381 + <div class="mxchat-site-url"><?php echo esc_html( $domain ); ?></div>
1382 + </div>
1383 +
1384 + <div class="mxchat-search-content">
1385 + <h3 class="mxchat-search-title">
1386 + <a href="<?php echo esc_url( $url ); ?>"
1387 + target="_blank"
1388 + rel="noopener noreferrer"
1389 + >
1390 + <?php echo esc_html( $title ); ?>
1391 + </a>
1392 + </h3>
1393 +
1394 + <?php if ( $thumbnail ) : ?>
1395 + <div class="mxchat-search-thumbnail">
1396 + <img
1397 + src="<?php echo esc_url( $thumbnail ); ?>"
1398 + alt="<?php echo esc_attr__( 'Thumbnail image', 'mxchat' ); ?>"
1399 + loading="lazy"
1400 + />
1401 + </div>
1402 + <?php endif; ?>
1403 +
1404 + <div class="mxchat-search-description">
1405 + <?php echo esc_html( $description ); ?>
1406 + </div>
1407 + </div>
1408 + </div>
1409 + <?php endforeach; ?>
1410 + </div>
1411 + <?php
1412 + return ob_get_clean();
1413 +}
1414 +
1415 +
1416 +//very good
3049 1417 public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3050 - // Step 1: Interpret the search query using the user's selected AI model
1418 +
1419 + // Step 1: Interpret the search query for better results
3051 1420 $refined_search_query = $this->mxchat_interpret_search_query($message);
3052 1421
1422 +
3053 1423 // If no query was interpreted, return a fallback message
3054 1424 if (empty($refined_search_query)) {
3055 - return array(
1425 + $this->fallbackResponse = [
3056 1426 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3057 1427 'html' => "",
3058 - );
1428 + ];
1429 + return;
3059 1430 }
3060 1431
3061 1432 // Brave API URL
3062 1433 $api_url = 'https://api.search.brave.com/res/v1/images/search';
@@ -3065,12 +1436,19 @@
3065 1436 $options = get_option('mxchat_options');
3066 1437 $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3067 1438
3068 1439 if (empty($api_key)) {
3069 - return array(
1440 +/*
1441 + if (defined('WP_DEBUG') && WP_DEBUG) {
1442 + error_log("Brave API key is missing.");
1443 + }
1444 +*/
1445 +
1446 + $this->fallbackResponse = [
3070 1447 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3071 1448 'html' => "",
3072 - );
1449 + ];
1450 + return;
3073 1451 }
3074 1452
3075 1453 $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3076 1454 $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
@@ -3081,8 +1459,16 @@
3081 1459 'count' => $image_count,
3082 1460 'safesearch' => $safe_search,
3083 1461 ], $api_url);
3084 1462
1463 +/*
1464 + // Log the final API URL for the search
1465 + if (defined('WP_DEBUG') && WP_DEBUG) {
1466 + error_log("Final API URL for Brave Image Search: " . esc_url_raw($api_url));
1467 + }
1468 +*/
1469 +
1470 +
3085 1471 // Implement caching
3086 1472 $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3087 1473 $body = get_transient($transient_key);
3088 1474
@@ -3095,16 +1481,22 @@
3095 1481 ],
3096 1482 'timeout' => 10,
3097 1483 ];
3098 1484
3099 - // SECURITY FIX: Changed to wp_safe_remote_get
3100 - $response = wp_safe_remote_get($api_url, $args);
1485 + $response = wp_remote_get($api_url, $args);
3101 1486
3102 1487 if (is_wp_error($response)) {
3103 - return array(
1488 +/*
1489 + if (defined('WP_DEBUG') && WP_DEBUG) {
1490 + error_log("Brave Image API request failed: " . $response->get_error_message());
1491 + }
1492 +*/
1493 +
1494 + $this->fallbackResponse = [
3104 1495 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3105 1496 'html' => "",
3106 - );
1497 + ];
1498 + return;
3107 1499 }
3108 1500
3109 1501 $body = json_decode(wp_remote_retrieve_body($response), true);
3110 1502 set_transient($transient_key, $body, HOUR_IN_SECONDS);
@@ -3112,16 +1504,10 @@
3112 1504
3113 1505 // Process the API response
3114 1506 if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3115 1507 $html_output = '<div class="mxchat-image-gallery">';
3116 -
3117 - // Get the configured image count (1-6)
3118 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3119 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3120 -
3121 - // Use only the requested number of images
3122 - for ($i = 0; $i < $display_count; $i++) {
3123 - $image = $body['results'][$i];
1508 +
1509 + foreach ($body['results'] as $image) {
3124 1510 $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3125 1511 $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3126 1512 $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3127 1513
@@ -3135,149 +1521,47 @@
3135 1521 }
3136 1522
3137 1523 $html_output .= '</div>';
3138 1524
3139 - // Create response text
3140 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3141 -
3142 - // Save both response text and HTML to chat history
3143 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1525 + $this->fallbackResponse = [
1526 + 'text' => "",
1527 + 'html' => $html_output,
1528 + ];
1529 +
1530 + // Save response in chat history
3144 1531 $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3145 1532
3146 - // Return the combined response
3147 - return array(
3148 - 'text' => $response_text,
3149 - 'html' => $html_output,
3150 - );
3151 1533 } else {
3152 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3153 -
3154 - // Save the error message to chat history
3155 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3156 -
3157 - return array(
3158 - 'text' => $response_text,
1534 +/*
1535 + if (defined('WP_DEBUG') && WP_DEBUG) {
1536 + error_log("Brave Image API response did not contain expected data structure or was empty: " . print_r($body, true));
1537 + }
1538 +*/
1539 +
1540 + $this->fallbackResponse = [
1541 + 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3159 1542 'html' => "",
3160 - );
1543 + ];
3161 1544 }
3162 1545 }
3163 -
3164 -/**
3165 - * Interpret the search query using the user's selected AI model
3166 - *
3167 - * @param string $user_query The original query from the user
3168 - * @return string The refined search query
3169 - */
3170 1546 public function mxchat_interpret_search_query($user_query) {
3171 1547 $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3172 1548
3173 - // Get options and determine the selected model
3174 - $options = $this->options ?? get_option('mxchat_options');
3175 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
1549 + // Retrieve OpenAI API key using 'api_key' as the option key
1550 + $api_key = isset($this->options['api_key']) ? sanitize_text_field($this->options['api_key']) : sanitize_text_field(get_option('mxchat_options')['api_key']);
3176 1551
3177 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3178 - if ($selected_model === 'custom-provider') {
3179 - return $this->interpret_query_with_custom($user_query, $system_prompt);
1552 + /*
1553 + // Log the API key check, without exposing the key
1554 + if (defined('WP_DEBUG') && WP_DEBUG) {
1555 + error_log("Retrieved OpenAI API Key: " . ($api_key ? "Present" : "Missing"));
3180 1556 }
1557 + */
3181 1558
3182 - // Extract model prefix to determine the provider
3183 - $model_parts = explode('-', $selected_model);
3184 - $provider = strtolower($model_parts[0]);
3185 -
3186 - // Determine which API key to use based on the provider
3187 - switch ($provider) {
3188 - case 'gemini':
3189 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3190 - if (empty($api_key)) {
3191 - return sanitize_text_field($user_query); // Default to original query if API key missing
3192 - }
3193 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3194 -
3195 - case 'claude':
3196 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3197 - if (empty($api_key)) {
3198 - return sanitize_text_field($user_query);
3199 - }
3200 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3201 -
3202 - case 'grok':
3203 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3204 - if (empty($api_key)) {
3205 - return sanitize_text_field($user_query);
3206 - }
3207 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3208 -
3209 - case 'deepseek':
3210 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3211 - if (empty($api_key)) {
3212 - return sanitize_text_field($user_query);
3213 - }
3214 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3215 -
3216 - case 'gpt':
3217 - default:
3218 - // Default to OpenAI for custom models or unrecognized prefixes
3219 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3220 - if (empty($api_key)) {
3221 - return sanitize_text_field($user_query);
3222 - }
3223 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
1559 + if (empty($api_key)) {
1560 + //error_log("OpenAI API key is missing.");
1561 + return sanitize_text_field($user_query); // Default to the original query if API key is missing
3224 1562 }
3225 -}
3226 1563
3227 -/**
3228 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3229 - * Uses the same base URL + auth scheme as the chat dispatcher.
3230 - */
3231 -private function interpret_query_with_custom($user_query, $system_prompt) {
3232 - $cfg = $this->mxchat_resolve_custom_provider();
3233 - if (empty($cfg['base_url'])) {
3234 - return sanitize_text_field($user_query);
3235 - }
3236 - $args = [
3237 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3238 - 'body' => wp_json_encode([
3239 - 'model' => $cfg['model'],
3240 - 'messages' => [
3241 - ['role' => 'system', 'content' => $system_prompt],
3242 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3243 - ],
3244 - 'temperature' => 0.2,
3245 - 'max_tokens' => 20,
3246 - ]),
3247 - 'method' => 'POST',
3248 - 'timeout' => 15,
3249 - ];
3250 - $response = wp_remote_post($cfg['chat_url'], $args);
3251 - if (is_wp_error($response)) {
3252 - return sanitize_text_field($user_query);
3253 - }
3254 - $body = json_decode(wp_remote_retrieve_body($response), true);
3255 - return isset($body['choices'][0]['message']['content'])
3256 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3257 - : sanitize_text_field($user_query);
3258 -}
3259 -
3260 -/**
3261 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3262 - * into the assoc-array form wp_remote_post expects.
3263 - */
3264 -private function mxchat_custom_provider_assoc_headers($cfg) {
3265 - $headers = ['Content-Type' => 'application/json'];
3266 - if (!empty($cfg['api_key'])) {
3267 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3268 - $headers['api-key'] = $cfg['api_key'];
3269 - } else {
3270 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3271 - }
3272 - }
3273 - return $headers;
3274 -}
3275 -
3276 -/**
3277 - * Interpret query using OpenAI models
3278 - */
3279 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3280 1564 $url = 'https://api.openai.com/v1/chat/completions';
3281 1565 $args = [
3282 1566 'headers' => [
3283 1567 'Authorization' => 'Bearer ' . $api_key,
@@ -3283,9 +1567,9 @@
3283 1567 'Authorization' => 'Bearer ' . $api_key,
3284 1568 'Content-Type' => 'application/json',
3285 1569 ],
3286 1570 'body' => wp_json_encode([
3287 - 'model' => $model,
1571 + 'model' => 'gpt-3.5-turbo',
3288 1572 'messages' => [
3289 1573 ['role' => 'system', 'content' => $system_prompt],
3290 1574 ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3291 1575 ],
@@ -3292,200 +1576,166 @@
3292 1576 'temperature' => 0.2,
3293 1577 'max_tokens' => 20,
3294 1578 ]),
3295 1579 'method' => 'POST',
3296 - 'timeout' => 15,
3297 1580 ];
3298 1581
3299 1582 $response = wp_remote_post($url, $args);
1583 +
3300 1584 if (is_wp_error($response)) {
3301 - return sanitize_text_field($user_query);
1585 + //error_log("OpenAI request failed: " . $response->get_error_message());
1586 + return sanitize_text_field($user_query); // Fallback to the original query if there's an error
3302 1587 }
3303 1588
3304 1589 $body = json_decode(wp_remote_retrieve_body($response), true);
3305 - return isset($body['choices'][0]['message']['content'])
3306 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3307 - : sanitize_text_field($user_query);
1590 +
1591 + // Check for a valid response and sanitize output
1592 + if (isset($body['choices'][0]['message']['content'])) {
1593 + $interpreted_query = sanitize_text_field(trim($body['choices'][0]['message']['content']));
1594 +
1595 + /*
1596 + // Log the interpreted query for debugging
1597 + if (defined('WP_DEBUG') && WP_DEBUG) {
1598 + error_log("Interpreted search query: " . $interpreted_query);
1599 + }
1600 + */
1601 +
1602 + return $interpreted_query;
1603 + } else {
1604 + //error_log("Unexpected API response format: " . print_r($body, true));
1605 + return sanitize_text_field($user_query);
1606 + }
3308 1607 }
3309 1608
3310 -/**
3311 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3312 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3313 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3314 - * stripping. We never send a `thinking` param either, which is required for
3315 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3316 - */
3317 -private function mxchat_claude_omits_temperature($model) {
3318 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3319 - return in_array($model, $no_temp, true);
3320 -}
3321 1609
3322 -/**
3323 - * Interpret query using Claude models
3324 - */
3325 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3326 - $url = 'https://api.anthropic.com/v1/messages';
3327 1610
3328 - $payload = [
3329 - 'model' => $model,
3330 - 'system' => $system_prompt,
3331 - 'messages' => [
3332 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3333 - ],
3334 - 'max_tokens' => 20,
3335 - 'temperature' => 0.2,
3336 - ];
3337 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
1611 +private function find_product_in_message($message) {
1612 + global $wpdb;
3338 1613
3339 - $args = [
3340 - 'headers' => [
3341 - 'Content-Type' => 'application/json',
3342 - 'x-api-key' => $api_key,
3343 - 'anthropic-version' => '2023-06-01',
3344 - ],
3345 - 'body' => wp_json_encode($payload),
3346 - 'method' => 'POST',
3347 - 'timeout' => 15,
3348 - ];
1614 + // Get embedding for the search query
1615 + $query_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1616 + if (!is_array($query_embedding)) {
1617 + return null;
1618 + }
3349 1619
3350 - $response = wp_remote_post($url, $args);
3351 - if (is_wp_error($response)) {
3352 - return sanitize_text_field($user_query);
1620 + // Get relevant content as string
1621 + $relevant_content = $this->mxchat_find_relevant_products($query_embedding);
1622 + if (empty($relevant_content)) {
1623 + // Return null to indicate no results and set fallback response
1624 + $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');
1625 + return null;
3353 1626 }
3354 1627
3355 - $body = json_decode(wp_remote_retrieve_body($response), true);
3356 - // claude-fable-5 prepends a thinking block to content — take the first
3357 - // TEXT block, not content[0].
3358 - foreach ((array) ($body['content'] ?? array()) as $block) {
3359 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3360 - return sanitize_text_field(trim($block['text']));
1628 + // Extract product URLs from the content
1629 + preg_match_all('/https?:\/\/[^\s<>"\']+?\/product\/[^\s<>"\']+/', $relevant_content, $matches);
1630 +
1631 + if (!empty($matches[0])) {
1632 + // Try each URL found
1633 + foreach ($matches[0] as $url) {
1634 + // Clean the URL
1635 + $url = rtrim($url, '/."\']');
1636 +
1637 + // Get the product slug
1638 + $path = parse_url($url, PHP_URL_PATH);
1639 + $slug = basename(rtrim($path, '/'));
1640 +
1641 + // Find product by slug
1642 + $args = array(
1643 + 'post_type' => 'product',
1644 + 'post_status' => 'publish',
1645 + 'name' => $slug,
1646 + 'posts_per_page' => 1
1647 + );
1648 +
1649 + $products = get_posts($args);
1650 +
1651 + if (!empty($products)) {
1652 + $product_id = $products[0]->ID;
1653 + $product = wc_get_product($product_id);
1654 +
1655 + if ($product && $product->is_purchasable()) {
1656 + return $product_id;
1657 + }
1658 + }
3361 1659 }
3362 1660 }
3363 1661
3364 - return sanitize_text_field($user_query);
3365 -}
1662 + // Fallback: Look for product names in the content
1663 + $products = wc_get_products([
1664 + 'status' => 'publish',
1665 + 'limit' => -1,
1666 + 'return' => 'all'
1667 + ]);
3366 1668
3367 -/**
3368 - * Interpret query using Gemini models
3369 - */
3370 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3371 - if ($model === 'gemini-3-pro-preview') {
3372 - $model = 'gemini-3.1-pro-preview';
1669 + foreach ($products as $product) {
1670 + $name = $product->get_name();
1671 + if (stripos($relevant_content, $name) !== false) {
1672 + if ($product->is_purchasable()) {
1673 + return $product->get_id();
1674 + }
1675 + }
3373 1676 }
3374 - // Use v1beta for preview models, v1 for stable models
3375 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3376 1677
3377 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
3378 -
3379 - $args = [
3380 - 'headers' => [
3381 - 'Content-Type' => 'application/json',
3382 - ],
3383 - 'body' => wp_json_encode([
3384 - 'contents' => [
3385 - [
3386 - 'role' => 'user',
3387 - 'parts' => [
3388 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3389 - ]
3390 - ]
3391 - ],
3392 - 'generationConfig' => [
3393 - 'temperature' => 0.2,
3394 - 'maxOutputTokens' => 20,
3395 - ],
3396 - ]),
3397 - 'method' => 'POST',
3398 - 'timeout' => 15,
3399 - ];
3400 -
3401 - $response = wp_remote_post($url, $args);
3402 - if (is_wp_error($response)) {
3403 - return sanitize_text_field($user_query);
3404 - }
3405 -
3406 - $body = json_decode(wp_remote_retrieve_body($response), true);
3407 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3408 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3409 - }
3410 -
3411 - return sanitize_text_field($user_query);
1678 + // If no product is found after all checks, set the fallback response
1679 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Try to be more specific", 'mxchat');
1680 + return null;
3412 1681 }
3413 1682
3414 -/**
3415 - * Interpret query using X.AI (Grok) models
3416 - */
3417 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3418 - $url = 'https://api.xai.com/v1/chat/completions';
3419 -
3420 - $args = [
3421 - 'headers' => [
3422 - 'Content-Type' => 'application/json',
3423 - 'Authorization' => 'Bearer ' . $api_key,
3424 - ],
3425 - 'body' => wp_json_encode([
3426 - 'model' => $model,
3427 - 'messages' => [
3428 - ['role' => 'system', 'content' => $system_prompt],
3429 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3430 - ],
3431 - 'temperature' => 0.2,
3432 - 'max_tokens' => 20,
3433 - ]),
3434 - 'method' => 'POST',
3435 - 'timeout' => 15,
3436 - ];
3437 -
3438 - $response = wp_remote_post($url, $args);
3439 - if (is_wp_error($response)) {
3440 - return sanitize_text_field($user_query);
3441 - }
3442 -
3443 - $body = json_decode(wp_remote_retrieve_body($response), true);
3444 - if (isset($body['choices'][0]['message']['content'])) {
3445 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3446 - }
3447 -
3448 - return sanitize_text_field($user_query);
1683 +// New method to handle intent responses
1684 +private function generate_intent_response($context_content, $session_id) {
1685 + // Convert the context array to a structured string for the AI
1686 + $context_string = $this->format_intent_context($context_content);
1687 +
1688 + // Generate AI response using the context
1689 + $response = $this->mxchat_generate_response(
1690 + $context_string,
1691 + $this->options['api_key'],
1692 + $this->options['xai_api_key'],
1693 + $this->options['claude_api_key'],
1694 + $this->options['deepseek_api_key'],
1695 + $this->mxchat_fetch_conversation_history_for_ai($session_id)
1696 + );
1697 +
1698 + $this->fallbackResponse['text'] = $response;
1699 + return true;
3449 1700 }
1701 +// Helper method to format intent context
1702 +private function format_intent_context($context) {
1703 + $context_string = esc_html__("INTENT CONTEXT:\n", 'mxchat');
3450 1704
3451 -/**
3452 - * Interpret query using DeepSeek models
3453 - */
3454 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3455 - $url = 'https://api.deepseek.com/v1/chat/completions';
3456 -
3457 - $args = [
3458 - 'headers' => [
3459 - 'Content-Type' => 'application/json',
3460 - 'Authorization' => 'Bearer ' . $api_key,
3461 - ],
3462 - 'body' => wp_json_encode([
3463 - 'model' => $model,
3464 - 'messages' => [
3465 - ['role' => 'system', 'content' => $system_prompt],
3466 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3467 - ],
3468 - 'temperature' => 0.2,
3469 - 'max_tokens' => 20,
3470 - ]),
3471 - 'method' => 'POST',
3472 - 'timeout' => 15,
3473 - ];
3474 -
3475 - $response = wp_remote_post($url, $args);
3476 - if (is_wp_error($response)) {
3477 - return sanitize_text_field($user_query);
1705 + switch ($context['intent']) {
1706 + case 'add_to_cart':
1707 + if ($context['status'] === 'success') {
1708 + $context_string .= esc_html__("Action: Successfully added product to cart\n", 'mxchat');
1709 + $context_string .= sprintf(esc_html__("Product: %s\n", 'mxchat'), $context['product']['name']);
1710 + $context_string .= esc_html__("Available actions: ", 'mxchat') . implode(', ', $context['available_actions']) . "\n";
1711 + $context_string .= sprintf(esc_html__("Cart URL: %s\n", 'mxchat'), $context['cart_url']);
1712 + $context_string .= esc_html__("\nPlease inform the user of the successful addition and their available options.", 'mxchat');
1713 + } else {
1714 + $context_string .= esc_html__("Action: Failed to add product to cart\n", 'mxchat');
1715 + $context_string .= sprintf(esc_html__("Reason: %s\n", 'mxchat'), $context['reason']);
1716 + switch ($context['reason']) {
1717 + case 'woocommerce_not_available':
1718 + $context_string .= esc_html__("\nPlease inform the user that shopping features are not available.", 'mxchat');
1719 + break;
1720 + case 'no_product_context':
1721 + $context_string .= esc_html__("\nPlease ask the user to specify which product they want to add.", 'mxchat');
1722 + break;
1723 + case 'product_not_found':
1724 + $context_string .= esc_html__("\nPlease inform the user that the product couldn't be found and ask them to try again.", 'mxchat');
1725 + break;
1726 + case 'add_to_cart_failed':
1727 + $context_string .= esc_html__("\nPlease apologize to the user and suggest they try again or ask for assistance.", 'mxchat');
1728 + break;
1729 + }
1730 + }
1731 + break;
3478 1732 }
3479 -
3480 - $body = json_decode(wp_remote_retrieve_body($response), true);
3481 - if (isset($body['choices'][0]['message']['content'])) {
3482 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3483 - }
3484 -
3485 - return sanitize_text_field($user_query);
1733 +
1734 + return $context_string;
3486 1735 }
3487 1736
1737 +
3488 1738 //very good
3489 1739 private function add_email_to_loops($email) {
3490 1740 // Sanitize the email
3491 1741 $email = sanitize_email($email);
@@ -3569,209 +1819,95 @@
3569 1819
3570 1820 // Default to proceeding with conversation if no specific PDF action is needed
3571 1821 $this->fallbackResponse['text'] = '';
3572 1822 }
3573 -
3574 -
3575 -/**
3576 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3577 - */
3578 1823 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3579 - // CLEAR DEBUG LOGGING
3580 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3581 - //error_log("PDF Source: " . $pdf_source);
3582 - //error_log("Max Pages: " . $max_pages);
3583 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3584 -
3585 - // Check if Advanced Claude Toolbar is available and enabled
3586 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3587 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3588 -
3589 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3590 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3591 -
3592 - if ($claude_available && $claude_enabled) {
3593 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3594 -
3595 - // Attempt Claude processing first
3596 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3597 -
3598 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3599 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3600 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3601 -
3602 - // Log first page details for verification
3603 - if (isset($claude_result[0])) {
3604 - $first_page = $claude_result[0];
3605 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3606 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3607 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3608 - }
3609 -
3610 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3611 - return $claude_result;
3612 - } else {
3613 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3614 - //error_log("Claude result type: " . gettype($claude_result));
3615 - if (is_array($claude_result)) {
3616 - //error_log("Claude result count: " . count($claude_result));
3617 - }
3618 - }
3619 - }
3620 -
3621 - // Fallback to basic processing
3622 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3623 -
3624 1824 $upload_dir = wp_upload_dir();
3625 1825 $temp_file = null;
3626 -
1826 +
3627 1827 try {
3628 - // Your existing basic processing code here...
3629 - // (I'll include the key parts with debug logging)
3630 -
1828 + // Handle URL vs local file
3631 1829 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3632 - //error_log("Downloading PDF from URL...");
3633 -
3634 - // SECURITY FIX: Validate URL before processing
3635 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3636 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
1830 + // Validate and download the file from URL
1831 + $temp_file = wp_tempnam($pdf_source); // Safe temporary file name
1832 + $response = wp_remote_get($pdf_source, ['timeout' => 60]);
1833 +
1834 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1835 + //error_log(esc_html__("Failed to download PDF. Error: ", 'mxchat') . print_r($response, true));
3637 1836 return false;
3638 1837 }
3639 -
3640 - $temp_file = wp_tempnam($pdf_source);
3641 -
3642 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3643 - $response = wp_safe_remote_get($pdf_source, [
3644 - 'timeout' => 60,
3645 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3646 - ]);
3647 -
3648 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3649 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3650 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
1838 +
1839 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
1840 +
1841 + // Validate that the downloaded file is a PDF
1842 + $mime_type = mime_content_type($temp_file);
1843 + if ($mime_type !== 'application/pdf') {
1844 + //error_log(esc_html__("Invalid MIME type detected for PDF: ", 'mxchat') . $mime_type);
1845 + unlink($temp_file);
3651 1846 return false;
3652 1847 }
3653 -
3654 - global $wp_filesystem;
3655 - if (empty($wp_filesystem)) {
3656 - require_once ABSPATH . 'wp-admin/includes/file.php';
3657 - WP_Filesystem();
3658 - }
3659 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3660 - //error_log("✅ PDF downloaded successfully");
3661 1848 } else {
1849 + // For local files, use the provided path directly
3662 1850 $temp_file = $pdf_source;
3663 - //error_log("Using local PDF file: " . $temp_file);
3664 1851 }
3665 -
3666 - // Parse PDF
3667 - //error_log("Parsing PDF with basic parser...");
3668 - mxchat_load_pdf_parser();
1852 +
1853 + // Parse and process the PDF
3669 1854 $parser = new \Smalot\PdfParser\Parser();
3670 1855 $pdf = $parser->parseFile($temp_file);
3671 1856 $pages = $pdf->getPages();
3672 -
3673 - //error_log("PDF contains " . count($pages) . " pages");
3674 -
1857 +
3675 1858 if (count($pages) > $max_pages) {
3676 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3677 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
1859 + //error_log(esc_html__("PDF exceeds the maximum allowed pages: ", 'mxchat') . count($pages));
1860 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3678 1861 unlink($temp_file);
3679 1862 }
3680 - return 'too_many_pages';
1863 + return esc_html__('too_many_pages', 'mxchat');
3681 1864 }
3682 -
1865 +
3683 1866 $embeddings = [];
3684 - $processed_pages = 0;
3685 -
3686 1867 foreach ($pages as $page_number => $page) {
3687 1868 $text = $page->getText();
3688 -
1869 +
1870 + // Ensure text is non-empty before generating embeddings
3689 1871 if (empty(trim($text))) {
3690 - //error_log("Skipping empty page: " . ($page_number + 1));
1872 + //error_log(esc_html__("Skipping empty page: ", 'mxchat') . ($page_number + 1));
3691 1873 continue;
3692 1874 }
3693 -
3694 - $text = $this->mxchat_clean_text($text);
3695 -
1875 +
3696 1876 $embedding = $this->mxchat_generate_embedding(
3697 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
1877 + esc_html__("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3698 1878 $this->options['api_key']
3699 1879 );
3700 -
1880 +
3701 1881 if ($embedding) {
3702 1882 $embeddings[] = [
3703 1883 'page_number' => $page_number + 1,
3704 1884 'embedding' => $embedding,
3705 1885 'text' => $text,
3706 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3707 - 'processing_method' => 'basic_pdf_parser'
3708 1886 ];
3709 - $processed_pages++;
1887 + } else {
1888 + //error_log(esc_html__("Failed to generate embedding for page ", 'mxchat') . ($page_number + 1));
3710 1889 }
3711 1890 }
3712 -
3713 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3714 -
3715 - // Cleanup
3716 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
1891 +
1892 + // Clean up downloaded file if it was from URL
1893 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3717 1894 unlink($temp_file);
3718 1895 }
3719 -
3720 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
1896 +
3721 1897 return $embeddings;
3722 -
1898 +
3723 1899 } catch (\Exception $e) {
3724 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
1900 + // error_log(esc_html__("Error parsing or processing PDF: ", 'mxchat') . $e->getMessage());
1901 +
1902 + // Cleanup in case of exception
3725 1903 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3726 1904 unlink($temp_file);
3727 1905 }
3728 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3729 - return false;
3730 - }
3731 -}
3732 1906
3733 -
3734 -/**
3735 - * Validate PDF URL for security
3736 - * Prevents SSRF attacks by blocking dangerous URLs
3737 - */
3738 -
3739 -private function mxchat_is_safe_pdf_url($url) {
3740 - // Use WordPress core function for comprehensive validation
3741 - // This blocks localhost, private IPs, and reserved IP ranges
3742 - $validated_url = wp_http_validate_url($url);
3743 -
3744 - if ($validated_url === false) {
3745 1907 return false;
3746 1908 }
3747 -
3748 - // Additional check: only allow HTTP/HTTPS schemes
3749 - $parsed = parse_url($url);
3750 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3751 - return false;
3752 - }
3753 -
3754 - return true;
3755 1909 }
3756 -
3757 -
3758 -private function mxchat_clean_text($text) {
3759 - // Remove excessive whitespace
3760 - $text = preg_replace('/\s+/', ' ', $text);
3761 -
3762 - // Remove control characters except newlines and tabs
3763 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3764 -
3765 - // Normalize line endings
3766 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3767 -
3768 - // Trim whitespace
3769 - $text = trim($text);
3770 -
3771 - return $text;
3772 -}
3773 -
3774 1910 private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3775 1911 //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3776 1912
3777 1913 $most_relevant = null;
@@ -3794,14 +1930,11 @@
3794 1930 }
3795 1931
3796 1932 return [];
3797 1933 }
3798 -
3799 -
1934 +// Add this to your class
3800 1935 public function handle_pdf_upload() {
3801 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3802 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3803 - }
1936 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3804 1937
3805 1938 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3806 1939 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 1940 return;
@@ -3806,29 +1939,12 @@
3806 1939 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3807 1940 return;
3808 1941 }
3809 1942
3810 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3811 - $options = get_option('mxchat_options', array());
3812 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3813 -
3814 - if ($show_pdf_button !== 'on') {
3815 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3816 - return;
3817 - }
3818 -
3819 1943 $file = $_FILES['pdf_file'];
3820 1944 $session_id = sanitize_text_field($_POST['session_id']);
3821 1945 $original_filename = sanitize_text_field($file['name']);
3822 1946
3823 - // Update session owner if it changed (e.g. IP changed due to network switch)
3824 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3825 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3826 -
3827 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3828 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3829 - }
3830 -
3831 1947 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3832 1948 if ($file_type['type'] !== 'application/pdf') {
3833 1949 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3834 1950 return;
@@ -3834,12 +1950,9 @@
3834 1950 return;
3835 1951 }
3836 1952
3837 1953 $upload_dir = wp_upload_dir();
3838 -
3839 - // SECURITY FIX: Generate random filename without exposing session_id
3840 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3841 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
1954 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3842 1955 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3843 1956
3844 1957 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3845 1958 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3870,9 +1983,8 @@
3870 1983 return;
3871 1984 }
3872 1985
3873 1986 if (!empty($embeddings)) {
3874 - // Store the mapping between session and the random filename
3875 1987 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3876 1988 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3877 1989 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3878 1990 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3893,11 +2005,9 @@
3893 2005 wp_send_json_error($error_message);
3894 2006 return;
3895 2007 }
3896 2008 public function handle_pdf_remove() {
3897 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3898 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3899 - }
2009 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3900 2010
3901 2011 if (empty($_POST['session_id'])) {
3902 2012 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
3903 2013 wp_die();
@@ -3918,8 +2028,10 @@
3918 2028 wp_die();
3919 2029 }
3920 2030
3921 2031
2032 +
2033 +
3922 2034 function mxchat_fetch_new_messages() {
3923 2035 $session_id = sanitize_text_field($_POST['session_id']);
3924 2036 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
3925 2037 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -3932,31 +2044,14 @@
3932 2044 }
3933 2045
3934 2046 $history = get_option("mxchat_history_{$session_id}", []);
3935 2047
3936 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
3937 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
3938 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
3939 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
3940 -
3941 2048 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
3942 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
3943 -
3944 2049 // If persistence is enabled, show all new messages
3945 2050 if ($persistence_enabled) {
3946 - $has_id = !empty($message['id']);
3947 - $is_agent = $message['role'] === 'agent';
3948 -
3949 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
3950 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
3951 - $is_newer = true;
3952 - } else {
3953 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
3954 - }
3955 -
3956 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
3957 -
3958 - return $has_id && $is_newer && $is_agent;
2051 + return !empty($message['id']) &&
2052 + strcmp($message['id'], $last_seen_id) > 0 &&
2053 + $message['role'] === 'agent';
3959 2054 }
3960 2055
3961 2056 // If persistence is disabled, only show messages after initial timestamp
3962 2057 return !empty($message['id']) &&
@@ -3963,19 +2058,17 @@
3963 2058 $message['role'] === 'agent' &&
3964 2059 $message['timestamp'] > $initial_timestamp;
3965 2060 });
3966 2061
3967 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2062 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
3968 2063
3969 - // Include current chat mode so frontend can detect agent→AI transitions
3970 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
3971 -
3972 2064 wp_send_json_success([
3973 - 'new_messages' => array_values($new_messages),
3974 - 'chat_mode' => $chat_mode
2065 + 'new_messages' => array_values($new_messages)
3975 2066 ]);
3976 2067 wp_die();
3977 2068 }
2069 +
2070 +
3978 2071 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
3979 2072 // First check if live agents are available
3980 2073 $live_agent_available = $this->options['live_agent_status'] ?? 'off';
3981 2074 if ($live_agent_available !== 'on') {
@@ -3994,101 +2087,18 @@
3994 2087 ]);
3995 2088 wp_die();
3996 2089 }
3997 2090
3998 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
3999 -
4000 - if (empty($slack_bot_token)) {
2091 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2092 + if (empty($slack_webhook_url)) {
4001 2093 return false;
4002 2094 }
4003 2095
4004 - // Check if channel already exists for this session
4005 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4006 -
4007 - if (empty($channel_id)) {
4008 - // Create new channel with session ID as name
4009 - $channel_name = $this->generate_channel_name($session_id);
4010 -
4011 - //error_log("Attempting to create channel: $channel_name");
4012 -
4013 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4014 - 'headers' => [
4015 - 'Content-Type' => 'application/json',
4016 - 'Authorization' => 'Bearer ' . $slack_bot_token
4017 - ],
4018 - 'body' => json_encode([
4019 - 'name' => $channel_name,
4020 - 'is_private' => false // Public channel - anyone in workspace can join
4021 - ])
4022 - ]);
4023 -
4024 - if (!is_wp_error($response)) {
4025 - $response_body = wp_remote_retrieve_body($response);
4026 - $response_data = json_decode($response_body, true);
4027 -
4028 - //error_log("Channel creation response: " . $response_body);
4029 -
4030 - if (isset($response_data['ok']) && $response_data['ok']) {
4031 - $channel_id = $response_data['channel']['id'];
4032 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4033 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4034 - update_option("mxchat_channel_{$session_id}", $channel_id);
4035 -
4036 - // Auto-invite agents to the channel
4037 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4038 -
4039 - if (!empty($agent_user_ids)) {
4040 - // Parse user IDs (one per line)
4041 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4042 -
4043 - foreach ($user_ids as $user_id_to_invite) {
4044 - //error_log("Inviting user to channel: $user_id_to_invite");
4045 -
4046 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4047 - 'headers' => [
4048 - 'Content-Type' => 'application/json',
4049 - 'Authorization' => 'Bearer ' . $slack_bot_token
4050 - ],
4051 - 'body' => json_encode([
4052 - 'channel' => $channel_id,
4053 - 'users' => $user_id_to_invite
4054 - ])
4055 - ]);
4056 -
4057 - if (!is_wp_error($invite_response)) {
4058 - $invite_body = wp_remote_retrieve_body($invite_response);
4059 - $invite_data = json_decode($invite_body, true);
4060 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4061 -
4062 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4063 - //error_log("Successfully invited user $user_id_to_invite to channel");
4064 - } else {
4065 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4066 - }
4067 - } else {
4068 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4069 - }
4070 - }
4071 - } else {
4072 - //error_log("No agent user IDs configured for auto-invite");
4073 - }
4074 - } else {
4075 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4076 - }
4077 - } else {
4078 - //error_log("WP Error creating channel: " . $response->get_error_message());
4079 - }
4080 -
4081 - if (empty($channel_id)) {
4082 - return false; // Failed to create channel
4083 - }
4084 - }
4085 -
4086 - // Get recent chat history
2096 + // Get recent chat history (last 5 messages)
4087 2097 $history = get_option("mxchat_history_{$session_id}", []);
4088 - $recent_history = array_slice($history, -5);
2098 + $recent_history = array_slice($history, -5); // Get last 5 messages
4089 2099
4090 - // Format conversation context
2100 + // Format conversation history
4091 2101 $conversation_context = "";
4092 2102 if (!empty($recent_history)) {
4093 2103 $conversation_context = "*Recent Conversation:*\n";
4094 2104 foreach ($recent_history as $hist_message) {
@@ -4099,284 +2109,84 @@
4099 2109 }
4100 2110
4101 2111 update_option("mxchat_mode_{$session_id}", 'agent');
4102 2112
4103 - // Send message to channel
4104 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4105 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4106 - $channel_message .= "*User ID:* `{$user_id}`\n\n";
4107 -
2113 + $webhook_data = [
2114 + 'blocks' => [
2115 + [
2116 + 'type' => 'header',
2117 + 'text' => [
2118 + 'type' => 'plain_text',
2119 + 'text' => '🔔 New Live Agent Request',
2120 + 'emoji' => true
2121 + ]
2122 + ],
2123 + [
2124 + 'type' => 'section',
2125 + 'fields' => [
2126 + [
2127 + 'type' => 'mrkdwn',
2128 + 'text' => sprintf('*User ID:*\n`%s`', $user_id)
2129 + ],
2130 + [
2131 + 'type' => 'mrkdwn',
2132 + 'text' => sprintf('*Session ID:*\n`%s`', $session_id)
2133 + ]
2134 + ]
2135 + ]
2136 + ]
2137 + ];
2138 +
2139 + // Add conversation history if exists
4108 2140 if (!empty($conversation_context)) {
4109 - $channel_message .= $conversation_context;
2141 + $webhook_data['blocks'][] = [
2142 + 'type' => 'section',
2143 + 'text' => [
2144 + 'type' => 'mrkdwn',
2145 + 'text' => $conversation_context
2146 + ]
2147 + ];
4110 2148 }
4111 -
4112 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4113 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
4114 2149
4115 - wp_remote_post('https://slack.com/api/chat.postMessage', [
2150 + // Add the current message
2151 + $webhook_data['blocks'][] = [
2152 + 'type' => 'section',
2153 + 'text' => [
2154 + 'type' => 'mrkdwn',
2155 + 'text' => sprintf('*Current Message:*\n%s', $message)
2156 + ]
2157 + ];
2158 +
2159 + // Add the reply button
2160 + $webhook_data['blocks'][] = [
2161 + 'type' => 'actions',
2162 + 'elements' => [
2163 + [
2164 + 'type' => 'button',
2165 + 'text' => [
2166 + 'type' => 'plain_text',
2167 + 'text' => '✍️ Reply',
2168 + 'emoji' => true
2169 + ],
2170 + 'value' => $session_id,
2171 + 'action_id' => 'reply_to_user',
2172 + 'style' => 'primary'
2173 + ]
2174 + ]
2175 + ];
2176 +
2177 + $response = wp_remote_post($slack_webhook_url, [
2178 + 'body' => json_encode($webhook_data),
4116 2179 'headers' => [
4117 2180 'Content-Type' => 'application/json',
4118 - 'Authorization' => 'Bearer ' . $slack_bot_token
4119 2181 ],
4120 - 'body' => json_encode([
4121 - 'channel' => $channel_id,
4122 - 'text' => $channel_message,
4123 - 'mrkdwn' => true
4124 - ])
4125 2182 ]);
4126 2183
4127 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4128 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4129 -
4130 - $this->fallbackResponse = [
4131 - 'text' => $success_message,
4132 - 'html' => '',
4133 - 'images' => [],
4134 - 'chat_mode' => 'agent'
4135 - ];
4136 -
4137 - wp_send_json([
4138 - 'success' => true,
4139 - 'text' => $success_message,
4140 - 'html' => '',
4141 - 'chat_mode' => 'agent',
4142 - 'session_id' => $session_id,
4143 - 'fallbackResponse' => $this->fallbackResponse
4144 - ]);
4145 - wp_die();
4146 -}
4147 -
4148 -private function generate_channel_name($session_id) {
4149 - $email = null;
4150 - $name = null;
4151 -
4152 - // 1. First priority: Check if user is logged in and get their info
4153 - if (is_user_logged_in()) {
4154 - $current_user = wp_get_current_user();
4155 - if (!empty($current_user->user_email)) {
4156 - $email = $current_user->user_email;
4157 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4158 - }
4159 - if (!empty($current_user->display_name)) {
4160 - $name = $current_user->display_name;
4161 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4162 - }
4163 - }
4164 -
4165 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4166 - if (empty($email)) {
4167 - $email_option_key = "mxchat_email_{$session_id}";
4168 - $saved_email = get_option($email_option_key);
4169 - if (!empty($saved_email)) {
4170 - $email = $saved_email;
4171 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4172 - }
4173 - }
4174 -
4175 - if (empty($name)) {
4176 - $name_option_key = "mxchat_name_{$session_id}";
4177 - $saved_name = get_option($name_option_key);
4178 - if (!empty($saved_name)) {
4179 - $name = $saved_name;
4180 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4181 - }
4182 - }
4183 -
4184 - // 3. Third priority: Check existing chat transcript for email/name
4185 - if (empty($email) || empty($name)) {
4186 - global $wpdb;
4187 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4188 - $existing_data = $wpdb->get_row($wpdb->prepare(
4189 - "SELECT user_email, user_name FROM $table_name WHERE session_id = %s AND (user_email IS NOT NULL OR user_name IS NOT NULL) LIMIT 1",
4190 - $session_id
4191 - ));
4192 -
4193 - if ($existing_data) {
4194 - if (empty($email) && !empty($existing_data->user_email)) {
4195 - $email = $existing_data->user_email;
4196 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4197 - }
4198 - if (empty($name) && !empty($existing_data->user_name)) {
4199 - $name = $existing_data->user_name;
4200 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4201 - }
4202 - }
4203 - }
4204 -
4205 - // 4. Generate channel name based on priority: Name > Email > Session ID
4206 - $channel_name = '';
4207 -
4208 - if (!empty($name)) {
4209 - // Convert name to valid Slack channel name
4210 - $base_name = strtolower(trim($name));
4211 - // Replace spaces and invalid characters
4212 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4213 - $base_name = preg_replace('/\s+/', '-', $base_name);
4214 - $base_name = trim($base_name, '-');
4215 -
4216 - // Get last 4 characters of session ID for uniqueness
4217 - $session_suffix = substr($session_id, -4);
4218 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4219 -
4220 - // Slack channel names have a 21 character limit
4221 - if (strlen($channel_name) > 21) {
4222 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4223 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4224 - $truncated_name = substr($base_name, 0, $available_space);
4225 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4226 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4227 - }
4228 -
4229 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4230 -
4231 - } elseif (!empty($email)) {
4232 - // Convert email to valid Slack channel name (your existing logic)
4233 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4234 - // Remove any remaining invalid characters
4235 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4236 - // Ensure it doesn't end with a hyphen
4237 - $channel_name = rtrim($channel_name, '-');
4238 - // Slack channel names have a 21 character limit, so truncate if needed
4239 - if (strlen($channel_name) > 21) {
4240 - $channel_name = substr($channel_name, 0, 21);
4241 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4242 - }
4243 -
4244 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4245 -
4246 - } else {
4247 - // Fallback to session ID if no name or email found
4248 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4249 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4250 - }
4251 -
4252 - // Final validation - ensure channel name meets Slack requirements
4253 - if (strlen($channel_name) > 21) {
4254 - $channel_name = substr($channel_name, 0, 21);
4255 - $channel_name = rtrim($channel_name, '-');
4256 - }
4257 -
4258 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4259 - return $channel_name;
4260 -}
4261 -
4262 -/**
4263 - * Telegram Live Agent Handover
4264 - * Creates a forum topic in the Telegram group and notifies agents
4265 - */
4266 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4267 - // Check if Telegram agents are available
4268 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4269 - if ($telegram_available !== 'on') {
4270 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4271 - $this->fallbackResponse = [
4272 - 'text' => $away_message,
4273 - 'html' => '',
4274 - 'images' => [],
4275 - 'chat_mode' => 'ai'
4276 - ];
4277 - wp_send_json([
4278 - 'text' => $away_message,
4279 - 'html' => '',
4280 - 'chat_mode' => 'ai',
4281 - 'session_id' => $session_id
4282 - ]);
4283 - wp_die();
4284 - }
4285 -
4286 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4287 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4288 -
4289 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
2184 + if (is_wp_error($response)) {
4290 2185 return false;
4291 2186 }
4292 2187
4293 - // Check if topic already exists for this session
4294 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4295 -
4296 - if (empty($topic_id)) {
4297 - // Generate topic name
4298 - $topic_name = $this->generate_telegram_topic_name($session_id);
4299 -
4300 - // Random icon color (Telegram forum topic colors)
4301 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4302 - $icon_color = $icon_colors[array_rand($icon_colors)];
4303 -
4304 - // Create forum topic
4305 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4306 - 'headers' => ['Content-Type' => 'application/json'],
4307 - 'body' => json_encode([
4308 - 'chat_id' => $telegram_group_id,
4309 - 'name' => $topic_name,
4310 - 'icon_color' => $icon_color
4311 - ])
4312 - ]);
4313 -
4314 - if (!is_wp_error($response)) {
4315 - $response_body = wp_remote_retrieve_body($response);
4316 - $response_data = json_decode($response_body, true);
4317 -
4318 - if (isset($response_data['ok']) && $response_data['ok']) {
4319 - $topic_id = $response_data['result']['message_thread_id'];
4320 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4321 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4322 - }
4323 - }
4324 -
4325 - if (empty($topic_id)) {
4326 - return false; // Failed to create topic
4327 - }
4328 - }
4329 -
4330 - // Get recent chat history
4331 - $history = get_option("mxchat_history_{$session_id}", []);
4332 - $recent_history = array_slice($history, -5);
4333 -
4334 - // Format conversation context for Telegram (HTML format)
4335 - $conversation_context = "";
4336 - if (!empty($recent_history)) {
4337 - $conversation_context = "<b>Recent Conversation:</b>\n";
4338 - foreach ($recent_history as $hist_message) {
4339 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4340 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4341 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4342 - }
4343 - $conversation_context .= "\n";
4344 - }
4345 -
4346 - // Get user info
4347 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4348 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4349 -
4350 - // Update session mode
4351 - update_option("mxchat_mode_{$session_id}", 'agent');
4352 -
4353 - // Send initial message to topic
4354 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4355 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4356 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4357 - $topic_message .= "<b>User:</b> {$user_name}\n";
4358 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4359 -
4360 - if (!empty($conversation_context)) {
4361 - $topic_message .= $conversation_context;
4362 - }
4363 -
4364 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4365 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4366 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4367 -
4368 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4369 - 'headers' => ['Content-Type' => 'application/json'],
4370 - 'body' => json_encode([
4371 - 'chat_id' => $telegram_group_id,
4372 - 'message_thread_id' => $topic_id,
4373 - 'text' => $topic_message,
4374 - 'parse_mode' => 'HTML'
4375 - ])
4376 - ]);
4377 -
4378 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
2188 + $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4379 2189 $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4380 2190
4381 2191 $this->fallbackResponse = [
4382 2192 'text' => $success_message,
@@ -4394,278 +2204,79 @@
4394 2204 'fallbackResponse' => $this->fallbackResponse
4395 2205 ]);
4396 2206 wp_die();
4397 2207 }
4398 -
4399 -/**
4400 - * Generate topic name for Telegram forum
4401 - */
4402 -private function generate_telegram_topic_name($session_id) {
4403 - $name = null;
4404 - $email = null;
4405 -
4406 - // Check logged in user
4407 - if (is_user_logged_in()) {
4408 - $current_user = wp_get_current_user();
4409 - if (!empty($current_user->display_name)) {
4410 - $name = $current_user->display_name;
4411 - }
4412 - if (!empty($current_user->user_email)) {
4413 - $email = $current_user->user_email;
4414 - }
4415 - }
4416 -
4417 - // Check session data
4418 - if (empty($name)) {
4419 - $name = get_option("mxchat_name_{$session_id}");
4420 - }
4421 - if (empty($email)) {
4422 - $email = get_option("mxchat_email_{$session_id}");
4423 - }
4424 -
4425 - // Generate topic name
4426 - $session_suffix = substr($session_id, -6);
4427 -
4428 - if (!empty($name)) {
4429 - // Clean name for topic (max 128 chars in Telegram)
4430 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4431 - $clean_name = trim($clean_name);
4432 - if (strlen($clean_name) > 50) {
4433 - $clean_name = substr($clean_name, 0, 50);
4434 - }
4435 - return "Chat - {$clean_name} ({$session_suffix})";
4436 - } elseif (!empty($email)) {
4437 - // Use email prefix
4438 - $email_prefix = explode('@', $email)[0];
4439 - if (strlen($email_prefix) > 30) {
4440 - $email_prefix = substr($email_prefix, 0, 30);
4441 - }
4442 - return "Chat - {$email_prefix} ({$session_suffix})";
4443 - }
4444 -
4445 - return "Chat - {$session_suffix}";
4446 -}
4447 -
4448 -/**
4449 - * Send user message to Telegram agent
4450 - */
4451 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4452 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4453 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4454 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4455 -
4456 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4457 - return false;
4458 - }
4459 -
4460 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4461 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4462 -
4463 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4464 - 'headers' => ['Content-Type' => 'application/json'],
4465 - 'body' => json_encode([
4466 - 'chat_id' => $group_id,
4467 - 'message_thread_id' => $topic_id,
4468 - 'text' => $user_message,
4469 - 'parse_mode' => 'HTML'
4470 - ])
4471 - ]);
4472 -
4473 - return !is_wp_error($response);
4474 -}
4475 -
4476 -/**
4477 - * Handle incoming Telegram webhook
4478 - */
4479 -public function handle_telegram_webhook(WP_REST_Request $request) {
4480 - $body = $request->get_body();
4481 - $data = json_decode($body, true);
4482 -
4483 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4484 -
4485 - // Handle message events from forum topics
4486 - if (isset($data['message'])) {
4487 - $message_data = $data['message'];
4488 -
4489 - // Skip if not from a forum topic
4490 - if (!isset($message_data['message_thread_id'])) {
4491 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4492 - return new WP_REST_Response(['ok' => true]);
4493 - }
4494 -
4495 - // Skip bot messages
4496 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4497 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4498 - return new WP_REST_Response(['ok' => true]);
4499 - }
4500 -
4501 - $chat_id = $message_data['chat']['id'] ?? '';
4502 - $topic_id = $message_data['message_thread_id'];
4503 - $message_text = $message_data['text'] ?? '';
4504 - $message_id = $message_data['message_id'] ?? '';
4505 - $from = $message_data['from'] ?? [];
4506 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4507 - if (empty($agent_name)) {
4508 - $agent_name = $from['username'] ?? 'Agent';
4509 - }
4510 -
4511 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4512 -
4513 - // Skip empty messages
4514 - if (empty($message_text)) {
4515 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4516 - return new WP_REST_Response(['ok' => true]);
4517 - }
4518 -
4519 - // Find session ID by topic ID - cast to string for comparison
4520 - global $wpdb;
4521 - $topic_id_str = strval($topic_id);
4522 - $session_option = $wpdb->get_var(
4523 - $wpdb->prepare(
4524 - "SELECT option_name FROM {$wpdb->options}
4525 - WHERE option_name LIKE %s
4526 - AND option_value = %s",
4527 - 'mxchat_telegram_topic_%',
4528 - $topic_id_str
4529 - )
4530 - );
4531 -
4532 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4533 -
4534 - if ($session_option) {
4535 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4536 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4537 -
4538 - // Verify the group ID matches
4539 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4540 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4541 -
4542 - if (strval($stored_group_id) != strval($chat_id)) {
4543 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4544 - return new WP_REST_Response(['ok' => true]);
4545 - }
4546 -
4547 - // Check for closure commands
4548 - $lower_text = strtolower(trim($message_text));
4549 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4550 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4551 - // End the live agent session
4552 - update_option("mxchat_mode_{$session_id}", 'ai');
4553 -
4554 - // Save disconnect message
4555 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4556 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4557 -
4558 - // Notify in Telegram
4559 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4560 - if (!empty($telegram_bot_token)) {
4561 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4562 - 'headers' => ['Content-Type' => 'application/json'],
4563 - 'body' => json_encode([
4564 - 'chat_id' => $chat_id,
4565 - 'message_thread_id' => $topic_id,
4566 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4567 - 'parse_mode' => 'HTML'
4568 - ])
4569 - ]);
4570 -
4571 - // Optionally close the topic
4572 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4573 - 'headers' => ['Content-Type' => 'application/json'],
4574 - 'body' => json_encode([
4575 - 'chat_id' => $chat_id,
4576 - 'message_thread_id' => $topic_id
4577 - ])
4578 - ]);
4579 - }
4580 -
4581 - return new WP_REST_Response(['ok' => true]);
4582 - }
4583 -
4584 - // Deduplicate messages
4585 - $message_key = md5($session_id . $message_id . $message_text);
4586 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4587 -
4588 - if (in_array($message_key, $processed_messages)) {
4589 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4590 - return new WP_REST_Response(['ok' => true]);
4591 - }
4592 -
4593 - $processed_messages[] = $message_key;
4594 - if (count($processed_messages) > 50) {
4595 - $processed_messages = array_slice($processed_messages, -50);
4596 - }
4597 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4598 -
4599 - // Save the agent message - format with agent name prefix for proper parsing
4600 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4601 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4602 -
4603 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4604 -
4605 - // Verify the message was saved to history
4606 - $history = get_option("mxchat_history_{$session_id}", []);
4607 - $last_message = end($history);
4608 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4609 -
4610 - // Send confirmation back to Telegram
4611 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4612 - if (!empty($telegram_bot_token)) {
4613 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4614 - if (!get_transient($confirm_key)) {
4615 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4616 - 'headers' => ['Content-Type' => 'application/json'],
4617 - 'body' => json_encode([
4618 - 'chat_id' => $chat_id,
4619 - 'message_thread_id' => $topic_id,
4620 - 'text' => "✅ <i>Message sent to user</i>",
4621 - 'parse_mode' => 'HTML',
4622 - 'reply_to_message_id' => $message_id
4623 - ])
4624 - ]);
4625 - set_transient($confirm_key, true, 300);
4626 - }
4627 - }
4628 - } else {
4629 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4630 - }
4631 - } else {
4632 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4633 - }
4634 -
4635 - return new WP_REST_Response(['ok' => true]);
4636 -}
4637 -
4638 2208 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4639 - // Check if this is a Telegram agent session
4640 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4641 - if (!empty($telegram_topic_id)) {
4642 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4643 - }
2209 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
4644 2210
4645 - // Otherwise, try Slack
4646 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4647 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4648 -
4649 - if (empty($slack_bot_token) || empty($channel_id)) {
2211 + if (empty($slack_webhook_url)) {
2212 + //error_log(esc_html__('Slack Webhook URL is not configured.', 'mxchat'));
4650 2213 return false;
4651 2214 }
4652 2215
4653 - $user_message = "💬 *User:* {$message}";
2216 + $webhook_data = [
2217 + 'blocks' => [
2218 + [
2219 + 'type' => 'header',
2220 + 'text' => [
2221 + 'type' => 'plain_text',
2222 + 'text' => esc_html__('📩 New Chat Message', 'mxchat'),
2223 + 'emoji' => true
2224 + ]
2225 + ],
2226 + [
2227 + 'type' => 'section',
2228 + 'fields' => [
2229 + [
2230 + 'type' => 'mrkdwn',
2231 + 'text' => sprintf(esc_html__('*User ID:*\n`%s`', 'mxchat'), $user_id)
2232 + ],
2233 + [
2234 + 'type' => 'mrkdwn',
2235 + 'text' => sprintf(esc_html__('*Session ID:*\n`%s`', 'mxchat'), $session_id)
2236 + ]
2237 + ]
2238 + ],
2239 + [
2240 + 'type' => 'section',
2241 + 'text' => [
2242 + 'type' => 'mrkdwn',
2243 + 'text' => sprintf(esc_html__('*Message:*\n%s', 'mxchat'), $message)
2244 + ]
2245 + ],
2246 + [
2247 + 'type' => 'actions',
2248 + 'elements' => [
2249 + [
2250 + 'type' => 'button',
2251 + 'text' => [
2252 + 'type' => 'plain_text',
2253 + 'text' => esc_html__('✍️ Reply', 'mxchat'),
2254 + 'emoji' => true
2255 + ],
2256 + 'value' => $session_id,
2257 + 'action_id' => 'reply_to_user',
2258 + 'style' => 'primary'
2259 + ]
2260 + ]
2261 + ]
2262 + ]
2263 + ];
4654 2264
4655 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
2265 + $response = wp_remote_post($slack_webhook_url, [
2266 + 'body' => json_encode($webhook_data),
4656 2267 'headers' => [
4657 2268 'Content-Type' => 'application/json',
4658 - 'Authorization' => 'Bearer ' . $slack_bot_token
4659 2269 ],
4660 - 'body' => json_encode([
4661 - 'channel' => $channel_id,
4662 - 'text' => $user_message,
4663 - 'mrkdwn' => true
4664 - ])
4665 2270 ]);
4666 2271
4667 - return !is_wp_error($response);
2272 + if (is_wp_error($response)) {
2273 + //error_log(esc_html__('Error sending message to Slack: ', 'mxchat') . $response->get_error_message());
2274 + return false;
2275 + }
2276 +
2277 + //error_log(esc_html__('Message sent to Slack successfully.', 'mxchat'));
2278 + return true;
4668 2279 }
4669 2280 public function handle_slack_interaction(WP_REST_Request $request) {
4670 2281 //error_log('Received Slack interaction');
4671 2282
@@ -4753,16 +2364,17 @@
4753 2364
4754 2365 // Default acknowledgment
4755 2366 return new WP_REST_Response(['ok' => true]);
4756 2367 }
2368 +
4757 2369 public function mxchat_handle_agent_response(WP_REST_Request $request) {
4758 2370 //error_log('Received agent response request');
4759 2371 //error_log('Request data: ' . print_r($request->get_params(), true));
4760 - // //error_log('Raw body: ' . file_get_contents('php://input'));
2372 + // error_log('Raw body: ' . file_get_contents('php://input'));
4761 2373
4762 2374 // Get the data from Slack's slash command format
4763 2375 $command_text = $request->get_param('text');
4764 - // //error_log('Command text: ' . $command_text);
2376 + // error_log('Command text: ' . $command_text);
4765 2377
4766 2378 if (empty($command_text)) {
4767 2379 //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
4768 2380 return new WP_REST_Response([
@@ -4787,9 +2399,9 @@
4787 2399 // Save the message
4788 2400 $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
4789 2401
4790 2402 if (!$message_id) {
4791 - // //error_log('Failed to save agent message');
2403 + // error_log('Failed to save agent message');
4792 2404 return new WP_REST_Response([
4793 2405 'error' => esc_html__('Failed to save message', 'mxchat')
4794 2406 ], 500);
4795 2407 }
@@ -4799,173 +2411,29 @@
4799 2411 'response_type' => 'in_channel',
4800 2412 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4801 2413 ], 200);
4802 2414 }
4803 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4804 - // Update mode to AI
4805 - update_option("mxchat_mode_{$session_id}", 'ai');
4806 -
4807 - // Clear any existing PDF context to start fresh
4808 - $this->clear_pdf_transients($session_id);
4809 -
4810 - // Set the response with explicit chat_mode
4811 - $this->fallbackResponse = [
4812 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4813 - 'html' => '',
4814 - 'images' => [],
4815 - 'chat_mode' => 'ai' // Ensure this is set
4816 - ];
4817 -
4818 - // Return the complete response array instead of just true
4819 - return $this->fallbackResponse;
4820 -}
4821 2415
4822 -public function handle_slack_messages(WP_REST_Request $request) {
4823 - // Log the incoming request for debugging
4824 - //error_log('Slack events request received: ' . $request->get_body());
4825 -
4826 - $body = $request->get_body();
4827 - $data = json_decode($body, true);
4828 -
4829 - // Handle Slack URL verification
4830 - if (isset($data['type']) && $data['type'] === 'url_verification') {
4831 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
4832 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
4833 - }
4834 -
4835 - // IMPORTANT: Handle Slack's event deduplication
4836 - if (isset($data['event_id'])) {
4837 - $event_id = $data['event_id'];
4838 - $processed_events = get_transient('mxchat_slack_events') ?: [];
4839 -
4840 - // Check if we've already processed this event
4841 - if (in_array($event_id, $processed_events)) {
4842 - //error_log("Duplicate event detected: $event_id");
4843 - return new WP_REST_Response(['ok' => true]);
4844 - }
4845 -
4846 - // Add this event to processed list
4847 - $processed_events[] = $event_id;
4848 - // Keep only last 100 events to prevent memory issues
4849 - if (count($processed_events) > 100) {
4850 - $processed_events = array_slice($processed_events, -100);
4851 - }
4852 - // Store for 1 hour
4853 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
4854 - }
4855 -
4856 - // Handle message events
4857 - if (isset($data['event']) && $data['event']['type'] === 'message') {
4858 - $event = $data['event'];
4859 -
4860 - // Skip bot messages and messages with subtypes (like bot_message)
4861 - if (isset($event['bot_id']) || isset($event['subtype'])) {
4862 - return new WP_REST_Response(['ok' => true]);
4863 - }
4864 -
4865 - // Additional check: Skip if this is a threaded reply to our confirmation
4866 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
4867 - return new WP_REST_Response(['ok' => true]);
4868 - }
4869 -
4870 - $channel_id = $event['channel'];
4871 - $message_text = $event['text'] ?? '';
4872 - $message_ts = $event['ts'] ?? '';
4873 2416
4874 - // Find session ID by looking for matching channel
4875 - global $wpdb;
4876 - $session_option = $wpdb->get_var(
4877 - $wpdb->prepare(
4878 - "SELECT option_name FROM {$wpdb->options}
4879 - WHERE option_name LIKE 'mxchat_channel_%'
4880 - AND option_value = %s",
4881 - $channel_id
4882 - )
4883 - );
2417 +public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
2418 + //error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
4884 2419
4885 - if ($session_option) {
4886 - $session_id = str_replace('mxchat_channel_', '', $session_option);
2420 + // Just update mode to AI
2421 + update_option("mxchat_mode_{$session_id}", 'ai');
4887 2422
4888 - // Create a unique key for this specific message
4889 - $message_key = md5($session_id . $message_ts . $message_text);
4890 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
2423 + // Initialize states
2424 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2425 + $this->productCardHtml = '';
4891 2426
4892 - // Check if we've already processed this exact message
4893 - if (in_array($message_key, $processed_messages)) {
4894 - //error_log("Duplicate message detected for session $session_id");
4895 - return new WP_REST_Response(['ok' => true]);
4896 - }
2427 + // Set the response message
2428 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
4897 2429
4898 - // Add to processed messages
4899 - $processed_messages[] = $message_key;
4900 - // Keep only last 50 messages per session
4901 - if (count($processed_messages) > 50) {
4902 - $processed_messages = array_slice($processed_messages, -50);
4903 - }
4904 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
2430 + return true; // Intent was handled
2431 +}
4905 2432
4906 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4907 2433
4908 - // Handle agent ending the chat — transfer back to AI
4909 - // Format: "!endchat" or "!endchat <custom message to user>"
4910 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
4911 - update_option("mxchat_mode_{$session_id}", 'ai');
4912 2434
4913 - // Extract custom message after !endchat, or use empty string
4914 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
4915 2435
4916 - // Send the agent's custom farewell message if provided
4917 - if (!empty($custom_message)) {
4918 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
4919 - }
4920 -
4921 - // Confirm in Slack channel
4922 - if (!empty($slack_bot_token)) {
4923 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4924 - 'headers' => [
4925 - 'Content-Type' => 'application/json',
4926 - 'Authorization' => 'Bearer ' . $slack_bot_token
4927 - ],
4928 - 'body' => json_encode([
4929 - 'channel' => $channel_id,
4930 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
4931 - 'mrkdwn' => true
4932 - ])
4933 - ]);
4934 - }
4935 -
4936 - return new WP_REST_Response(['ok' => true]);
4937 - }
4938 -
4939 - // Save the agent message
4940 - $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
4941 -
4942 - // Send confirmation back to Slack (only once)
4943 - if (!empty($slack_bot_token)) {
4944 - // Use a transient to prevent duplicate confirmations
4945 - $confirm_key = 'mxchat_confirm_' . $message_key;
4946 - if (!get_transient($confirm_key)) {
4947 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4948 - 'headers' => [
4949 - 'Content-Type' => 'application/json',
4950 - 'Authorization' => 'Bearer ' . $slack_bot_token
4951 - ],
4952 - 'body' => json_encode([
4953 - 'channel' => $channel_id,
4954 - 'text' => "✅ _Message sent to user_",
4955 - 'thread_ts' => $event['ts'] // Reply in thread
4956 - ])
4957 - ]);
4958 - // Set transient to prevent duplicate confirmations
4959 - set_transient($confirm_key, true, 300); // 5 minutes
4960 - }
4961 - }
4962 - }
4963 - }
4964 -
4965 - return new WP_REST_Response(['ok' => true]);
4966 -}
4967 -
4968 2436 // For the word upload handler
4969 2437 public function mxchat_handle_word_upload() {
4970 2438 // Delegate to word handler
4971 2439 $this->word_handler->mxchat_handle_word_upload();
@@ -4988,860 +2456,257 @@
4988 2456 return MxChat_User::mxchat_get_user_identifier();
4989 2457 }
4990 2458
4991 2459 private function mxchat_generate_embedding($text, $api_key) {
4992 - try {
4993 - // Get options and selected model
4994 - $options = get_option('mxchat_options');
4995 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
4996 -
4997 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
4998 - // Off by default so existing sites see byte-identical behavior.
4999 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5000 - return $this->mxchat_generate_embedding_custom($text);
5001 - }
5002 -
5003 - // Determine endpoint and API key based on model
5004 - if (strpos($selected_model, 'voyage') === 0) {
5005 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
5006 - $api_key = $options['voyage_api_key'] ?? '';
5007 -
5008 - // Check if Voyage API key is missing
5009 - if (empty($api_key)) {
5010 - //error_log('Voyage API key is missing');
5011 - return [
5012 - 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5013 - 'error_code' => 'missing_voyage_api_key'
5014 - ];
5015 - }
5016 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5017 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5018 - $api_key = $options['gemini_api_key'] ?? '';
5019 -
5020 - // Check if Gemini API key is missing
5021 - if (empty($api_key)) {
5022 - //error_log('Gemini API key is missing');
5023 - return [
5024 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5025 - 'error_code' => 'missing_gemini_api_key'
5026 - ];
5027 - }
5028 - } else {
5029 - $endpoint = 'https://api.openai.com/v1/embeddings';
5030 - // Use the passed API key for OpenAI
5031 -
5032 - // Check if OpenAI API key is missing
5033 - if (empty($api_key)) {
5034 - //error_log('OpenAI API key is missing');
5035 - return [
5036 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
5037 - 'error_code' => 'missing_openai_api_key'
5038 - ];
5039 - }
5040 - }
5041 -
5042 - // Check if text is empty
5043 - if (empty($text)) {
5044 - //error_log('Empty text provided for embedding generation');
5045 - return [
5046 - 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
5047 - 'error_code' => 'empty_embedding_text'
5048 - ];
5049 - }
5050 -
5051 - // Prepare request body based on provider
5052 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5053 - // Gemini API format
5054 - $request_body = [
5055 - 'model' => 'models/' . $selected_model,
5056 - 'content' => [
5057 - 'parts' => [
5058 - ['text' => $text]
5059 - ]
5060 - ],
5061 - 'outputDimensionality' => 1536
5062 - ];
5063 -
5064 - // Prepare headers for Gemini (API key as query parameter)
5065 - $endpoint .= '?key=' . $api_key;
5066 - $headers = [
5067 - 'Content-Type' => 'application/json'
5068 - ];
5069 - } else {
5070 - // OpenAI/Voyage API format
5071 - $request_body = [
5072 - 'input' => $text,
5073 - 'model' => $selected_model
5074 - ];
5075 -
5076 - // Add output_dimension for voyage-3-large
5077 - if ($selected_model === 'voyage-3-large') {
5078 - $request_body['output_dimension'] = 2048;
5079 - }
5080 -
5081 - // Prepare headers for OpenAI/Voyage
5082 - $headers = [
5083 - 'Content-Type' => 'application/json',
5084 - 'Authorization' => 'Bearer ' . $api_key
5085 - ];
5086 - }
5087 -
5088 - // Prepare request arguments
5089 - $args = [
5090 - 'body' => wp_json_encode($request_body),
5091 - 'headers' => $headers,
5092 - 'timeout' => 60,
5093 - 'redirection' => 5,
5094 - 'blocking' => true,
5095 - 'httpversion' => '1.0',
5096 - 'sslverify' => true,
5097 - ];
5098 -
5099 - // Make the request
5100 - $response = wp_remote_post($endpoint, $args);
5101 -
5102 - // Handle WordPress errors
5103 - if (is_wp_error($response)) {
5104 - $error_message = $response->get_error_message();
5105 - //error_log('Embedding Generation Error: ' . $error_message);
5106 - return [
5107 - 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
5108 - 'error_code' => 'embedding_connection_error'
5109 - ];
5110 - }
5111 -
5112 - // Check HTTP status code
5113 - $status_code = wp_remote_retrieve_response_code($response);
5114 - if ($status_code !== 200) {
5115 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5116 -
5117 - $error_message = isset($response_body['error']['message'])
5118 - ? $response_body['error']['message']
5119 - : 'HTTP Error ' . $status_code;
5120 -
5121 - $error_type = isset($response_body['error']['type'])
5122 - ? $response_body['error']['type']
5123 - : 'unknown';
5124 -
5125 - //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
5126 -
5127 - // Handle specific error types
5128 - switch ($error_type) {
5129 - case 'invalid_request_error':
5130 - if (strpos($error_message, 'API key') !== false) {
5131 - return [
5132 - 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
5133 - 'error_code' => 'embedding_invalid_api_key'
5134 - ];
5135 - }
5136 - break;
5137 -
5138 - case 'authentication_error':
5139 - return [
5140 - 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
5141 - 'error_code' => 'embedding_auth_error'
5142 - ];
5143 -
5144 - case 'rate_limit_exceeded':
5145 - return [
5146 - 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
5147 - 'error_code' => 'embedding_rate_limit'
5148 - ];
5149 -
5150 - case 'quota_exceeded':
5151 - return [
5152 - 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
5153 - 'error_code' => 'embedding_quota_exceeded'
5154 - ];
5155 - }
5156 -
5157 - // Generic error fallback
5158 - return [
5159 - 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
5160 - 'error_code' => 'embedding_api_error',
5161 - 'status_code' => $status_code
5162 - ];
5163 - }
5164 -
5165 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5166 -
5167 - // Handle different response formats based on provider
5168 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5169 - // Gemini API response format
5170 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5171 - return $response_body['embedding']['values'];
5172 - } else {
5173 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5174 - return [
5175 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5176 - 'error_code' => 'invalid_gemini_embedding_response'
5177 - ];
5178 - }
5179 - } else {
5180 - // OpenAI/Voyage API response format
5181 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5182 - return $response_body['data'][0]['embedding'];
5183 - } else {
5184 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5185 - return [
5186 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5187 - 'error_code' => 'invalid_embedding_response'
5188 - ];
5189 - }
5190 - }
5191 - } catch (Exception $e) {
5192 - //error_log('Embedding Exception: ' . $e->getMessage());
5193 - return [
5194 - 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
5195 - 'error_code' => 'embedding_exception'
5196 - ];
2460 + // Get options and selected model
2461 + $options = get_option('mxchat_options');
2462 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2463 +
2464 + // Determine endpoint and API key based on model
2465 + if (strpos($selected_model, 'voyage') === 0) {
2466 + $endpoint = 'https://api.voyageai.com/v1/embeddings';
2467 + $api_key = $options['voyage_api_key'] ?? '';
2468 + } else {
2469 + $endpoint = 'https://api.openai.com/v1/embeddings';
2470 + // Use the passed API key for OpenAI
5197 2471 }
5198 -}
5199 -
5200 -
5201 -/**
5202 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5203 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5204 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5205 - */
5206 -private function mxchat_generate_embedding_custom($text) {
5207 - if (empty($text)) {
5208 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
2472 +
2473 + // Prepare request body with conditional output_dimension
2474 + $request_body = [
2475 + 'input' => $text,
2476 + 'model' => $selected_model
2477 + ];
2478 +
2479 + // Add output_dimension for voyage-3-large
2480 + if ($selected_model === 'voyage-3-large') {
2481 + $request_body['output_dimension'] = 2048;
5209 2482 }
5210 - $cfg = $this->mxchat_resolve_custom_provider();
5211 - if (empty($cfg['base_url'])) {
5212 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5213 - }
5214 -
5215 - $options = get_option('mxchat_options');
5216 - $embed_url = $cfg['base_url'] . '/embeddings';
5217 - if (!empty($cfg['api_version'])) {
5218 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5219 - }
5220 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5221 - ? trim((string) $options['custom_provider_embedding_model'])
5222 - : $cfg['model'];
5223 -
5224 - $response = wp_remote_post($embed_url, [
5225 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5226 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
2483 +
2484 + // Prepare request arguments
2485 + $args = [
2486 + 'body' => wp_json_encode($request_body),
2487 + 'headers' => [
2488 + 'Content-Type' => 'application/json',
2489 + 'Authorization' => 'Bearer ' . $api_key,
2490 + ],
5227 2491 'timeout' => 60,
5228 - ]);
2492 + 'redirection' => 5,
2493 + 'blocking' => true,
2494 + 'httpversion' => '1.0',
2495 + 'sslverify' => true,
2496 + ];
2497 +
2498 + // Make the request
2499 + $response = wp_remote_post($endpoint, $args);
2500 +
2501 + // Rest of your existing code...
5229 2502 if (is_wp_error($response)) {
5230 - return [
5231 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5232 - 'error_code' => 'embedding_custom_connection_error',
5233 - ];
2503 + //error_log('Embedding Generation Error: ' . $response->get_error_message());
2504 + return null;
5234 2505 }
5235 - $status = wp_remote_retrieve_response_code($response);
5236 - $body = json_decode(wp_remote_retrieve_body($response), true);
5237 - if ($status !== 200) {
5238 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5239 - return [
5240 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5241 - 'error_code' => 'embedding_custom_api_error',
5242 - 'status_code' => $status,
5243 - ];
2506 +
2507 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
2508 +
2509 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
2510 + return $response_body['data'][0]['embedding'];
2511 + } else {
2512 + //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
2513 + return null;
5244 2514 }
5245 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5246 - return $body['data'][0]['embedding'];
5247 - }
5248 - return [
5249 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5250 - 'error_code' => 'embedding_custom_invalid_response',
5251 - ];
5252 2515 }
5253 2516
5254 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5255 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5256 2517
5257 - // Check for OpenAI Vector Store first (takes priority when enabled)
5258 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
2518 +private function mxchat_find_relevant_content($user_embedding) {
2519 + //error_log('MXChat Vector Search: Starting content search...');
5259 2520
5260 - if ($bot_vectorstore_config['use_vectorstore']) {
5261 - // Get current model to verify it's an OpenAI model
5262 - $bot_options = $this->get_bot_options($bot_id);
5263 - $mxchat_options = get_option('mxchat_options', array());
5264 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5265 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
2521 + // Retrieve the add-on settings from the database.
2522 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5266 2523
5267 - if ($this->is_openai_chat_model($selected_model)) {
5268 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5269 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5270 - } else {
5271 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5272 - }
5273 - }
2524 + // Determine whether Pinecone is enabled.
2525 + // We expect the sanitized setting to be a string '1' if enabled, otherwise '0'.
2526 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5274 2527
5275 - // Get bot-specific Pinecone configuration
5276 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
2528 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
5277 2529
5278 - // Debug: Log the Pinecone configuration
5279 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5280 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5281 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5282 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5283 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5284 -
5285 - // Determine whether to use Pinecone based on bot configuration
5286 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5287 -
5288 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5289 -
5290 - if ($use_pinecone) {
5291 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
2530 + if ($use_pinecone === 1) {
2531 + //error_log('MXChat Vector Search: Using Pinecone database');
2532 + return $this->find_relevant_content_pinecone($user_embedding);
5292 2533 } else {
5293 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
2534 + //error_log('MXChat Vector Search: Using WordPress database');
2535 + return $this->find_relevant_content_wordpress($user_embedding);
5294 2536 }
5295 2537 }
5296 2538
5297 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
2539 +
2540 +private function find_relevant_content_wordpress($user_embedding) {
5298 2541 global $wpdb;
5299 2542 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5300 - // Initialize similarity analysis storage
5301 - $this->last_similarity_analysis = [
5302 - 'knowledge_base_type' => 'WordPress Database',
5303 - 'bot_id' => $bot_id,
5304 - 'top_matches' => [],
5305 - 'threshold_used' => 0,
5306 - 'total_checked' => 0
5307 - ];
2543 + $cache_key = 'mxchat_system_prompt_embeddings';
2544 + $batch_size = 500;
5308 2545
5309 - // NEW: Initialize valid URLs array
5310 - $valid_urls = [];
2546 + // Log start of matching process
2547 + error_log('[MXCHAT] Starting similarity matching process');
5311 2548
5312 - // Get bot-specific options for similarity threshold
5313 - $bot_options = $this->get_bot_options($bot_id);
5314 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
2549 + // Retrieve embeddings from cache or database
2550 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2551 + if ($embeddings === false) {
2552 + error_log('[MXCHAT] Cache miss - loading embeddings from database');
2553 + $embeddings = [];
2554 + $offset = 0;
5315 2555
5316 - // Get knowledge manager instance for role checking
5317 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2556 + // Load in batches and build cache
2557 + do {
2558 + $query = $wpdb->prepare(
2559 + "SELECT id, embedding_vector
2560 + FROM {$system_prompt_table}
2561 + LIMIT %d OFFSET %d",
2562 + $batch_size,
2563 + $offset
2564 + );
5318 2565
5319 - // Get base similarity threshold from bot options or default options
5320 - $similarity_threshold = isset($current_options['similarity_threshold'])
5321 - ? ((int) $current_options['similarity_threshold']) / 100
5322 - : 0.35;
5323 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5324 -
5325 - // Precompute bot_filter once, outside the streaming loop
5326 - $bot_filter = '';
5327 - if ($bot_id !== 'default') {
5328 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5329 - if ($column_exists) {
5330 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5331 - }
5332 - }
5333 -
5334 - // ===== STREAMING TOP-K PASS =====
5335 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5336 - // - top 10 by raw similarity (for the testing/debug display panel)
5337 - // - candidates above threshold with access (capped) for context assembly
5338 - // This bounds peak memory regardless of knowledge base size and avoids loading
5339 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5340 - $batch_size = 250;
5341 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5342 - $top_display = [];
5343 - $candidates = [];
5344 - $total_checked = 0;
5345 - $offset = 0;
5346 -
5347 - do {
5348 - $batch = $wpdb->get_results($wpdb->prepare(
5349 - "SELECT id, embedding_vector, source_url, role_restriction
5350 - FROM {$system_prompt_table}
5351 - WHERE 1=1 {$bot_filter}
5352 - LIMIT %d OFFSET %d",
5353 - $batch_size,
5354 - $offset
5355 - ));
5356 -
5357 - if (empty($batch)) {
5358 - break;
5359 - }
5360 -
5361 - foreach ($batch as $row) {
5362 - $database_embedding = $row->embedding_vector
5363 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5364 - : null;
5365 -
5366 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5367 - unset($database_embedding);
5368 - continue;
2566 + $batch = $wpdb->get_results($query);
2567 + if (empty($batch)) {
2568 + break;
5369 2569 }
5370 2570
5371 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5372 - unset($database_embedding);
2571 + $embeddings = array_merge($embeddings, $batch);
2572 + $offset += $batch_size;
5373 2573
5374 - $role_restriction = $row->role_restriction ?? 'public';
5375 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5376 - $source_url = $row->source_url ?? '';
2574 + // Free memory
2575 + unset($batch);
5377 2576
5378 - // Maintain top 10 display buffer (insert-if-beats-worst)
5379 - if (count($top_display) < 10) {
5380 - $top_display[] = [
5381 - 'id' => $row->id,
5382 - 'similarity' => $similarity,
5383 - 'source_url' => $source_url,
5384 - 'role_restriction' => $role_restriction,
5385 - 'has_access' => $has_access,
5386 - ];
5387 - usort($top_display, function ($a, $b) {
5388 - return $b['similarity'] <=> $a['similarity'];
5389 - });
5390 - } elseif ($similarity > $top_display[9]['similarity']) {
5391 - $top_display[9] = [
5392 - 'id' => $row->id,
5393 - 'similarity' => $similarity,
5394 - 'source_url' => $source_url,
5395 - 'role_restriction' => $role_restriction,
5396 - 'has_access' => $has_access,
5397 - ];
5398 - usort($top_display, function ($a, $b) {
5399 - return $b['similarity'] <=> $a['similarity'];
5400 - });
5401 - }
2577 + } while (true);
5402 2578
5403 - // Track candidates for context assembly (above threshold + has access)
5404 - if ($similarity >= $similarity_threshold && $has_access) {
5405 - $candidates[] = [
5406 - 'id' => $row->id,
5407 - 'similarity' => $similarity,
5408 - 'source_url' => $source_url,
5409 - ];
5410 - }
5411 -
5412 - $total_checked++;
2579 + if (empty($embeddings)) {
2580 + error_log('[MXCHAT] No embeddings found in database');
2581 + return ''; // Return an empty string if no embeddings found
5413 2582 }
5414 -
5415 - unset($batch);
5416 -
5417 - // Trim candidates periodically to cap memory during long scans
5418 - if (count($candidates) > $max_candidates) {
5419 - usort($candidates, function ($a, $b) {
5420 - return $b['similarity'] <=> $a['similarity'];
5421 - });
5422 - $candidates = array_slice($candidates, 0, $max_candidates);
5423 - }
5424 -
5425 - $offset += $batch_size;
5426 - } while (true);
5427 -
5428 - if ($total_checked === 0) {
5429 - $this->current_valid_urls = [];
5430 - return '';
2583 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2584 + error_log('[MXCHAT] Cached ' . count($embeddings) . ' embeddings');
2585 + } else {
2586 + error_log('[MXCHAT] Using ' . count($embeddings) . ' cached embeddings');
5431 2587 }
5432 2588
5433 - // Final candidates sort (best first)
5434 - if (count($candidates) > 1) {
5435 - usort($candidates, function ($a, $b) {
5436 - return $b['similarity'] <=> $a['similarity'];
5437 - });
5438 - }
5439 -
5440 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5441 - // Gather unique IDs we actually need (top_display + candidates) and pull
5442 - // article_content in bounded IN() batches. This avoids loading content for
5443 - // every row during the similarity scan.
5444 - $needed_ids = [];
5445 - foreach ($top_display as $item) {
5446 - $needed_ids[$item['id']] = true;
5447 - }
5448 - foreach ($candidates as $item) {
5449 - $needed_ids[$item['id']] = true;
5450 - }
5451 - $needed_ids = array_keys($needed_ids);
5452 -
5453 - $content_map = [];
5454 - if (!empty($needed_ids)) {
5455 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5456 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5457 - $rows = $wpdb->get_results($wpdb->prepare(
5458 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5459 - ...$chunk_ids
5460 - ));
5461 - foreach ($rows as $r) {
5462 - $content_map[$r->id] = $r->article_content;
2589 + // Initialize array to store relevant results with similarity scores
2590 + $relevant_results = [];
2591 +
2592 + // Retrieve the similarity threshold
2593 + $similarity_threshold = ((int) get_option('mxchat_similarity_threshold', 80)) / 100;
2594 + error_log('[MXCHAT] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
2595 +
2596 + // Iterate through embeddings to calculate similarity
2597 + foreach ($embeddings as $embedding) {
2598 + $database_embedding = $embedding->embedding_vector
2599 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2600 + : null;
2601 + if (is_array($database_embedding) && is_array($user_embedding)) {
2602 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
2603 +
2604 + // Log each similarity score over 0.5 to reduce log spam
2605 + if ($similarity > 0.1) {
2606 + error_log(sprintf('[MXCHAT] ID: %d | Similarity Score: %.4f', $embedding->id, $similarity));
5463 2607 }
5464 - unset($rows);
2608 +
2609 + $relevant_results[] = [
2610 + 'id' => $embedding->id,
2611 + 'similarity' => $similarity
2612 + ];
5465 2613 }
2614 + // Free memory
2615 + unset($database_embedding);
5466 2616 }
5467 2617
5468 - // Build the all_similarities display array from the top 10
5469 - $all_similarities = [];
5470 - foreach ($top_display as $item) {
5471 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5472 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5473 - $is_chunk = $parsed_for_display['is_chunked'];
5474 - $chunk_meta = $parsed_for_display['metadata'];
5475 -
5476 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5477 - $source_display = $item['source_url'];
5478 - } else {
5479 - $content_preview = strip_tags($article_content_for_parse);
5480 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5481 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5482 - }
5483 -
5484 - $all_similarities[] = [
5485 - 'document_id' => $item['id'],
5486 - 'similarity' => $item['similarity'],
5487 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5488 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5489 - 'source_display' => $source_display,
5490 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5491 - 'used_for_context' => false,
5492 - 'role_restriction' => $item['role_restriction'],
5493 - 'has_access' => $item['has_access'],
5494 - 'filtered_out' => !$item['has_access'],
5495 - 'is_chunk' => $is_chunk,
5496 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5497 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5498 - ];
5499 - }
5500 -
5501 - // Build url_groups from candidates for chunk reassembly
5502 - $url_groups = array();
5503 - foreach ($candidates as $cand) {
5504 - $article_content = $content_map[$cand['id']] ?? '';
5505 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5506 - $is_chunked = $parsed['is_chunked'];
5507 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5508 - $text_content = $parsed['text'];
5509 -
5510 - $source_url = $cand['source_url'];
5511 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5512 -
5513 - if (!isset($url_groups[$group_key])) {
5514 - $url_groups[$group_key] = array(
5515 - 'source_url' => $source_url,
5516 - 'best_score' => 0,
5517 - 'is_chunked' => $is_chunked,
5518 - 'chunks' => array(),
5519 - 'single_text' => '',
5520 - 'single_id' => null
5521 - );
5522 - }
5523 -
5524 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5525 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5526 - }
5527 -
5528 - if ($is_chunked) {
5529 - $url_groups[$group_key]['is_chunked'] = true;
5530 - $url_groups[$group_key]['chunks'][] = array(
5531 - 'id' => $cand['id'],
5532 - 'score' => $cand['similarity'],
5533 - 'chunk_index' => $chunk_index,
5534 - 'text' => $text_content
5535 - );
5536 - } else {
5537 - $url_groups[$group_key]['single_text'] = $text_content;
5538 - $url_groups[$group_key]['single_id'] = $cand['id'];
5539 - }
5540 - }
5541 -
5542 - // Sort ALL similarities for testing display (highest first)
5543 - usort($all_similarities, function ($a, $b) {
2618 + // Filter and sort relevant results by similarity
2619 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2620 + return $result['similarity'] >= $similarity_threshold;
2621 + });
2622 + usort($relevant_results, function ($a, $b) {
5544 2623 return $b['similarity'] <=> $a['similarity'];
5545 2624 });
5546 2625
5547 - // Sort URL groups by best score (highest first)
5548 - uasort($url_groups, function($a, $b) {
5549 - return $b['best_score'] <=> $a['best_score'];
5550 - });
2626 + // Log number of results that met threshold
2627 + error_log('[MXCHAT] ' . count($relevant_results) . ' results met the similarity threshold');
5551 2628
5552 - // Get RAG sources limit from options (default 6, min 3, max 10)
5553 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5554 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5555 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
2629 + // Limit to the top 5 results
2630 + $top_results = array_slice($relevant_results, 0, 5);
5556 2631
5557 - // Take top N unique URLs based on user setting
5558 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5559 -
5560 - // Track which document IDs are used for context
5561 - $used_document_ids = [];
5562 - foreach ($top_urls as $group) {
5563 - if ($group['is_chunked']) {
5564 - foreach ($group['chunks'] as $chunk) {
5565 - $used_document_ids[] = $chunk['id'];
5566 - }
5567 - } elseif ($group['single_id']) {
5568 - $used_document_ids[] = $group['single_id'];
5569 - }
2632 + // Log the top matches
2633 + error_log('[MXCHAT] Top matching results:');
2634 + foreach ($top_results as $index => $result) {
2635 + error_log(sprintf('[MXCHAT] %d. ID: %d | Score: %.4f',
2636 + $index + 1,
2637 + $result['id'],
2638 + $result['similarity']
2639 + ));
5570 2640 }
5571 2641
5572 - // Update the all_similarities array to mark which were actually used
5573 - foreach ($all_similarities as &$similarity_item) {
5574 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5575 - }
5576 -
5577 - // Store top 10 for testing panel
5578 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5579 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5580 -
5581 - // Initialize final content
2642 + // Initialize the final content
5582 2643 $content = '';
5583 - $matches_used = 0;
5584 - $total_chunks_used = 0;
5585 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5586 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5587 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5588 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5589 2644
5590 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5591 - // Use fresh options to ensure we get the latest setting value
5592 - $fresh_options = get_option('mxchat_options', []);
5593 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5594 -
5595 - // Build content from top sources
5596 - foreach ($top_urls as $group_key => $group) {
5597 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5598 -
5599 - // Stop if we've hit the total chunk limit
5600 - if ($total_chunks_used >= $max_total_chunks) {
5601 - break;
5602 - }
5603 -
5604 - $full_text = '';
5605 - $chunks_in_this_source = 1; // Default for non-chunked content
5606 -
5607 - if ($group['is_chunked']) {
5608 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5609 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5610 -
5611 - // Fetch chunks for this URL with limit
5612 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5613 -
5614 - // If fetching all chunks fails, fall back to matched chunks
5615 - if (empty($full_text)) {
5616 - // Sort matched chunks by index and concatenate
5617 - usort($group['chunks'], function($a, $b) {
5618 - return $a['chunk_index'] <=> $b['chunk_index'];
5619 - });
5620 -
5621 - $chunk_texts = array();
5622 - $chunks_in_this_source = 0;
5623 - foreach ($group['chunks'] as $chunk) {
5624 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5625 - break;
5626 - }
5627 - $chunk_texts[] = $chunk['text'];
5628 - $chunks_in_this_source++;
5629 - }
5630 - $full_text = implode("\n\n", $chunk_texts);
2645 + // Fetch and combine content for the top results
2646 + foreach ($top_results as $result) {
2647 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
2648 + // Check if the content is PDF-related and add surrounding pages
2649 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
2650 + error_log('[MXCHAT] ID ' . $result['id'] . ' is PDF content, adding surrounding pages');
2651 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
2652 + "SELECT id, article_content FROM {$system_prompt_table}
2653 + WHERE id IN (
2654 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
2655 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
2656 + )",
2657 + $result['id'],
2658 + $result['id']
2659 + ));
2660 + // Add previous content if it exists
2661 + if (!empty($surrounding_content[0])) {
2662 + $content .= $surrounding_content[0]->article_content . "\n\n";
5631 2663 }
5632 - } else {
5633 - $full_text = $group['single_text'];
5634 - $chunks_in_this_source = 1;
5635 - }
5636 -
5637 - if (!empty($full_text)) {
5638 - // Strip URLs from content if citation links are disabled
5639 - if (!$citation_links_enabled) {
5640 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5641 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
2664 + // Add the main chunk content
2665 + $content .= $chunk_content . "\n\n";
2666 + // Add next content if it exists
2667 + if (!empty($surrounding_content[1])) {
2668 + $content .= $surrounding_content[1]->article_content . "\n\n";
5642 2669 }
5643 -
5644 - // Use numbered reference for URL-based entries, plain info label for manual entries
5645 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5646 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5647 - $matches_used++;
5648 - $content .= "## Reference " . $matches_used . " ##\n";
5649 - $content .= $full_text . "\n\n";
5650 -
5651 - // Only include citation URLs if citation links are enabled
5652 - if ($citation_links_enabled) {
5653 - $valid_urls[] = $source_url;
5654 - $content .= "URL: " . $source_url . "\n\n";
5655 - }
5656 - } else {
5657 - // Manual entry — no reference number, no citation
5658 - $content .= "## Information ##\n";
5659 - $content .= $full_text . "\n\n";
5660 - }
5661 -
5662 - // Extract any URLs from the text content itself (only if citation links enabled)
5663 - if ($citation_links_enabled) {
5664 - preg_match_all(
5665 - '#\bhttps?://[^\s<>"\']+#i',
5666 - $full_text,
5667 - $content_urls
5668 - );
5669 - if (!empty($content_urls[0])) {
5670 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5671 - }
5672 - }
5673 -
5674 - $total_chunks_used += $chunks_in_this_source;
5675 - }
5676 - }
5677 -
5678 - // NEW: Store unique valid URLs for validation
5679 - $this->current_valid_urls = array_unique($valid_urls);
5680 -
5681 - // Store sources and chunks counts for testing/transcript display
5682 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5683 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5684 -
5685 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5686 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5687 -
5688 - // Add response guidelines
5689 - if (empty($top_urls)) {
5690 - $content = "No reference information was found for this query.\n\n";
5691 - } else {
5692 - // Build response guidelines based on citation links setting
5693 - $content .= "\n## Response Guidelines ##\n" .
5694 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5695 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5696 - "If you don't have specific information or are uncertain about any details, it's always " .
5697 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5698 - "When information is incomplete, let them know you are unsure.\n\n";
5699 -
5700 - // Only add hyperlink instructions if citation links are enabled
5701 - if ($citation_links_enabled) {
5702 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5703 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5704 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5705 2670 } else {
5706 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5707 - "Simply provide helpful answers based on the reference information without citing sources.";
2671 + // For non-PDF content, add directly
2672 + $content .= $chunk_content . "\n\n";
5708 2673 }
5709 2674 }
5710 2675
2676 + // Log content length
2677 + error_log('[MXCHAT] Retrieved content length: ' . strlen(trim($content)) . ' characters');
2678 +
5711 2679 return trim($content);
5712 2680 }
5713 2681
2682 +
5714 2683 /**
5715 - * Fetch and reassemble chunks for a URL from WordPress database
5716 - *
5717 - * @param string $source_url The source URL to fetch chunks for
5718 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5719 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5720 - * @return string Reassembled content from chunks
2684 + * Find relevant content in Pinecone vector database
5721 2685 */
5722 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5723 - global $wpdb;
5724 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
2686 +private function find_relevant_content_pinecone($user_embedding) {
2687 + $options = get_option('mxchat_pinecone_addon_options', array());
2688 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
2689 + $host = $options['mxchat_pinecone_host'] ?? '';
5725 2690
5726 - // Fetch all rows with this source_url
5727 - $rows = $wpdb->get_results($wpdb->prepare(
5728 - "SELECT article_content FROM {$table}
5729 - WHERE source_url = %s
5730 - ORDER BY id ASC",
5731 - $source_url
5732 - ));
5733 -
5734 - if (empty($rows)) {
5735 - $chunk_count = 0;
2691 + if (empty($host) || empty($api_key)) {
2692 + //error_log('Pinecone credentials not properly configured');
5736 2693 return '';
5737 2694 }
5738 2695
5739 - // Parse and sort chunks by index
5740 - $chunks = array();
5741 - foreach ($rows as $row) {
5742 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
2696 + // Get similarity threshold from WordPress settings
2697 + $similarity_threshold = ((int) get_option('mxchat_similarity_threshold', 80)) / 100;
5743 2698
5744 - if ($parsed['is_chunked']) {
5745 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5746 - $chunks[$chunk_index] = $parsed['text'];
5747 - } else {
5748 - // Non-chunked content - just return it
5749 - $chunks[] = $parsed['text'];
5750 - }
5751 - }
5752 -
5753 - // Sort by chunk index
5754 - ksort($chunks);
5755 -
5756 - // Apply chunk limit if specified
5757 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5758 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5759 - }
5760 -
5761 - // Store actual chunk count
5762 - $chunk_count = count($chunks);
5763 -
5764 - // Reassemble content
5765 - return implode("\n\n", $chunks);
5766 -}
5767 -
5768 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5769 - global $wpdb;
5770 -
5771 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5772 - //error_log(" - bot_id: " . $bot_id);
5773 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5774 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5775 -
5776 - // Use bot-specific config or fall back to default
5777 - if ($bot_config === null) {
5778 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5779 - }
5780 -
5781 - $api_key = $bot_config['api_key'] ?? '';
5782 - $host = $bot_config['host'] ?? '';
5783 - $namespace = $bot_config['namespace'] ?? '';
5784 -
5785 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5786 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5787 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5788 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5789 -
5790 - // Initialize similarity analysis storage
5791 - $this->last_similarity_analysis = [
5792 - 'knowledge_base_type' => 'Pinecone',
5793 - 'bot_id' => $bot_id,
5794 - 'namespace' => $namespace,
5795 - 'top_matches' => [],
5796 - 'threshold_used' => 0,
5797 - 'total_checked' => 0
5798 - ];
5799 -
5800 - // NEW: Initialize valid URLs array
5801 - $valid_urls = [];
5802 -
5803 - if (empty($host) || empty($api_key)) {
5804 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5805 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5806 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5807 - // Store empty array for valid URLs since we can't proceed
5808 - $this->current_valid_urls = [];
5809 - return '';
5810 - }
5811 -
5812 - // Get knowledge manager instance for role checking
5813 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5814 -
5815 - // Get the similarity threshold from the bot options or main options
5816 - $bot_options = $this->get_bot_options($bot_id);
5817 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
5818 -
5819 - $similarity_threshold = isset($current_options['similarity_threshold'])
5820 - ? ((int) $current_options['similarity_threshold']) / 100
5821 - : 0.35;
5822 -
5823 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5824 -
5825 2699 // Prepare the query request for Pinecone
5826 2700 $api_endpoint = "https://{$host}/query";
5827 -
2701 +
5828 2702 $request_body = array(
5829 2703 'vector' => $user_embedding,
5830 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
2704 + 'topK' => 5,
5831 2705 'includeMetadata' => true,
5832 2706 'includeValues' => true
5833 2707 );
5834 -
5835 - // Add namespace if specified for this bot
5836 - if (!empty($namespace)) {
5837 - $request_body['namespace'] = $namespace;
5838 - }
5839 -
5840 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5841 - //error_log(" - Endpoint: " . $api_endpoint);
5842 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5843 -
2708 +
5844 2709 $response = wp_remote_post($api_endpoint, array(
5845 2710 'headers' => array(
5846 2711 'Api-Key' => $api_key,
5847 2712 'accept' => 'application/json',
@@ -5849,879 +2714,46 @@
5849 2714 ),
5850 2715 'body' => wp_json_encode($request_body),
5851 2716 'timeout' => 30
5852 2717 ));
5853 -
2718 +
5854 2719 if (is_wp_error($response)) {
5855 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5856 - // Store empty array for valid URLs
5857 - $this->current_valid_urls = [];
2720 + //error_log('Pinecone query error: ' . $response->get_error_message());
5858 2721 return '';
5859 2722 }
5860 -
2723 +
5861 2724 $response_code = wp_remote_retrieve_response_code($response);
5862 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5863 -
5864 2725 if ($response_code !== 200) {
5865 - $response_body = wp_remote_retrieve_body($response);
5866 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5867 - // Store empty array for valid URLs
5868 - $this->current_valid_urls = [];
2726 + //error_log('Pinecone API error: ' . wp_remote_retrieve_body($response));
5869 2727 return '';
5870 2728 }
5871 -
5872 - // ADD DETAILED DEBUG SECTION HERE
5873 - $response_body = wp_remote_retrieve_body($response);
5874 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5875 -
5876 - $results = json_decode($response_body, true);
5877 -
5878 - if (json_last_error() !== JSON_ERROR_NONE) {
5879 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5880 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5881 - // Store empty array for valid URLs
5882 - $this->current_valid_urls = [];
5883 - return '';
5884 - }
5885 -
5886 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5887 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5888 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5889 -
2729 +
2730 + $results = json_decode(wp_remote_retrieve_body($response), true);
5890 2731 if (empty($results['matches'])) {
5891 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5892 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5893 - // Store empty array for valid URLs
5894 - $this->current_valid_urls = [];
5895 2732 return '';
5896 2733 }
5897 -
5898 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
5899 -
5900 - // Log first match details for debugging
5901 - if (!empty($results['matches'][0])) {
5902 - $first_match = $results['matches'][0];
5903 - //error_log("MXCHAT DEBUG: First match details:");
5904 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
5905 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
5906 - if (isset($first_match['metadata'])) {
5907 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
5908 - }
5909 - }
5910 -
2734 +
5911 2735 // Initialize the final content
5912 2736 $content = '';
5913 - $matches_used = 0;
5914 - $matches_used_for_context = [];
5915 - $total_chunks_used = 0;
5916 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5917 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5918 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5919 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5920 2737
5921 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5922 - // Use fresh options to ensure we get the latest setting value
5923 - $fresh_options = get_option('mxchat_options', []);
5924 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5925 -
5926 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
5927 - $url_groups = array();
5928 -
5929 - foreach ($results['matches'] as $index => $match) {
2738 + // Process each match
2739 + foreach ($results['matches'] as $match) {
5930 2740 // Skip if similarity is below threshold
5931 2741 if ($match['score'] < $similarity_threshold) {
5932 2742 continue;
5933 2743 }
5934 2744
5935 - $metadata = $match['metadata'] ?? array();
5936 - $source_url = $metadata['source_url'] ?? '';
5937 - $match_id = $match['id'] ?? '';
5938 -
5939 - // LAZY ROLE CHECK: Only check role for content we're actually considering
5940 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
5941 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5942 -
5943 - // Skip if user doesn't have access
5944 - if (!$has_access) {
5945 - continue;
2745 + if (!empty($match['metadata']['text']) && !empty($match['metadata']['source_url'])) {
2746 + // Add content with citation
2747 + $content .= $match['metadata']['text'] . "\n";
2748 + $content .= "Source: " . $match['metadata']['source_url'] . "\n\n";
5946 2749 }
5947 -
5948 - // Use a unique key for manual entries without a source URL
5949 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
5950 -
5951 - // Group by source URL (or unique key for manual entries)
5952 - if (!isset($url_groups[$group_key])) {
5953 - $url_groups[$group_key] = array(
5954 - 'source_url' => $source_url,
5955 - 'best_score' => 0,
5956 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
5957 - 'chunks' => array(),
5958 - 'single_text' => ''
5959 - );
5960 - }
5961 -
5962 - // Track best score for this group
5963 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
5964 - $url_groups[$group_key]['best_score'] = $match['score'];
5965 - }
5966 -
5967 - // Store chunk info or single text
5968 - if ($url_groups[$group_key]['is_chunked']) {
5969 - $url_groups[$group_key]['chunks'][] = array(
5970 - 'id' => $match_id,
5971 - 'score' => $match['score'],
5972 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
5973 - 'text' => $metadata['text'] ?? ''
5974 - );
5975 - } else {
5976 - // Non-chunked content - just store the text
5977 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
5978 - $url_groups[$group_key]['single_id'] = $match_id;
5979 - }
5980 2750 }
5981 2751
5982 - // Sort URL groups by best score (highest first)
5983 - uasort($url_groups, function($a, $b) {
5984 - return $b['best_score'] <=> $a['best_score'];
5985 - });
5986 -
5987 - // Get RAG sources limit from options (default 6, min 3, max 10)
5988 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5989 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5990 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5991 -
5992 - // Take top N unique URLs based on user setting
5993 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5994 -
5995 - // Track which match IDs are actually used for context
5996 - foreach ($top_urls as $group) {
5997 - if ($group['is_chunked']) {
5998 - foreach ($group['chunks'] as $chunk) {
5999 - $matches_used_for_context[] = $chunk['id'];
6000 - }
6001 - } elseif (!empty($group['single_id'])) {
6002 - $matches_used_for_context[] = $group['single_id'];
6003 - }
6004 - }
6005 -
6006 - // Build content from top sources
6007 - foreach ($top_urls as $group_key => $group) {
6008 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6009 -
6010 - // Stop if we've hit the total chunk limit
6011 - if ($total_chunks_used >= $max_total_chunks) {
6012 - break;
6013 - }
6014 -
6015 - $full_text = '';
6016 - $chunks_in_this_source = 1; // Default for non-chunked content
6017 -
6018 - if ($group['is_chunked']) {
6019 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6020 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6021 -
6022 - // Fetch chunks for this URL with limit
6023 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6024 -
6025 - // If fetching all chunks fails, fall back to matched chunks
6026 - if (empty($full_text)) {
6027 - // Sort matched chunks by index and concatenate
6028 - usort($group['chunks'], function($a, $b) {
6029 - return $a['chunk_index'] <=> $b['chunk_index'];
6030 - });
6031 -
6032 - $chunk_texts = array();
6033 - $chunks_in_this_source = 0;
6034 - foreach ($group['chunks'] as $chunk) {
6035 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6036 - break;
6037 - }
6038 - $chunk_texts[] = $chunk['text'];
6039 - $chunks_in_this_source++;
6040 - }
6041 - $full_text = implode("\n\n", $chunk_texts);
6042 - }
6043 - } else {
6044 - $full_text = $group['single_text'];
6045 - $chunks_in_this_source = 1;
6046 - }
6047 -
6048 - if (!empty($full_text)) {
6049 - // Strip URLs from content if citation links are disabled
6050 - if (!$citation_links_enabled) {
6051 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6052 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6053 - }
6054 -
6055 - // Use numbered reference for URL-based entries, plain info label for manual entries
6056 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6057 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6058 - $matches_used++;
6059 - $content .= "## Reference " . $matches_used . " ##\n";
6060 - $content .= $full_text . "\n\n";
6061 -
6062 - // Only include citation URLs if citation links are enabled
6063 - if ($citation_links_enabled) {
6064 - $valid_urls[] = $source_url;
6065 - $content .= "URL: " . $source_url . "\n\n";
6066 - }
6067 - } else {
6068 - // Manual entry — no reference number, no citation
6069 - $content .= "## Information ##\n";
6070 - $content .= $full_text . "\n\n";
6071 - }
6072 -
6073 - // Extract any URLs from the text content itself (only if citation links enabled)
6074 - if ($citation_links_enabled) {
6075 - preg_match_all(
6076 - '#\bhttps?://[^\s<>"\']+#i',
6077 - $full_text,
6078 - $content_urls
6079 - );
6080 - if (!empty($content_urls[0])) {
6081 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6082 - }
6083 - }
6084 -
6085 - $total_chunks_used += $chunks_in_this_source;
6086 - }
6087 - }
6088 -
6089 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6090 - $all_matches = [];
6091 - foreach ($results['matches'] as $index => $match) {
6092 - if ($index >= 10) break; // Limit to top 10 for testing
6093 -
6094 - $match_id = $match['id'] ?? '';
6095 -
6096 - // Check role access for testing display (use cache if available)
6097 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6098 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6099 -
6100 - $source_display = '';
6101 - if (!empty($match['metadata']['source_url'])) {
6102 - $source_display = $match['metadata']['source_url'];
6103 - } else {
6104 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6105 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6106 - $source_display = substr(trim($content_preview), 0, 50) . '...';
6107 - }
6108 -
6109 - $match_id_for_display = $match['id'] ?? $index;
6110 -
6111 - // Check for chunk metadata in Pinecone
6112 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6113 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6114 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6115 -
6116 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6117 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6118 - $is_chunk = true;
6119 - }
6120 -
6121 - $all_matches[] = [
6122 - 'document_id' => $match_id_for_display,
6123 - 'similarity' => $match['score'],
6124 - 'similarity_percentage' => round($match['score'] * 100, 2),
6125 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6126 - 'source_display' => $source_display,
6127 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6128 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6129 - 'role_restriction' => $role_restriction,
6130 - 'has_access' => $has_access,
6131 - 'filtered_out' => !$has_access,
6132 - 'is_chunk' => $is_chunk,
6133 - 'chunk_index' => $chunk_index,
6134 - 'total_chunks' => $total_chunks
6135 - ];
6136 - }
6137 -
6138 - // Store for testing panel
6139 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6140 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6141 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6142 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6143 -
6144 - // NEW: Store unique valid URLs for validation
6145 - $this->current_valid_urls = array_unique($valid_urls);
6146 -
6147 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6148 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6149 -
6150 - // Add response guidelines
6151 - if ($matches_used === 0) {
6152 - $content = "No reference information was found for this query.\n\n";
6153 - } else {
6154 - // Build response guidelines based on citation links setting
6155 - $content .= "\n## Response Guidelines ##\n" .
6156 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6157 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6158 - "If you don't have specific information or are uncertain about any details, it's always " .
6159 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6160 - "When information is incomplete, let them know you are unsure.\n\n";
6161 -
6162 - // Only add hyperlink instructions if citation links are enabled
6163 - if ($citation_links_enabled) {
6164 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6165 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6166 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6167 - } else {
6168 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6169 - "Simply provide helpful answers based on the reference information without citing sources.";
6170 - }
6171 - }
6172 -
6173 2752 return trim($content);
6174 2753 }
6175 2754
6176 -/**
6177 - * Get role restriction for a single vector (with caching)
6178 - */
6179 -private function get_single_vector_role($vector_id, $metadata = array()) {
6180 - global $wpdb;
6181 -
6182 - if (empty($vector_id)) {
6183 - return 'public';
6184 - }
6185 -
6186 - // Check cache first
6187 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6188 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6189 -
6190 - if ($cached_role !== false) {
6191 - return $cached_role;
6192 - }
6193 -
6194 - $role_restriction = 'public';
6195 -
6196 - // First try Pinecone metadata
6197 - if (!empty($metadata['role_restriction'])) {
6198 - $role_restriction = $metadata['role_restriction'];
6199 - } else {
6200 - // Check WordPress table for user-modified roles
6201 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6202 - $stored_role = $wpdb->get_var($wpdb->prepare(
6203 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6204 - $vector_id
6205 - ));
6206 -
6207 - if ($stored_role) {
6208 - $role_restriction = $stored_role;
6209 - }
6210 - }
6211 -
6212 - // Cache individual role for 1 hour
6213 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6214 2755
6215 - return $role_restriction;
6216 -}
6217 -
6218 -/**
6219 - * Fetch and reassemble all chunks for a URL from Pinecone
6220 - *
6221 - * @param string $source_url The source URL to fetch chunks for
6222 - * @param array $bot_config Bot-specific Pinecone configuration
6223 - * @return string Reassembled content from all chunks
6224 - */
6225 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6226 - $api_key = $bot_config['api_key'] ?? '';
6227 - $host = $bot_config['host'] ?? '';
6228 - $namespace = $bot_config['namespace'] ?? '';
6229 -
6230 - if (empty($host) || empty($api_key)) {
6231 - $chunk_count = 0;
6232 - return '';
6233 - }
6234 -
6235 - $base_hash = md5($source_url);
6236 -
6237 - // Use Pinecone list API to find all chunk vectors with this prefix
6238 - $list_url = "https://{$host}/vectors/list";
6239 -
6240 - // Limit to max_chunks if specified, otherwise fetch up to 100
6241 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6242 -
6243 - $list_body = array(
6244 - 'prefix' => $base_hash . '_chunk_',
6245 - 'limit' => $fetch_limit
6246 - );
6247 -
6248 - if (!empty($namespace)) {
6249 - $list_body['namespace'] = $namespace;
6250 - }
6251 -
6252 - $list_response = wp_remote_post($list_url, array(
6253 - 'headers' => array(
6254 - 'Api-Key' => $api_key,
6255 - 'accept' => 'application/json',
6256 - 'content-type' => 'application/json'
6257 - ),
6258 - 'body' => wp_json_encode($list_body),
6259 - 'timeout' => 30
6260 - ));
6261 -
6262 - if (is_wp_error($list_response)) {
6263 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6264 - return '';
6265 - }
6266 -
6267 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6268 -
6269 - if (empty($list_data['vectors'])) {
6270 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6271 - return '';
6272 - }
6273 -
6274 - // Extract vector IDs
6275 - $vector_ids = array();
6276 - foreach ($list_data['vectors'] as $vector) {
6277 - if (isset($vector['id'])) {
6278 - $vector_ids[] = $vector['id'];
6279 - }
6280 - }
6281 -
6282 - if (empty($vector_ids)) {
6283 - return '';
6284 - }
6285 -
6286 - // Fetch all chunk content
6287 - $fetch_url = "https://{$host}/vectors/fetch";
6288 -
6289 - $fetch_body = array(
6290 - 'ids' => $vector_ids
6291 - );
6292 -
6293 - if (!empty($namespace)) {
6294 - $fetch_body['namespace'] = $namespace;
6295 - }
6296 -
6297 - $fetch_response = wp_remote_post($fetch_url, array(
6298 - 'headers' => array(
6299 - 'Api-Key' => $api_key,
6300 - 'accept' => 'application/json',
6301 - 'content-type' => 'application/json'
6302 - ),
6303 - 'body' => wp_json_encode($fetch_body),
6304 - 'timeout' => 30
6305 - ));
6306 -
6307 - if (is_wp_error($fetch_response)) {
6308 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6309 - return '';
6310 - }
6311 -
6312 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6313 -
6314 - if (empty($fetch_data['vectors'])) {
6315 - return '';
6316 - }
6317 -
6318 - // Sort chunks by index and reassemble
6319 - $chunks = array();
6320 - foreach ($fetch_data['vectors'] as $id => $vector) {
6321 - $metadata = $vector['metadata'] ?? array();
6322 - $chunk_index = $metadata['chunk_index'] ?? 0;
6323 - $text = $metadata['text'] ?? '';
6324 -
6325 - // Store chunk with its index
6326 - $chunks[$chunk_index] = $text;
6327 - }
6328 -
6329 - // Sort by chunk index
6330 - ksort($chunks);
6331 -
6332 - // Apply chunk limit if specified
6333 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6334 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6335 - }
6336 -
6337 - // Store actual chunk count
6338 - $chunk_count = count($chunks);
6339 -
6340 - // Reassemble content
6341 - return implode("\n\n", $chunks);
6342 -}
6343 -
6344 -/**
6345 - * Search for relevant content using OpenAI Vector Store (File Search)
6346 - *
6347 - * @param string $user_query The user's query text
6348 - * @param string $bot_id The bot ID
6349 - * @param array $vectorstore_config Vector Store configuration
6350 - * @return string Formatted context string with references
6351 - */
6352 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6353 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6354 - //error_log(" - bot_id: " . $bot_id);
6355 - //error_log(" - user_query length: " . strlen($user_query));
6356 -
6357 - // Get OpenAI API key
6358 - $mxchat_options = get_option('mxchat_options', array());
6359 - $api_key = $mxchat_options['api_key'] ?? '';
6360 -
6361 - // Reset vectorstore error tracking
6362 - $this->last_vectorstore_error = null;
6363 -
6364 - if (empty($api_key)) {
6365 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6366 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6367 - $this->current_valid_urls = [];
6368 - return '';
6369 - }
6370 -
6371 - // Get Vector Store configuration
6372 - if (empty($vectorstore_config)) {
6373 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6374 - }
6375 -
6376 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6377 - $max_results = $vectorstore_config['max_results'] ?? 5;
6378 -
6379 - if (empty($vectorstore_ids_string)) {
6380 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6381 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6382 - $this->current_valid_urls = [];
6383 - return '';
6384 - }
6385 -
6386 - // Parse Vector Store IDs
6387 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6388 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6389 -
6390 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6391 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6392 -
6393 - // Initialize similarity analysis storage
6394 - $this->last_similarity_analysis = [
6395 - 'knowledge_base_type' => 'OpenAI Vector Store',
6396 - 'bot_id' => $bot_id,
6397 - 'vectorstore_ids' => $vectorstore_ids,
6398 - 'top_matches' => [],
6399 - 'threshold_used' => 0,
6400 - 'total_checked' => 0
6401 - ];
6402 -
6403 - $valid_urls = [];
6404 -
6405 - // Get the selected model
6406 - $bot_options = $this->get_bot_options($bot_id);
6407 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6408 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6409 -
6410 - // Verify it's an OpenAI model
6411 - if (!$this->is_openai_chat_model($selected_model)) {
6412 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6413 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6414 - $this->current_valid_urls = [];
6415 - return '';
6416 - }
6417 -
6418 - // Use OpenAI Responses API with file_search tool
6419 - $request_body = array(
6420 - 'model' => $selected_model,
6421 - 'input' => $user_query,
6422 - 'tools' => array(
6423 - array(
6424 - 'type' => 'file_search',
6425 - 'vector_store_ids' => $vectorstore_ids,
6426 - 'max_num_results' => intval($max_results)
6427 - )
6428 - ),
6429 - 'include' => array('output[*].file_search_call.search_results')
6430 - );
6431 -
6432 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6433 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6434 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6435 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6436 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6437 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6438 -
6439 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6440 - 'headers' => array(
6441 - 'Authorization' => 'Bearer ' . $api_key,
6442 - 'Content-Type' => 'application/json'
6443 - ),
6444 - 'body' => wp_json_encode($request_body),
6445 - 'timeout' => 60
6446 - ));
6447 -
6448 - if (is_wp_error($response)) {
6449 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6450 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6451 - $this->current_valid_urls = [];
6452 - return '';
6453 - }
6454 -
6455 - $response_code = wp_remote_retrieve_response_code($response);
6456 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6457 -
6458 - $response_body = wp_remote_retrieve_body($response);
6459 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6460 -
6461 - if ($response_code !== 200) {
6462 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6463 - $api_error_detail = '';
6464 - $decoded_error = json_decode($response_body, true);
6465 - if (isset($decoded_error['error']['message'])) {
6466 - $api_error_detail = $decoded_error['error']['message'];
6467 - }
6468 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6469 - $this->current_valid_urls = [];
6470 - return '';
6471 - }
6472 - $result = json_decode($response_body, true);
6473 -
6474 - if (json_last_error() !== JSON_ERROR_NONE) {
6475 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6476 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6477 - $this->current_valid_urls = [];
6478 - return '';
6479 - }
6480 -
6481 - // Debug: Log the structure of the result
6482 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6483 - if (isset($result['output'])) {
6484 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6485 - foreach ($result['output'] as $idx => $out) {
6486 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6487 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6488 - }
6489 - } else {
6490 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6491 - }
6492 -
6493 - // Extract file search results from the response
6494 - $content = '';
6495 - $matches_used = 0;
6496 - $all_matches = [];
6497 -
6498 - // The Responses API returns output array with tool results
6499 - if (isset($result['output']) && is_array($result['output'])) {
6500 - foreach ($result['output'] as $output_item) {
6501 - // Look for file_search_call results
6502 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6503 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6504 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6505 -
6506 - // Check for search_results in the output item directly
6507 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6508 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6509 -
6510 - if (empty($search_results)) {
6511 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6512 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6513 - }
6514 -
6515 - foreach ($search_results as $index => $search_result) {
6516 - $filename = $search_result['filename'] ?? '';
6517 - $score = $search_result['score'] ?? 0;
6518 - $text_content = '';
6519 -
6520 - // Extract text content from the result
6521 - // The text can be directly on the result OR nested under content array
6522 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6523 - // Direct text field (OpenAI's actual format)
6524 - $text_content = $search_result['text'];
6525 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6526 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6527 - // Nested content array format
6528 - foreach ($search_result['content'] as $content_item) {
6529 - if (isset($content_item['text'])) {
6530 - $text_content .= $content_item['text'] . "\n";
6531 - }
6532 - }
6533 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6534 - } else {
6535 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6536 - }
6537 -
6538 - if (!empty($text_content)) {
6539 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6540 - $content .= trim($text_content) . "\n\n";
6541 -
6542 - if (!empty($filename)) {
6543 - $content .= "Source: " . $filename . "\n\n";
6544 - }
6545 -
6546 - // Extract URLs from content
6547 - preg_match_all(
6548 - '#\bhttps?://[^\s<>"\']+#i',
6549 - $text_content,
6550 - $content_urls
6551 - );
6552 - if (!empty($content_urls[0])) {
6553 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6554 - }
6555 -
6556 - $matches_used++;
6557 - }
6558 -
6559 - // Store for similarity analysis
6560 - $all_matches[] = [
6561 - 'document_id' => $filename ?: ('result_' . $index),
6562 - 'similarity' => $score,
6563 - 'similarity_percentage' => round($score * 100, 2),
6564 - 'above_threshold' => true,
6565 - 'source_display' => $filename,
6566 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6567 - 'used_for_context' => true,
6568 - 'role_restriction' => 'public',
6569 - 'has_access' => true,
6570 - 'filtered_out' => false
6571 - ];
6572 - }
6573 - }
6574 -
6575 - // Also check for message content with annotations (citations)
6576 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6577 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6578 - foreach ($output_item['content'] as $content_block) {
6579 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6580 - foreach ($content_block['annotations'] as $annotation) {
6581 - if (isset($annotation['filename'])) {
6582 - $filename = $annotation['filename'];
6583 - $score = $annotation['score'] ?? 0;
6584 - $text_content = '';
6585 -
6586 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6587 - foreach ($annotation['content'] as $ann_content) {
6588 - if (isset($ann_content['text'])) {
6589 - $text_content .= $ann_content['text'] . "\n";
6590 - }
6591 - }
6592 - }
6593 -
6594 - if (!empty($text_content) && $matches_used < $max_results) {
6595 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6596 - $content .= trim($text_content) . "\n\n";
6597 - $content .= "Source: " . $filename . "\n\n";
6598 -
6599 - preg_match_all(
6600 - '#\bhttps?://[^\s<>"\']+#i',
6601 - $text_content,
6602 - $content_urls
6603 - );
6604 - if (!empty($content_urls[0])) {
6605 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6606 - }
6607 -
6608 - $matches_used++;
6609 -
6610 - $all_matches[] = [
6611 - 'document_id' => $filename,
6612 - 'similarity' => $score,
6613 - 'similarity_percentage' => round($score * 100, 2),
6614 - 'above_threshold' => true,
6615 - 'source_display' => $filename,
6616 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6617 - 'used_for_context' => true,
6618 - 'role_restriction' => 'public',
6619 - 'has_access' => true,
6620 - 'filtered_out' => false
6621 - ];
6622 - }
6623 - }
6624 - }
6625 - }
6626 - }
6627 - }
6628 - }
6629 - }
6630 - }
6631 -
6632 - // Store for testing panel
6633 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6634 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6635 -
6636 - // Store unique valid URLs for validation
6637 - $this->current_valid_urls = array_unique($valid_urls);
6638 -
6639 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6640 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6641 -
6642 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6643 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6644 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6645 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6646 - if ($matches_used > 0) {
6647 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6648 - }
6649 -
6650 - // Check if citation links are enabled
6651 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6652 -
6653 - // Add response guidelines
6654 - if ($matches_used === 0) {
6655 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6656 - $content = "No reference information was found for this query.\n\n";
6657 - } else {
6658 - // Build response guidelines based on citation links setting
6659 - $content .= "\n## Response Guidelines ##\n" .
6660 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6661 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6662 - "If you don't have specific information or are uncertain about any details, it's always " .
6663 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6664 - "When information is incomplete, let them know you are unsure.\n\n";
6665 -
6666 - // Only add hyperlink instructions if citation links are enabled
6667 - if ($citation_links_enabled) {
6668 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6669 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6670 - } else {
6671 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6672 - "Simply provide helpful answers based on the reference information without citing sources.";
6673 - }
6674 - }
6675 -
6676 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6677 -
6678 - return trim($content);
6679 -}
6680 -
6681 -/**
6682 - * Check if the given model is an OpenAI chat model
6683 - *
6684 - * @param string $model The model ID
6685 - * @return bool True if it's an OpenAI model
6686 - */
6687 -private function is_openai_chat_model($model) {
6688 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6689 - foreach ($openai_prefixes as $prefix) {
6690 - if (strpos($model, $prefix) === 0) {
6691 - return true;
6692 - }
6693 - }
6694 - return false;
6695 -}
6696 -
6697 -/**
6698 - * Get bot-specific Vector Store configuration
6699 - *
6700 - * @param string $bot_id The bot ID
6701 - * @return array Configuration array
6702 - */
6703 -private function get_bot_vectorstore_config($bot_id = 'default') {
6704 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6705 -
6706 - // Default global settings
6707 - $default_config = array(
6708 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6709 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6710 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6711 - );
6712 -
6713 - // Allow multi-bot plugin to override with bot-specific settings
6714 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6715 -
6716 - // Preserve max_results from global settings if not set in bot config
6717 - if (!isset($bot_config['max_results'])) {
6718 - $bot_config['max_results'] = $default_config['max_results'];
6719 - }
6720 -
6721 - return $bot_config;
6722 -}
6723 -
6724 2756 private function mxchat_find_relevant_products($user_embedding) {
6725 2757 //error_log('MXChat Vector Search: Starting product search...');
6726 2758
6727 2759 // Retrieve the add-on settings from the database
@@ -6739,78 +2771,77 @@
6739 2771 //error_log('MXChat Vector Search: Using WordPress database for products');
6740 2772 return $this->find_relevant_products_wordpress($user_embedding);
6741 2773 }
6742 2774 }
2775 +
6743 2776 private function find_relevant_products_wordpress($user_embedding) {
6744 2777 global $wpdb;
6745 2778 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
2779 + $cache_key = 'mxchat_system_prompt_embeddings';
2780 + $batch_size = 500;
6746 2781
6747 - if (!is_array($user_embedding)) {
6748 - return '';
6749 - }
2782 + // Original WordPress database search logic
2783 + // [Previous implementation remains the same]
2784 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2785 + if ($embeddings === false) {
2786 + $embeddings = [];
2787 + $offset = 0;
6750 2788
6751 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6752 - // results above the similarity threshold. Peak memory is bounded by
6753 - // $batch_size embedding rows plus a 3-element top list.
6754 - $batch_size = 250;
6755 - $similarity_threshold = 0.85;
6756 - $top_k = 3;
6757 - $top_results = [];
6758 - $offset = 0;
2789 + do {
2790 + $query = $wpdb->prepare(
2791 + "SELECT id, embedding_vector
2792 + FROM {$system_prompt_table}
2793 + LIMIT %d OFFSET %d",
2794 + $batch_size,
2795 + $offset
2796 + );
6759 2797
6760 - do {
6761 - $batch = $wpdb->get_results($wpdb->prepare(
6762 - "SELECT id, embedding_vector
6763 - FROM {$system_prompt_table}
6764 - LIMIT %d OFFSET %d",
6765 - $batch_size,
6766 - $offset
6767 - ));
2798 + $batch = $wpdb->get_results($query);
2799 + if (empty($batch)) {
2800 + break;
2801 + }
6768 2802
6769 - if (empty($batch)) {
6770 - break;
6771 - }
2803 + $embeddings = array_merge($embeddings, $batch);
2804 + $offset += $batch_size;
6772 2805
6773 - foreach ($batch as $row) {
6774 - $database_embedding = $row->embedding_vector
6775 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6776 - : null;
2806 + unset($batch);
6777 2807
6778 - if (!is_array($database_embedding)) {
6779 - unset($database_embedding);
6780 - continue;
6781 - }
2808 + } while (true);
6782 2809
2810 + if (empty($embeddings)) {
2811 + return '';
2812 + }
2813 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2814 + }
2815 +
2816 + $relevant_results = [];
2817 + foreach ($embeddings as $embedding) {
2818 + $database_embedding = $embedding->embedding_vector
2819 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2820 + : null;
2821 + if (is_array($database_embedding) && is_array($user_embedding)) {
6783 2822 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6784 - unset($database_embedding);
6785 -
6786 - if ($similarity < $similarity_threshold) {
6787 - continue;
6788 - }
6789 -
6790 - // Insert into bounded top-K (kept sorted descending)
6791 - if (count($top_results) < $top_k) {
6792 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6793 - usort($top_results, function ($a, $b) {
6794 - return $b['similarity'] <=> $a['similarity'];
6795 - });
6796 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6797 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6798 - usort($top_results, function ($a, $b) {
6799 - return $b['similarity'] <=> $a['similarity'];
6800 - });
6801 - }
2823 + $relevant_results[] = [
2824 + 'id' => $embedding->id,
2825 + 'similarity' => $similarity
2826 + ];
6802 2827 }
2828 + unset($database_embedding);
2829 + }
6803 2830
6804 - unset($batch);
6805 - $offset += $batch_size;
6806 - } while (true);
2831 + // Use fixed threshold for products
2832 + $similarity_threshold = 0.85;
6807 2833
6808 - if (empty($top_results)) {
6809 - return '';
6810 - }
2834 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2835 + return $result['similarity'] >= $similarity_threshold;
2836 + });
2837 + usort($relevant_results, function ($a, $b) {
2838 + return $b['similarity'] <=> $a['similarity'];
2839 + });
6811 2840
2841 + $top_results = array_slice($relevant_results, 0, 5);
6812 2842 $content = '';
2843 +
6813 2844 foreach ($top_results as $result) {
6814 2845 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6815 2846 $content .= $chunk_content . "\n\n";
6816 2847 }
@@ -6817,9 +2848,9 @@
6817 2848
6818 2849 return trim($content);
6819 2850 }
6820 2851
6821 -
2852 +// Modified search function with correct filter syntax
6822 2853 private function find_relevant_products_pinecone($user_embedding) {
6823 2854 //error_log('Starting Pinecone product search...');
6824 2855
6825 2856 $options = get_option('mxchat_pinecone_addon_options', array());
@@ -6916,2479 +2947,319 @@
6916 2947
6917 2948 return null;
6918 2949 }
6919 2950
6920 -/**
6921 - * Get system instructions for a specific bot or default
6922 - * Checks for multi-bot add-on and uses bot-specific instructions if available
6923 - * Automatically strips URLs if citation links are disabled
6924 - * Replaces {visitor_name} placeholder with actual visitor name if available
6925 - *
6926 - * @param string $bot_id The bot ID to get instructions for
6927 - * @param string $session_id Optional session ID to lookup visitor name
6928 - */
6929 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
6930 - $instructions = '';
6931 -
6932 - // Check if multi-bot add-on is active
6933 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
6934 - // Get bot-specific options from multi-bot add-on
6935 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
6936 -
6937 - // If bot has custom system instructions, use those
6938 - if (!empty($bot_options['system_prompt_instructions'])) {
6939 - $instructions = $bot_options['system_prompt_instructions'];
6940 - }
6941 - }
6942 -
6943 - // Fall back to default system instructions
6944 - if (empty($instructions)) {
6945 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
6946 - }
6947 -
6948 - // Check if citation links are disabled - if so, strip URLs from instructions
6949 - $fresh_options = get_option('mxchat_options', []);
6950 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6951 -
6952 - if (!$citation_links_enabled && !empty($instructions)) {
6953 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
6954 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
6955 - }
6956 -
6957 - // Replace {visitor_name} placeholder with actual visitor name if available
6958 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
6959 - $name_option_key = "mxchat_name_{$session_id}";
6960 - $visitor_name = get_option($name_option_key, '');
6961 -
6962 - if (!empty($visitor_name)) {
6963 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
6964 - } else {
6965 - // Remove placeholder if no name is available
6966 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
6967 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
6968 - }
6969 - }
6970 -
6971 - // Allow developers to filter system instructions and process shortcodes
6972 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
6973 - $instructions = do_shortcode($instructions);
6974 -
6975 - return $instructions;
6976 -}
6977 -/**
6978 - * Get the current bot ID from session or request context
6979 - */
6980 -private function get_current_bot_id($session_id = '') {
6981 - // First, check if bot_id is passed in the current request
6982 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
6983 - return sanitize_key($_POST['bot_id']);
6984 - }
6985 -
6986 - // If not in POST, try to get it from session data
6987 - if (!empty($session_id)) {
6988 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
6989 - if (!empty($bot_id)) {
6990 - return $bot_id;
6991 - }
6992 - }
6993 -
6994 - // Fall back to default
6995 - return 'default';
6996 -}
6997 -private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $openrouter_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-5.1-chat-latest') {
2951 +// Function definition
2952 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $conversation_history) {
6998 2953 try {
6999 2954 if (!$relevant_content) {
7000 - $error_response = [
7001 - 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7002 - 'error_code' => 'no_relevant_content'
7003 - ];
7004 -
7005 - if ($testing_data !== null) {
7006 - $error_response['testing_data'] = $testing_data;
7007 - }
7008 -
7009 - return $error_response;
2955 + return esc_html__("I'm sorry, I couldn't find relevant information on that topic.", 'mxchat');
7010 2956 }
7011 -
2957 +
2958 + // Ensure conversation_history is an array
7012 2959 if (!is_array($conversation_history)) {
7013 2960 $conversation_history = array();
7014 2961 }
7015 -
7016 - // Check if this is an OpenRouter model
7017 - if ($selected_model === 'openrouter') {
7018 - // Get the actual OpenRouter model from options
7019 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7020 -
7021 - if (empty($openrouter_selected_model)) {
7022 - $error_response = [
7023 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7024 - 'error_code' => 'no_openrouter_model_selected'
7025 - ];
7026 - if ($testing_data !== null) {
7027 - $error_response['testing_data'] = $testing_data;
7028 - }
7029 - return $error_response;
7030 - }
7031 -
7032 - if (empty($openrouter_api_key)) {
7033 - $error_response = [
7034 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7035 - 'error_code' => 'missing_openrouter_api_key'
7036 - ];
7037 - if ($testing_data !== null) {
7038 - $error_response['testing_data'] = $testing_data;
7039 - }
7040 - return $error_response;
7041 - }
7042 -
7043 - if ($streaming) {
7044 - return $this->mxchat_generate_response_openrouter_stream(
7045 - $openrouter_selected_model,
7046 - $openrouter_api_key,
7047 - $conversation_history,
7048 - $relevant_content,
7049 - $session_id,
7050 - $testing_data
7051 - );
7052 - } else {
7053 - $response = $this->mxchat_generate_response_openrouter(
7054 - $openrouter_selected_model,
7055 - $openrouter_api_key,
7056 - $conversation_history,
7057 - $relevant_content
7058 - );
7059 - }
7060 -
7061 - if (is_array($response) && isset($response['error'])) {
7062 - if ($testing_data !== null) {
7063 - $response['testing_data'] = $testing_data;
7064 - }
7065 - return $response;
7066 - }
7067 -
7068 - return $response;
7069 - }
7070 2962
2963 + // Get selected model with default fallback
2964 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
2965 +
7071 2966 // Extract model prefix to determine the provider
7072 2967 $model_parts = explode('-', $selected_model);
7073 2968 $provider = strtolower($model_parts[0]);
7074 -
2969 +
7075 2970 // Handle model selection based on provider prefix
7076 2971 switch ($provider) {
7077 - case 'gemini':
7078 - if (empty($gemini_api_key)) {
7079 - $error_response = [
7080 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
7081 - 'error_code' => 'missing_gemini_api_key'
7082 - ];
7083 - if ($testing_data !== null) {
7084 - $error_response['testing_data'] = $testing_data;
7085 - }
7086 - return $error_response;
2972 + case 'claude':
2973 + if (empty($claude_api_key)) {
2974 + throw new Exception(esc_html__('Claude API key is not configured', 'mxchat'));
7087 2975 }
7088 - $response = $this->mxchat_generate_response_gemini(
2976 + return $this->mxchat_generate_response_claude(
7089 2977 $selected_model,
7090 - $gemini_api_key,
2978 + $claude_api_key,
7091 2979 $conversation_history,
7092 2980 $relevant_content
7093 2981 );
7094 - break;
7095 -
7096 - case 'claude':
7097 - if (empty($claude_api_key)) {
7098 - $error_response = [
7099 - 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
7100 - 'error_code' => 'missing_claude_api_key'
7101 - ];
7102 - if ($testing_data !== null) {
7103 - $error_response['testing_data'] = $testing_data;
7104 - }
7105 - return $error_response;
7106 - }
7107 - if ($streaming) {
7108 - return $this->mxchat_generate_response_claude_stream(
7109 - $selected_model,
7110 - $claude_api_key,
7111 - $conversation_history,
7112 - $relevant_content,
7113 - $session_id,
7114 - $testing_data
7115 - );
7116 - } else {
7117 - $response = $this->mxchat_generate_response_claude(
7118 - $selected_model,
7119 - $claude_api_key,
7120 - $conversation_history,
7121 - $relevant_content
7122 - );
7123 - }
7124 - break;
7125 -
2982 +
7126 2983 case 'grok':
7127 2984 if (empty($xai_api_key)) {
7128 - $error_response = [
7129 - 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
7130 - 'error_code' => 'missing_xai_api_key'
7131 - ];
7132 - if ($testing_data !== null) {
7133 - $error_response['testing_data'] = $testing_data;
7134 - }
7135 - return $error_response;
2985 + throw new Exception(esc_html__('X.AI API key is not configured', 'mxchat'));
7136 2986 }
7137 - if ($streaming) {
7138 - return $this->mxchat_generate_response_xai_stream(
7139 - $selected_model,
7140 - $xai_api_key,
7141 - $conversation_history,
7142 - $relevant_content,
7143 - $session_id,
7144 - $testing_data
7145 - );
7146 - } else {
7147 - $response = $this->mxchat_generate_response_xai(
7148 - $selected_model,
7149 - $xai_api_key,
7150 - $conversation_history,
7151 - $relevant_content
7152 - );
7153 - }
7154 - break;
7155 -
2987 + return $this->mxchat_generate_response_xai(
2988 + $selected_model,
2989 + $xai_api_key,
2990 + $conversation_history,
2991 + $relevant_content
2992 + );
2993 +
7156 2994 case 'deepseek':
7157 2995 if (empty($deepseek_api_key)) {
7158 - $error_response = [
7159 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
7160 - 'error_code' => 'missing_deepseek_api_key'
7161 - ];
7162 - if ($testing_data !== null) {
7163 - $error_response['testing_data'] = $testing_data;
7164 - }
7165 - return $error_response;
2996 + throw new Exception(esc_html__('DeepSeek API key is not configured', 'mxchat'));
7166 2997 }
7167 - if ($streaming) {
7168 - return $this->mxchat_generate_response_deepseek_stream(
7169 - $selected_model,
7170 - $deepseek_api_key,
7171 - $conversation_history,
7172 - $relevant_content,
7173 - $session_id,
7174 - $testing_data
7175 - );
7176 - } else {
7177 - $response = $this->mxchat_generate_response_deepseek(
7178 - $selected_model,
7179 - $deepseek_api_key,
7180 - $conversation_history,
7181 - $relevant_content
7182 - );
7183 - }
7184 - break;
7185 -
7186 - case 'custom':
7187 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7188 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7189 - if (empty($cp_base_url)) {
7190 - $error_response = [
7191 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7192 - 'error_code' => 'missing_custom_provider_base_url'
7193 - ];
7194 - if ($testing_data !== null) {
7195 - $error_response['testing_data'] = $testing_data;
7196 - }
7197 - return $error_response;
7198 - }
7199 - if ($streaming) {
7200 - return $this->mxchat_generate_response_custom_stream(
7201 - $selected_model,
7202 - $conversation_history,
7203 - $relevant_content,
7204 - $session_id,
7205 - $testing_data
7206 - );
7207 - } else {
7208 - $response = $this->mxchat_generate_response_custom(
7209 - $selected_model,
7210 - $conversation_history,
7211 - $relevant_content
7212 - );
7213 - }
7214 - break;
2998 + return $this->mxchat_generate_response_deepseek(
2999 + $selected_model,
3000 + $deepseek_api_key,
3001 + $conversation_history,
3002 + $relevant_content
3003 + );
7215 3004
7216 3005 case 'gpt':
7217 - case 'o1':
7218 3006 if (empty($api_key)) {
7219 - $error_response = [
7220 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7221 - 'error_code' => 'missing_openai_api_key'
7222 - ];
7223 - if ($testing_data !== null) {
7224 - $error_response['testing_data'] = $testing_data;
7225 - }
7226 - return $error_response;
3007 + throw new Exception(esc_html__('OpenAI API key is not configured', 'mxchat'));
7227 3008 }
3009 + return $this->mxchat_generate_response_openai(
3010 + $selected_model,
3011 + $api_key,
3012 + $conversation_history,
3013 + $relevant_content
3014 + );
7228 3015
7229 - // Check if web search is enabled for this OpenAI model
7230 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7231 - // Models that don't support web search
7232 - $unsupported_web_search_models = array('gpt-4.1-nano');
7233 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7234 -
7235 - if ($web_search_enabled && $model_supports_web_search) {
7236 - // Use Responses API (required for some models, or when web search is enabled)
7237 - return $this->mxchat_generate_response_openai_web_search(
7238 - $selected_model,
7239 - $api_key,
7240 - $conversation_history,
7241 - $relevant_content,
7242 - $session_id,
7243 - $testing_data,
7244 - $streaming
7245 - );
7246 - } elseif ($streaming) {
7247 - return $this->mxchat_generate_response_openai_stream(
7248 - $selected_model,
7249 - $api_key,
7250 - $conversation_history,
7251 - $relevant_content,
7252 - $session_id,
7253 - $testing_data
7254 - );
7255 - } else {
7256 - $response = $this->mxchat_generate_response_openai(
7257 - $selected_model,
7258 - $api_key,
7259 - $conversation_history,
7260 - $relevant_content
7261 - );
7262 - }
7263 - break;
7264 -
7265 3016 default:
3017 + // Default to OpenAI for custom models or unrecognized prefixes
7266 3018 if (empty($api_key)) {
7267 - $error_response = [
7268 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7269 - 'error_code' => 'missing_openai_api_key'
7270 - ];
7271 - if ($testing_data !== null) {
7272 - $error_response['testing_data'] = $testing_data;
7273 - }
7274 - return $error_response;
3019 + throw new Exception(esc_html__('OpenAI API key is not configured', 'mxchat'));
7275 3020 }
7276 -
7277 - // Check if web search is enabled (default case also handles OpenAI models)
7278 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7279 - $unsupported_web_search_models = array('gpt-4.1-nano');
7280 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7281 -
7282 - if ($web_search_enabled && $model_supports_web_search) {
7283 - return $this->mxchat_generate_response_openai_web_search(
7284 - $selected_model,
7285 - $api_key,
7286 - $conversation_history,
7287 - $relevant_content,
7288 - $session_id,
7289 - $testing_data,
7290 - $streaming
7291 - );
7292 - } elseif ($streaming) {
7293 - return $this->mxchat_generate_response_openai_stream(
7294 - $selected_model,
7295 - $api_key,
7296 - $conversation_history,
7297 - $relevant_content,
7298 - $session_id,
7299 - $testing_data
7300 - );
7301 - } else {
7302 - $response = $this->mxchat_generate_response_openai(
7303 - $selected_model,
7304 - $api_key,
7305 - $conversation_history,
7306 - $relevant_content
7307 - );
7308 - }
7309 - break;
7310 - }
7311 -
7312 - if (is_array($response) && isset($response['error'])) {
7313 - if ($testing_data !== null) {
7314 - $response['testing_data'] = $testing_data;
7315 - }
7316 - return $response;
7317 - }
7318 -
7319 - return $response;
7320 -
7321 - } catch (Exception $e) {
7322 - $error_response = [
7323 - 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7324 - 'error_code' => 'system_exception',
7325 - 'exception_details' => $e->getMessage()
7326 - ];
7327 -
7328 - if ($testing_data !== null) {
7329 - $error_response['testing_data'] = $testing_data;
7330 - }
7331 -
7332 - return $error_response;
7333 - }
7334 -}
7335 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7336 - try {
7337 - $bot_id = $this->get_current_bot_id($session_id);
7338 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7339 -
7340 - if (!is_array($conversation_history)) {
7341 - $conversation_history = array();
7342 - }
7343 -
7344 - $formatted_conversation = array();
7345 -
7346 - $formatted_conversation[] = array(
7347 - 'role' => 'system',
7348 - 'content' => $system_prompt_instructions . " " . $relevant_content
7349 - );
7350 -
7351 - foreach ($conversation_history as $message) {
7352 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7353 - $role = $message['role'];
7354 - if ($role === 'bot' || $role === 'agent') {
7355 - $role = 'assistant';
7356 - }
7357 - if (!in_array($role, ['system', 'assistant', 'user'])) {
7358 - $role = 'user';
7359 - }
7360 - $formatted_conversation[] = array(
7361 - 'role' => $role,
7362 - 'content' => $message['content']
3021 + return $this->mxchat_generate_response_openai(
3022 + $selected_model,
3023 + $api_key,
3024 + $conversation_history,
3025 + $relevant_content
7363 3026 );
7364 - }
7365 3027 }
7366 -
7367 - if (headers_sent() || !function_exists('curl_init')) {
7368 - $regular_response = $this->mxchat_generate_response_openrouter(
7369 - $selected_model,
7370 - $openrouter_api_key,
7371 - $conversation_history,
7372 - $relevant_content
7373 - );
7374 -
7375 - // Save bot response to transcript
7376 - if (!empty($regular_response) && !empty($session_id)) {
7377 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7378 - }
7379 -
7380 - $response_data = [
7381 - 'text' => $regular_response,
7382 - 'html' => '',
7383 - 'session_id' => $session_id
7384 - ];
7385 -
7386 - if ($testing_data !== null) {
7387 - $response_data['testing_data'] = $testing_data;
7388 - }
7389 -
7390 - header('Content-Type: application/json');
7391 - echo json_encode($response_data);
7392 - return true;
7393 - }
7394 -
7395 - $body = json_encode([
7396 - 'model' => $selected_model,
7397 - 'messages' => $formatted_conversation,
7398 - 'temperature' => 1,
7399 - 'stream' => true
7400 - ]);
7401 -
7402 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
7403 - // inside WRITEFUNCTION on first byte of a successful upstream.
7404 -
7405 - $captured_status_code = 0;
7406 - $captured_body_pre_stream = '';
7407 - $full_response = '';
7408 - $stream_started = false;
7409 - $buffer = '';
7410 - $errno = 0;
7411 - $last_curl_error = '';
7412 - $http_code = 0;
7413 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7414 - $backoff_ms = array(0, 750, 2000);
7415 -
7416 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7417 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7418 - usleep($backoff_ms[$attempt] * 1000);
7419 - }
7420 -
7421 - $captured_status_code = 0;
7422 - $captured_body_pre_stream = '';
7423 - $full_response = '';
7424 - $stream_started = false;
7425 - $buffer = '';
7426 -
7427 - $ch = curl_init();
7428 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
7429 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7430 - curl_setopt($ch, CURLOPT_POST, true);
7431 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7432 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7433 - 'Content-Type: application/json',
7434 - 'Authorization: Bearer ' . $openrouter_api_key,
7435 - 'HTTP-Referer: ' . home_url(),
7436 - 'X-Title: ' . get_bloginfo('name')
7437 - ));
7438 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7439 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7440 -
7441 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7442 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7443 - $captured_status_code = (int) $m[1];
7444 - }
7445 - return strlen($header);
7446 - });
7447 -
7448 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7449 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7450 - $captured_body_pre_stream .= $data;
7451 - return strlen($data);
7452 - }
7453 -
7454 - if (!$this->streaming_headers_sent) {
7455 - $this->setup_streaming_headers();
7456 - }
7457 -
7458 - if (!$stream_started && $testing_data !== null) {
7459 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7460 - flush();
7461 - $stream_started = true;
7462 - }
7463 -
7464 - $buffer .= $data;
7465 - $lines = explode("\n", $buffer);
7466 - $buffer = array_pop($lines);
7467 -
7468 - foreach ($lines as $line) {
7469 - if (trim($line) === '') {
7470 - continue;
7471 - }
7472 - if (strpos($line, 'data: ') !== 0) {
7473 - continue;
7474 - }
7475 -
7476 - $json_str = substr($line, 6);
7477 -
7478 - if (trim($json_str) === '[DONE]') {
7479 - echo "data: [DONE]\n\n";
7480 - flush();
7481 - continue;
7482 - }
7483 -
7484 - $json = json_decode(trim($json_str), true);
7485 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7486 - $content = $json['choices'][0]['delta']['content'];
7487 - $full_response .= $content;
7488 -
7489 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7490 - flush();
7491 - }
7492 - }
7493 -
7494 - return strlen($data);
7495 - });
7496 -
7497 - $response = curl_exec($ch);
7498 - $errno = curl_errno($ch);
7499 - $last_curl_error = curl_error($ch);
7500 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7501 - curl_close($ch);
7502 -
7503 - if (!$errno && $http_code === 200) {
7504 - break;
7505 - }
7506 -
7507 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7508 - $can_retry = !$this->streaming_headers_sent
7509 - && ($attempt + 1) < $max_attempts
7510 - && $is_transient;
7511 -
7512 - if (defined('WP_DEBUG') && WP_DEBUG) {
7513 - error_log(sprintf(
7514 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7515 - $attempt + 1, $max_attempts, $http_code, $errno,
7516 - $is_transient ? 'yes' : 'no',
7517 - $can_retry ? 'Retrying.' : 'Giving up.'
7518 - ));
7519 - }
7520 -
7521 - if (!$can_retry) {
7522 - break;
7523 - }
7524 - }
7525 -
7526 - if (!$errno && $http_code === 200) {
7527 - if (!empty($full_response) && !empty($session_id)) {
7528 - $rag_context_for_storage = null;
7529 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7530 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7531 -
7532 - if ($has_rag_data || $has_action_data) {
7533 - $rag_context_for_storage = [];
7534 -
7535 - if ($has_rag_data) {
7536 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7537 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7538 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7539 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7540 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7541 - }
7542 -
7543 - if ($has_action_data) {
7544 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7545 - }
7546 - }
7547 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7548 - }
7549 - return true;
7550 - }
7551 -
7552 - return $this->mxchat_stream_emit_fallback(
7553 - 'openai',
7554 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7555 - $session_id,
7556 - $testing_data
7557 - );
7558 -
7559 3028 } catch (Exception $e) {
7560 - return $this->mxchat_stream_emit_fallback(
7561 - 'openai',
7562 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content),
7563 - $session_id,
7564 - $testing_data
3029 + //error_log('MXChat Error: ' . $e->getMessage());
3030 + return sprintf(
3031 + esc_html__('An error occurred: %s', 'mxchat'),
3032 + esc_html($e->getMessage())
7565 3033 );
7566 3034 }
7567 3035 }
7568 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7569 - try {
7570 - $bot_id = $this->get_current_bot_id($session_id);
7571 -
7572 - // Get system prompt instructions using centralized function
7573 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7574 -
7575 - // Ensure conversation_history is an array
7576 - if (!is_array($conversation_history)) {
7577 - $conversation_history = array();
7578 - }
7579 3036
7580 - // Format conversation history for OpenAI
7581 - $formatted_conversation = array();
7582 3037
7583 - $formatted_conversation[] = array(
7584 - 'role' => 'system',
7585 - 'content' => $system_prompt_instructions . " " . $relevant_content
7586 - );
3038 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
3039 + // Ensure conversation_history is an array
3040 + if (!is_array($conversation_history)) {
3041 + $conversation_history = array();
3042 + }
7587 3043
7588 - foreach ($conversation_history as $message) {
7589 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7590 - $role = $message['role'];
7591 - if ($role === 'bot' || $role === 'agent') {
7592 - $role = 'assistant';
7593 - }
7594 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
7595 - $role = 'user';
7596 - }
7597 - $formatted_conversation[] = array(
7598 - 'role' => $role,
7599 - 'content' => $message['content']
7600 - );
7601 - }
7602 - }
3044 + // Get system prompt instructions from options
3045 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7603 3046
7604 - // Check if we can actually stream
7605 - if (headers_sent() || !function_exists('curl_init')) {
7606 - // Fallback to regular response with testing data
7607 - $regular_response = $this->mxchat_generate_response_openai(
7608 - $selected_model,
7609 - $api_key,
7610 - $conversation_history,
7611 - $relevant_content
7612 - );
7613 -
7614 - // Save bot response to transcript
7615 - if (!empty($regular_response) && !empty($session_id)) {
7616 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7617 - }
7618 -
7619 - $response_data = [
7620 - 'text' => $regular_response,
7621 - 'html' => '',
7622 - 'session_id' => $session_id
7623 - ];
7624 -
7625 - if ($testing_data !== null) {
7626 - $response_data['testing_data'] = $testing_data;
7627 - }
7628 -
7629 - header('Content-Type: application/json');
7630 - echo json_encode($response_data);
7631 - return true;
7632 - }
3047 + // Create a new array for the formatted conversation
3048 + $formatted_conversation = array();
7633 3049
7634 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
7635 - $is_gpt5_model = (
7636 - strpos($selected_model, 'gpt-5') === 0 ||
7637 - $selected_model === 'gpt-5.2' ||
7638 - $selected_model === 'gpt-5.1-2025-11-13' ||
7639 - $selected_model === 'gpt-5' ||
7640 - $selected_model === 'gpt-5-mini' ||
7641 - $selected_model === 'gpt-5-nano'
7642 - );
3050 + // Add system message first
3051 + $formatted_conversation[] = array(
3052 + 'role' => 'system',
3053 + 'content' => $system_prompt_instructions . " " . $relevant_content
3054 + );
7643 3055
7644 - // Build request body with optimal settings for fast streaming
7645 - $request_body = [
7646 - 'model' => $selected_model,
7647 - 'messages' => $formatted_conversation,
7648 - 'temperature' => 1,
7649 - 'stream' => true
7650 - ];
3056 + // Add the rest of the conversation history
3057 + foreach ($conversation_history as $message) {
3058 + if (is_array($message) && isset($message['role']) && isset($message['content'])) {
3059 + $role = $message['role'];
7651 3060
7652 - // Add reasoning_effort only for GPT-5 models that support it
7653 - // These chat models don't support reasoning_effort parameter
7654 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
7655 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
7656 - // GPT-5.1 uses 'low' instead of 'minimal'
7657 - if ($selected_model === 'gpt-5.1-2025-11-13') {
7658 - $request_body['reasoning_effort'] = 'low';
7659 - } elseif ($selected_model === 'gpt-5.5') {
7660 - $request_body['reasoning_effort'] = 'none';
7661 - } elseif ($selected_model === 'gpt-5.4') {
7662 - $request_body['reasoning_effort'] = 'none';
7663 - } else {
7664 - $request_body['reasoning_effort'] = 'minimal';
3061 + // Convert roles to supported format
3062 + if ($role === 'bot' || $role === 'agent') {
3063 + $role = 'assistant';
7665 3064 }
7666 - }
7667 -
7668 - $body = json_encode($request_body);
7669 -
7670 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
7671 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
7672 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
7673 -
7674 - $captured_status_code = 0;
7675 - $captured_body_pre_stream = '';
7676 - $full_response = '';
7677 - $stream_started = false;
7678 - $buffer = '';
7679 - $errno = 0;
7680 - $last_curl_error = '';
7681 - $http_code = 0;
7682 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
7683 - $backoff_ms = array(0, 750, 2000);
7684 -
7685 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
7686 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
7687 - usleep($backoff_ms[$attempt] * 1000);
3065 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3066 + $role = 'user';
7688 3067 }
7689 3068
7690 - // Reset per-attempt capture state.
7691 - $captured_status_code = 0;
7692 - $captured_body_pre_stream = '';
7693 - $full_response = '';
7694 - $stream_started = false;
7695 - $buffer = '';
7696 -
7697 - $ch = curl_init();
7698 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
7699 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
7700 - curl_setopt($ch, CURLOPT_POST, true);
7701 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
7702 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
7703 - 'Content-Type: application/json',
7704 - 'Authorization: Bearer ' . $api_key
7705 - ));
7706 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
7707 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
7708 -
7709 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
7710 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
7711 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
7712 - $captured_status_code = (int) $m[1];
7713 - }
7714 - return strlen($header);
7715 - });
7716 -
7717 - // Buffer control for real-time streaming
7718 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
7719 - // V2 guard: if upstream returned non-200, buffer body for transient
7720 - // classification and DO NOT emit to client. Stream channel must NOT open.
7721 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
7722 - $captured_body_pre_stream .= $data;
7723 - return strlen($data);
7724 - }
7725 -
7726 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
7727 - // After this point streaming_headers_sent === true → retry is structurally blocked.
7728 - if (!$this->streaming_headers_sent) {
7729 - $this->setup_streaming_headers();
7730 - }
7731 -
7732 - // Send testing data as the first event if available
7733 - if (!$stream_started && $testing_data !== null) {
7734 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
7735 - flush();
7736 - $stream_started = true;
7737 - }
7738 -
7739 - // CRITICAL FIX: Append new data to buffer
7740 - $buffer .= $data;
7741 -
7742 - // Process complete lines only
7743 - $lines = explode("\n", $buffer);
7744 -
7745 - // CRITICAL FIX: Keep the last incomplete line in the buffer
7746 - $buffer = array_pop($lines);
7747 -
7748 - foreach ($lines as $line) {
7749 - if (trim($line) === '') {
7750 - continue;
7751 - }
7752 - if (strpos($line, 'data: ') !== 0) {
7753 - continue;
7754 - }
7755 -
7756 - $json_str = substr($line, 6);
7757 -
7758 - if (trim($json_str) === '[DONE]') {
7759 - echo "data: [DONE]\n\n";
7760 - flush();
7761 - continue;
7762 - }
7763 -
7764 - $json = json_decode(trim($json_str), true);
7765 - if ($json && isset($json['choices'][0]['delta']['content'])) {
7766 - $content = $json['choices'][0]['delta']['content'];
7767 - $full_response .= $content;
7768 -
7769 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
7770 - flush();
7771 - }
7772 - }
7773 -
7774 - return strlen($data);
7775 - });
7776 -
7777 - $response = curl_exec($ch);
7778 - $errno = curl_errno($ch);
7779 - $last_curl_error = curl_error($ch);
7780 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
7781 - curl_close($ch);
7782 -
7783 - if (!$errno && $http_code === 200) {
7784 - break; // Happy path — WRITEFUNCTION already streamed everything.
7785 - }
7786 -
7787 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
7788 - $can_retry = !$this->streaming_headers_sent
7789 - && ($attempt + 1) < $max_attempts
7790 - && $is_transient;
7791 -
7792 - if (defined('WP_DEBUG') && WP_DEBUG) {
7793 - error_log(sprintf(
7794 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
7795 - $attempt + 1, $max_attempts, $http_code, $errno,
7796 - $is_transient ? 'yes' : 'no',
7797 - $can_retry ? 'Retrying.' : 'Giving up.'
7798 - ));
7799 - }
7800 -
7801 - if (!$can_retry) {
7802 - break;
7803 - }
3069 + $formatted_conversation[] = array(
3070 + 'role' => $role,
3071 + 'content' => $message['content']
3072 + );
7804 3073 }
7805 -
7806 - // Post-loop branch.
7807 - if (!$errno && $http_code === 200) {
7808 - // Happy path — save the complete response to maintain chat persistence.
7809 - if (!empty($full_response) && !empty($session_id)) {
7810 - $rag_context_for_storage = null;
7811 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
7812 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
7813 -
7814 - if ($has_rag_data || $has_action_data) {
7815 - $rag_context_for_storage = [];
7816 -
7817 - if ($has_rag_data) {
7818 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
7819 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
7820 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
7821 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
7822 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
7823 - }
7824 -
7825 - if ($has_action_data) {
7826 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
7827 - }
7828 - }
7829 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
7830 - }
7831 -
7832 - return true;
7833 - }
7834 -
7835 - // Failure path — branch on whether SSE channel was opened.
7836 - return $this->mxchat_stream_emit_fallback(
7837 - 'openai',
7838 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7839 - $session_id,
7840 - $testing_data
7841 - );
7842 -
7843 - } catch (Exception $e) {
7844 - return $this->mxchat_stream_emit_fallback(
7845 - 'openai',
7846 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content),
7847 - $session_id,
7848 - $testing_data
7849 - );
7850 3074 }
7851 -}
7852 3075
7853 -/**
7854 - * Shared fallback emitter for streaming chat functions. Two outcomes:
7855 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
7856 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
7857 - * normal bot bubble. Transcript row is persisted.
7858 - * - streaming_headers_sent === false: SSE channel never opened (retries
7859 - * exhausted on initial connect). Emit a clean JSON response — the path
7860 - * the widget would normally hit if streaming wasn't even attempted.
7861 - *
7862 - * Used by all six *_stream functions after their per-attempt retry loop.
7863 - */
7864 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
7865 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
3076 + $body = json_encode([
3077 + 'model' => $selected_model,
3078 + 'messages' => $formatted_conversation,
3079 + 'temperature' => 0.8,
3080 + 'stream' => false
3081 + ]);
7866 3082
7867 - if ($this->streaming_headers_sent) {
7868 - if ($is_error_array) {
7869 - echo "data: " . json_encode([
7870 - 'error' => true,
7871 - 'error_message' => $regular_response['error'],
7872 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7873 - 'text' => $regular_response['error'],
7874 - 'message' => $regular_response['error']
7875 - ]) . "\n\n";
7876 - echo "data: [DONE]\n\n";
7877 - flush();
7878 - return true;
7879 - }
7880 - $fallback_message = (string) $regular_response;
7881 - if (!empty($fallback_message) && !empty($session_id)) {
7882 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
7883 - }
7884 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
7885 - echo "data: [DONE]\n\n";
7886 - flush();
7887 - return true;
7888 - }
3083 + $args = [
3084 + 'body' => $body,
3085 + 'headers' => [
3086 + 'Content-Type' => 'application/json',
3087 + 'Authorization' => 'Bearer ' . $deepseek_api_key,
3088 + ],
3089 + 'timeout' => 60,
3090 + 'redirection' => 5,
3091 + 'blocking' => true,
3092 + 'httpversion' => '1.0',
3093 + 'sslverify' => true,
3094 + ];
7889 3095
7890 - // SSE channel never opened — clean JSON fallback.
7891 - if ($is_error_array) {
7892 - header('Content-Type: application/json');
7893 - echo json_encode(array(
7894 - 'error' => true,
7895 - 'error_message' => $regular_response['error'],
7896 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
7897 - 'text' => $regular_response['error'],
7898 - 'message' => $regular_response['error'],
7899 - ));
7900 - return true;
7901 - }
3096 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
7902 3097
7903 - $fallback_message = (string) $regular_response;
7904 - if (!empty($fallback_message) && !empty($session_id)) {
7905 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
3098 + if (is_wp_error($response)) {
3099 + //error_log('DeepSeek API Error: ' . $response->get_error_message());
3100 + return "Sorry, there was an error processing your request.";
7906 3101 }
7907 - $response_data = array(
7908 - 'text' => $fallback_message,
7909 - 'html' => '',
7910 - 'session_id' => $session_id,
7911 - );
7912 - if ($testing_data !== null) {
7913 - $response_data['testing_data'] = $testing_data;
7914 - }
7915 - header('Content-Type: application/json');
7916 - echo json_encode($response_data);
7917 - return true;
7918 -}
7919 3102
7920 -/**
7921 - * Resolve custom (OpenAI-compatible) provider config from settings.
7922 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
7923 - */
7924 -private function mxchat_resolve_custom_provider() {
7925 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
7926 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
7927 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
7928 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
7929 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
3103 + $response_body = wp_remote_retrieve_body($response);
3104 + $decoded_response = json_decode($response_body, true);
7930 3105
7931 - $chat_url = $base_url . '/chat/completions';
7932 - if (!empty($api_version)) {
7933 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
3106 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3107 + return trim($decoded_response['choices'][0]['message']['content']);
3108 + } else {
3109 + //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
3110 + return "Sorry, I couldn't process that request.";
7934 3111 }
7935 -
7936 - $headers = array('Content-Type: application/json');
7937 - if (!empty($api_key)) {
7938 - if ($auth_scheme === 'api-key') {
7939 - $headers[] = 'api-key: ' . $api_key;
7940 - } else {
7941 - $headers[] = 'Authorization: Bearer ' . $api_key;
7942 - }
7943 - }
7944 -
7945 - return array(
7946 - 'base_url' => $base_url,
7947 - 'api_key' => $api_key,
7948 - 'model' => $model !== '' ? $model : 'default',
7949 - 'auth_scheme' => $auth_scheme,
7950 - 'api_version' => $api_version,
7951 - 'chat_url' => $chat_url,
7952 - 'headers' => $headers,
7953 - );
7954 3112 }
7955 -
7956 -/**
7957 - * Streaming chat completion against an OpenAI-compatible custom provider
7958 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
7959 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
7960 - */
7961 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7962 - try {
7963 - $cfg = $this->mxchat_resolve_custom_provider();
7964 - if (empty($cfg['base_url'])) {
7965 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
7966 - }
7967 -
7968 - $bot_id = $this->get_current_bot_id($session_id);
7969 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7970 - if (!is_array($conversation_history)) {
7971 - $conversation_history = array();
7972 - }
7973 -
7974 - $formatted_conversation = array();
7975 - $formatted_conversation[] = array(
7976 - 'role' => 'system',
7977 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
7978 - );
7979 - foreach ($conversation_history as $message) {
7980 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7981 - $role = $message['role'];
7982 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
7983 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
7984 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
7985 - }
7986 - }
7987 -
7988 - if (headers_sent() || !function_exists('curl_init')) {
7989 - // No streaming capability — fall through to non-stream wrapper
7990 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
7991 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
7992 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
7993 - }
7994 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
7995 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
7996 - header('Content-Type: application/json');
7997 - echo json_encode($response_data);
7998 - return true;
7999 - }
8000 -
8001 - $request_body = array(
8002 - 'model' => $cfg['model'],
8003 - 'messages' => $formatted_conversation,
8004 - 'stream' => true,
8005 - );
8006 - $body = json_encode($request_body);
8007 -
8008 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8009 -
8010 - $captured_status_code = 0;
8011 - $captured_body_pre_stream = '';
8012 - $full_response = '';
8013 - $stream_started = false;
8014 - $buffer = '';
8015 - $errno = 0;
8016 - $http_code = 0;
8017 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8018 - $backoff_ms = array(0, 750, 2000);
8019 -
8020 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8021 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8022 - usleep($backoff_ms[$attempt] * 1000);
8023 - }
8024 -
8025 - $captured_status_code = 0;
8026 - $captured_body_pre_stream = '';
8027 - $full_response = '';
8028 - $stream_started = false;
8029 - $buffer = '';
8030 -
8031 - $ch = curl_init();
8032 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8033 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8034 - curl_setopt($ch, CURLOPT_POST, true);
8035 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8036 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8037 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8038 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8039 -
8040 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8041 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8042 - $captured_status_code = (int) $m[1];
8043 - }
8044 - return strlen($header);
8045 - });
8046 -
8047 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8048 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8049 - $captured_body_pre_stream .= $data;
8050 - return strlen($data);
8051 - }
8052 -
8053 - if (!$this->streaming_headers_sent) {
8054 - $this->setup_streaming_headers();
8055 - }
8056 -
8057 - if (!$stream_started && $testing_data !== null) {
8058 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8059 - flush();
8060 - $stream_started = true;
8061 - }
8062 - $buffer .= $data;
8063 - $lines = explode("\n", $buffer);
8064 - $buffer = array_pop($lines);
8065 - foreach ($lines as $line) {
8066 - if (trim($line) === '') { continue; }
8067 - if (strpos($line, 'data: ') !== 0) { continue; }
8068 - $json_str = substr($line, 6);
8069 - if (trim($json_str) === '[DONE]') {
8070 - echo "data: [DONE]\n\n";
8071 - flush();
8072 - continue;
8073 - }
8074 - $json = json_decode(trim($json_str), true);
8075 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8076 - $content = $json['choices'][0]['delta']['content'];
8077 - $full_response .= $content;
8078 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8079 - flush();
8080 - }
8081 - }
8082 - return strlen($data);
8083 - });
8084 -
8085 - $response = curl_exec($ch);
8086 - $errno = curl_errno($ch);
8087 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8088 - curl_close($ch);
8089 -
8090 - if (!$errno && $http_code === 200) {
8091 - break;
8092 - }
8093 -
8094 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8095 - $can_retry = !$this->streaming_headers_sent
8096 - && ($attempt + 1) < $max_attempts
8097 - && $is_transient;
8098 -
8099 - if (defined('WP_DEBUG') && WP_DEBUG) {
8100 - error_log(sprintf(
8101 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8102 - $attempt + 1, $max_attempts, $http_code, $errno,
8103 - $is_transient ? 'yes' : 'no',
8104 - $can_retry ? 'Retrying.' : 'Giving up.'
8105 - ));
8106 - }
8107 -
8108 - if (!$can_retry) {
8109 - break;
8110 - }
8111 - }
8112 -
8113 - if (!$errno && $http_code === 200) {
8114 - if (!empty($full_response) && !empty($session_id)) {
8115 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8116 - }
8117 - return true;
8118 - }
8119 -
8120 - return $this->mxchat_stream_emit_fallback(
8121 - 'openai',
8122 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8123 - $session_id,
8124 - $testing_data
8125 - );
8126 -
8127 - } catch (Exception $e) {
8128 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
3113 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
3114 + // Ensure conversation_history is an array
3115 + if (!is_array($conversation_history)) {
3116 + $conversation_history = array();
8129 3117 }
8130 -}
8131 3118
8132 -/**
8133 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8134 - * Returns string content on success, array['error'=>...] on failure.
8135 - */
8136 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8137 - $cfg = $this->mxchat_resolve_custom_provider();
8138 - if (empty($cfg['base_url'])) {
8139 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8140 - }
3119 + // Get system prompt instructions from options
3120 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8141 3121
8142 - $bot_id = $this->get_current_bot_id(null);
8143 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8144 - if (!is_array($conversation_history)) {
8145 - $conversation_history = array();
8146 - }
3122 + // Create a new array for the formatted conversation
3123 + $formatted_conversation = array();
8147 3124
8148 - $messages = array(array(
3125 + // Add system message first
3126 + $formatted_conversation[] = array(
8149 3127 'role' => 'system',
8150 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8151 - ));
3128 + 'content' => $system_prompt_instructions . " " . $relevant_content
3129 + );
3130 +
3131 + // Add the rest of the conversation history
8152 3132 foreach ($conversation_history as $message) {
8153 3133 if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8154 3134 $role = $message['role'];
8155 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8156 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8157 - $messages[] = array('role' => $role, 'content' => $message['content']);
8158 - }
8159 - }
8160 3135
8161 - $headers_assoc = array('Content-Type' => 'application/json');
8162 - if (!empty($cfg['api_key'])) {
8163 - if ($cfg['auth_scheme'] === 'api-key') {
8164 - $headers_assoc['api-key'] = $cfg['api_key'];
8165 - } else {
8166 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8167 - }
8168 - }
8169 -
8170 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8171 - 'headers' => $headers_assoc,
8172 - 'body' => wp_json_encode(array(
8173 - 'model' => $cfg['model'],
8174 - 'messages' => $messages,
8175 - )),
8176 - 'timeout' => 120,
8177 - ), 'openai');
8178 -
8179 - if (is_wp_error($response)) {
8180 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8181 - }
8182 - $code = (int) wp_remote_retrieve_response_code($response);
8183 - if ($code < 200 || $code >= 300) {
8184 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8185 - }
8186 - $body = json_decode(wp_remote_retrieve_body($response), true);
8187 - if (isset($body['choices'][0]['message']['content'])) {
8188 - return (string) $body['choices'][0]['message']['content'];
8189 - }
8190 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8191 -}
8192 -
8193 -/**
8194 - * Generate response using OpenAI Responses API with web search tool
8195 - * This uses the newer Responses API which supports web search functionality
8196 - */
8197 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8198 - try {
8199 - $bot_id = $this->get_current_bot_id($session_id);
8200 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8201 -
8202 - if (!is_array($conversation_history)) {
8203 - $conversation_history = array();
8204 - }
8205 -
8206 - // Build the input for Responses API
8207 - // The Responses API uses a different format - we need to construct the input properly
8208 - $input_parts = [];
8209 -
8210 - // Add system instructions as context
8211 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8212 -
8213 - // Build conversation as input items for Responses API
8214 - foreach ($conversation_history as $message) {
8215 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8216 - $role = $message['role'];
8217 - if ($role === 'bot' || $role === 'agent') {
8218 - $role = 'assistant';
8219 - }
8220 - if (!in_array($role, ['assistant', 'user'])) {
8221 - $role = 'user';
8222 - }
8223 - $input_parts[] = [
8224 - 'type' => 'message',
8225 - 'role' => $role,
8226 - 'content' => $message['content']
8227 - ];
3136 + // Convert roles to supported format
3137 + if ($role === 'bot' || $role === 'agent') {
3138 + $role = 'assistant';
8228 3139 }
8229 - }
8230 -
8231 - // Build request body for Responses API
8232 - $request_body = [
8233 - 'model' => $selected_model,
8234 - 'input' => $input_parts,
8235 - 'instructions' => $system_context,
8236 - 'stream' => $streaming
8237 - ];
8238 -
8239 - // Only add web search tool if web search is enabled in settings
8240 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8241 - if ($web_search_enabled) {
8242 - $request_body['tools'] = [
8243 - ['type' => 'web_search']
8244 - ];
8245 - }
8246 -
8247 - // Add reasoning effort for supported models
8248 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8249 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8250 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8251 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8252 - $request_body['reasoning'] = ['effort' => 'low'];
8253 - } elseif ($selected_model === 'gpt-5.5') {
8254 - $request_body['reasoning'] = ['effort' => 'low'];
8255 - } elseif ($selected_model === 'gpt-5.4') {
8256 - $request_body['reasoning'] = ['effort' => 'low'];
3140 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3141 + $role = 'user';
8257 3142 }
8258 - }
8259 3143
8260 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8261 -
8262 - if ($streaming) {
8263 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8264 - } else {
8265 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
3144 + $formatted_conversation[] = array(
3145 + 'role' => $role,
3146 + 'content' => $message['content']
3147 + );
8266 3148 }
8267 -
8268 - } catch (Exception $e) {
8269 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8270 - return [
8271 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8272 - 'error_code' => 'web_search_exception'
8273 - ];
8274 3149 }
8275 -}
8276 3150
8277 -/**
8278 - * Handle non-streaming web search response
8279 - */
8280 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8281 - $request_body['stream'] = false;
3151 + $body = json_encode([
3152 + 'model' => $selected_model,
3153 + 'messages' => $formatted_conversation,
3154 + 'temperature' => 0.8,
3155 + 'stream' => false
3156 + ]);
8282 3157
8283 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8284 - 'headers' => array(
3158 + $args = [
3159 + 'body' => $body,
3160 + 'headers' => [
3161 + 'Content-Type' => 'application/json',
8285 3162 'Authorization' => 'Bearer ' . $api_key,
8286 - 'Content-Type' => 'application/json'
8287 - ),
8288 - 'body' => json_encode($request_body),
8289 - 'timeout' => 90
8290 - ), 'openai');
3163 + ],
3164 + 'timeout' => 60,
3165 + 'redirection' => 5,
3166 + 'blocking' => true,
3167 + 'httpversion' => '1.0',
3168 + 'sslverify' => true,
3169 + ];
8291 3170
3171 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
3172 +
8292 3173 if (is_wp_error($response)) {
8293 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8294 - return [
8295 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8296 - 'error_code' => 'web_search_connection_error'
8297 - ];
3174 + //error_log('OpenAI API Error: ' . $response->get_error_message());
3175 + return "Sorry, there was an error processing your request.";
8298 3176 }
8299 3177
8300 - $response_code = wp_remote_retrieve_response_code($response);
8301 3178 $response_body = wp_remote_retrieve_body($response);
3179 + $decoded_response = json_decode($response_body, true);
8302 3180
8303 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8304 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8305 -
8306 - if ($response_code !== 200) {
8307 - $error_data = json_decode($response_body, true);
8308 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8309 - return [
8310 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8311 - 'error_code' => 'web_search_api_error'
8312 - ];
3181 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3182 + return trim($decoded_response['choices'][0]['message']['content']);
3183 + } else {
3184 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
3185 + return "Sorry, I couldn't process that request.";
8313 3186 }
8314 -
8315 - $result = json_decode($response_body, true);
8316 -
8317 - if (json_last_error() !== JSON_ERROR_NONE) {
8318 - return [
8319 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8320 - 'error_code' => 'web_search_json_error'
8321 - ];
8322 - }
8323 -
8324 - // Extract the response text and citations from Responses API format
8325 - $output_text = '';
8326 - $citations = [];
8327 -
8328 - if (isset($result['output'])) {
8329 - foreach ($result['output'] as $output_item) {
8330 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8331 - foreach ($output_item['content'] as $content_item) {
8332 - if ($content_item['type'] === 'output_text') {
8333 - $output_text .= $content_item['text'];
8334 -
8335 - // Extract citations/annotations
8336 - if (isset($content_item['annotations'])) {
8337 - foreach ($content_item['annotations'] as $annotation) {
8338 - if ($annotation['type'] === 'url_citation') {
8339 - $citations[] = [
8340 - 'url' => $annotation['url'],
8341 - 'title' => $annotation['title'] ?? ''
8342 - ];
8343 - }
8344 - }
8345 - }
8346 - }
8347 - }
8348 - }
8349 - }
8350 - }
8351 -
8352 - // If we have citations, append them to the response
8353 - if (!empty($citations)) {
8354 - $output_text .= "\n\n**Sources:**\n";
8355 - $seen_urls = [];
8356 - foreach ($citations as $citation) {
8357 - if (!in_array($citation['url'], $seen_urls)) {
8358 - $seen_urls[] = $citation['url'];
8359 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8360 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8361 - }
8362 - }
8363 - }
8364 -
8365 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8366 - // which includes rag_context for the "sources" link in transcripts.
8367 -
8368 - return $output_text;
8369 3187 }
3188 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
3189 + // Get system prompt instructions from options
3190 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8370 3191
8371 -/**
8372 - * Handle streaming web search response using Responses API
8373 - */
8374 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8375 - $request_body['stream'] = true;
3192 + // Add system prompt to relevant content
3193 + $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
8376 3194
8377 - // Check if we can stream
8378 - if (headers_sent() || !function_exists('curl_init')) {
8379 - // Fallback to non-streaming
8380 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8381 - }
3195 + // Prepend system instructions to the conversation history
3196 + array_unshift($conversation_history, [
3197 + 'role' => 'system',
3198 + 'content' => "Here are your instructions: " . $content_with_instructions
3199 + ]);
8382 3200
8383 - // Setup streaming headers
8384 - $this->setup_streaming_headers();
8385 -
8386 - $ch = curl_init();
8387 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8388 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8389 - curl_setopt($ch, CURLOPT_POST, true);
8390 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8391 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8392 - 'Content-Type: application/json',
8393 - 'Authorization: Bearer ' . $api_key
8394 - ));
8395 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8396 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8397 -
8398 - $full_response = '';
8399 - $stream_started = false;
8400 - $buffer = '';
8401 - $citations = [];
8402 -
8403 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
8404 - // Send testing data as first event if available
8405 - if (!$stream_started && $testing_data !== null) {
8406 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8407 - flush();
8408 - $stream_started = true;
8409 - }
8410 -
8411 - $buffer .= $data;
8412 - $lines = explode("\n", $buffer);
8413 - $buffer = array_pop($lines);
8414 -
8415 - foreach ($lines as $line) {
8416 - if (trim($line) === '') continue;
8417 - if (strpos($line, 'data: ') !== 0) continue;
8418 -
8419 - $json_str = substr($line, 6);
8420 -
8421 - if (trim($json_str) === '[DONE]') {
8422 - // Append citations if we have any
8423 - if (!empty($citations)) {
8424 - $citation_text = "\n\n**Sources:**\n";
8425 - $seen_urls = [];
8426 - foreach ($citations as $citation) {
8427 - if (!in_array($citation['url'], $seen_urls)) {
8428 - $seen_urls[] = $citation['url'];
8429 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8430 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8431 - }
8432 - }
8433 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
8434 - $full_response .= $citation_text;
8435 - flush();
8436 - }
8437 - echo "data: [DONE]\n\n";
8438 - flush();
8439 - continue;
3201 + // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
3202 + foreach ($conversation_history as &$message) {
3203 + if ($message['role'] === 'bot') {
3204 + $message['role'] = 'assistant';
3205 + } elseif ($message['role'] === 'agent') {
3206 + // Tag the message as coming from a live agent
3207 + $message['role'] = 'assistant';
3208 + if (!isset($message['metadata'])) {
3209 + $message['metadata'] = ['source' => 'live_agent'];
8440 3210 }
8441 -
8442 - $json = json_decode(trim($json_str), true);
8443 - if (!$json) continue;
8444 -
8445 - // Handle Responses API streaming events
8446 - // The format is different from Chat Completions
8447 - if (isset($json['type'])) {
8448 - switch ($json['type']) {
8449 - case 'response.output_text.delta':
8450 - // Text content delta
8451 - if (isset($json['delta'])) {
8452 - $content = $json['delta'];
8453 - $full_response .= $content;
8454 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8455 - flush();
8456 - }
8457 - break;
8458 -
8459 - case 'response.output_item.done':
8460 - // Check for citations in completed items
8461 - if (isset($json['item']['content'])) {
8462 - foreach ($json['item']['content'] as $content_item) {
8463 - if (isset($content_item['annotations'])) {
8464 - foreach ($content_item['annotations'] as $annotation) {
8465 - if ($annotation['type'] === 'url_citation') {
8466 - $citations[] = [
8467 - 'url' => $annotation['url'],
8468 - 'title' => $annotation['title'] ?? ''
8469 - ];
8470 - }
8471 - }
8472 - }
8473 - }
8474 - }
8475 - break;
8476 - }
8477 - }
8478 3211 }
8479 3212
8480 - return strlen($data);
8481 - });
8482 -
8483 - $response = curl_exec($ch);
8484 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8485 -
8486 - if (curl_errno($ch) || $http_code !== 200) {
8487 - $curl_error = curl_error($ch);
8488 - curl_close($ch);
8489 -
8490 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
8491 -
8492 - return $this->mxchat_stream_emit_fallback(
8493 - 'web_search',
8494 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
8495 - $session_id,
8496 - $testing_data
8497 - );
8498 - }
8499 -
8500 - curl_close($ch);
8501 -
8502 - // Save the complete response with RAG context so the "sources" link
8503 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
8504 - if (!empty($full_response) && !empty($session_id)) {
8505 - $rag_context_for_storage = null;
8506 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8507 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8508 -
8509 - if ($has_rag_data || $has_action_data) {
8510 - $rag_context_for_storage = [];
8511 -
8512 - if ($has_rag_data) {
8513 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8514 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8515 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8516 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8517 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8518 - }
8519 -
8520 - if ($has_action_data) {
8521 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8522 - }
3213 + // Ensure all roles are valid
3214 + if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
3215 + $message['role'] = 'user'; // Default to 'user'
8523 3216 }
8524 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8525 3217 }
8526 3218
8527 - return true;
8528 -}
8529 3219
8530 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8531 - try {
8532 - // Get bot ID from session or request
8533 - $bot_id = $this->get_current_bot_id($session_id);
8534 -
8535 - // Get system prompt instructions using centralized function
8536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8537 - // Ensure conversation_history is an array
8538 - if (!is_array($conversation_history)) {
8539 - $conversation_history = array();
8540 - }
3220 + // Build the request body
3221 + $body = json_encode([
3222 + 'model' => $selected_model,
3223 + 'messages' => $conversation_history,
3224 + 'temperature' => 0.8,
3225 + 'stream' => false
3226 + ]);
8541 3227
8542 - // Clean and validate conversation history
8543 - foreach ($conversation_history as &$message) {
8544 - // Convert bot and agent roles to assistant
8545 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
8546 - $message['role'] = 'assistant';
8547 - }
8548 -
8549 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
8550 - if (!in_array($message['role'], ['assistant', 'user'])) {
8551 - $message['role'] = 'user';
8552 - }
3228 + // Set up the API request
3229 + $args = [
3230 + 'body' => $body,
3231 + 'headers' => [
3232 + 'Content-Type' => 'application/json',
3233 + 'Authorization' => 'Bearer ' . $xai_api_key,
3234 + ],
3235 + 'timeout' => 60,
3236 + 'redirection' => 5,
3237 + 'blocking' => true,
3238 + 'httpversion' => '1.0',
3239 + 'sslverify' => true,
3240 + ];
8553 3241
8554 - // Ensure content field exists
8555 - if (!isset($message['content']) || empty($message['content'])) {
8556 - $message['content'] = '';
8557 - }
3242 + // Make the API request
3243 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
8558 3244
8559 - // Remove any unsupported fields
8560 - $message = array_intersect_key($message, array_flip(['role', 'content']));
8561 - }
8562 -
8563 - // Add relevant content as the latest user message
8564 - $conversation_history[] = [
8565 - 'role' => 'user',
8566 - 'content' => $relevant_content
8567 - ];
8568 -
8569 - // Prepare the request body with stream: true
8570 - $payload = [
8571 - 'model' => $selected_model,
8572 - 'messages' => $conversation_history,
8573 - 'max_tokens' => 1000,
8574 - 'temperature' => 0.8,
8575 - 'system' => $system_prompt_instructions,
8576 - 'stream' => true
8577 - ];
8578 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
8579 - $body = json_encode($payload);
8580 -
8581 - // Check if we can actually stream (headers not sent, etc.)
8582 - if (headers_sent() || !function_exists('curl_init')) {
8583 - // Fallback to regular response with testing data
8584 - //error_log("MxChat: Streaming not possible, falling back to regular response");
8585 - $regular_response = $this->mxchat_generate_response_claude(
8586 - $selected_model,
8587 - $claude_api_key,
8588 - array_slice($conversation_history, 0, -1), // Remove the added content
8589 - $relevant_content
8590 - );
8591 -
8592 - // Save bot response to transcript
8593 - if (!empty($regular_response) && !empty($session_id)) {
8594 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8595 - }
8596 -
8597 - // Return as JSON with testing data
8598 - $response_data = [
8599 - 'text' => $regular_response,
8600 - 'html' => '',
8601 - 'session_id' => $session_id
8602 - ];
8603 -
8604 - if ($testing_data !== null) {
8605 - $response_data['testing_data'] = $testing_data;
8606 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
8607 - }
8608 -
8609 - // Clear any streaming headers and send JSON
8610 - if (headers_sent() === false) {
8611 - header('Content-Type: application/json');
8612 - }
8613 - echo json_encode($response_data);
8614 - return true; // Indicate we handled the response
8615 - }
8616 -
8617 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8618 -
8619 - $captured_status_code = 0;
8620 - $captured_body_pre_stream = '';
8621 - $full_response = '';
8622 - $stream_started = false;
8623 - $buffer = '';
8624 - $errno = 0;
8625 - $http_code = 0;
8626 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8627 - $backoff_ms = array(0, 750, 2000);
8628 -
8629 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8630 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8631 - usleep($backoff_ms[$attempt] * 1000);
8632 - }
8633 -
8634 - $captured_status_code = 0;
8635 - $captured_body_pre_stream = '';
8636 - $full_response = '';
8637 - $stream_started = false;
8638 - $buffer = '';
8639 -
8640 - $ch = curl_init();
8641 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
8642 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8643 - curl_setopt($ch, CURLOPT_POST, true);
8644 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8645 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8646 - 'Content-Type: application/json',
8647 - 'x-api-key: ' . $claude_api_key,
8648 - 'anthropic-version: 2023-06-01'
8649 - ));
8650 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8651 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8652 -
8653 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8654 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8655 - $captured_status_code = (int) $m[1];
8656 - }
8657 - return strlen($header);
8658 - });
8659 -
8660 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8661 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8662 - $captured_body_pre_stream .= $data;
8663 - return strlen($data);
8664 - }
8665 -
8666 - if (!$this->streaming_headers_sent) {
8667 - $this->setup_streaming_headers();
8668 - }
8669 -
8670 - if (!$stream_started && $testing_data !== null) {
8671 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8672 - flush();
8673 - $stream_started = true;
8674 - }
8675 -
8676 - $buffer .= $data;
8677 - $lines = explode("\n", $buffer);
8678 - $buffer = array_pop($lines);
8679 -
8680 - foreach ($lines as $line) {
8681 - if (trim($line) === '') {
8682 - continue;
8683 - }
8684 -
8685 - if (strpos($line, 'event: ') === 0) {
8686 - continue;
8687 - }
8688 -
8689 - if (strpos($line, 'data: ') === 0) {
8690 - $json_str = substr($line, 6);
8691 -
8692 - $json = json_decode(trim($json_str), true);
8693 - if (json_last_error() !== JSON_ERROR_NONE) {
8694 - continue;
8695 - }
8696 -
8697 - if (isset($json['type'])) {
8698 - switch ($json['type']) {
8699 - case 'content_block_delta':
8700 - if (isset($json['delta']['text'])) {
8701 - $content = $json['delta']['text'];
8702 - $full_response .= $content;
8703 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8704 - flush();
8705 - }
8706 - break;
8707 -
8708 - case 'message_stop':
8709 - echo "data: [DONE]\n\n";
8710 - flush();
8711 - break;
8712 -
8713 - case 'error':
8714 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
8715 - flush();
8716 - break;
8717 - }
8718 - }
8719 - }
8720 - }
8721 -
8722 - return strlen($data);
8723 - });
8724 -
8725 - $response = curl_exec($ch);
8726 - $errno = curl_errno($ch);
8727 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8728 - curl_close($ch);
8729 -
8730 - if (!$errno && $http_code === 200) {
8731 - break;
8732 - }
8733 -
8734 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
8735 - $can_retry = !$this->streaming_headers_sent
8736 - && ($attempt + 1) < $max_attempts
8737 - && $is_transient;
8738 -
8739 - if (defined('WP_DEBUG') && WP_DEBUG) {
8740 - error_log(sprintf(
8741 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8742 - $attempt + 1, $max_attempts, $http_code, $errno,
8743 - $is_transient ? 'yes' : 'no',
8744 - $can_retry ? 'Retrying.' : 'Giving up.'
8745 - ));
8746 - }
8747 -
8748 - if (!$can_retry) {
8749 - break;
8750 - }
8751 - }
8752 -
8753 - if ($errno || $http_code !== 200) {
8754 - return $this->mxchat_stream_emit_fallback(
8755 - 'anthropic',
8756 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content),
8757 - $session_id,
8758 - $testing_data
8759 - );
8760 - }
8761 -
8762 - // Save the complete response to maintain chat persistence
8763 - if (!empty($full_response) && !empty($session_id)) {
8764 - // Prepare RAG context for streaming response
8765 - $rag_context_for_storage = null;
8766 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8767 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8768 -
8769 - if ($has_rag_data || $has_action_data) {
8770 - $rag_context_for_storage = [];
8771 -
8772 - if ($has_rag_data) {
8773 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8774 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8775 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8776 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8777 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8778 - }
8779 -
8780 - if ($has_action_data) {
8781 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8782 - }
8783 - }
8784 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8785 - }
8786 -
8787 - return true; // Indicate streaming completed successfully
8788 -
8789 - } catch (Exception $e) {
8790 - return $this->mxchat_stream_emit_fallback(
8791 - 'anthropic',
8792 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content),
8793 - $session_id,
8794 - $testing_data
8795 - );
3245 + // Process the response
3246 + if (is_wp_error($response)) {
3247 + return "Sorry, there was an error processing your request.";
8796 3248 }
8797 -}
8798 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8799 - try {
8800 - // Get bot ID from session or request
8801 - $bot_id = $this->get_current_bot_id($session_id);
8802 -
8803 - // Get system prompt instructions using centralized function
8804 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8805 -
8806 - // Ensure conversation_history is an array
8807 - if (!is_array($conversation_history)) {
8808 - $conversation_history = array();
8809 - }
8810 3249
8811 - // Format conversation history for X.AI (same as OpenAI format)
8812 - $formatted_conversation = array();
3250 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
8813 3251
8814 - $formatted_conversation[] = array(
8815 - 'role' => 'system',
8816 - 'content' => $system_prompt_instructions . " " . $relevant_content
8817 - );
8818 -
8819 - foreach ($conversation_history as $message) {
8820 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8821 - $role = $message['role'];
8822 - if ($role === 'bot' || $role === 'agent') {
8823 - $role = 'assistant';
8824 - }
8825 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8826 - $role = 'user';
8827 - }
8828 - $formatted_conversation[] = array(
8829 - 'role' => $role,
8830 - 'content' => $message['content']
8831 - );
8832 - }
8833 - }
8834 -
8835 - // Check if we can actually stream
8836 - if (headers_sent() || !function_exists('curl_init')) {
8837 - // Fallback to regular response with testing data
8838 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
8839 - $regular_response = $this->mxchat_generate_response_xai(
8840 - $selected_model,
8841 - $xai_api_key,
8842 - $conversation_history,
8843 - $relevant_content
8844 - );
8845 -
8846 - // Save bot response to transcript
8847 - if (!empty($regular_response) && !empty($session_id)) {
8848 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8849 - }
8850 -
8851 - $response_data = [
8852 - 'text' => $regular_response,
8853 - 'html' => '',
8854 - 'session_id' => $session_id
8855 - ];
8856 -
8857 - if ($testing_data !== null) {
8858 - $response_data['testing_data'] = $testing_data;
8859 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
8860 - }
8861 -
8862 - header('Content-Type: application/json');
8863 - echo json_encode($response_data);
8864 - return true;
8865 - }
8866 -
8867 - // Prepare the request body with stream: true
8868 - $body = json_encode([
8869 - 'model' => $selected_model,
8870 - 'messages' => $formatted_conversation,
8871 - 'temperature' => 0.8,
8872 - 'stream' => true
8873 - ]);
8874 -
8875 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8876 -
8877 - $captured_status_code = 0;
8878 - $captured_body_pre_stream = '';
8879 - $full_response = '';
8880 - $stream_started = false;
8881 - $buffer = '';
8882 - $errno = 0;
8883 - $http_code = 0;
8884 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8885 - $backoff_ms = array(0, 750, 2000);
8886 -
8887 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8888 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8889 - usleep($backoff_ms[$attempt] * 1000);
8890 - }
8891 -
8892 - $captured_status_code = 0;
8893 - $captured_body_pre_stream = '';
8894 - $full_response = '';
8895 - $stream_started = false;
8896 - $buffer = '';
8897 -
8898 - $ch = curl_init();
8899 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
8900 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8901 - curl_setopt($ch, CURLOPT_POST, true);
8902 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8903 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8904 - 'Content-Type: application/json',
8905 - 'Authorization: Bearer ' . $xai_api_key
8906 - ));
8907 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8908 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8909 -
8910 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8911 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8912 - $captured_status_code = (int) $m[1];
8913 - }
8914 - return strlen($header);
8915 - });
8916 -
8917 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8918 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8919 - $captured_body_pre_stream .= $data;
8920 - return strlen($data);
8921 - }
8922 -
8923 - if (!$this->streaming_headers_sent) {
8924 - $this->setup_streaming_headers();
8925 - }
8926 -
8927 - if (!$stream_started && $testing_data !== null) {
8928 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8929 - flush();
8930 - $stream_started = true;
8931 - }
8932 -
8933 - $buffer .= $data;
8934 - $lines = explode("\n", $buffer);
8935 - $buffer = array_pop($lines);
8936 -
8937 - foreach ($lines as $line) {
8938 - if (trim($line) === '') {
8939 - continue;
8940 - }
8941 - if (strpos($line, 'data: ') !== 0) {
8942 - continue;
8943 - }
8944 -
8945 - $json_str = substr($line, 6);
8946 -
8947 - if (trim($json_str) === '[DONE]') {
8948 - echo "data: [DONE]\n\n";
8949 - flush();
8950 - continue;
8951 - }
8952 -
8953 - $json = json_decode(trim($json_str), true);
8954 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8955 - $content = $json['choices'][0]['delta']['content'];
8956 - $full_response .= $content;
8957 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8958 - flush();
8959 - }
8960 - }
8961 -
8962 - return strlen($data);
8963 - });
8964 -
8965 - $response = curl_exec($ch);
8966 - $errno = curl_errno($ch);
8967 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8968 - curl_close($ch);
8969 -
8970 - if (!$errno && $http_code === 200) {
8971 - break;
8972 - }
8973 -
8974 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
8975 - $can_retry = !$this->streaming_headers_sent
8976 - && ($attempt + 1) < $max_attempts
8977 - && $is_transient;
8978 -
8979 - if (defined('WP_DEBUG') && WP_DEBUG) {
8980 - error_log(sprintf(
8981 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8982 - $attempt + 1, $max_attempts, $http_code, $errno,
8983 - $is_transient ? 'yes' : 'no',
8984 - $can_retry ? 'Retrying.' : 'Giving up.'
8985 - ));
8986 - }
8987 -
8988 - if (!$can_retry) {
8989 - break;
8990 - }
8991 - }
8992 -
8993 - if ($errno || $http_code !== 200) {
8994 - return $this->mxchat_stream_emit_fallback(
8995 - 'xai',
8996 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
8997 - $session_id,
8998 - $testing_data
8999 - );
9000 - }
9001 -
9002 - // Save the complete response to maintain chat persistence
9003 - if (!empty($full_response) && !empty($session_id)) {
9004 - // Prepare RAG context for streaming response
9005 - $rag_context_for_storage = null;
9006 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9007 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9008 -
9009 - if ($has_rag_data || $has_action_data) {
9010 - $rag_context_for_storage = [];
9011 -
9012 - if ($has_rag_data) {
9013 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9014 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9015 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9016 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9017 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9018 - }
9019 -
9020 - if ($has_action_data) {
9021 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9022 - }
9023 - }
9024 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9025 - }
9026 -
9027 - return true; // Indicate streaming completed successfully
9028 -
9029 - } catch (Exception $e) {
9030 - return $this->mxchat_stream_emit_fallback(
9031 - 'xai',
9032 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9033 - $session_id,
9034 - $testing_data
9035 - );
3252 + if (isset($response_body['choices'][0]['message']['content'])) {
3253 + return trim($response_body['choices'][0]['message']['content']);
3254 + } else {
3255 + return "Sorry, I couldn't process that request.";
9036 3256 }
9037 3257 }
9038 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9039 - try {
9040 - // Get bot ID from session or request
9041 - $bot_id = $this->get_current_bot_id($session_id);
9042 -
9043 - // Get system prompt instructions using centralized function
9044 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9045 -
9046 - // Ensure conversation_history is an array
9047 - if (!is_array($conversation_history)) {
9048 - $conversation_history = array();
9049 - }
3258 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
3259 + // Get system prompt instructions from options
3260 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
9050 3261
9051 - // Format conversation history for DeepSeek
9052 - $formatted_conversation = array();
9053 -
9054 - $formatted_conversation[] = array(
9055 - 'role' => 'system',
9056 - 'content' => $system_prompt_instructions . " " . $relevant_content
9057 - );
9058 -
9059 - foreach ($conversation_history as $message) {
9060 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9061 - $role = $message['role'];
9062 - if ($role === 'bot' || $role === 'agent') {
9063 - $role = 'assistant';
9064 - }
9065 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9066 - $role = 'user';
9067 - }
9068 - $formatted_conversation[] = array(
9069 - 'role' => $role,
9070 - 'content' => $message['content']
9071 - );
9072 - }
9073 - }
9074 -
9075 - // Check if we can actually stream
9076 - if (headers_sent() || !function_exists('curl_init')) {
9077 - // Fallback to regular response with testing data
9078 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9079 - $regular_response = $this->mxchat_generate_response_deepseek(
9080 - $selected_model,
9081 - $deepseek_api_key,
9082 - $conversation_history,
9083 - $relevant_content
9084 - );
9085 -
9086 - // Save bot response to transcript
9087 - if (!empty($regular_response) && !empty($session_id)) {
9088 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9089 - }
9090 -
9091 - $response_data = [
9092 - 'text' => $regular_response,
9093 - 'html' => '',
9094 - 'session_id' => $session_id
9095 - ];
9096 -
9097 - if ($testing_data !== null) {
9098 - $response_data['testing_data'] = $testing_data;
9099 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
9100 - }
9101 -
9102 - header('Content-Type: application/json');
9103 - echo json_encode($response_data);
9104 - return true;
9105 - }
9106 -
9107 - // Prepare the request body with stream: true
9108 - $body = json_encode([
9109 - 'model' => $selected_model,
9110 - 'messages' => $formatted_conversation,
9111 - 'temperature' => 0.8,
9112 - 'stream' => true
9113 - ]);
9114 -
9115 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9116 -
9117 - $captured_status_code = 0;
9118 - $captured_body_pre_stream = '';
9119 - $full_response = '';
9120 - $stream_started = false;
9121 - $buffer = '';
9122 - $errno = 0;
9123 - $http_code = 0;
9124 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9125 - $backoff_ms = array(0, 750, 2000);
9126 -
9127 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9128 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9129 - usleep($backoff_ms[$attempt] * 1000);
9130 - }
9131 -
9132 - $captured_status_code = 0;
9133 - $captured_body_pre_stream = '';
9134 - $full_response = '';
9135 - $stream_started = false;
9136 - $buffer = '';
9137 -
9138 - $ch = curl_init();
9139 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9140 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9141 - curl_setopt($ch, CURLOPT_POST, true);
9142 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9143 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9144 - 'Content-Type: application/json',
9145 - 'Authorization: Bearer ' . $deepseek_api_key
9146 - ));
9147 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9148 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9149 -
9150 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9151 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9152 - $captured_status_code = (int) $m[1];
9153 - }
9154 - return strlen($header);
9155 - });
9156 -
9157 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9158 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9159 - $captured_body_pre_stream .= $data;
9160 - return strlen($data);
9161 - }
9162 -
9163 - if (!$this->streaming_headers_sent) {
9164 - $this->setup_streaming_headers();
9165 - }
9166 -
9167 - if (!$stream_started && $testing_data !== null) {
9168 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9169 - flush();
9170 - $stream_started = true;
9171 - }
9172 -
9173 - $buffer .= $data;
9174 - $lines = explode("\n", $buffer);
9175 - $buffer = array_pop($lines);
9176 -
9177 - foreach ($lines as $line) {
9178 - if (trim($line) === '') {
9179 - continue;
9180 - }
9181 - if (strpos($line, 'data: ') !== 0) {
9182 - continue;
9183 - }
9184 -
9185 - $json_str = substr($line, 6);
9186 -
9187 - if (trim($json_str) === '[DONE]') {
9188 - echo "data: [DONE]\n\n";
9189 - flush();
9190 - continue;
9191 - }
9192 -
9193 - $json = json_decode(trim($json_str), true);
9194 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9195 - $content = $json['choices'][0]['delta']['content'];
9196 - $full_response .= $content;
9197 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9198 - flush();
9199 - }
9200 - }
9201 -
9202 - return strlen($data);
9203 - });
9204 -
9205 - $response = curl_exec($ch);
9206 - $errno = curl_errno($ch);
9207 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9208 - curl_close($ch);
9209 -
9210 - if (!$errno && $http_code === 200) {
9211 - break;
9212 - }
9213 -
9214 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9215 - $can_retry = !$this->streaming_headers_sent
9216 - && ($attempt + 1) < $max_attempts
9217 - && $is_transient;
9218 -
9219 - if (defined('WP_DEBUG') && WP_DEBUG) {
9220 - error_log(sprintf(
9221 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9222 - $attempt + 1, $max_attempts, $http_code, $errno,
9223 - $is_transient ? 'yes' : 'no',
9224 - $can_retry ? 'Retrying.' : 'Giving up.'
9225 - ));
9226 - }
9227 -
9228 - if (!$can_retry) {
9229 - break;
9230 - }
9231 - }
9232 -
9233 - if ($errno || $http_code !== 200) {
9234 - return $this->mxchat_stream_emit_fallback(
9235 - 'openai',
9236 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9237 - $session_id,
9238 - $testing_data
9239 - );
9240 - }
9241 -
9242 - // Save the complete response to maintain chat persistence
9243 - if (!empty($full_response) && !empty($session_id)) {
9244 - // Prepare RAG context for streaming response
9245 - $rag_context_for_storage = null;
9246 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9247 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9248 -
9249 - if ($has_rag_data || $has_action_data) {
9250 - $rag_context_for_storage = [];
9251 -
9252 - if ($has_rag_data) {
9253 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9254 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9255 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9256 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9257 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9258 - }
9259 -
9260 - if ($has_action_data) {
9261 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9262 - }
9263 - }
9264 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9265 - }
9266 -
9267 - return true; // Indicate streaming completed successfully
9268 -
9269 - } catch (Exception $e) {
9270 - return $this->mxchat_stream_emit_fallback(
9271 - 'openai',
9272 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9273 - $session_id,
9274 - $testing_data
9275 - );
9276 - }
9277 -}
9278 -
9279 -
9280 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content) {
9281 - try {
9282 - if (!is_array($conversation_history)) {
9283 - $conversation_history = array();
9284 - }
9285 -
9286 - $bot_id = $this->get_current_bot_id('');
9287 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9288 -
9289 - $formatted_conversation = array();
9290 -
9291 - $formatted_conversation[] = array(
9292 - 'role' => 'system',
9293 - 'content' => $system_prompt_instructions . " " . $relevant_content
9294 - );
9295 -
9296 - foreach ($conversation_history as $message) {
9297 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9298 - $role = $message['role'];
9299 -
9300 - if ($role === 'bot' || $role === 'agent') {
9301 - $role = 'assistant';
9302 - }
9303 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9304 - $role = 'user';
9305 - }
9306 -
9307 - $formatted_conversation[] = array(
9308 - 'role' => $role,
9309 - 'content' => $message['content']
9310 - );
9311 - }
9312 - }
9313 -
9314 - $body = json_encode([
9315 - 'model' => $selected_model,
9316 - 'messages' => $formatted_conversation,
9317 - 'temperature' => 1,
9318 - ]);
9319 -
9320 - $args = [
9321 - 'body' => $body,
9322 - 'headers' => [
9323 - 'Content-Type' => 'application/json',
9324 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9325 - 'HTTP-Referer' => home_url(),
9326 - 'X-Title' => get_bloginfo('name'),
9327 - ],
9328 - 'timeout' => 60,
9329 - 'redirection' => 5,
9330 - 'blocking' => true,
9331 - 'httpversion' => '1.0',
9332 - 'sslverify' => true,
9333 - ];
9334 -
9335 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9336 -
9337 - if (is_wp_error($response)) {
9338 - $error_message = $response->get_error_message();
9339 - return [
9340 - 'error' => esc_html__('Connection error when contacting OpenRouter: ', 'mxchat') . esc_html($error_message),
9341 - 'error_code' => 'openrouter_connection_error',
9342 - 'provider' => 'openrouter'
9343 - ];
9344 - }
9345 -
9346 - $status_code = wp_remote_retrieve_response_code($response);
9347 - if ($status_code !== 200) {
9348 - $response_body = wp_remote_retrieve_body($response);
9349 - $decoded_response = json_decode($response_body, true);
9350 -
9351 - $error_message = isset($decoded_response['error']['message'])
9352 - ? $decoded_response['error']['message']
9353 - : 'HTTP Error ' . $status_code;
9354 -
9355 - return [
9356 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9357 - 'error_code' => 'openrouter_api_error',
9358 - 'provider' => 'openrouter',
9359 - 'status_code' => $status_code
9360 - ];
9361 - }
9362 -
9363 - $response_body = wp_remote_retrieve_body($response);
9364 - $decoded_response = json_decode($response_body, true);
9365 -
9366 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9367 - return trim($decoded_response['choices'][0]['message']['content']);
9368 - } else {
9369 - return [
9370 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9371 - 'error_code' => 'openrouter_response_format_error',
9372 - 'provider' => 'openrouter'
9373 - ];
9374 - }
9375 - } catch (Exception $e) {
9376 - return [
9377 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9378 - 'error_code' => 'openrouter_exception',
9379 - 'provider' => 'openrouter'
9380 - ];
9381 - }
9382 -}
9383 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
9384 -
9385 - // Get bot ID from session or request
9386 - $bot_id = $this->get_current_bot_id($session_id);
9387 -
9388 - // Get system prompt instructions using centralized function
9389 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9390 -
9391 3262 // Clean and validate conversation history
9392 3263 foreach ($conversation_history as &$message) {
9393 3264 // Convert bot and agent roles to assistant
9394 3265 if ($message['role'] === 'bot' || $message['role'] === 'agent') {
@@ -9415,17 +3286,15 @@
9415 3286 'content' => $relevant_content
9416 3287 ];
9417 3288
9418 3289 // Build request body
9419 - $payload = [
3290 + $body = json_encode([
9420 3291 'model' => $selected_model,
9421 3292 'max_tokens' => 1000,
9422 3293 'temperature' => 0.8,
9423 3294 'messages' => $conversation_history,
9424 3295 'system' => $system_prompt_instructions
9425 - ];
9426 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9427 - $body = json_encode($payload);
3296 + ]);
9428 3297
9429 3298 // Set up API request
9430 3299 $args = [
9431 3300 'body' => $body,
@@ -9441,9 +3310,9 @@
9441 3310 'sslverify' => true,
9442 3311 ];
9443 3312
9444 3313 // Make API request
9445 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
3314 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
9446 3315
9447 3316 // Check for WordPress errors
9448 3317 if (is_wp_error($response)) {
9449 3318 //error_log("Claude API request error: " . $response->get_error_message());
@@ -9473,17 +3342,14 @@
9473 3342 //error_log("Claude API JSON decode error: " . json_last_error_msg());
9474 3343 return "Sorry, there was an error processing the API response.";
9475 3344 }
9476 3345
9477 - // Extract and validate response content. claude-fable-5 prepends a
9478 - // thinking block to content even with no thinking param — take the first
9479 - // TEXT block rather than content[0].
9480 - if (isset($response_body['content']) && is_array($response_body['content'])) {
9481 - foreach ($response_body['content'] as $block) {
9482 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
9483 - return trim($block['text']);
9484 - }
9485 - }
3346 + // Extract and validate response content
3347 + if (isset($response_body['content']) &&
3348 + is_array($response_body['content']) &&
3349 + !empty($response_body['content']) &&
3350 + isset($response_body['content'][0]['text'])) {
3351 + return trim($response_body['content'][0]['text']);
9486 3352 }
9487 3353
9488 3354 // Log unexpected response format
9489 3355 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
@@ -9488,828 +3354,11 @@
9488 3354 // Log unexpected response format
9489 3355 //error_log("Claude API unexpected response format: " . print_r($response_body, true));
9490 3356 return "Sorry, I received an unexpected response format from the API.";
9491 3357 }
9492 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
9493 - try {
9494 - // Ensure conversation_history is an array
9495 - if (!is_array($conversation_history)) {
9496 - $conversation_history = array();
9497 - }
9498 3358
9499 - // Get bot ID from session or request
9500 - $bot_id = $this->get_current_bot_id('');
9501 -
9502 - // Get system prompt instructions using centralized function
9503 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9504 -
9505 - // Create a new array for the formatted conversation
9506 - $formatted_conversation = array();
9507 3359
9508 - // Add system message first
9509 - $formatted_conversation[] = array(
9510 - 'role' => 'system',
9511 - 'content' => $system_prompt_instructions . " " . $relevant_content
9512 - );
9513 3360
9514 - // Add the rest of the conversation history
9515 - foreach ($conversation_history as $message) {
9516 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9517 - $role = $message['role'];
9518 -
9519 - // Convert roles to supported format
9520 - if ($role === 'bot' || $role === 'agent') {
9521 - $role = 'assistant';
9522 - }
9523 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9524 - $role = 'user';
9525 - }
9526 -
9527 - $formatted_conversation[] = array(
9528 - 'role' => $role,
9529 - 'content' => $message['content']
9530 - );
9531 - }
9532 - }
9533 -
9534 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
9535 - $is_gpt5_model = (
9536 - strpos($selected_model, 'gpt-5') === 0 ||
9537 - $selected_model === 'gpt-5.2' ||
9538 - $selected_model === 'gpt-5.1-2025-11-13' ||
9539 - $selected_model === 'gpt-5' ||
9540 - $selected_model === 'gpt-5-mini' ||
9541 - $selected_model === 'gpt-5-nano'
9542 - );
9543 -
9544 - // Build request body with optimal settings for fast responses
9545 - $request_body = [
9546 - 'model' => $selected_model,
9547 - 'messages' => $formatted_conversation,
9548 - 'temperature' => 1,
9549 - 'stream' => false
9550 - ];
9551 -
9552 - // Add reasoning_effort only for GPT-5 models that support it
9553 - // These chat models don't support reasoning_effort parameter
9554 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
9555 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
9556 - // GPT-5.1 uses 'low' instead of 'minimal'
9557 - if ($selected_model === 'gpt-5.1-2025-11-13') {
9558 - $request_body['reasoning_effort'] = 'low';
9559 - } elseif ($selected_model === 'gpt-5.5') {
9560 - $request_body['reasoning_effort'] = 'none';
9561 - } elseif ($selected_model === 'gpt-5.4') {
9562 - $request_body['reasoning_effort'] = 'none';
9563 - } else {
9564 - $request_body['reasoning_effort'] = 'minimal';
9565 - }
9566 - }
9567 -
9568 - $body = json_encode($request_body);
9569 -
9570 - $args = [
9571 - 'body' => $body,
9572 - 'headers' => [
9573 - 'Content-Type' => 'application/json',
9574 - 'Authorization' => 'Bearer ' . $api_key,
9575 - ],
9576 - 'timeout' => 60,
9577 - 'redirection' => 5,
9578 - 'blocking' => true,
9579 - 'httpversion' => '1.0',
9580 - 'sslverify' => true,
9581 - ];
9582 -
9583 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
9584 -
9585 - if (is_wp_error($response)) {
9586 - $error_message = $response->get_error_message();
9587 - return [
9588 - 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
9589 - 'error_code' => 'openai_connection_error',
9590 - 'provider' => 'openai'
9591 - ];
9592 - }
9593 -
9594 - $status_code = wp_remote_retrieve_response_code($response);
9595 - if ($status_code !== 200) {
9596 - $response_body = wp_remote_retrieve_body($response);
9597 - $decoded_response = json_decode($response_body, true);
9598 -
9599 - $error_message = isset($decoded_response['error']['message'])
9600 - ? $decoded_response['error']['message']
9601 - : 'HTTP Error ' . $status_code;
9602 -
9603 - $error_type = isset($decoded_response['error']['type'])
9604 - ? $decoded_response['error']['type']
9605 - : 'unknown';
9606 -
9607 - // Handle specific error types
9608 - switch ($error_type) {
9609 - case 'invalid_request_error':
9610 - if (strpos($error_message, 'API key') !== false) {
9611 - return [
9612 - 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
9613 - 'error_code' => 'openai_invalid_api_key',
9614 - 'provider' => 'openai'
9615 - ];
9616 - }
9617 - break;
9618 -
9619 - case 'authentication_error':
9620 - return [
9621 - 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
9622 - 'error_code' => 'openai_auth_error',
9623 - 'provider' => 'openai'
9624 - ];
9625 -
9626 - case 'rate_limit_exceeded':
9627 - return [
9628 - 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
9629 - 'error_code' => 'openai_rate_limit',
9630 - 'provider' => 'openai'
9631 - ];
9632 -
9633 - case 'quota_exceeded':
9634 - return [
9635 - 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
9636 - 'error_code' => 'openai_quota_exceeded',
9637 - 'provider' => 'openai'
9638 - ];
9639 - }
9640 -
9641 - // Generic error fallback
9642 - return [
9643 - 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
9644 - 'error_code' => 'openai_api_error',
9645 - 'provider' => 'openai',
9646 - 'status_code' => $status_code
9647 - ];
9648 - }
9649 -
9650 - $response_body = wp_remote_retrieve_body($response);
9651 - $decoded_response = json_decode($response_body, true);
9652 -
9653 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9654 - return trim($decoded_response['choices'][0]['message']['content']);
9655 - } else {
9656 - return [
9657 - 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
9658 - 'error_code' => 'openai_response_format_error',
9659 - 'provider' => 'openai'
9660 - ];
9661 - }
9662 - } catch (Exception $e) {
9663 - return [
9664 - 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
9665 - 'error_code' => 'openai_exception',
9666 - 'provider' => 'openai'
9667 - ];
9668 - }
9669 -}
9670 -
9671 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
9672 - try {
9673 - // Get bot ID from session or request
9674 - $bot_id = $this->get_current_bot_id($session_id);
9675 -
9676 - // Get system prompt instructions using centralized function
9677 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9678 -
9679 - // Add system prompt to relevant content
9680 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9681 -
9682 - // Prepend system instructions to the conversation history
9683 - array_unshift($conversation_history, [
9684 - 'role' => 'system',
9685 - 'content' => "Here are your instructions: " . $content_with_instructions
9686 - ]);
9687 -
9688 - // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
9689 - foreach ($conversation_history as &$message) {
9690 - if ($message['role'] === 'bot') {
9691 - $message['role'] = 'assistant';
9692 - } elseif ($message['role'] === 'agent') {
9693 - // Tag the message as coming from a live agent
9694 - $message['role'] = 'assistant';
9695 - if (!isset($message['metadata'])) {
9696 - $message['metadata'] = ['source' => 'live_agent'];
9697 - }
9698 - }
9699 -
9700 - // Ensure all roles are valid
9701 - if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
9702 - $message['role'] = 'user'; // Default to 'user'
9703 - }
9704 - }
9705 -
9706 - // Build the request body
9707 - $body = json_encode([
9708 - 'model' => $selected_model,
9709 - 'messages' => $conversation_history,
9710 - 'temperature' => 0.8,
9711 - 'stream' => false
9712 - ]);
9713 -
9714 - // Set up the API request
9715 - $args = [
9716 - 'body' => $body,
9717 - 'headers' => [
9718 - 'Content-Type' => 'application/json',
9719 - 'Authorization' => 'Bearer ' . $xai_api_key,
9720 - ],
9721 - 'timeout' => 60,
9722 - 'redirection' => 5,
9723 - 'blocking' => true,
9724 - 'httpversion' => '1.0',
9725 - 'sslverify' => true,
9726 - ];
9727 -
9728 - // Make the API request
9729 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
9730 -
9731 - // Process the response
9732 - if (is_wp_error($response)) {
9733 - $error_message = $response->get_error_message();
9734 - //error_log('X.AI API Error: ' . $error_message);
9735 - return [
9736 - 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
9737 - 'error_code' => 'xai_connection_error',
9738 - 'provider' => 'xai'
9739 - ];
9740 - }
9741 -
9742 - $status_code = wp_remote_retrieve_response_code($response);
9743 - if ($status_code !== 200) {
9744 - $response_body = wp_remote_retrieve_body($response);
9745 - $decoded_response = json_decode($response_body, true);
9746 -
9747 - // Log the full response for debugging
9748 - //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
9749 -
9750 - // Extract error message from X.AI's specific format
9751 - $error_message = '';
9752 -
9753 - // Check for direct error string (as seen in your logs)
9754 - if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
9755 - $error_message = $decoded_response['error'];
9756 - }
9757 - // Check for nested error object (OpenAI style)
9758 - elseif (isset($decoded_response['error']['message'])) {
9759 - $error_message = $decoded_response['error']['message'];
9760 - }
9761 - // Check for top-level message
9762 - elseif (isset($decoded_response['message'])) {
9763 - $error_message = $decoded_response['message'];
9764 - }
9765 - // Fallback
9766 - else {
9767 - $error_message = 'HTTP Error ' . $status_code;
9768 - }
9769 -
9770 - //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
9771 -
9772 - // Check for API key errors using string matching
9773 - if (stripos($error_message, 'api key') !== false ||
9774 - stripos($error_message, 'incorrect api key') !== false ||
9775 - stripos($error_message, 'invalid api key') !== false) {
9776 - return [
9777 - 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
9778 - 'error_code' => 'xai_invalid_api_key',
9779 - 'provider' => 'xai'
9780 - ];
9781 - }
9782 -
9783 - // Authentication errors
9784 - if ($status_code === 401 || $status_code === 403 ||
9785 - stripos($error_message, 'auth') !== false) {
9786 - return [
9787 - 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
9788 - 'error_code' => 'xai_auth_error',
9789 - 'provider' => 'xai'
9790 - ];
9791 - }
9792 -
9793 - // Model errors
9794 - if (stripos($error_message, 'model') !== false) {
9795 - return [
9796 - 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
9797 - 'error_code' => 'xai_invalid_model',
9798 - 'provider' => 'xai'
9799 - ];
9800 - }
9801 -
9802 - // Rate limit errors
9803 - if ($status_code === 429 ||
9804 - stripos($error_message, 'rate') !== false ||
9805 - stripos($error_message, 'limit') !== false) {
9806 - return [
9807 - 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
9808 - 'error_code' => 'xai_rate_limit',
9809 - 'provider' => 'xai'
9810 - ];
9811 - }
9812 -
9813 - // Quota errors
9814 - if (stripos($error_message, 'quota') !== false ||
9815 - stripos($error_message, 'billing') !== false) {
9816 - return [
9817 - 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
9818 - 'error_code' => 'xai_quota_exceeded',
9819 - 'provider' => 'xai'
9820 - ];
9821 - }
9822 -
9823 - // Server errors
9824 - if ($status_code >= 500) {
9825 - return [
9826 - 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
9827 - 'error_code' => 'xai_service_unavailable',
9828 - 'provider' => 'xai'
9829 - ];
9830 - }
9831 -
9832 - // Generic error fallback with the actual error message
9833 - return [
9834 - 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
9835 - 'error_code' => 'xai_api_error',
9836 - 'provider' => 'xai',
9837 - 'status_code' => $status_code
9838 - ];
9839 - }
9840 -
9841 - $response_body = wp_remote_retrieve_body($response);
9842 - $decoded_response = json_decode($response_body, true);
9843 -
9844 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9845 - return trim($decoded_response['choices'][0]['message']['content']);
9846 - } else {
9847 - //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
9848 - return [
9849 - 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
9850 - 'error_code' => 'xai_response_format_error',
9851 - 'provider' => 'xai'
9852 - ];
9853 - }
9854 -} catch (Exception $e) {
9855 - //error_log('X.AI Exception: ' . $e->getMessage());
9856 - return [
9857 - 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
9858 - 'error_code' => 'xai_exception',
9859 - 'provider' => 'xai'
9860 - ];
9861 -}
9862 -
9863 -
9864 -}
9865 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
9866 - try {
9867 - // Ensure conversation_history is an array
9868 - if (!is_array($conversation_history)) {
9869 - $conversation_history = array();
9870 - }
9871 -
9872 - // Get bot ID from session or request
9873 - $bot_id = $this->get_current_bot_id($session_id);
9874 -
9875 - // Get system prompt instructions using centralized function
9876 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9877 -
9878 - // Create a new array for the formatted conversation
9879 - $formatted_conversation = array();
9880 -
9881 - // Add system message first
9882 - $formatted_conversation[] = array(
9883 - 'role' => 'system',
9884 - 'content' => $system_prompt_instructions . " " . $relevant_content
9885 - );
9886 -
9887 - // Add the rest of the conversation history
9888 - foreach ($conversation_history as $message) {
9889 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9890 - $role = $message['role'];
9891 -
9892 - // Convert roles to supported format
9893 - if ($role === 'bot' || $role === 'agent') {
9894 - $role = 'assistant';
9895 - }
9896 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9897 - $role = 'user';
9898 - }
9899 -
9900 - $formatted_conversation[] = array(
9901 - 'role' => $role,
9902 - 'content' => $message['content']
9903 - );
9904 - }
9905 - }
9906 -
9907 - $body = json_encode([
9908 - 'model' => $selected_model,
9909 - 'messages' => $formatted_conversation,
9910 - 'temperature' => 0.8,
9911 - 'stream' => false
9912 - ]);
9913 -
9914 - $args = [
9915 - 'body' => $body,
9916 - 'headers' => [
9917 - 'Content-Type' => 'application/json',
9918 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
9919 - ],
9920 - 'timeout' => 60,
9921 - 'redirection' => 5,
9922 - 'blocking' => true,
9923 - 'httpversion' => '1.0',
9924 - 'sslverify' => true,
9925 - ];
9926 -
9927 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
9928 -
9929 - if (is_wp_error($response)) {
9930 - $error_message = $response->get_error_message();
9931 - //error_log('DeepSeek API Error: ' . $error_message);
9932 - return [
9933 - 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
9934 - 'error_code' => 'deepseek_connection_error',
9935 - 'provider' => 'deepseek'
9936 - ];
9937 - }
9938 -
9939 - $status_code = wp_remote_retrieve_response_code($response);
9940 - if ($status_code !== 200) {
9941 - $response_body = wp_remote_retrieve_body($response);
9942 - $decoded_response = json_decode($response_body, true);
9943 -
9944 - $error_message = isset($decoded_response['error']['message'])
9945 - ? $decoded_response['error']['message']
9946 - : 'HTTP Error ' . $status_code;
9947 -
9948 - $error_type = isset($decoded_response['error']['type'])
9949 - ? $decoded_response['error']['type']
9950 - : 'unknown';
9951 -
9952 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
9953 -
9954 - // Handle specific error types
9955 - switch ($status_code) {
9956 - case 401:
9957 - return [
9958 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
9959 - 'error_code' => 'deepseek_auth_error',
9960 - 'provider' => 'deepseek'
9961 - ];
9962 -
9963 - case 400:
9964 - if (strpos($error_message, 'API key') !== false) {
9965 - return [
9966 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
9967 - 'error_code' => 'deepseek_invalid_api_key',
9968 - 'provider' => 'deepseek'
9969 - ];
9970 - }
9971 - break;
9972 -
9973 - case 429:
9974 - if (strpos($error_message, 'quota') !== false) {
9975 - return [
9976 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
9977 - 'error_code' => 'deepseek_quota_exceeded',
9978 - 'provider' => 'deepseek'
9979 - ];
9980 - } else {
9981 - return [
9982 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
9983 - 'error_code' => 'deepseek_rate_limit',
9984 - 'provider' => 'deepseek'
9985 - ];
9986 - }
9987 -
9988 - case 500:
9989 - case 502:
9990 - case 503:
9991 - case 504:
9992 - return [
9993 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
9994 - 'error_code' => 'deepseek_service_unavailable',
9995 - 'provider' => 'deepseek'
9996 - ];
9997 - }
9998 -
9999 - // Generic error fallback
10000 - return [
10001 - 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
10002 - 'error_code' => 'deepseek_api_error',
10003 - 'provider' => 'deepseek',
10004 - 'status_code' => $status_code
10005 - ];
10006 - }
10007 -
10008 - $response_body = wp_remote_retrieve_body($response);
10009 - $decoded_response = json_decode($response_body, true);
10010 -
10011 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10012 - return trim($decoded_response['choices'][0]['message']['content']);
10013 - } else {
10014 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10015 - return [
10016 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
10017 - 'error_code' => 'deepseek_response_format_error',
10018 - 'provider' => 'deepseek'
10019 - ];
10020 - }
10021 - } catch (Exception $e) {
10022 - //error_log('DeepSeek Exception: ' . $e->getMessage());
10023 - return [
10024 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
10025 - 'error_code' => 'deepseek_exception',
10026 - 'provider' => 'deepseek'
10027 - ];
10028 - }
10029 -}
10030 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
10031 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10032 - // Auto-rescue existing installs whose saved model is the dead ID.
10033 - if ($selected_model === 'gemini-3-pro-preview') {
10034 - $selected_model = 'gemini-3.1-pro-preview';
10035 - }
10036 - // Get bot ID from session or request
10037 - $bot_id = $this->get_current_bot_id($session_id);
10038 -
10039 - // Get system prompt instructions using centralized function
10040 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10041 -
10042 - // Add system prompt to relevant content
10043 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10044 -
10045 - // Format messages for Gemini API
10046 - $formatted_messages = [];
10047 -
10048 - // Add system message as the first user message with role prefix
10049 - // Note: Gemini doesn't have a dedicated system role, so we use a prefixed user message
10050 - $formatted_messages[] = [
10051 - 'role' => 'user',
10052 - 'parts' => [
10053 - ['text' => "[System Instructions] " . $content_with_instructions]
10054 - ]
10055 - ];
10056 -
10057 - // Add model response to acknowledge system instructions
10058 - $formatted_messages[] = [
10059 - 'role' => 'model',
10060 - 'parts' => [
10061 - ['text' => "I understand and will follow these instructions."]
10062 - ]
10063 - ];
10064 -
10065 - // Process the rest of the conversation history
10066 - $current_role = null;
10067 - $current_parts = [];
10068 -
10069 - foreach ($conversation_history as $message) {
10070 - // Skip the first system message as we already handled it
10071 - if ($message['role'] === 'system') {
10072 - continue;
10073 - }
10074 -
10075 - // Map roles to Gemini format
10076 - $gemini_role = '';
10077 - if ($message['role'] === 'user') {
10078 - $gemini_role = 'user';
10079 - } else if (in_array($message['role'], ['assistant', 'bot', 'agent'])) {
10080 - $gemini_role = 'model';
10081 - } else {
10082 - // Skip unsupported roles
10083 - continue;
10084 - }
10085 -
10086 - // If we have a new role, add the previous message
10087 - if ($current_role !== null && $current_role !== $gemini_role && !empty($current_parts)) {
10088 - $formatted_messages[] = [
10089 - 'role' => $current_role,
10090 - 'parts' => $current_parts
10091 - ];
10092 - $current_parts = [];
10093 - }
10094 -
10095 - // Set current role and add text to parts
10096 - $current_role = $gemini_role;
10097 - $current_parts[] = ['text' => $message['content']];
10098 - }
10099 -
10100 - // Add the last message if there's content
10101 - if ($current_role !== null && !empty($current_parts)) {
10102 - $formatted_messages[] = [
10103 - 'role' => $current_role,
10104 - 'parts' => $current_parts
10105 - ];
10106 - }
10107 -
10108 - // Build the request body
10109 - $body = json_encode([
10110 - 'contents' => $formatted_messages,
10111 - 'generationConfig' => [
10112 - 'temperature' => 0.7,
10113 - 'topP' => 0.95,
10114 - 'topK' => 40,
10115 - 'maxOutputTokens' => 8192,
10116 - ],
10117 - 'safetySettings' => [
10118 - [
10119 - 'category' => 'HARM_CATEGORY_HARASSMENT',
10120 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10121 - ],
10122 - [
10123 - 'category' => 'HARM_CATEGORY_HATE_SPEECH',
10124 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10125 - ],
10126 - [
10127 - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
10128 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10129 - ],
10130 - [
10131 - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
10132 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10133 - ]
10134 - ]
10135 - ]);
10136 -
10137 - // Prepare the API endpoint
10138 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10139 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10140 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10141 -
10142 - // Set up the API request
10143 - $args = [
10144 - 'body' => $body,
10145 - 'headers' => [
10146 - 'Content-Type' => 'application/json',
10147 - ],
10148 - 'timeout' => 60,
10149 - 'redirection' => 5,
10150 - 'blocking' => true,
10151 - 'httpversion' => '1.0',
10152 - 'sslverify' => true,
10153 - ];
10154 -
10155 - // Make the API request
10156 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10157 -
10158 - // Process the response
10159 - if (is_wp_error($response)) {
10160 - return "Sorry, there was an error processing your request: " . $response->get_error_message();
10161 - }
10162 -
10163 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
10164 -
10165 - // Handle potential errors in the response
10166 - if (isset($response_body['error'])) {
10167 - //error_log('Gemini API Error: ' . json_encode($response_body['error']));
10168 - return "Sorry, there was an error with the Gemini API: " .
10169 - (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
10170 - }
10171 -
10172 - // Extract the response text
10173 - if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
10174 - return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
10175 - } else {
10176 - //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
10177 - return "Sorry, I couldn't process that request. The response format was unexpected.";
10178 - }
10179 -}
10180 -
10181 -
10182 -public function test_streaming_request() {
10183 - $options = get_option('mxchat_options', []);
10184 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
10185 -
10186 - // Detect provider from model prefix
10187 - $provider = strtolower(explode('-', $model)[0]);
10188 -
10189 - $sample_prompt = 'Hello! Can you stream this response back to me?';
10190 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
10191 - $headers = [];
10192 - $body = [];
10193 - $url = '';
10194 - $api_key = '';
10195 -
10196 - switch ($provider) {
10197 - case 'gpt':
10198 - case 'o1':
10199 - $api_key = $options['api_key'] ?? '';
10200 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
10201 - $url = 'https://api.openai.com/v1/chat/completions';
10202 - $headers = [
10203 - 'Content-Type: application/json',
10204 - 'Authorization: Bearer ' . $api_key
10205 - ];
10206 - $body = [
10207 - 'model' => $model,
10208 - 'messages' => $messages,
10209 - 'stream' => true
10210 - ];
10211 - break;
10212 -
10213 - case 'claude':
10214 - $api_key = $options['claude_api_key'] ?? '';
10215 - if (empty($api_key)) return '❌ Missing API key for Claude';
10216 - $url = 'https://api.anthropic.com/v1/messages';
10217 - $headers = [
10218 - 'Content-Type: application/json',
10219 - 'x-api-key: ' . $api_key,
10220 - 'anthropic-version: 2023-06-01'
10221 - ];
10222 - $body = [
10223 - 'model' => $model,
10224 - 'messages' => $messages,
10225 - 'max_tokens' => 100,
10226 - 'stream' => true
10227 - ];
10228 - break;
10229 -
10230 - case 'grok':
10231 - $api_key = $options['xai_api_key'] ?? '';
10232 - if (empty($api_key)) return '❌ Missing API key for X.AI';
10233 - $url = 'https://api.x.ai/v1/chat/completions';
10234 - $headers = [
10235 - 'Content-Type: application/json',
10236 - 'Authorization: Bearer ' . $api_key
10237 - ];
10238 - $body = [
10239 - 'model' => $model,
10240 - 'messages' => $messages,
10241 - 'stream' => true
10242 - ];
10243 - break;
10244 -
10245 - case 'deepseek':
10246 - if (empty($deepseek_api_key)) {
10247 - $error_response = [
10248 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
10249 - 'error_code' => 'missing_deepseek_api_key'
10250 - ];
10251 - if ($testing_data !== null) {
10252 - $error_response['testing_data'] = $testing_data;
10253 - }
10254 - return $error_response;
10255 - }
10256 - if ($streaming) {
10257 - return $this->mxchat_generate_response_deepseek_stream(
10258 - $selected_model,
10259 - $deepseek_api_key,
10260 - $conversation_history,
10261 - $relevant_content,
10262 - $session_id,
10263 - $testing_data // Pass testing data
10264 - );
10265 - } else {
10266 - $response = $this->mxchat_generate_response_deepseek(
10267 - $selected_model,
10268 - $deepseek_api_key,
10269 - $conversation_history,
10270 - $relevant_content
10271 - );
10272 - }
10273 - break;
10274 -
10275 - case 'gemini':
10276 - $api_key = $options['gemini_api_key'] ?? '';
10277 - if (empty($api_key)) return '❌ Missing API key for Gemini';
10278 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
10279 - $headers = ['Content-Type: application/json'];
10280 - $body = [
10281 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
10282 - 'generationConfig' => ['temperature' => 0.7]
10283 - ];
10284 - break;
10285 -
10286 - default:
10287 - return '❌ Unsupported provider: ' . $provider;
10288 - }
10289 -
10290 - // Do the actual streaming test
10291 - $ch = curl_init($url);
10292 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
10293 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
10294 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10295 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
10296 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
10297 -
10298 - $response = curl_exec($ch);
10299 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
10300 - $error = curl_error($ch);
10301 - curl_close($ch);
10302 -
10303 - if ($error) return "❌ cURL error: $error";
10304 - if ($http_code !== 200) {
10305 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
10306 - return "❌ HTTP $http_code: $error_message";
10307 - }
10308 -
10309 - return true;
10310 -}
10311 -
10312 3361 public function mxchat_dismiss_pre_chat_message() {
10313 3362 // Get and sanitize the user identifier
10314 3363 $user_id = $this->mxchat_get_user_identifier();
10315 3364 $user_id = sanitize_key($user_id);
@@ -10363,63 +3412,40 @@
10363 3412
10364 3413 return $dotProduct / ($normA * $normB);
10365 3414 }
10366 3415
3416 +public function mxchat_enqueue_scripts_styles() {
3417 + // Define version numbers for the styles and scripts
3418 + $chat_style_version = '2.0.7'; // Replace with your actual version
3419 + $chat_script_version = '2.0.7'; // Replace with your actual version
10367 3420
10368 -public function mxchat_enqueue_scripts_styles() {
10369 - // Fetch options from the database first to check loading strategy
10370 - $this->options = get_option('mxchat_options');
10371 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
3421 + // Enqueue the script
3422 + wp_enqueue_script(
3423 + 'mxchat-chat-js',
3424 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
3425 + array('jquery'),
3426 + $chat_script_version,
3427 + true
3428 + );
10372 3429
10373 - // Always enqueue CSS immediately
3430 + // Enqueue the CSS
10374 3431 wp_enqueue_style(
10375 3432 'mxchat-chat-css',
10376 3433 plugin_dir_url(__FILE__) . '../css/chat-style.css',
10377 3434 array(),
10378 - MXCHAT_VERSION
3435 + $chat_style_version
10379 3436 );
10380 3437
10381 - // Handle script loading based on strategy
10382 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10383 - // Enqueue the script normally
10384 - wp_enqueue_script(
10385 - 'mxchat-chat-js',
10386 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
10387 - array('jquery'),
10388 - MXCHAT_VERSION,
10389 - true
10390 - );
10391 -
10392 - // Add defer attribute if strategy is 'defer'
10393 - if ($loading_strategy === 'defer') {
10394 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
10395 - }
10396 - } else {
10397 - // For delay or interaction-based loading, we'll use a custom loader
10398 - // Don't enqueue the main script - we'll load it dynamically
10399 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
10400 - }
10401 -
3438 + // Fetch options from the database
3439 + $this->options = get_option('mxchat_options');
10402 3440 $prompts_options = get_option('mxchat_prompts_options', array());
10403 3441
10404 - // Check if AI theme is active - if so, skip inline colors in JavaScript
10405 - $theme_options = get_option('mxchat_theme_options', array());
10406 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10407 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10408 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10409 -
10410 3442 // Prepare settings for JavaScript
10411 3443 $style_settings = array(
10412 3444 'ajax_url' => admin_url('admin-ajax.php'),
10413 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
10414 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
10415 - // as a one-shot fallback for the first interaction on a fresh page load
10416 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
10417 - // but the widget refetches before each subsequent send.
10418 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10419 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10420 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
3445 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
10421 3446 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
3447 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
10422 3448 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10423 3449 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10424 3450 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10425 3451 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -10431,1329 +3457,84 @@
10431 3457 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10432 3458 'icon_color' => $this->options['icon_color'] ?? '#fff',
10433 3459 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10434 3460 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10435 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
3461 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off', // Example for consistency
3462 +
10436 3463 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10437 3464 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
3465 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
10438 3466 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10439 3467 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10440 3468 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10441 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10442 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
10443 - 'initial_email_state' => null, // Also fixed this undefined variable
10444 - 'skip_email_check' => true,
10445 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10446 - 'skip_inline_colors' => $skip_inline_colors,
10447 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
10448 - );
10449 3469
10450 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10451 - // print/transcript, satisfaction rating) come from the shared
10452 - // dynamic-settings method so this inline payload and the first-open
10453 - // refresh endpoint can never drift (plan-32db95).
10454 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
10455 -
10456 - // For normal/defer loading, use wp_localize_script
10457 - // For delayed loading, we store settings in a transient to be output inline
10458 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
10459 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10460 - } else {
10461 - // Store settings for the delayed loader to use
10462 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
10463 - }
10464 -}
10465 -
10466 -/**
10467 - * Output the delayed script loader for performance optimization
10468 - */
10469 -public function mxchat_output_delayed_script_loader() {
10470 - $this->options = get_option('mxchat_options');
10471 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
10472 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
10473 -
10474 - // Get the stored settings
10475 - $prompts_options = get_option('mxchat_prompts_options', array());
10476 - $theme_options = get_option('mxchat_theme_options', array());
10477 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
10478 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
10479 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
10480 -
10481 - $style_settings = array(
10482 - 'ajax_url' => admin_url('admin-ajax.php'),
10483 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
10484 - // before each send. This inline value is a one-shot fallback for the first interaction.
10485 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
10486 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
10487 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
10488 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
10489 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
10490 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
10491 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
10492 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
10493 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
10494 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
10495 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
10496 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
10497 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
10498 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
10499 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
10500 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
10501 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
10502 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
10503 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
10504 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
10505 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
10506 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
10507 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
10508 3470 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
10509 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
10510 - 'initial_email_state' => null,
10511 - 'skip_email_check' => true,
10512 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
10513 - 'skip_inline_colors' => $skip_inline_colors,
10514 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
3471 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
10515 3472 );
10516 3473
10517 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
10518 - // print/transcript, satisfaction rating) come from the shared
10519 - // dynamic-settings method so this inline payload and the first-open
10520 - // refresh endpoint can never drift (plan-32db95).
10521 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
10522 -
10523 - // Determine delay time based on strategy
10524 - $delay_ms = 0;
10525 - switch ($loading_strategy) {
10526 - case 'delay_1s':
10527 - $delay_ms = 1000;
10528 - break;
10529 - case 'delay_3s':
10530 - $delay_ms = 3000;
10531 - break;
10532 - case 'delay_5s':
10533 - $delay_ms = 5000;
10534 - break;
10535 - }
10536 -
10537 - ?>
10538 - <script type="text/javascript">
10539 - (function() {
10540 - var mxchatLoaded = false;
10541 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
10542 - window.mxchatChat = mxchatChat;
10543 -
10544 - function loadMxChatScript() {
10545 - if (mxchatLoaded) return;
10546 - mxchatLoaded = true;
10547 -
10548 - function appendChatScript() {
10549 - var script = document.createElement('script');
10550 - script.src = <?php echo wp_json_encode($script_url); ?>;
10551 - script.type = 'text/javascript';
10552 - document.body.appendChild(script);
10553 - }
10554 -
10555 - if (typeof jQuery !== 'undefined') {
10556 - appendChatScript();
10557 - } else {
10558 - var jq = document.createElement('script');
10559 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
10560 - jq.onload = appendChatScript;
10561 - document.body.appendChild(jq);
10562 - }
10563 - }
10564 -
10565 - <?php if ($loading_strategy === 'on_interaction'): ?>
10566 - // Load on user interaction
10567 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
10568 - events.forEach(function(evt) {
10569 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
10570 - });
10571 - // Fallback: load after 8 seconds if no interaction
10572 - setTimeout(loadMxChatScript, 8000);
10573 - <?php else: ?>
10574 - // Load after specified delay
10575 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
10576 - <?php endif; ?>
10577 - })();
10578 - </script>
10579 - <?php
3474 + // Pass the settings to the script
3475 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
10580 3476 }
10581 3477
10582 -/**
10583 - * Setup the cron jobs for rate limits with guard against multiple calls
10584 - */
10585 -public function setup_rate_limit_cron_jobs() {
10586 - // Add a guard to prevent multiple rapid calls
10587 - $last_setup = get_transient('mxchat_cron_setup_guard');
10588 - if ($last_setup && (time() - $last_setup) < 60) {
10589 - // Don't run again if we ran less than 60 seconds ago
10590 - return;
10591 - }
10592 -
10593 - // Set the guard
10594 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
10595 -
10596 - try {
10597 - // First, check if WordPress cron is disabled
10598 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
10599 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
10600 - $this->setup_fallback_rate_limit_system();
10601 - return;
10602 - }
10603 -
10604 - // Check if cron is already scheduled - if so, don't mess with it
10605 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
10606 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
10607 - return;
10608 - }
10609 -
10610 - // Clear any orphaned hooks (but don't loop indefinitely)
10611 - $hooks_to_clear = [
10612 - 'mxchat_reset_rate_limits',
10613 - 'mxchat_reset_hourly_rate_limits',
10614 - 'mxchat_reset_daily_rate_limits',
10615 - 'mxchat_reset_weekly_rate_limits',
10616 - 'mxchat_reset_monthly_rate_limits'
10617 - ];
10618 -
10619 - foreach ($hooks_to_clear as $hook) {
10620 - // Only clear a maximum of 3 instances to prevent infinite loops
10621 - $cleared = 0;
10622 - while (wp_next_scheduled($hook) && $cleared < 3) {
10623 - wp_clear_scheduled_hook($hook);
10624 - $cleared++;
10625 - }
10626 - }
10627 -
10628 - // Small delay after clearing
10629 - usleep(100000); // 0.1 seconds
10630 -
10631 - // Try to schedule the event
10632 - $initial_time = time() + 300; // Start in 5 minutes
10633 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
10634 -
10635 - if ($result === false) {
10636 - //error_log('MxChat: Failed to schedule cron, using fallback system');
10637 - $this->setup_fallback_rate_limit_system();
10638 - } else {
10639 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
10640 - }
10641 -
10642 - } catch (Exception $e) {
10643 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
10644 - $this->setup_fallback_rate_limit_system();
10645 - }
10646 -}
10647 3478
10648 -/**
10649 - * Try alternative cron scheduling methods
10650 - */
10651 -private function try_alternative_cron_scheduling($initial_time) {
10652 - try {
10653 - // Method 1: Try with current time instead of future time
10654 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
10655 - if ($result1 !== false) {
10656 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
10657 - return true;
10658 - }
10659 -
10660 - // Method 2: Try with a different interval
10661 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
10662 - if ($result2 !== false) {
10663 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
10664 - return true;
10665 - }
10666 -
10667 - // Method 3: Try wp_schedule_single_event first, then recurring
10668 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
10669 - if ($result3 !== false) {
10670 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
10671 - // Schedule the next one manually in the handler
10672 - return true;
10673 - }
10674 -
10675 - return false;
10676 -
10677 - } catch (Exception $e) {
10678 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
10679 - return false;
10680 - }
10681 -}
3479 +public function mxchat_reset_rate_limits() {
3480 + global $wpdb;
10682 3481
10683 -/**
10684 - * Enhanced fallback rate limit system
10685 - */
10686 -private function setup_fallback_rate_limit_system() {
10687 - // Set a flag to use database-based rate limit cleanup
10688 - update_option('mxchat_use_fallback_rate_limits', true);
10689 -
10690 - // Schedule a one-time check to happen on the next plugin load
10691 - update_option('mxchat_next_rate_limit_check', time() + 3600);
10692 -
10693 - // Also set up a more frequent fallback check (every 4 hours)
10694 - update_option('mxchat_fallback_check_interval', 4 * 3600);
10695 -
10696 - //error_log('MxChat: Fallback rate limit system activated');
10697 -}
3482 + // Define a cache key pattern for rate limits
3483 + $cache_key_pattern = 'mxchat_chat_limit_%';
10698 3484
10699 -/**
10700 - * Enhanced fallback check method
10701 - */
10702 -public function check_fallback_rate_limits() {
10703 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10704 -
10705 - if (!$use_fallback) {
10706 - return; // Regular cron is working
10707 - }
10708 -
10709 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10710 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
10711 -
10712 - if (time() >= $next_check) {
10713 - //error_log('MxChat: Running fallback rate limit cleanup');
10714 - $this->mxchat_reset_rate_limits();
10715 -
10716 - // Schedule next check
10717 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
10718 - }
10719 -}
10720 -/**
10721 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
10722 - */
10723 -public function check_rate_limit() {
10724 - // Check if we need to run fallback cleanup
10725 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
10726 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
10727 -
10728 - if ($use_fallback && time() >= $next_check) {
10729 - $this->mxchat_reset_rate_limits();
10730 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
10731 - }
10732 -
10733 - // Get bot ID from current request context
10734 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10735 -
10736 - // Get bot-specific options (includes rate limits if overridden)
10737 - $bot_options = $this->get_bot_options($bot_id);
10738 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
10739 -
10740 - // Use bot-specific rate limits if available, otherwise fall back to default
10741 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
3485 + // Retrieve all option names matching the pattern
3486 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
3487 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
10742 3488
10743 - // -------------------------------------------------------------------
10744 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
10745 - // it acts as a hard ceiling across all users + all roles. Default is
10746 - // 'unlimited' so existing installs are unchanged. Counter key drops
10747 - // both <role> and <user_id> segments — single pool per bot.
10748 - // -------------------------------------------------------------------
10749 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
10750 - ? $current_options['rate_limits_global']
10751 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
10752 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
10753 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
10754 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
10755 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
10756 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
10757 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
10758 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
10759 - if ((int) $global_data['count'] === 0) {
10760 - $global_data['timestamp'] = time();
10761 - update_option($global_option, $global_data);
10762 - }
10763 - $now = time();
10764 - $ts = (int) $global_data['timestamp'];
10765 - $reset = false;
10766 - switch ($global_timeframe) {
10767 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
10768 - case 'daily': $reset = ($now - $ts) >= 86400; break;
10769 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
10770 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
10771 - }
10772 - if ($reset) {
10773 - $global_data = ['count' => 0, 'timestamp' => $now];
10774 - update_option($global_option, $global_data);
10775 - }
10776 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
10777 - $global_msg = !empty($global_cfg['message'])
10778 - ? $global_cfg['message']
10779 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
10780 - return [
10781 - 'error' => true,
10782 - 'message' => $this->process_rate_limit_message_html($global_msg),
10783 - ];
10784 - }
10785 - // Reserve the slot for this request. Per-role check below also increments
10786 - // its own counter — that is intentional, both ceilings apply independently.
10787 - $global_data['count']++;
10788 - update_option($global_option, $global_data);
10789 - }
3489 + // db call ok; no-cache ok
3490 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- db call ok
3491 + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
10790 3492
10791 - // Determine user role or if logged out
10792 - if (is_user_logged_in()) {
10793 - $user = wp_get_current_user();
10794 - $user_id = $user->ID;
10795 -
10796 - // Get the user's primary role using reset() to safely get the first element
10797 - $user_roles = $user->roles;
10798 -
10799 - // Safely get the first role regardless of array key structure
10800 - if (!empty($user_roles) && is_array($user_roles)) {
10801 - $role = reset($user_roles); // This safely gets the first element regardless of key
10802 - } else {
10803 - $role = 'subscriber'; // Default to subscriber if no role found
3493 + // Clear the relevant cache entries
3494 + foreach ($option_names as $option_name) {
3495 + wp_cache_delete($option_name, 'options');
10804 3496 }
10805 - } else {
10806 - $role = 'logged_out';
10807 - // Use IP address for non-logged-in users
10808 - $user_id = $this->get_client_ip();
10809 - }
10810 -
10811 - // Check if rate limits are configured for this role
10812 - if (!isset($rate_limits_source[$role])) {
10813 - return true; // No limit set for this role
10814 - }
10815 -
10816 - $limit = $rate_limits_source[$role]['limit'];
10817 -
10818 - // If unlimited, return true immediately
10819 - if ($limit === 'unlimited') {
10820 - return true;
10821 - }
10822 -
10823 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
10824 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
10825 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
10826 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
10827 -
10828 - // Include bot_id in option name so each bot has separate rate limits
10829 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
10830 -
10831 - // Get the counter data
10832 - $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
10833 -
10834 - // If first request or counter reset needed, set the initial timestamp
10835 - if ($limit_data['count'] === 0) {
10836 - $limit_data['timestamp'] = time();
10837 - update_option($option_name, $limit_data);
10838 - }
10839 -
10840 - // Get the timeframe
10841 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
10842 - $rate_limits_source[$role]['timeframe'] : 'daily';
10843 -
10844 - // Check if the counter needs to be reset based on timeframe
10845 - $current_time = time();
10846 - $timestamp = $limit_data['timestamp'];
10847 - $should_reset = false;
10848 -
10849 - switch ($timeframe) {
10850 - case 'hourly':
10851 - $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
10852 - break;
10853 - case 'daily':
10854 - $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
10855 - break;
10856 - case 'weekly':
10857 - $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
10858 - break;
10859 - case 'monthly':
10860 - $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
10861 - break;
10862 - }
10863 -
10864 - // Reset the counter if the timeframe has passed
10865 - if ($should_reset) {
10866 - $limit_data = ['count' => 0, 'timestamp' => $current_time];
10867 - update_option($option_name, $limit_data);
10868 - }
10869 -
10870 - // Check if user has exceeded their limit
10871 - if ($limit_data['count'] >= intval($limit)) {
10872 - // Get the custom message for this role
10873 - $message = !empty($rate_limits_source[$role]['message'])
10874 - ? $rate_limits_source[$role]['message']
10875 - : __('Rate limit exceeded. Please try again later.', 'mxchat');
10876 -
10877 - // Add timeframe information to the message if placeholders exist
10878 - $timeframe_label = '';
10879 - switch ($timeframe) {
10880 - case 'hourly':
10881 - $timeframe_label = __('hour', 'mxchat');
10882 - break;
10883 - case 'daily':
10884 - $timeframe_label = __('day', 'mxchat');
10885 - break;
10886 - case 'weekly':
10887 - $timeframe_label = __('week', 'mxchat');
10888 - break;
10889 - case 'monthly':
10890 - $timeframe_label = __('month', 'mxchat');
10891 - break;
10892 - }
10893 -
10894 - // Replace placeholders in the message
10895 - $message = str_replace(
10896 - ['{limit}', '{count}', '{remaining}', '{timeframe}'],
10897 - [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
10898 - $message
10899 - );
10900 -
10901 - // Process HTML links in the message
10902 - $message = $this->process_rate_limit_message_html($message);
10903 -
10904 - // Return error with the processed message
10905 - return [
10906 - 'error' => true,
10907 - 'message' => $message
10908 - ];
10909 - }
10910 -
10911 - // Increment the counter
10912 - $limit_data['count']++;
10913 - update_option($option_name, $limit_data);
10914 -
10915 - return true;
10916 -}
10917 3497
10918 -/**
10919 - * Enhanced rate limit reset with better error handling
10920 - */
10921 -public function mxchat_reset_rate_limits() {
10922 - try {
10923 - global $wpdb;
10924 - $all_options = get_option('mxchat_options', []);
10925 - $current_time = time();
10926 -
10927 - // Get rate limit options with a safer query and limit
10928 - $option_names = $wpdb->get_col(
10929 - $wpdb->prepare(
10930 - "SELECT option_name FROM {$wpdb->options}
10931 - WHERE option_name LIKE %s
10932 - LIMIT 1000",
10933 - 'mxchat_chat_limit_%'
10934 - )
10935 - );
10936 -
10937 - if (empty($option_names)) {
10938 - return;
10939 - }
10940 -
10941 - $processed_count = 0;
10942 - $max_processing_time = 30; // Maximum 30 seconds
10943 - $start_time = time();
10944 -
10945 - foreach ($option_names as $option_name) {
10946 - // Check processing time limit
10947 - if ((time() - $start_time) > $max_processing_time) {
10948 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
10949 - break;
10950 - }
10951 -
10952 - // Parse the option name more safely
10953 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
10954 - continue;
10955 - }
10956 -
10957 - $role_and_user = $matches[1] . '_' . $matches[2];
10958 - $parts = explode('_', $role_and_user);
10959 -
10960 - if (count($parts) < 2) {
10961 - continue;
10962 - }
10963 -
10964 - // Extract role (everything except the last part which is user ID)
10965 - $user_id_part = array_pop($parts);
10966 - $role = implode('_', $parts);
10967 -
10968 - // Skip if role doesn't exist in our settings
10969 - if (!isset($all_options['rate_limits'][$role])) {
10970 - // Clean up orphaned entries
10971 - delete_option($option_name);
10972 - continue;
10973 - }
10974 -
10975 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
10976 - $limit_data = get_option($option_name);
10977 -
10978 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
10979 - // Clean up invalid entries
10980 - delete_option($option_name);
10981 - continue;
10982 - }
10983 -
10984 - $timestamp = $limit_data['timestamp'];
10985 - $should_reset = false;
10986 -
10987 - // Determine if we should reset based on the timeframe
10988 - switch ($timeframe) {
10989 - case 'hourly':
10990 - $should_reset = ($current_time - $timestamp) >= 3600;
10991 - break;
10992 - case 'daily':
10993 - $should_reset = ($current_time - $timestamp) >= 86400;
10994 - break;
10995 - case 'weekly':
10996 - $should_reset = ($current_time - $timestamp) >= 604800;
10997 - break;
10998 - case 'monthly':
10999 - $should_reset = ($current_time - $timestamp) >= 2592000;
11000 - break;
11001 - }
11002 -
11003 - // Reset the counter if the timeframe has passed
11004 - if ($should_reset) {
11005 - delete_option($option_name);
11006 - wp_cache_delete($option_name, 'options');
11007 - $processed_count++;
11008 - }
11009 - }
11010 -
11011 - // Clean up any orphaned cache entries
3498 + // Optionally, clear a general cache if you have one
11012 3499 wp_cache_delete('mxchat_all_chat_limits', 'options');
11013 -
11014 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11015 -
11016 - } catch (Exception $e) {
11017 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11018 3500 }
11019 -}
11020 3501
11021 -
11022 -/**
11023 - * Process HTML links in rate limit messages
11024 - *
11025 - * @param string $message The rate limit message
11026 - * @return string The processed message with safe HTML links
11027 - */
11028 -private function process_rate_limit_message_html($message) {
11029 - // Return original message if empty
11030 - if (empty($message)) {
11031 - return $message;
3502 +private function mxchat_fetch_woocommerce_products() {
3503 + // Ensure WooCommerce is active
3504 + if (!class_exists('WooCommerce')) {
3505 + return [];
11032 3506 }
11033 -
11034 - // First, convert markdown links to HTML
11035 - $message = $this->convert_markdown_links($message);
11036 -
11037 - // Then, auto-convert any remaining plain URLs to links
11038 - $message = $this->auto_link_urls($message);
11039 -
11040 - // Allow basic HTML tags for links and formatting
11041 - $allowed_tags = [
11042 - 'a' => [
11043 - 'href' => true,
11044 - 'target' => true,
11045 - 'rel' => true,
11046 - 'title' => true,
11047 - 'class' => true
11048 - ],
11049 - 'strong' => [],
11050 - 'em' => [],
11051 - 'br' => [],
11052 - 'b' => [],
11053 - 'i' => [],
11054 - 'span' => ['class' => true]
11055 - ];
11056 -
11057 - // Sanitize but allow the specified HTML tags
11058 - $processed_message = wp_kses($message, $allowed_tags);
11059 -
11060 - // If wp_kses stripped everything, return the original message as plain text
11061 - if (empty($processed_message) && !empty($message)) {
11062 - // Strip all HTML and return plain text as fallback
11063 - return wp_strip_all_tags($message);
11064 - }
11065 -
11066 - return $processed_message;
11067 -}
11068 3507
11069 -/**
11070 - * Convert markdown links to HTML
11071 - *
11072 - * @param string $text The text to process
11073 - * @return string The text with markdown links converted to HTML
11074 - */
11075 -private function convert_markdown_links($text) {
11076 - // Return original text if empty
11077 - if (empty($text)) {
11078 - return $text;
11079 - }
11080 -
11081 - // Pattern to match markdown links: [text](url)
11082 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
11083 -
11084 - $processed_text = preg_replace_callback($pattern, function($matches) {
11085 - $link_text = $matches[1];
11086 - $url = $matches[2];
11087 -
11088 - // Clean up any trailing punctuation from the URL
11089 - $url = rtrim($url, '.,;:!?');
11090 -
11091 - // Sanitize the link text and URL
11092 - $safe_text = esc_html($link_text);
11093 - $safe_url = esc_url($url);
11094 -
11095 - // Create the HTML link
11096 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
11097 - }, $text);
11098 -
11099 - // If preg_replace_callback failed, return original text
11100 - if ($processed_text === null) {
11101 - return $text;
11102 - }
11103 -
11104 - return $processed_text;
11105 -}
3508 + $args = array(
3509 + 'post_type' => 'product',
3510 + 'post_status' => 'publish',
3511 + 'posts_per_page' => -1,
3512 + );
11106 3513
11107 -/**
11108 - * Auto-convert plain URLs to clickable links
11109 - *
11110 - * @param string $text The text to process
11111 - * @return string The text with URLs converted to links
11112 - */
11113 -private function auto_link_urls($text) {
11114 - // Return original text if empty
11115 - if (empty($text)) {
11116 - return $text;
11117 - }
11118 -
11119 - // Simple pattern that avoids complex lookbehinds
11120 - // This will match URLs that are not already inside href attributes or markdown links
11121 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
11122 -
11123 - $processed_text = preg_replace_callback($pattern, function($matches) {
11124 - $url = $matches[0];
11125 - // Clean up any trailing punctuation that might have been captured
11126 - $url = rtrim($url, '.,;:!?');
11127 -
11128 - // Add target="_blank" and rel="noopener noreferrer" for security
11129 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
11130 - }, $text);
11131 -
11132 - // If preg_replace_callback failed, return original text
11133 - if ($processed_text === null) {
11134 - return $text;
11135 - }
11136 -
11137 - return $processed_text;
11138 -}
3514 + $products = get_posts($args);
3515 + $product_data = [];
11139 3516
3517 + foreach ($products as $product) {
3518 + $product_id = $product->ID;
3519 + $product_obj = wc_get_product($product_id);
11140 3520
11141 -// Helper function to get client IP address
11142 -private function get_client_ip() {
11143 - // Check for shared internet/ISP IP
11144 - if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
11145 - return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
3521 + $product_data[] = array(
3522 + 'id' => $product_id,
3523 + 'name' => $product_obj->get_name(),
3524 + 'description' => $product_obj->get_description(),
3525 + 'short_description' => $product_obj->get_short_description(),
3526 + 'url' => get_permalink($product_id),
3527 + 'price' => $product_obj->get_regular_price(),
3528 + 'sale_price' => $product_obj->get_sale_price(),
3529 + 'stock_status' => $product_obj->get_stock_status(),
3530 + 'sku' => $product_obj->get_sku(),
3531 + 'in_stock' => $product_obj->is_in_stock(),
3532 + 'total_sales' => $product_obj->get_total_sales(),
3533 + );
11146 3534 }
11147 -
11148 - // Check for IPs passing through proxies
11149 - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
11150 - // Use the first value in the comma-separated list
11151 - $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
11152 - return trim($forwarded_for[0]);
11153 - }
11154 -
11155 - if (!empty($_SERVER['REMOTE_ADDR'])) {
11156 - return sanitize_text_field($_SERVER['REMOTE_ADDR']);
11157 - }
11158 -
11159 - // Fallback
11160 - return 'unknown';
11161 -}
11162 3535
11163 -/**
11164 - * AJAX handler to get system information for testing panel
11165 - */
11166 -/**
11167 - * AJAX handler to get system information for testing panel
11168 - */
11169 -public function mxchat_get_system_info() {
11170 - // Verify nonce for security
11171 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11172 - wp_send_json_error(['message' => 'Invalid nonce']);
11173 - return;
11174 - }
11175 -
11176 - // Only allow admin users
11177 - if (!current_user_can('administrator')) {
11178 - wp_send_json_error(['message' => 'Unauthorized']);
11179 - return;
11180 - }
11181 -
11182 - // Get system prompt from options
11183 - $system_prompt = isset($this->options['system_prompt_instructions'])
11184 - ? $this->options['system_prompt_instructions']
11185 - : 'No system prompt configured';
11186 -
11187 - // Get selected model
11188 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
11189 -
11190 - // Check if OpenRouter is being used
11191 - $is_openrouter = ($selected_model === 'openrouter');
11192 - $openrouter_model = '';
11193 -
11194 - if ($is_openrouter) {
11195 - // Get the actual OpenRouter model that's selected
11196 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11197 - ? $this->options['openrouter_selected_model']
11198 - : 'No OpenRouter model selected';
11199 -
11200 - // Update selected_model display to show both
11201 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11202 - }
11203 -
11204 - // Get API key status (just check if they exist, don't expose the keys)
11205 - $api_status = [];
11206 - $api_status['openai'] = !empty($this->options['api_key']);
11207 - $api_status['claude'] = !empty($this->options['claude_api_key']);
11208 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11209 - $api_status['xai'] = !empty($this->options['xai_api_key']);
11210 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11211 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11212 -
11213 - wp_send_json_success([
11214 - 'system_prompt' => $system_prompt,
11215 - 'selected_model' => $selected_model,
11216 - 'is_openrouter' => $is_openrouter,
11217 - 'openrouter_model' => $openrouter_model,
11218 - 'api_status' => $api_status
11219 - ]);
3536 + return $product_data;
11220 3537 }
11221 -
11222 -/**
11223 - * AJAX handler to get similarity threshold
11224 - */
11225 -public function mxchat_get_similarity_threshold() {
11226 - // Verify nonce for security
11227 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11228 - wp_send_json_error(['message' => 'Invalid nonce']);
11229 - return;
11230 - }
11231 -
11232 - // Only allow admin users
11233 - if (!current_user_can('administrator')) {
11234 - wp_send_json_error(['message' => 'Unauthorized']);
11235 - return;
11236 - }
11237 -
11238 - // Get similarity threshold from main options (default 35%)
11239 - $similarity_threshold = isset($this->options['similarity_threshold'])
11240 - ? ((int) $this->options['similarity_threshold']) / 100
11241 - : 0.35;
11242 -
11243 - wp_send_json_success([
11244 - 'threshold' => $similarity_threshold,
11245 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
11246 - ]);
11247 -}
11248 -
11249 -/**
11250 - * AJAX handler to get knowledge base status
11251 - */
11252 -public function mxchat_get_kb_status() {
11253 - // Verify nonce for security
11254 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11255 - wp_send_json_error(['message' => 'Invalid nonce']);
11256 - return;
11257 - }
11258 -
11259 - // Only allow admin users
11260 - if (!current_user_can('administrator')) {
11261 - wp_send_json_error(['message' => 'Unauthorized']);
11262 - return;
11263 - }
11264 -
11265 - // Check OpenAI Vector Store first (takes priority)
11266 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11267 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11268 -
11269 - if ($use_vectorstore) {
11270 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11271 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11272 -
11273 - $kb_info = [
11274 - 'type' => 'OpenAI Vector Store',
11275 - 'status' => 'Active',
11276 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11277 - ];
11278 -
11279 - wp_send_json_success($kb_info);
11280 - return;
11281 - }
11282 -
11283 - // Check Pinecone vs WordPress
11284 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
11285 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11286 -
11287 - $kb_info = [
11288 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11289 - 'status' => 'Active'
11290 - ];
11291 -
11292 - // Get document count
11293 - if ($use_pinecone) {
11294 - $kb_info['documents'] = 'Connected to Pinecone';
11295 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
11296 - } else {
11297 - // Count documents in WordPress database
11298 - global $wpdb;
11299 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11300 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11301 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11302 - }
11303 -
11304 - wp_send_json_success($kb_info);
11305 -}
11306 -
11307 -/**
11308 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
11309 - */
11310 -public function mxchat_start_fresh_session() {
11311 - // Verify nonce for security
11312 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11313 - wp_send_json_error(['message' => 'Invalid nonce']);
11314 - return;
11315 - }
11316 -
11317 - // Only allow admin users
11318 - if (!current_user_can('administrator')) {
11319 - wp_send_json_error(['message' => 'Unauthorized']);
11320 - return;
11321 - }
11322 -
11323 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
11324 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
11325 -
11326 - if (empty($old_session_id)) {
11327 - wp_send_json_error(['message' => 'Old session ID required']);
11328 - return;
11329 - }
11330 -
11331 - // If no new session ID provided, generate one
11332 - if (empty($new_session_id)) {
11333 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
11334 - }
11335 -
11336 - // Clear ALL data associated with the old session
11337 - $this->clear_complete_session_data($old_session_id);
11338 -
11339 - // Initialize the new session
11340 - $this->initialize_fresh_session($new_session_id);
11341 -
11342 - wp_send_json_success([
11343 - 'message' => 'Fresh session started successfully',
11344 - 'new_session_id' => $new_session_id,
11345 - 'old_session_id' => $old_session_id
11346 - ]);
11347 -}
11348 -
11349 -/**
11350 - * Clear ALL data associated with a session (ENHANCED)
11351 - */
11352 -private function clear_complete_session_data($session_id) {
11353 - // Clear chat history
11354 - delete_option("mxchat_history_{$session_id}");
11355 -
11356 - // Clear chat mode
11357 - delete_option("mxchat_mode_{$session_id}");
11358 -
11359 - // Clear any PDF/Word transients
11360 - $this->clear_pdf_transients($session_id);
11361 - if (method_exists($this, 'clear_word_transients')) {
11362 - $this->clear_word_transients($session_id);
11363 - }
11364 -
11365 - // Clear agent-related data
11366 - delete_option("mxchat_channel_{$session_id}");
11367 - delete_option("mxchat_agent_name_{$session_id}");
11368 - delete_option("mxchat_email_{$session_id}");
11369 -
11370 - // Clear any recommendation flow state
11371 - delete_option("mxchat_sr_flow_state_{$session_id}");
11372 -
11373 - // Clear any cached embeddings or context
11374 - delete_transient("mxchat_context_{$session_id}");
11375 - delete_transient("mxchat_last_query_{$session_id}");
11376 -
11377 - // Clear any testing data
11378 - delete_transient("mxchat_testing_data_{$session_id}");
11379 -
11380 - // Clear any rate limiting data for this session
11381 - delete_transient("mxchat_rate_limit_{$session_id}");
11382 -
11383 - // Clear any other session-specific transients
11384 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
11385 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
11386 - delete_transient("mxchat_include_word_in_context_{$session_id}");
11387 -
11388 - // Clear form addon state (pending forms and submitted forms)
11389 - delete_option("mxchat_pending_form_{$session_id}");
11390 - delete_option("mxchat_submitted_forms_{$session_id}");
11391 -
11392 - //error_log("MxChat: Cleared all data for session: {$session_id}");
11393 -}
11394 -
11395 -/**
11396 - * Initialize a fresh session with default data
11397 - */
11398 -private function initialize_fresh_session($session_id) {
11399 - // Set default chat mode
11400 - update_option("mxchat_mode_{$session_id}", 'ai');
11401 -
11402 - //error_log("MxChat: Initialized fresh session: {$session_id}");
11403 -}
11404 -
11405 -/**
11406 - * Helper method to clear Word document transients (if you have Word support)
11407 - */
11408 -private function clear_word_transients($session_id) {
11409 - delete_transient('mxchat_word_url_' . $session_id);
11410 - delete_transient('mxchat_word_filename_' . $session_id);
11411 - delete_transient('mxchat_word_embeddings_' . $session_id);
11412 - delete_transient('mxchat_include_word_in_context_' . $session_id);
11413 -}
11414 -
11415 -/**
11416 - * Simplified testing data capture method (CLEANED UP)
11417 - */
11418 -private function capture_testing_data($user_embedding, $message, $session_id) {
11419 - // Only capture for admin users
11420 - if (!current_user_can('administrator')) {
11421 - return null;
11422 - }
11423 -
11424 - $testing_data = [
11425 - 'query' => $message,
11426 - 'timestamp' => time(),
11427 - 'top_matches' => [],
11428 - 'action_matches' => [] // Add action matches
11429 - ];
11430 -
11431 - // Get similarity threshold
11432 - $similarity_threshold = isset($this->options['similarity_threshold'])
11433 - ? ((int) $this->options['similarity_threshold']) / 100
11434 - : 0.35;
11435 -
11436 - $testing_data['similarity_threshold'] = $similarity_threshold;
11437 -
11438 - // Use the real similarity analysis if available
11439 - if ($this->last_similarity_analysis !== null) {
11440 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
11441 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
11442 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
11443 - } else {
11444 - // Fallback: determine knowledge base type
11445 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
11446 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11447 -
11448 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
11449 - }
11450 -
11451 - // Include action analysis if available
11452 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
11453 - $testing_data['action_matches'] = $this->last_action_analysis;
11454 -
11455 - // Clear it after capturing to avoid stale data
11456 - $this->last_action_analysis = null;
11457 - }
11458 -
11459 - return $testing_data;
11460 -}
11461 -
11462 -
11463 -/**
11464 - * Track URL clicks from chatbot responses
11465 - */
11466 -public function mxchat_track_url_click() {
11467 - // Verify nonce for security
11468 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11469 - wp_send_json_error(['message' => 'Invalid nonce']);
11470 - wp_die();
11471 - }
11472 -
11473 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11474 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
11475 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
11476 -
11477 - if (empty($session_id) || empty($clicked_url)) {
11478 - wp_send_json_error(['message' => 'Missing required data']);
11479 - wp_die();
11480 - }
11481 -
11482 - global $wpdb;
11483 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11484 -
11485 - // Insert click tracking record
11486 - $wpdb->insert(
11487 - $table_name,
11488 - [
11489 - 'session_id' => $session_id,
11490 - 'clicked_url' => $clicked_url,
11491 - 'message_context' => $message_context,
11492 - 'click_timestamp' => current_time('mysql', 1),
11493 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
11494 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
11495 - ]
11496 - );
11497 -
11498 - wp_send_json_success(['message' => 'Click tracked']);
11499 - wp_die();
11500 -}
11501 -
11502 -/**
11503 - * Get URL click analytics for a session
11504 - */
11505 -public function mxchat_get_url_clicks($session_id) {
11506 - global $wpdb;
11507 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
11508 -
11509 - $clicks = $wpdb->get_results($wpdb->prepare(
11510 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
11511 - $session_id
11512 - ));
11513 -
11514 - return $clicks;
11515 -}
11516 -/**
11517 - * Track the originating page where chat was started
11518 - */
11519 -public function mxchat_track_originating_page() {
11520 - // Verify nonce
11521 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11522 - wp_send_json_error(['message' => 'Invalid nonce']);
11523 - wp_die();
11524 - }
11525 -
11526 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11527 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
11528 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
11529 -
11530 - if (empty($session_id)) {
11531 - wp_send_json_error(['message' => 'Missing session ID']);
11532 - wp_die();
11533 - }
11534 -
11535 - global $wpdb;
11536 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
11537 -
11538 - // Check if we've already tracked for this session
11539 - $existing = $wpdb->get_var($wpdb->prepare(
11540 - "SELECT COUNT(*) FROM $table_name
11541 - WHERE session_id = %s
11542 - AND originating_page_url IS NOT NULL",
11543 - $session_id
11544 - ));
11545 -
11546 - if ($existing > 0) {
11547 - wp_send_json_success(['message' => 'Already tracked']);
11548 - wp_die();
11549 - }
11550 -
11551 - // Update the first message in this session with originating page info
11552 - $wpdb->query($wpdb->prepare(
11553 - "UPDATE $table_name
11554 - SET originating_page_url = %s,
11555 - originating_page_title = %s
11556 - WHERE session_id = %s
11557 - ORDER BY timestamp ASC
11558 - LIMIT 1",
11559 - $page_url,
11560 - $page_title,
11561 - $session_id
11562 - ));
11563 -
11564 - wp_send_json_success(['message' => 'Originating page tracked']);
11565 - wp_die();
11566 -}
11567 -
11568 -/**
11569 - * Validate and clean URLs from AI response
11570 - * Removes any URLs that aren't in the knowledge base
11571 - *
11572 - * @param string $response_text The AI-generated response
11573 - * @param array $valid_urls Array of URLs from the knowledge base
11574 - * @return string Cleaned response with invalid URLs removed/flagged
11575 - */
11576 -private function validate_and_clean_urls($response_text, $valid_urls) {
11577 - // DEBUG: Log what we're working with
11578 - //error_log("=== MxChat URL Validation Debug ===");
11579 - //error_log("Valid URLs count: " . count($valid_urls));
11580 - //error_log("Valid URLs: " . print_r($valid_urls, true));
11581 - //error_log("Response text length: " . strlen($response_text));
11582 - //error_log("Response text preview: " . substr($response_text, 0, 500));
11583 -
11584 - // If no valid URLs provided or empty response, return as-is
11585 - if (empty($valid_urls) || empty($response_text)) {
11586 - //error_log("Validation skipped - empty valid_urls or response");
11587 - return $response_text;
11588 - }
11589 -
11590 - // Extract all URLs from the AI response
11591 - // This regex matches http:// and https:// URLs
11592 - preg_match_all(
11593 - '#\bhttps?://[^\s<>"\')\]]+#i',
11594 - $response_text,
11595 - $matches
11596 - );
11597 -
11598 - // If no URLs found in response, return as-is
11599 - if (empty($matches[0])) {
11600 - //error_log("No URLs found in response");
11601 - return $response_text;
11602 - }
11603 -
11604 - $found_urls = $matches[0];
11605 - $cleaned_response = $response_text;
11606 - $removed_count = 0;
11607 -
11608 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
11609 - $normalized_valid_urls = array_map(function($url) {
11610 - // Remove trailing slash
11611 - $url = rtrim($url, '/');
11612 - // Remove URL fragments (#section)
11613 - $url = preg_replace('/#.*$/', '', $url);
11614 - // Remove trailing punctuation that might have been captured
11615 - $url = rtrim($url, '.,;:!?');
11616 - return $url;
11617 - }, $valid_urls);
11618 -
11619 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
11620 -
11621 - foreach ($found_urls as $found_url) {
11622 - // Clean up the found URL (remove trailing punctuation that might have been captured)
11623 - $clean_found_url = rtrim($found_url, '.,;:!?)');
11624 -
11625 - // DEBUG: Log each URL being checked
11626 - //error_log("Checking found URL: " . $found_url);
11627 -
11628 - // Normalize for comparison
11629 - $normalized_found = rtrim($clean_found_url, '/');
11630 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
11631 -
11632 - //error_log("Normalized found URL: " . $normalized_found);
11633 -
11634 - // Check if this URL exists in our valid URLs list
11635 - $is_valid = false;
11636 -
11637 - //error_log("Starting validation checks for: " . $normalized_found);
11638 -
11639 - // First, try exact match
11640 - if (in_array($normalized_found, $normalized_valid_urls)) {
11641 - $is_valid = true;
11642 - //error_log("EXACT MATCH FOUND");
11643 - } else {
11644 - //error_log("No exact match, checking variations...");
11645 - // If no exact match, check if it's a variation (with query params, etc.)
11646 - foreach ($normalized_valid_urls as $valid_url) {
11647 - //error_log(" Comparing against valid URL: " . $valid_url);
11648 -
11649 - // Check if the found URL starts with a valid URL (handles query params)
11650 - if (strpos($normalized_found, $valid_url) === 0) {
11651 - // Check what comes after the valid URL
11652 - $remainder = substr($normalized_found, strlen($valid_url));
11653 -
11654 - // Only valid if:
11655 - // 1. Exact match (remainder is empty)
11656 - // 2. Query params (starts with ?)
11657 - // 3. Fragment (starts with #)
11658 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
11659 - $is_valid = true;
11660 - //error_log(" MATCH: Found URL is valid variation of base URL");
11661 - break;
11662 - } else {
11663 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
11664 - }
11665 - }
11666 - // Also check the reverse (in case valid URL has query params)
11667 - if (strpos($valid_url, $normalized_found) === 0) {
11668 - $is_valid = true;
11669 - //error_log(" MATCH: Valid URL starts with found URL");
11670 - break;
11671 - }
11672 - }
11673 -
11674 - if (!$is_valid) {
11675 - //error_log("NO MATCH FOUND - URL should be removed");
11676 - }
11677 - }
11678 -
11679 - // If URL is not valid, remove it from the response
11680 - if (!$is_valid) {
11681 - // Log the removal for debugging
11682 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
11683 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
11684 -
11685 - $removed_count++;
11686 -
11687 - // Check if URL is part of a markdown link: [text](url)
11688 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
11689 - if (preg_match($markdown_pattern, $cleaned_response)) {
11690 - //error_log("Found markdown link, removing but keeping text");
11691 - // Remove the markdown link but keep the text
11692 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
11693 - }
11694 - // Check if URL is part of an HTML link: <a href="url">text</a>
11695 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
11696 - //error_log("Found HTML link, removing but keeping text");
11697 - // Remove the HTML link but keep the text
11698 - $link_text = $link_match[1];
11699 - $cleaned_response = preg_replace(
11700 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
11701 - $link_text,
11702 - $cleaned_response
11703 - );
11704 - }
11705 - // Otherwise just remove the bare URL
11706 - else {
11707 - //error_log("Removing bare URL");
11708 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
11709 - }
11710 - }
11711 - }
11712 -
11713 - // Log summary if any URLs were removed
11714 - if ($removed_count > 0) {
11715 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
11716 - } else {
11717 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
11718 - }
11719 -
11720 - // Clean up any double spaces or awkward punctuation left behind
11721 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
11722 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
11723 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
11724 -
11725 - //error_log("Final cleaned response: " . $cleaned_response);
11726 -
11727 - return trim($cleaned_response);
11728 -}
11729 -
11730 -/**
11731 - * AJAX handler to get current chat mode for a session
11732 - */
11733 -public function mxchat_get_current_chat_mode() {
11734 - // Verify nonce for security
11735 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
11736 - wp_send_json_error(['message' => 'Invalid nonce']);
11737 - wp_die();
11738 - }
11739 -
11740 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
11741 -
11742 - if (empty($session_id)) {
11743 - wp_send_json_error(['message' => 'Session ID missing']);
11744 - wp_die();
11745 - }
11746 -
11747 - // Get the current chat mode for this session
11748 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
11749 -
11750 - wp_send_json_success([
11751 - 'chat_mode' => $chat_mode
11752 - ]);
11753 - wp_die();
11754 -}
11755 -
11756 -
11757 3538
11758 3539 }
11759 3540 ?>