PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.1.4
MxChat – AI Chatbot & Content Generation for WordPress v2.1.4
3.2.22 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 All 153 releases
← All changes | includes/class-mxchat-integrator.php +1590 -10277 3.2.102.1.4 View file →
@@ -8,296 +8,51 @@
8 8 private $prompts_options;
9 9 private $chat_count;
10 10 private $fallbackResponse;
11 11 private $productCardHtml;
12 - // plan-mxchat-20260617-48a57a — function-calling UI payload capture. When a
13 - // model-invoked tool yields a UI element (generated image, woo product card,
14 - // image-search gallery), the FC loop stashes its html here so the FC outcome
15 - // handler can SURFACE it to the frontend the same way the intent path does,
16 - // instead of stripping it to text for the model (the bug: UI-bearing actions
17 - // rendered nothing under function calling).
18 - private $fc_ui_html = '';
19 - private $fc_ui_images = array();
20 - private $fc_ui_captured = false;
21 12 private $word_handler;
22 - private $last_similarity_analysis = null;
23 - private $current_valid_urls = [];
24 - private $last_vectorstore_error = null;
25 - private $is_streaming = false; // ADDED: Track if current request is streaming
26 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
27 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
28 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
29 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
30 13
31 -/**
32 - * Setup streaming headers - call this right before actually streaming
33 - * This delays header setup to allow actions/forms to return JSON responses
34 - */
35 -/**
36 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
37 - *
38 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
39 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
40 - * specific "overloaded" / "rate limit" body string. Returns immediately on
41 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
42 - *
43 - * Drop-in replacement for wp_remote_post — returns the same shape
44 - * (WP_Error or response array) so the caller's existing error-handling
45 - * code path is unchanged.
46 - *
47 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
48 - * paths (the *_response_openai / *_response_claude / etc functions).
49 - * For the *_stream variants, the cURL initial-connect happens inside a
50 - * read-chunks loop — retrying there safely (without re-emitting partial
51 - * stream chunks to the client) is a separate problem. Streaming paths
52 - * are NOT wrapped in this build; tracked as a follow-on.
53 - *
54 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
55 - * (default true). When false, behavior is identical to plain wp_remote_post.
56 - */
57 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
58 - $opts = is_array($this->options ?? null) ? $this->options : array();
59 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
60 - (string) $opts['auto_retry_on_transient_error'] !== '0';
61 -
62 - if (!$enabled) {
63 - return wp_remote_post($url, $args);
64 - }
65 -
66 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
67 - $last_response = null;
68 -
69 - foreach ($backoffs as $i => $delay_ms) {
70 - if ($delay_ms > 0) {
71 - usleep($delay_ms * 1000);
72 - }
73 - $response = wp_remote_post($url, $args);
74 - $last_response = $response;
75 -
76 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
77 - return $response;
78 - }
79 -
80 - if (defined('WP_DEBUG') && WP_DEBUG) {
81 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
82 - : (int) wp_remote_retrieve_response_code($response);
83 - error_log(sprintf(
84 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
85 - $provider_hint ?: 'unknown',
86 - $i + 1,
87 - $code_for_log,
88 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
89 - ));
90 - }
91 - }
92 -
93 - return $last_response;
94 -}
95 -
96 -/**
97 - * Returns true if a wp_remote_post response represents a TRANSIENT
98 - * provider error worth retrying. Conservative — only retries on signals
99 - * that are very likely to clear within a few seconds.
100 - *
101 - * Transient signals:
102 - * - WP_Error with timeout / connection / dns / ssl
103 - * - HTTP 429, 502, 503, 504
104 - * - Provider-specific overload bodies (gemini "overloaded", openai
105 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
106 - *
107 - * NOT transient (return false — fail-fast):
108 - * - 200/2xx (success)
109 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
110 - * budget; the user needs to fix something)
111 - * - Any other 4xx (assume permanent unless explicitly listed above)
112 - * - 5xx other than the four listed above (e.g. 500 generic server error
113 - * is often a malformed request on our side, not a transient outage)
114 - */
115 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
116 - if (is_wp_error($response)) {
117 - $code = $response->get_error_code();
118 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
119 - || stripos((string) $response->get_error_message(), 'timed out') !== false
120 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
121 - }
122 -
123 - $status = (int) wp_remote_retrieve_response_code($response);
124 - if (in_array($status, array(429, 502, 503, 504), true)) {
125 - return true;
126 - }
127 - if ($status >= 200 && $status < 300) {
128 - return false;
129 - }
130 - // Permanent 4xx that should fail fast — even with no body.
131 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
132 - return false;
133 - }
134 -
135 - // Provider-specific body inspection for the cases where the upstream
136 - // returns 200 with an error envelope (gemini does this for overload).
137 - $body = (string) wp_remote_retrieve_body($response);
138 - if ($body === '') {
139 - return false;
140 - }
141 - $lower = strtolower($body);
142 - $hint = strtolower((string) $provider_hint);
143 -
144 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
145 - || strpos($lower, 'high demand') !== false
146 - || strpos($lower, 'model is overloaded') !== false)) {
147 - return true;
148 - }
149 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
150 - || strpos($lower, '"type":"server_error"') !== false
151 - || strpos($lower, '"code":"server_error"') !== false)) {
152 - return true;
153 - }
154 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
155 - || strpos($lower, 'overloaded_error') !== false)) {
156 - return true;
157 - }
158 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
159 - return true;
160 - }
161 -
162 - return false;
163 -}
164 -
165 -/**
166 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
167 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
168 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
169 - * collect status separately from a plain wp_remote_post array shape, so the
170 - * non-streaming helper above can't be called directly. This delegate keeps
171 - * the classification rules identical across both paths.
172 - */
173 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
174 - if ($curl_errno) {
175 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
176 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
177 - return in_array($curl_errno, array(
178 - CURLE_OPERATION_TIMEDOUT,
179 - CURLE_COULDNT_CONNECT,
180 - CURLE_COULDNT_RESOLVE_HOST,
181 - CURLE_SSL_CONNECT_ERROR,
182 - CURLE_GOT_NOTHING,
183 - CURLE_SEND_ERROR,
184 - CURLE_RECV_ERROR,
185 - ), true);
186 - }
187 -
188 - $status = (int) $http_code;
189 - if (in_array($status, array(429, 502, 503, 504), true)) {
190 - return true;
191 - }
192 - if ($status >= 200 && $status < 300) {
193 - return false;
194 - }
195 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
196 - return false;
197 - }
198 -
199 - $body = (string) $body;
200 - if ($body === '') {
201 - return false;
202 - }
203 - $lower = strtolower($body);
204 - $hint = strtolower((string) $provider_hint);
205 -
206 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
207 - || strpos($lower, 'high demand') !== false
208 - || strpos($lower, 'model is overloaded') !== false)) {
209 - return true;
210 - }
211 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
212 - || strpos($lower, '"type":"server_error"') !== false
213 - || strpos($lower, '"code":"server_error"') !== false)) {
214 - return true;
215 - }
216 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
217 - || strpos($lower, 'overloaded_error') !== false)) {
218 - return true;
219 - }
220 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
221 - return true;
222 - }
223 -
224 - return false;
225 -}
226 -
227 -/**
228 - * Whether transient-error auto-retry is enabled in admin settings.
229 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
230 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
231 - */
232 -private function mxchat_retry_enabled() {
233 - $opts = is_array($this->options ?? null) ? $this->options : array();
234 - return !isset($opts['auto_retry_on_transient_error']) ||
235 - (string) $opts['auto_retry_on_transient_error'] !== '0';
236 -}
237 -
238 -private function setup_streaming_headers() {
239 - if ($this->streaming_headers_sent || headers_sent()) {
240 - return false;
241 - }
242 -
243 - // Disable output buffering
244 - while (ob_get_level()) {
245 - ob_end_flush();
246 - }
247 -
248 - // Set headers for SSE
249 - header('Content-Type: text/event-stream');
250 - header('Cache-Control: no-cache');
251 - header('Connection: keep-alive');
252 - header('X-Accel-Buffering: no');
253 -
254 - ob_implicit_flush(true);
255 - flush();
256 -
257 - $this->streaming_headers_sent = true;
258 - return true;
259 -}
260 -
261 -/**
262 - * Class constructor
263 - */
264 14 public function __construct() {
265 15 $this->options = get_option('mxchat_options');
266 16 $this->prompts_options = get_option('mxchat_prompts_options', array());
17 +
267 18 $this->chat_count = get_option('mxchat_chat_count', 0);
268 19 $this->word_handler = new MXChat_Word_Handler($this->options);
269 -
270 - // Add all action hooks
20 +
271 21 add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
272 22 add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
273 23 add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
24 +
274 25 add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
275 26 add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
276 -
277 27 // Add the AJAX actions for checking if the pre-chat message was dismissed
278 28 add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
279 29 add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
30 +
280 31 add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
281 32 add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
33 +
282 34 add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
283 35 add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
284 -
36 +
37 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
38 + wp_schedule_event(time(), 'daily', 'mxchat_reset_rate_limits');
39 + }
40 +
285 41 // Add REST API routes registration
286 42 add_action('rest_api_init', array($this, 'register_routes'));
43 +
287 44 add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
288 45 add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
289 -
290 - // Rate limit action - notice we removed the old schedule setup
46 +
291 47 add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
292 -
293 - // File upload and handling actions
48 +
294 49 add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
295 50 add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
296 51 add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
297 52 add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
298 -
299 - // Word document handling actions
53 +
54 + // Add these with your other add_action hooks
300 55 add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
301 56 add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
302 57 add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
303 58 add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
@@ -302,132 +57,16 @@
302 57 add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
303 58 add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
304 59 add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
305 60 add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
306 -
307 - // Email handling actions
61 +
308 62 add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
309 63 add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
310 64 add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
311 65 add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
312 -
313 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
314 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
315 -
316 - // Testing panel AJAX actions
317 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
318 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
319 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
320 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
321 - // Add to your existing constructor, in the section with other AJAX actions:
322 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
323 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
324 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
325 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
326 - // Add chat mode checking actions
327 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
328 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
329 -
330 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
331 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
332 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
333 -
334 - // Auto-email transcript action
335 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
336 -
337 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
338 -
339 -
340 66 }
341 67
342 -/**
343 - * Return a fresh nonce so cached pages can replace the stale one.
344 - * With `with_settings`, also returns the current behavior-gate settings so
345 - * the widget can correct stale inline-localized values (plan-32db95).
346 - */
347 -public function mxchat_refresh_nonce() {
348 - nocache_headers();
349 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
350 - if (!empty($_REQUEST['with_settings'])) {
351 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
352 - }
353 - wp_send_json_success($payload);
354 -}
355 68
356 -/**
357 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
358 - *
359 - * Every widget setting ships inline in page HTML via wp_localize_script, so
360 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
361 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
362 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
363 - * caches PHP can reach; this payload covers the rest — the widget requests
364 - * it on first open (via the nonce-refresh endpoints) and merges it over
365 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
366 - * nonce uses.
367 - *
368 - * Behavior gates + labels ONLY — colors stay inline because they're also
369 - * server-inline-styled, and a runtime swap would visibly flash.
370 - *
371 - * Both wp_localize_script blocks merge this exact array, so the inline and
372 - * refreshed payloads cannot drift.
373 - *
374 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
375 - * instead of trusting the instance copy.
376 - * @return array
377 - */
378 -public function get_dynamic_widget_settings($fresh = false) {
379 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
380 - if (!is_array($options)) {
381 - $options = array();
382 - }
383 - return array(
384 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
385 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
386 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
387 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
388 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
389 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
390 - // "Start new chat" header-menu item (plan ac2e81). Default OFF.
391 - 'reset_chat_enabled' => $options['reset_chat_enabled'] ?? 'off',
392 - 'reset_chat_label' => !empty($options['reset_chat_label']) ? esc_html($options['reset_chat_label']) : esc_html__('Start new chat', 'mxchat'),
393 - 'reset_chat_confirm' => esc_html__('Start a new chat? This clears the current conversation.', 'mxchat'),
394 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
395 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
396 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
397 - // scalars to string, and (string) false === '' — which the widget's
398 - // old gate read as enabled (plan-4bba64). The filter keeps its
399 - // boolean contract; only the emitted value is stringified.
400 - 'satisfaction_rating_enabled' => apply_filters(
401 - 'mxchat_satisfaction_rating_enabled',
402 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
403 - ) ? 'on' : 'off',
404 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
405 - 'satisfaction_rating_copy' => array(
406 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
407 - 'helpful' => esc_html__('Helpful', 'mxchat'),
408 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
409 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
410 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
411 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
412 - 'send' => esc_html__('Send', 'mxchat'),
413 - 'skip' => esc_html__('Skip', 'mxchat'),
414 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
415 - ),
416 - );
417 -}
418 -
419 -// In your core plugin's check_actions_for_addons method:
420 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
421 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
422 -
423 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
424 -
425 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
426 -
427 - return $result;
428 -}
429 -
430 69 private function mxchat_increment_chat_count() {
431 70 $chat_count = get_option('mxchat_chat_count', 0);
432 71 $chat_count++;
433 72 update_option('mxchat_chat_count', $chat_count);
@@ -439,22 +78,8 @@
439 78 wp_die();
440 79 }
441 80
442 81 $session_id = sanitize_text_field($_POST['session_id']);
443 -
444 - // SECURITY FIX: Verify session ownership before retrieving data
445 - // If IP/user changed, signal frontend to reset session instead of blocking
446 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
447 -
448 - // Check if this session has an owner recorded
449 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
450 -
451 - // Update session owner if it changed (e.g. IP changed due to network switch)
452 - // The session ID itself is the authentication — if the client has it, they own it
453 - if (!$session_owner || $session_owner !== $current_user_identifier) {
454 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
455 - }
456 -
457 82 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
458 83 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
459 84
460 85 if (empty($history)) {
@@ -471,25 +96,26 @@
471 96 'chat_mode' => $chat_mode
472 97 ]);
473 98 wp_die();
474 99 }
475 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
476 - $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 = [];
477 103
478 - // Check persistence setting - when OFF, only include messages from current page load
479 - $options = get_option('mxchat_options', []);
480 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
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 + }
481 111
482 - // Filter history when persistence is OFF to match what the user sees
483 - if (!$persistence_enabled && $session_start_timestamp > 0) {
484 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
485 - // Include messages from this page load onwards
486 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
487 - });
488 - // Re-index array after filtering
489 - $history = array_values($history);
490 - }
112 + return $formatted_history;
113 +}
491 114
115 +
116 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
117 + $history = get_option("mxchat_history_{$session_id}", []);
492 118 $formatted_history = [];
493 119
494 120 // Adjusted for code-heavy conversations
495 121 $max_tokens = 120000; // Context window size
@@ -524,9 +150,9 @@
524 150 if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
525 151 continue;
526 152 }
527 153
528 - // More accurate token estimation (1 token ≈ 4 characters)
154 + // More accurate token estimation (1 token ≈ 4 characters)
529 155 $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
530 156
531 157 // Check token budget with the new estimate
532 158 if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
@@ -561,19 +187,10 @@
561 187 return $formatted_history;
562 188 }
563 189
564 190 public function register_routes() {
565 - //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
191 + error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
566 192
567 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
568 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
569 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
570 - register_rest_route('mxchat/v1', '/nonce', [
571 - 'methods' => 'GET',
572 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
573 - 'permission_callback' => '__return_true',
574 - ]);
575 -
576 193 register_rest_route('mxchat/v1', '/stream', [
577 194 'methods' => 'GET',
578 195 'callback' => [$this, 'mxchat_stream_events'],
579 196 'permission_callback' => [$this, 'verify_chat_session'],
@@ -589,118 +206,19 @@
589 206 'methods' => 'POST',
590 207 'callback' => [$this, 'handle_slack_interaction'],
591 208 'permission_callback' => [$this, 'verify_slack_request'],
592 209 ]);
593 -
594 - register_rest_route('mxchat/v1', '/slack-messages', [
595 - 'methods' => 'POST',
596 - 'callback' => [$this, 'handle_slack_messages'],
597 - 'permission_callback' => [$this, 'verify_slack_request'],
598 - ]);
599 210
600 - // Telegram webhook endpoint
601 - register_rest_route('mxchat/v1', '/telegram-webhook', [
602 - 'methods' => 'POST',
603 - 'callback' => [$this, 'handle_telegram_webhook'],
604 - 'permission_callback' => [$this, 'verify_telegram_request'],
605 - ]);
606 -
607 - //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
211 + error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
608 212 }
609 213
610 214 /**
611 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
612 - * caches it for the session and includes it on every chat-send / stream-send /
613 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
614 - * we eliminate the entire class of "first-message Access denied" failures that
615 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
616 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
617 - * lives in the HTML body.
618 - *
619 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
620 - * client browser can't be used to flood the nonce-issuance path.
621 - *
622 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
623 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
624 - * backwards-compat window so cached pages still in users' browsers don't break
625 - * mid-session.
626 - *
627 - * @since 3.2.7
628 - */
629 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
630 - $ip = '';
631 - if (!empty($_SERVER['REMOTE_ADDR'])) {
632 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
633 - }
634 - if ($ip !== '') {
635 - // Best-effort rate limit. WP transients with sub-second TTL are racy
636 - // (parallel bursts can squeak through before set_transient completes);
637 - // we use 2s to make the gate slightly more reliable. Real production
638 - // rate-limiting at sub-second granularity needs Redis or DB row locks
639 - // — out of scope for this endpoint, which is already cheap.
640 - $key = 'mxchat_nonce_rl_' . md5($ip);
641 - if (get_transient($key)) {
642 - return new WP_REST_Response(array(
643 - 'error' => 'rate_limited',
644 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
645 - ), 429);
646 - }
647 - set_transient($key, 1, 2);
648 - }
649 -
650 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
651 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
652 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
653 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
654 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
655 - if ( ! is_user_logged_in() ) {
656 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
657 - if ( $maybe_uid ) {
658 - wp_set_current_user( $maybe_uid );
659 - }
660 - }
661 -
662 - $payload = array(
663 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
664 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
665 - );
666 -
667 - // plan-32db95: the widget's first-open refresh asks for current behavior
668 - // settings in the same round-trip, so stale inline-localized values on
669 - // cached pages get corrected without a second request. All values in
670 - // this payload already ship in public page HTML — nothing sensitive.
671 - if ($request->get_param('with_settings')) {
672 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
673 - }
674 -
675 - return new WP_REST_Response($payload, 200);
676 -}
677 -
678 -/**
679 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
680 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
681 - * action (inline-localized in older cached HTML). The legacy acceptance is
682 - * a 30-day backwards-compat window — to be removed in a follow-up release
683 - * after 2026-06-27.
684 - *
685 - * @param string $posted_nonce
686 - * @return bool
687 - */
688 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
689 - if (!is_string($posted_nonce) || $posted_nonce === '') {
690 - return false;
691 - }
692 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
693 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
694 -}
695 -
696 -/**
697 215 * Verify valid chat session
698 216 */
699 217 public function verify_chat_session($request) {
700 218 $session_id = $request->get_param('session_id');
701 219 if (empty($session_id)) {
702 - //error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
220 + error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
703 221 return false;
704 222 }
705 223
706 224 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
@@ -717,9 +235,9 @@
717 235 // Get the Slack signing secret from your plugin options
718 236 $valid_key = $this->options['live_agent_secret_key'] ?? '';
719 237
720 238 if (empty($valid_key)) {
721 - //error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
239 + error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
722 240 return false;
723 241 }
724 242
725 243 $timestamp = $request->get_header('X-Slack-Request-Timestamp');
@@ -726,15 +244,14 @@
726 244 $slack_signature = $request->get_header('X-Slack-Signature');
727 245
728 246 // Verify timestamp to prevent replay attacks
729 247 if (abs(time() - intval($timestamp)) > 300) {
730 - //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
248 + error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
731 249 return false;
732 250 }
733 251
734 - // Get raw request body from the WP_REST_Request object
735 - // (php://input may already be consumed by WordPress at this point)
736 - $request_body = $request->get_body();
252 + // Get raw request body
253 + $request_body = file_get_contents('php://input');
737 254
738 255 // Create the signature base string
739 256 $sig_basestring = "v0:{$timestamp}:{$request_body}";
740 257
@@ -743,43 +260,8 @@
743 260
744 261 // Compare signatures
745 262 return hash_equals($my_signature, $slack_signature);
746 263 }
747 -
748 -/**
749 - * Verify request is coming from Telegram.
750 - *
751 - * @param WP_REST_Request $request
752 - * @return bool True if valid, false otherwise.
753 - */
754 -public function verify_telegram_request($request) {
755 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
756 -
757 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
758 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
759 -
760 - if (empty($secret_token)) {
761 - // If no secret is configured, allow the request (for initial setup)
762 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
763 - return true;
764 - }
765 -
766 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
767 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
768 -
769 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
770 -
771 - if (empty($request_token)) {
772 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
773 - return false;
774 - }
775 -
776 - // Timing-safe comparison
777 - $result = hash_equals($secret_token, $request_token);
778 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
779 - return $result;
780 -}
781 -
782 264 public function mxchat_stream_events(WP_REST_Request $request) {
783 265 header('Content-Type: text/event-stream');
784 266 header('Cache-Control: no-cache');
785 267 header('Connection: keep-alive');
@@ -813,100 +295,60 @@
813 295
814 296
815 297
816 298
817 -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) {
818 300 global $wpdb;
301 +
819 302 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
820 - //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
821 -
822 - // Check if this is the first message in a new session (before any other database operations)
823 - $is_new_session = false;
824 - if ($role === 'user') { // Only check for user messages, not bot responses
825 - $existing_messages = $wpdb->get_var($wpdb->prepare(
826 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
827 - $session_id
828 - ));
829 - $is_new_session = ($existing_messages == 0);
830 -
831 - // Log for debugging
832 - if ($is_new_session) {
833 - //error_log("[DEBUG] This is a NEW session - first message");
834 - }
835 - }
836 -
837 - // SECURITY FIX: Set session ownership for new sessions
838 - if ($is_new_session && $role === 'user') {
839 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
840 - $session_owner_key = "mxchat_session_owner_{$session_id}";
841 -
842 - // Only set ownership if not already set
843 - if (!get_option($session_owner_key)) {
844 - update_option($session_owner_key, $current_user_identifier, 'no');
845 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
846 - }
847 - }
848 -
303 + error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
304 +
849 305 // 1) Extract agent name if present
850 306 $agent_name = '';
851 307 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
852 308 $agent_name = $matches[1];
853 309 $message = str_replace("Agent: $agent_name - ", '', $message);
310 +
854 311 $session_meta_key = "mxchat_agent_name_{$session_id}";
855 312 if (empty(get_option($session_meta_key))) {
856 313 update_option($session_meta_key, $agent_name);
857 - //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
314 + error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
858 315 }
859 316 }
860 -
317 +
861 318 // 2) Generate unique message_id
862 319 $message_id = uniqid();
863 - //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
864 -
320 + error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
321 +
865 322 // 3) Determine user_id
866 323 $user_id = is_user_logged_in() ? get_current_user_id() : 0;
867 -
324 +
868 325 // 4) Determine user_identifier
869 326 $user_identifier = $agent_name
870 327 ? $agent_name
871 328 : MxChat_User::mxchat_get_user_identifier();
872 -
329 +
873 330 // 5) Determine displayed_name
874 331 $user_email = MxChat_User::mxchat_get_user_email();
875 332 $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
876 -
333 +
877 334 // 6) Check for a saved email in wp_options
878 335 $email_option_key = "mxchat_email_{$session_id}";
879 336 $saved_email = get_option($email_option_key);
880 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
881 -
882 - // Check for a saved name in wp_options
883 - $name_option_key = "mxchat_name_{$session_id}";
884 - $saved_name = get_option($name_option_key);
885 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
886 -
887 - // If found, update DB user_email and user_name
888 - if ($saved_email || $saved_name) {
889 - $update_data = [];
890 - if ($saved_email) {
891 - $update_data['user_email'] = $saved_email;
892 - }
893 - if ($saved_name) {
894 - $update_data['user_name'] = $saved_name;
895 - }
896 -
897 - if (!empty($update_data)) {
898 - $update_res = $wpdb->update(
899 - $table_name,
900 - $update_data,
901 - ['session_id' => $session_id],
902 - array_fill(0, count($update_data), '%s'),
903 - ['%s']
904 - );
905 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
906 - }
337 + error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
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}");
907 349 }
908 -
350 +
909 351 // 7) Save to session history in wp_options
910 352 $history_key = "mxchat_history_{$session_id}";
911 353 $history = get_option($history_key, []);
912 354 $history[] = [
@@ -915,352 +357,34 @@
915 357 'content' => $message,
916 358 'timestamp' => round(microtime(true) * 1000),
917 359 'agent_name' => $displayed_name,
918 360 ];
919 - update_option($history_key, $history, 'no');
920 - //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
921 -
361 + update_option($history_key, $history);
362 + error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
363 +
922 364 // 8) Save the message to DB (INSERT)
923 365 $insert_data = [
924 366 'user_id' => $user_id,
925 367 'user_identifier'=> $user_identifier,
926 368 'user_email' => $saved_email ?: $user_email,
927 - 'user_name' => $saved_name ?: '', // Add name to insert data
928 369 'session_id' => $session_id,
929 370 'role' => $role,
930 371 'message' => $message,
931 372 'timestamp' => current_time('mysql', 1),
932 373 ];
933 -
934 - // IMPROVED: Handle originating page data
935 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
936 -
937 - if ($columns_exist) {
938 - if ($is_new_session && $role === 'user') {
939 - // For the first user message, set originating page data
940 -
941 - // First check if we have it from the parameter
942 - if ($originating_page && !empty($originating_page['url'])) {
943 - $insert_data['originating_page_url'] = $originating_page['url'];
944 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
945 -
946 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
947 - }
948 - // Otherwise check if it's stored in the instance property
949 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
950 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
951 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
952 -
953 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
954 -
955 - // Clear after using (= null, not unset(): unset() undeclares the property
956 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
957 - $this->pending_originating_page = null;
958 - }
959 - // Fallback to HTTP_REFERER if nothing else is available
960 - else if (isset($_SERVER['HTTP_REFERER'])) {
961 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
962 - $insert_data['originating_page_url'] = $referer_url;
963 -
964 - // Generate title from URL
965 - $parsed_url = parse_url($referer_url);
966 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
967 -
968 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
969 - $insert_data['originating_page_title'] = 'Homepage';
970 - } else {
971 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
972 - $insert_data['originating_page_title'] = ucwords(trim($title));
973 - }
974 -
975 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
976 - }
977 -
978 - // Store for this session so all messages have the same originating page
979 - if (!empty($insert_data['originating_page_url'])) {
980 - update_option("mxchat_originating_page_{$session_id}", [
981 - 'url' => $insert_data['originating_page_url'],
982 - 'title' => $insert_data['originating_page_title']
983 - ], 'no');
984 - }
985 - } else {
986 - // For subsequent messages in the session, use the stored originating page
987 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
988 - if ($stored_originating && !empty($stored_originating['url'])) {
989 - $insert_data['originating_page_url'] = $stored_originating['url'];
990 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
991 - }
992 - }
993 - }
374 + $wpdb->insert($table_name, $insert_data);
375 + error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
994 376
995 - // Add RAG context if provided (for bot messages)
996 - if ($rag_context !== null && $role === 'bot') {
997 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
998 - if ($rag_context_column_exists) {
999 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
1000 - }
1001 - }
1002 -
1003 - $wpdb->insert($table_name, $insert_data);
1004 - //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
1005 -
1006 - // 9) Send notification email if this is the first user message in a new session
1007 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
1008 - $this->send_new_chat_notification($session_id, array(
1009 - 'identifier' => $user_identifier,
1010 - 'email' => $saved_email ?: $user_email,
1011 - 'ip' => $_SERVER['REMOTE_ADDR']
1012 - ));
1013 - }
1014 -
1015 - // 10) Schedule delayed transcript email if enabled and message is from user
1016 - if ($wpdb->insert_id && $role === 'user') {
1017 - $this->schedule_delayed_transcript_email($session_id);
1018 - }
1019 -
1020 - //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
377 + error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1021 378 return $message_id;
1022 379 }
1023 380
1024 -private function send_new_chat_notification($session_id, $user_info = array()) {
1025 - $options = get_option('mxchat_transcripts_options');
1026 -
1027 - // Check if notifications are enabled
1028 - if (empty($options['mxchat_enable_notifications'])) {
1029 - return false;
1030 - }
1031 -
1032 - // Get notification email
1033 - $to = !empty($options['mxchat_notification_email']) ?
1034 - $options['mxchat_notification_email'] :
1035 - get_option('admin_email');
1036 -
1037 - if (!is_email($to)) {
1038 - return false;
1039 - }
1040 -
1041 - // Prepare email content
1042 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1043 -
1044 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1045 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1046 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1047 -
1048 - $message = sprintf(
1049 - "A new chat session has started on your website.\n\n" .
1050 - "Session ID: %s\n" .
1051 - "User: %s\n" .
1052 - "Email: %s\n" .
1053 - "IP Address: %s\n" .
1054 - "Time: %s\n\n" .
1055 - "View transcripts: %s",
1056 - $session_id,
1057 - $user_identifier,
1058 - $user_email,
1059 - $user_ip,
1060 - current_time('mysql'),
1061 - admin_url('admin.php?page=mxchat-transcripts')
1062 - );
1063 -
1064 - // Send email
1065 - return wp_mail($to, $subject, $message);
1066 -}
1067 -
1068 -/**
1069 - * Schedule delayed transcript email for a session
1070 - * Reschedules if a new user message is received
1071 - */
1072 -private function schedule_delayed_transcript_email($session_id) {
1073 - $options = get_option('mxchat_transcripts_options');
1074 -
1075 - // Check if auto-email is enabled
1076 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1077 - return;
1078 - }
1079 -
1080 - // Get notification email
1081 - $email = !empty($options['mxchat_notification_email']) ?
1082 - $options['mxchat_notification_email'] :
1083 - get_option('admin_email');
1084 -
1085 - if (!is_email($email)) {
1086 - return;
1087 - }
1088 -
1089 - // Get delay in minutes (default 30)
1090 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1091 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1092 -
1093 - // Clear any existing scheduled event for this session
1094 - $hook = 'mxchat_send_delayed_transcript';
1095 - $args = array($session_id);
1096 - $timestamp = wp_next_scheduled($hook, $args);
1097 -
1098 - if ($timestamp) {
1099 - wp_unschedule_event($timestamp, $hook, $args);
1100 - }
1101 -
1102 - // Schedule new event
1103 - $schedule_time = time() + ($delay_minutes * 60);
1104 - wp_schedule_single_event($schedule_time, $hook, $args);
1105 -}
1106 -
1107 -/**
1108 - * Check if chat messages contain contact information (email or phone number)
1109 - *
1110 - * @param array $messages Array of message objects with 'message' property
1111 - * @param object|null $session_data Session data object with user_email property
1112 - * @return bool True if contact info found, false otherwise
1113 - */
1114 -private function chat_contains_contact_info($messages, $session_data = null) {
1115 - // Check if session already has a stored email
1116 - if ($session_data && !empty($session_data->user_email)) {
1117 - return true;
1118 - }
1119 -
1120 - // Email regex pattern
1121 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1122 -
1123 - // Phone number patterns (covers various formats including international, WhatsApp style)
1124 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1125 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1126 -
1127 - // Only check user messages (not assistant responses)
1128 - foreach ($messages as $msg) {
1129 - if ($msg->role !== 'user') {
1130 - continue;
1131 - }
1132 -
1133 - $message_text = $msg->message;
1134 -
1135 - // Check for email
1136 - if (preg_match($email_pattern, $message_text)) {
1137 - return true;
1138 - }
1139 -
1140 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1141 - if (preg_match($phone_pattern, $message_text, $matches)) {
1142 - // Count actual digits to avoid matching short numbers
1143 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1144 - if (strlen($digits_only) >= 7) {
1145 - return true;
1146 - }
1147 - }
1148 - }
1149 -
1150 - return false;
1151 -}
1152 -
1153 -/**
1154 - * Send the delayed transcript email with .txt attachment
1155 - */
1156 -public function mxchat_send_delayed_transcript($session_id) {
1157 - global $wpdb;
1158 -
1159 - $options = get_option('mxchat_transcripts_options');
1160 -
1161 - // Get notification email
1162 - $to = !empty($options['mxchat_notification_email']) ?
1163 - $options['mxchat_notification_email'] :
1164 - get_option('admin_email');
1165 -
1166 - if (!is_email($to)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get all messages for this session
1171 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1172 - $messages = $wpdb->get_results($wpdb->prepare(
1173 - "SELECT role, message, timestamp FROM {$table_name}
1174 - WHERE session_id = %s
1175 - ORDER BY timestamp ASC",
1176 - $session_id
1177 - ));
1178 -
1179 - if (empty($messages)) {
1180 - return false;
1181 - }
1182 -
1183 - // Get session metadata
1184 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1185 - $session_data = $wpdb->get_row($wpdb->prepare(
1186 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1187 - $session_id
1188 - ));
1189 -
1190 - // Check if contact info is required and if it's present
1191 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1192 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1193 - // Contact info required but not found - skip sending
1194 - return false;
1195 - }
1196 -
1197 - // Build transcript content
1198 - $transcript_content = "Chat Transcript\n";
1199 - $transcript_content .= "================\n\n";
1200 - $transcript_content .= "Session ID: " . $session_id . "\n";
1201 -
1202 - if ($session_data) {
1203 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1204 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1205 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1206 - }
1207 -
1208 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1209 -
1210 - // Add messages
1211 - foreach ($messages as $msg) {
1212 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1213 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1214 - $transcript_content .= $msg->message . "\n\n";
1215 - }
1216 -
1217 - // Create temporary file for attachment using WP_Filesystem
1218 - $upload_dir = wp_upload_dir();
1219 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1220 - global $wp_filesystem;
1221 - if (empty($wp_filesystem)) {
1222 - require_once ABSPATH . 'wp-admin/includes/file.php';
1223 - WP_Filesystem();
1224 - }
1225 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1226 -
1227 - // Prepare email
1228 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1229 -
1230 - $message = "Please find attached the full chat transcript.\n\n";
1231 - $message .= "Session ID: {$session_id}\n";
1232 -
1233 - if ($session_data) {
1234 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1235 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1236 - }
1237 -
1238 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1239 -
1240 - // Send email with attachment
1241 - $attachments = array($temp_file);
1242 - $result = wp_mail($to, $subject, $message, '', $attachments);
1243 -
1244 - // Clean up temporary file
1245 - if (file_exists($temp_file)) {
1246 - unlink($temp_file);
1247 - }
1248 -
1249 - return $result;
1250 -}
1251 -
1252 -
1253 -
1254 381 public function mxchat_handle_save_email_and_response() {
1255 - //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1256 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
382 + error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1257 383
1258 - nocache_headers();
1259 -
1260 384 // Validate nonce
1261 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1262 - //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'));
1263 387 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1264 388 wp_die();
1265 389 }
1266 390
@@ -1265,41 +389,22 @@
1265 389 }
1266 390
1267 391 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1268 392 $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1269 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1270 393
1271 - //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}");
1272 395
1273 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
1274 - //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
396 + if (empty($session_id) || empty($email)) {
397 + error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1275 398 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1276 399 wp_die();
1277 400 }
1278 401
1279 - // Validate name if provided (check if name field is enabled and name is required)
1280 - $options = get_option('mxchat_options', []);
1281 - $name_field_enabled = isset($options['enable_name_field']) &&
1282 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1283 -
1284 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1285 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1286 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1287 - wp_die();
1288 - }
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}");
1289 406
1290 - // 1) Always store email in wp_options
1291 - $email_option_key = "mxchat_email_{$session_id}";
1292 - update_option($email_option_key, $email, 'no');
1293 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1294 -
1295 - // Store name in wp_options if provided
1296 - if (!empty($name)) {
1297 - $name_option_key = "mxchat_name_{$session_id}";
1298 - update_option($name_option_key, $name, 'no');
1299 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1300 - }
1301 -
1302 407 // 2) (Optional) Also store in DB if a row already exists
1303 408 global $wpdb;
1304 409 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1305 410
@@ -1306,52 +411,41 @@
1306 411 // Make sure we have a valid placeholder in prepare
1307 412 $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", $session_id);
1308 413 $session_count = $wpdb->get_var($sql);
1309 414
1310 - //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
415 + error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1311 416
1312 417 if ($session_count) {
1313 - // Update both user_email and user_name if row(s) exist
1314 - if (!empty($name)) {
1315 - $update_sql = $wpdb->prepare(
1316 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1317 - $email,
1318 - $name,
1319 - $session_id
1320 - );
1321 - } else {
1322 - $update_sql = $wpdb->prepare(
1323 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1324 - $email,
1325 - $session_id
1326 - );
1327 - }
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 + );
1328 424 $wpdb->query($update_sql);
1329 - //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
425 + error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1330 426 } else {
1331 - //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.");
1332 428 }
1333 429
1334 - // Provide success response (same as original)
430 + // Provide success response
1335 431 $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1336 - //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
432 + error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1337 433 wp_send_json_success(['message' => $bot_message]);
1338 434 wp_die();
1339 435 }
1340 436
1341 437 public function mxchat_check_email_provided() {
1342 - //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
438 + error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1343 439
1344 - nocache_headers();
1345 -
1346 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1347 - //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
440 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
441 + error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1348 442 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1349 443 }
1350 444
1351 445 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1352 - if (empty($session_id) || $session_id === 'null') {
1353 - //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
446 + if (empty($session_id)) {
447 + error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1354 448 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1355 449 }
1356 450
1357 451 // Check if the user is logged in
@@ -1356,83 +450,106 @@
1356 450
1357 451 // Check if the user is logged in
1358 452 if (is_user_logged_in()) {
1359 453 $current_user = wp_get_current_user();
1360 - //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1361 -
1362 - // Get user's display name for logged in users
1363 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1364 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1365 -
1366 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1367 - if (!empty($user_name)) {
1368 - $response_data['name'] = $user_name;
1369 - }
1370 -
1371 - wp_send_json_success($response_data);
454 + error_log("[DEBUG] User is logged in as {$current_user->user_email}");
455 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
1372 456 }
1373 457
1374 - // Check if name field is required
1375 - $options = get_option('mxchat_options', []);
1376 - $name_field_enabled = isset($options['enable_name_field']) &&
1377 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
458 + $option_key = "mxchat_email_{$session_id}";
459 + $stored_email = get_option($option_key, '');
1378 460
1379 - $email_option_key = "mxchat_email_{$session_id}";
1380 - $stored_email = get_option($email_option_key, '');
1381 -
1382 - // Check for stored name
1383 - $name_option_key = "mxchat_name_{$session_id}";
1384 - $stored_name = get_option($name_option_key, '');
461 + error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
1385 462
1386 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1387 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
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 +}
1388 471
1389 - // Check if we have email and name (if name is required)
1390 - $has_required_info = !empty($stored_email);
1391 -
1392 - if ($name_field_enabled) {
1393 - $has_required_info = $has_required_info && !empty($stored_name);
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';
1394 480 }
1395 481
1396 - if ($has_required_info) {
1397 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1398 -
1399 - $response_data = ['email' => $stored_email];
1400 - if (!empty($stored_name)) {
1401 - $response_data['name'] = $stored_name;
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);
1402 507 }
1403 -
1404 - wp_send_json_success($response_data);
1405 - } else {
1406 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
1407 - wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1408 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;
1409 513 }
1410 514
1411 -/**
1412 - * Send error response in appropriate format based on streaming mode
1413 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1414 - *
1415 - * @param string $error_message The error message to display
1416 - * @param string $error_code Optional error code for debugging
1417 - */
1418 -private function send_error_response($error_message, $error_code = 'api_error') {
1419 - if ($this->is_streaming) {
1420 - echo "data: " . json_encode([
1421 - 'error' => true,
1422 - 'error_message' => $error_message,
1423 - 'error_code' => $error_code,
1424 - 'text' => $error_message,
1425 - 'message' => $error_message
1426 - ]) . "\n\n";
1427 - echo "data: [DONE]\n\n";
1428 - flush();
1429 - } else {
1430 - wp_send_json_error([
1431 - 'error_message' => $error_message,
1432 - 'error_code' => $error_code
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' => []
1433 533 ]);
534 + wp_die();
1434 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 + ]);
1435 552 wp_die();
1436 553 }
1437 554
1438 555 public function mxchat_handle_chat_request() {
@@ -1437,30 +554,10 @@
1437 554
1438 555 public function mxchat_handle_chat_request() {
1439 556 global $wpdb;
1440 557
1441 - // Debug: Log incoming bot_id
1442 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1443 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1444 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1445 -
1446 - // Get bot-specific options
1447 - $bot_options = $this->get_bot_options($bot_id);
1448 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1449 558
1450 - // Check if this is a streaming request
1451 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1452 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1453 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1454 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1455 -
1456 - // ADDED: Store streaming state in class property for use in private methods
1457 - $this->is_streaming = $is_streaming;
1458 -
1459 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1460 - // This allows actions/forms to return JSON responses without header conflicts
1461 -
1462 - // Check if MX Chat Moderation is active
559 + // Check if MX Chat Moderation is active
1463 560 if (class_exists('MX_Chat_Moderation')) {
1464 561 // Get user email and IP
1465 562 $user_email = '';
1466 563 $user_ip = $_SERVER['REMOTE_ADDR'];
@@ -1494,16 +591,14 @@
1494 591 wp_die();
1495 592 }
1496 593 }
1497 594
595 +
596 + // Reset fallback response at the start of each request
1498 597 $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1499 598 $this->productCardHtml = '';
1500 - // Reset the per-turn function-calling UI capture (plan 48a57a).
1501 - $this->fc_ui_html = '';
1502 - $this->fc_ui_images = array();
1503 - $this->fc_ui_captured = false;
1504 599
1505 - // Get the actual WordPress user ID if logged in
600 + // Get the actual WordPress user ID if logged in
1506 601 $is_logged_in = is_user_logged_in();
1507 602 if ($is_logged_in) {
1508 603 $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1509 604 } else {
@@ -1513,336 +608,201 @@
1513 608
1514 609 // Get and sanitize the user identifier
1515 610 $user_id = sanitize_key($user_id);
1516 611
1517 - // Check rate limit using new settings structure
1518 - $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'));
1519 615
1520 - if ($rate_limit_result !== true) {
1521 - wp_send_json([
1522 - 'success' => false,
1523 - 'message' => $rate_limit_result['message'],
1524 - 'status' => 'rate_limit_exceeded'
1525 - ]);
1526 - wp_die();
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);
1527 661 }
1528 662
1529 663 // Rest of your existing code...
1530 664 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
665 + error_log("Session ID: $session_id");
1531 666
1532 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1533 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1534 - // the frontend FormData.append() to stringify a null session_id into the literal
1535 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1536 - // ghost sessions that group every visitor's first message under one row.
1537 - if ($session_id === 'null' || $session_id === 'undefined') {
1538 - $session_id = '';
1539 - }
1540 -
1541 667 if (empty($session_id)) {
668 + error_log("Error: Session ID is missing.");
1542 669 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1543 670 wp_die();
1544 671 }
1545 672
1546 - // Update session owner if it changed (e.g. IP changed due to network switch)
1547 - // The session ID itself is the authentication — if the client has it, they own it
1548 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1549 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1550 -
1551 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1552 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1553 - }
1554 -
1555 673 // Validate and sanitize the incoming message
1556 674 if (empty($_POST['message'])) {
675 + error_log("Error: No message received.");
1557 676 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1558 677 wp_die();
1559 678 }
1560 -
1561 -
1562 - // Track originating page for first message in session
1563 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1564 679
1565 - // Check if originating page columns exist
1566 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1567 680
1568 - if ($columns_exist) {
1569 - // Check if this session already has messages
1570 - $message_count = $wpdb->get_var($wpdb->prepare(
1571 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1572 - $session_id
1573 - ));
1574 -
1575 - // If this is the first message in the session
1576 - if ($message_count == 0) {
1577 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1578 - $originating_url = '';
1579 - $originating_title = '';
1580 -
1581 - // Try to get from POST data first (sent by JavaScript)
1582 - if (isset($_POST['current_page_url'])) {
1583 - $originating_url = esc_url_raw($_POST['current_page_url']);
1584 - $originating_title = isset($_POST['current_page_title'])
1585 - ? sanitize_text_field($_POST['current_page_title'])
1586 - : '';
1587 - }
1588 - // Fallback to HTTP_REFERER if not provided by JavaScript
1589 - else if (isset($_SERVER['HTTP_REFERER'])) {
1590 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1591 - }
1592 -
1593 - // Generate title if we have URL but no title
1594 - if ($originating_url && empty($originating_title)) {
1595 - $parsed_url = parse_url($originating_url);
1596 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1597 -
1598 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1599 - $originating_title = 'Homepage';
1600 - } else {
1601 - // Clean up the path to make a readable title
1602 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1603 - $originating_title = ucwords(trim($originating_title));
1604 - }
1605 - }
1606 -
1607 - // Store for later use when saving the message
1608 - $this->pending_originating_page = [
1609 - 'url' => $originating_url,
1610 - 'title' => $originating_title
1611 - ];
1612 - }
1613 - }
1614 -
1615 -
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 +];
1616 688
1617 - // Get page context if provided
1618 - $page_context = null;
1619 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1620 - $page_context_raw = stripslashes($_POST['page_context']);
1621 - $page_context = json_decode($page_context_raw, true);
1622 -
1623 - // Validate page context structure
1624 - if (is_array($page_context) &&
1625 - isset($page_context['url']) &&
1626 - isset($page_context['title']) &&
1627 - isset($page_context['content'])) {
1628 -
1629 - // Sanitize page context
1630 - $page_context['url'] = esc_url_raw($page_context['url']);
1631 - $page_context['title'] = sanitize_text_field($page_context['title']);
1632 - $page_context['content'] = wp_kses_post($page_context['content']);
1633 - } else {
1634 - $page_context = null;
1635 - }
1636 - }
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']);
1637 693
1638 - // Modify the message sanitization to preserve PHP tags in code blocks
1639 - $allowed_tags = [
1640 - 'pre' => [],
1641 - 'code' => ['class' => true],
1642 - 'span' => ['class' => true],
1643 - 'div' => ['class' => true],
1644 - ];
694 +// Then apply sanitization
695 +$message = wp_kses($message, $allowed_tags);
1645 696
1646 - // First preserve code blocks
1647 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1648 - return htmlspecialchars_decode($matches[0]);
1649 - }, $_POST['message']);
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);
1650 701
1651 - // Then apply sanitization
1652 - $message = wp_kses($message, $allowed_tags);
702 +$message = trim($message);
1653 703
1654 - // Preserve code blocks from markdown conversion
1655 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1656 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
704 +// Preserve code blocks from markdown conversion
705 +$message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1657 706
1658 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1659 - // Always initialize testing data for admins (no toggle needed)
1660 - $testing_data = null;
1661 - if (current_user_can('administrator')) {
1662 - // For vision messages, use the original user message for the query display
1663 - $query_for_testing = $message;
1664 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1665 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1666 - }
1667 -
1668 - $testing_data = [
1669 - 'query' => $query_for_testing,
1670 - 'timestamp' => time(),
1671 - 'top_matches' => [],
1672 - 'action_matches' => [], // Initialize action matches array
1673 - 'page_context' => $page_context, // Include page context in testing data
1674 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1675 - 'bot_id' => $bot_id // Include bot ID in testing data
1676 - ];
1677 -
1678 - // Get similarity threshold from bot options or default options
1679 - $similarity_threshold = isset($current_options['similarity_threshold'])
1680 - ? ((int) $current_options['similarity_threshold']) / 100
1681 - : 0.35;
1682 -
1683 - $testing_data['similarity_threshold'] = $similarity_threshold;
1684 -
1685 - // Determine knowledge base type using bot-specific config
1686 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1687 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1688 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1689 - }
1690 - // ===== 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);
1691 709
1692 - // Add debug before and after:
1693 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1694 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1695 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
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 +}
1696 728
729 + // Save the user's message
730 + $this->mxchat_save_chat_message($session_id, 'user', $message);
1697 731
1698 - // If the pre-processing returned a result (not the original message), use it directly
1699 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1700 - // Save the AI response
1701 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1702 -
1703 - // Save HTML content if provided
1704 - if (!empty($pre_processed_result['html'])) {
1705 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1706 - }
1707 -
1708 - // Add testing data if admin
1709 - $response_data = [
1710 - 'text' => $pre_processed_result['text'],
1711 - 'html' => $pre_processed_result['html'] ?? '',
1712 - 'session_id' => $session_id
1713 - ];
1714 -
1715 - if ($testing_data !== null) {
1716 - $response_data['testing_data'] = $testing_data;
1717 - }
1718 -
1719 - wp_send_json($response_data);
1720 - wp_die();
1721 - }
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);
1722 736
1723 - // Save the user's message - handle vision processed messages differently
1724 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1725 - // For vision messages, save the original user message with image indicator
1726 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1727 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1728 - $image_count = intval($_POST['vision_images_count']);
1729 - $original_message .= " [{$image_count} image(s)]";
1730 - }
1731 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1732 - } else {
1733 - // Regular message - save as normal
1734 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1735 - }
737 + // Send success response
738 + $response_message = $this->options['email_capture_response'] ??
739 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
1736 740
1737 -
1738 - if (is_email($message)) {
1739 - // Add the email to Loops
1740 - $this->add_email_to_loops($message);
1741 -
1742 - // Get the user's success message instruction using current_options
1743 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1744 -
1745 - // Set instruction for AI using the user's success message
1746 - $this->current_action_instruction = $user_success_message;
1747 -
1748 - // Clear the email capture transient since we got the email
1749 - delete_transient('mxchat_email_capture_' . $user_id);
1750 - }
1751 -
1752 - // Check if we're in an email capture flow but user hasn't provided email yet
1753 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1754 - // Check if the message contains an email (not the whole message being an email)
1755 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1756 - $extracted_email = $matches[0];
1757 -
1758 - // Add the extracted email to Loops
1759 - $this->add_email_to_loops($extracted_email);
1760 -
1761 - // Get the user's success message instruction using current_options
1762 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1763 -
1764 - // Set instruction for AI using the user's success message
1765 - $this->current_action_instruction = $user_success_message;
1766 -
1767 - // Clear the email capture transient since we got the email
1768 - delete_transient('mxchat_email_capture_' . $user_id);
1769 - }
1770 - // If no email found but we're in capture mode, remind them
1771 - else {
1772 - // Get the original instruction to remind them using current_options
1773 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1774 - $this->current_action_instruction = $original_instruction;
1775 - }
1776 - }
741 + wp_send_json([
742 + 'success' => true,
743 + 'status' => 'email_captured',
744 + 'message' => $response_message
745 + ]);
746 + wp_die();
747 + }
1777 748
1778 - $intent_info = '';
749 + $intent_info = '';
1779 750
1780 - // Check chat mode
1781 - $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");
1782 754
1783 - // Handle agent mode
1784 755 // Handle agent mode
1785 - if ($chat_mode === 'agent') {
1786 - // First, check for switch intent before doing anything else
1787 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
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);
1788 759
1789 - // Capture action analysis for testing panel after intent check
1790 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1791 - $testing_data['action_matches'] = $this->last_action_analysis;
1792 - }
1793 -
1794 - // Around line 506, in the agent mode handling section:
1795 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1796 - // Update chat mode first
1797 - update_option("mxchat_mode_{$session_id}", 'ai');
1798 -
1799 - // Clear any existing PDF context to start fresh
1800 - $this->clear_pdf_transients($session_id);
1801 -
1802 - // Prepare clean switch response with explicit chat_mode
1803 - $response_data = [
1804 - 'text' => $this->fallbackResponse['text'],
1805 - 'html' => $this->fallbackResponse['html'] ?? '',
1806 - 'session_id' => $session_id,
1807 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1808 - ];
1809 -
1810 - if ($testing_data !== null) {
1811 - $response_data['testing_data'] = $testing_data;
1812 - }
1813 -
1814 - // Save the mode switch message
1815 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1816 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1817 -
1818 - // Send response and exit
1819 - wp_send_json($response_data);
1820 - wp_die();
1821 - } elseif (!$intent_matched) {
1822 - // No intent matched, handle live agent message
1823 - try {
1824 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
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");
1825 763
1826 - $agent_response = [
1827 - 'status' => 'waiting_for_agent',
1828 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1829 - ];
1830 -
1831 - if ($testing_data !== null) {
1832 - $agent_response['testing_data'] = $testing_data;
1833 - }
764 + // Update chat mode first
765 + update_option("mxchat_mode_{$session_id}", 'ai');
1834 766
1835 - wp_send_json_success($agent_response);
1836 - } catch (\Exception $e) {
1837 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1838 - }
1839 - wp_die();
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'));
1840 798 }
799 + wp_die();
1841 800 }
801 + }
1842 802
1843 803 // Step 1: Check for new PDF URL in the message
1844 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
804 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1845 805 $new_pdf_url = $matches[0];
1846 806
1847 807 // Check if this is likely a PDF-related request
1848 808 $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
@@ -1864,15 +824,15 @@
1864 824
1865 825 // Clear previous PDF transients
1866 826 $this->clear_pdf_transients($session_id);
1867 827
1868 - // Process new PDF using current_options
1869 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
828 + // Process new PDF
829 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
1870 830 $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1871 831
1872 832 if ($embeddings === 'too_many_pages') {
1873 833 $error_text = sprintf(
1874 - $current_options['pdf_intent_error_text'] ??
834 + $this->options['pdf_intent_error_text'] ??
1875 835 esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1876 836 $max_pages
1877 837 );
1878 838 $this->fallbackResponse['text'] = $error_text;
@@ -1877,13 +837,15 @@
1877 837 );
1878 838 $this->fallbackResponse['text'] = $error_text;
1879 839 } elseif ($embeddings) {
1880 840 // Store new PDF information
841 + // Create a more meaningful filename from URL
1881 842 $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1882 843
1883 - // 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
1884 845 if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1885 846 strpos($pdf_filename, '.php') !== false) {
847 + // Create a timestamp-based name
1886 848 $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1887 849 }
1888 850
1889 851 set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
@@ -1890,257 +852,99 @@
1890 852 set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1891 853 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1892 854 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1893 855
1894 - $success_text = $current_options['pdf_intent_success_text'] ??
856 + $success_text = $this->options['pdf_intent_success_text'] ??
1895 857 esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1896 858
1897 - $pdf_response = [
859 + // Return success with filename for UI update
860 + wp_send_json([
1898 861 'success' => true,
1899 862 'message' => $success_text,
1900 863 'data' => [
1901 864 'filename' => $pdf_filename
1902 865 ]
1903 - ];
1904 -
1905 - if ($testing_data !== null) {
1906 - $pdf_response['testing_data'] = $testing_data;
1907 - }
1908 -
1909 - wp_send_json($pdf_response);
866 + ]);
1910 867 wp_die();
1911 868 } else {
1912 - $error_text = $current_options['pdf_intent_error_text'] ??
869 + $error_text = $this->options['pdf_intent_error_text'] ??
1913 870 esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1914 871 $this->fallbackResponse['text'] = $error_text;
1915 872 }
1916 873
1917 - $pdf_error_response = [
874 + wp_send_json([
1918 875 'success' => false,
1919 876 'message' => $this->fallbackResponse['text']
1920 - ];
1921 -
1922 - if ($testing_data !== null) {
1923 - $pdf_error_response['testing_data'] = $testing_data;
1924 - }
1925 -
1926 - wp_send_json($pdf_error_response);
877 + ]);
1927 878 wp_die();
1928 879 }
1929 880 }
1930 881 }
1931 882
883 + // Step 2: Detect intent and handle intent-based responses
884 +$intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
885 +error_log("Intent Result Type: " . gettype($intent_result));
1932 886
1933 - // Step 2: Detect intent and handle intent-based responses
1934 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1935 -
1936 - // Capture action analysis for testing panel after intent check
1937 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1938 - $testing_data['action_matches'] = $this->last_action_analysis;
1939 - }
1940 -
1941 - // Step 3: Handle the intent result appropriately
1942 - if ($intent_result !== false) {
1943 - // Intent was matched - ALWAYS send as JSON response, never streaming
1944 -
1945 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1946 - // Intent returned a direct response array
1947 - $response_data = [
1948 - 'text' => $intent_result['text'] ?? '',
1949 - 'html' => $intent_result['html'] ?? '',
1950 - 'session_id' => $session_id
1951 - ];
1952 -
1953 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1954 - if (isset($intent_result['chat_mode'])) {
1955 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1956 - }
1957 -
1958 - if ($testing_data !== null) {
1959 - $response_data['testing_data'] = $testing_data;
1960 - }
1961 -
1962 - wp_send_json($response_data);
1963 - wp_die();
1964 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1965 - // Intent returned true and set fallbackResponse
1966 -
1967 - // SAVE TO TRANSCRIPT
1968 - if (!empty($this->fallbackResponse['text'])) {
1969 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1970 - }
1971 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1972 - if (!empty($this->fallbackResponse['html'])) {
1973 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1974 - }
1975 -
1976 - $response_data = [
1977 - 'text' => $this->fallbackResponse['text'] ?? '',
1978 - 'html' => $this->fallbackResponse['html'] ?? '',
1979 - 'session_id' => $session_id
1980 - ];
1981 -
1982 - if (isset($this->fallbackResponse['chat_mode'])) {
1983 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
1984 - }
1985 -
1986 - if ($testing_data !== null) {
1987 - $response_data['testing_data'] = $testing_data;
1988 - }
1989 -
1990 - wp_send_json($response_data);
1991 - wp_die();
1992 - }
1993 - }
1994 -
1995 - // If we get here, no intent matched OR the intent didn't provide a usable response
1996 -
1997 - // Step 4: Generate AI response
1998 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
1999 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
2000 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
2001 - $this->mxchat_increment_chat_count();
887 +// Step 3: Handle the intent result appropriately
888 +if ($intent_result !== false) {
889 + // The intent was matched and handled
890 + error_log("Intent was matched and handled.");
891 +
892 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
893 + // Intent returned a direct response array
894 + error_log("Intent returned a direct response.");
895 + $response_data = [
896 + 'text' => $intent_result['text'] ?? '',
897 + 'html' => $intent_result['html'] ?? '',
898 + 'session_id' => $session_id
899 + ];
2002 900
2003 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
2004 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
2005 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
901 + wp_send_json($response_data);
902 + wp_die();
903 + }
904 + else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
905 + // Intent returned true and set fallbackResponse
906 + error_log("Intent returned true with fallbackResponse set.");
907 + $response_data = [
908 + 'text' => $this->fallbackResponse['text'] ?? '',
909 + 'html' => $this->fallbackResponse['html'] ?? '',
910 + 'session_id' => $session_id
911 + ];
2006 912
2007 - // Check if the embedding generation returned an error
2008 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
2009 - $error_message = $user_message_embedding['error'];
2010 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
913 + wp_send_json($response_data);
914 + wp_die();
915 + }
916 +
917 + // Intent was matched but no usable response was provided
918 + // This shouldn't happen with proper intent implementation
919 + error_log("Warning: Intent matched but no response provided.");
920 +}
2011 921
2012 - // FIXED: Send error in appropriate format based on streaming mode
2013 - if ($is_streaming) {
2014 - echo "data: " . json_encode([
2015 - 'error' => true,
2016 - 'error_message' => $error_message,
2017 - 'error_code' => $error_code,
2018 - 'text' => $error_message,
2019 - 'message' => $error_message
2020 - ]) . "\n\n";
2021 - echo "data: [DONE]\n\n";
2022 - flush();
2023 - } else {
2024 - wp_send_json_error([
2025 - 'error_message' => $error_message,
2026 - 'error_code' => $error_code
2027 - ]);
2028 - }
2029 - wp_die();
2030 - }
922 +// If we get here, no intent matched OR the intent didn't provide a usable response
923 +error_log("No matching intent or usable response. Generating AI response.");
2031 924
2032 - // Check if the embedding is valid
2033 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2034 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
925 + // Step 4: Generate AI response
926 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
927 + $this->mxchat_increment_chat_count();
2035 928
2036 - // FIXED: Send error in appropriate format based on streaming mode
2037 - if ($is_streaming) {
2038 - echo "data: " . json_encode([
2039 - 'error' => true,
2040 - 'error_message' => $error_message,
2041 - 'error_code' => 'invalid_embedding',
2042 - 'text' => $error_message,
2043 - 'message' => $error_message
2044 - ]) . "\n\n";
2045 - echo "data: [DONE]\n\n";
2046 - flush();
2047 - } else {
2048 - wp_send_json_error([
2049 - 'error_message' => $error_message,
2050 - 'error_code' => 'invalid_embedding'
2051 - ]);
2052 - }
2053 - wp_die();
2054 - }
929 + // Generate embedding for the user's query
930 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
931 + if (!is_array($user_message_embedding)) {
932 + error_log("Failed to generate message embedding for session $session_id");
933 + wp_send_json_error(esc_html__('Error processing your message.', 'mxchat'));
934 + wp_die();
935 + }
2055 936
2056 - // Build context with both knowledge base and PDF content if available
2057 - $context_content = "User asked: '{$message}'\n\n";
2058 -
2059 - // Add action instruction if present (add this right after the above line)
2060 - if (!empty($this->current_action_instruction)) {
2061 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2062 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2063 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2064 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2065 -
2066 - // Clear the instruction after using it
2067 - $this->current_action_instruction = null;
2068 - }
937 + // Build context with both knowledge base and PDF content if available
938 + $context_content = "User asked: '{$message}'\n\n";
2069 939
940 + // Get relevant content from knowledge base
941 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
942 + if (!empty($relevant_content)) {
943 + $context_content .= "Relevant content from knowledge database:\n" . $relevant_content . "\n\n";
944 + }
2070 945
2071 - // Add page context if available and contextual awareness is enabled using current_options
2072 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2073 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2074 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2075 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2076 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2077 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2078 - }
2079 946
2080 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2081 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2082 -
2083 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2084 - // Use fresh options to ensure we get the latest setting value
2085 - $fresh_options = get_option('mxchat_options', []);
2086 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2087 -
2088 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2089 - if ($citation_links_enabled && !empty($system_instructions)) {
2090 - preg_match_all(
2091 - '#\bhttps?://[^\s<>"\']+#i',
2092 - $system_instructions,
2093 - $system_instruction_urls
2094 - );
2095 -
2096 - if (!empty($system_instruction_urls[0])) {
2097 - // Merge with existing valid URLs
2098 - $this->current_valid_urls = array_merge(
2099 - $this->current_valid_urls,
2100 - $system_instruction_urls[0]
2101 - );
2102 - // Remove duplicates
2103 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2104 -
2105 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2106 - }
2107 - }
2108 -
2109 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2110 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2111 - // Update testing data with the REAL similarity analysis
2112 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2113 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2114 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2115 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2116 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2117 -}
2118 -// ===== END SIMILARITY DATA CAPTURE =====
2119 -
2120 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2121 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2122 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2123 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2124 -}
2125 -
2126 - if (!empty($relevant_content)) {
2127 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2128 - } else {
2129 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2130 - }
2131 -
2132 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2133 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2134 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2135 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2136 - foreach ($this->current_valid_urls as $url) {
2137 - $context_content .= "- " . $url . "\n";
2138 - }
2139 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2140 - $context_content .= "===== END APPROVED URLS =====\n\n";
2141 - }
2142 -
2143 947 // Check for and include PDF content
2144 948 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2145 949 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2146 950 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2171,329 +975,64 @@
2171 975 }
2172 976
2173 977 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2174 978
2175 - // Extract model from current options for bot-specific model support
2176 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2177 -
2178 - // ===== Native function-calling fallback (plan-mxchat-20260617-a41dee) =====
2179 - // Intents already missed (we're past the intent router). If function
2180 - // calling is enabled and the active model is tool-capable, let the model
2181 - // SELECT and run registered callbacks as tools — independent of intents,
2182 - // works with zero Actions. The tool round is buffered; the final answer is
2183 - // emitted via the SAME envelopes the normal path uses. Default-off, so
2184 - // existing installs never enter this branch.
2185 - if ($this->mxchat_fc_should_run($selected_model)) {
2186 - $fc_outcome = $this->mxchat_fc_attempt(
2187 - $message,
2188 - $context_content,
2189 - $conversation_history,
2190 - $selected_model,
2191 - $current_options,
2192 - $session_id,
2193 - $user_id
2194 - );
2195 - if (is_array($fc_outcome) && !empty($fc_outcome['handled'])) {
2196 - $fc_text = isset($fc_outcome['text']) ? $fc_outcome['text'] : '';
2197 - if (!empty($this->current_valid_urls)) {
2198 - $fc_text = $this->validate_and_clean_urls($fc_text, $this->current_valid_urls);
2199 - }
2200 - // plan-mxchat-20260617-48a57a — surface any UI element a tool
2201 - // produced (generated image / product card / image gallery) so the
2202 - // widget RENDERS it, instead of emitting only the model's text.
2203 - // The html was already saved to the transcript in
2204 - // mxchat_fc_execute_tool (or by the callback itself for self-saving
2205 - // core tools), so we persist ONLY the model's caption text here.
2206 - $fc_html = isset($this->fc_ui_html) ? $this->fc_ui_html : '';
2207 -
2208 - if ($fc_text !== '') {
2209 - $this->mxchat_save_chat_message($session_id, 'bot', $fc_text, null, null);
2210 - }
2211 -
2212 - if ($is_streaming) {
2213 - // The frontend SSE reader routes any event carrying text/html
2214 - // to handleNonStreamResponse(), which renders text + html in a
2215 - // single bot message — so emit one complete event (mirrors the
2216 - // intent path's text/html envelope).
2217 - $sse = array('session_id' => $session_id);
2218 - if ($fc_text !== '') $sse['text'] = $fc_text;
2219 - if ($fc_html !== '') $sse['html'] = $fc_html;
2220 - if ($fc_text === '' && $fc_html === '') $sse['text'] = $this->mxchat_fc_giveup_text();
2221 - echo "data: " . wp_json_encode($sse) . "\n\n";
2222 - echo "data: [DONE]\n\n";
2223 - flush();
2224 - } else {
2225 - $fc_response_data = array('text' => $fc_text, 'html' => $fc_html, 'session_id' => $session_id);
2226 - if ($testing_data !== null) {
2227 - $fc_response_data['testing_data'] = $testing_data;
2228 - }
2229 - wp_send_json($fc_response_data);
2230 - }
2231 - wp_die();
2232 - }
2233 - }
2234 - // ===== end function-calling fallback =====
2235 -
979 + // Generate the response using the full context
2236 980 $response = $this->mxchat_generate_response(
2237 981 $context_content,
2238 - $current_options['api_key'] ?? $this->options['api_key'],
2239 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2240 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2241 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2242 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2243 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2244 - $conversation_history,
2245 - $is_streaming,
2246 - $session_id,
2247 - $testing_data,
2248 - $selected_model
982 + $this->options['api_key'],
983 + $this->options['xai_api_key'],
984 + $this->options['claude_api_key'],
985 + $this->options['deepseek_api_key'],
986 + $this->options['gemini_api_key'], // Added Gemini API key
987 + $conversation_history
2249 988 );
2250 -
2251 - // Handle streaming vs non-streaming responses
2252 - if ($is_streaming) {
2253 - // Check if streaming actually happened or if it fell back to regular response
2254 - if ($response === true) {
2255 - wp_die();
2256 - }
2257 - // If we get here, streaming fell back to regular response, continue
2258 - // But if there's an error, we need to send it as SSE format since headers are already set
2259 - if (is_array($response) && isset($response['error'])) {
2260 - $error_message = $response['error'];
2261 - $error_code = $response['error_code'] ?? 'api_error';
2262 - // Send error in SSE format that the client JS can handle
2263 - echo "data: " . json_encode([
2264 - 'error' => true,
2265 - 'error_message' => $error_message,
2266 - 'error_code' => $error_code,
2267 - 'text' => $error_message, // Also include as text for fallback handling
2268 - 'message' => $error_message
2269 - ]) . "\n\n";
2270 - echo "data: [DONE]\n\n";
2271 - flush();
2272 - wp_die();
2273 - }
2274 - }
2275 989
2276 - // Check if the response is an error array (non-streaming mode)
2277 - if (is_array($response) && isset($response['error'])) {
2278 - wp_send_json_error([
2279 - 'error_message' => $response['error'],
2280 - 'error_code' => $response['error_code'] ?? 'api_error'
2281 - ]);
2282 - wp_die();
2283 - }
2284 -
2285 - // DEBUG: Check what we have
2286 - //error_log("=== BEFORE URL VALIDATION ===");
2287 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2288 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2289 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2290 -
2291 - // If we get here, the response is valid text - now validate URLs
2292 - if (!empty($this->current_valid_urls)) {
2293 - //error_log("CALLING validate_and_clean_urls");
2294 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2295 - } else {
2296 - //error_log("SKIPPING validation - current_valid_urls is empty");
2297 - }
2298 - // ===== END URL VALIDATION =====
990 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2299 991
2300 - // Prepare RAG context data for storage (only include documents used for context)
2301 - $rag_context_for_storage = null;
2302 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2303 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
992 + // Step 5: Save additional content if available
993 + if (!empty($this->productCardHtml)) {
994 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
995 + }
2304 996
2305 - if ($has_rag_data || $has_action_data) {
2306 - $rag_context_for_storage = [];
997 + if (!empty($this->fallbackResponse['html'])) {
998 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
999 + }
2307 1000
2308 - // Add RAG/source data if available
2309 - if ($has_rag_data) {
2310 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2311 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2312 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2313 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2314 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2315 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2316 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2317 - }
1001 + // Step 6: Return the response
1002 + $response_data = [
1003 + 'text' => $response,
1004 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1005 + 'session_id' => $session_id
1006 + ];
2318 1007
2319 - // Add action analysis data if available
2320 - if ($has_action_data) {
2321 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2322 - }
2323 - }
2324 -
2325 - // Save the cleaned response with RAG context
2326 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2327 -
2328 - // Step 5: Save additional content if available
2329 - if (!empty($this->productCardHtml)) {
2330 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2331 - }
2332 -
2333 - if (!empty($this->fallbackResponse['html'])) {
2334 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2335 - }
2336 -
2337 - // Step 6: Return the response
2338 - // DEBUG: Check if newlines exist in the response
2339 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2340 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2341 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2342 -
2343 - $response_data = [
2344 - 'text' => $response,
2345 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2346 - 'session_id' => $session_id
2347 - ];
2348 -
2349 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2350 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2351 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2352 - }
2353 -
2354 - // Also pass it as a top-level field so JS can show a better error message to admins
2355 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2356 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2357 - }
2358 -
2359 - // Always add testing data for admins (no toggle needed)
2360 - if ($testing_data !== null) {
2361 - $response_data['testing_data'] = $testing_data;
2362 - }
2363 -
2364 - wp_send_json($response_data);
2365 - wp_die();
1008 + wp_send_json($response_data);
1009 + wp_die();
2366 1010 }
2367 1011
2368 -/**
2369 - * Get bot-specific options for multi-bot functionality
2370 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2371 - */
2372 -// Also debug the bot options retrieval
2373 -private function get_bot_options($bot_id = 'default') {
2374 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2375 -
2376 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2377 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2378 - return array();
2379 - }
2380 -
2381 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2382 -
2383 - if (!empty($bot_options)) {
2384 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2385 - if (isset($bot_options['similarity_threshold'])) {
2386 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2387 - }
2388 - }
2389 -
2390 - return is_array($bot_options) ? $bot_options : array();
2391 -}
2392 -
2393 -/**
2394 - * Get bot-specific Pinecone configuration
2395 - * Used in the knowledge retrieval functions
2396 - */
2397 -// Also add debugging to your get_bot_pinecone_config function
2398 -private function get_bot_pinecone_config($bot_id = 'default') {
2399 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2400 -
2401 - // If default bot or multi-bot add-on not active, use default Pinecone config
2402 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2403 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2404 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2405 - $config = array(
2406 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2407 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2408 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2409 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2410 - );
2411 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2412 - return $config;
2413 - }
2414 -
2415 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2416 -
2417 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2418 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2419 -
2420 - if (!empty($bot_pinecone_config)) {
2421 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2422 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2423 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2424 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2425 - } else {
2426 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2427 - }
2428 -
2429 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2430 -}
2431 -
2432 -
1012 +// New function to check intents and invoke the callback function
2433 1013 // Updated function to check intents and invoke the callback function
2434 1014 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2435 1015 global $wpdb;
2436 1016 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2437 1017
2438 - // Get the current bot_id
2439 - $current_bot_id = $this->get_current_bot_id($session_id);
1018 + error_log('🔍 MXCHAT DEBUG: Intent Check Started ==================');
1019 + error_log("🔍 MXCHAT DEBUG: Message: '$message'");
1020 + error_log("🔍 MXCHAT DEBUG: Chat Mode: $chat_mode");
2440 1021
2441 1022 // Generate the user embedding
1023 + error_log('🔄 MXCHAT DEBUG: Generating user embedding');
2442 1024 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2443 -
2444 - // Check if embedding generation returned an error
2445 - if (is_array($user_embedding) && isset($user_embedding['error'])) {
2446 - $error_message = $user_embedding['error'];
2447 - $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2448 -
2449 - // FIXED: Send error in appropriate format based on streaming mode
2450 - if ($this->is_streaming) {
2451 - echo "data: " . json_encode([
2452 - 'error' => true,
2453 - 'error_message' => $error_message,
2454 - 'error_code' => $error_code,
2455 - 'text' => $error_message,
2456 - 'message' => $error_message
2457 - ]) . "\n\n";
2458 - echo "data: [DONE]\n\n";
2459 - flush();
2460 - } else {
2461 - wp_send_json_error([
2462 - 'error_message' => $error_message,
2463 - 'error_code' => $error_code
2464 - ]);
2465 - }
2466 - wp_die();
1025 + if (!is_array($user_embedding)) {
1026 + error_log('❌ MXCHAT DEBUG: Failed to generate user embedding');
1027 + return false;
2467 1028 }
2468 -
2469 - // Check if embedding is valid
2470 - if (!is_array($user_embedding) || empty($user_embedding)) {
2471 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2472 -
2473 - // FIXED: Send error in appropriate format based on streaming mode
2474 - if ($this->is_streaming) {
2475 - echo "data: " . json_encode([
2476 - 'error' => true,
2477 - 'error_message' => $error_message,
2478 - 'error_code' => 'invalid_embedding',
2479 - 'text' => $error_message,
2480 - 'message' => $error_message
2481 - ]) . "\n\n";
2482 - echo "data: [DONE]\n\n";
2483 - flush();
2484 - } else {
2485 - wp_send_json_error([
2486 - 'error_message' => $error_message,
2487 - 'error_code' => 'invalid_embedding'
2488 - ]);
2489 - }
2490 - wp_die();
2491 - }
2492 -
1029 + error_log('✅ MXCHAT DEBUG: User embedding generated successfully');
1030 +
2493 1031 // Fetch intents from the database
2494 1032 $table_name = $wpdb->prefix . 'mxchat_intents';
2495 1033 if ($chat_mode === 'agent') {
1034 + error_log('🔍 MXCHAT DEBUG: Agent mode - fetching only chatbot switch intent');
2496 1035 $query = $wpdb->prepare(
2497 1036 "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2498 1037 'mxchat_handle_switch_to_chatbot_intent'
2499 1038 );
@@ -2498,139 +1037,78 @@
2498 1037 'mxchat_handle_switch_to_chatbot_intent'
2499 1038 );
2500 1039 $intents = $wpdb->get_results($query);
2501 1040 } else {
1041 + error_log('🔍 MXCHAT DEBUG: AI mode - fetching all enabled intents');
1042 + // Only fetch enabled intents (either explicitly enabled with 1 or implicitly enabled with NULL for backward compatibility)
2502 1043 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2503 1044 }
2504 -
1045 +
1046 + error_log('🔍 MXCHAT DEBUG: Found ' . count($intents) . ' enabled intents to check');
1047 +
2505 1048 if (empty($intents)) {
1049 + error_log('❌ MXCHAT DEBUG: No enabled intents found in database');
2506 1050 return false;
2507 1051 }
2508 -
2509 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2510 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2511 - $phrases_by_intent = [];
2512 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2513 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2514 - foreach ($all_phrases as $p) {
2515 - $phrases_by_intent[$p->intent_id][] = $p;
2516 - }
2517 - }
2518 -
1052 +
2519 1053 $highest_similarity = -INF;
2520 1054 $matched_intent = null;
2521 -
2522 - // Array to store action analysis for testing panel
2523 - $action_analysis = [];
2524 -
1055 +
1056 + error_log('📊 MXCHAT DEBUG: Intent Similarity Scores ==================');
2525 1057 foreach ($intents as $intent) {
2526 - // Additional check for enabled state
1058 + error_log("🔄 MXCHAT DEBUG: Checking intent: '{$intent->intent_label}' (callback: {$intent->callback_function})");
1059 +
1060 + // Additional check for enabled state in case database structure was modified
2527 1061 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2528 1062 if (!$is_enabled) {
1063 + error_log("⚠️ MXCHAT DEBUG: Skipping disabled intent: {$intent->intent_label}");
2529 1064 continue;
2530 1065 }
2531 -
2532 - // Check if this action is enabled for the current bot
2533 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2534 - continue;
2535 - }
2536 -
2537 - $best_similarity = -INF;
2538 - $matched_phrase_text = '';
2539 -
2540 - // Check legacy embedding vector (existing behavior)
1066 +
2541 1067 $intent_embedding_serialized = $intent->embedding_vector;
2542 1068 $intent_embedding = $intent_embedding_serialized
2543 1069 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2544 1070 : null;
2545 -
2546 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2547 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2548 - if ($legacy_similarity > $best_similarity) {
2549 - $best_similarity = $legacy_similarity;
2550 - $matched_phrase_text = 'legacy';
2551 - }
2552 - }
2553 -
2554 - // Check individual phrase vectors
2555 - if (isset($phrases_by_intent[$intent->id])) {
2556 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2557 - $phrase_embedding = $phrase_row->embedding_vector
2558 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2559 - : null;
2560 - if (!is_array($phrase_embedding)) {
2561 - continue;
2562 - }
2563 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2564 - if ($phrase_similarity > $best_similarity) {
2565 - $best_similarity = $phrase_similarity;
2566 - $matched_phrase_text = $phrase_row->phrase;
2567 - }
2568 - }
2569 - }
2570 -
2571 - // Skip if no valid embedding was found at all
2572 - if ($best_similarity === -INF) {
1071 +
1072 + if (!is_array($intent_embedding)) {
1073 + error_log("❌ MXCHAT DEBUG: Invalid embedding for intent: {$intent->intent_label}");
2573 1074 continue;
2574 1075 }
2575 -
2576 - $similarity = $best_similarity;
1076 +
1077 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2577 1078 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2578 -
2579 - // Store action analysis data for testing panel
2580 - $action_analysis[] = [
2581 - 'intent_label' => $intent->intent_label,
2582 - 'callback_function' => $intent->callback_function,
2583 - 'similarity' => round($similarity, 4),
2584 - 'similarity_percentage' => round($similarity * 100, 2),
2585 - 'threshold' => $intent_threshold,
2586 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2587 - 'above_threshold' => $similarity >= $intent_threshold,
2588 - 'matched_phrase' => $matched_phrase_text,
2589 - 'triggered' => false // Will be updated below if this intent is triggered
2590 - ];
2591 -
1079 +
1080 + error_log("📊 MXCHAT DEBUG: Intent '{$intent->intent_label}' similarity: {$similarity}, threshold: {$intent_threshold}");
1081 +
2592 1082 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2593 1083 $highest_similarity = $similarity;
2594 1084 $matched_intent = $intent;
1085 + error_log("✅ MXCHAT DEBUG: New best match: '{$intent->intent_label}' with similarity {$similarity}");
2595 1086 }
2596 1087 }
1088 + error_log('📊 MXCHAT DEBUG: End Intent Scores ==================');
2597 1089
2598 - // Mark the triggered action if any
2599 1090 if ($matched_intent) {
2600 - foreach ($action_analysis as &$action) {
2601 - if ($action['intent_label'] === $matched_intent->intent_label) {
2602 - $action['triggered'] = true;
2603 - break;
2604 - }
2605 - }
2606 - }
2607 -
2608 - // Sort actions by similarity (highest first) and store for testing panel
2609 - usort($action_analysis, function($a, $b) {
2610 - return $b['similarity'] <=> $a['similarity'];
2611 - });
2612 -
2613 - // Store action analysis for testing panel capture
2614 - $this->last_action_analysis = $action_analysis;
2615 -
2616 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2617 - if ($matched_intent) {
1091 + error_log("🎯 MXCHAT DEBUG: Final Intent Match: '{$matched_intent->intent_label}'");
1092 + error_log("🔄 MXCHAT DEBUG: Invoking callback: {$matched_intent->callback_function}");
1093 +
2618 1094 // If the callback is a method on this instance (core callback), call it directly
2619 1095 if (method_exists($this, $matched_intent->callback_function)) {
1096 + error_log('🔍 MXCHAT DEBUG: Using direct method call for core callback');
2620 1097 $callback_result = call_user_func(
2621 - [$this, $matched_intent->callback_function],
2622 - $message,
2623 - $user_id,
2624 - $session_id,
2625 - $matched_intent,
2626 - $user_context ?? null
2627 - );
1098 + [$this, $matched_intent->callback_function],
1099 + $message,
1100 + $user_id,
1101 + $session_id,
1102 + $matched_intent,
1103 + $user_context
1104 + );
2628 1105 } else {
1106 + error_log('🔍 MXCHAT DEBUG: Using apply_filters for add-on callback');
2629 1107 // Otherwise, use apply_filters for add-on callbacks
2630 1108 $callback_result = apply_filters(
2631 1109 $matched_intent->callback_function,
2632 - false,
1110 + false, // default return value
2633 1111 $message,
2634 1112 $user_id,
2635 1113 $session_id,
2636 1114 $matched_intent
@@ -2636,50 +1114,22 @@
2636 1114 $matched_intent
2637 1115 );
2638 1116 }
2639 1117
2640 - // Handle the callback result properly
1118 + error_log('🔍 MXCHAT DEBUG: Callback result type: ' . gettype($callback_result));
2641 1119 if ($callback_result !== false) {
2642 - // If callback returned an array with chat_mode, use it directly
2643 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2644 - $this->fallbackResponse = $callback_result;
2645 - return $callback_result; // Return the full array
2646 - } else {
2647 - $this->fallbackResponse = $callback_result;
2648 - return true;
2649 - }
1120 + error_log('✅ MXCHAT DEBUG: Intent handled successfully');
1121 + $this->fallbackResponse = $callback_result;
1122 + return true;
2650 1123 }
1124 + error_log('❌ MXCHAT DEBUG: Callback returned false');
1125 + } else {
1126 + error_log('❌ MXCHAT DEBUG: No matching intent found');
2651 1127 }
2652 1128
1129 + error_log('🔍 MXCHAT DEBUG: Intent Check Completed ==================');
2653 1130 return false;
2654 1131 }
2655 -
2656 -/**
2657 - * Check if an action is enabled for a specific bot
2658 - */
2659 -private function is_action_enabled_for_bot($intent, $bot_id) {
2660 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2661 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2662 - return true;
2663 - }
2664 -
2665 - $enabled_bots = json_decode($intent->enabled_bots, true);
2666 -
2667 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2668 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2669 - return true;
2670 - }
2671 -
2672 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2673 - // default-bot actions are testable from the admin panel
2674 - if ($bot_id === 'testing') {
2675 - $bot_id = 'default';
2676 - }
2677 -
2678 - // Check if the current bot is in the enabled bots list
2679 - return in_array($bot_id, $enabled_bots);
2680 -}
2681 -
2682 1132 // Helper function to clear PDF and Word document related transients
2683 1133 private function clear_pdf_transients($session_id) {
2684 1134 // PDF transients
2685 1135 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2698,38 +1148,34 @@
2698 1148
2699 1149
2700 1150 //verified good
2701 1151 public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2702 - // Get the user's original instruction/message
2703 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2704 -
2705 - // Set instruction for AI - just pass along what the user wanted to say
2706 - $this->current_action_instruction = $user_instruction;
2707 -
2708 - // Set the transient to track email capture flow
1152 + // Log the message safely
1153 + error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
1154 +
1155 + // Initiate email capture flow
1156 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
1157 +
2709 1158 set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2710 -
2711 - // Return false to let the AI generate the response
2712 - return false;
1159 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1160 +
1161 + // Respond to the user
1162 + wp_send_json(['message' => $response]);
1163 + wp_die();
2713 1164 }
2714 1165
2715 1166 public function mxchat_generate_image($message, $user_id, $session_id) {
2716 - //error_log("Starting image generation for message: " . $message);
2717 -
2718 - // Prepare a prompt for OpenAI image generation
1167 + error_log("Starting image generation for message: " . $message);
1168 +
1169 + // Prepare a prompt for DALL-E
2719 1170 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2720 -
2721 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2722 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2723 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2724 - $image_response = $this->mxchat_generate_custom_image($prompt);
2725 - } else {
2726 - // Use the existing OpenAI API key
2727 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2728 - // Call OpenAI GPT Image to generate an image
2729 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2730 - }
2731 1171
1172 + // Use the existing OpenAI API key
1173 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1174 +
1175 + // Call DALL-E to generate an image
1176 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1177 +
2732 1178 // Check if the response contains an image URL
2733 1179 if (isset($image_response['imageUrl'])) {
2734 1180 $image_url = esc_url_raw($image_response['imageUrl']);
2735 1181
@@ -2748,9 +1194,9 @@
2748 1194 'images' => [$image_url]
2749 1195 ];
2750 1196
2751 1197 // For debugging/verification - Use json_encode to verify what's being set
2752 - //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
1198 + error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
2753 1199
2754 1200 // Return the response directly instead of relying on the property
2755 1201 return $this->fallbackResponse;
2756 1202 } else {
@@ -2765,132 +1211,31 @@
2765 1211 'html' => '',
2766 1212 'images' => []
2767 1213 ];
2768 1214
2769 - //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2770 - //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
1215 + error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
1216 + error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
2771 1217
2772 1218 // Return the response directly instead of relying on the property
2773 1219 return $this->fallbackResponse;
2774 1220 }
2775 1221 }
2776 -
2777 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2778 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2779 -
2780 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2781 - if (empty($gemini_api_key)) {
2782 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2783 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2784 - return ['text' => $response_text, 'html' => '', 'images' => []];
2785 - }
2786 -
2787 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2788 -
2789 - if (isset($image_response['imageUrl'])) {
2790 - $image_url = esc_url_raw($image_response['imageUrl']);
2791 -
2792 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2793 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2794 -
2795 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2796 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2797 -
2798 - $this->fallbackResponse = [
2799 - 'text' => $response_text,
2800 - 'html' => $response_html,
2801 - 'images' => [$image_url]
2802 - ];
2803 -
2804 - return $this->fallbackResponse;
2805 - } else {
2806 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2807 -
2808 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2809 -
2810 - $this->fallbackResponse = [
2811 - 'text' => $response_text,
2812 - 'html' => '',
2813 - 'images' => []
2814 - ];
2815 -
2816 - return $this->fallbackResponse;
2817 - }
2818 -}
2819 -
2820 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2821 - // Map the real mime type to a matching file extension so the saved file's
2822 - // extension always agrees with its bytes. A mismatch (e.g. Imagen returning
2823 - // webp bytes that were written into a ".png" file) makes the browser refuse
2824 - // to render the image even though the file saved successfully and the bot
2825 - // reported success — that was the Gemini/Imagen "image never renders" bug.
2826 - // OpenAI + custom-provider paths pass 'image/png' explicitly, so they are
2827 - // unaffected; this only matters for providers that return another type.
2828 - $mime_to_ext = [
2829 - 'image/jpeg' => 'jpg',
2830 - 'image/jpg' => 'jpg',
2831 - 'image/png' => 'png',
2832 - 'image/webp' => 'webp',
2833 - 'image/gif' => 'gif',
2834 - ];
2835 - $mime_type = strtolower(trim((string) $mime_type));
2836 - if (isset($mime_to_ext[$mime_type])) {
2837 - $extension = $mime_to_ext[$mime_type];
2838 - } else {
2839 - // Unknown/unsupported type: fall back to png and normalize the stored
2840 - // mime so the attachment record and the file extension stay consistent.
2841 - $extension = 'png';
2842 - $mime_type = 'image/png';
2843 - }
2844 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2845 - $decoded = base64_decode($base64_data);
2846 -
2847 - if ($decoded === false) {
2848 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2849 - }
2850 -
2851 - $upload = wp_upload_bits($filename, null, $decoded);
2852 -
2853 - if (!empty($upload['error'])) {
2854 - return new \WP_Error('upload_failed', $upload['error']);
2855 - }
2856 -
2857 - $attach_id = wp_insert_attachment([
2858 - 'post_mime_type' => $mime_type,
2859 - 'post_title' => $prefix,
2860 - 'post_content' => '',
2861 - 'post_status' => 'inherit',
2862 - ], $upload['file']);
2863 -
2864 - if (is_wp_error($attach_id)) {
2865 - return $attach_id;
2866 - }
2867 -
2868 - require_once ABSPATH . 'wp-admin/includes/image.php';
2869 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2870 - wp_update_attachment_metadata($attach_id, $metadata);
2871 -
2872 - return esc_url_raw(wp_get_attachment_url($attach_id));
2873 -}
2874 -
2875 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1222 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
2876 1223 $api_url = 'https://api.openai.com/v1/images/generations';
2877 1224 $body = json_encode([
2878 - 'prompt' => sanitize_text_field($prompt),
2879 - 'n' => 1,
2880 - 'size' => '1024x1024',
2881 - 'quality' => 'medium',
2882 - 'output_format' => 'png',
2883 - 'model' => sanitize_text_field($model),
1225 + 'prompt' => sanitize_text_field($prompt),
1226 + 'n' => 1,
1227 + 'size' => '1024x1024',
1228 + 'model' => sanitize_text_field($model),
2884 1229 ]);
2885 1230
2886 1231 $args = [
2887 - 'body' => $body,
1232 + 'body' => $body,
2888 1233 'headers' => [
2889 - 'Content-Type' => 'application/json',
1234 + 'Content-Type' => 'application/json',
2890 1235 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2891 1236 ],
2892 - 'method' => 'POST',
1237 + 'method' => 'POST',
2893 1238 'timeout' => absint($timeout),
2894 1239 ];
2895 1240
2896 1241 $response = wp_remote_post($api_url, $args);
@@ -2895,114 +1240,23 @@
2895 1240
2896 1241 $response = wp_remote_post($api_url, $args);
2897 1242
2898 1243 if (is_wp_error($response)) {
1244 + error_log("DALL-E request failed: " . $response->get_error_message());
2899 1245 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2900 1246 }
2901 1247
2902 1248 $response_body = json_decode(wp_remote_retrieve_body($response), true);
2903 1249
2904 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2905 - if ($b64) {
2906 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2907 - if (is_wp_error($saved_url)) {
2908 - return ['error' => $saved_url->get_error_message()];
2909 - }
2910 - return ['imageUrl' => $saved_url];
1250 + if (isset($response_body['data'][0]['url'])) {
1251 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
2911 1252 } else {
1253 + error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
2912 1254 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2913 1255 }
2914 1256 }
2915 1257
2916 1258 /**
2917 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2918 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2919 - */
2920 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2921 - $cfg = $this->mxchat_resolve_custom_provider();
2922 - if (empty($cfg['base_url'])) {
2923 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2924 - }
2925 - $url = $cfg['base_url'] . '/images/generations';
2926 - if (!empty($cfg['api_version'])) {
2927 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2928 - }
2929 - $body = wp_json_encode([
2930 - 'prompt' => sanitize_text_field($prompt),
2931 - 'n' => 1,
2932 - 'size' => '1024x1024',
2933 - 'model' => $cfg['model'],
2934 - ]);
2935 - $response = wp_remote_post($url, [
2936 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2937 - 'body' => $body,
2938 - 'method' => 'POST',
2939 - 'timeout' => absint($timeout),
2940 - ]);
2941 - if (is_wp_error($response)) {
2942 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2943 - }
2944 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2945 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2946 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2947 - if ($b64) {
2948 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2949 - if (is_wp_error($saved)) {
2950 - return ['error' => $saved->get_error_message()];
2951 - }
2952 - return ['imageUrl' => $saved];
2953 - }
2954 - $remote_url = $resp['data'][0]['url'] ?? null;
2955 - if ($remote_url) {
2956 - return ['imageUrl' => esc_url_raw($remote_url)];
2957 - }
2958 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2959 - return ['error' => esc_html($err_msg)];
2960 -}
2961 -
2962 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2963 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2964 -
2965 - $body = json_encode([
2966 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2967 - 'parameters' => [
2968 - 'sampleCount' => 1,
2969 - 'aspectRatio' => '1:1',
2970 - ],
2971 - ]);
2972 -
2973 - $args = [
2974 - 'body' => $body,
2975 - 'headers' => [
2976 - 'Content-Type' => 'application/json',
2977 - 'x-goog-api-key' => sanitize_text_field($api_key),
2978 - ],
2979 - 'method' => 'POST',
2980 - 'timeout' => absint($timeout),
2981 - ];
2982 -
2983 - $response = wp_remote_post($api_url, $args);
2984 -
2985 - if (is_wp_error($response)) {
2986 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2987 - }
2988 -
2989 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2990 -
2991 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
2992 - if ($b64) {
2993 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
2994 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
2995 - if (is_wp_error($saved_url)) {
2996 - return ['error' => $saved_url->get_error_message()];
2997 - }
2998 - return ['imageUrl' => $saved_url];
2999 - } else {
3000 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3001 - }
3002 -}
3003 -
3004 -/**
3005 1259 * Handle web search requests.
3006 1260 *
3007 1261 * Sends the refined search query to the Brave Search API and uses the
3008 1262 * results to generate a conversational response with the AI model.
@@ -3050,10 +1304,10 @@
3050 1304 $transient_key = 'mxchat_search_' . md5($refined_search_query);
3051 1305 $results = get_transient($transient_key);
3052 1306
3053 1307 if (false === $results) {
3054 - // SECURITY FIX: Changed to wp_safe_remote_get
3055 - $response = wp_safe_remote_get(
1308 + // Fetch new results from the Brave Search API
1309 + $response = wp_remote_get(
3056 1310 $api_url,
3057 1311 array(
3058 1312 'headers' => array(
3059 1313 'Accept' => 'application/json',
@@ -3132,28 +1386,119 @@
3132 1386 'html' => ''
3133 1387 );
3134 1388 }
3135 1389 }
1390 +/**
1391 + * Format search results into a natural text summary.
1392 + *
1393 + * @since 1.0.0
1394 + * @param array $results The search results from the API.
1395 + * @param string $query The original search query.
1396 + * @return string The text summary of the top results.
1397 + */
1398 +private function format_search_results( $results, $query ) {
1399 + $summary = sprintf(
1400 + esc_html__( 'Here are the most relevant results for "%s":', 'mxchat' ),
1401 + esc_html( $query )
1402 + ) . "\n\n";
3136 1403
3137 -//very good
1404 + $max_results = min( count( $results ), 3 );
1405 + for ( $i = 0; $i < $max_results; $i++ ) {
1406 + $result = $results[ $i ];
1407 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1408 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1409 +
1410 + // Append title and description to the summary
1411 + $summary .= sprintf(
1412 + "%s\n%s\n\n",
1413 + esc_html( $title ),
1414 + esc_html( $description )
1415 + );
1416 + }
1417 +
1418 + return $summary;
1419 +}
1420 +
3138 1421 /**
3139 - * Handle image search requests from the chatbot
1422 + * Generate HTML markup for search results.
3140 1423 *
3141 - * @param string $message The user's search query
3142 - * @param int $user_id The user's ID
3143 - * @param string $session_id The chat session ID
3144 - * @return array Response array with text and HTML content
1424 + * @since 1.0.0
1425 + * @param array $results The search results from the API.
1426 + * @param string $query The user-refined query.
1427 + * @return string The HTML markup for displaying the results.
3145 1428 */
1429 +private function generate_search_results_html( $results, $query ) {
1430 + ob_start();
1431 + ?>
1432 + <div class="mxchat-search-results">
1433 + <?php foreach ( $results as $result ) :
1434 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1435 + $url = isset( $result['url'] ) ? esc_url( $result['url'] ) : '#';
1436 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1437 + $favicon = isset( $result['meta_url']['favicon'] ) ? esc_url( $result['meta_url']['favicon'] ) : '';
1438 + $thumbnail = isset( $result['thumbnail']['src'] ) ? esc_url( $result['thumbnail']['src'] ) : '';
1439 + $domain = parse_url( $url, PHP_URL_HOST );
1440 + ?>
1441 + <div class="mxchat-search-item">
1442 + <div class="mxchat-search-header">
1443 + <?php if ( $favicon ) : ?>
1444 + <img
1445 + src="<?php echo esc_url( $favicon ); ?>"
1446 + class="mxchat-site-icon"
1447 + alt="<?php echo esc_attr__( 'Site icon', 'mxchat' ); ?>"
1448 + width="16"
1449 + height="16"
1450 + />
1451 + <?php endif; ?>
1452 + <div class="mxchat-site-url"><?php echo esc_html( $domain ); ?></div>
1453 + </div>
1454 +
1455 + <div class="mxchat-search-content">
1456 + <h3 class="mxchat-search-title">
1457 + <a href="<?php echo esc_url( $url ); ?>"
1458 + target="_blank"
1459 + rel="noopener noreferrer"
1460 + >
1461 + <?php echo esc_html( $title ); ?>
1462 + </a>
1463 + </h3>
1464 +
1465 + <?php if ( $thumbnail ) : ?>
1466 + <div class="mxchat-search-thumbnail">
1467 + <img
1468 + src="<?php echo esc_url( $thumbnail ); ?>"
1469 + alt="<?php echo esc_attr__( 'Thumbnail image', 'mxchat' ); ?>"
1470 + loading="lazy"
1471 + />
1472 + </div>
1473 + <?php endif; ?>
1474 +
1475 + <div class="mxchat-search-description">
1476 + <?php echo esc_html( $description ); ?>
1477 + </div>
1478 + </div>
1479 + </div>
1480 + <?php endforeach; ?>
1481 + </div>
1482 + <?php
1483 + return ob_get_clean();
1484 +}
1485 +
1486 +
1487 +//very good
3146 1488 public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3147 - // Step 1: Interpret the search query using the user's selected AI model
1489 +
1490 + // Step 1: Interpret the search query for better results
3148 1491 $refined_search_query = $this->mxchat_interpret_search_query($message);
3149 1492
1493 +
3150 1494 // If no query was interpreted, return a fallback message
3151 1495 if (empty($refined_search_query)) {
3152 - return array(
1496 + $this->fallbackResponse = [
3153 1497 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3154 1498 'html' => "",
3155 - );
1499 + ];
1500 + return;
3156 1501 }
3157 1502
3158 1503 // Brave API URL
3159 1504 $api_url = 'https://api.search.brave.com/res/v1/images/search';
@@ -3162,12 +1507,19 @@
3162 1507 $options = get_option('mxchat_options');
3163 1508 $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3164 1509
3165 1510 if (empty($api_key)) {
3166 - return array(
1511 +/*
1512 + if (defined('WP_DEBUG') && WP_DEBUG) {
1513 + error_log("Brave API key is missing.");
1514 + }
1515 +*/
1516 +
1517 + $this->fallbackResponse = [
3167 1518 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3168 1519 'html' => "",
3169 - );
1520 + ];
1521 + return;
3170 1522 }
3171 1523
3172 1524 $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3173 1525 $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
@@ -3178,8 +1530,16 @@
3178 1530 'count' => $image_count,
3179 1531 'safesearch' => $safe_search,
3180 1532 ], $api_url);
3181 1533
1534 +/*
1535 + // Log the final API URL for the search
1536 + if (defined('WP_DEBUG') && WP_DEBUG) {
1537 + error_log("Final API URL for Brave Image Search: " . esc_url_raw($api_url));
1538 + }
1539 +*/
1540 +
1541 +
3182 1542 // Implement caching
3183 1543 $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3184 1544 $body = get_transient($transient_key);
3185 1545
@@ -3192,16 +1552,22 @@
3192 1552 ],
3193 1553 'timeout' => 10,
3194 1554 ];
3195 1555
3196 - // SECURITY FIX: Changed to wp_safe_remote_get
3197 - $response = wp_safe_remote_get($api_url, $args);
1556 + $response = wp_remote_get($api_url, $args);
3198 1557
3199 1558 if (is_wp_error($response)) {
3200 - return array(
1559 +/*
1560 + if (defined('WP_DEBUG') && WP_DEBUG) {
1561 + error_log("Brave Image API request failed: " . $response->get_error_message());
1562 + }
1563 +*/
1564 +
1565 + $this->fallbackResponse = [
3201 1566 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3202 1567 'html' => "",
3203 - );
1568 + ];
1569 + return;
3204 1570 }
3205 1571
3206 1572 $body = json_decode(wp_remote_retrieve_body($response), true);
3207 1573 set_transient($transient_key, $body, HOUR_IN_SECONDS);
@@ -3209,16 +1575,10 @@
3209 1575
3210 1576 // Process the API response
3211 1577 if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3212 1578 $html_output = '<div class="mxchat-image-gallery">';
3213 -
3214 - // Get the configured image count (1-6)
3215 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3216 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3217 -
3218 - // Use only the requested number of images
3219 - for ($i = 0; $i < $display_count; $i++) {
3220 - $image = $body['results'][$i];
1579 +
1580 + foreach ($body['results'] as $image) {
3221 1581 $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3222 1582 $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3223 1583 $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3224 1584
@@ -3232,149 +1592,47 @@
3232 1592 }
3233 1593
3234 1594 $html_output .= '</div>';
3235 1595
3236 - // Create response text
3237 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3238 -
3239 - // Save both response text and HTML to chat history
3240 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1596 + $this->fallbackResponse = [
1597 + 'text' => "",
1598 + 'html' => $html_output,
1599 + ];
1600 +
1601 + // Save response in chat history
3241 1602 $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3242 1603
3243 - // Return the combined response
3244 - return array(
3245 - 'text' => $response_text,
3246 - 'html' => $html_output,
3247 - );
3248 1604 } else {
3249 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3250 -
3251 - // Save the error message to chat history
3252 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3253 -
3254 - return array(
3255 - 'text' => $response_text,
1605 +/*
1606 + if (defined('WP_DEBUG') && WP_DEBUG) {
1607 + error_log("Brave Image API response did not contain expected data structure or was empty: " . print_r($body, true));
1608 + }
1609 +*/
1610 +
1611 + $this->fallbackResponse = [
1612 + 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3256 1613 'html' => "",
3257 - );
1614 + ];
3258 1615 }
3259 1616 }
3260 -
3261 -/**
3262 - * Interpret the search query using the user's selected AI model
3263 - *
3264 - * @param string $user_query The original query from the user
3265 - * @return string The refined search query
3266 - */
3267 1617 public function mxchat_interpret_search_query($user_query) {
3268 1618 $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3269 1619
3270 - // Get options and determine the selected model
3271 - $options = $this->options ?? get_option('mxchat_options');
3272 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
1620 + // Retrieve OpenAI API key using 'api_key' as the option key
1621 + $api_key = isset($this->options['api_key']) ? sanitize_text_field($this->options['api_key']) : sanitize_text_field(get_option('mxchat_options')['api_key']);
3273 1622
3274 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3275 - if ($selected_model === 'custom-provider') {
3276 - return $this->interpret_query_with_custom($user_query, $system_prompt);
1623 + /*
1624 + // Log the API key check, without exposing the key
1625 + if (defined('WP_DEBUG') && WP_DEBUG) {
1626 + error_log("Retrieved OpenAI API Key: " . ($api_key ? "Present" : "Missing"));
3277 1627 }
1628 + */
3278 1629
3279 - // Extract model prefix to determine the provider
3280 - $model_parts = explode('-', $selected_model);
3281 - $provider = strtolower($model_parts[0]);
3282 -
3283 - // Determine which API key to use based on the provider
3284 - switch ($provider) {
3285 - case 'gemini':
3286 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3287 - if (empty($api_key)) {
3288 - return sanitize_text_field($user_query); // Default to original query if API key missing
3289 - }
3290 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3291 -
3292 - case 'claude':
3293 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3294 - if (empty($api_key)) {
3295 - return sanitize_text_field($user_query);
3296 - }
3297 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3298 -
3299 - case 'grok':
3300 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3301 - if (empty($api_key)) {
3302 - return sanitize_text_field($user_query);
3303 - }
3304 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3305 -
3306 - case 'deepseek':
3307 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3308 - if (empty($api_key)) {
3309 - return sanitize_text_field($user_query);
3310 - }
3311 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3312 -
3313 - case 'gpt':
3314 - default:
3315 - // Default to OpenAI for custom models or unrecognized prefixes
3316 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3317 - if (empty($api_key)) {
3318 - return sanitize_text_field($user_query);
3319 - }
3320 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
1630 + if (empty($api_key)) {
1631 + error_log("OpenAI API key is missing.");
1632 + return sanitize_text_field($user_query); // Default to the original query if API key is missing
3321 1633 }
3322 -}
3323 1634
3324 -/**
3325 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3326 - * Uses the same base URL + auth scheme as the chat dispatcher.
3327 - */
3328 -private function interpret_query_with_custom($user_query, $system_prompt) {
3329 - $cfg = $this->mxchat_resolve_custom_provider();
3330 - if (empty($cfg['base_url'])) {
3331 - return sanitize_text_field($user_query);
3332 - }
3333 - $args = [
3334 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3335 - 'body' => wp_json_encode([
3336 - 'model' => $cfg['model'],
3337 - 'messages' => [
3338 - ['role' => 'system', 'content' => $system_prompt],
3339 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3340 - ],
3341 - 'temperature' => 0.2,
3342 - 'max_tokens' => 20,
3343 - ]),
3344 - 'method' => 'POST',
3345 - 'timeout' => 15,
3346 - ];
3347 - $response = wp_remote_post($cfg['chat_url'], $args);
3348 - if (is_wp_error($response)) {
3349 - return sanitize_text_field($user_query);
3350 - }
3351 - $body = json_decode(wp_remote_retrieve_body($response), true);
3352 - return isset($body['choices'][0]['message']['content'])
3353 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3354 - : sanitize_text_field($user_query);
3355 -}
3356 -
3357 -/**
3358 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3359 - * into the assoc-array form wp_remote_post expects.
3360 - */
3361 -private function mxchat_custom_provider_assoc_headers($cfg) {
3362 - $headers = ['Content-Type' => 'application/json'];
3363 - if (!empty($cfg['api_key'])) {
3364 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3365 - $headers['api-key'] = $cfg['api_key'];
3366 - } else {
3367 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3368 - }
3369 - }
3370 - return $headers;
3371 -}
3372 -
3373 -/**
3374 - * Interpret query using OpenAI models
3375 - */
3376 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3377 1635 $url = 'https://api.openai.com/v1/chat/completions';
3378 1636 $args = [
3379 1637 'headers' => [
3380 1638 'Authorization' => 'Bearer ' . $api_key,
@@ -3380,9 +1638,9 @@
3380 1638 'Authorization' => 'Bearer ' . $api_key,
3381 1639 'Content-Type' => 'application/json',
3382 1640 ],
3383 1641 'body' => wp_json_encode([
3384 - 'model' => $model,
1642 + 'model' => 'gpt-3.5-turbo',
3385 1643 'messages' => [
3386 1644 ['role' => 'system', 'content' => $system_prompt],
3387 1645 ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3388 1646 ],
@@ -3389,204 +1647,166 @@
3389 1647 'temperature' => 0.2,
3390 1648 'max_tokens' => 20,
3391 1649 ]),
3392 1650 'method' => 'POST',
3393 - 'timeout' => 15,
3394 1651 ];
3395 1652
3396 1653 $response = wp_remote_post($url, $args);
1654 +
3397 1655 if (is_wp_error($response)) {
3398 - return sanitize_text_field($user_query);
1656 + error_log("OpenAI request failed: " . $response->get_error_message());
1657 + return sanitize_text_field($user_query); // Fallback to the original query if there's an error
3399 1658 }
3400 1659
3401 1660 $body = json_decode(wp_remote_retrieve_body($response), true);
3402 - return isset($body['choices'][0]['message']['content'])
3403 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3404 - : sanitize_text_field($user_query);
1661 +
1662 + // Check for a valid response and sanitize output
1663 + if (isset($body['choices'][0]['message']['content'])) {
1664 + $interpreted_query = sanitize_text_field(trim($body['choices'][0]['message']['content']));
1665 +
1666 + /*
1667 + // Log the interpreted query for debugging
1668 + if (defined('WP_DEBUG') && WP_DEBUG) {
1669 + error_log("Interpreted search query: " . $interpreted_query);
1670 + }
1671 + */
1672 +
1673 + return $interpreted_query;
1674 + } else {
1675 + error_log("Unexpected API response format: " . print_r($body, true));
1676 + return sanitize_text_field($user_query);
1677 + }
3405 1678 }
3406 1679
3407 -/**
3408 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3409 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3410 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3411 - * stripping. We never send a `thinking` param either, which is required for
3412 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3413 - */
3414 -private function mxchat_claude_omits_temperature($model) {
3415 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3416 - return in_array($model, $no_temp, true);
3417 -}
3418 1680
3419 -/**
3420 - * Interpret query using Claude models
3421 - */
3422 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3423 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3424 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3425 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3426 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3427 - $url = 'https://api.anthropic.com/v1/messages';
3428 1681
3429 - $payload = [
3430 - 'model' => $model,
3431 - 'system' => $system_prompt,
3432 - 'messages' => [
3433 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3434 - ],
3435 - 'max_tokens' => 20,
3436 - 'temperature' => 0.2,
3437 - ];
3438 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
1682 +private function find_product_in_message($message) {
1683 + global $wpdb;
3439 1684
3440 - $args = [
3441 - 'headers' => [
3442 - 'Content-Type' => 'application/json',
3443 - 'x-api-key' => $api_key,
3444 - 'anthropic-version' => '2023-06-01',
3445 - ],
3446 - 'body' => wp_json_encode($payload),
3447 - 'method' => 'POST',
3448 - 'timeout' => 15,
3449 - ];
1685 + // Get embedding for the search query
1686 + $query_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1687 + if (!is_array($query_embedding)) {
1688 + return null;
1689 + }
3450 1690
3451 - $response = wp_remote_post($url, $args);
3452 - if (is_wp_error($response)) {
3453 - return sanitize_text_field($user_query);
1691 + // Get relevant content as string
1692 + $relevant_content = $this->mxchat_find_relevant_products($query_embedding);
1693 + if (empty($relevant_content)) {
1694 + // Return null to indicate no results and set fallback response
1695 + $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');
1696 + return null;
3454 1697 }
3455 1698
3456 - $body = json_decode(wp_remote_retrieve_body($response), true);
3457 - // claude-fable-5 prepends a thinking block to content — take the first
3458 - // TEXT block, not content[0].
3459 - foreach ((array) ($body['content'] ?? array()) as $block) {
3460 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3461 - return sanitize_text_field(trim($block['text']));
1699 + // Extract product URLs from the content
1700 + preg_match_all('/https?:\/\/[^\s<>"\']+?\/product\/[^\s<>"\']+/', $relevant_content, $matches);
1701 +
1702 + if (!empty($matches[0])) {
1703 + // Try each URL found
1704 + foreach ($matches[0] as $url) {
1705 + // Clean the URL
1706 + $url = rtrim($url, '/."\']');
1707 +
1708 + // Get the product slug
1709 + $path = parse_url($url, PHP_URL_PATH);
1710 + $slug = basename(rtrim($path, '/'));
1711 +
1712 + // Find product by slug
1713 + $args = array(
1714 + 'post_type' => 'product',
1715 + 'post_status' => 'publish',
1716 + 'name' => $slug,
1717 + 'posts_per_page' => 1
1718 + );
1719 +
1720 + $products = get_posts($args);
1721 +
1722 + if (!empty($products)) {
1723 + $product_id = $products[0]->ID;
1724 + $product = wc_get_product($product_id);
1725 +
1726 + if ($product && $product->is_purchasable()) {
1727 + return $product_id;
1728 + }
1729 + }
3462 1730 }
3463 1731 }
3464 1732
3465 - return sanitize_text_field($user_query);
3466 -}
1733 + // Fallback: Look for product names in the content
1734 + $products = wc_get_products([
1735 + 'status' => 'publish',
1736 + 'limit' => -1,
1737 + 'return' => 'all'
1738 + ]);
3467 1739
3468 -/**
3469 - * Interpret query using Gemini models
3470 - */
3471 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3472 - if ($model === 'gemini-3-pro-preview') {
3473 - $model = 'gemini-3.1-pro-preview';
1740 + foreach ($products as $product) {
1741 + $name = $product->get_name();
1742 + if (stripos($relevant_content, $name) !== false) {
1743 + if ($product->is_purchasable()) {
1744 + return $product->get_id();
1745 + }
1746 + }
3474 1747 }
3475 - // Use v1beta for preview models, v1 for stable models
3476 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3477 1748
3478 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
3479 -
3480 - $args = [
3481 - 'headers' => [
3482 - 'Content-Type' => 'application/json',
3483 - ],
3484 - 'body' => wp_json_encode([
3485 - 'contents' => [
3486 - [
3487 - 'role' => 'user',
3488 - 'parts' => [
3489 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3490 - ]
3491 - ]
3492 - ],
3493 - 'generationConfig' => [
3494 - 'temperature' => 0.2,
3495 - 'maxOutputTokens' => 20,
3496 - ],
3497 - ]),
3498 - 'method' => 'POST',
3499 - 'timeout' => 15,
3500 - ];
3501 -
3502 - $response = wp_remote_post($url, $args);
3503 - if (is_wp_error($response)) {
3504 - return sanitize_text_field($user_query);
3505 - }
3506 -
3507 - $body = json_decode(wp_remote_retrieve_body($response), true);
3508 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3509 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3510 - }
3511 -
3512 - return sanitize_text_field($user_query);
1749 + // If no product is found after all checks, set the fallback response
1750 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Try to be more specific", 'mxchat');
1751 + return null;
3513 1752 }
3514 1753
3515 -/**
3516 - * Interpret query using X.AI (Grok) models
3517 - */
3518 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3519 - $url = 'https://api.xai.com/v1/chat/completions';
3520 -
3521 - $args = [
3522 - 'headers' => [
3523 - 'Content-Type' => 'application/json',
3524 - 'Authorization' => 'Bearer ' . $api_key,
3525 - ],
3526 - 'body' => wp_json_encode([
3527 - 'model' => $model,
3528 - 'messages' => [
3529 - ['role' => 'system', 'content' => $system_prompt],
3530 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3531 - ],
3532 - 'temperature' => 0.2,
3533 - 'max_tokens' => 20,
3534 - ]),
3535 - 'method' => 'POST',
3536 - 'timeout' => 15,
3537 - ];
3538 -
3539 - $response = wp_remote_post($url, $args);
3540 - if (is_wp_error($response)) {
3541 - return sanitize_text_field($user_query);
3542 - }
3543 -
3544 - $body = json_decode(wp_remote_retrieve_body($response), true);
3545 - if (isset($body['choices'][0]['message']['content'])) {
3546 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3547 - }
3548 -
3549 - return sanitize_text_field($user_query);
1754 +// New method to handle intent responses
1755 +private function generate_intent_response($context_content, $session_id) {
1756 + // Convert the context array to a structured string for the AI
1757 + $context_string = $this->format_intent_context($context_content);
1758 + // Generate AI response using the context
1759 + $response = $this->mxchat_generate_response(
1760 + $context_string,
1761 + $this->options['api_key'],
1762 + $this->options['xai_api_key'],
1763 + $this->options['claude_api_key'],
1764 + $this->options['deepseek_api_key'],
1765 + $this->options['gemini_api_key'], // Added Gemini API key
1766 + $this->mxchat_fetch_conversation_history_for_ai($session_id)
1767 + );
1768 + $this->fallbackResponse['text'] = $response;
1769 + return true;
3550 1770 }
3551 1771
3552 -/**
3553 - * Interpret query using DeepSeek models
3554 - */
3555 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3556 - $url = 'https://api.deepseek.com/v1/chat/completions';
3557 -
3558 - $args = [
3559 - 'headers' => [
3560 - 'Content-Type' => 'application/json',
3561 - 'Authorization' => 'Bearer ' . $api_key,
3562 - ],
3563 - 'body' => wp_json_encode([
3564 - 'model' => $model,
3565 - 'messages' => [
3566 - ['role' => 'system', 'content' => $system_prompt],
3567 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3568 - ],
3569 - 'temperature' => 0.2,
3570 - 'max_tokens' => 20,
3571 - ]),
3572 - 'method' => 'POST',
3573 - 'timeout' => 15,
3574 - ];
3575 -
3576 - $response = wp_remote_post($url, $args);
3577 - if (is_wp_error($response)) {
3578 - return sanitize_text_field($user_query);
1772 +// Helper method to format intent context
1773 +private function format_intent_context($context) {
1774 + $context_string = esc_html__("INTENT CONTEXT:\n", 'mxchat');
1775 +
1776 + switch ($context['intent']) {
1777 + case 'add_to_cart':
1778 + if ($context['status'] === 'success') {
1779 + $context_string .= esc_html__("Action: Successfully added product to cart\n", 'mxchat');
1780 + $context_string .= sprintf(esc_html__("Product: %s\n", 'mxchat'), $context['product']['name']);
1781 + $context_string .= esc_html__("Available actions: ", 'mxchat') . implode(', ', $context['available_actions']) . "\n";
1782 + $context_string .= sprintf(esc_html__("Cart URL: %s\n", 'mxchat'), $context['cart_url']);
1783 + $context_string .= esc_html__("\nPlease inform the user of the successful addition and their available options.", 'mxchat');
1784 + } else {
1785 + $context_string .= esc_html__("Action: Failed to add product to cart\n", 'mxchat');
1786 + $context_string .= sprintf(esc_html__("Reason: %s\n", 'mxchat'), $context['reason']);
1787 + switch ($context['reason']) {
1788 + case 'woocommerce_not_available':
1789 + $context_string .= esc_html__("\nPlease inform the user that shopping features are not available.", 'mxchat');
1790 + break;
1791 + case 'no_product_context':
1792 + $context_string .= esc_html__("\nPlease ask the user to specify which product they want to add.", 'mxchat');
1793 + break;
1794 + case 'product_not_found':
1795 + $context_string .= esc_html__("\nPlease inform the user that the product couldn't be found and ask them to try again.", 'mxchat');
1796 + break;
1797 + case 'add_to_cart_failed':
1798 + $context_string .= esc_html__("\nPlease apologize to the user and suggest they try again or ask for assistance.", 'mxchat');
1799 + break;
1800 + }
1801 + }
1802 + break;
3579 1803 }
3580 -
3581 - $body = json_decode(wp_remote_retrieve_body($response), true);
3582 - if (isset($body['choices'][0]['message']['content'])) {
3583 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3584 - }
3585 -
3586 - return sanitize_text_field($user_query);
1804 +
1805 + return $context_string;
3587 1806 }
3588 1807
1808 +
3589 1809 //very good
3590 1810 private function add_email_to_loops($email) {
3591 1811 // Sanitize the email
3592 1812 $email = sanitize_email($email);
@@ -3596,9 +1816,9 @@
3596 1816 $mailing_list_id = isset($this->options['loops_mailing_list']) ? sanitize_text_field($this->options['loops_mailing_list']) : '';
3597 1817
3598 1818 // Check for missing API key or mailing list ID
3599 1819 if (empty($api_key) || empty($mailing_list_id)) {
3600 - //error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
1820 + error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
3601 1821 return;
3602 1822 }
3603 1823
3604 1824 $data = array(
@@ -3622,9 +1842,9 @@
3622 1842 $response = wp_remote_post($url, $args);
3623 1843
3624 1844 // Handle errors in the API request
3625 1845 if (is_wp_error($response)) {
3626 - //error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
1846 + error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
3627 1847 return;
3628 1848 }
3629 1849
3630 1850 // Check for non-200 HTTP responses
@@ -3630,9 +1850,9 @@
3630 1850 // Check for non-200 HTTP responses
3631 1851 $response_code = wp_remote_retrieve_response_code($response);
3632 1852 if ($response_code != 200) {
3633 1853 $response_body = wp_remote_retrieve_body($response);
3634 - //error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
1854 + error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
3635 1855 }
3636 1856 }
3637 1857
3638 1858 public function mxchat_handle_pdf_discussion($message, $user_id, $session_id) {
@@ -3670,211 +1890,97 @@
3670 1890
3671 1891 // Default to proceeding with conversation if no specific PDF action is needed
3672 1892 $this->fallbackResponse['text'] = '';
3673 1893 }
3674 -
3675 -
3676 -/**
3677 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3678 - */
3679 1894 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3680 - // CLEAR DEBUG LOGGING
3681 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3682 - //error_log("PDF Source: " . $pdf_source);
3683 - //error_log("Max Pages: " . $max_pages);
3684 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3685 -
3686 - // Check if Advanced Claude Toolbar is available and enabled
3687 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3688 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3689 -
3690 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3691 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3692 -
3693 - if ($claude_available && $claude_enabled) {
3694 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3695 -
3696 - // Attempt Claude processing first
3697 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3698 -
3699 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3700 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3701 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3702 -
3703 - // Log first page details for verification
3704 - if (isset($claude_result[0])) {
3705 - $first_page = $claude_result[0];
3706 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3707 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3708 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3709 - }
3710 -
3711 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3712 - return $claude_result;
3713 - } else {
3714 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3715 - //error_log("Claude result type: " . gettype($claude_result));
3716 - if (is_array($claude_result)) {
3717 - //error_log("Claude result count: " . count($claude_result));
3718 - }
3719 - }
3720 - }
3721 -
3722 - // Fallback to basic processing
3723 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3724 -
3725 1895 $upload_dir = wp_upload_dir();
3726 1896 $temp_file = null;
3727 -
1897 +
3728 1898 try {
3729 - // Your existing basic processing code here...
3730 - // (I'll include the key parts with debug logging)
3731 -
1899 + // Handle URL vs local file
3732 1900 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3733 - //error_log("Downloading PDF from URL...");
3734 -
3735 - // SECURITY FIX: Validate URL before processing
3736 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3737 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
1901 + // Validate and download the file from URL
1902 + $temp_file = wp_tempnam($pdf_source); // Safe temporary file name
1903 + $response = wp_remote_get($pdf_source, ['timeout' => 60]);
1904 +
1905 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1906 + error_log(esc_html__("Failed to download PDF. Error: ", 'mxchat') . print_r($response, true));
3738 1907 return false;
3739 1908 }
3740 -
3741 - $temp_file = wp_tempnam($pdf_source);
3742 -
3743 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3744 - $response = wp_safe_remote_get($pdf_source, [
3745 - 'timeout' => 60,
3746 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3747 - ]);
3748 -
3749 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3750 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3751 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
1909 +
1910 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
1911 +
1912 + // Validate that the downloaded file is a PDF
1913 + $mime_type = mime_content_type($temp_file);
1914 + if ($mime_type !== 'application/pdf') {
1915 + error_log(esc_html__("Invalid MIME type detected for PDF: ", 'mxchat') . $mime_type);
1916 + unlink($temp_file);
3752 1917 return false;
3753 1918 }
3754 -
3755 - global $wp_filesystem;
3756 - if (empty($wp_filesystem)) {
3757 - require_once ABSPATH . 'wp-admin/includes/file.php';
3758 - WP_Filesystem();
3759 - }
3760 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3761 - //error_log("✅ PDF downloaded successfully");
3762 1919 } else {
1920 + // For local files, use the provided path directly
3763 1921 $temp_file = $pdf_source;
3764 - //error_log("Using local PDF file: " . $temp_file);
3765 1922 }
3766 -
3767 - // Parse PDF
3768 - //error_log("Parsing PDF with basic parser...");
3769 - mxchat_load_pdf_parser();
1923 +
1924 + // Parse and process the PDF
3770 1925 $parser = new \Smalot\PdfParser\Parser();
3771 1926 $pdf = $parser->parseFile($temp_file);
3772 1927 $pages = $pdf->getPages();
3773 -
3774 - //error_log("PDF contains " . count($pages) . " pages");
3775 -
1928 +
3776 1929 if (count($pages) > $max_pages) {
3777 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3778 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
1930 + error_log(esc_html__("PDF exceeds the maximum allowed pages: ", 'mxchat') . count($pages));
1931 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3779 1932 unlink($temp_file);
3780 1933 }
3781 - return 'too_many_pages';
1934 + return esc_html__('too_many_pages', 'mxchat');
3782 1935 }
3783 -
1936 +
3784 1937 $embeddings = [];
3785 - $processed_pages = 0;
3786 -
3787 1938 foreach ($pages as $page_number => $page) {
3788 1939 $text = $page->getText();
3789 -
1940 +
1941 + // Ensure text is non-empty before generating embeddings
3790 1942 if (empty(trim($text))) {
3791 - //error_log("Skipping empty page: " . ($page_number + 1));
1943 + error_log(esc_html__("Skipping empty page: ", 'mxchat') . ($page_number + 1));
3792 1944 continue;
3793 1945 }
3794 -
3795 - $text = $this->mxchat_clean_text($text);
3796 -
1946 +
3797 1947 $embedding = $this->mxchat_generate_embedding(
3798 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
1948 + esc_html__("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3799 1949 $this->options['api_key']
3800 1950 );
3801 -
1951 +
3802 1952 if ($embedding) {
3803 1953 $embeddings[] = [
3804 1954 'page_number' => $page_number + 1,
3805 1955 'embedding' => $embedding,
3806 1956 'text' => $text,
3807 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3808 - 'processing_method' => 'basic_pdf_parser'
3809 1957 ];
3810 - $processed_pages++;
1958 + } else {
1959 + error_log(esc_html__("Failed to generate embedding for page ", 'mxchat') . ($page_number + 1));
3811 1960 }
3812 1961 }
3813 -
3814 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3815 -
3816 - // Cleanup
3817 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
1962 +
1963 + // Clean up downloaded file if it was from URL
1964 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3818 1965 unlink($temp_file);
3819 1966 }
3820 -
3821 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
1967 +
3822 1968 return $embeddings;
3823 -
1969 +
3824 1970 } catch (\Exception $e) {
3825 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
1971 + // error_log(esc_html__("Error parsing or processing PDF: ", 'mxchat') . $e->getMessage());
1972 +
1973 + // Cleanup in case of exception
3826 1974 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3827 1975 unlink($temp_file);
3828 1976 }
3829 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3830 - return false;
3831 - }
3832 -}
3833 1977
3834 -
3835 -/**
3836 - * Validate PDF URL for security
3837 - * Prevents SSRF attacks by blocking dangerous URLs
3838 - */
3839 -
3840 -private function mxchat_is_safe_pdf_url($url) {
3841 - // Use WordPress core function for comprehensive validation
3842 - // This blocks localhost, private IPs, and reserved IP ranges
3843 - $validated_url = wp_http_validate_url($url);
3844 -
3845 - if ($validated_url === false) {
3846 1978 return false;
3847 1979 }
3848 -
3849 - // Additional check: only allow HTTP/HTTPS schemes
3850 - $parsed = parse_url($url);
3851 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3852 - return false;
3853 - }
3854 -
3855 - return true;
3856 1980 }
3857 -
3858 -
3859 -private function mxchat_clean_text($text) {
3860 - // Remove excessive whitespace
3861 - $text = preg_replace('/\s+/', ' ', $text);
3862 -
3863 - // Remove control characters except newlines and tabs
3864 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3865 -
3866 - // Normalize line endings
3867 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3868 -
3869 - // Trim whitespace
3870 - $text = trim($text);
3871 -
3872 - return $text;
3873 -}
3874 -
3875 1981 private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3876 - //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
1982 + error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3877 1983
3878 1984 $most_relevant = null;
3879 1985 $highest_similarity = -INF;
3880 1986
@@ -3895,14 +2001,11 @@
3895 2001 }
3896 2002
3897 2003 return [];
3898 2004 }
3899 -
3900 -
2005 +// Add this to your class
3901 2006 public function handle_pdf_upload() {
3902 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3903 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3904 - }
2007 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
3905 2008
3906 2009 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3907 2010 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3908 2011 return;
@@ -3907,29 +2010,12 @@
3907 2010 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3908 2011 return;
3909 2012 }
3910 2013
3911 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3912 - $options = get_option('mxchat_options', array());
3913 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3914 -
3915 - if ($show_pdf_button !== 'on') {
3916 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3917 - return;
3918 - }
3919 -
3920 2014 $file = $_FILES['pdf_file'];
3921 2015 $session_id = sanitize_text_field($_POST['session_id']);
3922 2016 $original_filename = sanitize_text_field($file['name']);
3923 2017
3924 - // Update session owner if it changed (e.g. IP changed due to network switch)
3925 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3926 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3927 -
3928 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3929 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3930 - }
3931 -
3932 2018 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3933 2019 if ($file_type['type'] !== 'application/pdf') {
3934 2020 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3935 2021 return;
@@ -3935,12 +2021,9 @@
3935 2021 return;
3936 2022 }
3937 2023
3938 2024 $upload_dir = wp_upload_dir();
3939 -
3940 - // SECURITY FIX: Generate random filename without exposing session_id
3941 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3942 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2025 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
3943 2026 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3944 2027
3945 2028 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3946 2029 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -3971,9 +2054,8 @@
3971 2054 return;
3972 2055 }
3973 2056
3974 2057 if (!empty($embeddings)) {
3975 - // Store the mapping between session and the random filename
3976 2058 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3977 2059 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3978 2060 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3979 2061 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -3994,11 +2076,9 @@
3994 2076 wp_send_json_error($error_message);
3995 2077 return;
3996 2078 }
3997 2079 public function handle_pdf_remove() {
3998 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3999 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4000 - }
2080 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
4001 2081
4002 2082 if (empty($_POST['session_id'])) {
4003 2083 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
4004 2084 wp_die();
@@ -4019,8 +2099,10 @@
4019 2099 wp_die();
4020 2100 }
4021 2101
4022 2102
2103 +
2104 +
4023 2105 function mxchat_fetch_new_messages() {
4024 2106 $session_id = sanitize_text_field($_POST['session_id']);
4025 2107 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
4026 2108 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -4026,9 +2108,9 @@
4026 2108 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
4027 2109 $initial_timestamp = isset($_POST['initial_timestamp']) ? intval($_POST['initial_timestamp']) : 0;
4028 2110
4029 2111 if (empty($session_id)) {
4030 - //error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
2112 + error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
4031 2113 wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
4032 2114 wp_die();
4033 2115 }
4034 2116
@@ -4033,31 +2115,14 @@
4033 2115 }
4034 2116
4035 2117 $history = get_option("mxchat_history_{$session_id}", []);
4036 2118
4037 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
4038 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
4039 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
4040 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
4041 -
4042 2119 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
4043 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
4044 -
4045 2120 // If persistence is enabled, show all new messages
4046 2121 if ($persistence_enabled) {
4047 - $has_id = !empty($message['id']);
4048 - $is_agent = $message['role'] === 'agent';
4049 -
4050 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
4051 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
4052 - $is_newer = true;
4053 - } else {
4054 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
4055 - }
4056 -
4057 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
4058 -
4059 - return $has_id && $is_newer && $is_agent;
2122 + return !empty($message['id']) &&
2123 + strcmp($message['id'], $last_seen_id) > 0 &&
2124 + $message['role'] === 'agent';
4060 2125 }
4061 2126
4062 2127 // If persistence is disabled, only show messages after initial timestamp
4063 2128 return !empty($message['id']) &&
@@ -4064,19 +2129,17 @@
4064 2129 $message['role'] === 'agent' &&
4065 2130 $message['timestamp'] > $initial_timestamp;
4066 2131 });
4067 2132
4068 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2133 + error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
4069 2134
4070 - // Include current chat mode so frontend can detect agent→AI transitions
4071 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
4072 -
4073 2135 wp_send_json_success([
4074 - 'new_messages' => array_values($new_messages),
4075 - 'chat_mode' => $chat_mode
2136 + 'new_messages' => array_values($new_messages)
4076 2137 ]);
4077 2138 wp_die();
4078 2139 }
2140 +
2141 +
4079 2142 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
4080 2143 // First check if live agents are available
4081 2144 $live_agent_available = $this->options['live_agent_status'] ?? 'off';
4082 2145 if ($live_agent_available !== 'on') {
@@ -4095,101 +2158,18 @@
4095 2158 ]);
4096 2159 wp_die();
4097 2160 }
4098 2161
4099 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4100 -
4101 - if (empty($slack_bot_token)) {
2162 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2163 + if (empty($slack_webhook_url)) {
4102 2164 return false;
4103 2165 }
4104 2166
4105 - // Check if channel already exists for this session
4106 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4107 -
4108 - if (empty($channel_id)) {
4109 - // Create new channel with session ID as name
4110 - $channel_name = $this->generate_channel_name($session_id);
4111 -
4112 - //error_log("Attempting to create channel: $channel_name");
4113 -
4114 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4115 - 'headers' => [
4116 - 'Content-Type' => 'application/json',
4117 - 'Authorization' => 'Bearer ' . $slack_bot_token
4118 - ],
4119 - 'body' => json_encode([
4120 - 'name' => $channel_name,
4121 - 'is_private' => false // Public channel - anyone in workspace can join
4122 - ])
4123 - ]);
4124 -
4125 - if (!is_wp_error($response)) {
4126 - $response_body = wp_remote_retrieve_body($response);
4127 - $response_data = json_decode($response_body, true);
4128 -
4129 - //error_log("Channel creation response: " . $response_body);
4130 -
4131 - if (isset($response_data['ok']) && $response_data['ok']) {
4132 - $channel_id = $response_data['channel']['id'];
4133 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4134 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4135 - update_option("mxchat_channel_{$session_id}", $channel_id);
4136 -
4137 - // Auto-invite agents to the channel
4138 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4139 -
4140 - if (!empty($agent_user_ids)) {
4141 - // Parse user IDs (one per line)
4142 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4143 -
4144 - foreach ($user_ids as $user_id_to_invite) {
4145 - //error_log("Inviting user to channel: $user_id_to_invite");
4146 -
4147 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4148 - 'headers' => [
4149 - 'Content-Type' => 'application/json',
4150 - 'Authorization' => 'Bearer ' . $slack_bot_token
4151 - ],
4152 - 'body' => json_encode([
4153 - 'channel' => $channel_id,
4154 - 'users' => $user_id_to_invite
4155 - ])
4156 - ]);
4157 -
4158 - if (!is_wp_error($invite_response)) {
4159 - $invite_body = wp_remote_retrieve_body($invite_response);
4160 - $invite_data = json_decode($invite_body, true);
4161 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4162 -
4163 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4164 - //error_log("Successfully invited user $user_id_to_invite to channel");
4165 - } else {
4166 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4167 - }
4168 - } else {
4169 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4170 - }
4171 - }
4172 - } else {
4173 - //error_log("No agent user IDs configured for auto-invite");
4174 - }
4175 - } else {
4176 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4177 - }
4178 - } else {
4179 - //error_log("WP Error creating channel: " . $response->get_error_message());
4180 - }
4181 -
4182 - if (empty($channel_id)) {
4183 - return false; // Failed to create channel
4184 - }
4185 - }
4186 -
4187 - // Get recent chat history
2167 + // Get recent chat history (last 5 messages)
4188 2168 $history = get_option("mxchat_history_{$session_id}", []);
4189 - $recent_history = array_slice($history, -5);
2169 + $recent_history = array_slice($history, -5); // Get last 5 messages
4190 2170
4191 - // Format conversation context
2171 + // Format conversation history
4192 2172 $conversation_context = "";
4193 2173 if (!empty($recent_history)) {
4194 2174 $conversation_context = "*Recent Conversation:*\n";
4195 2175 foreach ($recent_history as $hist_message) {
@@ -4200,284 +2180,84 @@
4200 2180 }
4201 2181
4202 2182 update_option("mxchat_mode_{$session_id}", 'agent');
4203 2183
4204 - // Send message to channel
4205 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4206 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4207 - $channel_message .= "*User ID:* `{$user_id}`\n\n";
4208 -
2184 + $webhook_data = [
2185 + 'blocks' => [
2186 + [
2187 + 'type' => 'header',
2188 + 'text' => [
2189 + 'type' => 'plain_text',
2190 + 'text' => '🔔 New Live Agent Request',
2191 + 'emoji' => true
2192 + ]
2193 + ],
2194 + [
2195 + 'type' => 'section',
2196 + 'fields' => [
2197 + [
2198 + 'type' => 'mrkdwn',
2199 + 'text' => sprintf('*User ID:*\n`%s`', $user_id)
2200 + ],
2201 + [
2202 + 'type' => 'mrkdwn',
2203 + 'text' => sprintf('*Session ID:*\n`%s`', $session_id)
2204 + ]
2205 + ]
2206 + ]
2207 + ]
2208 + ];
2209 +
2210 + // Add conversation history if exists
4209 2211 if (!empty($conversation_context)) {
4210 - $channel_message .= $conversation_context;
2212 + $webhook_data['blocks'][] = [
2213 + 'type' => 'section',
2214 + 'text' => [
2215 + 'type' => 'mrkdwn',
2216 + 'text' => $conversation_context
2217 + ]
2218 + ];
4211 2219 }
4212 -
4213 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4214 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
4215 2220
4216 - wp_remote_post('https://slack.com/api/chat.postMessage', [
2221 + // Add the current message
2222 + $webhook_data['blocks'][] = [
2223 + 'type' => 'section',
2224 + 'text' => [
2225 + 'type' => 'mrkdwn',
2226 + 'text' => sprintf('*Current Message:*\n%s', $message)
2227 + ]
2228 + ];
2229 +
2230 + // Add the reply button
2231 + $webhook_data['blocks'][] = [
2232 + 'type' => 'actions',
2233 + 'elements' => [
2234 + [
2235 + 'type' => 'button',
2236 + 'text' => [
2237 + 'type' => 'plain_text',
2238 + 'text' => '✍️ Reply',
2239 + 'emoji' => true
2240 + ],
2241 + 'value' => $session_id,
2242 + 'action_id' => 'reply_to_user',
2243 + 'style' => 'primary'
2244 + ]
2245 + ]
2246 + ];
2247 +
2248 + $response = wp_remote_post($slack_webhook_url, [
2249 + 'body' => json_encode($webhook_data),
4217 2250 'headers' => [
4218 2251 'Content-Type' => 'application/json',
4219 - 'Authorization' => 'Bearer ' . $slack_bot_token
4220 2252 ],
4221 - 'body' => json_encode([
4222 - 'channel' => $channel_id,
4223 - 'text' => $channel_message,
4224 - 'mrkdwn' => true
4225 - ])
4226 2253 ]);
4227 2254
4228 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4229 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4230 -
4231 - $this->fallbackResponse = [
4232 - 'text' => $success_message,
4233 - 'html' => '',
4234 - 'images' => [],
4235 - 'chat_mode' => 'agent'
4236 - ];
4237 -
4238 - wp_send_json([
4239 - 'success' => true,
4240 - 'text' => $success_message,
4241 - 'html' => '',
4242 - 'chat_mode' => 'agent',
4243 - 'session_id' => $session_id,
4244 - 'fallbackResponse' => $this->fallbackResponse
4245 - ]);
4246 - wp_die();
4247 -}
4248 -
4249 -private function generate_channel_name($session_id) {
4250 - $email = null;
4251 - $name = null;
4252 -
4253 - // 1. First priority: Check if user is logged in and get their info
4254 - if (is_user_logged_in()) {
4255 - $current_user = wp_get_current_user();
4256 - if (!empty($current_user->user_email)) {
4257 - $email = $current_user->user_email;
4258 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4259 - }
4260 - if (!empty($current_user->display_name)) {
4261 - $name = $current_user->display_name;
4262 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4263 - }
4264 - }
4265 -
4266 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4267 - if (empty($email)) {
4268 - $email_option_key = "mxchat_email_{$session_id}";
4269 - $saved_email = get_option($email_option_key);
4270 - if (!empty($saved_email)) {
4271 - $email = $saved_email;
4272 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4273 - }
4274 - }
4275 -
4276 - if (empty($name)) {
4277 - $name_option_key = "mxchat_name_{$session_id}";
4278 - $saved_name = get_option($name_option_key);
4279 - if (!empty($saved_name)) {
4280 - $name = $saved_name;
4281 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4282 - }
4283 - }
4284 -
4285 - // 3. Third priority: Check existing chat transcript for email/name
4286 - if (empty($email) || empty($name)) {
4287 - global $wpdb;
4288 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4289 - $existing_data = $wpdb->get_row($wpdb->prepare(
4290 - "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",
4291 - $session_id
4292 - ));
4293 -
4294 - if ($existing_data) {
4295 - if (empty($email) && !empty($existing_data->user_email)) {
4296 - $email = $existing_data->user_email;
4297 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4298 - }
4299 - if (empty($name) && !empty($existing_data->user_name)) {
4300 - $name = $existing_data->user_name;
4301 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4302 - }
4303 - }
4304 - }
4305 -
4306 - // 4. Generate channel name based on priority: Name > Email > Session ID
4307 - $channel_name = '';
4308 -
4309 - if (!empty($name)) {
4310 - // Convert name to valid Slack channel name
4311 - $base_name = strtolower(trim($name));
4312 - // Replace spaces and invalid characters
4313 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4314 - $base_name = preg_replace('/\s+/', '-', $base_name);
4315 - $base_name = trim($base_name, '-');
4316 -
4317 - // Get last 4 characters of session ID for uniqueness
4318 - $session_suffix = substr($session_id, -4);
4319 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4320 -
4321 - // Slack channel names have a 21 character limit
4322 - if (strlen($channel_name) > 21) {
4323 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4324 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4325 - $truncated_name = substr($base_name, 0, $available_space);
4326 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4327 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4328 - }
4329 -
4330 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4331 -
4332 - } elseif (!empty($email)) {
4333 - // Convert email to valid Slack channel name (your existing logic)
4334 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4335 - // Remove any remaining invalid characters
4336 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4337 - // Ensure it doesn't end with a hyphen
4338 - $channel_name = rtrim($channel_name, '-');
4339 - // Slack channel names have a 21 character limit, so truncate if needed
4340 - if (strlen($channel_name) > 21) {
4341 - $channel_name = substr($channel_name, 0, 21);
4342 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4343 - }
4344 -
4345 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4346 -
4347 - } else {
4348 - // Fallback to session ID if no name or email found
4349 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4350 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4351 - }
4352 -
4353 - // Final validation - ensure channel name meets Slack requirements
4354 - if (strlen($channel_name) > 21) {
4355 - $channel_name = substr($channel_name, 0, 21);
4356 - $channel_name = rtrim($channel_name, '-');
4357 - }
4358 -
4359 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4360 - return $channel_name;
4361 -}
4362 -
4363 -/**
4364 - * Telegram Live Agent Handover
4365 - * Creates a forum topic in the Telegram group and notifies agents
4366 - */
4367 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4368 - // Check if Telegram agents are available
4369 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4370 - if ($telegram_available !== 'on') {
4371 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4372 - $this->fallbackResponse = [
4373 - 'text' => $away_message,
4374 - 'html' => '',
4375 - 'images' => [],
4376 - 'chat_mode' => 'ai'
4377 - ];
4378 - wp_send_json([
4379 - 'text' => $away_message,
4380 - 'html' => '',
4381 - 'chat_mode' => 'ai',
4382 - 'session_id' => $session_id
4383 - ]);
4384 - wp_die();
4385 - }
4386 -
4387 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4388 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4389 -
4390 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
2255 + if (is_wp_error($response)) {
4391 2256 return false;
4392 2257 }
4393 2258
4394 - // Check if topic already exists for this session
4395 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4396 -
4397 - if (empty($topic_id)) {
4398 - // Generate topic name
4399 - $topic_name = $this->generate_telegram_topic_name($session_id);
4400 -
4401 - // Random icon color (Telegram forum topic colors)
4402 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4403 - $icon_color = $icon_colors[array_rand($icon_colors)];
4404 -
4405 - // Create forum topic
4406 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4407 - 'headers' => ['Content-Type' => 'application/json'],
4408 - 'body' => json_encode([
4409 - 'chat_id' => $telegram_group_id,
4410 - 'name' => $topic_name,
4411 - 'icon_color' => $icon_color
4412 - ])
4413 - ]);
4414 -
4415 - if (!is_wp_error($response)) {
4416 - $response_body = wp_remote_retrieve_body($response);
4417 - $response_data = json_decode($response_body, true);
4418 -
4419 - if (isset($response_data['ok']) && $response_data['ok']) {
4420 - $topic_id = $response_data['result']['message_thread_id'];
4421 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4422 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4423 - }
4424 - }
4425 -
4426 - if (empty($topic_id)) {
4427 - return false; // Failed to create topic
4428 - }
4429 - }
4430 -
4431 - // Get recent chat history
4432 - $history = get_option("mxchat_history_{$session_id}", []);
4433 - $recent_history = array_slice($history, -5);
4434 -
4435 - // Format conversation context for Telegram (HTML format)
4436 - $conversation_context = "";
4437 - if (!empty($recent_history)) {
4438 - $conversation_context = "<b>Recent Conversation:</b>\n";
4439 - foreach ($recent_history as $hist_message) {
4440 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4441 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4442 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4443 - }
4444 - $conversation_context .= "\n";
4445 - }
4446 -
4447 - // Get user info
4448 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4449 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4450 -
4451 - // Update session mode
4452 - update_option("mxchat_mode_{$session_id}", 'agent');
4453 -
4454 - // Send initial message to topic
4455 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4456 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4457 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4458 - $topic_message .= "<b>User:</b> {$user_name}\n";
4459 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4460 -
4461 - if (!empty($conversation_context)) {
4462 - $topic_message .= $conversation_context;
4463 - }
4464 -
4465 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4466 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4467 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4468 -
4469 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4470 - 'headers' => ['Content-Type' => 'application/json'],
4471 - 'body' => json_encode([
4472 - 'chat_id' => $telegram_group_id,
4473 - 'message_thread_id' => $topic_id,
4474 - 'text' => $topic_message,
4475 - 'parse_mode' => 'HTML'
4476 - ])
4477 - ]);
4478 -
4479 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
2259 + $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4480 2260 $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4481 2261
4482 2262 $this->fallbackResponse = [
4483 2263 'text' => $success_message,
@@ -4495,284 +2275,85 @@
4495 2275 'fallbackResponse' => $this->fallbackResponse
4496 2276 ]);
4497 2277 wp_die();
4498 2278 }
4499 -
4500 -/**
4501 - * Generate topic name for Telegram forum
4502 - */
4503 -private function generate_telegram_topic_name($session_id) {
4504 - $name = null;
4505 - $email = null;
4506 -
4507 - // Check logged in user
4508 - if (is_user_logged_in()) {
4509 - $current_user = wp_get_current_user();
4510 - if (!empty($current_user->display_name)) {
4511 - $name = $current_user->display_name;
4512 - }
4513 - if (!empty($current_user->user_email)) {
4514 - $email = $current_user->user_email;
4515 - }
4516 - }
4517 -
4518 - // Check session data
4519 - if (empty($name)) {
4520 - $name = get_option("mxchat_name_{$session_id}");
4521 - }
4522 - if (empty($email)) {
4523 - $email = get_option("mxchat_email_{$session_id}");
4524 - }
4525 -
4526 - // Generate topic name
4527 - $session_suffix = substr($session_id, -6);
4528 -
4529 - if (!empty($name)) {
4530 - // Clean name for topic (max 128 chars in Telegram)
4531 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4532 - $clean_name = trim($clean_name);
4533 - if (strlen($clean_name) > 50) {
4534 - $clean_name = substr($clean_name, 0, 50);
4535 - }
4536 - return "Chat - {$clean_name} ({$session_suffix})";
4537 - } elseif (!empty($email)) {
4538 - // Use email prefix
4539 - $email_prefix = explode('@', $email)[0];
4540 - if (strlen($email_prefix) > 30) {
4541 - $email_prefix = substr($email_prefix, 0, 30);
4542 - }
4543 - return "Chat - {$email_prefix} ({$session_suffix})";
4544 - }
4545 -
4546 - return "Chat - {$session_suffix}";
4547 -}
4548 -
4549 -/**
4550 - * Send user message to Telegram agent
4551 - */
4552 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4553 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4554 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4555 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4556 -
4557 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4558 - return false;
4559 - }
4560 -
4561 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4562 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4563 -
4564 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4565 - 'headers' => ['Content-Type' => 'application/json'],
4566 - 'body' => json_encode([
4567 - 'chat_id' => $group_id,
4568 - 'message_thread_id' => $topic_id,
4569 - 'text' => $user_message,
4570 - 'parse_mode' => 'HTML'
4571 - ])
4572 - ]);
4573 -
4574 - return !is_wp_error($response);
4575 -}
4576 -
4577 -/**
4578 - * Handle incoming Telegram webhook
4579 - */
4580 -public function handle_telegram_webhook(WP_REST_Request $request) {
4581 - $body = $request->get_body();
4582 - $data = json_decode($body, true);
4583 -
4584 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4585 -
4586 - // Handle message events from forum topics
4587 - if (isset($data['message'])) {
4588 - $message_data = $data['message'];
4589 -
4590 - // Skip if not from a forum topic
4591 - if (!isset($message_data['message_thread_id'])) {
4592 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4593 - return new WP_REST_Response(['ok' => true]);
4594 - }
4595 -
4596 - // Skip bot messages
4597 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4598 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4599 - return new WP_REST_Response(['ok' => true]);
4600 - }
4601 -
4602 - $chat_id = $message_data['chat']['id'] ?? '';
4603 - $topic_id = $message_data['message_thread_id'];
4604 - $message_text = $message_data['text'] ?? '';
4605 - $message_id = $message_data['message_id'] ?? '';
4606 - $from = $message_data['from'] ?? [];
4607 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4608 - if (empty($agent_name)) {
4609 - $agent_name = $from['username'] ?? 'Agent';
4610 - }
4611 -
4612 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4613 -
4614 - // Skip empty messages
4615 - if (empty($message_text)) {
4616 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4617 - return new WP_REST_Response(['ok' => true]);
4618 - }
4619 -
4620 - // Find session ID by topic ID - cast to string for comparison
4621 - global $wpdb;
4622 - $topic_id_str = strval($topic_id);
4623 - $session_option = $wpdb->get_var(
4624 - $wpdb->prepare(
4625 - "SELECT option_name FROM {$wpdb->options}
4626 - WHERE option_name LIKE %s
4627 - AND option_value = %s",
4628 - 'mxchat_telegram_topic_%',
4629 - $topic_id_str
4630 - )
4631 - );
4632 -
4633 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4634 -
4635 - if ($session_option) {
4636 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4637 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4638 -
4639 - // Verify the group ID matches
4640 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4641 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4642 -
4643 - if (strval($stored_group_id) != strval($chat_id)) {
4644 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4645 - return new WP_REST_Response(['ok' => true]);
4646 - }
4647 -
4648 - // Check for closure commands
4649 - $lower_text = strtolower(trim($message_text));
4650 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4651 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4652 - // End the live agent session
4653 - update_option("mxchat_mode_{$session_id}", 'ai');
4654 -
4655 - // Save disconnect message
4656 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4657 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4658 -
4659 - // Notify in Telegram
4660 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4661 - if (!empty($telegram_bot_token)) {
4662 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4663 - 'headers' => ['Content-Type' => 'application/json'],
4664 - 'body' => json_encode([
4665 - 'chat_id' => $chat_id,
4666 - 'message_thread_id' => $topic_id,
4667 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4668 - 'parse_mode' => 'HTML'
4669 - ])
4670 - ]);
4671 -
4672 - // Optionally close the topic
4673 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4674 - 'headers' => ['Content-Type' => 'application/json'],
4675 - 'body' => json_encode([
4676 - 'chat_id' => $chat_id,
4677 - 'message_thread_id' => $topic_id
4678 - ])
4679 - ]);
4680 - }
4681 -
4682 - return new WP_REST_Response(['ok' => true]);
4683 - }
4684 -
4685 - // Deduplicate messages
4686 - $message_key = md5($session_id . $message_id . $message_text);
4687 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4688 -
4689 - if (in_array($message_key, $processed_messages)) {
4690 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4691 - return new WP_REST_Response(['ok' => true]);
4692 - }
4693 -
4694 - $processed_messages[] = $message_key;
4695 - if (count($processed_messages) > 50) {
4696 - $processed_messages = array_slice($processed_messages, -50);
4697 - }
4698 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4699 -
4700 - // Save the agent message - format with agent name prefix for proper parsing
4701 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4702 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4703 -
4704 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4705 -
4706 - // Verify the message was saved to history
4707 - $history = get_option("mxchat_history_{$session_id}", []);
4708 - $last_message = end($history);
4709 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4710 -
4711 - // Send confirmation back to Telegram
4712 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4713 - if (!empty($telegram_bot_token)) {
4714 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4715 - if (!get_transient($confirm_key)) {
4716 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4717 - 'headers' => ['Content-Type' => 'application/json'],
4718 - 'body' => json_encode([
4719 - 'chat_id' => $chat_id,
4720 - 'message_thread_id' => $topic_id,
4721 - 'text' => "✅ <i>Message sent to user</i>",
4722 - 'parse_mode' => 'HTML',
4723 - 'reply_to_message_id' => $message_id
4724 - ])
4725 - ]);
4726 - set_transient($confirm_key, true, 300);
4727 - }
4728 - }
4729 - } else {
4730 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4731 - }
4732 - } else {
4733 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4734 - }
4735 -
4736 - return new WP_REST_Response(['ok' => true]);
4737 -}
4738 -
4739 2279 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4740 - // Check if this is a Telegram agent session
4741 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4742 - if (!empty($telegram_topic_id)) {
4743 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4744 - }
2280 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
4745 2281
4746 - // Otherwise, try Slack
4747 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4748 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4749 -
4750 - if (empty($slack_bot_token) || empty($channel_id)) {
2282 + if (empty($slack_webhook_url)) {
2283 + error_log(esc_html__('Slack Webhook URL is not configured.', 'mxchat'));
4751 2284 return false;
4752 2285 }
4753 2286
4754 - $user_message = "💬 *User:* {$message}";
2287 + $webhook_data = [
2288 + 'blocks' => [
2289 + [
2290 + 'type' => 'header',
2291 + 'text' => [
2292 + 'type' => 'plain_text',
2293 + 'text' => esc_html__('📩 New Chat Message', 'mxchat'),
2294 + 'emoji' => true
2295 + ]
2296 + ],
2297 + [
2298 + 'type' => 'section',
2299 + 'fields' => [
2300 + [
2301 + 'type' => 'mrkdwn',
2302 + 'text' => sprintf(esc_html__('*User ID:*\n`%s`', 'mxchat'), $user_id)
2303 + ],
2304 + [
2305 + 'type' => 'mrkdwn',
2306 + 'text' => sprintf(esc_html__('*Session ID:*\n`%s`', 'mxchat'), $session_id)
2307 + ]
2308 + ]
2309 + ],
2310 + [
2311 + 'type' => 'section',
2312 + 'text' => [
2313 + 'type' => 'mrkdwn',
2314 + 'text' => sprintf(esc_html__('*Message:*\n%s', 'mxchat'), $message)
2315 + ]
2316 + ],
2317 + [
2318 + 'type' => 'actions',
2319 + 'elements' => [
2320 + [
2321 + 'type' => 'button',
2322 + 'text' => [
2323 + 'type' => 'plain_text',
2324 + 'text' => esc_html__('✍️ Reply', 'mxchat'),
2325 + 'emoji' => true
2326 + ],
2327 + 'value' => $session_id,
2328 + 'action_id' => 'reply_to_user',
2329 + 'style' => 'primary'
2330 + ]
2331 + ]
2332 + ]
2333 + ]
2334 + ];
4755 2335
4756 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
2336 + $response = wp_remote_post($slack_webhook_url, [
2337 + 'body' => json_encode($webhook_data),
4757 2338 'headers' => [
4758 2339 'Content-Type' => 'application/json',
4759 - 'Authorization' => 'Bearer ' . $slack_bot_token
4760 2340 ],
4761 - 'body' => json_encode([
4762 - 'channel' => $channel_id,
4763 - 'text' => $user_message,
4764 - 'mrkdwn' => true
4765 - ])
4766 2341 ]);
4767 2342
4768 - return !is_wp_error($response);
2343 + if (is_wp_error($response)) {
2344 + error_log(esc_html__('Error sending message to Slack: ', 'mxchat') . $response->get_error_message());
2345 + return false;
2346 + }
2347 +
2348 + error_log(esc_html__('Message sent to Slack successfully.', 'mxchat'));
2349 + return true;
4769 2350 }
4770 2351 public function handle_slack_interaction(WP_REST_Request $request) {
4771 - //error_log('Received Slack interaction');
2352 + error_log('Received Slack interaction');
4772 2353
4773 2354 $payload = json_decode($request->get_param('payload'), true);
4774 - //error_log('Payload: ' . print_r($payload, true));
2355 + error_log('Payload: ' . print_r($payload, true));
4775 2356
4776 2357 // Handle button click
4777 2358 if ($payload['type'] === 'block_actions' && $payload['actions'][0]['action_id'] === 'reply_to_user') {
4778 2359 $session_id = $payload['actions'][0]['value'];
@@ -4781,9 +2362,9 @@
4781 2362 // Get Bot Token from settings
4782 2363 $slack_token = $this->options['live_agent_bot_token'] ?? '';
4783 2364
4784 2365 if (empty($slack_token)) {
4785 - //error_log('Slack Bot Token not configured');
2366 + error_log('Slack Bot Token not configured');
4786 2367 return new WP_REST_Response(['error' => esc_html__('Bot token not configured', 'mxchat')], 400);
4787 2368 }
4788 2369 $response = wp_remote_post('https://slack.com/api/views.open', [
4789 2370 'headers' => [
@@ -4830,9 +2411,9 @@
4830 2411 ]
4831 2412 ])
4832 2413 ]);
4833 2414
4834 - //error_log('Views.open response: ' . print_r($response, true));
2415 + error_log('Views.open response: ' . print_r($response, true));
4835 2416
4836 2417 // Return immediate acknowledgment
4837 2418 return new WP_REST_Response(['ok' => true]);
4838 2419 }
@@ -4854,19 +2435,20 @@
4854 2435
4855 2436 // Default acknowledgment
4856 2437 return new WP_REST_Response(['ok' => true]);
4857 2438 }
2439 +
4858 2440 public function mxchat_handle_agent_response(WP_REST_Request $request) {
4859 - //error_log('Received agent response request');
4860 - //error_log('Request data: ' . print_r($request->get_params(), true));
4861 - // //error_log('Raw body: ' . file_get_contents('php://input'));
2441 + error_log('Received agent response request');
2442 + error_log('Request data: ' . print_r($request->get_params(), true));
2443 + // error_log('Raw body: ' . file_get_contents('php://input'));
4862 2444
4863 2445 // Get the data from Slack's slash command format
4864 2446 $command_text = $request->get_param('text');
4865 - // //error_log('Command text: ' . $command_text);
2447 + // error_log('Command text: ' . $command_text);
4866 2448
4867 2449 if (empty($command_text)) {
4868 - //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
2450 + error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
4869 2451 return new WP_REST_Response([
4870 2452 'error' => esc_html__('Command text is required. Format: /reply session_id message', 'mxchat')
4871 2453 ], 400);
4872 2454 }
@@ -4873,9 +2455,9 @@
4873 2455
4874 2456 // Split the command text into session_id and message
4875 2457 $parts = explode(' ', $command_text, 2);
4876 2458 if (count($parts) !== 2) {
4877 - //error_log('Agent response error: Invalid command format');
2459 + error_log('Agent response error: Invalid command format');
4878 2460 return new WP_REST_Response([
4879 2461 'error' => esc_html__('Invalid format. Use: /reply session_id message', 'mxchat')
4880 2462 ], 400);
4881 2463 }
@@ -4882,15 +2464,15 @@
4882 2464
4883 2465 $session_id = sanitize_text_field($parts[0]);
4884 2466 $message = sanitize_text_field($parts[1]);
4885 2467
4886 - //error_log("Processing agent response - Session ID: $session_id, Message: $message");
2468 + error_log("Processing agent response - Session ID: $session_id, Message: $message");
4887 2469
4888 2470 // Save the message
4889 2471 $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
4890 2472
4891 2473 if (!$message_id) {
4892 - // //error_log('Failed to save agent message');
2474 + // error_log('Failed to save agent message');
4893 2475 return new WP_REST_Response([
4894 2476 'error' => esc_html__('Failed to save message', 'mxchat')
4895 2477 ], 500);
4896 2478 }
@@ -4900,173 +2482,29 @@
4900 2482 'response_type' => 'in_channel',
4901 2483 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4902 2484 ], 200);
4903 2485 }
4904 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4905 - // Update mode to AI
4906 - update_option("mxchat_mode_{$session_id}", 'ai');
4907 -
4908 - // Clear any existing PDF context to start fresh
4909 - $this->clear_pdf_transients($session_id);
4910 -
4911 - // Set the response with explicit chat_mode
4912 - $this->fallbackResponse = [
4913 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4914 - 'html' => '',
4915 - 'images' => [],
4916 - 'chat_mode' => 'ai' // Ensure this is set
4917 - ];
4918 -
4919 - // Return the complete response array instead of just true
4920 - return $this->fallbackResponse;
4921 -}
4922 2486
4923 -public function handle_slack_messages(WP_REST_Request $request) {
4924 - // Log the incoming request for debugging
4925 - //error_log('Slack events request received: ' . $request->get_body());
4926 -
4927 - $body = $request->get_body();
4928 - $data = json_decode($body, true);
4929 -
4930 - // Handle Slack URL verification
4931 - if (isset($data['type']) && $data['type'] === 'url_verification') {
4932 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
4933 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
4934 - }
4935 -
4936 - // IMPORTANT: Handle Slack's event deduplication
4937 - if (isset($data['event_id'])) {
4938 - $event_id = $data['event_id'];
4939 - $processed_events = get_transient('mxchat_slack_events') ?: [];
4940 -
4941 - // Check if we've already processed this event
4942 - if (in_array($event_id, $processed_events)) {
4943 - //error_log("Duplicate event detected: $event_id");
4944 - return new WP_REST_Response(['ok' => true]);
4945 - }
4946 -
4947 - // Add this event to processed list
4948 - $processed_events[] = $event_id;
4949 - // Keep only last 100 events to prevent memory issues
4950 - if (count($processed_events) > 100) {
4951 - $processed_events = array_slice($processed_events, -100);
4952 - }
4953 - // Store for 1 hour
4954 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
4955 - }
4956 -
4957 - // Handle message events
4958 - if (isset($data['event']) && $data['event']['type'] === 'message') {
4959 - $event = $data['event'];
4960 -
4961 - // Skip bot messages and messages with subtypes (like bot_message)
4962 - if (isset($event['bot_id']) || isset($event['subtype'])) {
4963 - return new WP_REST_Response(['ok' => true]);
4964 - }
4965 -
4966 - // Additional check: Skip if this is a threaded reply to our confirmation
4967 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
4968 - return new WP_REST_Response(['ok' => true]);
4969 - }
4970 -
4971 - $channel_id = $event['channel'];
4972 - $message_text = $event['text'] ?? '';
4973 - $message_ts = $event['ts'] ?? '';
4974 2487
4975 - // Find session ID by looking for matching channel
4976 - global $wpdb;
4977 - $session_option = $wpdb->get_var(
4978 - $wpdb->prepare(
4979 - "SELECT option_name FROM {$wpdb->options}
4980 - WHERE option_name LIKE 'mxchat_channel_%'
4981 - AND option_value = %s",
4982 - $channel_id
4983 - )
4984 - );
2488 +public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
2489 + error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
4985 2490
4986 - if ($session_option) {
4987 - $session_id = str_replace('mxchat_channel_', '', $session_option);
2491 + // Just update mode to AI
2492 + update_option("mxchat_mode_{$session_id}", 'ai');
4988 2493
4989 - // Create a unique key for this specific message
4990 - $message_key = md5($session_id . $message_ts . $message_text);
4991 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
2494 + // Initialize states
2495 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2496 + $this->productCardHtml = '';
4992 2497
4993 - // Check if we've already processed this exact message
4994 - if (in_array($message_key, $processed_messages)) {
4995 - //error_log("Duplicate message detected for session $session_id");
4996 - return new WP_REST_Response(['ok' => true]);
4997 - }
2498 + // Set the response message
2499 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
4998 2500
4999 - // Add to processed messages
5000 - $processed_messages[] = $message_key;
5001 - // Keep only last 50 messages per session
5002 - if (count($processed_messages) > 50) {
5003 - $processed_messages = array_slice($processed_messages, -50);
5004 - }
5005 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
2501 + return true; // Intent was handled
2502 +}
5006 2503
5007 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5008 2504
5009 - // Handle agent ending the chat — transfer back to AI
5010 - // Format: "!endchat" or "!endchat <custom message to user>"
5011 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
5012 - update_option("mxchat_mode_{$session_id}", 'ai');
5013 2505
5014 - // Extract custom message after !endchat, or use empty string
5015 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
5016 2506
5017 - // Send the agent's custom farewell message if provided
5018 - if (!empty($custom_message)) {
5019 - $this->mxchat_save_chat_message($session_id, 'agent', $custom_message);
5020 - }
5021 -
5022 - // Confirm in Slack channel
5023 - if (!empty($slack_bot_token)) {
5024 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5025 - 'headers' => [
5026 - 'Content-Type' => 'application/json',
5027 - 'Authorization' => 'Bearer ' . $slack_bot_token
5028 - ],
5029 - 'body' => json_encode([
5030 - 'channel' => $channel_id,
5031 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
5032 - 'mrkdwn' => true
5033 - ])
5034 - ]);
5035 - }
5036 -
5037 - return new WP_REST_Response(['ok' => true]);
5038 - }
5039 -
5040 - // Save the agent message
5041 - $this->mxchat_save_chat_message($session_id, 'agent', $message_text);
5042 -
5043 - // Send confirmation back to Slack (only once)
5044 - if (!empty($slack_bot_token)) {
5045 - // Use a transient to prevent duplicate confirmations
5046 - $confirm_key = 'mxchat_confirm_' . $message_key;
5047 - if (!get_transient($confirm_key)) {
5048 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5049 - 'headers' => [
5050 - 'Content-Type' => 'application/json',
5051 - 'Authorization' => 'Bearer ' . $slack_bot_token
5052 - ],
5053 - 'body' => json_encode([
5054 - 'channel' => $channel_id,
5055 - 'text' => "✅ _Message sent to user_",
5056 - 'thread_ts' => $event['ts'] // Reply in thread
5057 - ])
5058 - ]);
5059 - // Set transient to prevent duplicate confirmations
5060 - set_transient($confirm_key, true, 300); // 5 minutes
5061 - }
5062 - }
5063 - }
5064 - }
5065 -
5066 - return new WP_REST_Response(['ok' => true]);
5067 -}
5068 -
5069 2507 // For the word upload handler
5070 2508 public function mxchat_handle_word_upload() {
5071 2509 // Delegate to word handler
5072 2510 $this->word_handler->mxchat_handle_word_upload();
@@ -5089,860 +2527,261 @@
5089 2527 return MxChat_User::mxchat_get_user_identifier();
5090 2528 }
5091 2529
5092 2530 private function mxchat_generate_embedding($text, $api_key) {
5093 - try {
5094 - // Get options and selected model
5095 - $options = get_option('mxchat_options');
5096 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5097 -
5098 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
5099 - // Off by default so existing sites see byte-identical behavior.
5100 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5101 - return $this->mxchat_generate_embedding_custom($text);
5102 - }
5103 -
5104 - // Determine endpoint and API key based on model
5105 - if (strpos($selected_model, 'voyage') === 0) {
5106 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
5107 - $api_key = $options['voyage_api_key'] ?? '';
5108 -
5109 - // Check if Voyage API key is missing
5110 - if (empty($api_key)) {
5111 - //error_log('Voyage API key is missing');
5112 - return [
5113 - 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5114 - 'error_code' => 'missing_voyage_api_key'
5115 - ];
5116 - }
5117 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5118 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5119 - $api_key = $options['gemini_api_key'] ?? '';
5120 -
5121 - // Check if Gemini API key is missing
5122 - if (empty($api_key)) {
5123 - //error_log('Gemini API key is missing');
5124 - return [
5125 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5126 - 'error_code' => 'missing_gemini_api_key'
5127 - ];
5128 - }
5129 - } else {
5130 - $endpoint = 'https://api.openai.com/v1/embeddings';
5131 - // Use the passed API key for OpenAI
5132 -
5133 - // Check if OpenAI API key is missing
5134 - if (empty($api_key)) {
5135 - //error_log('OpenAI API key is missing');
5136 - return [
5137 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
5138 - 'error_code' => 'missing_openai_api_key'
5139 - ];
5140 - }
5141 - }
5142 -
5143 - // Check if text is empty
5144 - if (empty($text)) {
5145 - //error_log('Empty text provided for embedding generation');
5146 - return [
5147 - 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
5148 - 'error_code' => 'empty_embedding_text'
5149 - ];
5150 - }
5151 -
5152 - // Prepare request body based on provider
5153 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5154 - // Gemini API format
5155 - $request_body = [
5156 - 'model' => 'models/' . $selected_model,
5157 - 'content' => [
5158 - 'parts' => [
5159 - ['text' => $text]
5160 - ]
5161 - ],
5162 - 'outputDimensionality' => 1536
5163 - ];
5164 -
5165 - // Prepare headers for Gemini (API key as query parameter)
5166 - $endpoint .= '?key=' . $api_key;
5167 - $headers = [
5168 - 'Content-Type' => 'application/json'
5169 - ];
5170 - } else {
5171 - // OpenAI/Voyage API format
5172 - $request_body = [
5173 - 'input' => $text,
5174 - 'model' => $selected_model
5175 - ];
5176 -
5177 - // Add output_dimension for voyage-3-large
5178 - if ($selected_model === 'voyage-3-large') {
5179 - $request_body['output_dimension'] = 2048;
5180 - }
5181 -
5182 - // Prepare headers for OpenAI/Voyage
5183 - $headers = [
5184 - 'Content-Type' => 'application/json',
5185 - 'Authorization' => 'Bearer ' . $api_key
5186 - ];
5187 - }
5188 -
5189 - // Prepare request arguments
5190 - $args = [
5191 - 'body' => wp_json_encode($request_body),
5192 - 'headers' => $headers,
5193 - 'timeout' => 60,
5194 - 'redirection' => 5,
5195 - 'blocking' => true,
5196 - 'httpversion' => '1.0',
5197 - 'sslverify' => true,
5198 - ];
5199 -
5200 - // Make the request
5201 - $response = wp_remote_post($endpoint, $args);
5202 -
5203 - // Handle WordPress errors
5204 - if (is_wp_error($response)) {
5205 - $error_message = $response->get_error_message();
5206 - //error_log('Embedding Generation Error: ' . $error_message);
5207 - return [
5208 - 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
5209 - 'error_code' => 'embedding_connection_error'
5210 - ];
5211 - }
5212 -
5213 - // Check HTTP status code
5214 - $status_code = wp_remote_retrieve_response_code($response);
5215 - if ($status_code !== 200) {
5216 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5217 -
5218 - $error_message = isset($response_body['error']['message'])
5219 - ? $response_body['error']['message']
5220 - : 'HTTP Error ' . $status_code;
5221 -
5222 - $error_type = isset($response_body['error']['type'])
5223 - ? $response_body['error']['type']
5224 - : 'unknown';
5225 -
5226 - //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
5227 -
5228 - // Handle specific error types
5229 - switch ($error_type) {
5230 - case 'invalid_request_error':
5231 - if (strpos($error_message, 'API key') !== false) {
5232 - return [
5233 - 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
5234 - 'error_code' => 'embedding_invalid_api_key'
5235 - ];
5236 - }
5237 - break;
5238 -
5239 - case 'authentication_error':
5240 - return [
5241 - 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
5242 - 'error_code' => 'embedding_auth_error'
5243 - ];
5244 -
5245 - case 'rate_limit_exceeded':
5246 - return [
5247 - 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
5248 - 'error_code' => 'embedding_rate_limit'
5249 - ];
5250 -
5251 - case 'quota_exceeded':
5252 - return [
5253 - 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
5254 - 'error_code' => 'embedding_quota_exceeded'
5255 - ];
5256 - }
5257 -
5258 - // Generic error fallback
5259 - return [
5260 - 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
5261 - 'error_code' => 'embedding_api_error',
5262 - 'status_code' => $status_code
5263 - ];
5264 - }
5265 -
5266 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5267 -
5268 - // Handle different response formats based on provider
5269 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5270 - // Gemini API response format
5271 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5272 - return $response_body['embedding']['values'];
5273 - } else {
5274 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5275 - return [
5276 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5277 - 'error_code' => 'invalid_gemini_embedding_response'
5278 - ];
5279 - }
5280 - } else {
5281 - // OpenAI/Voyage API response format
5282 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5283 - return $response_body['data'][0]['embedding'];
5284 - } else {
5285 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5286 - return [
5287 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5288 - 'error_code' => 'invalid_embedding_response'
5289 - ];
5290 - }
5291 - }
5292 - } catch (Exception $e) {
5293 - //error_log('Embedding Exception: ' . $e->getMessage());
5294 - return [
5295 - 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
5296 - 'error_code' => 'embedding_exception'
5297 - ];
2531 + // Get options and selected model
2532 + $options = get_option('mxchat_options');
2533 + $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
2534 +
2535 + // Determine endpoint and API key based on model
2536 + if (strpos($selected_model, 'voyage') === 0) {
2537 + $endpoint = 'https://api.voyageai.com/v1/embeddings';
2538 + $api_key = $options['voyage_api_key'] ?? '';
2539 + } else {
2540 + $endpoint = 'https://api.openai.com/v1/embeddings';
2541 + // Use the passed API key for OpenAI
5298 2542 }
5299 -}
5300 -
5301 -
5302 -/**
5303 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5304 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5305 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5306 - */
5307 -private function mxchat_generate_embedding_custom($text) {
5308 - if (empty($text)) {
5309 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
2543 +
2544 + // Prepare request body with conditional output_dimension
2545 + $request_body = [
2546 + 'input' => $text,
2547 + 'model' => $selected_model
2548 + ];
2549 +
2550 + // Add output_dimension for voyage-3-large
2551 + if ($selected_model === 'voyage-3-large') {
2552 + $request_body['output_dimension'] = 2048;
5310 2553 }
5311 - $cfg = $this->mxchat_resolve_custom_provider();
5312 - if (empty($cfg['base_url'])) {
5313 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5314 - }
5315 -
5316 - $options = get_option('mxchat_options');
5317 - $embed_url = $cfg['base_url'] . '/embeddings';
5318 - if (!empty($cfg['api_version'])) {
5319 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5320 - }
5321 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5322 - ? trim((string) $options['custom_provider_embedding_model'])
5323 - : $cfg['model'];
5324 -
5325 - $response = wp_remote_post($embed_url, [
5326 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5327 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
2554 +
2555 + // Prepare request arguments
2556 + $args = [
2557 + 'body' => wp_json_encode($request_body),
2558 + 'headers' => [
2559 + 'Content-Type' => 'application/json',
2560 + 'Authorization' => 'Bearer ' . $api_key,
2561 + ],
5328 2562 'timeout' => 60,
5329 - ]);
2563 + 'redirection' => 5,
2564 + 'blocking' => true,
2565 + 'httpversion' => '1.0',
2566 + 'sslverify' => true,
2567 + ];
2568 +
2569 + // Make the request
2570 + $response = wp_remote_post($endpoint, $args);
2571 +
2572 + // Rest of your existing code...
5330 2573 if (is_wp_error($response)) {
5331 - return [
5332 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5333 - 'error_code' => 'embedding_custom_connection_error',
5334 - ];
2574 + error_log('Embedding Generation Error: ' . $response->get_error_message());
2575 + return null;
5335 2576 }
5336 - $status = wp_remote_retrieve_response_code($response);
5337 - $body = json_decode(wp_remote_retrieve_body($response), true);
5338 - if ($status !== 200) {
5339 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5340 - return [
5341 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5342 - 'error_code' => 'embedding_custom_api_error',
5343 - 'status_code' => $status,
5344 - ];
2577 +
2578 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
2579 +
2580 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
2581 + return $response_body['data'][0]['embedding'];
2582 + } else {
2583 + error_log('Invalid embedding response: ' . wp_json_encode($response_body));
2584 + return null;
5345 2585 }
5346 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5347 - return $body['data'][0]['embedding'];
5348 - }
5349 - return [
5350 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5351 - 'error_code' => 'embedding_custom_invalid_response',
5352 - ];
5353 2586 }
5354 2587
5355 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5356 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5357 2588
5358 - // Check for OpenAI Vector Store first (takes priority when enabled)
5359 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
2589 +private function mxchat_find_relevant_content($user_embedding) {
2590 + error_log('MXChat Vector Search: Starting content search...');
5360 2591
5361 - if ($bot_vectorstore_config['use_vectorstore']) {
5362 - // Get current model to verify it's an OpenAI model
5363 - $bot_options = $this->get_bot_options($bot_id);
5364 - $mxchat_options = get_option('mxchat_options', array());
5365 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5366 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
2592 + // Retrieve the add-on settings from the database.
2593 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5367 2594
5368 - if ($this->is_openai_chat_model($selected_model)) {
5369 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5370 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5371 - } else {
5372 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5373 - }
5374 - }
2595 + // Determine whether Pinecone is enabled.
2596 + // We expect the sanitized setting to be a string '1' if enabled, otherwise '0'.
2597 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5375 2598
5376 - // Get bot-specific Pinecone configuration
5377 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
2599 + error_log('Pinecone enabled flag: ' . $use_pinecone);
5378 2600
5379 - // Debug: Log the Pinecone configuration
5380 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5381 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5382 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5383 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5384 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5385 -
5386 - // Determine whether to use Pinecone based on bot configuration
5387 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5388 -
5389 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5390 -
5391 - if ($use_pinecone) {
5392 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
2601 + if ($use_pinecone === 1) {
2602 + error_log('MXChat Vector Search: Using Pinecone database');
2603 + return $this->find_relevant_content_pinecone($user_embedding);
5393 2604 } else {
5394 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
2605 + error_log('MXChat Vector Search: Using WordPress database');
2606 + return $this->find_relevant_content_wordpress($user_embedding);
5395 2607 }
5396 2608 }
5397 2609
5398 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
2610 +
2611 +private function find_relevant_content_wordpress($user_embedding) {
5399 2612 global $wpdb;
5400 2613 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5401 - // Initialize similarity analysis storage
5402 - $this->last_similarity_analysis = [
5403 - 'knowledge_base_type' => 'WordPress Database',
5404 - 'bot_id' => $bot_id,
5405 - 'top_matches' => [],
5406 - 'threshold_used' => 0,
5407 - 'total_checked' => 0
5408 - ];
2614 + $cache_key = 'mxchat_system_prompt_embeddings';
2615 + $batch_size = 500;
5409 2616
5410 - // NEW: Initialize valid URLs array
5411 - $valid_urls = [];
2617 + // Log start of matching process
2618 + error_log('[MXCHAT] Starting similarity matching process');
5412 2619
5413 - // Get bot-specific options for similarity threshold
5414 - $bot_options = $this->get_bot_options($bot_id);
5415 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
2620 + // Retrieve embeddings from cache or database
2621 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2622 + if ($embeddings === false) {
2623 + error_log('[MXCHAT] Cache miss - loading embeddings from database');
2624 + $embeddings = [];
2625 + $offset = 0;
5416 2626
5417 - // Get knowledge manager instance for role checking
5418 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2627 + // Load in batches and build cache
2628 + do {
2629 + $query = $wpdb->prepare(
2630 + "SELECT id, embedding_vector
2631 + FROM {$system_prompt_table}
2632 + LIMIT %d OFFSET %d",
2633 + $batch_size,
2634 + $offset
2635 + );
5419 2636
5420 - // Get base similarity threshold from bot options or default options
5421 - $similarity_threshold = isset($current_options['similarity_threshold'])
5422 - ? ((int) $current_options['similarity_threshold']) / 100
5423 - : 0.35;
5424 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5425 -
5426 - // Precompute bot_filter once, outside the streaming loop
5427 - $bot_filter = '';
5428 - if ($bot_id !== 'default') {
5429 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5430 - if ($column_exists) {
5431 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5432 - }
5433 - }
5434 -
5435 - // ===== STREAMING TOP-K PASS =====
5436 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5437 - // - top 10 by raw similarity (for the testing/debug display panel)
5438 - // - candidates above threshold with access (capped) for context assembly
5439 - // This bounds peak memory regardless of knowledge base size and avoids loading
5440 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5441 - $batch_size = 250;
5442 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5443 - $top_display = [];
5444 - $candidates = [];
5445 - $total_checked = 0;
5446 - $offset = 0;
5447 -
5448 - do {
5449 - $batch = $wpdb->get_results($wpdb->prepare(
5450 - "SELECT id, embedding_vector, source_url, role_restriction
5451 - FROM {$system_prompt_table}
5452 - WHERE 1=1 {$bot_filter}
5453 - LIMIT %d OFFSET %d",
5454 - $batch_size,
5455 - $offset
5456 - ));
5457 -
5458 - if (empty($batch)) {
5459 - break;
5460 - }
5461 -
5462 - foreach ($batch as $row) {
5463 - $database_embedding = $row->embedding_vector
5464 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5465 - : null;
5466 -
5467 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5468 - unset($database_embedding);
5469 - continue;
2637 + $batch = $wpdb->get_results($query);
2638 + if (empty($batch)) {
2639 + break;
5470 2640 }
5471 2641
5472 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5473 - unset($database_embedding);
2642 + $embeddings = array_merge($embeddings, $batch);
2643 + $offset += $batch_size;
5474 2644
5475 - $role_restriction = $row->role_restriction ?? 'public';
5476 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5477 - $source_url = $row->source_url ?? '';
2645 + // Free memory
2646 + unset($batch);
5478 2647
5479 - // Maintain top 10 display buffer (insert-if-beats-worst)
5480 - if (count($top_display) < 10) {
5481 - $top_display[] = [
5482 - 'id' => $row->id,
5483 - 'similarity' => $similarity,
5484 - 'source_url' => $source_url,
5485 - 'role_restriction' => $role_restriction,
5486 - 'has_access' => $has_access,
5487 - ];
5488 - usort($top_display, function ($a, $b) {
5489 - return $b['similarity'] <=> $a['similarity'];
5490 - });
5491 - } elseif ($similarity > $top_display[9]['similarity']) {
5492 - $top_display[9] = [
5493 - 'id' => $row->id,
5494 - 'similarity' => $similarity,
5495 - 'source_url' => $source_url,
5496 - 'role_restriction' => $role_restriction,
5497 - 'has_access' => $has_access,
5498 - ];
5499 - usort($top_display, function ($a, $b) {
5500 - return $b['similarity'] <=> $a['similarity'];
5501 - });
5502 - }
2648 + } while (true);
5503 2649
5504 - // Track candidates for context assembly (above threshold + has access)
5505 - if ($similarity >= $similarity_threshold && $has_access) {
5506 - $candidates[] = [
5507 - 'id' => $row->id,
5508 - 'similarity' => $similarity,
5509 - 'source_url' => $source_url,
5510 - ];
5511 - }
5512 -
5513 - $total_checked++;
2650 + if (empty($embeddings)) {
2651 + error_log('[MXCHAT] No embeddings found in database');
2652 + return ''; // Return an empty string if no embeddings found
5514 2653 }
5515 -
5516 - unset($batch);
5517 -
5518 - // Trim candidates periodically to cap memory during long scans
5519 - if (count($candidates) > $max_candidates) {
5520 - usort($candidates, function ($a, $b) {
5521 - return $b['similarity'] <=> $a['similarity'];
5522 - });
5523 - $candidates = array_slice($candidates, 0, $max_candidates);
5524 - }
5525 -
5526 - $offset += $batch_size;
5527 - } while (true);
5528 -
5529 - if ($total_checked === 0) {
5530 - $this->current_valid_urls = [];
5531 - return '';
2654 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2655 + error_log('[MXCHAT] Cached ' . count($embeddings) . ' embeddings');
2656 + } else {
2657 + error_log('[MXCHAT] Using ' . count($embeddings) . ' cached embeddings');
5532 2658 }
5533 2659
5534 - // Final candidates sort (best first)
5535 - if (count($candidates) > 1) {
5536 - usort($candidates, function ($a, $b) {
5537 - return $b['similarity'] <=> $a['similarity'];
5538 - });
5539 - }
5540 -
5541 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5542 - // Gather unique IDs we actually need (top_display + candidates) and pull
5543 - // article_content in bounded IN() batches. This avoids loading content for
5544 - // every row during the similarity scan.
5545 - $needed_ids = [];
5546 - foreach ($top_display as $item) {
5547 - $needed_ids[$item['id']] = true;
5548 - }
5549 - foreach ($candidates as $item) {
5550 - $needed_ids[$item['id']] = true;
5551 - }
5552 - $needed_ids = array_keys($needed_ids);
5553 -
5554 - $content_map = [];
5555 - if (!empty($needed_ids)) {
5556 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5557 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5558 - $rows = $wpdb->get_results($wpdb->prepare(
5559 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5560 - ...$chunk_ids
5561 - ));
5562 - foreach ($rows as $r) {
5563 - $content_map[$r->id] = $r->article_content;
2660 + // Initialize array to store relevant results with similarity scores
2661 + $relevant_results = [];
2662 +
2663 + // Get the similarity threshold from the main options array only
2664 + $main_options = get_option('mxchat_options', []);
2665 + $similarity_threshold = isset($main_options['similarity_threshold'])
2666 + ? ((int) $main_options['similarity_threshold']) / 100
2667 + : 0.8; // Default to 80%
2668 +
2669 + error_log('[MXCHAT] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
2670 +
2671 + // Iterate through embeddings to calculate similarity
2672 + foreach ($embeddings as $embedding) {
2673 + $database_embedding = $embedding->embedding_vector
2674 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2675 + : null;
2676 + if (is_array($database_embedding) && is_array($user_embedding)) {
2677 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
2678 +
2679 + // Log each similarity score over 0.5 to reduce log spam
2680 + if ($similarity > 0.1) {
2681 + error_log(sprintf('[MXCHAT] ID: %d | Similarity Score: %.4f', $embedding->id, $similarity));
5564 2682 }
5565 - unset($rows);
2683 +
2684 + $relevant_results[] = [
2685 + 'id' => $embedding->id,
2686 + 'similarity' => $similarity
2687 + ];
5566 2688 }
2689 + // Free memory
2690 + unset($database_embedding);
5567 2691 }
5568 2692
5569 - // Build the all_similarities display array from the top 10
5570 - $all_similarities = [];
5571 - foreach ($top_display as $item) {
5572 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5573 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5574 - $is_chunk = $parsed_for_display['is_chunked'];
5575 - $chunk_meta = $parsed_for_display['metadata'];
5576 -
5577 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5578 - $source_display = $item['source_url'];
5579 - } else {
5580 - $content_preview = strip_tags($article_content_for_parse);
5581 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5582 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5583 - }
5584 -
5585 - $all_similarities[] = [
5586 - 'document_id' => $item['id'],
5587 - 'similarity' => $item['similarity'],
5588 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5589 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5590 - 'source_display' => $source_display,
5591 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5592 - 'used_for_context' => false,
5593 - 'role_restriction' => $item['role_restriction'],
5594 - 'has_access' => $item['has_access'],
5595 - 'filtered_out' => !$item['has_access'],
5596 - 'is_chunk' => $is_chunk,
5597 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5598 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5599 - ];
5600 - }
5601 -
5602 - // Build url_groups from candidates for chunk reassembly
5603 - $url_groups = array();
5604 - foreach ($candidates as $cand) {
5605 - $article_content = $content_map[$cand['id']] ?? '';
5606 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5607 - $is_chunked = $parsed['is_chunked'];
5608 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5609 - $text_content = $parsed['text'];
5610 -
5611 - $source_url = $cand['source_url'];
5612 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5613 -
5614 - if (!isset($url_groups[$group_key])) {
5615 - $url_groups[$group_key] = array(
5616 - 'source_url' => $source_url,
5617 - 'best_score' => 0,
5618 - 'is_chunked' => $is_chunked,
5619 - 'chunks' => array(),
5620 - 'single_text' => '',
5621 - 'single_id' => null
5622 - );
5623 - }
5624 -
5625 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5626 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5627 - }
5628 -
5629 - if ($is_chunked) {
5630 - $url_groups[$group_key]['is_chunked'] = true;
5631 - $url_groups[$group_key]['chunks'][] = array(
5632 - 'id' => $cand['id'],
5633 - 'score' => $cand['similarity'],
5634 - 'chunk_index' => $chunk_index,
5635 - 'text' => $text_content
5636 - );
5637 - } else {
5638 - $url_groups[$group_key]['single_text'] = $text_content;
5639 - $url_groups[$group_key]['single_id'] = $cand['id'];
5640 - }
5641 - }
5642 -
5643 - // Sort ALL similarities for testing display (highest first)
5644 - usort($all_similarities, function ($a, $b) {
2693 + // Filter and sort relevant results by similarity
2694 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2695 + return $result['similarity'] >= $similarity_threshold;
2696 + });
2697 + usort($relevant_results, function ($a, $b) {
5645 2698 return $b['similarity'] <=> $a['similarity'];
5646 2699 });
5647 2700
5648 - // Sort URL groups by best score (highest first)
5649 - uasort($url_groups, function($a, $b) {
5650 - return $b['best_score'] <=> $a['best_score'];
5651 - });
2701 + // Log number of results that met threshold
2702 + error_log('[MXCHAT] ' . count($relevant_results) . ' results met the similarity threshold');
5652 2703
5653 - // Get RAG sources limit from options (default 6, min 3, max 10)
5654 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5655 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5656 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
2704 + // Limit to the top 5 results
2705 + $top_results = array_slice($relevant_results, 0, 5);
5657 2706
5658 - // Take top N unique URLs based on user setting
5659 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
2707 + // Log the top matches
2708 + error_log('[MXCHAT] Top matching results:');
2709 + foreach ($top_results as $index => $result) {
5660 2710
5661 - // Track which document IDs are used for context
5662 - $used_document_ids = [];
5663 - foreach ($top_urls as $group) {
5664 - if ($group['is_chunked']) {
5665 - foreach ($group['chunks'] as $chunk) {
5666 - $used_document_ids[] = $chunk['id'];
5667 - }
5668 - } elseif ($group['single_id']) {
5669 - $used_document_ids[] = $group['single_id'];
5670 - }
5671 2711 }
5672 2712
5673 - // Update the all_similarities array to mark which were actually used
5674 - foreach ($all_similarities as &$similarity_item) {
5675 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5676 - }
5677 -
5678 - // Store top 10 for testing panel
5679 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5680 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5681 -
5682 - // Initialize final content
2713 + // Initialize the final content
5683 2714 $content = '';
5684 - $matches_used = 0;
5685 - $total_chunks_used = 0;
5686 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5687 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5688 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5689 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5690 2715
5691 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5692 - // Use fresh options to ensure we get the latest setting value
5693 - $fresh_options = get_option('mxchat_options', []);
5694 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5695 -
5696 - // Build content from top sources
5697 - foreach ($top_urls as $group_key => $group) {
5698 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5699 -
5700 - // Stop if we've hit the total chunk limit
5701 - if ($total_chunks_used >= $max_total_chunks) {
5702 - break;
5703 - }
5704 -
5705 - $full_text = '';
5706 - $chunks_in_this_source = 1; // Default for non-chunked content
5707 -
5708 - if ($group['is_chunked']) {
5709 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5710 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5711 -
5712 - // Fetch chunks for this URL with limit
5713 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5714 -
5715 - // If fetching all chunks fails, fall back to matched chunks
5716 - if (empty($full_text)) {
5717 - // Sort matched chunks by index and concatenate
5718 - usort($group['chunks'], function($a, $b) {
5719 - return $a['chunk_index'] <=> $b['chunk_index'];
5720 - });
5721 -
5722 - $chunk_texts = array();
5723 - $chunks_in_this_source = 0;
5724 - foreach ($group['chunks'] as $chunk) {
5725 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5726 - break;
5727 - }
5728 - $chunk_texts[] = $chunk['text'];
5729 - $chunks_in_this_source++;
5730 - }
5731 - $full_text = implode("\n\n", $chunk_texts);
2716 + // Fetch and combine content for the top results
2717 + foreach ($top_results as $result) {
2718 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
2719 + // Check if the content is PDF-related and add surrounding pages
2720 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
2721 + error_log('[MXCHAT] ID ' . $result['id'] . ' is PDF content, adding surrounding pages');
2722 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
2723 + "SELECT id, article_content FROM {$system_prompt_table}
2724 + WHERE id IN (
2725 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
2726 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
2727 + )",
2728 + $result['id'],
2729 + $result['id']
2730 + ));
2731 + // Add previous content if it exists
2732 + if (!empty($surrounding_content[0])) {
2733 + $content .= $surrounding_content[0]->article_content . "\n\n";
5732 2734 }
5733 - } else {
5734 - $full_text = $group['single_text'];
5735 - $chunks_in_this_source = 1;
5736 - }
5737 -
5738 - if (!empty($full_text)) {
5739 - // Strip URLs from content if citation links are disabled
5740 - if (!$citation_links_enabled) {
5741 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5742 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
2735 + // Add the main chunk content
2736 + $content .= $chunk_content . "\n\n";
2737 + // Add next content if it exists
2738 + if (!empty($surrounding_content[1])) {
2739 + $content .= $surrounding_content[1]->article_content . "\n\n";
5743 2740 }
5744 -
5745 - // Use numbered reference for URL-based entries, plain info label for manual entries
5746 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5747 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5748 - $matches_used++;
5749 - $content .= "## Reference " . $matches_used . " ##\n";
5750 - $content .= $full_text . "\n\n";
5751 -
5752 - // Only include citation URLs if citation links are enabled
5753 - if ($citation_links_enabled) {
5754 - $valid_urls[] = $source_url;
5755 - $content .= "URL: " . $source_url . "\n\n";
5756 - }
5757 - } else {
5758 - // Manual entry — no reference number, no citation
5759 - $content .= "## Information ##\n";
5760 - $content .= $full_text . "\n\n";
5761 - }
5762 -
5763 - // Extract any URLs from the text content itself (only if citation links enabled)
5764 - if ($citation_links_enabled) {
5765 - preg_match_all(
5766 - '#\bhttps?://[^\s<>"\']+#i',
5767 - $full_text,
5768 - $content_urls
5769 - );
5770 - if (!empty($content_urls[0])) {
5771 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5772 - }
5773 - }
5774 -
5775 - $total_chunks_used += $chunks_in_this_source;
5776 - }
5777 - }
5778 -
5779 - // NEW: Store unique valid URLs for validation
5780 - $this->current_valid_urls = array_unique($valid_urls);
5781 -
5782 - // Store sources and chunks counts for testing/transcript display
5783 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5784 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5785 -
5786 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5787 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5788 -
5789 - // Add response guidelines
5790 - if (empty($top_urls)) {
5791 - $content = "No reference information was found for this query.\n\n";
5792 - } else {
5793 - // Build response guidelines based on citation links setting
5794 - $content .= "\n## Response Guidelines ##\n" .
5795 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5796 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5797 - "If you don't have specific information or are uncertain about any details, it's always " .
5798 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5799 - "When information is incomplete, let them know you are unsure.\n\n";
5800 -
5801 - // Only add hyperlink instructions if citation links are enabled
5802 - if ($citation_links_enabled) {
5803 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5804 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5805 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5806 2741 } else {
5807 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5808 - "Simply provide helpful answers based on the reference information without citing sources.";
2742 + // For non-PDF content, add directly
2743 + $content .= $chunk_content . "\n\n";
5809 2744 }
5810 2745 }
5811 2746
2747 + // Log content length
2748 + error_log('[MXCHAT] Retrieved content length: ' . strlen(trim($content)) . ' characters');
2749 +
5812 2750 return trim($content);
5813 2751 }
5814 2752
5815 -/**
5816 - * Fetch and reassemble chunks for a URL from WordPress database
5817 - *
5818 - * @param string $source_url The source URL to fetch chunks for
5819 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5820 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5821 - * @return string Reassembled content from chunks
5822 - */
5823 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5824 - global $wpdb;
5825 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5826 2753
5827 - // Fetch all rows with this source_url
5828 - $rows = $wpdb->get_results($wpdb->prepare(
5829 - "SELECT article_content FROM {$table}
5830 - WHERE source_url = %s
5831 - ORDER BY id ASC",
5832 - $source_url
5833 - ));
5834 -
5835 - if (empty($rows)) {
5836 - $chunk_count = 0;
5837 - return '';
5838 - }
5839 -
5840 - // Parse and sort chunks by index
5841 - $chunks = array();
5842 - foreach ($rows as $row) {
5843 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5844 -
5845 - if ($parsed['is_chunked']) {
5846 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5847 - $chunks[$chunk_index] = $parsed['text'];
5848 - } else {
5849 - // Non-chunked content - just return it
5850 - $chunks[] = $parsed['text'];
5851 - }
5852 - }
5853 -
5854 - // Sort by chunk index
5855 - ksort($chunks);
5856 -
5857 - // Apply chunk limit if specified
5858 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5859 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5860 - }
5861 -
5862 - // Store actual chunk count
5863 - $chunk_count = count($chunks);
5864 -
5865 - // Reassemble content
5866 - return implode("\n\n", $chunks);
5867 -}
5868 -
5869 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5870 - global $wpdb;
2754 +private function find_relevant_content_pinecone($user_embedding) {
2755 + $options = get_option('mxchat_pinecone_addon_options', array());
2756 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
2757 + $host = $options['mxchat_pinecone_host'] ?? '';
5871 2758
5872 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5873 - //error_log(" - bot_id: " . $bot_id);
5874 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5875 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5876 -
5877 - // Use bot-specific config or fall back to default
5878 - if ($bot_config === null) {
5879 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5880 - }
5881 -
5882 - $api_key = $bot_config['api_key'] ?? '';
5883 - $host = $bot_config['host'] ?? '';
5884 - $namespace = $bot_config['namespace'] ?? '';
5885 -
5886 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5887 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
5888 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
5889 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
5890 -
5891 - // Initialize similarity analysis storage
5892 - $this->last_similarity_analysis = [
5893 - 'knowledge_base_type' => 'Pinecone',
5894 - 'bot_id' => $bot_id,
5895 - 'namespace' => $namespace,
5896 - 'top_matches' => [],
5897 - 'threshold_used' => 0,
5898 - 'total_checked' => 0
5899 - ];
5900 -
5901 - // NEW: Initialize valid URLs array
5902 - $valid_urls = [];
5903 -
5904 2759 if (empty($host) || empty($api_key)) {
5905 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
5906 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
5907 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
5908 - // Store empty array for valid URLs since we can't proceed
5909 - $this->current_valid_urls = [];
2760 + error_log('[MXCHAT Debug] Pinecone credentials not properly configured');
5910 2761 return '';
5911 2762 }
5912 2763
5913 - // Get knowledge manager instance for role checking
5914 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2764 + // Get the similarity threshold from the main options array only
2765 + $main_options = get_option('mxchat_options', []);
2766 + $similarity_threshold = isset($main_options['similarity_threshold'])
2767 + ? ((int) $main_options['similarity_threshold']) / 100
2768 + : 0.8; // Default to 80%
5915 2769
5916 - // Get the similarity threshold from the bot options or main options
5917 - $bot_options = $this->get_bot_options($bot_id);
5918 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
2770 + error_log('[MXCHAT Debug] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
5919 2771
5920 - $similarity_threshold = isset($current_options['similarity_threshold'])
5921 - ? ((int) $current_options['similarity_threshold']) / 100
5922 - : 0.35;
5923 -
5924 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5925 -
5926 2772 // Prepare the query request for Pinecone
5927 2773 $api_endpoint = "https://{$host}/query";
5928 2774
2775 + error_log('[MXCHAT Debug] Querying Pinecone at: ' . $api_endpoint);
2776 +
5929 2777 $request_body = array(
5930 2778 'vector' => $user_embedding,
5931 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
2779 + 'topK' => 5,
5932 2780 'includeMetadata' => true,
5933 2781 'includeValues' => true
5934 2782 );
5935 2783
5936 - // Add namespace if specified for this bot
5937 - if (!empty($namespace)) {
5938 - $request_body['namespace'] = $namespace;
5939 - }
5940 -
5941 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
5942 - //error_log(" - Endpoint: " . $api_endpoint);
5943 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
5944 -
5945 2784 $response = wp_remote_post($api_endpoint, array(
5946 2785 'headers' => array(
5947 2786 'Api-Key' => $api_key,
5948 2787 'accept' => 'application/json',
@@ -5952,879 +2791,56 @@
5952 2791 'timeout' => 30
5953 2792 ));
5954 2793
5955 2794 if (is_wp_error($response)) {
5956 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
5957 - // Store empty array for valid URLs
5958 - $this->current_valid_urls = [];
2795 + error_log('[MXCHAT Debug] Pinecone query error: ' . $response->get_error_message());
5959 2796 return '';
5960 2797 }
5961 2798
5962 2799 $response_code = wp_remote_retrieve_response_code($response);
5963 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
5964 -
5965 2800 if ($response_code !== 200) {
5966 - $response_body = wp_remote_retrieve_body($response);
5967 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
5968 - // Store empty array for valid URLs
5969 - $this->current_valid_urls = [];
2801 + error_log('[MXCHAT Debug] Pinecone API error: ' . wp_remote_retrieve_body($response));
5970 2802 return '';
5971 2803 }
5972 2804
5973 - // ADD DETAILED DEBUG SECTION HERE
5974 - $response_body = wp_remote_retrieve_body($response);
5975 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
5976 -
5977 - $results = json_decode($response_body, true);
5978 -
5979 - if (json_last_error() !== JSON_ERROR_NONE) {
5980 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
5981 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
5982 - // Store empty array for valid URLs
5983 - $this->current_valid_urls = [];
5984 - return '';
5985 - }
5986 -
5987 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
5988 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
5989 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
5990 -
2805 + $results = json_decode(wp_remote_retrieve_body($response), true);
5991 2806 if (empty($results['matches'])) {
5992 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
5993 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
5994 - // Store empty array for valid URLs
5995 - $this->current_valid_urls = [];
2807 + error_log('[MXCHAT Debug] No matches found in Pinecone response');
5996 2808 return '';
5997 2809 }
5998 2810
5999 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
2811 + error_log('[MXCHAT Debug] Found ' . count($results['matches']) . ' matches in Pinecone');
6000 2812
6001 - // Log first match details for debugging
6002 - if (!empty($results['matches'][0])) {
6003 - $first_match = $results['matches'][0];
6004 - //error_log("MXCHAT DEBUG: First match details:");
6005 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
6006 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
6007 - if (isset($first_match['metadata'])) {
6008 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
6009 - }
6010 - }
6011 -
6012 2813 // Initialize the final content
6013 2814 $content = '';
6014 - $matches_used = 0;
6015 - $matches_used_for_context = [];
6016 - $total_chunks_used = 0;
6017 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6018 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6019 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6020 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
2815 + $matches_above_threshold = 0;
2816 +
2817 + // Process each match
2818 + foreach ($results['matches'] as $index => $match) {
2819 + // Log score for each match
6021 2820
6022 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6023 - // Use fresh options to ensure we get the latest setting value
6024 - $fresh_options = get_option('mxchat_options', []);
6025 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6026 -
6027 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
6028 - $url_groups = array();
6029 -
6030 - foreach ($results['matches'] as $index => $match) {
6031 2821 // Skip if similarity is below threshold
6032 2822 if ($match['score'] < $similarity_threshold) {
6033 2823 continue;
6034 2824 }
6035 -
6036 - $metadata = $match['metadata'] ?? array();
6037 - $source_url = $metadata['source_url'] ?? '';
6038 - $match_id = $match['id'] ?? '';
6039 -
6040 - // LAZY ROLE CHECK: Only check role for content we're actually considering
6041 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
6042 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6043 -
6044 - // Skip if user doesn't have access
6045 - if (!$has_access) {
6046 - continue;
6047 - }
6048 -
6049 - // Use a unique key for manual entries without a source URL
6050 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
6051 -
6052 - // Group by source URL (or unique key for manual entries)
6053 - if (!isset($url_groups[$group_key])) {
6054 - $url_groups[$group_key] = array(
6055 - 'source_url' => $source_url,
6056 - 'best_score' => 0,
6057 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
6058 - 'chunks' => array(),
6059 - 'single_text' => ''
6060 - );
6061 - }
6062 -
6063 - // Track best score for this group
6064 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
6065 - $url_groups[$group_key]['best_score'] = $match['score'];
6066 - }
6067 -
6068 - // Store chunk info or single text
6069 - if ($url_groups[$group_key]['is_chunked']) {
6070 - $url_groups[$group_key]['chunks'][] = array(
6071 - 'id' => $match_id,
6072 - 'score' => $match['score'],
6073 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
6074 - 'text' => $metadata['text'] ?? ''
6075 - );
6076 - } else {
6077 - // Non-chunked content - just store the text
6078 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
6079 - $url_groups[$group_key]['single_id'] = $match_id;
6080 - }
6081 - }
6082 -
6083 - // Sort URL groups by best score (highest first)
6084 - uasort($url_groups, function($a, $b) {
6085 - return $b['best_score'] <=> $a['best_score'];
6086 - });
6087 -
6088 - // Get RAG sources limit from options (default 6, min 3, max 10)
6089 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6090 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6091 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
6092 -
6093 - // Take top N unique URLs based on user setting
6094 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
6095 -
6096 - // Track which match IDs are actually used for context
6097 - foreach ($top_urls as $group) {
6098 - if ($group['is_chunked']) {
6099 - foreach ($group['chunks'] as $chunk) {
6100 - $matches_used_for_context[] = $chunk['id'];
6101 - }
6102 - } elseif (!empty($group['single_id'])) {
6103 - $matches_used_for_context[] = $group['single_id'];
6104 - }
6105 - }
6106 -
6107 - // Build content from top sources
6108 - foreach ($top_urls as $group_key => $group) {
6109 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6110 -
6111 - // Stop if we've hit the total chunk limit
6112 - if ($total_chunks_used >= $max_total_chunks) {
6113 - break;
6114 - }
6115 -
6116 - $full_text = '';
6117 - $chunks_in_this_source = 1; // Default for non-chunked content
6118 -
6119 - if ($group['is_chunked']) {
6120 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6121 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6122 -
6123 - // Fetch chunks for this URL with limit
6124 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6125 -
6126 - // If fetching all chunks fails, fall back to matched chunks
6127 - if (empty($full_text)) {
6128 - // Sort matched chunks by index and concatenate
6129 - usort($group['chunks'], function($a, $b) {
6130 - return $a['chunk_index'] <=> $b['chunk_index'];
6131 - });
6132 -
6133 - $chunk_texts = array();
6134 - $chunks_in_this_source = 0;
6135 - foreach ($group['chunks'] as $chunk) {
6136 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6137 - break;
6138 - }
6139 - $chunk_texts[] = $chunk['text'];
6140 - $chunks_in_this_source++;
6141 - }
6142 - $full_text = implode("\n\n", $chunk_texts);
6143 - }
6144 - } else {
6145 - $full_text = $group['single_text'];
6146 - $chunks_in_this_source = 1;
6147 - }
6148 -
6149 - if (!empty($full_text)) {
6150 - // Strip URLs from content if citation links are disabled
6151 - if (!$citation_links_enabled) {
6152 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6153 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6154 - }
6155 -
6156 - // Use numbered reference for URL-based entries, plain info label for manual entries
6157 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6158 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6159 - $matches_used++;
6160 - $content .= "## Reference " . $matches_used . " ##\n";
6161 - $content .= $full_text . "\n\n";
6162 -
6163 - // Only include citation URLs if citation links are enabled
6164 - if ($citation_links_enabled) {
6165 - $valid_urls[] = $source_url;
6166 - $content .= "URL: " . $source_url . "\n\n";
6167 - }
6168 - } else {
6169 - // Manual entry — no reference number, no citation
6170 - $content .= "## Information ##\n";
6171 - $content .= $full_text . "\n\n";
6172 - }
6173 -
6174 - // Extract any URLs from the text content itself (only if citation links enabled)
6175 - if ($citation_links_enabled) {
6176 - preg_match_all(
6177 - '#\bhttps?://[^\s<>"\']+#i',
6178 - $full_text,
6179 - $content_urls
6180 - );
6181 - if (!empty($content_urls[0])) {
6182 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6183 - }
6184 - }
6185 -
6186 - $total_chunks_used += $chunks_in_this_source;
6187 - }
6188 - }
6189 -
6190 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6191 - $all_matches = [];
6192 - foreach ($results['matches'] as $index => $match) {
6193 - if ($index >= 10) break; // Limit to top 10 for testing
6194 2825
6195 - $match_id = $match['id'] ?? '';
2826 + $matches_above_threshold++;
6196 2827
6197 - // Check role access for testing display (use cache if available)
6198 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6199 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6200 -
6201 - $source_display = '';
6202 - if (!empty($match['metadata']['source_url'])) {
6203 - $source_display = $match['metadata']['source_url'];
6204 - } else {
6205 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6206 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6207 - $source_display = substr(trim($content_preview), 0, 50) . '...';
2828 + if (!empty($match['metadata']['text']) && !empty($match['metadata']['source_url'])) {
2829 + // Add content with citation
2830 + $content .= $match['metadata']['text'] . "\n";
2831 + $content .= "Source: " . $match['metadata']['source_url'] . "\n\n";
6208 2832 }
6209 -
6210 - $match_id_for_display = $match['id'] ?? $index;
6211 -
6212 - // Check for chunk metadata in Pinecone
6213 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6214 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6215 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6216 -
6217 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6218 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6219 - $is_chunk = true;
6220 - }
6221 -
6222 - $all_matches[] = [
6223 - 'document_id' => $match_id_for_display,
6224 - 'similarity' => $match['score'],
6225 - 'similarity_percentage' => round($match['score'] * 100, 2),
6226 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6227 - 'source_display' => $source_display,
6228 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6229 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6230 - 'role_restriction' => $role_restriction,
6231 - 'has_access' => $has_access,
6232 - 'filtered_out' => !$has_access,
6233 - 'is_chunk' => $is_chunk,
6234 - 'chunk_index' => $chunk_index,
6235 - 'total_chunks' => $total_chunks
6236 - ];
6237 2833 }
6238 2834
6239 - // Store for testing panel
6240 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6241 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6242 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6243 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6244 -
6245 - // NEW: Store unique valid URLs for validation
6246 - $this->current_valid_urls = array_unique($valid_urls);
6247 -
6248 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6249 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6250 -
6251 - // Add response guidelines
6252 - if ($matches_used === 0) {
6253 - $content = "No reference information was found for this query.\n\n";
6254 - } else {
6255 - // Build response guidelines based on citation links setting
6256 - $content .= "\n## Response Guidelines ##\n" .
6257 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6258 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6259 - "If you don't have specific information or are uncertain about any details, it's always " .
6260 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6261 - "When information is incomplete, let them know you are unsure.\n\n";
6262 -
6263 - // Only add hyperlink instructions if citation links are enabled
6264 - if ($citation_links_enabled) {
6265 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6266 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6267 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6268 - } else {
6269 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6270 - "Simply provide helpful answers based on the reference information without citing sources.";
6271 - }
6272 - }
6273 -
6274 - return trim($content);
6275 -}
6276 -
6277 -/**
6278 - * Get role restriction for a single vector (with caching)
6279 - */
6280 -private function get_single_vector_role($vector_id, $metadata = array()) {
6281 - global $wpdb;
2835 + error_log('[MXCHAT Debug] Total matches used (above threshold): ' . $matches_above_threshold);
2836 + error_log('[MXCHAT Debug] Content length returned: ' . strlen(trim($content)) . ' characters');
6282 2837
6283 - if (empty($vector_id)) {
6284 - return 'public';
6285 - }
6286 -
6287 - // Check cache first
6288 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6289 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6290 -
6291 - if ($cached_role !== false) {
6292 - return $cached_role;
6293 - }
6294 -
6295 - $role_restriction = 'public';
6296 -
6297 - // First try Pinecone metadata
6298 - if (!empty($metadata['role_restriction'])) {
6299 - $role_restriction = $metadata['role_restriction'];
6300 - } else {
6301 - // Check WordPress table for user-modified roles
6302 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6303 - $stored_role = $wpdb->get_var($wpdb->prepare(
6304 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6305 - $vector_id
6306 - ));
6307 -
6308 - if ($stored_role) {
6309 - $role_restriction = $stored_role;
6310 - }
6311 - }
6312 -
6313 - // Cache individual role for 1 hour
6314 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6315 -
6316 - return $role_restriction;
6317 -}
6318 -
6319 -/**
6320 - * Fetch and reassemble all chunks for a URL from Pinecone
6321 - *
6322 - * @param string $source_url The source URL to fetch chunks for
6323 - * @param array $bot_config Bot-specific Pinecone configuration
6324 - * @return string Reassembled content from all chunks
6325 - */
6326 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6327 - $api_key = $bot_config['api_key'] ?? '';
6328 - $host = $bot_config['host'] ?? '';
6329 - $namespace = $bot_config['namespace'] ?? '';
6330 -
6331 - if (empty($host) || empty($api_key)) {
6332 - $chunk_count = 0;
6333 - return '';
6334 - }
6335 -
6336 - $base_hash = md5($source_url);
6337 -
6338 - // Use Pinecone list API to find all chunk vectors with this prefix
6339 - $list_url = "https://{$host}/vectors/list";
6340 -
6341 - // Limit to max_chunks if specified, otherwise fetch up to 100
6342 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6343 -
6344 - $list_body = array(
6345 - 'prefix' => $base_hash . '_chunk_',
6346 - 'limit' => $fetch_limit
6347 - );
6348 -
6349 - if (!empty($namespace)) {
6350 - $list_body['namespace'] = $namespace;
6351 - }
6352 -
6353 - $list_response = wp_remote_post($list_url, array(
6354 - 'headers' => array(
6355 - 'Api-Key' => $api_key,
6356 - 'accept' => 'application/json',
6357 - 'content-type' => 'application/json'
6358 - ),
6359 - 'body' => wp_json_encode($list_body),
6360 - 'timeout' => 30
6361 - ));
6362 -
6363 - if (is_wp_error($list_response)) {
6364 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6365 - return '';
6366 - }
6367 -
6368 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6369 -
6370 - if (empty($list_data['vectors'])) {
6371 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6372 - return '';
6373 - }
6374 -
6375 - // Extract vector IDs
6376 - $vector_ids = array();
6377 - foreach ($list_data['vectors'] as $vector) {
6378 - if (isset($vector['id'])) {
6379 - $vector_ids[] = $vector['id'];
6380 - }
6381 - }
6382 -
6383 - if (empty($vector_ids)) {
6384 - return '';
6385 - }
6386 -
6387 - // Fetch all chunk content
6388 - $fetch_url = "https://{$host}/vectors/fetch";
6389 -
6390 - $fetch_body = array(
6391 - 'ids' => $vector_ids
6392 - );
6393 -
6394 - if (!empty($namespace)) {
6395 - $fetch_body['namespace'] = $namespace;
6396 - }
6397 -
6398 - $fetch_response = wp_remote_post($fetch_url, array(
6399 - 'headers' => array(
6400 - 'Api-Key' => $api_key,
6401 - 'accept' => 'application/json',
6402 - 'content-type' => 'application/json'
6403 - ),
6404 - 'body' => wp_json_encode($fetch_body),
6405 - 'timeout' => 30
6406 - ));
6407 -
6408 - if (is_wp_error($fetch_response)) {
6409 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6410 - return '';
6411 - }
6412 -
6413 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6414 -
6415 - if (empty($fetch_data['vectors'])) {
6416 - return '';
6417 - }
6418 -
6419 - // Sort chunks by index and reassemble
6420 - $chunks = array();
6421 - foreach ($fetch_data['vectors'] as $id => $vector) {
6422 - $metadata = $vector['metadata'] ?? array();
6423 - $chunk_index = $metadata['chunk_index'] ?? 0;
6424 - $text = $metadata['text'] ?? '';
6425 -
6426 - // Store chunk with its index
6427 - $chunks[$chunk_index] = $text;
6428 - }
6429 -
6430 - // Sort by chunk index
6431 - ksort($chunks);
6432 -
6433 - // Apply chunk limit if specified
6434 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6435 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6436 - }
6437 -
6438 - // Store actual chunk count
6439 - $chunk_count = count($chunks);
6440 -
6441 - // Reassemble content
6442 - return implode("\n\n", $chunks);
6443 -}
6444 -
6445 -/**
6446 - * Search for relevant content using OpenAI Vector Store (File Search)
6447 - *
6448 - * @param string $user_query The user's query text
6449 - * @param string $bot_id The bot ID
6450 - * @param array $vectorstore_config Vector Store configuration
6451 - * @return string Formatted context string with references
6452 - */
6453 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6454 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6455 - //error_log(" - bot_id: " . $bot_id);
6456 - //error_log(" - user_query length: " . strlen($user_query));
6457 -
6458 - // Get OpenAI API key
6459 - $mxchat_options = get_option('mxchat_options', array());
6460 - $api_key = $mxchat_options['api_key'] ?? '';
6461 -
6462 - // Reset vectorstore error tracking
6463 - $this->last_vectorstore_error = null;
6464 -
6465 - if (empty($api_key)) {
6466 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6467 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6468 - $this->current_valid_urls = [];
6469 - return '';
6470 - }
6471 -
6472 - // Get Vector Store configuration
6473 - if (empty($vectorstore_config)) {
6474 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6475 - }
6476 -
6477 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6478 - $max_results = $vectorstore_config['max_results'] ?? 5;
6479 -
6480 - if (empty($vectorstore_ids_string)) {
6481 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6482 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6483 - $this->current_valid_urls = [];
6484 - return '';
6485 - }
6486 -
6487 - // Parse Vector Store IDs
6488 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6489 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6490 -
6491 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6492 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6493 -
6494 - // Initialize similarity analysis storage
6495 - $this->last_similarity_analysis = [
6496 - 'knowledge_base_type' => 'OpenAI Vector Store',
6497 - 'bot_id' => $bot_id,
6498 - 'vectorstore_ids' => $vectorstore_ids,
6499 - 'top_matches' => [],
6500 - 'threshold_used' => 0,
6501 - 'total_checked' => 0
6502 - ];
6503 -
6504 - $valid_urls = [];
6505 -
6506 - // Get the selected model
6507 - $bot_options = $this->get_bot_options($bot_id);
6508 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6509 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6510 -
6511 - // Verify it's an OpenAI model
6512 - if (!$this->is_openai_chat_model($selected_model)) {
6513 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6514 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6515 - $this->current_valid_urls = [];
6516 - return '';
6517 - }
6518 -
6519 - // Use OpenAI Responses API with file_search tool
6520 - $request_body = array(
6521 - 'model' => $selected_model,
6522 - 'input' => $user_query,
6523 - 'tools' => array(
6524 - array(
6525 - 'type' => 'file_search',
6526 - 'vector_store_ids' => $vectorstore_ids,
6527 - 'max_num_results' => intval($max_results)
6528 - )
6529 - ),
6530 - 'include' => array('output[*].file_search_call.search_results')
6531 - );
6532 -
6533 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6534 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6535 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6536 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6537 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6538 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6539 -
6540 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6541 - 'headers' => array(
6542 - 'Authorization' => 'Bearer ' . $api_key,
6543 - 'Content-Type' => 'application/json'
6544 - ),
6545 - 'body' => wp_json_encode($request_body),
6546 - 'timeout' => 60
6547 - ));
6548 -
6549 - if (is_wp_error($response)) {
6550 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6551 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6552 - $this->current_valid_urls = [];
6553 - return '';
6554 - }
6555 -
6556 - $response_code = wp_remote_retrieve_response_code($response);
6557 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6558 -
6559 - $response_body = wp_remote_retrieve_body($response);
6560 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6561 -
6562 - if ($response_code !== 200) {
6563 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6564 - $api_error_detail = '';
6565 - $decoded_error = json_decode($response_body, true);
6566 - if (isset($decoded_error['error']['message'])) {
6567 - $api_error_detail = $decoded_error['error']['message'];
6568 - }
6569 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6570 - $this->current_valid_urls = [];
6571 - return '';
6572 - }
6573 - $result = json_decode($response_body, true);
6574 -
6575 - if (json_last_error() !== JSON_ERROR_NONE) {
6576 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6577 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6578 - $this->current_valid_urls = [];
6579 - return '';
6580 - }
6581 -
6582 - // Debug: Log the structure of the result
6583 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6584 - if (isset($result['output'])) {
6585 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6586 - foreach ($result['output'] as $idx => $out) {
6587 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6588 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6589 - }
6590 - } else {
6591 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6592 - }
6593 -
6594 - // Extract file search results from the response
6595 - $content = '';
6596 - $matches_used = 0;
6597 - $all_matches = [];
6598 -
6599 - // The Responses API returns output array with tool results
6600 - if (isset($result['output']) && is_array($result['output'])) {
6601 - foreach ($result['output'] as $output_item) {
6602 - // Look for file_search_call results
6603 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6604 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6605 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6606 -
6607 - // Check for search_results in the output item directly
6608 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6609 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6610 -
6611 - if (empty($search_results)) {
6612 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6613 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6614 - }
6615 -
6616 - foreach ($search_results as $index => $search_result) {
6617 - $filename = $search_result['filename'] ?? '';
6618 - $score = $search_result['score'] ?? 0;
6619 - $text_content = '';
6620 -
6621 - // Extract text content from the result
6622 - // The text can be directly on the result OR nested under content array
6623 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6624 - // Direct text field (OpenAI's actual format)
6625 - $text_content = $search_result['text'];
6626 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6627 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6628 - // Nested content array format
6629 - foreach ($search_result['content'] as $content_item) {
6630 - if (isset($content_item['text'])) {
6631 - $text_content .= $content_item['text'] . "\n";
6632 - }
6633 - }
6634 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6635 - } else {
6636 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6637 - }
6638 -
6639 - if (!empty($text_content)) {
6640 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6641 - $content .= trim($text_content) . "\n\n";
6642 -
6643 - if (!empty($filename)) {
6644 - $content .= "Source: " . $filename . "\n\n";
6645 - }
6646 -
6647 - // Extract URLs from content
6648 - preg_match_all(
6649 - '#\bhttps?://[^\s<>"\']+#i',
6650 - $text_content,
6651 - $content_urls
6652 - );
6653 - if (!empty($content_urls[0])) {
6654 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6655 - }
6656 -
6657 - $matches_used++;
6658 - }
6659 -
6660 - // Store for similarity analysis
6661 - $all_matches[] = [
6662 - 'document_id' => $filename ?: ('result_' . $index),
6663 - 'similarity' => $score,
6664 - 'similarity_percentage' => round($score * 100, 2),
6665 - 'above_threshold' => true,
6666 - 'source_display' => $filename,
6667 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6668 - 'used_for_context' => true,
6669 - 'role_restriction' => 'public',
6670 - 'has_access' => true,
6671 - 'filtered_out' => false
6672 - ];
6673 - }
6674 - }
6675 -
6676 - // Also check for message content with annotations (citations)
6677 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6678 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6679 - foreach ($output_item['content'] as $content_block) {
6680 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6681 - foreach ($content_block['annotations'] as $annotation) {
6682 - if (isset($annotation['filename'])) {
6683 - $filename = $annotation['filename'];
6684 - $score = $annotation['score'] ?? 0;
6685 - $text_content = '';
6686 -
6687 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6688 - foreach ($annotation['content'] as $ann_content) {
6689 - if (isset($ann_content['text'])) {
6690 - $text_content .= $ann_content['text'] . "\n";
6691 - }
6692 - }
6693 - }
6694 -
6695 - if (!empty($text_content) && $matches_used < $max_results) {
6696 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6697 - $content .= trim($text_content) . "\n\n";
6698 - $content .= "Source: " . $filename . "\n\n";
6699 -
6700 - preg_match_all(
6701 - '#\bhttps?://[^\s<>"\']+#i',
6702 - $text_content,
6703 - $content_urls
6704 - );
6705 - if (!empty($content_urls[0])) {
6706 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6707 - }
6708 -
6709 - $matches_used++;
6710 -
6711 - $all_matches[] = [
6712 - 'document_id' => $filename,
6713 - 'similarity' => $score,
6714 - 'similarity_percentage' => round($score * 100, 2),
6715 - 'above_threshold' => true,
6716 - 'source_display' => $filename,
6717 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6718 - 'used_for_context' => true,
6719 - 'role_restriction' => 'public',
6720 - 'has_access' => true,
6721 - 'filtered_out' => false
6722 - ];
6723 - }
6724 - }
6725 - }
6726 - }
6727 - }
6728 - }
6729 - }
6730 - }
6731 - }
6732 -
6733 - // Store for testing panel
6734 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6735 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6736 -
6737 - // Store unique valid URLs for validation
6738 - $this->current_valid_urls = array_unique($valid_urls);
6739 -
6740 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6741 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6742 -
6743 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6744 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6745 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6746 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6747 - if ($matches_used > 0) {
6748 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6749 - }
6750 -
6751 - // Check if citation links are enabled
6752 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6753 -
6754 - // Add response guidelines
6755 - if ($matches_used === 0) {
6756 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6757 - $content = "No reference information was found for this query.\n\n";
6758 - } else {
6759 - // Build response guidelines based on citation links setting
6760 - $content .= "\n## Response Guidelines ##\n" .
6761 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6762 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6763 - "If you don't have specific information or are uncertain about any details, it's always " .
6764 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6765 - "When information is incomplete, let them know you are unsure.\n\n";
6766 -
6767 - // Only add hyperlink instructions if citation links are enabled
6768 - if ($citation_links_enabled) {
6769 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6770 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6771 - } else {
6772 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6773 - "Simply provide helpful answers based on the reference information without citing sources.";
6774 - }
6775 - }
6776 -
6777 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6778 -
6779 2838 return trim($content);
6780 2839 }
6781 2840
6782 -/**
6783 - * Check if the given model is an OpenAI chat model
6784 - *
6785 - * @param string $model The model ID
6786 - * @return bool True if it's an OpenAI model
6787 - */
6788 -private function is_openai_chat_model($model) {
6789 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6790 - foreach ($openai_prefixes as $prefix) {
6791 - if (strpos($model, $prefix) === 0) {
6792 - return true;
6793 - }
6794 - }
6795 - return false;
6796 -}
6797 -
6798 -/**
6799 - * Get bot-specific Vector Store configuration
6800 - *
6801 - * @param string $bot_id The bot ID
6802 - * @return array Configuration array
6803 - */
6804 -private function get_bot_vectorstore_config($bot_id = 'default') {
6805 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6806 -
6807 - // Default global settings
6808 - $default_config = array(
6809 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6810 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6811 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6812 - );
6813 -
6814 - // Allow multi-bot plugin to override with bot-specific settings
6815 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6816 -
6817 - // Preserve max_results from global settings if not set in bot config
6818 - if (!isset($bot_config['max_results'])) {
6819 - $bot_config['max_results'] = $default_config['max_results'];
6820 - }
6821 -
6822 - return $bot_config;
6823 -}
6824 -
6825 2841 private function mxchat_find_relevant_products($user_embedding) {
6826 - //error_log('MXChat Vector Search: Starting product search...');
2842 + error_log('MXChat Vector Search: Starting product search...');
6827 2843
6828 2844 // Retrieve the add-on settings from the database
6829 2845 $addon_options = get_option('mxchat_pinecone_addon_options', array());
6830 2846
@@ -6830,88 +2846,87 @@
6830 2846
6831 2847 // Determine whether Pinecone is enabled
6832 2848 $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
6833 2849
6834 - //error_log('Pinecone enabled flag: ' . $use_pinecone);
2850 + error_log('Pinecone enabled flag: ' . $use_pinecone);
6835 2851
6836 2852 if ($use_pinecone === 1) {
6837 - //error_log('MXChat Vector Search: Using Pinecone database for products');
2853 + error_log('MXChat Vector Search: Using Pinecone database for products');
6838 2854 return $this->find_relevant_products_pinecone($user_embedding);
6839 2855 } else {
6840 - //error_log('MXChat Vector Search: Using WordPress database for products');
2856 + error_log('MXChat Vector Search: Using WordPress database for products');
6841 2857 return $this->find_relevant_products_wordpress($user_embedding);
6842 2858 }
6843 2859 }
2860 +
6844 2861 private function find_relevant_products_wordpress($user_embedding) {
6845 2862 global $wpdb;
6846 2863 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
2864 + $cache_key = 'mxchat_system_prompt_embeddings';
2865 + $batch_size = 500;
6847 2866
6848 - if (!is_array($user_embedding)) {
6849 - return '';
6850 - }
2867 + // Original WordPress database search logic
2868 + // [Previous implementation remains the same]
2869 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2870 + if ($embeddings === false) {
2871 + $embeddings = [];
2872 + $offset = 0;
6851 2873
6852 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6853 - // results above the similarity threshold. Peak memory is bounded by
6854 - // $batch_size embedding rows plus a 3-element top list.
6855 - $batch_size = 250;
6856 - $similarity_threshold = 0.85;
6857 - $top_k = 3;
6858 - $top_results = [];
6859 - $offset = 0;
2874 + do {
2875 + $query = $wpdb->prepare(
2876 + "SELECT id, embedding_vector
2877 + FROM {$system_prompt_table}
2878 + LIMIT %d OFFSET %d",
2879 + $batch_size,
2880 + $offset
2881 + );
6860 2882
6861 - do {
6862 - $batch = $wpdb->get_results($wpdb->prepare(
6863 - "SELECT id, embedding_vector
6864 - FROM {$system_prompt_table}
6865 - LIMIT %d OFFSET %d",
6866 - $batch_size,
6867 - $offset
6868 - ));
2883 + $batch = $wpdb->get_results($query);
2884 + if (empty($batch)) {
2885 + break;
2886 + }
6869 2887
6870 - if (empty($batch)) {
6871 - break;
6872 - }
2888 + $embeddings = array_merge($embeddings, $batch);
2889 + $offset += $batch_size;
6873 2890
6874 - foreach ($batch as $row) {
6875 - $database_embedding = $row->embedding_vector
6876 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6877 - : null;
2891 + unset($batch);
6878 2892
6879 - if (!is_array($database_embedding)) {
6880 - unset($database_embedding);
6881 - continue;
6882 - }
2893 + } while (true);
6883 2894
2895 + if (empty($embeddings)) {
2896 + return '';
2897 + }
2898 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2899 + }
2900 +
2901 + $relevant_results = [];
2902 + foreach ($embeddings as $embedding) {
2903 + $database_embedding = $embedding->embedding_vector
2904 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2905 + : null;
2906 + if (is_array($database_embedding) && is_array($user_embedding)) {
6884 2907 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
6885 - unset($database_embedding);
6886 -
6887 - if ($similarity < $similarity_threshold) {
6888 - continue;
6889 - }
6890 -
6891 - // Insert into bounded top-K (kept sorted descending)
6892 - if (count($top_results) < $top_k) {
6893 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
6894 - usort($top_results, function ($a, $b) {
6895 - return $b['similarity'] <=> $a['similarity'];
6896 - });
6897 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
6898 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
6899 - usort($top_results, function ($a, $b) {
6900 - return $b['similarity'] <=> $a['similarity'];
6901 - });
6902 - }
2908 + $relevant_results[] = [
2909 + 'id' => $embedding->id,
2910 + 'similarity' => $similarity
2911 + ];
6903 2912 }
2913 + unset($database_embedding);
2914 + }
6904 2915
6905 - unset($batch);
6906 - $offset += $batch_size;
6907 - } while (true);
2916 + // Use fixed threshold for products
2917 + $similarity_threshold = 0.85;
6908 2918
6909 - if (empty($top_results)) {
6910 - return '';
6911 - }
2919 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2920 + return $result['similarity'] >= $similarity_threshold;
2921 + });
2922 + usort($relevant_results, function ($a, $b) {
2923 + return $b['similarity'] <=> $a['similarity'];
2924 + });
6912 2925
2926 + $top_results = array_slice($relevant_results, 0, 5);
6913 2927 $content = '';
2928 +
6914 2929 foreach ($top_results as $result) {
6915 2930 $chunk_content = $this->fetch_content_with_product_links($result['id']);
6916 2931 $content .= $chunk_content . "\n\n";
6917 2932 }
@@ -6918,11 +2933,11 @@
6918 2933
6919 2934 return trim($content);
6920 2935 }
6921 2936
6922 -
2937 +// Modified search function with correct filter syntax
6923 2938 private function find_relevant_products_pinecone($user_embedding) {
6924 - //error_log('Starting Pinecone product search...');
2939 + error_log('Starting Pinecone product search...');
6925 2940
6926 2941 $options = get_option('mxchat_pinecone_addon_options', array());
6927 2942 $api_key = $options['mxchat_pinecone_api_key'] ?? '';
6928 2943 $host = $options['mxchat_pinecone_host'] ?? '';
@@ -6927,9 +2942,9 @@
6927 2942 $api_key = $options['mxchat_pinecone_api_key'] ?? '';
6928 2943 $host = $options['mxchat_pinecone_host'] ?? '';
6929 2944
6930 2945 if (empty($host) || empty($api_key)) {
6931 - //error_log('Pinecone credentials not properly configured for product search');
2946 + error_log('Pinecone credentials not properly configured for product search');
6932 2947 return '';
6933 2948 }
6934 2949
6935 2950 $similarity_threshold = 0.85;
@@ -6944,9 +2959,9 @@
6944 2959 'type' => 'product'
6945 2960 )
6946 2961 );
6947 2962
6948 - //error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
2963 + error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
6949 2964
6950 2965 $response = wp_remote_post($api_endpoint, array(
6951 2966 'headers' => array(
6952 2967 'Api-Key' => $api_key,
@@ -6957,25 +2972,25 @@
6957 2972 'timeout' => 30
6958 2973 ));
6959 2974
6960 2975 if (is_wp_error($response)) {
6961 - //error_log('Pinecone product query error: ' . $response->get_error_message());
2976 + error_log('Pinecone product query error: ' . $response->get_error_message());
6962 2977 return '';
6963 2978 }
6964 2979
6965 2980 $response_code = wp_remote_retrieve_response_code($response);
6966 - //error_log('Pinecone response code: ' . $response_code);
2981 + error_log('Pinecone response code: ' . $response_code);
6967 2982
6968 2983 if ($response_code !== 200) {
6969 - //error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
2984 + error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
6970 2985 return '';
6971 2986 }
6972 2987
6973 2988 $results = json_decode(wp_remote_retrieve_body($response), true);
6974 - //error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
2989 + error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
6975 2990
6976 2991 if (empty($results['matches'])) {
6977 - //error_log('No matches found in Pinecone response');
2992 + error_log('No matches found in Pinecone response');
6978 2993 return '';
6979 2994 }
6980 2995
6981 2996 $content = '';
@@ -6980,9 +2995,9 @@
6980 2995
6981 2996 $content = '';
6982 2997 foreach ($results['matches'] as $match) {
6983 2998 if ($match['score'] < $similarity_threshold) {
6984 - //error_log("Match below threshold: " . $match['score']);
2999 + error_log("Match below threshold: " . $match['score']);
6985 3000 continue;
6986 3001 }
6987 3002
6988 3003 if (!empty($match['metadata']['text'])) {
@@ -7017,3053 +3032,321 @@
7017 3032
7018 3033 return null;
7019 3034 }
7020 3035
7021 -/**
7022 - * Get system instructions for a specific bot or default
7023 - * Checks for multi-bot add-on and uses bot-specific instructions if available
7024 - * Automatically strips URLs if citation links are disabled
7025 - * Replaces {visitor_name} placeholder with actual visitor name if available
7026 - *
7027 - * @param string $bot_id The bot ID to get instructions for
7028 - * @param string $session_id Optional session ID to lookup visitor name
7029 - */
7030 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
7031 - $instructions = '';
7032 -
7033 - // Check if multi-bot add-on is active
7034 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
7035 - // Get bot-specific options from multi-bot add-on
7036 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
7037 -
7038 - // If bot has custom system instructions, use those
7039 - if (!empty($bot_options['system_prompt_instructions'])) {
7040 - $instructions = $bot_options['system_prompt_instructions'];
7041 - }
7042 - }
7043 -
7044 - // Fall back to default system instructions
7045 - if (empty($instructions)) {
7046 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7047 - }
7048 -
7049 - // Check if citation links are disabled - if so, strip URLs from instructions
7050 - $fresh_options = get_option('mxchat_options', []);
7051 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
7052 -
7053 - if (!$citation_links_enabled && !empty($instructions)) {
7054 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
7055 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
7056 - }
7057 -
7058 - // Replace {visitor_name} placeholder with actual visitor name if available
7059 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
7060 - $name_option_key = "mxchat_name_{$session_id}";
7061 - $visitor_name = get_option($name_option_key, '');
7062 -
7063 - if (!empty($visitor_name)) {
7064 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
7065 - } else {
7066 - // Remove placeholder if no name is available
7067 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
7068 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
7069 - }
7070 - }
7071 -
7072 - // Allow developers to filter system instructions and process shortcodes
7073 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
7074 - $instructions = do_shortcode($instructions);
7075 -
7076 - return $instructions;
7077 -}
7078 -/**
7079 - * Get the current bot ID from session or request context
7080 - */
7081 -private function get_current_bot_id($session_id = '') {
7082 - // First, check if bot_id is passed in the current request
7083 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
7084 - return sanitize_key($_POST['bot_id']);
7085 - }
7086 -
7087 - // If not in POST, try to get it from session data
7088 - if (!empty($session_id)) {
7089 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
7090 - if (!empty($bot_id)) {
7091 - return $bot_id;
7092 - }
7093 - }
7094 -
7095 - // Fall back to default
7096 - return 'default';
7097 -}
7098 -/* ====================================================================== *
7099 - * Native function-calling loop (plan-mxchat-20260617-a41dee)
7100 - *
7101 - * Model-driven tool use. The model is offered MxChat's enabled callbacks as
7102 - * tools (sourced from MxChat_Tool_Registry, the single source the admin AI
7103 - * Tools checklist also reads). When the model calls a tool, the matching
7104 - * callback runs through its EXISTING permission checks, its output is fed
7105 - * back, and the loop continues up to a depth cap. INDEPENDENT of the
7106 - * intent→callback router — it runs only after intents miss, and works with
7107 - * ZERO Actions created.
7108 - *
7109 - * Entered ONLY when: function calling is enabled + the active model is
7110 - * tool-capable + at least one tool is enabled. Default-off, so existing
7111 - * installs never enter this branch (byte-for-byte unchanged behavior). The
7112 - * tool round is buffered (non-streaming) per the plan; the final answer is
7113 - * emitted via the same SSE/JSON envelopes the normal path uses.
7114 - * ====================================================================== */
7115 -
7116 -/** Gate: should the function-calling loop handle this turn? */
7117 -private function mxchat_fc_should_run($selected_model) {
7118 - if (!class_exists('MxChat_Tool_Registry') || !MxChat_Tool_Registry::is_enabled()) {
7119 - return false;
7120 - }
7121 - if (class_exists('MxChat_Model_Catalog') && !MxChat_Model_Catalog::supports_tools($selected_model)) {
7122 - return false;
7123 - }
7124 - $tools = MxChat_Tool_Registry::enabled_tools();
7125 - return !empty($tools);
7126 -}
7127 -
7128 -private function mxchat_fc_log($msg) {
7129 - if (defined('MXCHAT_DEV_MODE') && MXCHAT_DEV_MODE) {
7130 - error_log('[MxChat FC] ' . $msg);
7131 - }
7132 -}
7133 -
7134 -/**
7135 - * Resolve provider transport details. Returns null when FC can't run for this
7136 - * model/config (missing key, unsupported provider) so the caller falls back to
7137 - * the normal path. OpenAI/xAI/DeepSeek/OpenRouter/Custom share the
7138 - * OpenAI-compatible 'openai' family; Claude and Gemini are distinct.
7139 - */
7140 -private function mxchat_fc_resolve_provider($selected_model, $opts) {
7141 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
7142 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
7143 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
7144 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
7145 - if ($selected_model === 'openrouter') {
7146 - $model = isset($opts['openrouter_selected_model']) ? $opts['openrouter_selected_model'] : '';
7147 - $key = isset($opts['openrouter_api_key']) ? $opts['openrouter_api_key'] : '';
7148 - if ($model === '' || $key === '') return null;
7149 - return array('family'=>'openai','model'=>$model,'url'=>'https://openrouter.ai/api/v1/chat/completions',
7150 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7151 - }
7152 - $prefix = strtolower(explode('-', $selected_model)[0]);
7153 - switch ($prefix) {
7154 - case 'gpt': case 'o1': case 'o3': case 'o4':
7155 - $key = isset($opts['api_key']) ? $opts['api_key'] : '';
7156 - if ($key === '') return null;
7157 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.openai.com/v1/chat/completions',
7158 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7159 - case 'claude':
7160 - $key = isset($opts['claude_api_key']) ? $opts['claude_api_key'] : '';
7161 - if ($key === '') return null;
7162 - return array('family'=>'anthropic','model'=>$selected_model,'url'=>'https://api.anthropic.com/v1/messages',
7163 - 'headers'=>array('Content-Type'=>'application/json','x-api-key'=>$key,'anthropic-version'=>'2023-06-01'),'tag'=>'anthropic');
7164 - case 'gemini':
7165 - $key = isset($opts['gemini_api_key']) ? $opts['gemini_api_key'] : '';
7166 - if ($key === '') return null;
7167 - return array('family'=>'gemini','model'=>$selected_model,'key'=>$key,'tag'=>'gemini');
7168 - case 'grok': case 'xai':
7169 - $key = isset($opts['xai_api_key']) ? $opts['xai_api_key'] : '';
7170 - if ($key === '') return null;
7171 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.x.ai/v1/chat/completions',
7172 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'xai');
7173 - case 'deepseek':
7174 - $key = isset($opts['deepseek_api_key']) ? $opts['deepseek_api_key'] : '';
7175 - if ($key === '') return null;
7176 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.deepseek.com/v1/chat/completions',
7177 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7178 - case 'custom':
7179 - $base = isset($opts['custom_provider_base_url']) ? rtrim($opts['custom_provider_base_url'], '/') : '';
7180 - $key = isset($opts['custom_provider_api_key']) ? $opts['custom_provider_api_key'] : '';
7181 - $model = isset($opts['custom_provider_model']) ? $opts['custom_provider_model'] : '';
7182 - if ($base === '' || $model === '') return null;
7183 - $url = (strpos($base, 'chat/completions') !== false) ? $base : $base . '/chat/completions';
7184 - $headers = array('Content-Type'=>'application/json');
7185 - if ($key !== '') $headers['Authorization'] = 'Bearer '.$key;
7186 - return array('family'=>'openai','model'=>$model,'url'=>$url,'headers'=>$headers,'tag'=>'openai');
7187 - }
7188 - return null;
7189 -}
7190 -
7191 -/**
7192 - * Top-level function-calling attempt. Returns:
7193 - * ['handled'=>true, 'text'=>'<final answer>'] when the model used ≥1 tool
7194 - * ['handled'=>false] otherwise (caller falls back
7195 - * to the normal streamed path)
7196 - */
7197 -private function mxchat_fc_attempt($message, $relevant_content, $conversation_history, $selected_model, $opts, $session_id, $user_id) {
7198 - $prov = $this->mxchat_fc_resolve_provider($selected_model, $opts);
7199 - if (!$prov) {
7200 - return array('handled' => false);
7201 - }
7202 - $tools = MxChat_Tool_Registry::enabled_tools();
7203 - if (empty($tools)) {
7204 - return array('handled' => false);
7205 - }
7206 -
7207 - $bot_id = $this->get_current_bot_id($session_id);
7208 - $system = $this->get_system_instructions($bot_id, $session_id);
7209 -
7210 - // Force callbacks into return-mode (some echo SSE directly when streaming);
7211 - // we buffer the whole tool round, then emit once. Restored in finally.
7212 - $prev_streaming = $this->is_streaming;
7213 - $this->is_streaming = false;
3036 +// Function definition
3037 +// Function definition
3038 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history) {
7214 3039 try {
7215 - if ($prov['family'] === 'anthropic') {
7216 - return $this->mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7217 - } elseif ($prov['family'] === 'gemini') {
7218 - return $this->mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7219 - }
7220 - return $this->mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7221 - } catch (\Throwable $e) {
7222 - $this->mxchat_fc_log('attempt threw: ' . $e->getMessage());
7223 - return array('handled' => false);
7224 - } finally {
7225 - $this->is_streaming = $prev_streaming;
7226 - }
7227 -}
7228 -
7229 -/** Normalize MxChat history rows to [{role:user|assistant, content}]. */
7230 -private function mxchat_fc_normalize_history($conversation_history) {
7231 - $out = array();
7232 - if (!is_array($conversation_history)) return $out;
7233 - foreach ($conversation_history as $m) {
7234 - if (!is_array($m) || !isset($m['role']) || !isset($m['content'])) continue;
7235 - $role = $m['role'];
7236 - if ($role === 'bot' || $role === 'agent') $role = 'assistant';
7237 - if (!in_array($role, array('user', 'assistant'), true)) $role = 'user';
7238 - $out[] = array('role' => $role, 'content' => (string) $m['content']);
7239 - }
7240 - return $out;
7241 -}
7242 -
7243 -/** Execute the matched callback for a tool call. Returns ['ok'=>bool,'content'=>string]. */
7244 -private function mxchat_fc_execute_tool($tool_name, $args, $orig_message, $user_id, $session_id) {
7245 - $tool = MxChat_Tool_Registry::tool_by_name($tool_name, true); // enabled-only
7246 - if (!$tool) {
7247 - return array('ok' => false, 'content' => 'This tool is not available or not enabled.');
7248 - }
7249 - $fn = $tool['callback'];
7250 -
7251 - // MxChat callbacks are message-driven: hand them the model's `query`
7252 - // (falling back to the original user message).
7253 - $query = '';
7254 - if (is_array($args) && isset($args['query']) && is_string($args['query'])) {
7255 - $query = $args['query'];
7256 - }
7257 - if ($query === '') $query = $orig_message;
7258 -
7259 - // Synthetic intent row (matches wp_mxchat_intents columns → no undefined-prop warnings).
7260 - $synthetic_intent = (object) array(
7261 - 'id' => 0, 'intent_label' => $tool['label'], 'phrases' => '',
7262 - 'embedding_vector' => '', 'callback_function' => $fn,
7263 - 'similarity_threshold' => 0.0, 'enabled' => 1, 'enabled_bots' => null,
7264 - );
7265 -
7266 - try {
7267 - if (!empty($tool['is_addon'])) {
7268 - $result = apply_filters($fn, false, $query, $user_id, $session_id, $synthetic_intent);
7269 - } elseif (method_exists($this, $fn)) {
7270 - $result = call_user_func(array($this, $fn), $query, $user_id, $session_id, $synthetic_intent, null);
7271 - } else {
7272 - return array('ok' => false, 'content' => 'Tool implementation not found.');
7273 - }
7274 - } catch (\Throwable $e) {
7275 - $this->mxchat_fc_log("tool {$fn} threw: " . $e->getMessage());
7276 - return array('ok' => false, 'content' => 'The tool failed to run.');
7277 - }
7278 -
7279 - // plan-mxchat-20260617-48a57a — surface UI-bearing tool output.
7280 - // If the callback produced a UI element (generated image, product card, image
7281 - // gallery), its html MUST reach the FRONTEND as a real rendered bot message —
7282 - // NOT be stripped to text and handed to the model to paraphrase (that was the
7283 - // bug: under function calling, UI-bearing actions rendered nothing). Capture
7284 - // the html here; the FC outcome handler emits it in the response envelope.
7285 - $ui = $this->mxchat_fc_ui_payload_from($result);
7286 - if ($ui['html'] !== '' || !empty($ui['images'])) {
7287 - if ($ui['html'] !== '') {
7288 - $this->fc_ui_html .= ($this->fc_ui_html !== '' ? "\n" : '') . $ui['html'];
7289 - }
7290 - if (!empty($ui['images']) && is_array($ui['images'])) {
7291 - $this->fc_ui_images = array_merge($this->fc_ui_images, $ui['images']);
7292 - }
7293 - $this->fc_ui_captured = true;
7294 -
7295 - // Persist the html to the transcript ONLY if the callback did not already
7296 - // do so itself. Core image/search callbacks self-save (text + html);
7297 - // add-on callbacks (e.g. woo product cards) return html for the caller to
7298 - // save. ui_self_saves carries this from the registry; default by source
7299 - // (core self-saves, add-on does not) when a tool predates the flag.
7300 - $self_saves = array_key_exists('ui_self_saves', $tool)
7301 - ? !empty($tool['ui_self_saves'])
7302 - : empty($tool['is_addon']);
7303 - if ($ui['html'] !== '' && !$self_saves) {
7304 - $this->mxchat_save_chat_message($session_id, 'bot', $ui['html']);
7305 - }
7306 -
7307 - // Hand the MODEL a short acknowledgment (never the raw or stripped html)
7308 - // so the loop can add a one-line caption without trying to re-describe a
7309 - // visual it cannot see and without duplicating the displayed element.
7310 - $summary = isset($ui['text']) ? trim((string) $ui['text']) : '';
7311 - $ack = __('[A visual result has already been shown to the user in the chat. Do not repeat or describe it in detail — reply with at most a brief one-line caption.]', 'mxchat');
7312 - $content = $summary !== '' ? ($ack . ' ' . $summary) : $ack;
7313 - $this->mxchat_fc_log("executed {$fn} → [ui payload surfaced] " . substr($content, 0, 120));
7314 - return array('ok' => true, 'content' => $content);
7315 - }
7316 -
7317 - $content = $this->mxchat_fc_stringify_result($result);
7318 - $this->mxchat_fc_log("executed {$fn} → " . substr($content, 0, 160));
7319 - return array('ok' => true, 'content' => $content);
7320 -}
7321 -
7322 -/**
7323 - * Extract a UI payload (html + images + text) from a tool callback's return,
7324 - * falling back to $this->fallbackResponse for callbacks that return true after
7325 - * setting it. plan-mxchat-20260617-48a57a.
7326 - *
7327 - * @return array{html:string,images:array,text:string}
7328 - */
7329 -private function mxchat_fc_ui_payload_from($result) {
7330 - $src = null;
7331 - if (is_array($result)) {
7332 - $src = $result;
7333 - } elseif ($result === true && isset($this->fallbackResponse) && is_array($this->fallbackResponse)) {
7334 - $src = $this->fallbackResponse;
7335 - }
7336 - $html = (is_array($src) && isset($src['html']) && is_string($src['html'])) ? $src['html'] : '';
7337 - $images = (is_array($src) && isset($src['images']) && is_array($src['images'])) ? $src['images'] : array();
7338 - $text = (is_array($src) && isset($src['text'])) ? (string) $src['text'] : '';
7339 - return array('html' => $html, 'images' => $images, 'text' => $text);
7340 -}
7341 -
7342 -/** Coerce a callback's return (string|array|true|false) into a tool-result string. */
7343 -private function mxchat_fc_stringify_result($result) {
7344 - if (is_string($result)) {
7345 - return $result === '' ? 'No result.' : $result;
7346 - }
7347 - if ($result === true) {
7348 - // Callbacks that set fallbackResponse and return true.
7349 - $fb = isset($this->fallbackResponse) ? $this->fallbackResponse : null;
7350 - if (is_array($fb)) {
7351 - if (!empty($fb['text'])) return (string) $fb['text'];
7352 - if (!empty($fb['html'])) return wp_strip_all_tags((string) $fb['html']);
7353 - }
7354 - return 'Done.';
7355 - }
7356 - if ($result === false || $result === null) {
7357 - return 'No result.';
7358 - }
7359 - if (is_array($result)) {
7360 - if (isset($result['text']) && $result['text'] !== '') return (string) $result['text'];
7361 - if (isset($result['html']) && $result['html'] !== '') return wp_strip_all_tags((string) $result['html']);
7362 - $json = wp_json_encode($result);
7363 - return $json !== false ? $json : 'No result.';
7364 - }
7365 - return (string) $result;
7366 -}
7367 -
7368 -/** HTTP code + decoded body for a function-calling request. */
7369 -private function mxchat_fc_post($url, $body, $headers, $tag) {
7370 - $args = array(
7371 - 'body' => wp_json_encode($body),
7372 - 'headers' => $headers,
7373 - 'timeout' => 60,
7374 - 'redirection' => 5,
7375 - 'blocking' => true,
7376 - 'httpversion' => '1.0',
7377 - 'sslverify' => true,
7378 - );
7379 - $response = $this->mxchat_provider_call_with_retry($url, $args, $tag);
7380 - if (is_wp_error($response)) {
7381 - return array('code' => 0, 'data' => null, 'error' => $response->get_error_message());
7382 - }
7383 - $code = (int) wp_remote_retrieve_response_code($response);
7384 - $data = json_decode(wp_remote_retrieve_body($response), true);
7385 - return array('code' => $code, 'data' => $data, 'error' => null);
7386 -}
7387 -
7388 -/* ---------------- OpenAI-compatible loop (OpenAI/xAI/DeepSeek/OpenRouter/Custom) -------------- */
7389 -private function mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7390 - $messages = array();
7391 - $messages[] = array('role' => 'system', 'content' => $system . ' ' . $relevant_content);
7392 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7393 - $messages[] = $m;
7394 - }
7395 -
7396 - $depth = MxChat_Tool_Registry::max_depth();
7397 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7398 - $tool_schema = MxChat_Tool_Registry::to_openai_tools($tools);
7399 - $used_tool = false;
7400 - $calls_made = 0;
7401 -
7402 - for ($step = 0; $step <= $depth; $step++) {
7403 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7404 - $body = array('model' => $prov['model'], 'messages' => $messages, 'temperature' => 1, 'stream' => false);
7405 - if ($offer_tools) {
7406 - $body['tools'] = $tool_schema;
7407 - $body['tool_choice'] = 'auto';
7408 - }
7409 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7410 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7411 - $this->mxchat_fc_log('openai call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7412 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7413 - }
7414 - $msg = isset($r['data']['choices'][0]['message']) ? $r['data']['choices'][0]['message'] : null;
7415 - if (!$msg) {
7416 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7417 - }
7418 - $tool_calls = isset($msg['tool_calls']) && is_array($msg['tool_calls']) ? $msg['tool_calls'] : array();
7419 - if (empty($tool_calls)) {
7420 - $text = isset($msg['content']) ? trim((string) $msg['content']) : '';
7421 - if (!$used_tool) return array('handled' => false); // model never used a tool → normal path
7422 - return array('handled' => true, 'text' => ($text !== '' ? $text : $this->mxchat_fc_giveup_text()));
7423 - }
7424 - // Append the assistant tool-call turn verbatim, then a tool result per call.
7425 - $used_tool = true;
7426 - $messages[] = $msg;
7427 - foreach ($tool_calls as $tc) {
7428 - if ($calls_made >= $budget) break;
7429 - $calls_made++;
7430 - $name = isset($tc['function']['name']) ? $tc['function']['name'] : '';
7431 - $args = array();
7432 - if (isset($tc['function']['arguments'])) {
7433 - $decoded = json_decode($tc['function']['arguments'], true);
7434 - if (is_array($decoded)) $args = $decoded;
7435 - }
7436 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7437 - $messages[] = array(
7438 - 'role' => 'tool',
7439 - 'tool_call_id' => isset($tc['id']) ? $tc['id'] : '',
7440 - 'content' => $exec['content'],
7441 - );
7442 - }
7443 - }
7444 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7445 -}
7446 -
7447 -/* ---------------- Anthropic Claude loop ---------------- */
7448 -private function mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7449 - $messages = $this->mxchat_fc_normalize_history($conversation_history);
7450 - $messages[] = array('role' => 'user', 'content' => $relevant_content);
7451 -
7452 - $depth = MxChat_Tool_Registry::max_depth();
7453 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7454 - $tool_schema = MxChat_Tool_Registry::to_anthropic_tools($tools);
7455 - $omit_temp = $this->mxchat_claude_omits_temperature($prov['model']);
7456 - $used_tool = false;
7457 - $calls_made = 0;
7458 -
7459 - for ($step = 0; $step <= $depth; $step++) {
7460 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7461 - $body = array('model' => $prov['model'], 'max_tokens' => 1024, 'temperature' => 0.8,
7462 - 'messages' => $messages, 'system' => $system);
7463 - if ($omit_temp) unset($body['temperature']);
7464 - if ($offer_tools) {
7465 - $body['tools'] = $tool_schema;
7466 - $body['tool_choice'] = array('type' => 'auto');
7467 - }
7468 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7469 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7470 - $this->mxchat_fc_log('anthropic call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7471 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7472 - }
7473 - $content = isset($r['data']['content']) && is_array($r['data']['content']) ? $r['data']['content'] : array();
7474 - $tool_uses = array();
7475 - $text_out = '';
7476 - foreach ($content as $block) {
7477 - if (!isset($block['type'])) continue;
7478 - if ($block['type'] === 'tool_use') {
7479 - $tool_uses[] = $block;
7480 - } elseif ($block['type'] === 'text' && isset($block['text'])) {
7481 - $text_out .= $block['text'];
7482 - }
7483 - }
7484 - if (empty($tool_uses)) {
7485 - if (!$used_tool) return array('handled' => false);
7486 - $text_out = trim($text_out);
7487 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7488 - }
7489 - // Append the assistant turn (the full content array), then a user turn of tool_result blocks.
7490 - $used_tool = true;
7491 - $messages[] = array('role' => 'assistant', 'content' => $content);
7492 - $results = array();
7493 - foreach ($tool_uses as $tu) {
7494 - if ($calls_made >= $budget) break;
7495 - $calls_made++;
7496 - $name = isset($tu['name']) ? $tu['name'] : '';
7497 - $args = isset($tu['input']) && is_array($tu['input']) ? $tu['input'] : array();
7498 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7499 - $results[] = array(
7500 - 'type' => 'tool_result',
7501 - 'tool_use_id' => isset($tu['id']) ? $tu['id'] : '',
7502 - 'content' => $exec['content'],
7503 - );
7504 - }
7505 - $messages[] = array('role' => 'user', 'content' => $results);
7506 - }
7507 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7508 -}
7509 -
7510 -/* ---------------- Google Gemini loop ---------------- */
7511 -private function mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7512 - $contents = array();
7513 - $contents[] = array('role' => 'user', 'parts' => array(array('text' => '[System Instructions] ' . $system . ' ' . $relevant_content)));
7514 - $contents[] = array('role' => 'model', 'parts' => array(array('text' => 'I understand and will follow these instructions.')));
7515 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7516 - $contents[] = array('role' => ($m['role'] === 'assistant' ? 'model' : 'user'),
7517 - 'parts' => array(array('text' => $m['content'])));
7518 - }
7519 -
7520 - $depth = MxChat_Tool_Registry::max_depth();
7521 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7522 - $tool_schema = MxChat_Tool_Registry::to_gemini_tools($tools);
7523 - // Function calling (tools + functionDeclarations + toolConfig) is a v1beta feature on the
7524 - // Generative Language REST API. The v1 endpoint silently ignores the tools array, so a
7525 - // non-preview model (e.g. gemini-2.5-pro, gemini-3.5-flash, gemini-3.1-flash-lite) would
7526 - // just answer in text and never emit a tool call. Always use v1beta for the FC loop —
7527 - // confirmed against Google's function-calling docs (their REST example targets
7528 - // v1beta/models/gemini-3.5-flash:generateContent). v1beta is a superset, so every model
7529 - // reachable on v1 is also reachable here.
7530 - $api_version = 'v1beta';
7531 - $url = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $prov['model'] . ':generateContent?key=' . $prov['key'];
7532 - $headers = array('Content-Type' => 'application/json');
7533 - $used_tool = false;
7534 - $calls_made = 0;
7535 -
7536 - for ($step = 0; $step <= $depth; $step++) {
7537 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7538 - $body = array(
7539 - 'contents' => $contents,
7540 - 'generationConfig' => array('temperature' => 0.7, 'topP' => 0.95, 'topK' => 40, 'maxOutputTokens' => 8192),
7541 - );
7542 - if ($offer_tools) {
7543 - $body['tools'] = $tool_schema;
7544 - $body['toolConfig'] = array('functionCallingConfig' => array('mode' => 'AUTO'));
7545 - }
7546 - $r = $this->mxchat_fc_post($url, $body, $headers, 'gemini');
7547 - if ($r['code'] !== 200 || !is_array($r['data']) || isset($r['data']['error'])) {
7548 - $this->mxchat_fc_log('gemini call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7549 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7550 - }
7551 - $parts = isset($r['data']['candidates'][0]['content']['parts']) && is_array($r['data']['candidates'][0]['content']['parts'])
7552 - ? $r['data']['candidates'][0]['content']['parts'] : array();
7553 - $fn_calls = array();
7554 - $text_out = '';
7555 - foreach ($parts as $p) {
7556 - if (isset($p['functionCall'])) {
7557 - $fn_calls[] = $p['functionCall'];
7558 - } elseif (isset($p['text'])) {
7559 - $text_out .= $p['text'];
7560 - }
7561 - }
7562 - if (empty($fn_calls)) {
7563 - if (!$used_tool) return array('handled' => false);
7564 - $text_out = trim($text_out);
7565 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7566 - }
7567 - // Append the model turn (its parts) then a user turn of functionResponse parts.
7568 - $used_tool = true;
7569 - $contents[] = array('role' => 'model', 'parts' => $parts);
7570 - $resp_parts = array();
7571 - foreach ($fn_calls as $fcall) {
7572 - if ($calls_made >= $budget) break;
7573 - $calls_made++;
7574 - $name = isset($fcall['name']) ? $fcall['name'] : '';
7575 - $args = isset($fcall['args']) && is_array($fcall['args']) ? $fcall['args'] : array();
7576 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7577 - $fr = array('name' => $name, 'response' => array('result' => $exec['content']));
7578 - // Gemini 3 function calls carry a unique id; echo the matching id back in the
7579 - // functionResponse so the model maps the result to the right call (Google REST
7580 - // guidance). Older models omit the id — then we send none, exactly as before.
7581 - if (isset($fcall['id']) && $fcall['id'] !== '') { $fr['id'] = $fcall['id']; }
7582 - $resp_parts[] = array('functionResponse' => $fr);
7583 - }
7584 - $contents[] = array('role' => 'user', 'parts' => $resp_parts);
7585 - }
7586 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7587 -}
7588 -
7589 -private function mxchat_fc_giveup_text() {
7590 - return esc_html__('I looked into that but could not put together a final answer. Please try rephrasing your request.', 'mxchat');
7591 -}
7592 -
7593 -private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $openrouter_api_key, $conversation_history, $streaming = false, $session_id = '', $testing_data = null, $selected_model = 'gpt-5.1-chat-latest') {
7594 - try {
7595 3040 if (!$relevant_content) {
7596 - $error_response = [
7597 - 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7598 - 'error_code' => 'no_relevant_content'
7599 - ];
7600 -
7601 - if ($testing_data !== null) {
7602 - $error_response['testing_data'] = $testing_data;
7603 - }
7604 -
7605 - return $error_response;
3041 + return esc_html__("I'm sorry, I couldn't find relevant information on that topic.", 'mxchat');
7606 3042 }
7607 -
3043 + // Ensure conversation_history is an array
7608 3044 if (!is_array($conversation_history)) {
7609 3045 $conversation_history = array();
7610 3046 }
7611 -
7612 - // Check if this is an OpenRouter model
7613 - if ($selected_model === 'openrouter') {
7614 - // Get the actual OpenRouter model from options
7615 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7616 -
7617 - if (empty($openrouter_selected_model)) {
7618 - $error_response = [
7619 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7620 - 'error_code' => 'no_openrouter_model_selected'
7621 - ];
7622 - if ($testing_data !== null) {
7623 - $error_response['testing_data'] = $testing_data;
7624 - }
7625 - return $error_response;
7626 - }
7627 -
7628 - if (empty($openrouter_api_key)) {
7629 - $error_response = [
7630 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7631 - 'error_code' => 'missing_openrouter_api_key'
7632 - ];
7633 - if ($testing_data !== null) {
7634 - $error_response['testing_data'] = $testing_data;
7635 - }
7636 - return $error_response;
7637 - }
7638 -
7639 - if ($streaming) {
7640 - return $this->mxchat_generate_response_openrouter_stream(
7641 - $openrouter_selected_model,
7642 - $openrouter_api_key,
7643 - $conversation_history,
7644 - $relevant_content,
7645 - $session_id,
7646 - $testing_data
7647 - );
7648 - } else {
7649 - $response = $this->mxchat_generate_response_openrouter(
7650 - $openrouter_selected_model,
7651 - $openrouter_api_key,
7652 - $conversation_history,
7653 - $relevant_content,
7654 - $session_id
7655 - );
7656 - }
7657 -
7658 - if (is_array($response) && isset($response['error'])) {
7659 - if ($testing_data !== null) {
7660 - $response['testing_data'] = $testing_data;
7661 - }
7662 - return $response;
7663 - }
7664 -
7665 - return $response;
7666 - }
7667 -
3047 + // Get selected model with default fallback
3048 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
7668 3049 // Extract model prefix to determine the provider
7669 3050 $model_parts = explode('-', $selected_model);
7670 3051 $provider = strtolower($model_parts[0]);
7671 -
7672 3052 // Handle model selection based on provider prefix
7673 3053 switch ($provider) {
7674 3054 case 'gemini':
7675 3055 if (empty($gemini_api_key)) {
7676 - $error_response = [
7677 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
7678 - 'error_code' => 'missing_gemini_api_key'
7679 - ];
7680 - if ($testing_data !== null) {
7681 - $error_response['testing_data'] = $testing_data;
7682 - }
7683 - return $error_response;
3056 + throw new Exception(esc_html__('Google Gemini API key is not configured', 'mxchat'));
7684 3057 }
7685 - $response = $this->mxchat_generate_response_gemini(
3058 + return $this->mxchat_generate_response_gemini(
7686 3059 $selected_model,
7687 3060 $gemini_api_key,
7688 3061 $conversation_history,
7689 - $relevant_content,
7690 - $session_id
3062 + $relevant_content
7691 3063 );
7692 - break;
7693 -
7694 3064 case 'claude':
7695 3065 if (empty($claude_api_key)) {
7696 - $error_response = [
7697 - 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
7698 - 'error_code' => 'missing_claude_api_key'
7699 - ];
7700 - if ($testing_data !== null) {
7701 - $error_response['testing_data'] = $testing_data;
7702 - }
7703 - return $error_response;
3066 + throw new Exception(esc_html__('Claude API key is not configured', 'mxchat'));
7704 3067 }
7705 - if ($streaming) {
7706 - return $this->mxchat_generate_response_claude_stream(
7707 - $selected_model,
7708 - $claude_api_key,
7709 - $conversation_history,
7710 - $relevant_content,
7711 - $session_id,
7712 - $testing_data
7713 - );
7714 - } else {
7715 - $response = $this->mxchat_generate_response_claude(
7716 - $selected_model,
7717 - $claude_api_key,
7718 - $conversation_history,
7719 - $relevant_content,
7720 - $session_id
7721 - );
7722 - }
7723 - break;
7724 -
3068 + return $this->mxchat_generate_response_claude(
3069 + $selected_model,
3070 + $claude_api_key,
3071 + $conversation_history,
3072 + $relevant_content
3073 + );
7725 3074 case 'grok':
7726 3075 if (empty($xai_api_key)) {
7727 - $error_response = [
7728 - 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
7729 - 'error_code' => 'missing_xai_api_key'
7730 - ];
7731 - if ($testing_data !== null) {
7732 - $error_response['testing_data'] = $testing_data;
7733 - }
7734 - return $error_response;
3076 + throw new Exception(esc_html__('X.AI API key is not configured', 'mxchat'));
7735 3077 }
7736 - if ($streaming) {
7737 - return $this->mxchat_generate_response_xai_stream(
7738 - $selected_model,
7739 - $xai_api_key,
7740 - $conversation_history,
7741 - $relevant_content,
7742 - $session_id,
7743 - $testing_data
7744 - );
7745 - } else {
7746 - $response = $this->mxchat_generate_response_xai(
7747 - $selected_model,
7748 - $xai_api_key,
7749 - $conversation_history,
7750 - $relevant_content,
7751 - $session_id
7752 - );
7753 - }
7754 - break;
7755 -
3078 + return $this->mxchat_generate_response_xai(
3079 + $selected_model,
3080 + $xai_api_key,
3081 + $conversation_history,
3082 + $relevant_content
3083 + );
7756 3084 case 'deepseek':
7757 3085 if (empty($deepseek_api_key)) {
7758 - $error_response = [
7759 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
7760 - 'error_code' => 'missing_deepseek_api_key'
7761 - ];
7762 - if ($testing_data !== null) {
7763 - $error_response['testing_data'] = $testing_data;
7764 - }
7765 - return $error_response;
3086 + throw new Exception(esc_html__('DeepSeek API key is not configured', 'mxchat'));
7766 3087 }
7767 - if ($streaming) {
7768 - return $this->mxchat_generate_response_deepseek_stream(
7769 - $selected_model,
7770 - $deepseek_api_key,
7771 - $conversation_history,
7772 - $relevant_content,
7773 - $session_id,
7774 - $testing_data
7775 - );
7776 - } else {
7777 - $response = $this->mxchat_generate_response_deepseek(
7778 - $selected_model,
7779 - $deepseek_api_key,
7780 - $conversation_history,
7781 - $relevant_content,
7782 - $session_id
7783 - );
7784 - }
7785 - break;
7786 -
7787 - case 'custom':
7788 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7789 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7790 - if (empty($cp_base_url)) {
7791 - $error_response = [
7792 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7793 - 'error_code' => 'missing_custom_provider_base_url'
7794 - ];
7795 - if ($testing_data !== null) {
7796 - $error_response['testing_data'] = $testing_data;
7797 - }
7798 - return $error_response;
7799 - }
7800 - if ($streaming) {
7801 - return $this->mxchat_generate_response_custom_stream(
7802 - $selected_model,
7803 - $conversation_history,
7804 - $relevant_content,
7805 - $session_id,
7806 - $testing_data
7807 - );
7808 - } else {
7809 - $response = $this->mxchat_generate_response_custom(
7810 - $selected_model,
7811 - $conversation_history,
7812 - $relevant_content
7813 - );
7814 - }
7815 - break;
7816 -
3088 + return $this->mxchat_generate_response_deepseek(
3089 + $selected_model,
3090 + $deepseek_api_key,
3091 + $conversation_history,
3092 + $relevant_content
3093 + );
7817 3094 case 'gpt':
7818 - case 'o1':
7819 3095 if (empty($api_key)) {
7820 - $error_response = [
7821 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7822 - 'error_code' => 'missing_openai_api_key'
7823 - ];
7824 - if ($testing_data !== null) {
7825 - $error_response['testing_data'] = $testing_data;
7826 - }
7827 - return $error_response;
3096 + throw new Exception(esc_html__('OpenAI API key is not configured', 'mxchat'));
7828 3097 }
7829 -
7830 - // Check if web search is enabled for this OpenAI model
7831 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7832 - // Models that don't support web search
7833 - $unsupported_web_search_models = array('gpt-4.1-nano');
7834 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7835 -
7836 - if ($web_search_enabled && $model_supports_web_search) {
7837 - // Use Responses API (required for some models, or when web search is enabled)
7838 - return $this->mxchat_generate_response_openai_web_search(
7839 - $selected_model,
7840 - $api_key,
7841 - $conversation_history,
7842 - $relevant_content,
7843 - $session_id,
7844 - $testing_data,
7845 - $streaming
7846 - );
7847 - } elseif ($streaming) {
7848 - return $this->mxchat_generate_response_openai_stream(
7849 - $selected_model,
7850 - $api_key,
7851 - $conversation_history,
7852 - $relevant_content,
7853 - $session_id,
7854 - $testing_data
7855 - );
7856 - } else {
7857 - $response = $this->mxchat_generate_response_openai(
7858 - $selected_model,
7859 - $api_key,
7860 - $conversation_history,
7861 - $relevant_content,
7862 - $session_id
7863 - );
7864 - }
7865 - break;
7866 -
3098 + return $this->mxchat_generate_response_openai(
3099 + $selected_model,
3100 + $api_key,
3101 + $conversation_history,
3102 + $relevant_content
3103 + );
7867 3104 default:
3105 + // Default to OpenAI for custom models or unrecognized prefixes
7868 3106 if (empty($api_key)) {
7869 - $error_response = [
7870 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7871 - 'error_code' => 'missing_openai_api_key'
7872 - ];
7873 - if ($testing_data !== null) {
7874 - $error_response['testing_data'] = $testing_data;
7875 - }
7876 - return $error_response;
3107 + throw new Exception(esc_html__('OpenAI API key is not configured', 'mxchat'));
7877 3108 }
7878 -
7879 - // Check if web search is enabled (default case also handles OpenAI models)
7880 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7881 - $unsupported_web_search_models = array('gpt-4.1-nano');
7882 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7883 -
7884 - if ($web_search_enabled && $model_supports_web_search) {
7885 - return $this->mxchat_generate_response_openai_web_search(
7886 - $selected_model,
7887 - $api_key,
7888 - $conversation_history,
7889 - $relevant_content,
7890 - $session_id,
7891 - $testing_data,
7892 - $streaming
7893 - );
7894 - } elseif ($streaming) {
7895 - return $this->mxchat_generate_response_openai_stream(
7896 - $selected_model,
7897 - $api_key,
7898 - $conversation_history,
7899 - $relevant_content,
7900 - $session_id,
7901 - $testing_data
7902 - );
7903 - } else {
7904 - $response = $this->mxchat_generate_response_openai(
7905 - $selected_model,
7906 - $api_key,
7907 - $conversation_history,
7908 - $relevant_content,
7909 - $session_id
7910 - );
7911 - }
7912 - break;
7913 - }
7914 -
7915 - if (is_array($response) && isset($response['error'])) {
7916 - if ($testing_data !== null) {
7917 - $response['testing_data'] = $testing_data;
7918 - }
7919 - return $response;
7920 - }
7921 -
7922 - return $response;
7923 -
7924 - } catch (Exception $e) {
7925 - $error_response = [
7926 - 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
7927 - 'error_code' => 'system_exception',
7928 - 'exception_details' => $e->getMessage()
7929 - ];
7930 -
7931 - if ($testing_data !== null) {
7932 - $error_response['testing_data'] = $testing_data;
7933 - }
7934 -
7935 - return $error_response;
7936 - }
7937 -}
7938 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
7939 - try {
7940 - $bot_id = $this->get_current_bot_id($session_id);
7941 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
7942 -
7943 - if (!is_array($conversation_history)) {
7944 - $conversation_history = array();
7945 - }
7946 -
7947 - $formatted_conversation = array();
7948 -
7949 - $formatted_conversation[] = array(
7950 - 'role' => 'system',
7951 - 'content' => $system_prompt_instructions . " " . $relevant_content
7952 - );
7953 -
7954 - foreach ($conversation_history as $message) {
7955 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
7956 - $role = $message['role'];
7957 - if ($role === 'bot' || $role === 'agent') {
7958 - $role = 'assistant';
7959 - }
7960 - if (!in_array($role, ['system', 'assistant', 'user'])) {
7961 - $role = 'user';
7962 - }
7963 - $formatted_conversation[] = array(
7964 - 'role' => $role,
7965 - 'content' => $message['content']
3109 + return $this->mxchat_generate_response_openai(
3110 + $selected_model,
3111 + $api_key,
3112 + $conversation_history,
3113 + $relevant_content
7966 3114 );
7967 - }
7968 3115 }
7969 -
7970 - if (headers_sent() || !function_exists('curl_init')) {
7971 - $regular_response = $this->mxchat_generate_response_openrouter(
7972 - $selected_model,
7973 - $openrouter_api_key,
7974 - $conversation_history,
7975 - $relevant_content,
7976 - $session_id
7977 - );
7978 -
7979 - // Save bot response to transcript
7980 - if (!empty($regular_response) && !empty($session_id)) {
7981 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
7982 - }
7983 -
7984 - $response_data = [
7985 - 'text' => $regular_response,
7986 - 'html' => '',
7987 - 'session_id' => $session_id
7988 - ];
7989 -
7990 - if ($testing_data !== null) {
7991 - $response_data['testing_data'] = $testing_data;
7992 - }
7993 -
7994 - header('Content-Type: application/json');
7995 - echo json_encode($response_data);
7996 - return true;
7997 - }
7998 -
7999 - $body = json_encode([
8000 - 'model' => $selected_model,
8001 - 'messages' => $formatted_conversation,
8002 - 'temperature' => 1,
8003 - 'stream' => true
8004 - ]);
8005 -
8006 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
8007 - // inside WRITEFUNCTION on first byte of a successful upstream.
8008 -
8009 - $captured_status_code = 0;
8010 - $captured_body_pre_stream = '';
8011 - $full_response = '';
8012 - $stream_started = false;
8013 - $buffer = '';
8014 - $errno = 0;
8015 - $last_curl_error = '';
8016 - $http_code = 0;
8017 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8018 - $backoff_ms = array(0, 750, 2000);
8019 -
8020 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8021 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8022 - usleep($backoff_ms[$attempt] * 1000);
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, 'https://openrouter.ai/api/v1/chat/completions');
8033 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8034 - curl_setopt($ch, CURLOPT_POST, true);
8035 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8036 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8037 - 'Content-Type: application/json',
8038 - 'Authorization: Bearer ' . $openrouter_api_key,
8039 - 'HTTP-Referer: ' . home_url(),
8040 - 'X-Title: ' . get_bloginfo('name')
8041 - ));
8042 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8043 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8044 -
8045 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8046 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8047 - $captured_status_code = (int) $m[1];
8048 - }
8049 - return strlen($header);
8050 - });
8051 -
8052 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8053 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8054 - $captured_body_pre_stream .= $data;
8055 - return strlen($data);
8056 - }
8057 -
8058 - if (!$this->streaming_headers_sent) {
8059 - $this->setup_streaming_headers();
8060 - }
8061 -
8062 - if (!$stream_started && $testing_data !== null) {
8063 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8064 - flush();
8065 - $stream_started = true;
8066 - }
8067 -
8068 - $buffer .= $data;
8069 - $lines = explode("\n", $buffer);
8070 - $buffer = array_pop($lines);
8071 -
8072 - foreach ($lines as $line) {
8073 - if (trim($line) === '') {
8074 - continue;
8075 - }
8076 - if (strpos($line, 'data: ') !== 0) {
8077 - continue;
8078 - }
8079 -
8080 - $json_str = substr($line, 6);
8081 -
8082 - if (trim($json_str) === '[DONE]') {
8083 - echo "data: [DONE]\n\n";
8084 - flush();
8085 - continue;
8086 - }
8087 -
8088 - $json = json_decode(trim($json_str), true);
8089 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8090 - $content = $json['choices'][0]['delta']['content'];
8091 - $full_response .= $content;
8092 -
8093 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8094 - flush();
8095 - }
8096 - }
8097 -
8098 - return strlen($data);
8099 - });
8100 -
8101 - $response = curl_exec($ch);
8102 - $errno = curl_errno($ch);
8103 - $last_curl_error = curl_error($ch);
8104 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8105 - curl_close($ch);
8106 -
8107 - if (!$errno && $http_code === 200) {
8108 - break;
8109 - }
8110 -
8111 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8112 - $can_retry = !$this->streaming_headers_sent
8113 - && ($attempt + 1) < $max_attempts
8114 - && $is_transient;
8115 -
8116 - if (defined('WP_DEBUG') && WP_DEBUG) {
8117 - error_log(sprintf(
8118 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8119 - $attempt + 1, $max_attempts, $http_code, $errno,
8120 - $is_transient ? 'yes' : 'no',
8121 - $can_retry ? 'Retrying.' : 'Giving up.'
8122 - ));
8123 - }
8124 -
8125 - if (!$can_retry) {
8126 - break;
8127 - }
8128 - }
8129 -
8130 - if (!$errno && $http_code === 200) {
8131 - if (!empty($full_response) && !empty($session_id)) {
8132 - $rag_context_for_storage = null;
8133 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8134 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8135 -
8136 - if ($has_rag_data || $has_action_data) {
8137 - $rag_context_for_storage = [];
8138 -
8139 - if ($has_rag_data) {
8140 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8141 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8142 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8143 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8144 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8145 - }
8146 -
8147 - if ($has_action_data) {
8148 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8149 - }
8150 - }
8151 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8152 - }
8153 - return true;
8154 - }
8155 -
8156 - return $this->mxchat_stream_emit_fallback(
8157 - 'openai',
8158 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8159 - $session_id,
8160 - $testing_data
8161 - );
8162 -
8163 3116 } catch (Exception $e) {
8164 - return $this->mxchat_stream_emit_fallback(
8165 - 'openai',
8166 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8167 - $session_id,
8168 - $testing_data
3117 + error_log('MXChat Error: ' . $e->getMessage());
3118 + return sprintf(
3119 + esc_html__('An error occurred: %s', 'mxchat'),
3120 + esc_html($e->getMessage())
8169 3121 );
8170 3122 }
8171 3123 }
8172 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8173 - try {
8174 - $bot_id = $this->get_current_bot_id($session_id);
8175 -
8176 - // Get system prompt instructions using centralized function
8177 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8178 -
8179 - // Ensure conversation_history is an array
8180 - if (!is_array($conversation_history)) {
8181 - $conversation_history = array();
8182 - }
8183 3124
8184 - // Format conversation history for OpenAI
8185 - $formatted_conversation = array();
8186 -
8187 - $formatted_conversation[] = array(
8188 - 'role' => 'system',
8189 - 'content' => $system_prompt_instructions . " " . $relevant_content
8190 - );
8191 -
8192 - foreach ($conversation_history as $message) {
8193 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8194 - $role = $message['role'];
8195 - if ($role === 'bot' || $role === 'agent') {
8196 - $role = 'assistant';
8197 - }
8198 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8199 - $role = 'user';
8200 - }
8201 - $formatted_conversation[] = array(
8202 - 'role' => $role,
8203 - 'content' => $message['content']
8204 - );
8205 - }
8206 - }
8207 -
8208 - // Check if we can actually stream
8209 - if (headers_sent() || !function_exists('curl_init')) {
8210 - // Fallback to regular response with testing data
8211 - $regular_response = $this->mxchat_generate_response_openai(
8212 - $selected_model,
8213 - $api_key,
8214 - $conversation_history,
8215 - $relevant_content,
8216 - $session_id
8217 - );
8218 -
8219 - // Save bot response to transcript
8220 - if (!empty($regular_response) && !empty($session_id)) {
8221 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8222 - }
8223 -
8224 - $response_data = [
8225 - 'text' => $regular_response,
8226 - 'html' => '',
8227 - 'session_id' => $session_id
8228 - ];
8229 -
8230 - if ($testing_data !== null) {
8231 - $response_data['testing_data'] = $testing_data;
8232 - }
8233 -
8234 - header('Content-Type: application/json');
8235 - echo json_encode($response_data);
8236 - return true;
8237 - }
8238 -
8239 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
8240 - $is_gpt5_model = (
8241 - strpos($selected_model, 'gpt-5') === 0 ||
8242 - $selected_model === 'gpt-5.2' ||
8243 - $selected_model === 'gpt-5.1-2025-11-13' ||
8244 - $selected_model === 'gpt-5' ||
8245 - $selected_model === 'gpt-5-mini' ||
8246 - $selected_model === 'gpt-5-nano'
8247 - );
8248 -
8249 - // Build request body with optimal settings for fast streaming
8250 - $request_body = [
8251 - 'model' => $selected_model,
8252 - 'messages' => $formatted_conversation,
8253 - 'temperature' => 1,
8254 - 'stream' => true
8255 - ];
8256 -
8257 - // Add reasoning_effort only for GPT-5 models that support it
8258 - // These chat models don't support reasoning_effort parameter
8259 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8260 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
8261 - // GPT-5.1 uses 'low' instead of 'minimal'
8262 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8263 - $request_body['reasoning_effort'] = 'low';
8264 - } elseif ($selected_model === 'gpt-5.5') {
8265 - $request_body['reasoning_effort'] = 'none';
8266 - } elseif ($selected_model === 'gpt-5.4') {
8267 - $request_body['reasoning_effort'] = 'none';
8268 - } else {
8269 - $request_body['reasoning_effort'] = 'minimal';
8270 - }
8271 - }
8272 -
8273 - $body = json_encode($request_body);
8274 -
8275 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
8276 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
8277 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
8278 -
8279 - $captured_status_code = 0;
8280 - $captured_body_pre_stream = '';
8281 - $full_response = '';
8282 - $stream_started = false;
8283 - $buffer = '';
8284 - $errno = 0;
8285 - $last_curl_error = '';
8286 - $http_code = 0;
8287 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8288 - $backoff_ms = array(0, 750, 2000);
8289 -
8290 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8291 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8292 - usleep($backoff_ms[$attempt] * 1000);
8293 - }
8294 -
8295 - // Reset per-attempt capture state.
8296 - $captured_status_code = 0;
8297 - $captured_body_pre_stream = '';
8298 - $full_response = '';
8299 - $stream_started = false;
8300 - $buffer = '';
8301 -
8302 - $ch = curl_init();
8303 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
8304 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8305 - curl_setopt($ch, CURLOPT_POST, true);
8306 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8307 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8308 - 'Content-Type: application/json',
8309 - 'Authorization: Bearer ' . $api_key
8310 - ));
8311 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8312 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8313 -
8314 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
8315 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8316 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8317 - $captured_status_code = (int) $m[1];
8318 - }
8319 - return strlen($header);
8320 - });
8321 -
8322 - // Buffer control for real-time streaming
8323 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8324 - // V2 guard: if upstream returned non-200, buffer body for transient
8325 - // classification and DO NOT emit to client. Stream channel must NOT open.
8326 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8327 - $captured_body_pre_stream .= $data;
8328 - return strlen($data);
8329 - }
8330 -
8331 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
8332 - // After this point streaming_headers_sent === true → retry is structurally blocked.
8333 - if (!$this->streaming_headers_sent) {
8334 - $this->setup_streaming_headers();
8335 - }
8336 -
8337 - // Send testing data as the first event if available
8338 - if (!$stream_started && $testing_data !== null) {
8339 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8340 - flush();
8341 - $stream_started = true;
8342 - }
8343 -
8344 - // CRITICAL FIX: Append new data to buffer
8345 - $buffer .= $data;
8346 -
8347 - // Process complete lines only
8348 - $lines = explode("\n", $buffer);
8349 -
8350 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8351 - $buffer = array_pop($lines);
8352 -
8353 - foreach ($lines as $line) {
8354 - if (trim($line) === '') {
8355 - continue;
8356 - }
8357 - if (strpos($line, 'data: ') !== 0) {
8358 - continue;
8359 - }
8360 -
8361 - $json_str = substr($line, 6);
8362 -
8363 - if (trim($json_str) === '[DONE]') {
8364 - echo "data: [DONE]\n\n";
8365 - flush();
8366 - continue;
8367 - }
8368 -
8369 - $json = json_decode(trim($json_str), true);
8370 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8371 - $content = $json['choices'][0]['delta']['content'];
8372 - $full_response .= $content;
8373 -
8374 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8375 - flush();
8376 - }
8377 - }
8378 -
8379 - return strlen($data);
8380 - });
8381 -
8382 - $response = curl_exec($ch);
8383 - $errno = curl_errno($ch);
8384 - $last_curl_error = curl_error($ch);
8385 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8386 - curl_close($ch);
8387 -
8388 - if (!$errno && $http_code === 200) {
8389 - break; // Happy path — WRITEFUNCTION already streamed everything.
8390 - }
8391 -
8392 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8393 - $can_retry = !$this->streaming_headers_sent
8394 - && ($attempt + 1) < $max_attempts
8395 - && $is_transient;
8396 -
8397 - if (defined('WP_DEBUG') && WP_DEBUG) {
8398 - error_log(sprintf(
8399 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8400 - $attempt + 1, $max_attempts, $http_code, $errno,
8401 - $is_transient ? 'yes' : 'no',
8402 - $can_retry ? 'Retrying.' : 'Giving up.'
8403 - ));
8404 - }
8405 -
8406 - if (!$can_retry) {
8407 - break;
8408 - }
8409 - }
8410 -
8411 - // Post-loop branch.
8412 - if (!$errno && $http_code === 200) {
8413 - // Happy path — save the complete response to maintain chat persistence.
8414 - if (!empty($full_response) && !empty($session_id)) {
8415 - $rag_context_for_storage = null;
8416 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8417 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8418 -
8419 - if ($has_rag_data || $has_action_data) {
8420 - $rag_context_for_storage = [];
8421 -
8422 - if ($has_rag_data) {
8423 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8424 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8425 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8426 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8427 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8428 - }
8429 -
8430 - if ($has_action_data) {
8431 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8432 - }
8433 - }
8434 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8435 - }
8436 -
8437 - return true;
8438 - }
8439 -
8440 - // Failure path — branch on whether SSE channel was opened.
8441 - return $this->mxchat_stream_emit_fallback(
8442 - 'openai',
8443 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8444 - $session_id,
8445 - $testing_data
8446 - );
8447 -
8448 - } catch (Exception $e) {
8449 - return $this->mxchat_stream_emit_fallback(
8450 - 'openai',
8451 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8452 - $session_id,
8453 - $testing_data
8454 - );
3125 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
3126 + // Ensure conversation_history is an array
3127 + if (!is_array($conversation_history)) {
3128 + $conversation_history = array();
8455 3129 }
8456 -}
8457 3130
8458 -/**
8459 - * Shared fallback emitter for streaming chat functions. Two outcomes:
8460 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
8461 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
8462 - * normal bot bubble. Transcript row is persisted.
8463 - * - streaming_headers_sent === false: SSE channel never opened (retries
8464 - * exhausted on initial connect). Emit a clean JSON response — the path
8465 - * the widget would normally hit if streaming wasn't even attempted.
8466 - *
8467 - * Used by all six *_stream functions after their per-attempt retry loop.
8468 - */
8469 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
8470 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
3131 + // Get system prompt instructions from options
3132 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8471 3133
8472 - if ($this->streaming_headers_sent) {
8473 - if ($is_error_array) {
8474 - echo "data: " . json_encode([
8475 - 'error' => true,
8476 - 'error_message' => $regular_response['error'],
8477 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8478 - 'text' => $regular_response['error'],
8479 - 'message' => $regular_response['error']
8480 - ]) . "\n\n";
8481 - echo "data: [DONE]\n\n";
8482 - flush();
8483 - return true;
8484 - }
8485 - $fallback_message = (string) $regular_response;
8486 - if (!empty($fallback_message) && !empty($session_id)) {
8487 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8488 - }
8489 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
8490 - echo "data: [DONE]\n\n";
8491 - flush();
8492 - return true;
8493 - }
3134 + // Create a new array for the formatted conversation
3135 + $formatted_conversation = array();
8494 3136
8495 - // SSE channel never opened — clean JSON fallback.
8496 - if ($is_error_array) {
8497 - header('Content-Type: application/json');
8498 - echo json_encode(array(
8499 - 'error' => true,
8500 - 'error_message' => $regular_response['error'],
8501 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8502 - 'text' => $regular_response['error'],
8503 - 'message' => $regular_response['error'],
8504 - ));
8505 - return true;
8506 - }
8507 -
8508 - $fallback_message = (string) $regular_response;
8509 - if (!empty($fallback_message) && !empty($session_id)) {
8510 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8511 - }
8512 - $response_data = array(
8513 - 'text' => $fallback_message,
8514 - 'html' => '',
8515 - 'session_id' => $session_id,
3137 + // Add system message first
3138 + $formatted_conversation[] = array(
3139 + 'role' => 'system',
3140 + 'content' => $system_prompt_instructions . " " . $relevant_content
8516 3141 );
8517 - if ($testing_data !== null) {
8518 - $response_data['testing_data'] = $testing_data;
8519 - }
8520 - header('Content-Type: application/json');
8521 - echo json_encode($response_data);
8522 - return true;
8523 -}
8524 3142
8525 -/**
8526 - * Resolve custom (OpenAI-compatible) provider config from settings.
8527 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
8528 - */
8529 -private function mxchat_resolve_custom_provider() {
8530 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
8531 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
8532 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
8533 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
8534 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
8535 -
8536 - $chat_url = $base_url . '/chat/completions';
8537 - if (!empty($api_version)) {
8538 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
8539 - }
8540 -
8541 - $headers = array('Content-Type: application/json');
8542 - if (!empty($api_key)) {
8543 - if ($auth_scheme === 'api-key') {
8544 - $headers[] = 'api-key: ' . $api_key;
8545 - } else {
8546 - $headers[] = 'Authorization: Bearer ' . $api_key;
8547 - }
8548 - }
8549 -
8550 - return array(
8551 - 'base_url' => $base_url,
8552 - 'api_key' => $api_key,
8553 - 'model' => $model !== '' ? $model : 'default',
8554 - 'auth_scheme' => $auth_scheme,
8555 - 'api_version' => $api_version,
8556 - 'chat_url' => $chat_url,
8557 - 'headers' => $headers,
8558 - );
8559 -}
8560 -
8561 -/**
8562 - * Streaming chat completion against an OpenAI-compatible custom provider
8563 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
8564 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
8565 - */
8566 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8567 - try {
8568 - $cfg = $this->mxchat_resolve_custom_provider();
8569 - if (empty($cfg['base_url'])) {
8570 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8571 - }
8572 -
8573 - $bot_id = $this->get_current_bot_id($session_id);
8574 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8575 - if (!is_array($conversation_history)) {
8576 - $conversation_history = array();
8577 - }
8578 -
8579 - $formatted_conversation = array();
8580 - $formatted_conversation[] = array(
8581 - 'role' => 'system',
8582 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8583 - );
8584 - foreach ($conversation_history as $message) {
8585 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8586 - $role = $message['role'];
8587 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8588 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8589 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
8590 - }
8591 - }
8592 -
8593 - if (headers_sent() || !function_exists('curl_init')) {
8594 - // No streaming capability — fall through to non-stream wrapper
8595 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
8596 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
8597 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
8598 - }
8599 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
8600 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
8601 - header('Content-Type: application/json');
8602 - echo json_encode($response_data);
8603 - return true;
8604 - }
8605 -
8606 - $request_body = array(
8607 - 'model' => $cfg['model'],
8608 - 'messages' => $formatted_conversation,
8609 - 'stream' => true,
8610 - );
8611 - $body = json_encode($request_body);
8612 -
8613 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8614 -
8615 - $captured_status_code = 0;
8616 - $captured_body_pre_stream = '';
8617 - $full_response = '';
8618 - $stream_started = false;
8619 - $buffer = '';
8620 - $errno = 0;
8621 - $http_code = 0;
8622 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8623 - $backoff_ms = array(0, 750, 2000);
8624 -
8625 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8626 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8627 - usleep($backoff_ms[$attempt] * 1000);
8628 - }
8629 -
8630 - $captured_status_code = 0;
8631 - $captured_body_pre_stream = '';
8632 - $full_response = '';
8633 - $stream_started = false;
8634 - $buffer = '';
8635 -
8636 - $ch = curl_init();
8637 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8638 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8639 - curl_setopt($ch, CURLOPT_POST, true);
8640 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8641 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8642 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8643 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8644 -
8645 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8646 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8647 - $captured_status_code = (int) $m[1];
8648 - }
8649 - return strlen($header);
8650 - });
8651 -
8652 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8653 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8654 - $captured_body_pre_stream .= $data;
8655 - return strlen($data);
8656 - }
8657 -
8658 - if (!$this->streaming_headers_sent) {
8659 - $this->setup_streaming_headers();
8660 - }
8661 -
8662 - if (!$stream_started && $testing_data !== null) {
8663 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8664 - flush();
8665 - $stream_started = true;
8666 - }
8667 - $buffer .= $data;
8668 - $lines = explode("\n", $buffer);
8669 - $buffer = array_pop($lines);
8670 - foreach ($lines as $line) {
8671 - if (trim($line) === '') { continue; }
8672 - if (strpos($line, 'data: ') !== 0) { continue; }
8673 - $json_str = substr($line, 6);
8674 - if (trim($json_str) === '[DONE]') {
8675 - echo "data: [DONE]\n\n";
8676 - flush();
8677 - continue;
8678 - }
8679 - $json = json_decode(trim($json_str), true);
8680 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8681 - $content = $json['choices'][0]['delta']['content'];
8682 - $full_response .= $content;
8683 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8684 - flush();
8685 - }
8686 - }
8687 - return strlen($data);
8688 - });
8689 -
8690 - $response = curl_exec($ch);
8691 - $errno = curl_errno($ch);
8692 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8693 - curl_close($ch);
8694 -
8695 - if (!$errno && $http_code === 200) {
8696 - break;
8697 - }
8698 -
8699 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8700 - $can_retry = !$this->streaming_headers_sent
8701 - && ($attempt + 1) < $max_attempts
8702 - && $is_transient;
8703 -
8704 - if (defined('WP_DEBUG') && WP_DEBUG) {
8705 - error_log(sprintf(
8706 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8707 - $attempt + 1, $max_attempts, $http_code, $errno,
8708 - $is_transient ? 'yes' : 'no',
8709 - $can_retry ? 'Retrying.' : 'Giving up.'
8710 - ));
8711 - }
8712 -
8713 - if (!$can_retry) {
8714 - break;
8715 - }
8716 - }
8717 -
8718 - if (!$errno && $http_code === 200) {
8719 - if (!empty($full_response) && !empty($session_id)) {
8720 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8721 - }
8722 - return true;
8723 - }
8724 -
8725 - return $this->mxchat_stream_emit_fallback(
8726 - 'openai',
8727 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8728 - $session_id,
8729 - $testing_data
8730 - );
8731 -
8732 - } catch (Exception $e) {
8733 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8734 - }
8735 -}
8736 -
8737 -/**
8738 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8739 - * Returns string content on success, array['error'=>...] on failure.
8740 - */
8741 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8742 - $cfg = $this->mxchat_resolve_custom_provider();
8743 - if (empty($cfg['base_url'])) {
8744 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8745 - }
8746 -
8747 - $bot_id = $this->get_current_bot_id(null);
8748 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8749 - if (!is_array($conversation_history)) {
8750 - $conversation_history = array();
8751 - }
8752 -
8753 - $messages = array(array(
8754 - 'role' => 'system',
8755 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8756 - ));
3143 + // Add the rest of the conversation history
8757 3144 foreach ($conversation_history as $message) {
8758 3145 if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8759 3146 $role = $message['role'];
8760 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8761 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8762 - $messages[] = array('role' => $role, 'content' => $message['content']);
8763 - }
8764 - }
8765 3147
8766 - $headers_assoc = array('Content-Type' => 'application/json');
8767 - if (!empty($cfg['api_key'])) {
8768 - if ($cfg['auth_scheme'] === 'api-key') {
8769 - $headers_assoc['api-key'] = $cfg['api_key'];
8770 - } else {
8771 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8772 - }
8773 - }
8774 -
8775 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8776 - 'headers' => $headers_assoc,
8777 - 'body' => wp_json_encode(array(
8778 - 'model' => $cfg['model'],
8779 - 'messages' => $messages,
8780 - )),
8781 - 'timeout' => 120,
8782 - ), 'openai');
8783 -
8784 - if (is_wp_error($response)) {
8785 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8786 - }
8787 - $code = (int) wp_remote_retrieve_response_code($response);
8788 - if ($code < 200 || $code >= 300) {
8789 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8790 - }
8791 - $body = json_decode(wp_remote_retrieve_body($response), true);
8792 - if (isset($body['choices'][0]['message']['content'])) {
8793 - return (string) $body['choices'][0]['message']['content'];
8794 - }
8795 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8796 -}
8797 -
8798 -/**
8799 - * Generate response using OpenAI Responses API with web search tool
8800 - * This uses the newer Responses API which supports web search functionality
8801 - */
8802 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8803 - try {
8804 - $bot_id = $this->get_current_bot_id($session_id);
8805 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8806 -
8807 - if (!is_array($conversation_history)) {
8808 - $conversation_history = array();
8809 - }
8810 -
8811 - // Build the input for Responses API
8812 - // The Responses API uses a different format - we need to construct the input properly
8813 - $input_parts = [];
8814 -
8815 - // Add system instructions as context
8816 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8817 -
8818 - // Build conversation as input items for Responses API
8819 - foreach ($conversation_history as $message) {
8820 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8821 - $role = $message['role'];
8822 - if ($role === 'bot' || $role === 'agent') {
8823 - $role = 'assistant';
8824 - }
8825 - if (!in_array($role, ['assistant', 'user'])) {
8826 - $role = 'user';
8827 - }
8828 - $input_parts[] = [
8829 - 'type' => 'message',
8830 - 'role' => $role,
8831 - 'content' => $message['content']
8832 - ];
3148 + // Convert roles to supported format
3149 + if ($role === 'bot' || $role === 'agent') {
3150 + $role = 'assistant';
8833 3151 }
8834 - }
8835 -
8836 - // Build request body for Responses API
8837 - $request_body = [
8838 - 'model' => $selected_model,
8839 - 'input' => $input_parts,
8840 - 'instructions' => $system_context,
8841 - 'stream' => $streaming
8842 - ];
8843 -
8844 - // Only add web search tool if web search is enabled in settings
8845 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8846 - if ($web_search_enabled) {
8847 - $request_body['tools'] = [
8848 - ['type' => 'web_search']
8849 - ];
8850 - }
8851 -
8852 - // Add reasoning effort for supported models
8853 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8854 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8855 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8856 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8857 - $request_body['reasoning'] = ['effort' => 'low'];
8858 - } elseif ($selected_model === 'gpt-5.5') {
8859 - $request_body['reasoning'] = ['effort' => 'low'];
8860 - } elseif ($selected_model === 'gpt-5.4') {
8861 - $request_body['reasoning'] = ['effort' => 'low'];
3152 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3153 + $role = 'user';
8862 3154 }
8863 - }
8864 3155
8865 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8866 -
8867 - if ($streaming) {
8868 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8869 - } else {
8870 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
3156 + $formatted_conversation[] = array(
3157 + 'role' => $role,
3158 + 'content' => $message['content']
3159 + );
8871 3160 }
3161 + }
8872 3162
8873 - } catch (Exception $e) {
8874 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8875 - return [
8876 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8877 - 'error_code' => 'web_search_exception'
8878 - ];
8879 - }
8880 -}
3163 + $body = json_encode([
3164 + 'model' => $selected_model,
3165 + 'messages' => $formatted_conversation,
3166 + 'temperature' => 0.8,
3167 + 'stream' => false
3168 + ]);
8881 3169
8882 -/**
8883 - * Handle non-streaming web search response
8884 - */
8885 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8886 - $request_body['stream'] = false;
3170 + $args = [
3171 + 'body' => $body,
3172 + 'headers' => [
3173 + 'Content-Type' => 'application/json',
3174 + 'Authorization' => 'Bearer ' . $deepseek_api_key,
3175 + ],
3176 + 'timeout' => 60,
3177 + 'redirection' => 5,
3178 + 'blocking' => true,
3179 + 'httpversion' => '1.0',
3180 + 'sslverify' => true,
3181 + ];
8887 3182
8888 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
8889 - 'headers' => array(
8890 - 'Authorization' => 'Bearer ' . $api_key,
8891 - 'Content-Type' => 'application/json'
8892 - ),
8893 - 'body' => json_encode($request_body),
8894 - 'timeout' => 90
8895 - ), 'openai');
3183 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
8896 3184
8897 3185 if (is_wp_error($response)) {
8898 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
8899 - return [
8900 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
8901 - 'error_code' => 'web_search_connection_error'
8902 - ];
3186 + error_log('DeepSeek API Error: ' . $response->get_error_message());
3187 + return "Sorry, there was an error processing your request.";
8903 3188 }
8904 3189
8905 - $response_code = wp_remote_retrieve_response_code($response);
8906 3190 $response_body = wp_remote_retrieve_body($response);
3191 + $decoded_response = json_decode($response_body, true);
8907 3192
8908 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
8909 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
8910 -
8911 - if ($response_code !== 200) {
8912 - $error_data = json_decode($response_body, true);
8913 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
8914 - return [
8915 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
8916 - 'error_code' => 'web_search_api_error'
8917 - ];
3193 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3194 + return trim($decoded_response['choices'][0]['message']['content']);
3195 + } else {
3196 + error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
3197 + return "Sorry, I couldn't process that request.";
8918 3198 }
8919 -
8920 - $result = json_decode($response_body, true);
8921 -
8922 - if (json_last_error() !== JSON_ERROR_NONE) {
8923 - return [
8924 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
8925 - 'error_code' => 'web_search_json_error'
8926 - ];
8927 - }
8928 -
8929 - // Extract the response text and citations from Responses API format
8930 - $output_text = '';
8931 - $citations = [];
8932 -
8933 - if (isset($result['output'])) {
8934 - foreach ($result['output'] as $output_item) {
8935 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
8936 - foreach ($output_item['content'] as $content_item) {
8937 - if ($content_item['type'] === 'output_text') {
8938 - $output_text .= $content_item['text'];
8939 -
8940 - // Extract citations/annotations
8941 - if (isset($content_item['annotations'])) {
8942 - foreach ($content_item['annotations'] as $annotation) {
8943 - if ($annotation['type'] === 'url_citation') {
8944 - $citations[] = [
8945 - 'url' => $annotation['url'],
8946 - 'title' => $annotation['title'] ?? ''
8947 - ];
8948 - }
8949 - }
8950 - }
8951 - }
8952 - }
8953 - }
8954 - }
8955 - }
8956 -
8957 - // If we have citations, append them to the response
8958 - if (!empty($citations)) {
8959 - $output_text .= "\n\n**Sources:**\n";
8960 - $seen_urls = [];
8961 - foreach ($citations as $citation) {
8962 - if (!in_array($citation['url'], $seen_urls)) {
8963 - $seen_urls[] = $citation['url'];
8964 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
8965 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
8966 - }
8967 - }
8968 - }
8969 -
8970 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
8971 - // which includes rag_context for the "sources" link in transcripts.
8972 -
8973 - return $output_text;
8974 3199 }
8975 -
8976 -/**
8977 - * Handle streaming web search response using Responses API
8978 - */
8979 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
8980 - $request_body['stream'] = true;
8981 -
8982 - // Check if we can stream
8983 - if (headers_sent() || !function_exists('curl_init')) {
8984 - // Fallback to non-streaming
8985 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
3200 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
3201 + // Ensure conversation_history is an array
3202 + if (!is_array($conversation_history)) {
3203 + $conversation_history = array();
8986 3204 }
8987 3205
8988 - // Setup streaming headers
8989 - $this->setup_streaming_headers();
3206 + // Get system prompt instructions from options
3207 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
8990 3208
8991 - $ch = curl_init();
8992 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
8993 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8994 - curl_setopt($ch, CURLOPT_POST, true);
8995 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
8996 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8997 - 'Content-Type: application/json',
8998 - 'Authorization: Bearer ' . $api_key
8999 - ));
9000 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9001 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
3209 + // Create a new array for the formatted conversation
3210 + $formatted_conversation = array();
9002 3211
9003 - $full_response = '';
9004 - $stream_started = false;
9005 - $buffer = '';
9006 - $citations = [];
3212 + // Add system message first
3213 + $formatted_conversation[] = array(
3214 + 'role' => 'system',
3215 + 'content' => $system_prompt_instructions . " " . $relevant_content
3216 + );
9007 3217
9008 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
9009 - // Send testing data as first event if available
9010 - if (!$stream_started && $testing_data !== null) {
9011 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9012 - flush();
9013 - $stream_started = true;
9014 - }
3218 + // Add the rest of the conversation history
3219 + foreach ($conversation_history as $message) {
3220 + if (is_array($message) && isset($message['role']) && isset($message['content'])) {
3221 + $role = $message['role'];
9015 3222
9016 - $buffer .= $data;
9017 - $lines = explode("\n", $buffer);
9018 - $buffer = array_pop($lines);
9019 -
9020 - foreach ($lines as $line) {
9021 - if (trim($line) === '') continue;
9022 - if (strpos($line, 'data: ') !== 0) continue;
9023 -
9024 - $json_str = substr($line, 6);
9025 -
9026 - if (trim($json_str) === '[DONE]') {
9027 - // Append citations if we have any
9028 - if (!empty($citations)) {
9029 - $citation_text = "\n\n**Sources:**\n";
9030 - $seen_urls = [];
9031 - foreach ($citations as $citation) {
9032 - if (!in_array($citation['url'], $seen_urls)) {
9033 - $seen_urls[] = $citation['url'];
9034 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9035 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9036 - }
9037 - }
9038 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
9039 - $full_response .= $citation_text;
9040 - flush();
9041 - }
9042 - echo "data: [DONE]\n\n";
9043 - flush();
9044 - continue;
3223 + // Convert roles to supported format
3224 + if ($role === 'bot' || $role === 'agent') {
3225 + $role = 'assistant';
9045 3226 }
9046 -
9047 - $json = json_decode(trim($json_str), true);
9048 - if (!$json) continue;
9049 -
9050 - // Handle Responses API streaming events
9051 - // The format is different from Chat Completions
9052 - if (isset($json['type'])) {
9053 - switch ($json['type']) {
9054 - case 'response.output_text.delta':
9055 - // Text content delta
9056 - if (isset($json['delta'])) {
9057 - $content = $json['delta'];
9058 - $full_response .= $content;
9059 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9060 - flush();
9061 - }
9062 - break;
9063 -
9064 - case 'response.output_item.done':
9065 - // Check for citations in completed items
9066 - if (isset($json['item']['content'])) {
9067 - foreach ($json['item']['content'] as $content_item) {
9068 - if (isset($content_item['annotations'])) {
9069 - foreach ($content_item['annotations'] as $annotation) {
9070 - if ($annotation['type'] === 'url_citation') {
9071 - $citations[] = [
9072 - 'url' => $annotation['url'],
9073 - 'title' => $annotation['title'] ?? ''
9074 - ];
9075 - }
9076 - }
9077 - }
9078 - }
9079 - }
9080 - break;
9081 - }
3227 + if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
3228 + $role = 'user';
9082 3229 }
9083 - }
9084 3230
9085 - return strlen($data);
9086 - });
9087 -
9088 - $response = curl_exec($ch);
9089 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9090 -
9091 - if (curl_errno($ch) || $http_code !== 200) {
9092 - $curl_error = curl_error($ch);
9093 - curl_close($ch);
9094 -
9095 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
9096 -
9097 - return $this->mxchat_stream_emit_fallback(
9098 - 'web_search',
9099 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
9100 - $session_id,
9101 - $testing_data
9102 - );
9103 - }
9104 -
9105 - curl_close($ch);
9106 -
9107 - // Save the complete response with RAG context so the "sources" link
9108 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
9109 - if (!empty($full_response) && !empty($session_id)) {
9110 - $rag_context_for_storage = null;
9111 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9112 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9113 -
9114 - if ($has_rag_data || $has_action_data) {
9115 - $rag_context_for_storage = [];
9116 -
9117 - if ($has_rag_data) {
9118 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9119 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9120 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9121 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9122 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9123 - }
9124 -
9125 - if ($has_action_data) {
9126 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9127 - }
3231 + $formatted_conversation[] = array(
3232 + 'role' => $role,
3233 + 'content' => $message['content']
3234 + );
9128 3235 }
9129 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9130 3236 }
9131 3237
9132 - return true;
9133 -}
3238 + $body = json_encode([
3239 + 'model' => $selected_model,
3240 + 'messages' => $formatted_conversation,
3241 + 'temperature' => 0.8,
3242 + 'stream' => false
3243 + ]);
9134 3244
9135 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9136 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
9137 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
9138 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
9139 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
9140 - try {
9141 - // Get bot ID from session or request
9142 - $bot_id = $this->get_current_bot_id($session_id);
9143 -
9144 - // Get system prompt instructions using centralized function
9145 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9146 - // Ensure conversation_history is an array
9147 - if (!is_array($conversation_history)) {
9148 - $conversation_history = array();
9149 - }
3245 + $args = [
3246 + 'body' => $body,
3247 + 'headers' => [
3248 + 'Content-Type' => 'application/json',
3249 + 'Authorization' => 'Bearer ' . $api_key,
3250 + ],
3251 + 'timeout' => 60,
3252 + 'redirection' => 5,
3253 + 'blocking' => true,
3254 + 'httpversion' => '1.0',
3255 + 'sslverify' => true,
3256 + ];
9150 3257
9151 - // Clean and validate conversation history
9152 - foreach ($conversation_history as &$message) {
9153 - // Convert bot and agent roles to assistant
9154 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
9155 - $message['role'] = 'assistant';
9156 - }
9157 -
9158 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
9159 - if (!in_array($message['role'], ['assistant', 'user'])) {
9160 - $message['role'] = 'user';
9161 - }
3258 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
9162 3259
9163 - // Ensure content field exists
9164 - if (!isset($message['content']) || empty($message['content'])) {
9165 - $message['content'] = '';
9166 - }
9167 -
9168 - // Remove any unsupported fields
9169 - $message = array_intersect_key($message, array_flip(['role', 'content']));
9170 - }
9171 -
9172 - // Add relevant content as the latest user message
9173 - $conversation_history[] = [
9174 - 'role' => 'user',
9175 - 'content' => $relevant_content
9176 - ];
9177 -
9178 - // Prepare the request body with stream: true
9179 - $payload = [
9180 - 'model' => $selected_model,
9181 - 'messages' => $conversation_history,
9182 - 'max_tokens' => 1000,
9183 - 'temperature' => 0.8,
9184 - 'system' => $system_prompt_instructions,
9185 - 'stream' => true
9186 - ];
9187 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9188 - $body = json_encode($payload);
9189 -
9190 - // Check if we can actually stream (headers not sent, etc.)
9191 - if (headers_sent() || !function_exists('curl_init')) {
9192 - // Fallback to regular response with testing data
9193 - //error_log("MxChat: Streaming not possible, falling back to regular response");
9194 - $regular_response = $this->mxchat_generate_response_claude(
9195 - $selected_model,
9196 - $claude_api_key,
9197 - array_slice($conversation_history, 0, -1), // Remove the added content
9198 - $relevant_content,
9199 - $session_id
9200 - );
9201 -
9202 - // Save bot response to transcript
9203 - if (!empty($regular_response) && !empty($session_id)) {
9204 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9205 - }
9206 -
9207 - // Return as JSON with testing data
9208 - $response_data = [
9209 - 'text' => $regular_response,
9210 - 'html' => '',
9211 - 'session_id' => $session_id
9212 - ];
9213 -
9214 - if ($testing_data !== null) {
9215 - $response_data['testing_data'] = $testing_data;
9216 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
9217 - }
9218 -
9219 - // Clear any streaming headers and send JSON
9220 - if (headers_sent() === false) {
9221 - header('Content-Type: application/json');
9222 - }
9223 - echo json_encode($response_data);
9224 - return true; // Indicate we handled the response
9225 - }
9226 -
9227 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9228 -
9229 - $captured_status_code = 0;
9230 - $captured_body_pre_stream = '';
9231 - $full_response = '';
9232 - $stream_started = false;
9233 - $buffer = '';
9234 - $errno = 0;
9235 - $http_code = 0;
9236 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9237 - $backoff_ms = array(0, 750, 2000);
9238 -
9239 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9240 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9241 - usleep($backoff_ms[$attempt] * 1000);
9242 - }
9243 -
9244 - $captured_status_code = 0;
9245 - $captured_body_pre_stream = '';
9246 - $full_response = '';
9247 - $stream_started = false;
9248 - $buffer = '';
9249 -
9250 - $ch = curl_init();
9251 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
9252 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9253 - curl_setopt($ch, CURLOPT_POST, true);
9254 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9255 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9256 - 'Content-Type: application/json',
9257 - 'x-api-key: ' . $claude_api_key,
9258 - 'anthropic-version: 2023-06-01'
9259 - ));
9260 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9261 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9262 -
9263 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9264 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9265 - $captured_status_code = (int) $m[1];
9266 - }
9267 - return strlen($header);
9268 - });
9269 -
9270 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9271 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9272 - $captured_body_pre_stream .= $data;
9273 - return strlen($data);
9274 - }
9275 -
9276 - if (!$this->streaming_headers_sent) {
9277 - $this->setup_streaming_headers();
9278 - }
9279 -
9280 - if (!$stream_started && $testing_data !== null) {
9281 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9282 - flush();
9283 - $stream_started = true;
9284 - }
9285 -
9286 - $buffer .= $data;
9287 - $lines = explode("\n", $buffer);
9288 - $buffer = array_pop($lines);
9289 -
9290 - foreach ($lines as $line) {
9291 - if (trim($line) === '') {
9292 - continue;
9293 - }
9294 -
9295 - if (strpos($line, 'event: ') === 0) {
9296 - continue;
9297 - }
9298 -
9299 - if (strpos($line, 'data: ') === 0) {
9300 - $json_str = substr($line, 6);
9301 -
9302 - $json = json_decode(trim($json_str), true);
9303 - if (json_last_error() !== JSON_ERROR_NONE) {
9304 - continue;
9305 - }
9306 -
9307 - if (isset($json['type'])) {
9308 - switch ($json['type']) {
9309 - case 'content_block_delta':
9310 - if (isset($json['delta']['text'])) {
9311 - $content = $json['delta']['text'];
9312 - $full_response .= $content;
9313 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9314 - flush();
9315 - }
9316 - break;
9317 -
9318 - case 'message_stop':
9319 - echo "data: [DONE]\n\n";
9320 - flush();
9321 - break;
9322 -
9323 - case 'error':
9324 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
9325 - flush();
9326 - break;
9327 - }
9328 - }
9329 - }
9330 - }
9331 -
9332 - return strlen($data);
9333 - });
9334 -
9335 - $response = curl_exec($ch);
9336 - $errno = curl_errno($ch);
9337 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9338 - curl_close($ch);
9339 -
9340 - if (!$errno && $http_code === 200) {
9341 - break;
9342 - }
9343 -
9344 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
9345 - $can_retry = !$this->streaming_headers_sent
9346 - && ($attempt + 1) < $max_attempts
9347 - && $is_transient;
9348 -
9349 - if (defined('WP_DEBUG') && WP_DEBUG) {
9350 - error_log(sprintf(
9351 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9352 - $attempt + 1, $max_attempts, $http_code, $errno,
9353 - $is_transient ? 'yes' : 'no',
9354 - $can_retry ? 'Retrying.' : 'Giving up.'
9355 - ));
9356 - }
9357 -
9358 - if (!$can_retry) {
9359 - break;
9360 - }
9361 - }
9362 -
9363 - if ($errno || $http_code !== 200) {
9364 - return $this->mxchat_stream_emit_fallback(
9365 - 'anthropic',
9366 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content, $session_id),
9367 - $session_id,
9368 - $testing_data
9369 - );
9370 - }
9371 -
9372 - // Save the complete response to maintain chat persistence
9373 - if (!empty($full_response) && !empty($session_id)) {
9374 - // Prepare RAG context for streaming response
9375 - $rag_context_for_storage = null;
9376 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9377 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9378 -
9379 - if ($has_rag_data || $has_action_data) {
9380 - $rag_context_for_storage = [];
9381 -
9382 - if ($has_rag_data) {
9383 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9384 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9385 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9386 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9387 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9388 - }
9389 -
9390 - if ($has_action_data) {
9391 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9392 - }
9393 - }
9394 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9395 - }
9396 -
9397 - return true; // Indicate streaming completed successfully
9398 -
9399 - } catch (Exception $e) {
9400 - return $this->mxchat_stream_emit_fallback(
9401 - 'anthropic',
9402 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id),
9403 - $session_id,
9404 - $testing_data
9405 - );
3260 + if (is_wp_error($response)) {
3261 + error_log('OpenAI API Error: ' . $response->get_error_message());
3262 + return "Sorry, there was an error processing your request.";
9406 3263 }
9407 -}
9408 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9409 - try {
9410 - // Get bot ID from session or request
9411 - $bot_id = $this->get_current_bot_id($session_id);
9412 -
9413 - // Get system prompt instructions using centralized function
9414 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9415 -
9416 - // Ensure conversation_history is an array
9417 - if (!is_array($conversation_history)) {
9418 - $conversation_history = array();
9419 - }
9420 3264
9421 - // Format conversation history for X.AI (same as OpenAI format)
9422 - $formatted_conversation = array();
3265 + $response_body = wp_remote_retrieve_body($response);
3266 + $decoded_response = json_decode($response_body, true);
9423 3267
9424 - $formatted_conversation[] = array(
9425 - 'role' => 'system',
9426 - 'content' => $system_prompt_instructions . " " . $relevant_content
9427 - );
9428 -
9429 - foreach ($conversation_history as $message) {
9430 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9431 - $role = $message['role'];
9432 - if ($role === 'bot' || $role === 'agent') {
9433 - $role = 'assistant';
9434 - }
9435 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9436 - $role = 'user';
9437 - }
9438 - $formatted_conversation[] = array(
9439 - 'role' => $role,
9440 - 'content' => $message['content']
9441 - );
9442 - }
9443 - }
9444 -
9445 - // Check if we can actually stream
9446 - if (headers_sent() || !function_exists('curl_init')) {
9447 - // Fallback to regular response with testing data
9448 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
9449 - $regular_response = $this->mxchat_generate_response_xai(
9450 - $selected_model,
9451 - $xai_api_key,
9452 - $conversation_history,
9453 - $relevant_content,
9454 - $session_id
9455 - );
9456 -
9457 - // Save bot response to transcript
9458 - if (!empty($regular_response) && !empty($session_id)) {
9459 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9460 - }
9461 -
9462 - $response_data = [
9463 - 'text' => $regular_response,
9464 - 'html' => '',
9465 - 'session_id' => $session_id
9466 - ];
9467 -
9468 - if ($testing_data !== null) {
9469 - $response_data['testing_data'] = $testing_data;
9470 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
9471 - }
9472 -
9473 - header('Content-Type: application/json');
9474 - echo json_encode($response_data);
9475 - return true;
9476 - }
9477 -
9478 - // Prepare the request body with stream: true
9479 - $body = json_encode([
9480 - 'model' => $selected_model,
9481 - 'messages' => $formatted_conversation,
9482 - 'temperature' => 0.8,
9483 - 'stream' => true
9484 - ]);
9485 -
9486 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9487 -
9488 - $captured_status_code = 0;
9489 - $captured_body_pre_stream = '';
9490 - $full_response = '';
9491 - $stream_started = false;
9492 - $buffer = '';
9493 - $errno = 0;
9494 - $http_code = 0;
9495 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9496 - $backoff_ms = array(0, 750, 2000);
9497 -
9498 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9499 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9500 - usleep($backoff_ms[$attempt] * 1000);
9501 - }
9502 -
9503 - $captured_status_code = 0;
9504 - $captured_body_pre_stream = '';
9505 - $full_response = '';
9506 - $stream_started = false;
9507 - $buffer = '';
9508 -
9509 - $ch = curl_init();
9510 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
9511 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9512 - curl_setopt($ch, CURLOPT_POST, true);
9513 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9514 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9515 - 'Content-Type: application/json',
9516 - 'Authorization: Bearer ' . $xai_api_key
9517 - ));
9518 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9519 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9520 -
9521 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9522 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9523 - $captured_status_code = (int) $m[1];
9524 - }
9525 - return strlen($header);
9526 - });
9527 -
9528 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9529 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9530 - $captured_body_pre_stream .= $data;
9531 - return strlen($data);
9532 - }
9533 -
9534 - if (!$this->streaming_headers_sent) {
9535 - $this->setup_streaming_headers();
9536 - }
9537 -
9538 - if (!$stream_started && $testing_data !== null) {
9539 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9540 - flush();
9541 - $stream_started = true;
9542 - }
9543 -
9544 - $buffer .= $data;
9545 - $lines = explode("\n", $buffer);
9546 - $buffer = array_pop($lines);
9547 -
9548 - foreach ($lines as $line) {
9549 - if (trim($line) === '') {
9550 - continue;
9551 - }
9552 - if (strpos($line, 'data: ') !== 0) {
9553 - continue;
9554 - }
9555 -
9556 - $json_str = substr($line, 6);
9557 -
9558 - if (trim($json_str) === '[DONE]') {
9559 - echo "data: [DONE]\n\n";
9560 - flush();
9561 - continue;
9562 - }
9563 -
9564 - $json = json_decode(trim($json_str), true);
9565 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9566 - $content = $json['choices'][0]['delta']['content'];
9567 - $full_response .= $content;
9568 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9569 - flush();
9570 - }
9571 - }
9572 -
9573 - return strlen($data);
9574 - });
9575 -
9576 - $response = curl_exec($ch);
9577 - $errno = curl_errno($ch);
9578 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9579 - curl_close($ch);
9580 -
9581 - if (!$errno && $http_code === 200) {
9582 - break;
9583 - }
9584 -
9585 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
9586 - $can_retry = !$this->streaming_headers_sent
9587 - && ($attempt + 1) < $max_attempts
9588 - && $is_transient;
9589 -
9590 - if (defined('WP_DEBUG') && WP_DEBUG) {
9591 - error_log(sprintf(
9592 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9593 - $attempt + 1, $max_attempts, $http_code, $errno,
9594 - $is_transient ? 'yes' : 'no',
9595 - $can_retry ? 'Retrying.' : 'Giving up.'
9596 - ));
9597 - }
9598 -
9599 - if (!$can_retry) {
9600 - break;
9601 - }
9602 - }
9603 -
9604 - if ($errno || $http_code !== 200) {
9605 - return $this->mxchat_stream_emit_fallback(
9606 - 'xai',
9607 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id),
9608 - $session_id,
9609 - $testing_data
9610 - );
9611 - }
9612 -
9613 - // Save the complete response to maintain chat persistence
9614 - if (!empty($full_response) && !empty($session_id)) {
9615 - // Prepare RAG context for streaming response
9616 - $rag_context_for_storage = null;
9617 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9618 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9619 -
9620 - if ($has_rag_data || $has_action_data) {
9621 - $rag_context_for_storage = [];
9622 -
9623 - if ($has_rag_data) {
9624 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9625 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9626 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9627 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9628 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9629 - }
9630 -
9631 - if ($has_action_data) {
9632 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9633 - }
9634 - }
9635 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9636 - }
9637 -
9638 - return true; // Indicate streaming completed successfully
9639 -
9640 - } catch (Exception $e) {
9641 - return $this->mxchat_stream_emit_fallback(
9642 - 'xai',
9643 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9644 - $session_id,
9645 - $testing_data
9646 - );
3268 + if (isset($decoded_response['choices'][0]['message']['content'])) {
3269 + return trim($decoded_response['choices'][0]['message']['content']);
3270 + } else {
3271 + error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
3272 + return "Sorry, I couldn't process that request.";
9647 3273 }
9648 3274 }
9649 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9650 - try {
9651 - // Get bot ID from session or request
9652 - $bot_id = $this->get_current_bot_id($session_id);
9653 -
9654 - // Get system prompt instructions using centralized function
9655 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9656 -
9657 - // Ensure conversation_history is an array
9658 - if (!is_array($conversation_history)) {
9659 - $conversation_history = array();
9660 - }
3275 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
3276 + // Get system prompt instructions from options
3277 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
9661 3278
9662 - // Format conversation history for DeepSeek
9663 - $formatted_conversation = array();
3279 + // Add system prompt to relevant content
3280 + $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
9664 3281
9665 - $formatted_conversation[] = array(
9666 - 'role' => 'system',
9667 - 'content' => $system_prompt_instructions . " " . $relevant_content
9668 - );
3282 + // Prepend system instructions to the conversation history
3283 + array_unshift($conversation_history, [
3284 + 'role' => 'system',
3285 + 'content' => "Here are your instructions: " . $content_with_instructions
3286 + ]);
9669 3287
9670 - foreach ($conversation_history as $message) {
9671 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9672 - $role = $message['role'];
9673 - if ($role === 'bot' || $role === 'agent') {
9674 - $role = 'assistant';
9675 - }
9676 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9677 - $role = 'user';
9678 - }
9679 - $formatted_conversation[] = array(
9680 - 'role' => $role,
9681 - 'content' => $message['content']
9682 - );
3288 + // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
3289 + foreach ($conversation_history as &$message) {
3290 + if ($message['role'] === 'bot') {
3291 + $message['role'] = 'assistant';
3292 + } elseif ($message['role'] === 'agent') {
3293 + // Tag the message as coming from a live agent
3294 + $message['role'] = 'assistant';
3295 + if (!isset($message['metadata'])) {
3296 + $message['metadata'] = ['source' => 'live_agent'];
9683 3297 }
9684 3298 }
9685 3299
9686 - // Check if we can actually stream
9687 - if (headers_sent() || !function_exists('curl_init')) {
9688 - // Fallback to regular response with testing data
9689 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9690 - $regular_response = $this->mxchat_generate_response_deepseek(
9691 - $selected_model,
9692 - $deepseek_api_key,
9693 - $conversation_history,
9694 - $relevant_content,
9695 - $session_id
9696 - );
9697 -
9698 - // Save bot response to transcript
9699 - if (!empty($regular_response) && !empty($session_id)) {
9700 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9701 - }
9702 -
9703 - $response_data = [
9704 - 'text' => $regular_response,
9705 - 'html' => '',
9706 - 'session_id' => $session_id
9707 - ];
9708 -
9709 - if ($testing_data !== null) {
9710 - $response_data['testing_data'] = $testing_data;
9711 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
9712 - }
9713 -
9714 - header('Content-Type: application/json');
9715 - echo json_encode($response_data);
9716 - return true;
3300 + // Ensure all roles are valid
3301 + if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
3302 + $message['role'] = 'user'; // Default to 'user'
9717 3303 }
9718 -
9719 - // Prepare the request body with stream: true
9720 - $body = json_encode([
9721 - 'model' => $selected_model,
9722 - 'messages' => $formatted_conversation,
9723 - 'temperature' => 0.8,
9724 - 'stream' => true
9725 - ]);
9726 -
9727 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9728 -
9729 - $captured_status_code = 0;
9730 - $captured_body_pre_stream = '';
9731 - $full_response = '';
9732 - $stream_started = false;
9733 - $buffer = '';
9734 - $errno = 0;
9735 - $http_code = 0;
9736 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9737 - $backoff_ms = array(0, 750, 2000);
9738 -
9739 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9740 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9741 - usleep($backoff_ms[$attempt] * 1000);
9742 - }
9743 -
9744 - $captured_status_code = 0;
9745 - $captured_body_pre_stream = '';
9746 - $full_response = '';
9747 - $stream_started = false;
9748 - $buffer = '';
9749 -
9750 - $ch = curl_init();
9751 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9752 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9753 - curl_setopt($ch, CURLOPT_POST, true);
9754 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9755 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9756 - 'Content-Type: application/json',
9757 - 'Authorization: Bearer ' . $deepseek_api_key
9758 - ));
9759 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9760 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9761 -
9762 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9763 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9764 - $captured_status_code = (int) $m[1];
9765 - }
9766 - return strlen($header);
9767 - });
9768 -
9769 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9770 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9771 - $captured_body_pre_stream .= $data;
9772 - return strlen($data);
9773 - }
9774 -
9775 - if (!$this->streaming_headers_sent) {
9776 - $this->setup_streaming_headers();
9777 - }
9778 -
9779 - if (!$stream_started && $testing_data !== null) {
9780 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9781 - flush();
9782 - $stream_started = true;
9783 - }
9784 -
9785 - $buffer .= $data;
9786 - $lines = explode("\n", $buffer);
9787 - $buffer = array_pop($lines);
9788 -
9789 - foreach ($lines as $line) {
9790 - if (trim($line) === '') {
9791 - continue;
9792 - }
9793 - if (strpos($line, 'data: ') !== 0) {
9794 - continue;
9795 - }
9796 -
9797 - $json_str = substr($line, 6);
9798 -
9799 - if (trim($json_str) === '[DONE]') {
9800 - echo "data: [DONE]\n\n";
9801 - flush();
9802 - continue;
9803 - }
9804 -
9805 - $json = json_decode(trim($json_str), true);
9806 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9807 - $content = $json['choices'][0]['delta']['content'];
9808 - $full_response .= $content;
9809 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9810 - flush();
9811 - }
9812 - }
9813 -
9814 - return strlen($data);
9815 - });
9816 -
9817 - $response = curl_exec($ch);
9818 - $errno = curl_errno($ch);
9819 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9820 - curl_close($ch);
9821 -
9822 - if (!$errno && $http_code === 200) {
9823 - break;
9824 - }
9825 -
9826 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9827 - $can_retry = !$this->streaming_headers_sent
9828 - && ($attempt + 1) < $max_attempts
9829 - && $is_transient;
9830 -
9831 - if (defined('WP_DEBUG') && WP_DEBUG) {
9832 - error_log(sprintf(
9833 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9834 - $attempt + 1, $max_attempts, $http_code, $errno,
9835 - $is_transient ? 'yes' : 'no',
9836 - $can_retry ? 'Retrying.' : 'Giving up.'
9837 - ));
9838 - }
9839 -
9840 - if (!$can_retry) {
9841 - break;
9842 - }
9843 - }
9844 -
9845 - if ($errno || $http_code !== 200) {
9846 - return $this->mxchat_stream_emit_fallback(
9847 - 'openai',
9848 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id),
9849 - $session_id,
9850 - $testing_data
9851 - );
9852 - }
9853 -
9854 - // Save the complete response to maintain chat persistence
9855 - if (!empty($full_response) && !empty($session_id)) {
9856 - // Prepare RAG context for streaming response
9857 - $rag_context_for_storage = null;
9858 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9859 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9860 -
9861 - if ($has_rag_data || $has_action_data) {
9862 - $rag_context_for_storage = [];
9863 -
9864 - if ($has_rag_data) {
9865 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9866 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9867 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9868 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9869 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9870 - }
9871 -
9872 - if ($has_action_data) {
9873 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9874 - }
9875 - }
9876 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9877 - }
9878 -
9879 - return true; // Indicate streaming completed successfully
9880 -
9881 - } catch (Exception $e) {
9882 - return $this->mxchat_stream_emit_fallback(
9883 - 'openai',
9884 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
9885 - $session_id,
9886 - $testing_data
9887 - );
9888 3304 }
9889 -}
9890 3305
9891 3306
9892 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id = '') {
9893 - try {
9894 - if (!is_array($conversation_history)) {
9895 - $conversation_history = array();
9896 - }
3307 + // Build the request body
3308 + $body = json_encode([
3309 + 'model' => $selected_model,
3310 + 'messages' => $conversation_history,
3311 + 'temperature' => 0.8,
3312 + 'stream' => false
3313 + ]);
9897 3314
9898 - $bot_id = $this->get_current_bot_id($session_id);
9899 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9900 -
9901 - $formatted_conversation = array();
3315 + // Set up the API request
3316 + $args = [
3317 + 'body' => $body,
3318 + 'headers' => [
3319 + 'Content-Type' => 'application/json',
3320 + 'Authorization' => 'Bearer ' . $xai_api_key,
3321 + ],
3322 + 'timeout' => 60,
3323 + 'redirection' => 5,
3324 + 'blocking' => true,
3325 + 'httpversion' => '1.0',
3326 + 'sslverify' => true,
3327 + ];
9902 3328
9903 - $formatted_conversation[] = array(
9904 - 'role' => 'system',
9905 - 'content' => $system_prompt_instructions . " " . $relevant_content
9906 - );
3329 + // Make the API request
3330 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
9907 3331
9908 - foreach ($conversation_history as $message) {
9909 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9910 - $role = $message['role'];
9911 -
9912 - if ($role === 'bot' || $role === 'agent') {
9913 - $role = 'assistant';
9914 - }
9915 - if (!in_array($role, ['system', 'assistant', 'user'])) {
9916 - $role = 'user';
9917 - }
9918 -
9919 - $formatted_conversation[] = array(
9920 - 'role' => $role,
9921 - 'content' => $message['content']
9922 - );
9923 - }
9924 - }
9925 -
9926 - $body = json_encode([
9927 - 'model' => $selected_model,
9928 - 'messages' => $formatted_conversation,
9929 - 'temperature' => 1,
9930 - ]);
9931 -
9932 - $args = [
9933 - 'body' => $body,
9934 - 'headers' => [
9935 - 'Content-Type' => 'application/json',
9936 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
9937 - 'HTTP-Referer' => home_url(),
9938 - 'X-Title' => get_bloginfo('name'),
9939 - ],
9940 - 'timeout' => 60,
9941 - 'redirection' => 5,
9942 - 'blocking' => true,
9943 - 'httpversion' => '1.0',
9944 - 'sslverify' => true,
9945 - ];
9946 -
9947 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
9948 -
9949 - if (is_wp_error($response)) {
9950 - $error_message = $response->get_error_message();
9951 - return [
9952 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenRouter'),
9953 - 'error_code' => 'openrouter_connection_error',
9954 - 'provider' => 'openrouter'
9955 - ];
9956 - }
9957 -
9958 - $status_code = wp_remote_retrieve_response_code($response);
9959 - if ($status_code !== 200) {
9960 - $response_body = wp_remote_retrieve_body($response);
9961 - $decoded_response = json_decode($response_body, true);
9962 -
9963 - $error_message = isset($decoded_response['error']['message'])
9964 - ? $decoded_response['error']['message']
9965 - : 'HTTP Error ' . $status_code;
9966 -
9967 - return [
9968 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
9969 - 'error_code' => 'openrouter_api_error',
9970 - 'provider' => 'openrouter',
9971 - 'status_code' => $status_code
9972 - ];
9973 - }
9974 -
9975 - $response_body = wp_remote_retrieve_body($response);
9976 - $decoded_response = json_decode($response_body, true);
9977 -
9978 - if (isset($decoded_response['choices'][0]['message']['content'])) {
9979 - return trim($decoded_response['choices'][0]['message']['content']);
9980 - } else {
9981 - return [
9982 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
9983 - 'error_code' => 'openrouter_response_format_error',
9984 - 'provider' => 'openrouter'
9985 - ];
9986 - }
9987 - } catch (Exception $e) {
9988 - return [
9989 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
9990 - 'error_code' => 'openrouter_exception',
9991 - 'provider' => 'openrouter'
9992 - ];
3332 + // Process the response
3333 + if (is_wp_error($response)) {
3334 + return "Sorry, there was an error processing your request.";
9993 3335 }
9994 -}
9995 3336
9996 -/**
9997 - * Build a chat-bubble-safe message for a non-200 provider (chat) error.
9998 - *
9999 - * Visitors must NEVER see raw API internals (model names, key/billing/quota
10000 - * text). Admins (manage_options) get an actionable hint — and, for the common
10001 - * "model not available on this key" case, a direct pointer to change the model
10002 - * (the site owner can fix it in one click). Anthropic returns model-access as a
10003 - * 4xx with a message like "Claude Fable 5 is not available. Please use Opus 4.8."
10004 - *
10005 - * Provider-agnostic by design (reusable for the xai/gemini/deepseek branches),
10006 - * but Anthropic is the confirmed, reproduced case wired up here (plan 1d3b0f).
10007 - *
10008 - * @param int $http_code HTTP status from the provider.
10009 - * @param string $error_message Raw provider error.message (may be empty).
10010 - * @param string $provider_label Human provider name, e.g. 'Anthropic'.
10011 - * @return string Message safe to render as a chat bubble.
10012 - */
10013 -private function mxchat_friendly_chat_error($http_code, $error_message, $provider_label = '') {
10014 - $raw = trim((string) $error_message);
3337 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
10015 3338
10016 - // Detect a model-access / availability problem the site owner can fix by
10017 - // choosing a different model. (Anthropic phrasing + the common API shapes.)
10018 - $low = strtolower($raw);
10019 - $is_model_access = (strpos($low, 'not available') !== false)
10020 - || (strpos($low, 'does not have access') !== false)
10021 - || (strpos($low, 'do not have access') !== false)
10022 - || (strpos($low, 'does not exist') !== false) // OpenAI: "model `x` does not exist or you do not have access"
10023 - || (strpos($low, 'model_not_found') !== false)
10024 - || (strpos($low, 'not_found_error') !== false)
10025 - || (strpos($low, 'model not found') !== false) // xAI
10026 - || (strpos($low, 'not found') !== false) // Gemini: "models/x is not found for API version ..."
10027 - || (strpos($low, 'permission_denied') !== false) // Gemini gated model
10028 - || (strpos($low, 'permission denied') !== false);
10029 -
10030 - if (current_user_can('manage_options')) {
10031 - if ($is_model_access) {
10032 - return $raw !== ''
10033 - ? sprintf(
10034 - /* translators: %s: raw provider error detail */
10035 - esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings. (Details: %s)', 'mxchat'),
10036 - $raw
10037 - )
10038 - : esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings.', 'mxchat');
10039 - }
10040 - return $raw !== ''
10041 - ? sprintf(
10042 - /* translators: 1: provider label, 2: raw provider error detail */
10043 - esc_html__('The AI provider (%1$s) returned an error: %2$s. Check your model and API key in MxChat → Settings.', 'mxchat'),
10044 - $provider_label !== '' ? $provider_label : esc_html__('AI', 'mxchat'),
10045 - $raw
10046 - )
10047 - : esc_html__('The AI provider returned an error. Check your model and API key in MxChat → Settings.', 'mxchat');
3339 + if (isset($response_body['choices'][0]['message']['content'])) {
3340 + return trim($response_body['choices'][0]['message']['content']);
3341 + } else {
3342 + return "Sorry, I couldn't process that request.";
10048 3343 }
10049 -
10050 - // Visitors: friendly, generic, no internals leaked.
10051 - return esc_html__('Sorry, I\'m having trouble responding right now. Please try again in a moment.', 'mxchat');
10052 3344 }
3345 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
3346 + // Get system prompt instructions from options
3347 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10053 3348
10054 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id = '') {
10055 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
10056 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
10057 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
10058 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
10059 -
10060 - // Get bot ID from session or request
10061 - $bot_id = $this->get_current_bot_id($session_id);
10062 -
10063 - // Get system prompt instructions using centralized function
10064 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10065 -
10066 3349 // Clean and validate conversation history
10067 3350 foreach ($conversation_history as &$message) {
10068 3351 // Convert bot and agent roles to assistant
10069 3352 if ($message['role'] === 'bot' || $message['role'] === 'agent') {
@@ -10090,17 +3373,15 @@
10090 3373 'content' => $relevant_content
10091 3374 ];
10092 3375
10093 3376 // Build request body
10094 - $payload = [
3377 + $body = json_encode([
10095 3378 'model' => $selected_model,
10096 3379 'max_tokens' => 1000,
10097 3380 'temperature' => 0.8,
10098 3381 'messages' => $conversation_history,
10099 3382 'system' => $system_prompt_instructions
10100 - ];
10101 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
10102 - $body = json_encode($payload);
3383 + ]);
10103 3384
10104 3385 // Set up API request
10105 3386 $args = [
10106 3387 'body' => $body,
@@ -10116,13 +3397,13 @@
10116 3397 'sslverify' => true,
10117 3398 ];
10118 3399
10119 3400 // Make API request
10120 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
3401 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
10121 3402
10122 3403 // Check for WordPress errors
10123 3404 if (is_wp_error($response)) {
10124 - //error_log("Claude API request error: " . $response->get_error_message());
3405 + error_log("Claude API request error: " . $response->get_error_message());
10125 3406 return "Sorry, there was an error connecting to the API.";
10126 3407 }
10127 3408
10128 3409 // Check HTTP response code
@@ -10128,21 +3409,17 @@
10128 3409 // Check HTTP response code
10129 3410 $http_code = wp_remote_retrieve_response_code($response);
10130 3411 if ($http_code !== 200) {
10131 3412 $error_body = wp_remote_retrieve_body($response);
10132 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
3413 + error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
10133 3414
10134 3415 // Try to extract error message from response
10135 3416 $error_data = json_decode($error_body, true);
10136 - $error_message = isset($error_data['error']['message']) ?
10137 - $error_data['error']['message'] :
3417 + $error_message = isset($error_data['error']['message']) ?
3418 + $error_data['error']['message'] :
10138 3419 "HTTP error " . $http_code;
10139 -
10140 - // Surface an admin-actionable message (and a model-change pointer for the
10141 - // model-access case) without leaking raw API internals to visitors. This
10142 - // is the single chokepoint for BOTH the non-streaming and streaming Claude
10143 - // paths (the stream's non-200 fallback re-enters this method). plan 1d3b0f.
10144 - return $this->mxchat_friendly_chat_error($http_code, $error_message, 'Anthropic');
3420 +
3421 + return "Sorry, the API returned an error: " . $error_message;
10145 3422 }
10146 3423
10147 3424 // Parse response
10148 3425 $response_body = json_decode(wp_remote_retrieve_body($response), true);
@@ -10148,584 +3425,29 @@
10148 3425 $response_body = json_decode(wp_remote_retrieve_body($response), true);
10149 3426
10150 3427 // Check for JSON decode errors
10151 3428 if (json_last_error() !== JSON_ERROR_NONE) {
10152 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
3429 + error_log("Claude API JSON decode error: " . json_last_error_msg());
10153 3430 return "Sorry, there was an error processing the API response.";
10154 3431 }
10155 3432
10156 - // Extract and validate response content. claude-fable-5 prepends a
10157 - // thinking block to content even with no thinking param — take the first
10158 - // TEXT block rather than content[0].
10159 - if (isset($response_body['content']) && is_array($response_body['content'])) {
10160 - foreach ($response_body['content'] as $block) {
10161 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
10162 - return trim($block['text']);
10163 - }
10164 - }
3433 + // Extract and validate response content
3434 + if (isset($response_body['content']) &&
3435 + is_array($response_body['content']) &&
3436 + !empty($response_body['content']) &&
3437 + isset($response_body['content'][0]['text'])) {
3438 + return trim($response_body['content'][0]['text']);
10165 3439 }
10166 3440
10167 3441 // Log unexpected response format
10168 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
3442 + error_log("Claude API unexpected response format: " . print_r($response_body, true));
10169 3443 return "Sorry, I received an unexpected response format from the API.";
10170 3444 }
10171 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id = '') {
10172 - try {
10173 - // Ensure conversation_history is an array
10174 - if (!is_array($conversation_history)) {
10175 - $conversation_history = array();
10176 - }
10177 3445
10178 - // Get bot ID from session or request. plan eb9c38: resolve the real bot
10179 - // from the session (was hardcoded '' → always default bot on multi-bot
10180 - // installs) and fix the undefined $session_id that fed get_system_instructions.
10181 - $bot_id = $this->get_current_bot_id($session_id);
10182 -
10183 - // Get system prompt instructions using centralized function
10184 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10185 -
10186 - // Create a new array for the formatted conversation
10187 - $formatted_conversation = array();
10188 -
10189 - // Add system message first
10190 - $formatted_conversation[] = array(
10191 - 'role' => 'system',
10192 - 'content' => $system_prompt_instructions . " " . $relevant_content
10193 - );
10194 -
10195 - // Add the rest of the conversation history
10196 - foreach ($conversation_history as $message) {
10197 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10198 - $role = $message['role'];
10199 -
10200 - // Convert roles to supported format
10201 - if ($role === 'bot' || $role === 'agent') {
10202 - $role = 'assistant';
10203 - }
10204 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10205 - $role = 'user';
10206 - }
10207 -
10208 - $formatted_conversation[] = array(
10209 - 'role' => $role,
10210 - 'content' => $message['content']
10211 - );
10212 - }
10213 - }
10214 -
10215 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
10216 - $is_gpt5_model = (
10217 - strpos($selected_model, 'gpt-5') === 0 ||
10218 - $selected_model === 'gpt-5.2' ||
10219 - $selected_model === 'gpt-5.1-2025-11-13' ||
10220 - $selected_model === 'gpt-5' ||
10221 - $selected_model === 'gpt-5-mini' ||
10222 - $selected_model === 'gpt-5-nano'
10223 - );
10224 -
10225 - // Build request body with optimal settings for fast responses
10226 - $request_body = [
10227 - 'model' => $selected_model,
10228 - 'messages' => $formatted_conversation,
10229 - 'temperature' => 1,
10230 - 'stream' => false
10231 - ];
10232 -
10233 - // Add reasoning_effort only for GPT-5 models that support it
10234 - // These chat models don't support reasoning_effort parameter
10235 - $no_reasoning_models = array('gpt-5.2', 'gpt-5.1-chat-latest', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
10236 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
10237 - // GPT-5.1 uses 'low' instead of 'minimal'
10238 - if ($selected_model === 'gpt-5.1-2025-11-13') {
10239 - $request_body['reasoning_effort'] = 'low';
10240 - } elseif ($selected_model === 'gpt-5.5') {
10241 - $request_body['reasoning_effort'] = 'none';
10242 - } elseif ($selected_model === 'gpt-5.4') {
10243 - $request_body['reasoning_effort'] = 'none';
10244 - } else {
10245 - $request_body['reasoning_effort'] = 'minimal';
10246 - }
10247 - }
10248 -
10249 - $body = json_encode($request_body);
10250 -
10251 - $args = [
10252 - 'body' => $body,
10253 - 'headers' => [
10254 - 'Content-Type' => 'application/json',
10255 - 'Authorization' => 'Bearer ' . $api_key,
10256 - ],
10257 - 'timeout' => 60,
10258 - 'redirection' => 5,
10259 - 'blocking' => true,
10260 - 'httpversion' => '1.0',
10261 - 'sslverify' => true,
10262 - ];
10263 -
10264 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
10265 -
10266 - if (is_wp_error($response)) {
10267 - $error_message = $response->get_error_message();
10268 - return [
10269 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenAI'),
10270 - 'error_code' => 'openai_connection_error',
10271 - 'provider' => 'openai'
10272 - ];
10273 - }
10274 -
10275 - $status_code = wp_remote_retrieve_response_code($response);
10276 - if ($status_code !== 200) {
10277 - $response_body = wp_remote_retrieve_body($response);
10278 - $decoded_response = json_decode($response_body, true);
10279 -
10280 - $error_message = isset($decoded_response['error']['message'])
10281 - ? $decoded_response['error']['message']
10282 - : 'HTTP Error ' . $status_code;
10283 -
10284 - $error_type = isset($decoded_response['error']['type'])
10285 - ? $decoded_response['error']['type']
10286 - : 'unknown';
10287 -
10288 - // Handle specific error types
10289 - switch ($error_type) {
10290 - case 'invalid_request_error':
10291 - if (strpos($error_message, 'API key') !== false) {
10292 - return [
10293 - 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
10294 - 'error_code' => 'openai_invalid_api_key',
10295 - 'provider' => 'openai'
10296 - ];
10297 - }
10298 - break;
10299 -
10300 - case 'authentication_error':
10301 - return [
10302 - 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
10303 - 'error_code' => 'openai_auth_error',
10304 - 'provider' => 'openai'
10305 - ];
10306 -
10307 - case 'rate_limit_exceeded':
10308 - return [
10309 - 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
10310 - 'error_code' => 'openai_rate_limit',
10311 - 'provider' => 'openai'
10312 - ];
10313 -
10314 - case 'quota_exceeded':
10315 - return [
10316 - 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
10317 - 'error_code' => 'openai_quota_exceeded',
10318 - 'provider' => 'openai'
10319 - ];
10320 - }
10321 -
10322 - // Generic error fallback only — the typed cases above already produce
10323 - // clean messages. Route the raw-tail generic case through the leak-safe
10324 - // helper so visitors never see provider internals. plan 5da59a.
10325 - return [
10326 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'OpenAI'),
10327 - 'error_code' => 'openai_api_error',
10328 - 'provider' => 'openai',
10329 - 'status_code' => $status_code
10330 - ];
10331 - }
10332 -
10333 - $response_body = wp_remote_retrieve_body($response);
10334 - $decoded_response = json_decode($response_body, true);
10335 -
10336 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10337 - return trim($decoded_response['choices'][0]['message']['content']);
10338 - } else {
10339 - return [
10340 - 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
10341 - 'error_code' => 'openai_response_format_error',
10342 - 'provider' => 'openai'
10343 - ];
10344 - }
10345 - } catch (Exception $e) {
10346 - return [
10347 - 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
10348 - 'error_code' => 'openai_exception',
10349 - 'provider' => 'openai'
10350 - ];
10351 - }
10352 -}
10353 -
10354 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id = '') {
10355 - try {
10356 - // Get bot ID from session or request
10357 - $bot_id = $this->get_current_bot_id($session_id);
10358 -
10359 - // Get system prompt instructions using centralized function
10360 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10361 -
10362 - // Add system prompt to relevant content
10363 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10364 -
10365 - // Prepend system instructions to the conversation history
10366 - array_unshift($conversation_history, [
10367 - 'role' => 'system',
10368 - 'content' => "Here are your instructions: " . $content_with_instructions
10369 - ]);
10370 -
10371 - // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
10372 - foreach ($conversation_history as &$message) {
10373 - if ($message['role'] === 'bot') {
10374 - $message['role'] = 'assistant';
10375 - } elseif ($message['role'] === 'agent') {
10376 - // Tag the message as coming from a live agent
10377 - $message['role'] = 'assistant';
10378 - if (!isset($message['metadata'])) {
10379 - $message['metadata'] = ['source' => 'live_agent'];
10380 - }
10381 - }
10382 -
10383 - // Ensure all roles are valid
10384 - if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
10385 - $message['role'] = 'user'; // Default to 'user'
10386 - }
10387 - }
10388 -
10389 - // Build the request body
10390 - $body = json_encode([
10391 - 'model' => $selected_model,
10392 - 'messages' => $conversation_history,
10393 - 'temperature' => 0.8,
10394 - 'stream' => false
10395 - ]);
10396 -
10397 - // Set up the API request
10398 - $args = [
10399 - 'body' => $body,
10400 - 'headers' => [
10401 - 'Content-Type' => 'application/json',
10402 - 'Authorization' => 'Bearer ' . $xai_api_key,
10403 - ],
10404 - 'timeout' => 60,
10405 - 'redirection' => 5,
10406 - 'blocking' => true,
10407 - 'httpversion' => '1.0',
10408 - 'sslverify' => true,
10409 - ];
10410 -
10411 - // Make the API request
10412 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
10413 -
10414 - // Process the response
10415 - if (is_wp_error($response)) {
10416 - $error_message = $response->get_error_message();
10417 - //error_log('X.AI API Error: ' . $error_message);
10418 - return [
10419 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'X.AI'),
10420 - 'error_code' => 'xai_connection_error',
10421 - 'provider' => 'xai'
10422 - ];
10423 - }
10424 -
10425 - $status_code = wp_remote_retrieve_response_code($response);
10426 - if ($status_code !== 200) {
10427 - $response_body = wp_remote_retrieve_body($response);
10428 - $decoded_response = json_decode($response_body, true);
10429 -
10430 - // Log the full response for debugging
10431 - //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
10432 -
10433 - // Extract error message from X.AI's specific format
10434 - $error_message = '';
10435 -
10436 - // Check for direct error string (as seen in your logs)
10437 - if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
10438 - $error_message = $decoded_response['error'];
10439 - }
10440 - // Check for nested error object (OpenAI style)
10441 - elseif (isset($decoded_response['error']['message'])) {
10442 - $error_message = $decoded_response['error']['message'];
10443 - }
10444 - // Check for top-level message
10445 - elseif (isset($decoded_response['message'])) {
10446 - $error_message = $decoded_response['message'];
10447 - }
10448 - // Fallback
10449 - else {
10450 - $error_message = 'HTTP Error ' . $status_code;
10451 - }
10452 -
10453 - //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
10454 -
10455 - // Check for API key errors using string matching
10456 - if (stripos($error_message, 'api key') !== false ||
10457 - stripos($error_message, 'incorrect api key') !== false ||
10458 - stripos($error_message, 'invalid api key') !== false) {
10459 - return [
10460 - 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
10461 - 'error_code' => 'xai_invalid_api_key',
10462 - 'provider' => 'xai'
10463 - ];
10464 - }
10465 -
10466 - // Authentication errors
10467 - if ($status_code === 401 || $status_code === 403 ||
10468 - stripos($error_message, 'auth') !== false) {
10469 - return [
10470 - 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
10471 - 'error_code' => 'xai_auth_error',
10472 - 'provider' => 'xai'
10473 - ];
10474 - }
10475 -
10476 - // Model errors
10477 - if (stripos($error_message, 'model') !== false) {
10478 - return [
10479 - 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
10480 - 'error_code' => 'xai_invalid_model',
10481 - 'provider' => 'xai'
10482 - ];
10483 - }
10484 -
10485 - // Rate limit errors
10486 - if ($status_code === 429 ||
10487 - stripos($error_message, 'rate') !== false ||
10488 - stripos($error_message, 'limit') !== false) {
10489 - return [
10490 - 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
10491 - 'error_code' => 'xai_rate_limit',
10492 - 'provider' => 'xai'
10493 - ];
10494 - }
10495 -
10496 - // Quota errors
10497 - if (stripos($error_message, 'quota') !== false ||
10498 - stripos($error_message, 'billing') !== false) {
10499 - return [
10500 - 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
10501 - 'error_code' => 'xai_quota_exceeded',
10502 - 'provider' => 'xai'
10503 - ];
10504 - }
10505 -
10506 - // Server errors
10507 - if ($status_code >= 500) {
10508 - return [
10509 - 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
10510 - 'error_code' => 'xai_service_unavailable',
10511 - 'provider' => 'xai'
10512 - ];
10513 - }
10514 -
10515 - // Generic error fallback. Route the user-facing text through the
10516 - // leak-safe helper (admins get an actionable hint, visitors a generic
10517 - // fallback) instead of echoing raw provider internals. Preserve the
10518 - // structured contract (error_code/provider/status_code) for logging. plan 5da59a.
10519 - return [
10520 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'xAI'),
10521 - 'error_code' => 'xai_api_error',
10522 - 'provider' => 'xai',
10523 - 'status_code' => $status_code
10524 - ];
10525 - }
10526 -
10527 - $response_body = wp_remote_retrieve_body($response);
10528 - $decoded_response = json_decode($response_body, true);
10529 -
10530 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10531 - return trim($decoded_response['choices'][0]['message']['content']);
10532 - } else {
10533 - //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
10534 - return [
10535 - 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
10536 - 'error_code' => 'xai_response_format_error',
10537 - 'provider' => 'xai'
10538 - ];
10539 - }
10540 -} catch (Exception $e) {
10541 - //error_log('X.AI Exception: ' . $e->getMessage());
10542 - return [
10543 - 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
10544 - 'error_code' => 'xai_exception',
10545 - 'provider' => 'xai'
10546 - ];
10547 -}
10548 -
10549 -
10550 -}
10551 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id = '') {
10552 - try {
10553 - // Ensure conversation_history is an array
10554 - if (!is_array($conversation_history)) {
10555 - $conversation_history = array();
10556 - }
10557 -
10558 - // Get bot ID from session or request
10559 - $bot_id = $this->get_current_bot_id($session_id);
10560 -
10561 - // Get system prompt instructions using centralized function
10562 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10563 -
10564 - // Create a new array for the formatted conversation
10565 - $formatted_conversation = array();
10566 -
10567 - // Add system message first
10568 - $formatted_conversation[] = array(
10569 - 'role' => 'system',
10570 - 'content' => $system_prompt_instructions . " " . $relevant_content
10571 - );
10572 -
10573 - // Add the rest of the conversation history
10574 - foreach ($conversation_history as $message) {
10575 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10576 - $role = $message['role'];
10577 -
10578 - // Convert roles to supported format
10579 - if ($role === 'bot' || $role === 'agent') {
10580 - $role = 'assistant';
10581 - }
10582 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10583 - $role = 'user';
10584 - }
10585 -
10586 - $formatted_conversation[] = array(
10587 - 'role' => $role,
10588 - 'content' => $message['content']
10589 - );
10590 - }
10591 - }
10592 -
10593 - $body = json_encode([
10594 - 'model' => $selected_model,
10595 - 'messages' => $formatted_conversation,
10596 - 'temperature' => 0.8,
10597 - 'stream' => false
10598 - ]);
10599 -
10600 - $args = [
10601 - 'body' => $body,
10602 - 'headers' => [
10603 - 'Content-Type' => 'application/json',
10604 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
10605 - ],
10606 - 'timeout' => 60,
10607 - 'redirection' => 5,
10608 - 'blocking' => true,
10609 - 'httpversion' => '1.0',
10610 - 'sslverify' => true,
10611 - ];
10612 -
10613 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
10614 -
10615 - if (is_wp_error($response)) {
10616 - $error_message = $response->get_error_message();
10617 - //error_log('DeepSeek API Error: ' . $error_message);
10618 - return [
10619 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'DeepSeek'),
10620 - 'error_code' => 'deepseek_connection_error',
10621 - 'provider' => 'deepseek'
10622 - ];
10623 - }
10624 -
10625 - $status_code = wp_remote_retrieve_response_code($response);
10626 - if ($status_code !== 200) {
10627 - $response_body = wp_remote_retrieve_body($response);
10628 - $decoded_response = json_decode($response_body, true);
10629 -
10630 - $error_message = isset($decoded_response['error']['message'])
10631 - ? $decoded_response['error']['message']
10632 - : 'HTTP Error ' . $status_code;
10633 -
10634 - $error_type = isset($decoded_response['error']['type'])
10635 - ? $decoded_response['error']['type']
10636 - : 'unknown';
10637 -
10638 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
10639 -
10640 - // Handle specific error types
10641 - switch ($status_code) {
10642 - case 401:
10643 - return [
10644 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
10645 - 'error_code' => 'deepseek_auth_error',
10646 - 'provider' => 'deepseek'
10647 - ];
10648 -
10649 - case 400:
10650 - if (strpos($error_message, 'API key') !== false) {
10651 - return [
10652 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
10653 - 'error_code' => 'deepseek_invalid_api_key',
10654 - 'provider' => 'deepseek'
10655 - ];
10656 - }
10657 - break;
10658 -
10659 - case 429:
10660 - if (strpos($error_message, 'quota') !== false) {
10661 - return [
10662 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
10663 - 'error_code' => 'deepseek_quota_exceeded',
10664 - 'provider' => 'deepseek'
10665 - ];
10666 - } else {
10667 - return [
10668 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
10669 - 'error_code' => 'deepseek_rate_limit',
10670 - 'provider' => 'deepseek'
10671 - ];
10672 - }
10673 -
10674 - case 500:
10675 - case 502:
10676 - case 503:
10677 - case 504:
10678 - return [
10679 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
10680 - 'error_code' => 'deepseek_service_unavailable',
10681 - 'provider' => 'deepseek'
10682 - ];
10683 - }
10684 -
10685 - // Generic error fallback — leak-safe helper (see plan 5da59a / 1d3b0f).
10686 - return [
10687 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'DeepSeek'),
10688 - 'error_code' => 'deepseek_api_error',
10689 - 'provider' => 'deepseek',
10690 - 'status_code' => $status_code
10691 - ];
10692 - }
10693 -
10694 - $response_body = wp_remote_retrieve_body($response);
10695 - $decoded_response = json_decode($response_body, true);
10696 -
10697 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10698 - return trim($decoded_response['choices'][0]['message']['content']);
10699 - } else {
10700 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10701 - return [
10702 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
10703 - 'error_code' => 'deepseek_response_format_error',
10704 - 'provider' => 'deepseek'
10705 - ];
10706 - }
10707 - } catch (Exception $e) {
10708 - //error_log('DeepSeek Exception: ' . $e->getMessage());
10709 - return [
10710 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
10711 - 'error_code' => 'deepseek_exception',
10712 - 'provider' => 'deepseek'
10713 - ];
10714 - }
10715 -}
10716 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content, $session_id = '') {
10717 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10718 - // Auto-rescue existing installs whose saved model is the dead ID.
10719 - if ($selected_model === 'gemini-3-pro-preview') {
10720 - $selected_model = 'gemini-3.1-pro-preview';
10721 - }
10722 - // Get bot ID from session or request
10723 - $bot_id = $this->get_current_bot_id($session_id);
10724 -
10725 - // Get system prompt instructions using centralized function
10726 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10727 -
3446 +private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
3447 + // Get system prompt instructions from options
3448 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3449 +
10728 3450 // Add system prompt to relevant content
10729 3451 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10730 3452
10731 3453 // Format messages for Gemini API
@@ -10820,11 +3542,9 @@
10820 3542 ]
10821 3543 ]);
10822 3544
10823 3545 // Prepare the API endpoint
10824 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models
10825 - $api_version = (strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10826 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
3546 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10827 3547
10828 3548 // Set up the API request
10829 3549 $args = [
10830 3550 'body' => $body,
@@ -10838,31 +3558,22 @@
10838 3558 'sslverify' => true,
10839 3559 ];
10840 3560
10841 3561 // Make the API request
10842 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10843 -
3562 + $response = wp_remote_post($api_endpoint, $args);
3563 +
10844 3564 // Process the response
10845 3565 if (is_wp_error($response)) {
10846 - // plan b13282: route the transport-error string through the leak-safe helper
10847 - // (admin-actionable, generic for visitors) instead of echoing the raw WP HTTP
10848 - // error. http_code 0 = no HTTP response, so the helper uses the generic branch.
10849 - return $this->mxchat_friendly_chat_error(0, $response->get_error_message(), 'Gemini');
3566 + return "Sorry, there was an error processing your request: " . $response->get_error_message();
10850 3567 }
10851 3568
10852 3569 $response_body = json_decode(wp_remote_retrieve_body($response), true);
10853 3570
10854 - // Handle potential errors in the response. Gemini surfaces errors as a
10855 - // 200/non-200 body with an `error` envelope; route the user-facing text
10856 - // through the leak-safe helper (admin-actionable, no visitor leak) rather
10857 - // than echoing the raw provider message. plan 5da59a.
3571 + // Handle potential errors in the response
10858 3572 if (isset($response_body['error'])) {
10859 - //error_log('Gemini API Error: ' . json_encode($response_body['error']));
10860 - $gemini_error_message = isset($response_body['error']['message'])
10861 - ? $response_body['error']['message']
10862 - : 'Unknown error';
10863 - $gemini_http_code = wp_remote_retrieve_response_code($response);
10864 - return $this->mxchat_friendly_chat_error($gemini_http_code, $gemini_error_message, 'Gemini');
3573 + error_log('Gemini API Error: ' . json_encode($response_body['error']));
3574 + return "Sorry, there was an error with the Gemini API: " .
3575 + (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
10865 3576 }
10866 3577
10867 3578 // Extract the response text
10868 3579 if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
@@ -10867,145 +3578,15 @@
10867 3578 // Extract the response text
10868 3579 if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
10869 3580 return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
10870 3581 } else {
10871 - //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
3582 + error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
10872 3583 return "Sorry, I couldn't process that request. The response format was unexpected.";
10873 3584 }
10874 3585 }
10875 3586
10876 3587
10877 -public function test_streaming_request() {
10878 - $options = get_option('mxchat_options', []);
10879 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
10880 3588
10881 - // Detect provider from model prefix
10882 - $provider = strtolower(explode('-', $model)[0]);
10883 -
10884 - $sample_prompt = 'Hello! Can you stream this response back to me?';
10885 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
10886 - $headers = [];
10887 - $body = [];
10888 - $url = '';
10889 - $api_key = '';
10890 -
10891 - switch ($provider) {
10892 - case 'gpt':
10893 - case 'o1':
10894 - $api_key = $options['api_key'] ?? '';
10895 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
10896 - $url = 'https://api.openai.com/v1/chat/completions';
10897 - $headers = [
10898 - 'Content-Type: application/json',
10899 - 'Authorization: Bearer ' . $api_key
10900 - ];
10901 - $body = [
10902 - 'model' => $model,
10903 - 'messages' => $messages,
10904 - 'stream' => true
10905 - ];
10906 - break;
10907 -
10908 - case 'claude':
10909 - $api_key = $options['claude_api_key'] ?? '';
10910 - if (empty($api_key)) return '❌ Missing API key for Claude';
10911 - $url = 'https://api.anthropic.com/v1/messages';
10912 - $headers = [
10913 - 'Content-Type: application/json',
10914 - 'x-api-key: ' . $api_key,
10915 - 'anthropic-version: 2023-06-01'
10916 - ];
10917 - $body = [
10918 - 'model' => $model,
10919 - 'messages' => $messages,
10920 - 'max_tokens' => 100,
10921 - 'stream' => true
10922 - ];
10923 - break;
10924 -
10925 - case 'grok':
10926 - $api_key = $options['xai_api_key'] ?? '';
10927 - if (empty($api_key)) return '❌ Missing API key for X.AI';
10928 - $url = 'https://api.x.ai/v1/chat/completions';
10929 - $headers = [
10930 - 'Content-Type: application/json',
10931 - 'Authorization: Bearer ' . $api_key
10932 - ];
10933 - $body = [
10934 - 'model' => $model,
10935 - 'messages' => $messages,
10936 - 'stream' => true
10937 - ];
10938 - break;
10939 -
10940 - case 'deepseek':
10941 - if (empty($deepseek_api_key)) {
10942 - $error_response = [
10943 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
10944 - 'error_code' => 'missing_deepseek_api_key'
10945 - ];
10946 - if ($testing_data !== null) {
10947 - $error_response['testing_data'] = $testing_data;
10948 - }
10949 - return $error_response;
10950 - }
10951 - if ($streaming) {
10952 - return $this->mxchat_generate_response_deepseek_stream(
10953 - $selected_model,
10954 - $deepseek_api_key,
10955 - $conversation_history,
10956 - $relevant_content,
10957 - $session_id,
10958 - $testing_data // Pass testing data
10959 - );
10960 - } else {
10961 - $response = $this->mxchat_generate_response_deepseek(
10962 - $selected_model,
10963 - $deepseek_api_key,
10964 - $conversation_history,
10965 - $relevant_content,
10966 - $session_id
10967 - );
10968 - }
10969 - break;
10970 -
10971 - case 'gemini':
10972 - $api_key = $options['gemini_api_key'] ?? '';
10973 - if (empty($api_key)) return '❌ Missing API key for Gemini';
10974 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
10975 - $headers = ['Content-Type: application/json'];
10976 - $body = [
10977 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
10978 - 'generationConfig' => ['temperature' => 0.7]
10979 - ];
10980 - break;
10981 -
10982 - default:
10983 - return '❌ Unsupported provider: ' . $provider;
10984 - }
10985 -
10986 - // Do the actual streaming test
10987 - $ch = curl_init($url);
10988 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
10989 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
10990 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
10991 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
10992 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
10993 -
10994 - $response = curl_exec($ch);
10995 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
10996 - $error = curl_error($ch);
10997 - curl_close($ch);
10998 -
10999 - if ($error) return "❌ cURL error: $error";
11000 - if ($http_code !== 200) {
11001 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
11002 - return "❌ HTTP $http_code: $error_message";
11003 - }
11004 -
11005 - return true;
11006 -}
11007 -
11008 3589 public function mxchat_dismiss_pre_chat_message() {
11009 3590 // Get and sanitize the user identifier
11010 3591 $user_id = $this->mxchat_get_user_identifier();
11011 3592 $user_id = sanitize_key($user_id);
@@ -11026,9 +3607,9 @@
11026 3607 $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
11027 3608 $dismissed = get_transient($transient_key);
11028 3609
11029 3610 // Log the result to see if it's being set correctly
11030 - //error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
3611 + error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
11031 3612
11032 3613 if ($dismissed) {
11033 3614 wp_send_json_success(['dismissed' => true]);
11034 3615 } else {
@@ -11059,63 +3640,40 @@
11059 3640
11060 3641 return $dotProduct / ($normA * $normB);
11061 3642 }
11062 3643
3644 +public function mxchat_enqueue_scripts_styles() {
3645 + // Define version numbers for the styles and scripts
3646 + $chat_style_version = '2.1.4'; // Replace with your actual version
3647 + $chat_script_version = '2.1.4'; // Replace with your actual version
11063 3648
11064 -public function mxchat_enqueue_scripts_styles() {
11065 - // Fetch options from the database first to check loading strategy
11066 - $this->options = get_option('mxchat_options');
11067 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
3649 + // Enqueue the script
3650 + wp_enqueue_script(
3651 + 'mxchat-chat-js',
3652 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
3653 + array('jquery'),
3654 + $chat_script_version,
3655 + true
3656 + );
11068 3657
11069 - // Always enqueue CSS immediately
3658 + // Enqueue the CSS
11070 3659 wp_enqueue_style(
11071 3660 'mxchat-chat-css',
11072 3661 plugin_dir_url(__FILE__) . '../css/chat-style.css',
11073 3662 array(),
11074 - MXCHAT_VERSION
3663 + $chat_style_version
11075 3664 );
11076 3665
11077 - // Handle script loading based on strategy
11078 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11079 - // Enqueue the script normally
11080 - wp_enqueue_script(
11081 - 'mxchat-chat-js',
11082 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
11083 - array('jquery'),
11084 - MXCHAT_VERSION,
11085 - true
11086 - );
11087 -
11088 - // Add defer attribute if strategy is 'defer'
11089 - if ($loading_strategy === 'defer') {
11090 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
11091 - }
11092 - } else {
11093 - // For delay or interaction-based loading, we'll use a custom loader
11094 - // Don't enqueue the main script - we'll load it dynamically
11095 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
11096 - }
11097 -
3666 + // Fetch options from the database
3667 + $this->options = get_option('mxchat_options');
11098 3668 $prompts_options = get_option('mxchat_prompts_options', array());
11099 3669
11100 - // Check if AI theme is active - if so, skip inline colors in JavaScript
11101 - $theme_options = get_option('mxchat_theme_options', array());
11102 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11103 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11104 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11105 -
11106 3670 // Prepare settings for JavaScript
11107 3671 $style_settings = array(
11108 3672 'ajax_url' => admin_url('admin-ajax.php'),
11109 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
11110 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
11111 - // as a one-shot fallback for the first interaction on a fresh page load
11112 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
11113 - // but the widget refetches before each subsequent send.
11114 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11115 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11116 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
3673 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
11117 3674 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
3675 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
11118 3676 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11119 3677 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11120 3678 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11121 3679 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -11127,1329 +3685,84 @@
11127 3685 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11128 3686 'icon_color' => $this->options['icon_color'] ?? '#fff',
11129 3687 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11130 3688 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11131 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
3689 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off', // Example for consistency
3690 +
11132 3691 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11133 3692 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
3693 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
11134 3694 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11135 3695 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11136 3696 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11137 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11138 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
11139 - 'initial_email_state' => null, // Also fixed this undefined variable
11140 - 'skip_email_check' => true,
11141 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11142 - 'skip_inline_colors' => $skip_inline_colors,
11143 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11144 - );
11145 3697
11146 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11147 - // print/transcript, satisfaction rating) come from the shared
11148 - // dynamic-settings method so this inline payload and the first-open
11149 - // refresh endpoint can never drift (plan-32db95).
11150 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11151 -
11152 - // For normal/defer loading, use wp_localize_script
11153 - // For delayed loading, we store settings in a transient to be output inline
11154 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11155 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11156 - } else {
11157 - // Store settings for the delayed loader to use
11158 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
11159 - }
11160 -}
11161 -
11162 -/**
11163 - * Output the delayed script loader for performance optimization
11164 - */
11165 -public function mxchat_output_delayed_script_loader() {
11166 - $this->options = get_option('mxchat_options');
11167 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11168 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
11169 -
11170 - // Get the stored settings
11171 - $prompts_options = get_option('mxchat_prompts_options', array());
11172 - $theme_options = get_option('mxchat_theme_options', array());
11173 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11174 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11175 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11176 -
11177 - $style_settings = array(
11178 - 'ajax_url' => admin_url('admin-ajax.php'),
11179 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
11180 - // before each send. This inline value is a one-shot fallback for the first interaction.
11181 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11182 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11183 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11184 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11185 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11186 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11187 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11188 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11189 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11190 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11191 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11192 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11193 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11194 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11195 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11196 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11197 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11198 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11199 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11200 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11201 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11202 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11203 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11204 3698 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11205 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
11206 - 'initial_email_state' => null,
11207 - 'skip_email_check' => true,
11208 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11209 - 'skip_inline_colors' => $skip_inline_colors,
11210 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
3699 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
11211 3700 );
11212 3701
11213 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11214 - // print/transcript, satisfaction rating) come from the shared
11215 - // dynamic-settings method so this inline payload and the first-open
11216 - // refresh endpoint can never drift (plan-32db95).
11217 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11218 -
11219 - // Determine delay time based on strategy
11220 - $delay_ms = 0;
11221 - switch ($loading_strategy) {
11222 - case 'delay_1s':
11223 - $delay_ms = 1000;
11224 - break;
11225 - case 'delay_3s':
11226 - $delay_ms = 3000;
11227 - break;
11228 - case 'delay_5s':
11229 - $delay_ms = 5000;
11230 - break;
11231 - }
11232 -
11233 - ?>
11234 - <script type="text/javascript">
11235 - (function() {
11236 - var mxchatLoaded = false;
11237 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
11238 - window.mxchatChat = mxchatChat;
11239 -
11240 - function loadMxChatScript() {
11241 - if (mxchatLoaded) return;
11242 - mxchatLoaded = true;
11243 -
11244 - function appendChatScript() {
11245 - var script = document.createElement('script');
11246 - script.src = <?php echo wp_json_encode($script_url); ?>;
11247 - script.type = 'text/javascript';
11248 - document.body.appendChild(script);
11249 - }
11250 -
11251 - if (typeof jQuery !== 'undefined') {
11252 - appendChatScript();
11253 - } else {
11254 - var jq = document.createElement('script');
11255 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
11256 - jq.onload = appendChatScript;
11257 - document.body.appendChild(jq);
11258 - }
11259 - }
11260 -
11261 - <?php if ($loading_strategy === 'on_interaction'): ?>
11262 - // Load on user interaction
11263 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
11264 - events.forEach(function(evt) {
11265 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
11266 - });
11267 - // Fallback: load after 8 seconds if no interaction
11268 - setTimeout(loadMxChatScript, 8000);
11269 - <?php else: ?>
11270 - // Load after specified delay
11271 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
11272 - <?php endif; ?>
11273 - })();
11274 - </script>
11275 - <?php
3702 + // Pass the settings to the script
3703 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11276 3704 }
11277 3705
11278 -/**
11279 - * Setup the cron jobs for rate limits with guard against multiple calls
11280 - */
11281 -public function setup_rate_limit_cron_jobs() {
11282 - // Add a guard to prevent multiple rapid calls
11283 - $last_setup = get_transient('mxchat_cron_setup_guard');
11284 - if ($last_setup && (time() - $last_setup) < 60) {
11285 - // Don't run again if we ran less than 60 seconds ago
11286 - return;
11287 - }
11288 -
11289 - // Set the guard
11290 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
11291 -
11292 - try {
11293 - // First, check if WordPress cron is disabled
11294 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
11295 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
11296 - $this->setup_fallback_rate_limit_system();
11297 - return;
11298 - }
11299 -
11300 - // Check if cron is already scheduled - if so, don't mess with it
11301 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
11302 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
11303 - return;
11304 - }
11305 -
11306 - // Clear any orphaned hooks (but don't loop indefinitely)
11307 - $hooks_to_clear = [
11308 - 'mxchat_reset_rate_limits',
11309 - 'mxchat_reset_hourly_rate_limits',
11310 - 'mxchat_reset_daily_rate_limits',
11311 - 'mxchat_reset_weekly_rate_limits',
11312 - 'mxchat_reset_monthly_rate_limits'
11313 - ];
11314 -
11315 - foreach ($hooks_to_clear as $hook) {
11316 - // Only clear a maximum of 3 instances to prevent infinite loops
11317 - $cleared = 0;
11318 - while (wp_next_scheduled($hook) && $cleared < 3) {
11319 - wp_clear_scheduled_hook($hook);
11320 - $cleared++;
11321 - }
11322 - }
11323 -
11324 - // Small delay after clearing
11325 - usleep(100000); // 0.1 seconds
11326 -
11327 - // Try to schedule the event
11328 - $initial_time = time() + 300; // Start in 5 minutes
11329 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
11330 -
11331 - if ($result === false) {
11332 - //error_log('MxChat: Failed to schedule cron, using fallback system');
11333 - $this->setup_fallback_rate_limit_system();
11334 - } else {
11335 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
11336 - }
11337 -
11338 - } catch (Exception $e) {
11339 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
11340 - $this->setup_fallback_rate_limit_system();
11341 - }
11342 -}
11343 3706
11344 -/**
11345 - * Try alternative cron scheduling methods
11346 - */
11347 -private function try_alternative_cron_scheduling($initial_time) {
11348 - try {
11349 - // Method 1: Try with current time instead of future time
11350 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
11351 - if ($result1 !== false) {
11352 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
11353 - return true;
11354 - }
11355 -
11356 - // Method 2: Try with a different interval
11357 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
11358 - if ($result2 !== false) {
11359 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
11360 - return true;
11361 - }
11362 -
11363 - // Method 3: Try wp_schedule_single_event first, then recurring
11364 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
11365 - if ($result3 !== false) {
11366 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
11367 - // Schedule the next one manually in the handler
11368 - return true;
11369 - }
11370 -
11371 - return false;
11372 -
11373 - } catch (Exception $e) {
11374 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
11375 - return false;
11376 - }
11377 -}
3707 +public function mxchat_reset_rate_limits() {
3708 + global $wpdb;
11378 3709
11379 -/**
11380 - * Enhanced fallback rate limit system
11381 - */
11382 -private function setup_fallback_rate_limit_system() {
11383 - // Set a flag to use database-based rate limit cleanup
11384 - update_option('mxchat_use_fallback_rate_limits', true);
11385 -
11386 - // Schedule a one-time check to happen on the next plugin load
11387 - update_option('mxchat_next_rate_limit_check', time() + 3600);
11388 -
11389 - // Also set up a more frequent fallback check (every 4 hours)
11390 - update_option('mxchat_fallback_check_interval', 4 * 3600);
11391 -
11392 - //error_log('MxChat: Fallback rate limit system activated');
11393 -}
3710 + // Define a cache key pattern for rate limits
3711 + $cache_key_pattern = 'mxchat_chat_limit_%';
11394 3712
11395 -/**
11396 - * Enhanced fallback check method
11397 - */
11398 -public function check_fallback_rate_limits() {
11399 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11400 -
11401 - if (!$use_fallback) {
11402 - return; // Regular cron is working
11403 - }
11404 -
11405 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11406 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
11407 -
11408 - if (time() >= $next_check) {
11409 - //error_log('MxChat: Running fallback rate limit cleanup');
11410 - $this->mxchat_reset_rate_limits();
11411 -
11412 - // Schedule next check
11413 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
11414 - }
11415 -}
11416 -/**
11417 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
11418 - */
11419 -public function check_rate_limit() {
11420 - // Check if we need to run fallback cleanup
11421 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11422 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11423 -
11424 - if ($use_fallback && time() >= $next_check) {
11425 - $this->mxchat_reset_rate_limits();
11426 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
11427 - }
11428 -
11429 - // Get bot ID from current request context
11430 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11431 -
11432 - // Get bot-specific options (includes rate limits if overridden)
11433 - $bot_options = $this->get_bot_options($bot_id);
11434 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
11435 -
11436 - // Use bot-specific rate limits if available, otherwise fall back to default
11437 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
3713 + // Retrieve all option names matching the pattern
3714 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
3715 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
11438 3716
11439 - // -------------------------------------------------------------------
11440 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
11441 - // it acts as a hard ceiling across all users + all roles. Default is
11442 - // 'unlimited' so existing installs are unchanged. Counter key drops
11443 - // both <role> and <user_id> segments — single pool per bot.
11444 - // -------------------------------------------------------------------
11445 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
11446 - ? $current_options['rate_limits_global']
11447 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
11448 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
11449 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
11450 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
11451 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11452 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
11453 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
11454 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
11455 - if ((int) $global_data['count'] === 0) {
11456 - $global_data['timestamp'] = time();
11457 - update_option($global_option, $global_data);
11458 - }
11459 - $now = time();
11460 - $ts = (int) $global_data['timestamp'];
11461 - $reset = false;
11462 - switch ($global_timeframe) {
11463 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
11464 - case 'daily': $reset = ($now - $ts) >= 86400; break;
11465 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
11466 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
11467 - }
11468 - if ($reset) {
11469 - $global_data = ['count' => 0, 'timestamp' => $now];
11470 - update_option($global_option, $global_data);
11471 - }
11472 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
11473 - $global_msg = !empty($global_cfg['message'])
11474 - ? $global_cfg['message']
11475 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
11476 - return [
11477 - 'error' => true,
11478 - 'message' => $this->process_rate_limit_message_html($global_msg),
11479 - ];
11480 - }
11481 - // Reserve the slot for this request. Per-role check below also increments
11482 - // its own counter — that is intentional, both ceilings apply independently.
11483 - $global_data['count']++;
11484 - update_option($global_option, $global_data);
11485 - }
3717 + // db call ok; no-cache ok
3718 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- db call ok
3719 + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
11486 3720
11487 - // Determine user role or if logged out
11488 - if (is_user_logged_in()) {
11489 - $user = wp_get_current_user();
11490 - $user_id = $user->ID;
11491 -
11492 - // Get the user's primary role using reset() to safely get the first element
11493 - $user_roles = $user->roles;
11494 -
11495 - // Safely get the first role regardless of array key structure
11496 - if (!empty($user_roles) && is_array($user_roles)) {
11497 - $role = reset($user_roles); // This safely gets the first element regardless of key
11498 - } else {
11499 - $role = 'subscriber'; // Default to subscriber if no role found
3721 + // Clear the relevant cache entries
3722 + foreach ($option_names as $option_name) {
3723 + wp_cache_delete($option_name, 'options');
11500 3724 }
11501 - } else {
11502 - $role = 'logged_out';
11503 - // Use IP address for non-logged-in users
11504 - $user_id = $this->get_client_ip();
11505 - }
11506 -
11507 - // Check if rate limits are configured for this role
11508 - if (!isset($rate_limits_source[$role])) {
11509 - return true; // No limit set for this role
11510 - }
11511 -
11512 - $limit = $rate_limits_source[$role]['limit'];
11513 -
11514 - // If unlimited, return true immediately
11515 - if ($limit === 'unlimited') {
11516 - return true;
11517 - }
11518 -
11519 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
11520 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
11521 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
11522 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
11523 -
11524 - // Include bot_id in option name so each bot has separate rate limits
11525 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
11526 -
11527 - // Get the counter data
11528 - $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
11529 -
11530 - // If first request or counter reset needed, set the initial timestamp
11531 - if ($limit_data['count'] === 0) {
11532 - $limit_data['timestamp'] = time();
11533 - update_option($option_name, $limit_data);
11534 - }
11535 -
11536 - // Get the timeframe
11537 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
11538 - $rate_limits_source[$role]['timeframe'] : 'daily';
11539 -
11540 - // Check if the counter needs to be reset based on timeframe
11541 - $current_time = time();
11542 - $timestamp = $limit_data['timestamp'];
11543 - $should_reset = false;
11544 -
11545 - switch ($timeframe) {
11546 - case 'hourly':
11547 - $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
11548 - break;
11549 - case 'daily':
11550 - $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
11551 - break;
11552 - case 'weekly':
11553 - $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
11554 - break;
11555 - case 'monthly':
11556 - $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
11557 - break;
11558 - }
11559 -
11560 - // Reset the counter if the timeframe has passed
11561 - if ($should_reset) {
11562 - $limit_data = ['count' => 0, 'timestamp' => $current_time];
11563 - update_option($option_name, $limit_data);
11564 - }
11565 -
11566 - // Check if user has exceeded their limit
11567 - if ($limit_data['count'] >= intval($limit)) {
11568 - // Get the custom message for this role
11569 - $message = !empty($rate_limits_source[$role]['message'])
11570 - ? $rate_limits_source[$role]['message']
11571 - : __('Rate limit exceeded. Please try again later.', 'mxchat');
11572 -
11573 - // Add timeframe information to the message if placeholders exist
11574 - $timeframe_label = '';
11575 - switch ($timeframe) {
11576 - case 'hourly':
11577 - $timeframe_label = __('hour', 'mxchat');
11578 - break;
11579 - case 'daily':
11580 - $timeframe_label = __('day', 'mxchat');
11581 - break;
11582 - case 'weekly':
11583 - $timeframe_label = __('week', 'mxchat');
11584 - break;
11585 - case 'monthly':
11586 - $timeframe_label = __('month', 'mxchat');
11587 - break;
11588 - }
11589 -
11590 - // Replace placeholders in the message
11591 - $message = str_replace(
11592 - ['{limit}', '{count}', '{remaining}', '{timeframe}'],
11593 - [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
11594 - $message
11595 - );
11596 -
11597 - // Process HTML links in the message
11598 - $message = $this->process_rate_limit_message_html($message);
11599 -
11600 - // Return error with the processed message
11601 - return [
11602 - 'error' => true,
11603 - 'message' => $message
11604 - ];
11605 - }
11606 -
11607 - // Increment the counter
11608 - $limit_data['count']++;
11609 - update_option($option_name, $limit_data);
11610 -
11611 - return true;
11612 -}
11613 3725
11614 -/**
11615 - * Enhanced rate limit reset with better error handling
11616 - */
11617 -public function mxchat_reset_rate_limits() {
11618 - try {
11619 - global $wpdb;
11620 - $all_options = get_option('mxchat_options', []);
11621 - $current_time = time();
11622 -
11623 - // Get rate limit options with a safer query and limit
11624 - $option_names = $wpdb->get_col(
11625 - $wpdb->prepare(
11626 - "SELECT option_name FROM {$wpdb->options}
11627 - WHERE option_name LIKE %s
11628 - LIMIT 1000",
11629 - 'mxchat_chat_limit_%'
11630 - )
11631 - );
11632 -
11633 - if (empty($option_names)) {
11634 - return;
11635 - }
11636 -
11637 - $processed_count = 0;
11638 - $max_processing_time = 30; // Maximum 30 seconds
11639 - $start_time = time();
11640 -
11641 - foreach ($option_names as $option_name) {
11642 - // Check processing time limit
11643 - if ((time() - $start_time) > $max_processing_time) {
11644 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
11645 - break;
11646 - }
11647 -
11648 - // Parse the option name more safely
11649 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
11650 - continue;
11651 - }
11652 -
11653 - $role_and_user = $matches[1] . '_' . $matches[2];
11654 - $parts = explode('_', $role_and_user);
11655 -
11656 - if (count($parts) < 2) {
11657 - continue;
11658 - }
11659 -
11660 - // Extract role (everything except the last part which is user ID)
11661 - $user_id_part = array_pop($parts);
11662 - $role = implode('_', $parts);
11663 -
11664 - // Skip if role doesn't exist in our settings
11665 - if (!isset($all_options['rate_limits'][$role])) {
11666 - // Clean up orphaned entries
11667 - delete_option($option_name);
11668 - continue;
11669 - }
11670 -
11671 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
11672 - $limit_data = get_option($option_name);
11673 -
11674 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
11675 - // Clean up invalid entries
11676 - delete_option($option_name);
11677 - continue;
11678 - }
11679 -
11680 - $timestamp = $limit_data['timestamp'];
11681 - $should_reset = false;
11682 -
11683 - // Determine if we should reset based on the timeframe
11684 - switch ($timeframe) {
11685 - case 'hourly':
11686 - $should_reset = ($current_time - $timestamp) >= 3600;
11687 - break;
11688 - case 'daily':
11689 - $should_reset = ($current_time - $timestamp) >= 86400;
11690 - break;
11691 - case 'weekly':
11692 - $should_reset = ($current_time - $timestamp) >= 604800;
11693 - break;
11694 - case 'monthly':
11695 - $should_reset = ($current_time - $timestamp) >= 2592000;
11696 - break;
11697 - }
11698 -
11699 - // Reset the counter if the timeframe has passed
11700 - if ($should_reset) {
11701 - delete_option($option_name);
11702 - wp_cache_delete($option_name, 'options');
11703 - $processed_count++;
11704 - }
11705 - }
11706 -
11707 - // Clean up any orphaned cache entries
3726 + // Optionally, clear a general cache if you have one
11708 3727 wp_cache_delete('mxchat_all_chat_limits', 'options');
11709 -
11710 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11711 -
11712 - } catch (Exception $e) {
11713 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11714 3728 }
11715 -}
11716 3729
11717 -
11718 -/**
11719 - * Process HTML links in rate limit messages
11720 - *
11721 - * @param string $message The rate limit message
11722 - * @return string The processed message with safe HTML links
11723 - */
11724 -private function process_rate_limit_message_html($message) {
11725 - // Return original message if empty
11726 - if (empty($message)) {
11727 - return $message;
3730 +private function mxchat_fetch_woocommerce_products() {
3731 + // Ensure WooCommerce is active
3732 + if (!class_exists('WooCommerce')) {
3733 + return [];
11728 3734 }
11729 -
11730 - // First, convert markdown links to HTML
11731 - $message = $this->convert_markdown_links($message);
11732 -
11733 - // Then, auto-convert any remaining plain URLs to links
11734 - $message = $this->auto_link_urls($message);
11735 -
11736 - // Allow basic HTML tags for links and formatting
11737 - $allowed_tags = [
11738 - 'a' => [
11739 - 'href' => true,
11740 - 'target' => true,
11741 - 'rel' => true,
11742 - 'title' => true,
11743 - 'class' => true
11744 - ],
11745 - 'strong' => [],
11746 - 'em' => [],
11747 - 'br' => [],
11748 - 'b' => [],
11749 - 'i' => [],
11750 - 'span' => ['class' => true]
11751 - ];
11752 -
11753 - // Sanitize but allow the specified HTML tags
11754 - $processed_message = wp_kses($message, $allowed_tags);
11755 -
11756 - // If wp_kses stripped everything, return the original message as plain text
11757 - if (empty($processed_message) && !empty($message)) {
11758 - // Strip all HTML and return plain text as fallback
11759 - return wp_strip_all_tags($message);
11760 - }
11761 -
11762 - return $processed_message;
11763 -}
11764 3735
11765 -/**
11766 - * Convert markdown links to HTML
11767 - *
11768 - * @param string $text The text to process
11769 - * @return string The text with markdown links converted to HTML
11770 - */
11771 -private function convert_markdown_links($text) {
11772 - // Return original text if empty
11773 - if (empty($text)) {
11774 - return $text;
11775 - }
11776 -
11777 - // Pattern to match markdown links: [text](url)
11778 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
11779 -
11780 - $processed_text = preg_replace_callback($pattern, function($matches) {
11781 - $link_text = $matches[1];
11782 - $url = $matches[2];
11783 -
11784 - // Clean up any trailing punctuation from the URL
11785 - $url = rtrim($url, '.,;:!?');
11786 -
11787 - // Sanitize the link text and URL
11788 - $safe_text = esc_html($link_text);
11789 - $safe_url = esc_url($url);
11790 -
11791 - // Create the HTML link
11792 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
11793 - }, $text);
11794 -
11795 - // If preg_replace_callback failed, return original text
11796 - if ($processed_text === null) {
11797 - return $text;
11798 - }
11799 -
11800 - return $processed_text;
11801 -}
3736 + $args = array(
3737 + 'post_type' => 'product',
3738 + 'post_status' => 'publish',
3739 + 'posts_per_page' => -1,
3740 + );
11802 3741
11803 -/**
11804 - * Auto-convert plain URLs to clickable links
11805 - *
11806 - * @param string $text The text to process
11807 - * @return string The text with URLs converted to links
11808 - */
11809 -private function auto_link_urls($text) {
11810 - // Return original text if empty
11811 - if (empty($text)) {
11812 - return $text;
11813 - }
11814 -
11815 - // Simple pattern that avoids complex lookbehinds
11816 - // This will match URLs that are not already inside href attributes or markdown links
11817 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
11818 -
11819 - $processed_text = preg_replace_callback($pattern, function($matches) {
11820 - $url = $matches[0];
11821 - // Clean up any trailing punctuation that might have been captured
11822 - $url = rtrim($url, '.,;:!?');
11823 -
11824 - // Add target="_blank" and rel="noopener noreferrer" for security
11825 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
11826 - }, $text);
11827 -
11828 - // If preg_replace_callback failed, return original text
11829 - if ($processed_text === null) {
11830 - return $text;
11831 - }
11832 -
11833 - return $processed_text;
11834 -}
3742 + $products = get_posts($args);
3743 + $product_data = [];
11835 3744
3745 + foreach ($products as $product) {
3746 + $product_id = $product->ID;
3747 + $product_obj = wc_get_product($product_id);
11836 3748
11837 -// Helper function to get client IP address
11838 -private function get_client_ip() {
11839 - // Check for shared internet/ISP IP
11840 - if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
11841 - return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
3749 + $product_data[] = array(
3750 + 'id' => $product_id,
3751 + 'name' => $product_obj->get_name(),
3752 + 'description' => $product_obj->get_description(),
3753 + 'short_description' => $product_obj->get_short_description(),
3754 + 'url' => get_permalink($product_id),
3755 + 'price' => $product_obj->get_regular_price(),
3756 + 'sale_price' => $product_obj->get_sale_price(),
3757 + 'stock_status' => $product_obj->get_stock_status(),
3758 + 'sku' => $product_obj->get_sku(),
3759 + 'in_stock' => $product_obj->is_in_stock(),
3760 + 'total_sales' => $product_obj->get_total_sales(),
3761 + );
11842 3762 }
11843 -
11844 - // Check for IPs passing through proxies
11845 - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
11846 - // Use the first value in the comma-separated list
11847 - $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
11848 - return trim($forwarded_for[0]);
11849 - }
11850 -
11851 - if (!empty($_SERVER['REMOTE_ADDR'])) {
11852 - return sanitize_text_field($_SERVER['REMOTE_ADDR']);
11853 - }
11854 -
11855 - // Fallback
11856 - return 'unknown';
11857 -}
11858 3763
11859 -/**
11860 - * AJAX handler to get system information for testing panel
11861 - */
11862 -/**
11863 - * AJAX handler to get system information for testing panel
11864 - */
11865 -public function mxchat_get_system_info() {
11866 - // Verify nonce for security
11867 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11868 - wp_send_json_error(['message' => 'Invalid nonce']);
11869 - return;
11870 - }
11871 -
11872 - // Only allow admin users
11873 - if (!current_user_can('administrator')) {
11874 - wp_send_json_error(['message' => 'Unauthorized']);
11875 - return;
11876 - }
11877 -
11878 - // Get system prompt from options
11879 - $system_prompt = isset($this->options['system_prompt_instructions'])
11880 - ? $this->options['system_prompt_instructions']
11881 - : 'No system prompt configured';
11882 -
11883 - // Get selected model
11884 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
11885 -
11886 - // Check if OpenRouter is being used
11887 - $is_openrouter = ($selected_model === 'openrouter');
11888 - $openrouter_model = '';
11889 -
11890 - if ($is_openrouter) {
11891 - // Get the actual OpenRouter model that's selected
11892 - $openrouter_model = isset($this->options['openrouter_selected_model'])
11893 - ? $this->options['openrouter_selected_model']
11894 - : 'No OpenRouter model selected';
11895 -
11896 - // Update selected_model display to show both
11897 - $selected_model = 'OpenRouter: ' . $openrouter_model;
11898 - }
11899 -
11900 - // Get API key status (just check if they exist, don't expose the keys)
11901 - $api_status = [];
11902 - $api_status['openai'] = !empty($this->options['api_key']);
11903 - $api_status['claude'] = !empty($this->options['claude_api_key']);
11904 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
11905 - $api_status['xai'] = !empty($this->options['xai_api_key']);
11906 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
11907 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
11908 -
11909 - wp_send_json_success([
11910 - 'system_prompt' => $system_prompt,
11911 - 'selected_model' => $selected_model,
11912 - 'is_openrouter' => $is_openrouter,
11913 - 'openrouter_model' => $openrouter_model,
11914 - 'api_status' => $api_status
11915 - ]);
3764 + return $product_data;
11916 3765 }
11917 -
11918 -/**
11919 - * AJAX handler to get similarity threshold
11920 - */
11921 -public function mxchat_get_similarity_threshold() {
11922 - // Verify nonce for security
11923 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11924 - wp_send_json_error(['message' => 'Invalid nonce']);
11925 - return;
11926 - }
11927 -
11928 - // Only allow admin users
11929 - if (!current_user_can('administrator')) {
11930 - wp_send_json_error(['message' => 'Unauthorized']);
11931 - return;
11932 - }
11933 -
11934 - // Get similarity threshold from main options (default 35%)
11935 - $similarity_threshold = isset($this->options['similarity_threshold'])
11936 - ? ((int) $this->options['similarity_threshold']) / 100
11937 - : 0.35;
11938 -
11939 - wp_send_json_success([
11940 - 'threshold' => $similarity_threshold,
11941 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
11942 - ]);
11943 -}
11944 -
11945 -/**
11946 - * AJAX handler to get knowledge base status
11947 - */
11948 -public function mxchat_get_kb_status() {
11949 - // Verify nonce for security
11950 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
11951 - wp_send_json_error(['message' => 'Invalid nonce']);
11952 - return;
11953 - }
11954 -
11955 - // Only allow admin users
11956 - if (!current_user_can('administrator')) {
11957 - wp_send_json_error(['message' => 'Unauthorized']);
11958 - return;
11959 - }
11960 -
11961 - // Check OpenAI Vector Store first (takes priority)
11962 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
11963 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
11964 -
11965 - if ($use_vectorstore) {
11966 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
11967 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
11968 -
11969 - $kb_info = [
11970 - 'type' => 'OpenAI Vector Store',
11971 - 'status' => 'Active',
11972 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
11973 - ];
11974 -
11975 - wp_send_json_success($kb_info);
11976 - return;
11977 - }
11978 -
11979 - // Check Pinecone vs WordPress
11980 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
11981 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
11982 -
11983 - $kb_info = [
11984 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
11985 - 'status' => 'Active'
11986 - ];
11987 -
11988 - // Get document count
11989 - if ($use_pinecone) {
11990 - $kb_info['documents'] = 'Connected to Pinecone';
11991 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
11992 - } else {
11993 - // Count documents in WordPress database
11994 - global $wpdb;
11995 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
11996 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
11997 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
11998 - }
11999 -
12000 - wp_send_json_success($kb_info);
12001 -}
12002 -
12003 -/**
12004 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
12005 - */
12006 -public function mxchat_start_fresh_session() {
12007 - // Verify nonce for security
12008 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12009 - wp_send_json_error(['message' => 'Invalid nonce']);
12010 - return;
12011 - }
12012 -
12013 - // Only allow admin users
12014 - if (!current_user_can('administrator')) {
12015 - wp_send_json_error(['message' => 'Unauthorized']);
12016 - return;
12017 - }
12018 -
12019 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
12020 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
12021 -
12022 - if (empty($old_session_id)) {
12023 - wp_send_json_error(['message' => 'Old session ID required']);
12024 - return;
12025 - }
12026 -
12027 - // If no new session ID provided, generate one
12028 - if (empty($new_session_id)) {
12029 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
12030 - }
12031 -
12032 - // Clear ALL data associated with the old session
12033 - $this->clear_complete_session_data($old_session_id);
12034 -
12035 - // Initialize the new session
12036 - $this->initialize_fresh_session($new_session_id);
12037 -
12038 - wp_send_json_success([
12039 - 'message' => 'Fresh session started successfully',
12040 - 'new_session_id' => $new_session_id,
12041 - 'old_session_id' => $old_session_id
12042 - ]);
12043 -}
12044 -
12045 -/**
12046 - * Clear ALL data associated with a session (ENHANCED)
12047 - */
12048 -private function clear_complete_session_data($session_id) {
12049 - // Clear chat history
12050 - delete_option("mxchat_history_{$session_id}");
12051 -
12052 - // Clear chat mode
12053 - delete_option("mxchat_mode_{$session_id}");
12054 -
12055 - // Clear any PDF/Word transients
12056 - $this->clear_pdf_transients($session_id);
12057 - if (method_exists($this, 'clear_word_transients')) {
12058 - $this->clear_word_transients($session_id);
12059 - }
12060 -
12061 - // Clear agent-related data
12062 - delete_option("mxchat_channel_{$session_id}");
12063 - delete_option("mxchat_agent_name_{$session_id}");
12064 - delete_option("mxchat_email_{$session_id}");
12065 -
12066 - // Clear any recommendation flow state
12067 - delete_option("mxchat_sr_flow_state_{$session_id}");
12068 -
12069 - // Clear any cached embeddings or context
12070 - delete_transient("mxchat_context_{$session_id}");
12071 - delete_transient("mxchat_last_query_{$session_id}");
12072 -
12073 - // Clear any testing data
12074 - delete_transient("mxchat_testing_data_{$session_id}");
12075 -
12076 - // Clear any rate limiting data for this session
12077 - delete_transient("mxchat_rate_limit_{$session_id}");
12078 -
12079 - // Clear any other session-specific transients
12080 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
12081 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
12082 - delete_transient("mxchat_include_word_in_context_{$session_id}");
12083 -
12084 - // Clear form addon state (pending forms and submitted forms)
12085 - delete_option("mxchat_pending_form_{$session_id}");
12086 - delete_option("mxchat_submitted_forms_{$session_id}");
12087 -
12088 - //error_log("MxChat: Cleared all data for session: {$session_id}");
12089 -}
12090 -
12091 -/**
12092 - * Initialize a fresh session with default data
12093 - */
12094 -private function initialize_fresh_session($session_id) {
12095 - // Set default chat mode
12096 - update_option("mxchat_mode_{$session_id}", 'ai');
12097 -
12098 - //error_log("MxChat: Initialized fresh session: {$session_id}");
12099 -}
12100 -
12101 -/**
12102 - * Helper method to clear Word document transients (if you have Word support)
12103 - */
12104 -private function clear_word_transients($session_id) {
12105 - delete_transient('mxchat_word_url_' . $session_id);
12106 - delete_transient('mxchat_word_filename_' . $session_id);
12107 - delete_transient('mxchat_word_embeddings_' . $session_id);
12108 - delete_transient('mxchat_include_word_in_context_' . $session_id);
12109 -}
12110 -
12111 -/**
12112 - * Simplified testing data capture method (CLEANED UP)
12113 - */
12114 -private function capture_testing_data($user_embedding, $message, $session_id) {
12115 - // Only capture for admin users
12116 - if (!current_user_can('administrator')) {
12117 - return null;
12118 - }
12119 -
12120 - $testing_data = [
12121 - 'query' => $message,
12122 - 'timestamp' => time(),
12123 - 'top_matches' => [],
12124 - 'action_matches' => [] // Add action matches
12125 - ];
12126 -
12127 - // Get similarity threshold
12128 - $similarity_threshold = isset($this->options['similarity_threshold'])
12129 - ? ((int) $this->options['similarity_threshold']) / 100
12130 - : 0.35;
12131 -
12132 - $testing_data['similarity_threshold'] = $similarity_threshold;
12133 -
12134 - // Use the real similarity analysis if available
12135 - if ($this->last_similarity_analysis !== null) {
12136 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
12137 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
12138 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
12139 - } else {
12140 - // Fallback: determine knowledge base type
12141 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12142 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12143 -
12144 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
12145 - }
12146 -
12147 - // Include action analysis if available
12148 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
12149 - $testing_data['action_matches'] = $this->last_action_analysis;
12150 -
12151 - // Clear it after capturing to avoid stale data
12152 - $this->last_action_analysis = null;
12153 - }
12154 -
12155 - return $testing_data;
12156 -}
12157 -
12158 -
12159 -/**
12160 - * Track URL clicks from chatbot responses
12161 - */
12162 -public function mxchat_track_url_click() {
12163 - // Verify nonce for security
12164 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12165 - wp_send_json_error(['message' => 'Invalid nonce']);
12166 - wp_die();
12167 - }
12168 -
12169 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12170 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
12171 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
12172 -
12173 - if (empty($session_id) || empty($clicked_url)) {
12174 - wp_send_json_error(['message' => 'Missing required data']);
12175 - wp_die();
12176 - }
12177 -
12178 - global $wpdb;
12179 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12180 -
12181 - // Insert click tracking record
12182 - $wpdb->insert(
12183 - $table_name,
12184 - [
12185 - 'session_id' => $session_id,
12186 - 'clicked_url' => $clicked_url,
12187 - 'message_context' => $message_context,
12188 - 'click_timestamp' => current_time('mysql', 1),
12189 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
12190 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
12191 - ]
12192 - );
12193 -
12194 - wp_send_json_success(['message' => 'Click tracked']);
12195 - wp_die();
12196 -}
12197 -
12198 -/**
12199 - * Get URL click analytics for a session
12200 - */
12201 -public function mxchat_get_url_clicks($session_id) {
12202 - global $wpdb;
12203 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12204 -
12205 - $clicks = $wpdb->get_results($wpdb->prepare(
12206 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
12207 - $session_id
12208 - ));
12209 -
12210 - return $clicks;
12211 -}
12212 -/**
12213 - * Track the originating page where chat was started
12214 - */
12215 -public function mxchat_track_originating_page() {
12216 - // Verify nonce
12217 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12218 - wp_send_json_error(['message' => 'Invalid nonce']);
12219 - wp_die();
12220 - }
12221 -
12222 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12223 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
12224 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
12225 -
12226 - if (empty($session_id)) {
12227 - wp_send_json_error(['message' => 'Missing session ID']);
12228 - wp_die();
12229 - }
12230 -
12231 - global $wpdb;
12232 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
12233 -
12234 - // Check if we've already tracked for this session
12235 - $existing = $wpdb->get_var($wpdb->prepare(
12236 - "SELECT COUNT(*) FROM $table_name
12237 - WHERE session_id = %s
12238 - AND originating_page_url IS NOT NULL",
12239 - $session_id
12240 - ));
12241 -
12242 - if ($existing > 0) {
12243 - wp_send_json_success(['message' => 'Already tracked']);
12244 - wp_die();
12245 - }
12246 -
12247 - // Update the first message in this session with originating page info
12248 - $wpdb->query($wpdb->prepare(
12249 - "UPDATE $table_name
12250 - SET originating_page_url = %s,
12251 - originating_page_title = %s
12252 - WHERE session_id = %s
12253 - ORDER BY timestamp ASC
12254 - LIMIT 1",
12255 - $page_url,
12256 - $page_title,
12257 - $session_id
12258 - ));
12259 -
12260 - wp_send_json_success(['message' => 'Originating page tracked']);
12261 - wp_die();
12262 -}
12263 -
12264 -/**
12265 - * Validate and clean URLs from AI response
12266 - * Removes any URLs that aren't in the knowledge base
12267 - *
12268 - * @param string $response_text The AI-generated response
12269 - * @param array $valid_urls Array of URLs from the knowledge base
12270 - * @return string Cleaned response with invalid URLs removed/flagged
12271 - */
12272 -private function validate_and_clean_urls($response_text, $valid_urls) {
12273 - // DEBUG: Log what we're working with
12274 - //error_log("=== MxChat URL Validation Debug ===");
12275 - //error_log("Valid URLs count: " . count($valid_urls));
12276 - //error_log("Valid URLs: " . print_r($valid_urls, true));
12277 - //error_log("Response text length: " . strlen($response_text));
12278 - //error_log("Response text preview: " . substr($response_text, 0, 500));
12279 -
12280 - // If no valid URLs provided or empty response, return as-is
12281 - if (empty($valid_urls) || empty($response_text)) {
12282 - //error_log("Validation skipped - empty valid_urls or response");
12283 - return $response_text;
12284 - }
12285 -
12286 - // Extract all URLs from the AI response
12287 - // This regex matches http:// and https:// URLs
12288 - preg_match_all(
12289 - '#\bhttps?://[^\s<>"\')\]]+#i',
12290 - $response_text,
12291 - $matches
12292 - );
12293 -
12294 - // If no URLs found in response, return as-is
12295 - if (empty($matches[0])) {
12296 - //error_log("No URLs found in response");
12297 - return $response_text;
12298 - }
12299 -
12300 - $found_urls = $matches[0];
12301 - $cleaned_response = $response_text;
12302 - $removed_count = 0;
12303 -
12304 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
12305 - $normalized_valid_urls = array_map(function($url) {
12306 - // Remove trailing slash
12307 - $url = rtrim($url, '/');
12308 - // Remove URL fragments (#section)
12309 - $url = preg_replace('/#.*$/', '', $url);
12310 - // Remove trailing punctuation that might have been captured
12311 - $url = rtrim($url, '.,;:!?');
12312 - return $url;
12313 - }, $valid_urls);
12314 -
12315 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
12316 -
12317 - foreach ($found_urls as $found_url) {
12318 - // Clean up the found URL (remove trailing punctuation that might have been captured)
12319 - $clean_found_url = rtrim($found_url, '.,;:!?)');
12320 -
12321 - // DEBUG: Log each URL being checked
12322 - //error_log("Checking found URL: " . $found_url);
12323 -
12324 - // Normalize for comparison
12325 - $normalized_found = rtrim($clean_found_url, '/');
12326 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
12327 -
12328 - //error_log("Normalized found URL: " . $normalized_found);
12329 -
12330 - // Check if this URL exists in our valid URLs list
12331 - $is_valid = false;
12332 -
12333 - //error_log("Starting validation checks for: " . $normalized_found);
12334 -
12335 - // First, try exact match
12336 - if (in_array($normalized_found, $normalized_valid_urls)) {
12337 - $is_valid = true;
12338 - //error_log("EXACT MATCH FOUND");
12339 - } else {
12340 - //error_log("No exact match, checking variations...");
12341 - // If no exact match, check if it's a variation (with query params, etc.)
12342 - foreach ($normalized_valid_urls as $valid_url) {
12343 - //error_log(" Comparing against valid URL: " . $valid_url);
12344 -
12345 - // Check if the found URL starts with a valid URL (handles query params)
12346 - if (strpos($normalized_found, $valid_url) === 0) {
12347 - // Check what comes after the valid URL
12348 - $remainder = substr($normalized_found, strlen($valid_url));
12349 -
12350 - // Only valid if:
12351 - // 1. Exact match (remainder is empty)
12352 - // 2. Query params (starts with ?)
12353 - // 3. Fragment (starts with #)
12354 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
12355 - $is_valid = true;
12356 - //error_log(" MATCH: Found URL is valid variation of base URL");
12357 - break;
12358 - } else {
12359 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
12360 - }
12361 - }
12362 - // Also check the reverse (in case valid URL has query params)
12363 - if (strpos($valid_url, $normalized_found) === 0) {
12364 - $is_valid = true;
12365 - //error_log(" MATCH: Valid URL starts with found URL");
12366 - break;
12367 - }
12368 - }
12369 -
12370 - if (!$is_valid) {
12371 - //error_log("NO MATCH FOUND - URL should be removed");
12372 - }
12373 - }
12374 -
12375 - // If URL is not valid, remove it from the response
12376 - if (!$is_valid) {
12377 - // Log the removal for debugging
12378 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
12379 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
12380 -
12381 - $removed_count++;
12382 -
12383 - // Check if URL is part of a markdown link: [text](url)
12384 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
12385 - if (preg_match($markdown_pattern, $cleaned_response)) {
12386 - //error_log("Found markdown link, removing but keeping text");
12387 - // Remove the markdown link but keep the text
12388 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
12389 - }
12390 - // Check if URL is part of an HTML link: <a href="url">text</a>
12391 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
12392 - //error_log("Found HTML link, removing but keeping text");
12393 - // Remove the HTML link but keep the text
12394 - $link_text = $link_match[1];
12395 - $cleaned_response = preg_replace(
12396 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
12397 - $link_text,
12398 - $cleaned_response
12399 - );
12400 - }
12401 - // Otherwise just remove the bare URL
12402 - else {
12403 - //error_log("Removing bare URL");
12404 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
12405 - }
12406 - }
12407 - }
12408 -
12409 - // Log summary if any URLs were removed
12410 - if ($removed_count > 0) {
12411 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
12412 - } else {
12413 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
12414 - }
12415 -
12416 - // Clean up any double spaces or awkward punctuation left behind
12417 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
12418 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
12419 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
12420 -
12421 - //error_log("Final cleaned response: " . $cleaned_response);
12422 -
12423 - return trim($cleaned_response);
12424 -}
12425 -
12426 -/**
12427 - * AJAX handler to get current chat mode for a session
12428 - */
12429 -public function mxchat_get_current_chat_mode() {
12430 - // Verify nonce for security
12431 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12432 - wp_send_json_error(['message' => 'Invalid nonce']);
12433 - wp_die();
12434 - }
12435 -
12436 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12437 -
12438 - if (empty($session_id)) {
12439 - wp_send_json_error(['message' => 'Session ID missing']);
12440 - wp_die();
12441 - }
12442 -
12443 - // Get the current chat mode for this session
12444 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
12445 -
12446 - wp_send_json_success([
12447 - 'chat_mode' => $chat_mode
12448 - ]);
12449 - wp_die();
12450 -}
12451 -
12452 -
12453 3766
12454 3767 }
12455 3768 ?>