PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.1.7
MxChat – AI Chatbot & Content Generation for WordPress v2.1.7
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 +1605 -10045 3.2.142.1.7 View file →
@@ -8,263 +8,28 @@
8 8 private $prompts_options;
9 9 private $chat_count;
10 10 private $fallbackResponse;
11 11 private $productCardHtml;
12 - // plan-mxchat-20260717-03ba33 — consent-safe YouTube embed queued during RAG
13 - // retrieval when a video-backed KB entry is used as context. Emitted on the
14 - // response 'html' channel alongside productCardHtml (non-streaming path,
15 - // same constraint as product cards).
16 - private $videoEmbedHtml = '';
17 - // plan-mxchat-20260617-48a57a — function-calling UI payload capture. When a
18 - // model-invoked tool yields a UI element (generated image, woo product card,
19 - // image-search gallery), the FC loop stashes its html here so the FC outcome
20 - // handler can SURFACE it to the frontend the same way the intent path does,
21 - // instead of stripping it to text for the model (the bug: UI-bearing actions
22 - // rendered nothing under function calling).
23 - private $fc_ui_html = '';
24 - private $fc_ui_images = array();
25 - private $fc_ui_captured = false;
26 12 private $word_handler;
27 - private $last_similarity_analysis = null;
28 - private $current_valid_urls = [];
29 - private $last_vectorstore_error = null;
30 - private $is_streaming = false; // ADDED: Track if current request is streaming
31 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
32 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
33 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
34 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
35 13
36 14 /**
37 - * Setup streaming headers - call this right before actually streaming
38 - * This delays header setup to allow actions/forms to return JSON responses
15 + * Setup the cron jobs for rate limits
39 16 */
40 -/**
41 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
42 - *
43 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
44 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
45 - * specific "overloaded" / "rate limit" body string. Returns immediately on
46 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
47 - *
48 - * Drop-in replacement for wp_remote_post — returns the same shape
49 - * (WP_Error or response array) so the caller's existing error-handling
50 - * code path is unchanged.
51 - *
52 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
53 - * paths (the *_response_openai / *_response_claude / etc functions).
54 - * For the *_stream variants, the cURL initial-connect happens inside a
55 - * read-chunks loop — retrying there safely (without re-emitting partial
56 - * stream chunks to the client) is a separate problem. Streaming paths
57 - * are NOT wrapped in this build; tracked as a follow-on.
58 - *
59 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
60 - * (default true). When false, behavior is identical to plain wp_remote_post.
61 - */
62 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
63 - $opts = is_array($this->options ?? null) ? $this->options : array();
64 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
65 - (string) $opts['auto_retry_on_transient_error'] !== '0';
66 -
67 - if (!$enabled) {
68 - return wp_remote_post($url, $args);
17 +public function setup_rate_limit_cron_jobs() {
18 + // Clear previous schedules
19 + wp_clear_scheduled_hook('mxchat_reset_rate_limits');
20 + wp_clear_scheduled_hook('mxchat_reset_hourly_rate_limits');
21 + wp_clear_scheduled_hook('mxchat_reset_daily_rate_limits');
22 + wp_clear_scheduled_hook('mxchat_reset_weekly_rate_limits');
23 + wp_clear_scheduled_hook('mxchat_reset_monthly_rate_limits');
24 +
25 + // Schedule the main rate limit reset check (runs hourly)
26 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
27 + wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
69 28 }
70 -
71 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
72 - $last_response = null;
73 -
74 - foreach ($backoffs as $i => $delay_ms) {
75 - if ($delay_ms > 0) {
76 - usleep($delay_ms * 1000);
77 - }
78 - $response = wp_remote_post($url, $args);
79 - $last_response = $response;
80 -
81 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
82 - return $response;
83 - }
84 -
85 - if (defined('WP_DEBUG') && WP_DEBUG) {
86 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
87 - : (int) wp_remote_retrieve_response_code($response);
88 - error_log(sprintf(
89 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
90 - $provider_hint ?: 'unknown',
91 - $i + 1,
92 - $code_for_log,
93 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
94 - ));
95 - }
96 - }
97 -
98 - return $last_response;
99 29 }
100 30
101 31 /**
102 - * Returns true if a wp_remote_post response represents a TRANSIENT
103 - * provider error worth retrying. Conservative — only retries on signals
104 - * that are very likely to clear within a few seconds.
105 - *
106 - * Transient signals:
107 - * - WP_Error with timeout / connection / dns / ssl
108 - * - HTTP 429, 502, 503, 504
109 - * - Provider-specific overload bodies (gemini "overloaded", openai
110 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
111 - *
112 - * NOT transient (return false — fail-fast):
113 - * - 200/2xx (success)
114 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
115 - * budget; the user needs to fix something)
116 - * - Any other 4xx (assume permanent unless explicitly listed above)
117 - * - 5xx other than the four listed above (e.g. 500 generic server error
118 - * is often a malformed request on our side, not a transient outage)
119 - */
120 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
121 - if (is_wp_error($response)) {
122 - $code = $response->get_error_code();
123 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
124 - || stripos((string) $response->get_error_message(), 'timed out') !== false
125 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
126 - }
127 -
128 - $status = (int) wp_remote_retrieve_response_code($response);
129 - if (in_array($status, array(429, 502, 503, 504), true)) {
130 - return true;
131 - }
132 - if ($status >= 200 && $status < 300) {
133 - return false;
134 - }
135 - // Permanent 4xx that should fail fast — even with no body.
136 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
137 - return false;
138 - }
139 -
140 - // Provider-specific body inspection for the cases where the upstream
141 - // returns 200 with an error envelope (gemini does this for overload).
142 - $body = (string) wp_remote_retrieve_body($response);
143 - if ($body === '') {
144 - return false;
145 - }
146 - $lower = strtolower($body);
147 - $hint = strtolower((string) $provider_hint);
148 -
149 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
150 - || strpos($lower, 'high demand') !== false
151 - || strpos($lower, 'model is overloaded') !== false)) {
152 - return true;
153 - }
154 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
155 - || strpos($lower, '"type":"server_error"') !== false
156 - || strpos($lower, '"code":"server_error"') !== false)) {
157 - return true;
158 - }
159 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
160 - || strpos($lower, 'overloaded_error') !== false)) {
161 - return true;
162 - }
163 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
164 - return true;
165 - }
166 -
167 - return false;
168 -}
169 -
170 -/**
171 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
172 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
173 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
174 - * collect status separately from a plain wp_remote_post array shape, so the
175 - * non-streaming helper above can't be called directly. This delegate keeps
176 - * the classification rules identical across both paths.
177 - */
178 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
179 - if ($curl_errno) {
180 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
181 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
182 - return in_array($curl_errno, array(
183 - CURLE_OPERATION_TIMEDOUT,
184 - CURLE_COULDNT_CONNECT,
185 - CURLE_COULDNT_RESOLVE_HOST,
186 - CURLE_SSL_CONNECT_ERROR,
187 - CURLE_GOT_NOTHING,
188 - CURLE_SEND_ERROR,
189 - CURLE_RECV_ERROR,
190 - ), true);
191 - }
192 -
193 - $status = (int) $http_code;
194 - if (in_array($status, array(429, 502, 503, 504), true)) {
195 - return true;
196 - }
197 - if ($status >= 200 && $status < 300) {
198 - return false;
199 - }
200 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
201 - return false;
202 - }
203 -
204 - $body = (string) $body;
205 - if ($body === '') {
206 - return false;
207 - }
208 - $lower = strtolower($body);
209 - $hint = strtolower((string) $provider_hint);
210 -
211 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
212 - || strpos($lower, 'high demand') !== false
213 - || strpos($lower, 'model is overloaded') !== false)) {
214 - return true;
215 - }
216 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
217 - || strpos($lower, '"type":"server_error"') !== false
218 - || strpos($lower, '"code":"server_error"') !== false)) {
219 - return true;
220 - }
221 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
222 - || strpos($lower, 'overloaded_error') !== false)) {
223 - return true;
224 - }
225 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
226 - return true;
227 - }
228 -
229 - return false;
230 -}
231 -
232 -/**
233 - * Whether transient-error auto-retry is enabled in admin settings.
234 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
235 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
236 - */
237 -private function mxchat_retry_enabled() {
238 - $opts = is_array($this->options ?? null) ? $this->options : array();
239 - return !isset($opts['auto_retry_on_transient_error']) ||
240 - (string) $opts['auto_retry_on_transient_error'] !== '0';
241 -}
242 -
243 -private function setup_streaming_headers() {
244 - if ($this->streaming_headers_sent || headers_sent()) {
245 - return false;
246 - }
247 -
248 - // Disable output buffering
249 - while (ob_get_level()) {
250 - ob_end_flush();
251 - }
252 -
253 - // Set headers for SSE
254 - header('Content-Type: text/event-stream');
255 - header('Cache-Control: no-cache');
256 - header('Connection: keep-alive');
257 - header('X-Accel-Buffering: no');
258 -
259 - ob_implicit_flush(true);
260 - flush();
261 -
262 - $this->streaming_headers_sent = true;
263 - return true;
264 -}
265 -
266 -/**
267 32 * Class constructor
268 33 */
269 34 public function __construct() {
270 35 $this->options = get_option('mxchat_options');
@@ -271,8 +36,11 @@
271 36 $this->prompts_options = get_option('mxchat_prompts_options', array());
272 37 $this->chat_count = get_option('mxchat_chat_count', 0);
273 38 $this->word_handler = new MXChat_Word_Handler($this->options);
274 39
40 + // Setup the cron jobs for rate limits
41 + $this->setup_rate_limit_cron_jobs();
42 +
275 43 // Add all action hooks
276 44 add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
277 45 add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
278 46 add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
@@ -313,126 +81,11 @@
313 81 add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
314 82 add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
315 83 add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
316 84 add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
317 -
318 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
319 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
320 -
321 - // Testing panel AJAX actions
322 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
323 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
324 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
325 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
326 - // Add to your existing constructor, in the section with other AJAX actions:
327 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
328 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
329 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
330 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
331 - // Add chat mode checking actions
332 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
333 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
334 -
335 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
336 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
337 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
338 -
339 - // Auto-email transcript action
340 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
341 -
342 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
343 -
344 -
345 85 }
346 86
347 -/**
348 - * Return a fresh nonce so cached pages can replace the stale one.
349 - * With `with_settings`, also returns the current behavior-gate settings so
350 - * the widget can correct stale inline-localized values (plan-32db95).
351 - */
352 -public function mxchat_refresh_nonce() {
353 - nocache_headers();
354 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
355 - if (!empty($_REQUEST['with_settings'])) {
356 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
357 - }
358 - wp_send_json_success($payload);
359 -}
360 87
361 -/**
362 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
363 - *
364 - * Every widget setting ships inline in page HTML via wp_localize_script, so
365 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
366 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
367 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
368 - * caches PHP can reach; this payload covers the rest — the widget requests
369 - * it on first open (via the nonce-refresh endpoints) and merges it over
370 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
371 - * nonce uses.
372 - *
373 - * Behavior gates + labels ONLY — colors stay inline because they're also
374 - * server-inline-styled, and a runtime swap would visibly flash.
375 - *
376 - * Both wp_localize_script blocks merge this exact array, so the inline and
377 - * refreshed payloads cannot drift.
378 - *
379 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
380 - * instead of trusting the instance copy.
381 - * @return array
382 - */
383 -public function get_dynamic_widget_settings($fresh = false) {
384 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
385 - if (!is_array($options)) {
386 - $options = array();
387 - }
388 - return array(
389 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
390 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
391 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
392 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
393 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
394 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
395 - // "Start new chat" header-menu item (plan ac2e81). Default OFF.
396 - 'reset_chat_enabled' => $options['reset_chat_enabled'] ?? 'off',
397 - 'reset_chat_label' => !empty($options['reset_chat_label']) ? esc_html($options['reset_chat_label']) : esc_html__('Start new chat', 'mxchat'),
398 - 'reset_chat_confirm' => esc_html__('Start a new chat? This clears the current conversation.', 'mxchat'),
399 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
400 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
401 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
402 - // scalars to string, and (string) false === '' — which the widget's
403 - // old gate read as enabled (plan-4bba64). The filter keeps its
404 - // boolean contract; only the emitted value is stringified.
405 - 'satisfaction_rating_enabled' => apply_filters(
406 - 'mxchat_satisfaction_rating_enabled',
407 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
408 - ) ? 'on' : 'off',
409 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
410 - 'satisfaction_rating_copy' => array(
411 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
412 - 'helpful' => esc_html__('Helpful', 'mxchat'),
413 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
414 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
415 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
416 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
417 - 'send' => esc_html__('Send', 'mxchat'),
418 - 'skip' => esc_html__('Skip', 'mxchat'),
419 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
420 - ),
421 - );
422 -}
423 -
424 -// In your core plugin's check_actions_for_addons method:
425 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
426 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
427 -
428 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
429 -
430 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
431 -
432 - return $result;
433 -}
434 -
435 88 private function mxchat_increment_chat_count() {
436 89 $chat_count = get_option('mxchat_chat_count', 0);
437 90 $chat_count++;
438 91 update_option('mxchat_chat_count', $chat_count);
@@ -444,22 +97,8 @@
444 97 wp_die();
445 98 }
446 99
447 100 $session_id = sanitize_text_field($_POST['session_id']);
448 -
449 - // SECURITY FIX: Verify session ownership before retrieving data
450 - // If IP/user changed, signal frontend to reset session instead of blocking
451 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
452 -
453 - // Check if this session has an owner recorded
454 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
455 -
456 - // Update session owner if it changed (e.g. IP changed due to network switch)
457 - // The session ID itself is the authentication — if the client has it, they own it
458 - if (!$session_owner || $session_owner !== $current_user_identifier) {
459 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
460 - }
461 -
462 101 $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
463 102 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
464 103
465 104 if (empty($history)) {
@@ -476,25 +115,26 @@
476 115 'chat_mode' => $chat_mode
477 116 ]);
478 117 wp_die();
479 118 }
480 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
481 - $history = get_option("mxchat_history_{$session_id}", []);
119 +private function mxchat_fetch_conversation_history_for_ajax($session_id) {
120 + $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history based on session ID
121 + $formatted_history = [];
482 122
483 - // Check persistence setting - when OFF, only include messages from current page load
484 - $options = get_option('mxchat_options', []);
485 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
123 + // Format the history to align with the expected structure for OpenAI
124 + foreach ($history as $entry) {
125 + $formatted_history[] = [
126 + 'role' => $entry['role'], // Ensure this matches 'user' or 'assistant'
127 + 'content' => $entry['content']
128 + ];
129 + }
486 130
487 - // Filter history when persistence is OFF to match what the user sees
488 - if (!$persistence_enabled && $session_start_timestamp > 0) {
489 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
490 - // Include messages from this page load onwards
491 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
492 - });
493 - // Re-index array after filtering
494 - $history = array_values($history);
495 - }
131 + return $formatted_history;
132 +}
496 133
134 +
135 +private function mxchat_fetch_conversation_history_for_ai($session_id) {
136 + $history = get_option("mxchat_history_{$session_id}", []);
497 137 $formatted_history = [];
498 138
499 139 // Adjusted for code-heavy conversations
500 140 $max_tokens = 120000; // Context window size
@@ -529,9 +169,9 @@
529 169 if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
530 170 continue;
531 171 }
532 172
533 - // More accurate token estimation (1 token ≈ 4 characters)
173 + // More accurate token estimation (1 token ≈ 4 characters)
534 174 $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
535 175
536 176 // Check token budget with the new estimate
537 177 if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
@@ -568,17 +208,8 @@
568 208
569 209 public function register_routes() {
570 210 //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
571 211
572 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
573 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
574 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
575 - register_rest_route('mxchat/v1', '/nonce', [
576 - 'methods' => 'GET',
577 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
578 - 'permission_callback' => '__return_true',
579 - ]);
580 -
581 212 register_rest_route('mxchat/v1', '/stream', [
582 213 'methods' => 'GET',
583 214 'callback' => [$this, 'mxchat_stream_events'],
584 215 'permission_callback' => [$this, 'verify_chat_session'],
@@ -594,112 +225,13 @@
594 225 'methods' => 'POST',
595 226 'callback' => [$this, 'handle_slack_interaction'],
596 227 'permission_callback' => [$this, 'verify_slack_request'],
597 228 ]);
598 -
599 - register_rest_route('mxchat/v1', '/slack-messages', [
600 - 'methods' => 'POST',
601 - 'callback' => [$this, 'handle_slack_messages'],
602 - 'permission_callback' => [$this, 'verify_slack_request'],
603 - ]);
604 229
605 - // Telegram webhook endpoint
606 - register_rest_route('mxchat/v1', '/telegram-webhook', [
607 - 'methods' => 'POST',
608 - 'callback' => [$this, 'handle_telegram_webhook'],
609 - 'permission_callback' => [$this, 'verify_telegram_request'],
610 - ]);
611 -
612 230 //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
613 231 }
614 232
615 233 /**
616 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
617 - * caches it for the session and includes it on every chat-send / stream-send /
618 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
619 - * we eliminate the entire class of "first-message Access denied" failures that
620 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
621 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
622 - * lives in the HTML body.
623 - *
624 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
625 - * client browser can't be used to flood the nonce-issuance path.
626 - *
627 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
628 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
629 - * backwards-compat window so cached pages still in users' browsers don't break
630 - * mid-session.
631 - *
632 - * @since 3.2.7
633 - */
634 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
635 - $ip = '';
636 - if (!empty($_SERVER['REMOTE_ADDR'])) {
637 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
638 - }
639 - if ($ip !== '') {
640 - // Best-effort rate limit. WP transients with sub-second TTL are racy
641 - // (parallel bursts can squeak through before set_transient completes);
642 - // we use 2s to make the gate slightly more reliable. Real production
643 - // rate-limiting at sub-second granularity needs Redis or DB row locks
644 - // — out of scope for this endpoint, which is already cheap.
645 - $key = 'mxchat_nonce_rl_' . md5($ip);
646 - if (get_transient($key)) {
647 - return new WP_REST_Response(array(
648 - 'error' => 'rate_limited',
649 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
650 - ), 429);
651 - }
652 - set_transient($key, 1, 2);
653 - }
654 -
655 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
656 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
657 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
658 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
659 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
660 - if ( ! is_user_logged_in() ) {
661 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
662 - if ( $maybe_uid ) {
663 - wp_set_current_user( $maybe_uid );
664 - }
665 - }
666 -
667 - $payload = array(
668 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
669 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
670 - );
671 -
672 - // plan-32db95: the widget's first-open refresh asks for current behavior
673 - // settings in the same round-trip, so stale inline-localized values on
674 - // cached pages get corrected without a second request. All values in
675 - // this payload already ship in public page HTML — nothing sensitive.
676 - if ($request->get_param('with_settings')) {
677 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
678 - }
679 -
680 - return new WP_REST_Response($payload, 200);
681 -}
682 -
683 -/**
684 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
685 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
686 - * action (inline-localized in older cached HTML). The legacy acceptance is
687 - * a 30-day backwards-compat window — to be removed in a follow-up release
688 - * after 2026-06-27.
689 - *
690 - * @param string $posted_nonce
691 - * @return bool
692 - */
693 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
694 - if (!is_string($posted_nonce) || $posted_nonce === '') {
695 - return false;
696 - }
697 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
698 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
699 -}
700 -
701 -/**
702 234 * Verify valid chat session
703 235 */
704 236 public function verify_chat_session($request) {
705 237 $session_id = $request->get_param('session_id');
@@ -735,11 +267,10 @@
735 267 //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
736 268 return false;
737 269 }
738 270
739 - // Get raw request body from the WP_REST_Request object
740 - // (php://input may already be consumed by WordPress at this point)
741 - $request_body = $request->get_body();
271 + // Get raw request body
272 + $request_body = file_get_contents('php://input');
742 273
743 274 // Create the signature base string
744 275 $sig_basestring = "v0:{$timestamp}:{$request_body}";
745 276
@@ -748,93 +279,8 @@
748 279
749 280 // Compare signatures
750 281 return hash_equals($my_signature, $slack_signature);
751 282 }
752 -
753 -/**
754 - * Verify request is coming from Telegram.
755 - *
756 - * @param WP_REST_Request $request
757 - * @return bool True if valid, false otherwise.
758 - */
759 -public function verify_telegram_request($request) {
760 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
761 -
762 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
763 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
764 -
765 - if (empty($secret_token)) {
766 - // No secret configured (legacy setup). Do NOT fail open to the whole
767 - // internet — that lets an unauthenticated caller write agent-branded
768 - // messages. Fall back to verifying the request originates from
769 - // Telegram's published webhook IP ranges so existing no-secret installs
770 - // keep working while an arbitrary-internet caller is blocked. Setting a
771 - // real secret (see the admin notice) is the recommended path.
772 - // (plan-0c17b5)
773 - $peer = isset($_SERVER['REMOTE_ADDR']) ? (string) $_SERVER['REMOTE_ADDR'] : '';
774 - if ($this->mxchat_ip_in_telegram_ranges($peer)) {
775 - return true;
776 - }
777 - error_log('MxChat: Telegram webhook has no secret configured and the request '
778 - . 'is not from a Telegram IP range; rejected. Set a webhook secret to secure it.');
779 - return false;
780 - }
781 -
782 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
783 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
784 -
785 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
786 -
787 - if (empty($request_token)) {
788 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
789 - return false;
790 - }
791 -
792 - // Timing-safe comparison
793 - $result = hash_equals($secret_token, $request_token);
794 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
795 - return $result;
796 -}
797 -
798 -/**
799 - * Whether $ip falls within Telegram's published webhook IPv4 ranges
800 - * (149.154.160.0/20 and 91.108.4.0/22). Used as an authenticity fallback for
801 - * the Telegram webhook when no secret token is configured, so a legacy
802 - * no-secret install keeps working without failing open to the entire internet.
803 - *
804 - * Uses the real TCP peer (REMOTE_ADDR); a spoofable X-Forwarded-For is NOT
805 - * consulted. Behind a reverse proxy / CDN that rewrites REMOTE_ADDR this may
806 - * not match — which is exactly why configuring a real webhook secret is the
807 - * recommended path. (plan-0c17b5)
808 - *
809 - * @param string $ip Candidate IPv4 address.
810 - * @return bool
811 - */
812 -private function mxchat_ip_in_telegram_ranges($ip) {
813 - if (!is_string($ip) || $ip === '' || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
814 - return false;
815 - }
816 - $ip_long = ip2long($ip);
817 - if ($ip_long === false) {
818 - return false;
819 - }
820 - $ranges = array(
821 - array('149.154.160.0', 20),
822 - array('91.108.4.0', 22),
823 - );
824 - foreach ($ranges as $range) {
825 - $subnet_long = ip2long($range[0]);
826 - if ($subnet_long === false) {
827 - continue;
828 - }
829 - $mask = (0xFFFFFFFF << (32 - $range[1])) & 0xFFFFFFFF;
830 - if (($ip_long & $mask) === ($subnet_long & $mask)) {
831 - return true;
832 - }
833 - }
834 - return false;
835 -}
836 -
837 283 public function mxchat_stream_events(WP_REST_Request $request) {
838 284 header('Content-Type: text/event-stream');
839 285 header('Cache-Control: no-cache');
840 286 header('Connection: keep-alive');
@@ -868,45 +314,20 @@
868 314
869 315
870 316
871 317
872 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
318 +private function mxchat_save_chat_message($session_id, $role, $message) {
873 319 global $wpdb;
320 +
874 321 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
875 322 //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
876 -
877 - // Check if this is the first message in a new session (before any other database operations)
878 - $is_new_session = false;
879 - if ($role === 'user') { // Only check for user messages, not bot responses
880 - $existing_messages = $wpdb->get_var($wpdb->prepare(
881 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
882 - $session_id
883 - ));
884 - $is_new_session = ($existing_messages == 0);
885 -
886 - // Log for debugging
887 - if ($is_new_session) {
888 - //error_log("[DEBUG] This is a NEW session - first message");
889 - }
890 - }
891 -
892 - // SECURITY FIX: Set session ownership for new sessions
893 - if ($is_new_session && $role === 'user') {
894 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
895 - $session_owner_key = "mxchat_session_owner_{$session_id}";
896 -
897 - // Only set ownership if not already set
898 - if (!get_option($session_owner_key)) {
899 - update_option($session_owner_key, $current_user_identifier, 'no');
900 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
901 - }
902 - }
903 -
323 +
904 324 // 1) Extract agent name if present
905 325 $agent_name = '';
906 326 if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
907 327 $agent_name = $matches[1];
908 328 $message = str_replace("Agent: $agent_name - ", '', $message);
329 +
909 330 $session_meta_key = "mxchat_agent_name_{$session_id}";
910 331 if (empty(get_option($session_meta_key))) {
911 332 update_option($session_meta_key, $agent_name);
912 333 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
@@ -911,57 +332,42 @@
911 332 update_option($session_meta_key, $agent_name);
912 333 //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
913 334 }
914 335 }
915 -
336 +
916 337 // 2) Generate unique message_id
917 338 $message_id = uniqid();
918 339 //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
919 -
340 +
920 341 // 3) Determine user_id
921 342 $user_id = is_user_logged_in() ? get_current_user_id() : 0;
922 -
343 +
923 344 // 4) Determine user_identifier
924 345 $user_identifier = $agent_name
925 346 ? $agent_name
926 347 : MxChat_User::mxchat_get_user_identifier();
927 -
348 +
928 349 // 5) Determine displayed_name
929 350 $user_email = MxChat_User::mxchat_get_user_email();
930 351 $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
931 -
352 +
932 353 // 6) Check for a saved email in wp_options
933 354 $email_option_key = "mxchat_email_{$session_id}";
934 355 $saved_email = get_option($email_option_key);
935 356 //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
936 -
937 - // Check for a saved name in wp_options
938 - $name_option_key = "mxchat_name_{$session_id}";
939 - $saved_name = get_option($name_option_key);
940 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
941 -
942 - // If found, update DB user_email and user_name
943 - if ($saved_email || $saved_name) {
944 - $update_data = [];
945 - if ($saved_email) {
946 - $update_data['user_email'] = $saved_email;
947 - }
948 - if ($saved_name) {
949 - $update_data['user_name'] = $saved_name;
950 - }
951 -
952 - if (!empty($update_data)) {
953 - $update_res = $wpdb->update(
954 - $table_name,
955 - $update_data,
956 - ['session_id' => $session_id],
957 - array_fill(0, count($update_data), '%s'),
958 - ['%s']
959 - );
960 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
961 - }
357 +
358 + // If found, update DB user_email
359 + if ($saved_email) {
360 + $update_res = $wpdb->update(
361 + $table_name,
362 + ['user_email' => $saved_email],
363 + ['session_id' => $session_id],
364 + ['%s'],
365 + ['%s']
366 + );
367 + //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email update for session_id {$session_id}. update_res: {$update_res}");
962 368 }
963 -
369 +
964 370 // 7) Save to session history in wp_options
965 371 $history_key = "mxchat_history_{$session_id}";
966 372 $history = get_option($history_key, []);
967 373 $history[] = [
@@ -970,351 +376,33 @@
970 376 'content' => $message,
971 377 'timestamp' => round(microtime(true) * 1000),
972 378 'agent_name' => $displayed_name,
973 379 ];
974 - update_option($history_key, $history, 'no');
380 + update_option($history_key, $history);
975 381 //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
976 -
382 +
977 383 // 8) Save the message to DB (INSERT)
978 384 $insert_data = [
979 385 'user_id' => $user_id,
980 386 'user_identifier'=> $user_identifier,
981 387 'user_email' => $saved_email ?: $user_email,
982 - 'user_name' => $saved_name ?: '', // Add name to insert data
983 388 'session_id' => $session_id,
984 389 'role' => $role,
985 390 'message' => $message,
986 391 'timestamp' => current_time('mysql', 1),
987 392 ];
988 -
989 - // IMPROVED: Handle originating page data
990 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
991 -
992 - if ($columns_exist) {
993 - if ($is_new_session && $role === 'user') {
994 - // For the first user message, set originating page data
995 -
996 - // First check if we have it from the parameter
997 - if ($originating_page && !empty($originating_page['url'])) {
998 - $insert_data['originating_page_url'] = $originating_page['url'];
999 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
1000 -
1001 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
1002 - }
1003 - // Otherwise check if it's stored in the instance property
1004 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
1005 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
1006 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
1007 -
1008 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
1009 -
1010 - // Clear after using (= null, not unset(): unset() undeclares the property
1011 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
1012 - $this->pending_originating_page = null;
1013 - }
1014 - // Fallback to HTTP_REFERER if nothing else is available
1015 - else if (isset($_SERVER['HTTP_REFERER'])) {
1016 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1017 - $insert_data['originating_page_url'] = $referer_url;
1018 -
1019 - // Generate title from URL
1020 - $parsed_url = parse_url($referer_url);
1021 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1022 -
1023 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1024 - $insert_data['originating_page_title'] = 'Homepage';
1025 - } else {
1026 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1027 - $insert_data['originating_page_title'] = ucwords(trim($title));
1028 - }
1029 -
1030 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
1031 - }
1032 -
1033 - // Store for this session so all messages have the same originating page
1034 - if (!empty($insert_data['originating_page_url'])) {
1035 - update_option("mxchat_originating_page_{$session_id}", [
1036 - 'url' => $insert_data['originating_page_url'],
1037 - 'title' => $insert_data['originating_page_title']
1038 - ], 'no');
1039 - }
1040 - } else {
1041 - // For subsequent messages in the session, use the stored originating page
1042 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
1043 - if ($stored_originating && !empty($stored_originating['url'])) {
1044 - $insert_data['originating_page_url'] = $stored_originating['url'];
1045 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
1046 - }
1047 - }
1048 - }
1049 -
1050 - // Add RAG context if provided (for bot messages)
1051 - if ($rag_context !== null && $role === 'bot') {
1052 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
1053 - if ($rag_context_column_exists) {
1054 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
1055 - }
1056 - }
1057 -
1058 393 $wpdb->insert($table_name, $insert_data);
1059 394 //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
1060 -
1061 - // 9) Send notification email if this is the first user message in a new session
1062 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
1063 - $this->send_new_chat_notification($session_id, array(
1064 - 'identifier' => $user_identifier,
1065 - 'email' => $saved_email ?: $user_email,
1066 - 'ip' => $_SERVER['REMOTE_ADDR']
1067 - ));
1068 - }
1069 -
1070 - // 10) Schedule delayed transcript email if enabled and message is from user
1071 - if ($wpdb->insert_id && $role === 'user') {
1072 - $this->schedule_delayed_transcript_email($session_id);
1073 - }
1074 -
395 +
1075 396 //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1076 397 return $message_id;
1077 398 }
1078 399
1079 -private function send_new_chat_notification($session_id, $user_info = array()) {
1080 - $options = get_option('mxchat_transcripts_options');
1081 -
1082 - // Check if notifications are enabled
1083 - if (empty($options['mxchat_enable_notifications'])) {
1084 - return false;
1085 - }
1086 -
1087 - // Get notification email
1088 - $to = !empty($options['mxchat_notification_email']) ?
1089 - $options['mxchat_notification_email'] :
1090 - get_option('admin_email');
1091 -
1092 - if (!is_email($to)) {
1093 - return false;
1094 - }
1095 -
1096 - // Prepare email content
1097 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1098 -
1099 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1100 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1101 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1102 -
1103 - $message = sprintf(
1104 - "A new chat session has started on your website.\n\n" .
1105 - "Session ID: %s\n" .
1106 - "User: %s\n" .
1107 - "Email: %s\n" .
1108 - "IP Address: %s\n" .
1109 - "Time: %s\n\n" .
1110 - "View transcripts: %s",
1111 - $session_id,
1112 - $user_identifier,
1113 - $user_email,
1114 - $user_ip,
1115 - current_time('mysql'),
1116 - admin_url('admin.php?page=mxchat-transcripts')
1117 - );
1118 -
1119 - // Send email
1120 - return wp_mail($to, $subject, $message);
1121 -}
1122 -
1123 -/**
1124 - * Schedule delayed transcript email for a session
1125 - * Reschedules if a new user message is received
1126 - */
1127 -private function schedule_delayed_transcript_email($session_id) {
1128 - $options = get_option('mxchat_transcripts_options');
1129 -
1130 - // Check if auto-email is enabled
1131 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1132 - return;
1133 - }
1134 -
1135 - // Get notification email
1136 - $email = !empty($options['mxchat_notification_email']) ?
1137 - $options['mxchat_notification_email'] :
1138 - get_option('admin_email');
1139 -
1140 - if (!is_email($email)) {
1141 - return;
1142 - }
1143 -
1144 - // Get delay in minutes (default 30)
1145 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1146 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1147 -
1148 - // Clear any existing scheduled event for this session
1149 - $hook = 'mxchat_send_delayed_transcript';
1150 - $args = array($session_id);
1151 - $timestamp = wp_next_scheduled($hook, $args);
1152 -
1153 - if ($timestamp) {
1154 - wp_unschedule_event($timestamp, $hook, $args);
1155 - }
1156 -
1157 - // Schedule new event
1158 - $schedule_time = time() + ($delay_minutes * 60);
1159 - wp_schedule_single_event($schedule_time, $hook, $args);
1160 -}
1161 -
1162 -/**
1163 - * Check if chat messages contain contact information (email or phone number)
1164 - *
1165 - * @param array $messages Array of message objects with 'message' property
1166 - * @param object|null $session_data Session data object with user_email property
1167 - * @return bool True if contact info found, false otherwise
1168 - */
1169 -private function chat_contains_contact_info($messages, $session_data = null) {
1170 - // Check if session already has a stored email
1171 - if ($session_data && !empty($session_data->user_email)) {
1172 - return true;
1173 - }
1174 -
1175 - // Email regex pattern
1176 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1177 -
1178 - // Phone number patterns (covers various formats including international, WhatsApp style)
1179 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1180 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1181 -
1182 - // Only check user messages (not assistant responses)
1183 - foreach ($messages as $msg) {
1184 - if ($msg->role !== 'user') {
1185 - continue;
1186 - }
1187 -
1188 - $message_text = $msg->message;
1189 -
1190 - // Check for email
1191 - if (preg_match($email_pattern, $message_text)) {
1192 - return true;
1193 - }
1194 -
1195 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1196 - if (preg_match($phone_pattern, $message_text, $matches)) {
1197 - // Count actual digits to avoid matching short numbers
1198 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1199 - if (strlen($digits_only) >= 7) {
1200 - return true;
1201 - }
1202 - }
1203 - }
1204 -
1205 - return false;
1206 -}
1207 -
1208 -/**
1209 - * Send the delayed transcript email with .txt attachment
1210 - */
1211 -public function mxchat_send_delayed_transcript($session_id) {
1212 - global $wpdb;
1213 -
1214 - $options = get_option('mxchat_transcripts_options');
1215 -
1216 - // Get notification email
1217 - $to = !empty($options['mxchat_notification_email']) ?
1218 - $options['mxchat_notification_email'] :
1219 - get_option('admin_email');
1220 -
1221 - if (!is_email($to)) {
1222 - return false;
1223 - }
1224 -
1225 - // Get all messages for this session
1226 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1227 - $messages = $wpdb->get_results($wpdb->prepare(
1228 - "SELECT role, message, timestamp FROM {$table_name}
1229 - WHERE session_id = %s
1230 - ORDER BY timestamp ASC",
1231 - $session_id
1232 - ));
1233 -
1234 - if (empty($messages)) {
1235 - return false;
1236 - }
1237 -
1238 - // Get session metadata
1239 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1240 - $session_data = $wpdb->get_row($wpdb->prepare(
1241 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1242 - $session_id
1243 - ));
1244 -
1245 - // Check if contact info is required and if it's present
1246 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1247 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1248 - // Contact info required but not found - skip sending
1249 - return false;
1250 - }
1251 -
1252 - // Build transcript content
1253 - $transcript_content = "Chat Transcript\n";
1254 - $transcript_content .= "================\n\n";
1255 - $transcript_content .= "Session ID: " . $session_id . "\n";
1256 -
1257 - if ($session_data) {
1258 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1259 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1260 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1261 - }
1262 -
1263 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1264 -
1265 - // Add messages
1266 - foreach ($messages as $msg) {
1267 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1268 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1269 - $transcript_content .= $msg->message . "\n\n";
1270 - }
1271 -
1272 - // Create temporary file for attachment using WP_Filesystem
1273 - $upload_dir = wp_upload_dir();
1274 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1275 - global $wp_filesystem;
1276 - if (empty($wp_filesystem)) {
1277 - require_once ABSPATH . 'wp-admin/includes/file.php';
1278 - WP_Filesystem();
1279 - }
1280 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1281 -
1282 - // Prepare email
1283 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1284 -
1285 - $message = "Please find attached the full chat transcript.\n\n";
1286 - $message .= "Session ID: {$session_id}\n";
1287 -
1288 - if ($session_data) {
1289 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1290 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1291 - }
1292 -
1293 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1294 -
1295 - // Send email with attachment
1296 - $attachments = array($temp_file);
1297 - $result = wp_mail($to, $subject, $message, '', $attachments);
1298 -
1299 - // Clean up temporary file
1300 - if (file_exists($temp_file)) {
1301 - unlink($temp_file);
1302 - }
1303 -
1304 - return $result;
1305 -}
1306 -
1307 -
1308 -
1309 400 public function mxchat_handle_save_email_and_response() {
1310 401 //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1311 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1312 402
1313 - nocache_headers();
1314 -
1315 403 // Validate nonce
1316 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
404 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1317 405 //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1318 406 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1319 407 wp_die();
1320 408 }
@@ -1320,41 +408,22 @@
1320 408 }
1321 409
1322 410 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1323 411 $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1324 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1325 412
1326 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
413 + //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}");
1327 414
1328 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
415 + if (empty($session_id) || empty($email)) {
1329 416 //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1330 417 wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1331 418 wp_die();
1332 419 }
1333 420
1334 - // Validate name if provided (check if name field is enabled and name is required)
1335 - $options = get_option('mxchat_options', []);
1336 - $name_field_enabled = isset($options['enable_name_field']) &&
1337 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1338 -
1339 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1340 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1341 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1342 - wp_die();
1343 - }
421 + // 1) Always store in wp_options
422 + $option_key = "mxchat_email_{$session_id}";
423 + update_option($option_key, $email);
424 + //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$option_key} => {$email}");
1344 425
1345 - // 1) Always store email in wp_options
1346 - $email_option_key = "mxchat_email_{$session_id}";
1347 - update_option($email_option_key, $email, 'no');
1348 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1349 -
1350 - // Store name in wp_options if provided
1351 - if (!empty($name)) {
1352 - $name_option_key = "mxchat_name_{$session_id}";
1353 - update_option($name_option_key, $name, 'no');
1354 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1355 - }
1356 -
1357 426 // 2) (Optional) Also store in DB if a row already exists
1358 427 global $wpdb;
1359 428 $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1360 429
@@ -1364,30 +433,21 @@
1364 433
1365 434 //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1366 435
1367 436 if ($session_count) {
1368 - // Update both user_email and user_name if row(s) exist
1369 - if (!empty($name)) {
1370 - $update_sql = $wpdb->prepare(
1371 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1372 - $email,
1373 - $name,
1374 - $session_id
1375 - );
1376 - } else {
1377 - $update_sql = $wpdb->prepare(
1378 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1379 - $email,
1380 - $session_id
1381 - );
1382 - }
437 + // Update user_email if row(s) exist
438 + $update_sql = $wpdb->prepare(
439 + "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
440 + $email,
441 + $session_id
442 + );
1383 443 $wpdb->query($update_sql);
1384 444 //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1385 445 } else {
1386 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
446 + //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email is only in wp_options.");
1387 447 }
1388 448
1389 - // Provide success response (same as original)
449 + // Provide success response
1390 450 $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1391 451 //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1392 452 wp_send_json_success(['message' => $bot_message]);
1393 453 wp_die();
@@ -1395,17 +455,15 @@
1395 455
1396 456 public function mxchat_check_email_provided() {
1397 457 //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1398 458
1399 - nocache_headers();
1400 -
1401 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
459 + if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mxchat_chat_nonce')) {
1402 460 //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1403 461 wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1404 462 }
1405 463
1406 464 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1407 - if (empty($session_id) || $session_id === 'null') {
465 + if (empty($session_id)) {
1408 466 //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1409 467 wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1410 468 }
1411 469
@@ -1412,82 +470,61 @@
1412 470 // Check if the user is logged in
1413 471 if (is_user_logged_in()) {
1414 472 $current_user = wp_get_current_user();
1415 473 //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1416 -
1417 - // Get user's display name for logged in users
1418 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1419 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1420 -
1421 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1422 - if (!empty($user_name)) {
1423 - $response_data['name'] = $user_name;
1424 - }
1425 -
1426 - wp_send_json_success($response_data);
474 + wp_send_json_success(['logged_in' => true, 'email' => $current_user->user_email]);
1427 475 }
1428 476
1429 - // Check if name field is required
1430 - $options = get_option('mxchat_options', []);
1431 - $name_field_enabled = isset($options['enable_name_field']) &&
1432 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
477 + $option_key = "mxchat_email_{$session_id}";
478 + $stored_email = get_option($option_key, '');
1433 479
1434 - $email_option_key = "mxchat_email_{$session_id}";
1435 - $stored_email = get_option($email_option_key, '');
1436 -
1437 - // Check for stored name
1438 - $name_option_key = "mxchat_name_{$session_id}";
1439 - $stored_name = get_option($name_option_key, '');
480 + //error_log("[DEBUG] mxchat_check_email_provided -> Checking option: {$option_key}, found: {$stored_email}");
1440 481
1441 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1442 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1443 -
1444 - // Check if we have email and name (if name is required)
1445 - $has_required_info = !empty($stored_email);
1446 -
1447 - if ($name_field_enabled) {
1448 - $has_required_info = $has_required_info && !empty($stored_name);
1449 - }
1450 -
1451 - if ($has_required_info) {
1452 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1453 -
1454 - $response_data = ['email' => $stored_email];
1455 - if (!empty($stored_name)) {
1456 - $response_data['name'] = $stored_name;
1457 - }
1458 -
1459 - wp_send_json_success($response_data);
482 + if (!empty($stored_email)) {
483 + //error_log("[DEBUG] mxchat_check_email_provided -> Email found, returning success");
484 + wp_send_json_success(['email' => $stored_email]);
1460 485 } else {
1461 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
486 + //error_log("[DEBUG] mxchat_check_email_provided -> No email found, returning error");
1462 487 wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1463 488 }
1464 489 }
1465 490
1466 -/**
1467 - * Send error response in appropriate format based on streaming mode
1468 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1469 - *
1470 - * @param string $error_message The error message to display
1471 - * @param string $error_code Optional error code for debugging
1472 - */
1473 -private function send_error_response($error_message, $error_code = 'api_error') {
1474 - if ($this->is_streaming) {
1475 - echo "data: " . json_encode([
1476 - 'error' => true,
1477 - 'error_message' => $error_message,
1478 - 'error_code' => $error_code,
1479 - 'text' => $error_message,
1480 - 'message' => $error_message
1481 - ]) . "\n\n";
1482 - echo "data: [DONE]\n\n";
1483 - flush();
1484 - } else {
1485 - wp_send_json_error([
1486 - 'error_message' => $error_message,
1487 - 'error_code' => $error_code
491 +// Add this to your plugin's main PHP file
492 +public function mxchat_check_new_messages() {
493 + if (!isset($_POST['session_id']) || !isset($_POST['last_seen_id'])) {
494 + wp_send_json_error(['message' => 'Missing required parameters']);
495 + wp_die();
496 + }
497 +
498 + $session_id = sanitize_text_field($_POST['session_id']);
499 + $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
500 +
501 + // Get chat history
502 + $history = get_option("mxchat_history_{$session_id}", []);
503 +
504 + if (empty($history)) {
505 + wp_send_json_success([
506 + 'hasNewMessages' => false,
507 + 'new_messages' => []
1488 508 ]);
509 + wp_die();
1489 510 }
511 +
512 + // Filter new messages
513 + $new_messages = array_filter($history, function($message) use ($last_seen_id) {
514 + return isset($message['id']) && $message['id'] > $last_seen_id;
515 + });
516 +
517 + // Sort by ID to ensure proper order
518 + usort($new_messages, function($a, $b) {
519 + return $a['id'] <=> $b['id'];
520 + });
521 +
522 + wp_send_json_success([
523 + 'hasNewMessages' => !empty($new_messages),
524 + 'new_messages' => array_values($new_messages),
525 + 'latestMessageId' => end($new_messages)['id'] ?? $last_seen_id
526 + ]);
1490 527 wp_die();
1491 528 }
1492 529
1493 530 public function mxchat_handle_chat_request() {
@@ -1492,30 +529,10 @@
1492 529
1493 530 public function mxchat_handle_chat_request() {
1494 531 global $wpdb;
1495 532
1496 - // Debug: Log incoming bot_id
1497 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1498 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1499 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1500 -
1501 - // Get bot-specific options
1502 - $bot_options = $this->get_bot_options($bot_id);
1503 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1504 533
1505 - // Check if this is a streaming request
1506 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1507 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1508 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1509 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1510 -
1511 - // ADDED: Store streaming state in class property for use in private methods
1512 - $this->is_streaming = $is_streaming;
1513 -
1514 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1515 - // This allows actions/forms to return JSON responses without header conflicts
1516 -
1517 - // Check if MX Chat Moderation is active
534 + // Check if MX Chat Moderation is active
1518 535 if (class_exists('MX_Chat_Moderation')) {
1519 536 // Get user email and IP
1520 537 $user_email = '';
1521 538 $user_ip = $_SERVER['REMOTE_ADDR'];
@@ -1549,374 +566,184 @@
1549 566 wp_die();
1550 567 }
1551 568 }
1552 569
1553 - $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1554 - $this->productCardHtml = '';
1555 - $this->videoEmbedHtml = '';
1556 - // Reset the per-turn function-calling UI capture (plan 48a57a).
1557 - $this->fc_ui_html = '';
1558 - $this->fc_ui_images = array();
1559 - $this->fc_ui_captured = false;
570 +$this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
571 +$this->productCardHtml = '';
1560 572
1561 - // Get the actual WordPress user ID if logged in
1562 - $is_logged_in = is_user_logged_in();
1563 - if ($is_logged_in) {
1564 - $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1565 - } else {
1566 - // For logged-out users, use your existing identifier method
1567 - $user_id = $this->mxchat_get_user_identifier();
1568 - }
573 +// Get the actual WordPress user ID if logged in
574 +$is_logged_in = is_user_logged_in();
575 +if ($is_logged_in) {
576 + $user_id = get_current_user_id(); // This will get the actual WordPress user ID
577 +} else {
578 + // For logged-out users, use your existing identifier method
579 + $user_id = $this->mxchat_get_user_identifier();
580 +}
1569 581
1570 - // Get and sanitize the user identifier
1571 - $user_id = sanitize_key($user_id);
582 +// Get and sanitize the user identifier
583 +$user_id = sanitize_key($user_id);
1572 584
1573 - // Check rate limit using new settings structure
1574 - $rate_limit_result = $this->check_rate_limit();
585 +// Check rate limit using new settings structure
586 +$rate_limit_result = $this->check_rate_limit();
1575 587
1576 - if ($rate_limit_result !== true) {
1577 - wp_send_json([
1578 - 'success' => false,
1579 - 'message' => $rate_limit_result['message'],
1580 - 'status' => 'rate_limit_exceeded'
1581 - ]);
1582 - wp_die();
1583 - }
588 +// Add this at the start of your rate limit checking in mxchat_handle_chat_request()
589 +//error_log('MXChat Rate Limit: Starting rate limit check in handle_chat_request()');
1584 590
591 +// Then right after checking the result:
592 +if ($rate_limit_result !== true) {
593 + //error_log('MXChat Rate Limit: Rate limit exceeded, returning error');
594 + wp_send_json([
595 + 'success' => false,
596 + 'message' => $rate_limit_result['message'],
597 + 'status' => 'rate_limit_exceeded'
598 + ]);
599 + wp_die();
600 +} else {
601 + //error_log('MXChat Rate Limit: Check passed successfully');
602 +}
603 +
1585 604 // Rest of your existing code...
1586 605 $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
606 + //error_log("Session ID: $session_id");
1587 607
1588 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1589 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1590 - // the frontend FormData.append() to stringify a null session_id into the literal
1591 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1592 - // ghost sessions that group every visitor's first message under one row.
1593 - if ($session_id === 'null' || $session_id === 'undefined') {
1594 - $session_id = '';
1595 - }
1596 -
1597 608 if (empty($session_id)) {
609 + //error_log("Error: Session ID is missing.");
1598 610 wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1599 611 wp_die();
1600 612 }
1601 613
1602 - // Update session owner if it changed (e.g. IP changed due to network switch)
1603 - // The session ID itself is the authentication — if the client has it, they own it
1604 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1605 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1606 -
1607 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1608 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1609 - }
1610 -
1611 614 // Validate and sanitize the incoming message
1612 615 if (empty($_POST['message'])) {
616 + //error_log("Error: No message received.");
1613 617 wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1614 618 wp_die();
1615 619 }
1616 620
1617 - // Enforce the configurable max input length (plan a3fae2 part C). 0 = unlimited.
1618 - // Server-side guard backing the textarea's client-side maxlength (which is bypassable).
1619 - // Reads the global core setting and measures characters (mb_strlen on the unslashed
1620 - // raw POST), matching the maxlength semantics.
1621 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
1622 - if ($mxchat_max_input_length > 0) {
1623 - $mxchat_incoming_raw = is_string($_POST['message']) ? wp_unslash($_POST['message']) : '';
1624 - if (mb_strlen($mxchat_incoming_raw) > $mxchat_max_input_length) {
1625 - wp_send_json([
1626 - 'success' => false,
1627 - /* translators: %d: maximum allowed characters */
1628 - 'message' => sprintf(esc_html__('Your message is too long. Please keep it under %d characters.', 'mxchat'), $mxchat_max_input_length),
1629 - 'status' => 'message_too_long'
1630 - ]);
1631 - wp_die();
1632 - }
1633 - }
1634 621
622 +// Modify the message sanitization to preserve PHP tags in code blocks
623 +$allowed_tags = [
624 + 'pre' => [],
625 + 'code' => ['class' => true],
626 + 'span' => ['class' => true],
627 + 'div' => ['class' => true],
628 +];
1635 629
1636 - // Track originating page for first message in session
1637 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
630 +// First preserve code blocks
631 +$message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
632 + return htmlspecialchars_decode($matches[0]);
633 +}, $_POST['message']);
1638 634
1639 - // Check if originating page columns exist
1640 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
635 +// Then apply sanitization
636 +$message = wp_kses($message, $allowed_tags);
1641 637
1642 - if ($columns_exist) {
1643 - // Check if this session already has messages
1644 - $message_count = $wpdb->get_var($wpdb->prepare(
1645 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1646 - $session_id
1647 - ));
1648 -
1649 - // If this is the first message in the session
1650 - if ($message_count == 0) {
1651 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1652 - $originating_url = '';
1653 - $originating_title = '';
1654 -
1655 - // Try to get from POST data first (sent by JavaScript)
1656 - if (isset($_POST['current_page_url'])) {
1657 - $originating_url = esc_url_raw($_POST['current_page_url']);
1658 - $originating_title = isset($_POST['current_page_title'])
1659 - ? sanitize_text_field($_POST['current_page_title'])
1660 - : '';
1661 - }
1662 - // Fallback to HTTP_REFERER if not provided by JavaScript
1663 - else if (isset($_SERVER['HTTP_REFERER'])) {
1664 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1665 - }
1666 -
1667 - // Generate title if we have URL but no title
1668 - if ($originating_url && empty($originating_title)) {
1669 - $parsed_url = parse_url($originating_url);
1670 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1671 -
1672 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1673 - $originating_title = 'Homepage';
1674 - } else {
1675 - // Clean up the path to make a readable title
1676 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1677 - $originating_title = ucwords(trim($originating_title));
1678 - }
1679 - }
1680 -
1681 - // Store for later use when saving the message
1682 - $this->pending_originating_page = [
1683 - 'url' => $originating_url,
1684 - 'title' => $originating_title
1685 - ];
1686 - }
1687 - }
1688 -
1689 -
638 +// Decode code blocks
639 +$message = preg_replace_callback('/(&lt;pre&gt;&lt;code.*?&gt;.*?&lt;\/code&gt;&lt;\/pre&gt;)/s', function($matches) {
640 + return htmlspecialchars_decode($matches[1]);
641 +}, $message);
1690 642
1691 - // Get page context if provided
1692 - $page_context = null;
1693 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1694 - $page_context_raw = stripslashes($_POST['page_context']);
1695 - $page_context = json_decode($page_context_raw, true);
1696 -
1697 - // Validate page context structure
1698 - if (is_array($page_context) &&
1699 - isset($page_context['url']) &&
1700 - isset($page_context['title']) &&
1701 - isset($page_context['content'])) {
1702 -
1703 - // Sanitize page context
1704 - $page_context['url'] = esc_url_raw($page_context['url']);
1705 - $page_context['title'] = sanitize_text_field($page_context['title']);
1706 - $page_context['content'] = wp_kses_post($page_context['content']);
1707 - } else {
1708 - $page_context = null;
1709 - }
1710 - }
643 +$message = trim($message);
1711 644
1712 - // Modify the message sanitization to preserve PHP tags in code blocks
1713 - $allowed_tags = [
1714 - 'pre' => [],
1715 - 'code' => ['class' => true],
1716 - 'span' => ['class' => true],
1717 - 'div' => ['class' => true],
1718 - ];
645 +// Preserve code blocks from markdown conversion
646 +$message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1719 647
1720 - // First preserve code blocks
1721 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1722 - return htmlspecialchars_decode($matches[0]);
1723 - }, $_POST['message']);
648 +// Check if any add-ons want to pre-process this message (for web search etc.)
649 +$pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1724 650
1725 - // Then apply sanitization
1726 - $message = wp_kses($message, $allowed_tags);
651 +// If the pre-processing returned a result (not the original message), use it directly
652 +if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
653 + // Save the AI response
654 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
655 +
656 + // Save HTML content if provided
657 + if (!empty($pre_processed_result['html'])) {
658 + $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
659 + }
660 +
661 + // Return the response
662 + wp_send_json([
663 + 'text' => $pre_processed_result['text'],
664 + 'html' => $pre_processed_result['html'] ?? '',
665 + 'session_id' => $session_id
666 + ]);
667 + wp_die();
668 +}
1727 669
1728 - // Preserve code blocks from markdown conversion
1729 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1730 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
670 + // Save the user's message
671 + $this->mxchat_save_chat_message($session_id, 'user', $message);
1731 672
1732 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1733 - // Always initialize testing data for admins (no toggle needed)
1734 - $testing_data = null;
1735 - if (current_user_can('administrator')) {
1736 - // For vision messages, use the original user message for the query display
1737 - $query_for_testing = $message;
1738 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1739 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1740 - }
1741 -
1742 - $testing_data = [
1743 - 'query' => $query_for_testing,
1744 - 'timestamp' => time(),
1745 - 'top_matches' => [],
1746 - 'action_matches' => [], // Initialize action matches array
1747 - 'page_context' => $page_context, // Include page context in testing data
1748 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1749 - 'bot_id' => $bot_id // Include bot ID in testing data
1750 - ];
1751 -
1752 - // Get similarity threshold from bot options or default options
1753 - $similarity_threshold = isset($current_options['similarity_threshold'])
1754 - ? ((int) $current_options['similarity_threshold']) / 100
1755 - : 0.35;
1756 -
1757 - $testing_data['similarity_threshold'] = $similarity_threshold;
1758 -
1759 - // Determine knowledge base type using bot-specific config
1760 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1761 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1762 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1763 - }
1764 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
673 + // Check if the message is an email address
674 + if (is_email($message)) {
675 + // Add the email to Loops
676 + $this->add_email_to_loops($message);
1765 677
1766 - // Add debug before and after:
1767 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1768 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1769 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
678 + // Send success response
679 + $response_message = $this->options['email_capture_response'] ??
680 + esc_html__('Thank you! Your coupon is on the way!', 'mxchat');
1770 681
682 + wp_send_json([
683 + 'success' => true,
684 + 'status' => 'email_captured',
685 + 'message' => $response_message
686 + ]);
687 + wp_die();
688 + }
1771 689
1772 - // If the pre-processing returned a result (not the original message), use it directly
1773 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1774 - // Save the AI response
1775 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1776 -
1777 - // Save HTML content if provided
1778 - if (!empty($pre_processed_result['html'])) {
1779 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1780 - }
1781 -
1782 - // Add testing data if admin
1783 - $response_data = [
1784 - 'text' => $pre_processed_result['text'],
1785 - 'html' => $pre_processed_result['html'] ?? '',
1786 - 'session_id' => $session_id
1787 - ];
1788 -
1789 - if ($testing_data !== null) {
1790 - $response_data['testing_data'] = $testing_data;
1791 - }
1792 -
1793 - wp_send_json($response_data);
1794 - wp_die();
1795 - }
690 + $intent_info = '';
1796 691
1797 - // Save the user's message - handle vision processed messages differently
1798 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1799 - // For vision messages, save the original user message with image indicator
1800 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1801 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1802 - $image_count = intval($_POST['vision_images_count']);
1803 - $original_message .= " [{$image_count} image(s)]";
1804 - }
1805 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1806 - } else {
1807 - // Regular message - save as normal
1808 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1809 - }
692 + // Check chat mode
693 + $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
694 + //error_log("Chat Mode: $chat_mode");
1810 695
1811 -
1812 - if (is_email($message)) {
1813 - // Add the email to Loops
1814 - $this->add_email_to_loops($message);
1815 -
1816 - // Get the user's success message instruction using current_options
1817 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1818 -
1819 - // Set instruction for AI using the user's success message
1820 - $this->current_action_instruction = $user_success_message;
1821 -
1822 - // Clear the email capture transient since we got the email
1823 - delete_transient('mxchat_email_capture_' . $user_id);
1824 - }
1825 -
1826 - // Check if we're in an email capture flow but user hasn't provided email yet
1827 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1828 - // Check if the message contains an email (not the whole message being an email)
1829 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1830 - $extracted_email = $matches[0];
1831 -
1832 - // Add the extracted email to Loops
1833 - $this->add_email_to_loops($extracted_email);
1834 -
1835 - // Get the user's success message instruction using current_options
1836 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1837 -
1838 - // Set instruction for AI using the user's success message
1839 - $this->current_action_instruction = $user_success_message;
1840 -
1841 - // Clear the email capture transient since we got the email
1842 - delete_transient('mxchat_email_capture_' . $user_id);
1843 - }
1844 - // If no email found but we're in capture mode, remind them
1845 - else {
1846 - // Get the original instruction to remind them using current_options
1847 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1848 - $this->current_action_instruction = $original_instruction;
1849 - }
1850 - }
696 + // Handle agent mode
697 + if ($chat_mode === 'agent') {
698 + // First, check for switch intent before doing anything else
699 + $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1851 700
1852 - $intent_info = '';
701 + // If we matched an intent and it's the switch intent, handle it
702 + if ($intent_matched && !empty($this->fallbackResponse['text'])) {
703 + //error_log("Switch to chatbot intent detected");
1853 704
1854 - // Check chat mode
1855 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
705 + // Update chat mode first
706 + update_option("mxchat_mode_{$session_id}", 'ai');
1856 707
1857 - // Handle agent mode
1858 - // Handle agent mode
1859 - if ($chat_mode === 'agent') {
1860 - // First, check for switch intent before doing anything else
1861 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
708 + // Clear any existing PDF context to start fresh
709 + $this->clear_pdf_transients($session_id);
1862 710
1863 - // Capture action analysis for testing panel after intent check
1864 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1865 - $testing_data['action_matches'] = $this->last_action_analysis;
1866 - }
1867 -
1868 - // Around line 506, in the agent mode handling section:
1869 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1870 - // Update chat mode first
1871 - update_option("mxchat_mode_{$session_id}", 'ai');
1872 -
1873 - // Clear any existing PDF context to start fresh
1874 - $this->clear_pdf_transients($session_id);
1875 -
1876 - // Prepare clean switch response with explicit chat_mode
1877 - $response_data = [
1878 - 'text' => $this->fallbackResponse['text'],
1879 - 'html' => $this->fallbackResponse['html'] ?? '',
1880 - 'session_id' => $session_id,
1881 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1882 - ];
1883 -
1884 - if ($testing_data !== null) {
1885 - $response_data['testing_data'] = $testing_data;
1886 - }
1887 -
1888 - // Save the mode switch message
1889 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1890 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1891 -
1892 - // Send response and exit
1893 - wp_send_json($response_data);
1894 - wp_die();
1895 - } elseif (!$intent_matched) {
1896 - // No intent matched, handle live agent message
1897 - try {
1898 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
711 + // Prepare clean switch response
712 + $response_data = [
713 + 'text' => $this->fallbackResponse['text'],
714 + 'html' => '',
715 + 'session_id' => $session_id,
716 + 'chat_mode' => 'ai'
717 + ];
1899 718
1900 - $agent_response = [
1901 - 'status' => 'waiting_for_agent',
1902 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1903 - ];
1904 -
1905 - if ($testing_data !== null) {
1906 - $agent_response['testing_data'] = $testing_data;
1907 - }
719 + // Save the mode switch message
720 + $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
721 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1908 722
1909 - wp_send_json_success($agent_response);
1910 - } catch (\Exception $e) {
1911 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1912 - }
1913 - wp_die();
723 + // Send response and exit
724 + wp_send_json($response_data);
725 + wp_die();
726 + } elseif (!$intent_matched) {
727 + // No intent matched, handle live agent message
728 + try {
729 + $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
730 + //error_log("Message sent to agent.");
731 +
732 + wp_send_json_success([
733 + 'status' => 'waiting_for_agent',
734 + 'message' => esc_html__('Message sent to live agent.', 'mxchat')
735 + ]);
736 + } catch (\Exception $e) {
737 + //error_log("Error sending message to agent: " . $e->getMessage());
738 + wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1914 739 }
740 + wp_die();
1915 741 }
742 + }
1916 743
1917 744 // Step 1: Check for new PDF URL in the message
1918 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
745 + if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1919 746 $new_pdf_url = $matches[0];
1920 747
1921 748 // Check if this is likely a PDF-related request
1922 749 $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
@@ -1938,15 +765,15 @@
1938 765
1939 766 // Clear previous PDF transients
1940 767 $this->clear_pdf_transients($session_id);
1941 768
1942 - // Process new PDF using current_options
1943 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
769 + // Process new PDF
770 + $max_pages = $this->options['pdf_max_pages'] ?? 69;
1944 771 $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1945 772
1946 773 if ($embeddings === 'too_many_pages') {
1947 774 $error_text = sprintf(
1948 - $current_options['pdf_intent_error_text'] ??
775 + $this->options['pdf_intent_error_text'] ??
1949 776 esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1950 777 $max_pages
1951 778 );
1952 779 $this->fallbackResponse['text'] = $error_text;
@@ -1951,13 +778,15 @@
1951 778 );
1952 779 $this->fallbackResponse['text'] = $error_text;
1953 780 } elseif ($embeddings) {
1954 781 // Store new PDF information
782 + // Create a more meaningful filename from URL
1955 783 $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1956 784
1957 - // If the filename is generic, create a more descriptive one
785 + // If the filename is generic (like results_download.php), create a more descriptive one
1958 786 if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1959 787 strpos($pdf_filename, '.php') !== false) {
788 + // Create a timestamp-based name
1960 789 $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1961 790 }
1962 791
1963 792 set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
@@ -1964,257 +793,155 @@
1964 793 set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1965 794 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1966 795 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1967 796
1968 - $success_text = $current_options['pdf_intent_success_text'] ??
797 + $success_text = $this->options['pdf_intent_success_text'] ??
1969 798 esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1970 799
1971 - $pdf_response = [
800 + // Return success with filename for UI update
801 + wp_send_json([
1972 802 'success' => true,
1973 803 'message' => $success_text,
1974 804 'data' => [
1975 805 'filename' => $pdf_filename
1976 806 ]
1977 - ];
1978 -
1979 - if ($testing_data !== null) {
1980 - $pdf_response['testing_data'] = $testing_data;
1981 - }
1982 -
1983 - wp_send_json($pdf_response);
807 + ]);
1984 808 wp_die();
1985 809 } else {
1986 - $error_text = $current_options['pdf_intent_error_text'] ??
810 + $error_text = $this->options['pdf_intent_error_text'] ??
1987 811 esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1988 812 $this->fallbackResponse['text'] = $error_text;
1989 813 }
1990 814
1991 - $pdf_error_response = [
815 + wp_send_json([
1992 816 'success' => false,
1993 817 'message' => $this->fallbackResponse['text']
1994 - ];
1995 -
1996 - if ($testing_data !== null) {
1997 - $pdf_error_response['testing_data'] = $testing_data;
1998 - }
1999 -
2000 - wp_send_json($pdf_error_response);
818 + ]);
2001 819 wp_die();
2002 820 }
2003 821 }
2004 822 }
2005 -
2006 -
2007 - // Step 2: Detect intent and handle intent-based responses
2008 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
2009 -
2010 - // Capture action analysis for testing panel after intent check
2011 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
2012 - $testing_data['action_matches'] = $this->last_action_analysis;
823 +
824 +
825 +// Add this before the intent check section (before Step 2) in mxchat_handle_chat_request
826 +// Check if there's an active recommendation flow session
827 +$flow_state = get_option("mxchat_sr_flow_state_{$session_id}", array());
828 +if (!empty($flow_state) && isset($flow_state['flow_id'])) {
829 + //error_log('MXCHAT DEBUG: Detected active recommendation flow, routing directly');
830 +
831 + // Create a dummy intent object that matches the original intent
832 + $dummy_intent = new stdClass();
833 + $dummy_intent->intent_label = 'Recommendation Flow ' . $flow_state['flow_id'];
834 + $dummy_intent->phrases = ''; // Empty phrases to avoid matching the original trigger
835 +
836 + // Call the recommendation flow handler directly
837 + $response_data = apply_filters('mxchat_sr_recommendation_flow', false, $message, $user_id, $session_id, $dummy_intent);
838 +
839 + // If the handler returned a response, send it
840 + if (is_array($response_data) && (isset($response_data['text']) || isset($response_data['html']))) {
841 + // Save the bot's response to the chat history
842 + if (!empty($response_data['text'])) {
843 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['text']);
2013 844 }
2014 -
2015 - // Step 3: Handle the intent result appropriately
2016 - if ($intent_result !== false) {
2017 - // Intent was matched - ALWAYS send as JSON response, never streaming
2018 -
2019 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
2020 - // Intent returned a direct response array
2021 - $response_data = [
2022 - 'text' => $intent_result['text'] ?? '',
2023 - 'html' => $intent_result['html'] ?? '',
2024 - 'session_id' => $session_id
2025 - ];
2026 -
2027 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
2028 - if (isset($intent_result['chat_mode'])) {
2029 - $response_data['chat_mode'] = $intent_result['chat_mode'];
2030 - }
2031 -
2032 - if ($testing_data !== null) {
2033 - $response_data['testing_data'] = $testing_data;
2034 - }
2035 -
2036 - wp_send_json($response_data);
2037 - wp_die();
2038 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
2039 - // Intent returned true and set fallbackResponse
2040 -
2041 - // SAVE TO TRANSCRIPT
2042 - if (!empty($this->fallbackResponse['text'])) {
2043 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
2044 - }
2045 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
2046 - if (!empty($this->fallbackResponse['html'])) {
2047 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2048 - }
2049 -
2050 - $response_data = [
2051 - 'text' => $this->fallbackResponse['text'] ?? '',
2052 - 'html' => $this->fallbackResponse['html'] ?? '',
2053 - 'session_id' => $session_id
2054 - ];
2055 -
2056 - if (isset($this->fallbackResponse['chat_mode'])) {
2057 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
2058 - }
2059 -
2060 - if ($testing_data !== null) {
2061 - $response_data['testing_data'] = $testing_data;
2062 - }
2063 -
2064 - wp_send_json($response_data);
2065 - wp_die();
2066 - }
845 + if (!empty($response_data['html'])) {
846 + $this->mxchat_save_chat_message($session_id, 'bot', $response_data['html']);
2067 847 }
848 +
849 + // Send the response
850 + wp_send_json($response_data);
851 + wp_die();
852 + }
853 +
854 + // If we reach here, the flow handler didn't provide a usable response
855 + // We'll continue with regular processing
856 + //error_log('MXCHAT DEBUG: Recommendation flow handler did not provide a usable response');
857 +}
2068 858
2069 - // If we get here, no intent matched OR the intent didn't provide a usable response
859 + // Step 2: Detect intent and handle intent-based responses
860 +$intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
861 +//error_log("Intent Result Type: " . gettype($intent_result));
2070 862
2071 - // Step 4: Generate AI response
2072 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
2073 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
2074 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
2075 - $this->mxchat_increment_chat_count();
863 +// Step 3: Handle the intent result appropriately
864 +if ($intent_result !== false) {
865 + // The intent was matched and handled
866 + //error_log("Intent was matched and handled.");
867 +
868 + if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
869 + // Intent returned a direct response array
870 + //error_log("Intent returned a direct response.");
871 + $response_data = [
872 + 'text' => $intent_result['text'] ?? '',
873 + 'html' => $intent_result['html'] ?? '',
874 + 'session_id' => $session_id
875 + ];
2076 876
2077 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
2078 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
2079 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
877 + wp_send_json($response_data);
878 + wp_die();
879 + }
880 + else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
881 + // Intent returned true and set fallbackResponse
882 + //error_log("Intent returned true with fallbackResponse set.");
883 + $response_data = [
884 + 'text' => $this->fallbackResponse['text'] ?? '',
885 + 'html' => $this->fallbackResponse['html'] ?? '',
886 + 'session_id' => $session_id
887 + ];
2080 888
2081 - // Check if the embedding generation returned an error
2082 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
2083 - $error_message = $user_message_embedding['error'];
2084 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
889 + wp_send_json($response_data);
890 + wp_die();
891 + }
892 +
893 + // Intent was matched but no usable response was provided
894 + // This shouldn't happen with proper intent implementation
895 + //error_log("Warning: Intent matched but no response provided.");
896 +}
2085 897
2086 - // FIXED: Send error in appropriate format based on streaming mode
2087 - if ($is_streaming) {
2088 - echo "data: " . json_encode([
2089 - 'error' => true,
2090 - 'error_message' => $error_message,
2091 - 'error_code' => $error_code,
2092 - 'text' => $error_message,
2093 - 'message' => $error_message
2094 - ]) . "\n\n";
2095 - echo "data: [DONE]\n\n";
2096 - flush();
2097 - } else {
898 + // If we get here, no intent matched OR the intent didn't provide a usable response
899 + //error_log("No matching intent or usable response. Generating AI response.");
900 +
901 + // Step 4: Generate AI response
902 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id);
903 + $this->mxchat_increment_chat_count();
904 +
905 + // Generate embedding for the user's query
906 + $user_message_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
907 +
908 + // Check if the embedding generation returned an error
909 + if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
910 + $error_message = $user_message_embedding['error'];
911 + $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
912 +
913 + //error_log("Embedding error for session $session_id: $error_message (Code: $error_code)");
914 +
915 + // Important: Structure the error data correctly for wp_send_json_error
2098 916 wp_send_json_error([
2099 917 'error_message' => $error_message,
2100 918 'error_code' => $error_code
2101 919 ]);
920 + wp_die();
2102 921 }
2103 - wp_die();
2104 - }
2105 -
2106 - // Check if the embedding is valid
2107 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2108 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2109 -
2110 - // FIXED: Send error in appropriate format based on streaming mode
2111 - if ($is_streaming) {
2112 - echo "data: " . json_encode([
2113 - 'error' => true,
2114 - 'error_message' => $error_message,
2115 - 'error_code' => 'invalid_embedding',
2116 - 'text' => $error_message,
2117 - 'message' => $error_message
2118 - ]) . "\n\n";
2119 - echo "data: [DONE]\n\n";
2120 - flush();
2121 - } else {
922 +
923 + // Check if the embedding is valid
924 + if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
925 + //error_log("Failed to generate message embedding for session $session_id");
2122 926 wp_send_json_error([
2123 - 'error_message' => $error_message,
927 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
2124 928 'error_code' => 'invalid_embedding'
2125 929 ]);
930 + wp_die();
2126 931 }
2127 - wp_die();
2128 - }
2129 932
2130 933 // Build context with both knowledge base and PDF content if available
2131 934 $context_content = "User asked: '{$message}'\n\n";
2132 -
2133 - // Add action instruction if present (add this right after the above line)
2134 - if (!empty($this->current_action_instruction)) {
2135 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2136 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2137 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2138 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2139 -
2140 - // Clear the instruction after using it
2141 - $this->current_action_instruction = null;
2142 - }
2143 935
2144 936
2145 - // Add page context if available and contextual awareness is enabled using current_options
2146 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2147 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2148 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2149 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2150 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2151 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2152 - }
937 + // Get relevant content from knowledge base
938 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
939 + if (!empty($relevant_content)) {
940 + $context_content .= "Relevant content from knowledge database:\n" . $relevant_content . "\n\n";
941 + }
2153 942
2154 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2155 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2156 -
2157 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2158 - // Use fresh options to ensure we get the latest setting value
2159 - $fresh_options = get_option('mxchat_options', []);
2160 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2161 943
2162 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2163 - if ($citation_links_enabled && !empty($system_instructions)) {
2164 - preg_match_all(
2165 - '#\bhttps?://[^\s<>"\']+#i',
2166 - $system_instructions,
2167 - $system_instruction_urls
2168 - );
2169 -
2170 - if (!empty($system_instruction_urls[0])) {
2171 - // Merge with existing valid URLs
2172 - $this->current_valid_urls = array_merge(
2173 - $this->current_valid_urls,
2174 - $system_instruction_urls[0]
2175 - );
2176 - // Remove duplicates
2177 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2178 -
2179 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2180 - }
2181 - }
2182 -
2183 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2184 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2185 - // Update testing data with the REAL similarity analysis
2186 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2187 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2188 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2189 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2190 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2191 -}
2192 -// ===== END SIMILARITY DATA CAPTURE =====
2193 -
2194 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2195 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2196 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2197 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2198 -}
2199 -
2200 - if (!empty($relevant_content)) {
2201 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2202 - } else {
2203 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2204 - }
2205 -
2206 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2207 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2208 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2209 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2210 - foreach ($this->current_valid_urls as $url) {
2211 - $context_content .= "- " . $url . "\n";
2212 - }
2213 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2214 - $context_content .= "===== END APPROVED URLS =====\n\n";
2215 - }
2216 -
2217 944 // Check for and include PDF content
2218 945 $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2219 946 $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2220 947 $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
@@ -2245,143 +972,24 @@
2245 972 }
2246 973
2247 974 $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2248 975
2249 - // Extract model from current options for bot-specific model support
2250 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2251 -
2252 - // ===== Native function-calling fallback (plan-mxchat-20260617-a41dee) =====
2253 - // Intents already missed (we're past the intent router). If function
2254 - // calling is enabled and the active model is tool-capable, let the model
2255 - // SELECT and run registered callbacks as tools — independent of intents,
2256 - // works with zero Actions. The tool round is buffered; the final answer is
2257 - // emitted via the SAME envelopes the normal path uses. Default-off, so
2258 - // existing installs never enter this branch.
2259 - if ($this->mxchat_fc_should_run($selected_model)) {
2260 - $fc_outcome = $this->mxchat_fc_attempt(
2261 - $message,
2262 - $context_content,
2263 - $conversation_history,
2264 - $selected_model,
2265 - $current_options,
2266 - $session_id,
2267 - $user_id
2268 - );
2269 - if (is_array($fc_outcome) && !empty($fc_outcome['handled'])) {
2270 - $fc_text = isset($fc_outcome['text']) ? $fc_outcome['text'] : '';
2271 - if (!empty($this->current_valid_urls)) {
2272 - $fc_text = $this->validate_and_clean_urls($fc_text, $this->current_valid_urls, $session_id, $bot_id);
2273 - }
2274 - // plan-mxchat-20260617-48a57a — surface any UI element a tool
2275 - // produced (generated image / product card / image gallery) so the
2276 - // widget RENDERS it, instead of emitting only the model's text.
2277 - // The html was already saved to the transcript in
2278 - // mxchat_fc_execute_tool (or by the callback itself for self-saving
2279 - // core tools), so we persist ONLY the model's caption text here.
2280 - $fc_html = isset($this->fc_ui_html) ? $this->fc_ui_html : '';
2281 -
2282 - if ($fc_text !== '') {
2283 - $this->mxchat_save_chat_message($session_id, 'bot', $fc_text, null, null);
2284 - }
2285 -
2286 - // A video-backed KB source queued during retrieval (03ba33) must
2287 - // surface on the FC path too — the FC envelopes below are the ONLY
2288 - // exit for this turn, so append it to the html channel and persist
2289 - // it (tool html was already saved in mxchat_fc_execute_tool; the
2290 - // video embed has no other save point on this path).
2291 - if (!empty($this->videoEmbedHtml)) {
2292 - $fc_html .= $this->videoEmbedHtml;
2293 - $this->mxchat_save_chat_message($session_id, 'bot', $this->videoEmbedHtml);
2294 - }
2295 -
2296 - if ($is_streaming) {
2297 - // The frontend SSE reader routes any event carrying text/html
2298 - // to handleNonStreamResponse(), which renders text + html in a
2299 - // single bot message — so emit one complete event (mirrors the
2300 - // intent path's text/html envelope).
2301 - $sse = array('session_id' => $session_id);
2302 - if ($fc_text !== '') $sse['text'] = $fc_text;
2303 - if ($fc_html !== '') $sse['html'] = $fc_html;
2304 - if ($fc_text === '' && $fc_html === '') $sse['text'] = $this->mxchat_fc_giveup_text();
2305 - echo "data: " . wp_json_encode($sse) . "\n\n";
2306 - echo "data: [DONE]\n\n";
2307 - flush();
2308 - } else {
2309 - $fc_response_data = array('text' => $fc_text, 'html' => $fc_html, 'session_id' => $session_id);
2310 - if ($testing_data !== null) {
2311 - $fc_response_data['testing_data'] = $testing_data;
2312 - }
2313 - wp_send_json($fc_response_data);
2314 - }
2315 - wp_die();
2316 - }
2317 - }
2318 - // ===== end function-calling fallback =====
2319 -
2320 - // Streaming + a queued video embed (03ba33): the provider handlers own the
2321 - // token stream and the [DONE] terminator, so the embed rides a dedicated
2322 - // append_html SSE event emitted BEFORE the stream starts. The client
2323 - // stashes it and appends it as its own bot bubble after [DONE] — old
2324 - // cached widget JS simply ignores the unknown key (no content/text/html/
2325 - // error field, so no branch matches). Transcript save happens after the
2326 - // stream completes, so history order matches the live order (text, then
2327 - // embed).
2328 - if ($is_streaming && !empty($this->videoEmbedHtml)) {
2329 - echo "data: " . wp_json_encode(array(
2330 - 'append_html' => $this->videoEmbedHtml,
2331 - 'session_id' => $session_id,
2332 - )) . "\n\n";
2333 - flush();
2334 - }
2335 -
976 + // Generate the response using the full context
2336 977 $response = $this->mxchat_generate_response(
2337 978 $context_content,
2338 - $current_options['api_key'] ?? $this->options['api_key'],
2339 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2340 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2341 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2342 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2343 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2344 - $conversation_history,
2345 - $is_streaming,
2346 - $session_id,
2347 - $testing_data,
2348 - $selected_model
979 + $this->options['api_key'],
980 + $this->options['xai_api_key'],
981 + $this->options['claude_api_key'],
982 + $this->options['deepseek_api_key'],
983 + $this->options['gemini_api_key'],
984 + $conversation_history
2349 985 );
2350 986
2351 - // Handle streaming vs non-streaming responses
2352 - if ($is_streaming) {
2353 - // Check if streaming actually happened or if it fell back to regular response
2354 - if ($response === true) {
2355 - // Persist the video embed AFTER the provider saved the streamed
2356 - // text, so history replays in the same order the visitor saw
2357 - // (text bubble, then embed bubble). See 03ba33.
2358 - if (!empty($this->videoEmbedHtml)) {
2359 - $this->mxchat_save_chat_message($session_id, 'bot', $this->videoEmbedHtml);
2360 - }
2361 - wp_die();
2362 - }
2363 - // If we get here, streaming fell back to regular response, continue
2364 - // But if there's an error, we need to send it as SSE format since headers are already set
2365 - if (is_array($response) && isset($response['error'])) {
2366 - $error_message = $response['error'];
2367 - $error_code = $response['error_code'] ?? 'api_error';
2368 - // Send error in SSE format that the client JS can handle
2369 - echo "data: " . json_encode([
2370 - 'error' => true,
2371 - 'error_message' => $error_message,
2372 - 'error_code' => $error_code,
2373 - 'text' => $error_message, // Also include as text for fallback handling
2374 - 'message' => $error_message
2375 - ]) . "\n\n";
2376 - echo "data: [DONE]\n\n";
2377 - flush();
2378 - wp_die();
2379 - }
2380 - }
2381 -
2382 - // Check if the response is an error array (non-streaming mode)
987 + // Check if the response is an error array
2383 988 if (is_array($response) && isset($response['error'])) {
989 + //error_log("AI Response Error: " . $response['error'] . " (Code: " . ($response['error_code'] ?? 'unknown') . ")");
990 +
991 + // Send a user-friendly error message
2384 992 wp_send_json_error([
2385 993 'error_message' => $response['error'],
2386 994 'error_code' => $response['error_code'] ?? 'api_error'
2387 995 ]);
@@ -2387,247 +995,77 @@
2387 995 ]);
2388 996 wp_die();
2389 997 }
2390 998
2391 - // DEBUG: Check what we have
2392 - //error_log("=== BEFORE URL VALIDATION ===");
2393 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2394 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2395 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2396 -
2397 - // If we get here, the response is valid text - now validate URLs
2398 - if (!empty($this->current_valid_urls)) {
2399 - //error_log("CALLING validate_and_clean_urls");
2400 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls, $session_id, $bot_id);
2401 - } else {
2402 - //error_log("SKIPPING validation - current_valid_urls is empty");
2403 - }
2404 - // ===== END URL VALIDATION =====
999 + // If we get here, the response is valid text
1000 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
2405 1001
2406 - // Prepare RAG context data for storage (only include documents used for context)
2407 - $rag_context_for_storage = null;
2408 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2409 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2410 -
2411 - if ($has_rag_data || $has_action_data) {
2412 - $rag_context_for_storage = [];
2413 -
2414 - // Add RAG/source data if available
2415 - if ($has_rag_data) {
2416 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2417 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2418 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2419 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2420 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2421 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2422 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2423 - }
2424 -
2425 - // Add action analysis data if available
2426 - if ($has_action_data) {
2427 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2428 - }
2429 - }
2430 -
2431 - // Save the cleaned response with RAG context
2432 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2433 -
2434 - // Step 5: Save additional content if available
2435 - if (!empty($this->productCardHtml)) {
2436 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2437 - }
2438 -
2439 - if (!empty($this->fallbackResponse['html'])) {
2440 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2441 - }
2442 -
2443 - if (!empty($this->videoEmbedHtml)) {
2444 - $this->mxchat_save_chat_message($session_id, 'bot', $this->videoEmbedHtml);
2445 - }
2446 -
2447 - // Step 6: Return the response
2448 - // DEBUG: Check if newlines exist in the response
2449 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2450 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2451 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2452 -
2453 - // Product cards and action html keep their existing either/or precedence;
2454 - // a queued video embed (03ba33) is APPENDED so it can coexist with both.
2455 - $additional_html = !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? '');
2456 - if (!empty($this->videoEmbedHtml)) {
2457 - $additional_html .= $this->videoEmbedHtml;
2458 - }
2459 -
2460 - $response_data = [
2461 - 'text' => $response,
2462 - 'html' => $additional_html,
2463 - 'session_id' => $session_id
2464 - ];
2465 -
2466 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2467 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2468 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2469 - }
2470 -
2471 - // Also pass it as a top-level field so JS can show a better error message to admins
2472 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2473 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2474 - }
2475 -
2476 - // Always add testing data for admins (no toggle needed)
2477 - if ($testing_data !== null) {
2478 - $response_data['testing_data'] = $testing_data;
2479 - }
2480 -
2481 - wp_send_json($response_data);
2482 - wp_die();
2483 -}
2484 -
2485 -/**
2486 - * Get bot-specific options for multi-bot functionality
2487 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2488 - */
2489 -// Also debug the bot options retrieval
2490 -private function get_bot_options($bot_id = 'default') {
2491 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2492 -
2493 - // The admin Testing tab renders the real widget as bot_id "testing", which
2494 - // is not a registered multi-bot. It must resolve the DEFAULT bot's config
2495 - // so the Testing chat behaves exactly like the front-end (same precedent
2496 - // as the Actions enabled_bots check).
2497 - if ($bot_id === 'testing') {
2498 - $bot_id = 'default';
1002 + // Step 5: Save additional content if available
1003 + if (!empty($this->productCardHtml)) {
1004 + $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2499 1005 }
2500 1006
2501 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2502 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2503 - return array();
1007 + if (!empty($this->fallbackResponse['html'])) {
1008 + $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2504 1009 }
2505 -
2506 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2507 -
2508 - if (!empty($bot_options)) {
2509 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2510 - if (isset($bot_options['similarity_threshold'])) {
2511 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2512 - }
2513 - }
2514 -
2515 - return is_array($bot_options) ? $bot_options : array();
2516 -}
2517 1010
2518 -/**
2519 - * Get bot-specific Pinecone configuration
2520 - * Used in the knowledge retrieval functions
2521 - */
2522 -// Also add debugging to your get_bot_pinecone_config function
2523 -private function get_bot_pinecone_config($bot_id = 'default') {
2524 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
1011 + // Step 6: Return the response
1012 + $response_data = [
1013 + 'text' => $response,
1014 + 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
1015 + 'session_id' => $session_id
1016 + ];
2525 1017
2526 - // Admin Testing tab bot → resolve the DEFAULT bot's backend. Without this,
2527 - // on a multi-bot + Pinecone site the filter below gets an unknown bot id
2528 - // with an EMPTY default, returns array(), and the dispatcher silently
2529 - // searches the WordPress DB while the front-end searches Pinecone — the
2530 - // Testing panel then reports similarity results from a different KB.
2531 - if ($bot_id === 'testing') {
2532 - $bot_id = 'default';
2533 - }
2534 -
2535 - // If default bot or multi-bot add-on not active, use default Pinecone config
2536 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2537 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2538 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2539 - $config = array(
2540 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2541 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2542 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2543 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2544 - );
2545 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2546 - return $config;
2547 - }
2548 -
2549 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2550 -
2551 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2552 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2553 -
2554 - if (!empty($bot_pinecone_config)) {
2555 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2556 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2557 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2558 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2559 - } else {
2560 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2561 - }
2562 -
2563 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
1018 + wp_send_json($response_data);
1019 + wp_die();
2564 1020 }
2565 1021
2566 -
2567 1022 // Updated function to check intents and invoke the callback function
2568 1023 private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2569 1024 global $wpdb;
2570 1025 $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2571 1026
2572 - // Get the current bot_id
2573 - $current_bot_id = $this->get_current_bot_id($session_id);
1027 + //error_log('🔍 MXCHAT DEBUG: Intent Check Started ==================');
1028 + //error_log("🔍 MXCHAT DEBUG: Message: '$message'");
1029 + //error_log("🔍 MXCHAT DEBUG: Chat Mode: $chat_mode");
2574 1030
2575 1031 // Generate the user embedding
1032 + //error_log('🔄 MXCHAT DEBUG: Generating user embedding');
2576 1033 $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2577 -
1034 +
2578 1035 // Check if embedding generation returned an error
2579 1036 if (is_array($user_embedding) && isset($user_embedding['error'])) {
2580 1037 $error_message = $user_embedding['error'];
2581 1038 $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2582 -
2583 - // FIXED: Send error in appropriate format based on streaming mode
2584 - if ($this->is_streaming) {
2585 - echo "data: " . json_encode([
2586 - 'error' => true,
2587 - 'error_message' => $error_message,
2588 - 'error_code' => $error_code,
2589 - 'text' => $error_message,
2590 - 'message' => $error_message
2591 - ]) . "\n\n";
2592 - echo "data: [DONE]\n\n";
2593 - flush();
2594 - } else {
2595 - wp_send_json_error([
2596 - 'error_message' => $error_message,
2597 - 'error_code' => $error_code
2598 - ]);
2599 - }
1039 +
1040 + //error_log("❌ MXCHAT DEBUG: Embedding error: $error_message (Code: $error_code)");
1041 +
1042 + // Send the error to the frontend
1043 + wp_send_json_error([
1044 + 'error_message' => $error_message,
1045 + 'error_code' => $error_code
1046 + ]);
2600 1047 wp_die();
2601 1048 }
2602 -
2603 - // Check if embedding is valid
1049 +
1050 + // Check if embedding is valid (not an error and is an array)
2604 1051 if (!is_array($user_embedding) || empty($user_embedding)) {
2605 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2606 -
2607 - // FIXED: Send error in appropriate format based on streaming mode
2608 - if ($this->is_streaming) {
2609 - echo "data: " . json_encode([
2610 - 'error' => true,
2611 - 'error_message' => $error_message,
2612 - 'error_code' => 'invalid_embedding',
2613 - 'text' => $error_message,
2614 - 'message' => $error_message
2615 - ]) . "\n\n";
2616 - echo "data: [DONE]\n\n";
2617 - flush();
2618 - } else {
2619 - wp_send_json_error([
2620 - 'error_message' => $error_message,
2621 - 'error_code' => 'invalid_embedding'
2622 - ]);
2623 - }
1052 + //error_log('❌ MXCHAT DEBUG: Failed to generate user embedding');
1053 +
1054 + // Send a generic error to the frontend
1055 + wp_send_json_error([
1056 + 'error_message' => esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat'),
1057 + 'error_code' => 'invalid_embedding'
1058 + ]);
2624 1059 wp_die();
2625 1060 }
2626 -
1061 +
1062 + //error_log('✅ MXCHAT DEBUG: User embedding generated successfully');
1063 +
2627 1064 // Fetch intents from the database
2628 1065 $table_name = $wpdb->prefix . 'mxchat_intents';
2629 1066 if ($chat_mode === 'agent') {
1067 + //error_log('🔍 MXCHAT DEBUG: Agent mode - fetching only chatbot switch intent');
2630 1068 $query = $wpdb->prepare(
2631 1069 "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2632 1070 'mxchat_handle_switch_to_chatbot_intent'
2633 1071 );
@@ -2632,139 +1070,78 @@
2632 1070 'mxchat_handle_switch_to_chatbot_intent'
2633 1071 );
2634 1072 $intents = $wpdb->get_results($query);
2635 1073 } else {
1074 + //error_log('🔍 MXCHAT DEBUG: AI mode - fetching all enabled intents');
1075 + // Only fetch enabled intents (either explicitly enabled with 1 or implicitly enabled with NULL for backward compatibility)
2636 1076 $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2637 1077 }
2638 -
1078 +
1079 + //error_log('🔍 MXCHAT DEBUG: Found ' . count($intents) . ' enabled intents to check');
1080 +
2639 1081 if (empty($intents)) {
1082 + //error_log('❌ MXCHAT DEBUG: No enabled intents found in database');
2640 1083 return false;
2641 1084 }
2642 -
2643 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2644 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2645 - $phrases_by_intent = [];
2646 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2647 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2648 - foreach ($all_phrases as $p) {
2649 - $phrases_by_intent[$p->intent_id][] = $p;
2650 - }
2651 - }
2652 -
1085 +
2653 1086 $highest_similarity = -INF;
2654 1087 $matched_intent = null;
2655 -
2656 - // Array to store action analysis for testing panel
2657 - $action_analysis = [];
2658 -
1088 +
1089 + //error_log('📊 MXCHAT DEBUG: Intent Similarity Scores ==================');
2659 1090 foreach ($intents as $intent) {
2660 - // Additional check for enabled state
1091 + //error_log("🔄 MXCHAT DEBUG: Checking intent: '{$intent->intent_label}' (callback: {$intent->callback_function})");
1092 +
1093 + // Additional check for enabled state in case database structure was modified
2661 1094 $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2662 1095 if (!$is_enabled) {
1096 + //error_log("⚠️ MXCHAT DEBUG: Skipping disabled intent: {$intent->intent_label}");
2663 1097 continue;
2664 1098 }
2665 -
2666 - // Check if this action is enabled for the current bot
2667 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2668 - continue;
2669 - }
2670 -
2671 - $best_similarity = -INF;
2672 - $matched_phrase_text = '';
2673 -
2674 - // Check legacy embedding vector (existing behavior)
1099 +
2675 1100 $intent_embedding_serialized = $intent->embedding_vector;
2676 1101 $intent_embedding = $intent_embedding_serialized
2677 1102 ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2678 1103 : null;
2679 -
2680 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2681 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2682 - if ($legacy_similarity > $best_similarity) {
2683 - $best_similarity = $legacy_similarity;
2684 - $matched_phrase_text = 'legacy';
2685 - }
2686 - }
2687 -
2688 - // Check individual phrase vectors
2689 - if (isset($phrases_by_intent[$intent->id])) {
2690 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2691 - $phrase_embedding = $phrase_row->embedding_vector
2692 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2693 - : null;
2694 - if (!is_array($phrase_embedding)) {
2695 - continue;
2696 - }
2697 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2698 - if ($phrase_similarity > $best_similarity) {
2699 - $best_similarity = $phrase_similarity;
2700 - $matched_phrase_text = $phrase_row->phrase;
2701 - }
2702 - }
2703 - }
2704 -
2705 - // Skip if no valid embedding was found at all
2706 - if ($best_similarity === -INF) {
1104 +
1105 + if (!is_array($intent_embedding)) {
1106 + //error_log("❌ MXCHAT DEBUG: Invalid embedding for intent: {$intent->intent_label}");
2707 1107 continue;
2708 1108 }
2709 -
2710 - $similarity = $best_similarity;
1109 +
1110 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2711 1111 $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2712 -
2713 - // Store action analysis data for testing panel
2714 - $action_analysis[] = [
2715 - 'intent_label' => $intent->intent_label,
2716 - 'callback_function' => $intent->callback_function,
2717 - 'similarity' => round($similarity, 4),
2718 - 'similarity_percentage' => round($similarity * 100, 2),
2719 - 'threshold' => $intent_threshold,
2720 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2721 - 'above_threshold' => $similarity >= $intent_threshold,
2722 - 'matched_phrase' => $matched_phrase_text,
2723 - 'triggered' => false // Will be updated below if this intent is triggered
2724 - ];
2725 -
1112 +
1113 + //error_log("📊 MXCHAT DEBUG: Intent '{$intent->intent_label}' similarity: {$similarity}, threshold: {$intent_threshold}");
1114 +
2726 1115 if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2727 1116 $highest_similarity = $similarity;
2728 1117 $matched_intent = $intent;
1118 + //error_log("✅ MXCHAT DEBUG: New best match: '{$intent->intent_label}' with similarity {$similarity}");
2729 1119 }
2730 1120 }
1121 + //error_log('📊 MXCHAT DEBUG: End Intent Scores ==================');
2731 1122
2732 - // Mark the triggered action if any
2733 1123 if ($matched_intent) {
2734 - foreach ($action_analysis as &$action) {
2735 - if ($action['intent_label'] === $matched_intent->intent_label) {
2736 - $action['triggered'] = true;
2737 - break;
2738 - }
2739 - }
2740 - }
2741 -
2742 - // Sort actions by similarity (highest first) and store for testing panel
2743 - usort($action_analysis, function($a, $b) {
2744 - return $b['similarity'] <=> $a['similarity'];
2745 - });
2746 -
2747 - // Store action analysis for testing panel capture
2748 - $this->last_action_analysis = $action_analysis;
2749 -
2750 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2751 - if ($matched_intent) {
1124 + //error_log("🎯 MXCHAT DEBUG: Final Intent Match: '{$matched_intent->intent_label}'");
1125 + //error_log("🔄 MXCHAT DEBUG: Invoking callback: {$matched_intent->callback_function}");
1126 +
2752 1127 // If the callback is a method on this instance (core callback), call it directly
2753 1128 if (method_exists($this, $matched_intent->callback_function)) {
1129 + //error_log('🔍 MXCHAT DEBUG: Using direct method call for core callback');
2754 1130 $callback_result = call_user_func(
2755 - [$this, $matched_intent->callback_function],
2756 - $message,
2757 - $user_id,
2758 - $session_id,
2759 - $matched_intent,
2760 - $user_context ?? null
2761 - );
1131 + [$this, $matched_intent->callback_function],
1132 + $message,
1133 + $user_id,
1134 + $session_id,
1135 + $matched_intent,
1136 + $user_context
1137 + );
2762 1138 } else {
1139 + //error_log('🔍 MXCHAT DEBUG: Using apply_filters for add-on callback');
2763 1140 // Otherwise, use apply_filters for add-on callbacks
2764 1141 $callback_result = apply_filters(
2765 1142 $matched_intent->callback_function,
2766 - false,
1143 + false, // default return value
2767 1144 $message,
2768 1145 $user_id,
2769 1146 $session_id,
2770 1147 $matched_intent
@@ -2770,50 +1147,26 @@
2770 1147 $matched_intent
2771 1148 );
2772 1149 }
2773 1150
2774 - // Handle the callback result properly
1151 + //error_log('🔍 MXCHAT DEBUG: Callback result type: ' . gettype($callback_result));
2775 1152 if ($callback_result !== false) {
2776 - // If callback returned an array with chat_mode, use it directly
2777 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2778 - $this->fallbackResponse = $callback_result;
2779 - return $callback_result; // Return the full array
2780 - } else {
2781 - $this->fallbackResponse = $callback_result;
2782 - return true;
2783 - }
1153 + //error_log('✅ MXCHAT DEBUG: Intent handled successfully');
1154 + $this->fallbackResponse = $callback_result;
1155 + return true;
2784 1156 }
1157 + //error_log('❌ MXCHAT DEBUG: Callback returned false');
1158 + } else {
1159 + //error_log('❌ MXCHAT DEBUG: No matching intent found');
2785 1160 }
2786 1161
1162 + //error_log('🔍 MXCHAT DEBUG: Intent Check Completed ==================');
2787 1163 return false;
2788 1164 }
2789 1165
2790 -/**
2791 - * Check if an action is enabled for a specific bot
2792 - */
2793 -private function is_action_enabled_for_bot($intent, $bot_id) {
2794 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2795 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2796 - return true;
2797 - }
2798 1166
2799 - $enabled_bots = json_decode($intent->enabled_bots, true);
2800 1167
2801 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2802 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2803 - return true;
2804 - }
2805 1168
2806 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2807 - // default-bot actions are testable from the admin panel
2808 - if ($bot_id === 'testing') {
2809 - $bot_id = 'default';
2810 - }
2811 -
2812 - // Check if the current bot is in the enabled bots list
2813 - return in_array($bot_id, $enabled_bots);
2814 -}
2815 -
2816 1169 // Helper function to clear PDF and Word document related transients
2817 1170 private function clear_pdf_transients($session_id) {
2818 1171 // PDF transients
2819 1172 delete_transient('mxchat_pdf_url_' . $session_id);
@@ -2832,38 +1185,34 @@
2832 1185
2833 1186
2834 1187 //verified good
2835 1188 public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2836 - // Get the user's original instruction/message
2837 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2838 -
2839 - // Set instruction for AI - just pass along what the user wanted to say
2840 - $this->current_action_instruction = $user_instruction;
2841 -
2842 - // Set the transient to track email capture flow
1189 + // Log the message safely
1190 + //error_log("Triggered email capture intent for message: " . sanitize_text_field($message));
1191 +
1192 + // Initiate email capture flow
1193 + $response = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Would you like to join our mailing list? Please provide your email below.", 'mxchat'));
1194 +
2843 1195 set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2844 -
2845 - // Return false to let the AI generate the response
2846 - return false;
1196 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
1197 +
1198 + // Respond to the user
1199 + wp_send_json(['message' => $response]);
1200 + wp_die();
2847 1201 }
2848 1202
2849 1203 public function mxchat_generate_image($message, $user_id, $session_id) {
2850 1204 //error_log("Starting image generation for message: " . $message);
2851 -
2852 - // Prepare a prompt for OpenAI image generation
1205 +
1206 + // Prepare a prompt for DALL-E
2853 1207 $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2854 -
2855 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2856 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2857 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2858 - $image_response = $this->mxchat_generate_custom_image($prompt);
2859 - } else {
2860 - // Use the existing OpenAI API key
2861 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2862 - // Call OpenAI GPT Image to generate an image
2863 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2864 - }
2865 1208
1209 + // Use the existing OpenAI API key
1210 + $openai_api_key = sanitize_text_field($this->options['api_key']);
1211 +
1212 + // Call DALL-E to generate an image
1213 + $image_response = $this->mxchat_generate_dalle_image($prompt, $openai_api_key);
1214 +
2866 1215 // Check if the response contains an image URL
2867 1216 if (isset($image_response['imageUrl'])) {
2868 1217 $image_url = esc_url_raw($image_response['imageUrl']);
2869 1218
@@ -2906,125 +1255,24 @@
2906 1255 // Return the response directly instead of relying on the property
2907 1256 return $this->fallbackResponse;
2908 1257 }
2909 1258 }
2910 -
2911 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2912 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2913 -
2914 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2915 - if (empty($gemini_api_key)) {
2916 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2917 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2918 - return ['text' => $response_text, 'html' => '', 'images' => []];
2919 - }
2920 -
2921 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2922 -
2923 - if (isset($image_response['imageUrl'])) {
2924 - $image_url = esc_url_raw($image_response['imageUrl']);
2925 -
2926 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2927 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2928 -
2929 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2930 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2931 -
2932 - $this->fallbackResponse = [
2933 - 'text' => $response_text,
2934 - 'html' => $response_html,
2935 - 'images' => [$image_url]
2936 - ];
2937 -
2938 - return $this->fallbackResponse;
2939 - } else {
2940 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2941 -
2942 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2943 -
2944 - $this->fallbackResponse = [
2945 - 'text' => $response_text,
2946 - 'html' => '',
2947 - 'images' => []
2948 - ];
2949 -
2950 - return $this->fallbackResponse;
2951 - }
2952 -}
2953 -
2954 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2955 - // Map the real mime type to a matching file extension so the saved file's
2956 - // extension always agrees with its bytes. A mismatch (e.g. Imagen returning
2957 - // webp bytes that were written into a ".png" file) makes the browser refuse
2958 - // to render the image even though the file saved successfully and the bot
2959 - // reported success — that was the Gemini/Imagen "image never renders" bug.
2960 - // OpenAI + custom-provider paths pass 'image/png' explicitly, so they are
2961 - // unaffected; this only matters for providers that return another type.
2962 - $mime_to_ext = [
2963 - 'image/jpeg' => 'jpg',
2964 - 'image/jpg' => 'jpg',
2965 - 'image/png' => 'png',
2966 - 'image/webp' => 'webp',
2967 - 'image/gif' => 'gif',
2968 - ];
2969 - $mime_type = strtolower(trim((string) $mime_type));
2970 - if (isset($mime_to_ext[$mime_type])) {
2971 - $extension = $mime_to_ext[$mime_type];
2972 - } else {
2973 - // Unknown/unsupported type: fall back to png and normalize the stored
2974 - // mime so the attachment record and the file extension stay consistent.
2975 - $extension = 'png';
2976 - $mime_type = 'image/png';
2977 - }
2978 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2979 - $decoded = base64_decode($base64_data);
2980 -
2981 - if ($decoded === false) {
2982 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2983 - }
2984 -
2985 - $upload = wp_upload_bits($filename, null, $decoded);
2986 -
2987 - if (!empty($upload['error'])) {
2988 - return new \WP_Error('upload_failed', $upload['error']);
2989 - }
2990 -
2991 - $attach_id = wp_insert_attachment([
2992 - 'post_mime_type' => $mime_type,
2993 - 'post_title' => $prefix,
2994 - 'post_content' => '',
2995 - 'post_status' => 'inherit',
2996 - ], $upload['file']);
2997 -
2998 - if (is_wp_error($attach_id)) {
2999 - return $attach_id;
3000 - }
3001 -
3002 - require_once ABSPATH . 'wp-admin/includes/image.php';
3003 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
3004 - wp_update_attachment_metadata($attach_id, $metadata);
3005 -
3006 - return esc_url_raw(wp_get_attachment_url($attach_id));
3007 -}
3008 -
3009 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
1259 +private function mxchat_generate_dalle_image($prompt, $api_key, $model = 'dall-e-3', $timeout = 60) {
3010 1260 $api_url = 'https://api.openai.com/v1/images/generations';
3011 1261 $body = json_encode([
3012 - 'prompt' => sanitize_text_field($prompt),
3013 - 'n' => 1,
3014 - 'size' => '1024x1024',
3015 - 'quality' => 'medium',
3016 - 'output_format' => 'png',
3017 - 'model' => sanitize_text_field($model),
1262 + 'prompt' => sanitize_text_field($prompt),
1263 + 'n' => 1,
1264 + 'size' => '1024x1024',
1265 + 'model' => sanitize_text_field($model),
3018 1266 ]);
3019 1267
3020 1268 $args = [
3021 - 'body' => $body,
1269 + 'body' => $body,
3022 1270 'headers' => [
3023 - 'Content-Type' => 'application/json',
1271 + 'Content-Type' => 'application/json',
3024 1272 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
3025 1273 ],
3026 - 'method' => 'POST',
1274 + 'method' => 'POST',
3027 1275 'timeout' => absint($timeout),
3028 1276 ];
3029 1277
3030 1278 $response = wp_remote_post($api_url, $args);
@@ -3029,114 +1277,23 @@
3029 1277
3030 1278 $response = wp_remote_post($api_url, $args);
3031 1279
3032 1280 if (is_wp_error($response)) {
1281 + //error_log("DALL-E request failed: " . $response->get_error_message());
3033 1282 return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
3034 1283 }
3035 1284
3036 1285 $response_body = json_decode(wp_remote_retrieve_body($response), true);
3037 1286
3038 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
3039 - if ($b64) {
3040 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
3041 - if (is_wp_error($saved_url)) {
3042 - return ['error' => $saved_url->get_error_message()];
3043 - }
3044 - return ['imageUrl' => $saved_url];
1287 + if (isset($response_body['data'][0]['url'])) {
1288 + return ['imageUrl' => esc_url_raw($response_body['data'][0]['url'])];
3045 1289 } else {
1290 + //error_log("DALL-E response error: " . wp_remote_retrieve_body($response));
3046 1291 return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3047 1292 }
3048 1293 }
3049 1294
3050 1295 /**
3051 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
3052 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
3053 - */
3054 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
3055 - $cfg = $this->mxchat_resolve_custom_provider();
3056 - if (empty($cfg['base_url'])) {
3057 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
3058 - }
3059 - $url = $cfg['base_url'] . '/images/generations';
3060 - if (!empty($cfg['api_version'])) {
3061 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
3062 - }
3063 - $body = wp_json_encode([
3064 - 'prompt' => sanitize_text_field($prompt),
3065 - 'n' => 1,
3066 - 'size' => '1024x1024',
3067 - 'model' => $cfg['model'],
3068 - ]);
3069 - $response = wp_remote_post($url, [
3070 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3071 - 'body' => $body,
3072 - 'method' => 'POST',
3073 - 'timeout' => absint($timeout),
3074 - ]);
3075 - if (is_wp_error($response)) {
3076 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
3077 - }
3078 - $resp = json_decode(wp_remote_retrieve_body($response), true);
3079 - // Try b64 first (matches OpenAI shape), then url-based fallback.
3080 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
3081 - if ($b64) {
3082 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
3083 - if (is_wp_error($saved)) {
3084 - return ['error' => $saved->get_error_message()];
3085 - }
3086 - return ['imageUrl' => $saved];
3087 - }
3088 - $remote_url = $resp['data'][0]['url'] ?? null;
3089 - if ($remote_url) {
3090 - return ['imageUrl' => esc_url_raw($remote_url)];
3091 - }
3092 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
3093 - return ['error' => esc_html($err_msg)];
3094 -}
3095 -
3096 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
3097 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
3098 -
3099 - $body = json_encode([
3100 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
3101 - 'parameters' => [
3102 - 'sampleCount' => 1,
3103 - 'aspectRatio' => '1:1',
3104 - ],
3105 - ]);
3106 -
3107 - $args = [
3108 - 'body' => $body,
3109 - 'headers' => [
3110 - 'Content-Type' => 'application/json',
3111 - 'x-goog-api-key' => sanitize_text_field($api_key),
3112 - ],
3113 - 'method' => 'POST',
3114 - 'timeout' => absint($timeout),
3115 - ];
3116 -
3117 - $response = wp_remote_post($api_url, $args);
3118 -
3119 - if (is_wp_error($response)) {
3120 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
3121 - }
3122 -
3123 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
3124 -
3125 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
3126 - if ($b64) {
3127 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
3128 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
3129 - if (is_wp_error($saved_url)) {
3130 - return ['error' => $saved_url->get_error_message()];
3131 - }
3132 - return ['imageUrl' => $saved_url];
3133 - } else {
3134 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3135 - }
3136 -}
3137 -
3138 -/**
3139 1296 * Handle web search requests.
3140 1297 *
3141 1298 * Sends the refined search query to the Brave Search API and uses the
3142 1299 * results to generate a conversational response with the AI model.
@@ -3184,10 +1341,10 @@
3184 1341 $transient_key = 'mxchat_search_' . md5($refined_search_query);
3185 1342 $results = get_transient($transient_key);
3186 1343
3187 1344 if (false === $results) {
3188 - // SECURITY FIX: Changed to wp_safe_remote_get
3189 - $response = wp_safe_remote_get(
1345 + // Fetch new results from the Brave Search API
1346 + $response = wp_remote_get(
3190 1347 $api_url,
3191 1348 array(
3192 1349 'headers' => array(
3193 1350 'Accept' => 'application/json',
@@ -3266,28 +1423,119 @@
3266 1423 'html' => ''
3267 1424 );
3268 1425 }
3269 1426 }
1427 +/**
1428 + * Format search results into a natural text summary.
1429 + *
1430 + * @since 1.0.0
1431 + * @param array $results The search results from the API.
1432 + * @param string $query The original search query.
1433 + * @return string The text summary of the top results.
1434 + */
1435 +private function format_search_results( $results, $query ) {
1436 + $summary = sprintf(
1437 + esc_html__( 'Here are the most relevant results for "%s":', 'mxchat' ),
1438 + esc_html( $query )
1439 + ) . "\n\n";
3270 1440
3271 -//very good
1441 + $max_results = min( count( $results ), 3 );
1442 + for ( $i = 0; $i < $max_results; $i++ ) {
1443 + $result = $results[ $i ];
1444 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1445 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1446 +
1447 + // Append title and description to the summary
1448 + $summary .= sprintf(
1449 + "%s\n%s\n\n",
1450 + esc_html( $title ),
1451 + esc_html( $description )
1452 + );
1453 + }
1454 +
1455 + return $summary;
1456 +}
1457 +
3272 1458 /**
3273 - * Handle image search requests from the chatbot
1459 + * Generate HTML markup for search results.
3274 1460 *
3275 - * @param string $message The user's search query
3276 - * @param int $user_id The user's ID
3277 - * @param string $session_id The chat session ID
3278 - * @return array Response array with text and HTML content
1461 + * @since 1.0.0
1462 + * @param array $results The search results from the API.
1463 + * @param string $query The user-refined query.
1464 + * @return string The HTML markup for displaying the results.
3279 1465 */
1466 +private function generate_search_results_html( $results, $query ) {
1467 + ob_start();
1468 + ?>
1469 + <div class="mxchat-search-results">
1470 + <?php foreach ( $results as $result ) :
1471 + $title = isset( $result['title'] ) ? wp_strip_all_tags( $result['title'] ) : '';
1472 + $url = isset( $result['url'] ) ? esc_url( $result['url'] ) : '#';
1473 + $description = isset( $result['description'] ) ? wp_strip_all_tags( $result['description'] ) : '';
1474 + $favicon = isset( $result['meta_url']['favicon'] ) ? esc_url( $result['meta_url']['favicon'] ) : '';
1475 + $thumbnail = isset( $result['thumbnail']['src'] ) ? esc_url( $result['thumbnail']['src'] ) : '';
1476 + $domain = parse_url( $url, PHP_URL_HOST );
1477 + ?>
1478 + <div class="mxchat-search-item">
1479 + <div class="mxchat-search-header">
1480 + <?php if ( $favicon ) : ?>
1481 + <img
1482 + src="<?php echo esc_url( $favicon ); ?>"
1483 + class="mxchat-site-icon"
1484 + alt="<?php echo esc_attr__( 'Site icon', 'mxchat' ); ?>"
1485 + width="16"
1486 + height="16"
1487 + />
1488 + <?php endif; ?>
1489 + <div class="mxchat-site-url"><?php echo esc_html( $domain ); ?></div>
1490 + </div>
1491 +
1492 + <div class="mxchat-search-content">
1493 + <h3 class="mxchat-search-title">
1494 + <a href="<?php echo esc_url( $url ); ?>"
1495 + target="_blank"
1496 + rel="noopener noreferrer"
1497 + >
1498 + <?php echo esc_html( $title ); ?>
1499 + </a>
1500 + </h3>
1501 +
1502 + <?php if ( $thumbnail ) : ?>
1503 + <div class="mxchat-search-thumbnail">
1504 + <img
1505 + src="<?php echo esc_url( $thumbnail ); ?>"
1506 + alt="<?php echo esc_attr__( 'Thumbnail image', 'mxchat' ); ?>"
1507 + loading="lazy"
1508 + />
1509 + </div>
1510 + <?php endif; ?>
1511 +
1512 + <div class="mxchat-search-description">
1513 + <?php echo esc_html( $description ); ?>
1514 + </div>
1515 + </div>
1516 + </div>
1517 + <?php endforeach; ?>
1518 + </div>
1519 + <?php
1520 + return ob_get_clean();
1521 +}
1522 +
1523 +
1524 +//very good
3280 1525 public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3281 - // Step 1: Interpret the search query using the user's selected AI model
1526 +
1527 + // Step 1: Interpret the search query for better results
3282 1528 $refined_search_query = $this->mxchat_interpret_search_query($message);
3283 1529
1530 +
3284 1531 // If no query was interpreted, return a fallback message
3285 1532 if (empty($refined_search_query)) {
3286 - return array(
1533 + $this->fallbackResponse = [
3287 1534 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3288 1535 'html' => "",
3289 - );
1536 + ];
1537 + return;
3290 1538 }
3291 1539
3292 1540 // Brave API URL
3293 1541 $api_url = 'https://api.search.brave.com/res/v1/images/search';
@@ -3296,12 +1544,19 @@
3296 1544 $options = get_option('mxchat_options');
3297 1545 $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3298 1546
3299 1547 if (empty($api_key)) {
3300 - return array(
1548 +/*
1549 + if (defined('WP_DEBUG') && WP_DEBUG) {
1550 + //error_log("Brave API key is missing.");
1551 + }
1552 +*/
1553 +
1554 + $this->fallbackResponse = [
3301 1555 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3302 1556 'html' => "",
3303 - );
1557 + ];
1558 + return;
3304 1559 }
3305 1560
3306 1561 $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3307 1562 $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
@@ -3312,8 +1567,16 @@
3312 1567 'count' => $image_count,
3313 1568 'safesearch' => $safe_search,
3314 1569 ], $api_url);
3315 1570
1571 +/*
1572 + // Log the final API URL for the search
1573 + if (defined('WP_DEBUG') && WP_DEBUG) {
1574 + //error_log("Final API URL for Brave Image Search: " . esc_url_raw($api_url));
1575 + }
1576 +*/
1577 +
1578 +
3316 1579 // Implement caching
3317 1580 $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3318 1581 $body = get_transient($transient_key);
3319 1582
@@ -3326,16 +1589,22 @@
3326 1589 ],
3327 1590 'timeout' => 10,
3328 1591 ];
3329 1592
3330 - // SECURITY FIX: Changed to wp_safe_remote_get
3331 - $response = wp_safe_remote_get($api_url, $args);
1593 + $response = wp_remote_get($api_url, $args);
3332 1594
3333 1595 if (is_wp_error($response)) {
3334 - return array(
1596 +/*
1597 + if (defined('WP_DEBUG') && WP_DEBUG) {
1598 + //error_log("Brave Image API request failed: " . $response->get_error_message());
1599 + }
1600 +*/
1601 +
1602 + $this->fallbackResponse = [
3335 1603 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3336 1604 'html' => "",
3337 - );
1605 + ];
1606 + return;
3338 1607 }
3339 1608
3340 1609 $body = json_decode(wp_remote_retrieve_body($response), true);
3341 1610 set_transient($transient_key, $body, HOUR_IN_SECONDS);
@@ -3343,16 +1612,10 @@
3343 1612
3344 1613 // Process the API response
3345 1614 if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3346 1615 $html_output = '<div class="mxchat-image-gallery">';
3347 -
3348 - // Get the configured image count (1-6)
3349 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3350 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3351 -
3352 - // Use only the requested number of images
3353 - for ($i = 0; $i < $display_count; $i++) {
3354 - $image = $body['results'][$i];
1616 +
1617 + foreach ($body['results'] as $image) {
3355 1618 $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3356 1619 $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3357 1620 $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3358 1621
@@ -3366,462 +1629,243 @@
3366 1629 }
3367 1630
3368 1631 $html_output .= '</div>';
3369 1632
3370 - // Create response text
3371 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3372 -
3373 - // Save both response text and HTML to chat history
3374 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
1633 + $this->fallbackResponse = [
1634 + 'text' => "",
1635 + 'html' => $html_output,
1636 + ];
1637 +
1638 + // Save response in chat history
3375 1639 $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3376 1640
3377 - // Return the combined response
3378 - return array(
3379 - 'text' => $response_text,
3380 - 'html' => $html_output,
3381 - );
3382 1641 } else {
3383 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3384 -
3385 - // Save the error message to chat history
3386 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3387 -
3388 - return array(
3389 - 'text' => $response_text,
1642 +/*
1643 + if (defined('WP_DEBUG') && WP_DEBUG) {
1644 + //error_log("Brave Image API response did not contain expected data structure or was empty: " . print_r($body, true));
1645 + }
1646 +*/
1647 +
1648 + $this->fallbackResponse = [
1649 + 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3390 1650 'html' => "",
3391 - );
1651 + ];
3392 1652 }
3393 1653 }
3394 -
3395 -/**
3396 - * Interpret the search query using the user's selected AI model
3397 - *
3398 - * @param string $user_query The original query from the user
3399 - * @return string The refined search query
3400 - */
3401 1654 public function mxchat_interpret_search_query($user_query) {
3402 1655 $system_prompt = esc_html__("Interpret the user's request to provide only the essential keywords or phrases for image searching. Remove conversational language, politeness, or extra context. Return a concise search query that doesn't lose any of the original meaning.", 'mxchat');
3403 1656
3404 - // Get options and determine the selected model
3405 - $options = $this->options ?? get_option('mxchat_options');
3406 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
1657 + // Retrieve OpenAI API key using 'api_key' as the option key
1658 + $api_key = isset($this->options['api_key']) ? sanitize_text_field($this->options['api_key']) : sanitize_text_field(get_option('mxchat_options')['api_key']);
3407 1659
3408 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3409 - if ($selected_model === 'custom-provider') {
3410 - return $this->interpret_query_with_custom($user_query, $system_prompt);
1660 + /*
1661 + // Log the API key check, without exposing the key
1662 + if (defined('WP_DEBUG') && WP_DEBUG) {
1663 + //error_log("Retrieved OpenAI API Key: " . ($api_key ? "Present" : "Missing"));
3411 1664 }
1665 + */
3412 1666
3413 - // Extract model prefix to determine the provider
3414 - $model_parts = explode('-', $selected_model);
3415 - $provider = strtolower($model_parts[0]);
3416 -
3417 - // Determine which API key to use based on the provider
3418 - switch ($provider) {
3419 - case 'gemini':
3420 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3421 - if (empty($api_key)) {
3422 - return sanitize_text_field($user_query); // Default to original query if API key missing
3423 - }
3424 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3425 -
3426 - case 'claude':
3427 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3428 - if (empty($api_key)) {
3429 - return sanitize_text_field($user_query);
3430 - }
3431 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3432 -
3433 - case 'grok':
3434 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3435 - if (empty($api_key)) {
3436 - return sanitize_text_field($user_query);
3437 - }
3438 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3439 -
3440 - case 'deepseek':
3441 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3442 - if (empty($api_key)) {
3443 - return sanitize_text_field($user_query);
3444 - }
3445 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3446 -
3447 - case 'gpt':
3448 - default:
3449 - // Default to OpenAI for custom models or unrecognized prefixes
3450 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3451 - if (empty($api_key)) {
3452 - return sanitize_text_field($user_query);
3453 - }
3454 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
1667 + if (empty($api_key)) {
1668 + //error_log("OpenAI API key is missing.");
1669 + return sanitize_text_field($user_query); // Default to the original query if API key is missing
3455 1670 }
3456 -}
3457 1671
3458 -/**
3459 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3460 - * Uses the same base URL + auth scheme as the chat dispatcher.
3461 - */
3462 -private function interpret_query_with_custom($user_query, $system_prompt) {
3463 - $cfg = $this->mxchat_resolve_custom_provider();
3464 - if (empty($cfg['base_url'])) {
3465 - return sanitize_text_field($user_query);
3466 - }
3467 - // plan-mxchat-20260715-7124f4: a custom OpenAI-compatible endpoint pointed at
3468 - // a gpt-5-class model rejects temperature!=1 and the legacy max_tokens key.
3469 - // Byte-identical for ordinary custom models (temperature kept, max_tokens
3470 - // used); only gpt-5-class custom models change (best-effort — custom
3471 - // endpoints vary).
3472 - $token_key = $this->mxchat_openai_token_param_for($cfg['model']);
3473 - $payload = [
3474 - 'model' => $cfg['model'],
3475 - 'messages' => [
3476 - ['role' => 'system', 'content' => $system_prompt],
3477 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3478 - ],
3479 - $token_key => 20,
3480 - ];
3481 - if ($this->mxchat_openai_supports_temperature_for($cfg['model'])) {
3482 - $payload['temperature'] = 0.2;
3483 - }
3484 - $args = [
3485 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3486 - 'body' => wp_json_encode($payload),
3487 - 'method' => 'POST',
3488 - 'timeout' => 15,
3489 - ];
3490 - $response = wp_remote_post($cfg['chat_url'], $args);
3491 - if (is_wp_error($response)) {
3492 - return sanitize_text_field($user_query);
3493 - }
3494 - $body = json_decode(wp_remote_retrieve_body($response), true);
3495 - return isset($body['choices'][0]['message']['content'])
3496 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3497 - : sanitize_text_field($user_query);
3498 -}
3499 -
3500 -/**
3501 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3502 - * into the assoc-array form wp_remote_post expects.
3503 - */
3504 -private function mxchat_custom_provider_assoc_headers($cfg) {
3505 - $headers = ['Content-Type' => 'application/json'];
3506 - if (!empty($cfg['api_key'])) {
3507 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3508 - $headers['api-key'] = $cfg['api_key'];
3509 - } else {
3510 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3511 - }
3512 - }
3513 - return $headers;
3514 -}
3515 -
3516 -/**
3517 - * Interpret query using OpenAI models
3518 - */
3519 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3520 1672 $url = 'https://api.openai.com/v1/chat/completions';
3521 - // plan-mxchat-20260715-7124f4: the default chat model is gpt-5.1-chat-latest
3522 - // and every gpt-5* rejects both a non-default temperature and the legacy
3523 - // max_tokens key (400). This call swallowed the 400 and silently degraded to
3524 - // the raw query on every gpt-5 install, quietly disabling product/image
3525 - // search-query interpretation. Derive capability from the core catalog
3526 - // (dcb71c) so this tracks future model adds; strpos fallback for a
3527 - // partial-upgrade window where the catalog method isn't loaded.
3528 - $token_key = $this->mxchat_openai_token_param_for($model);
3529 - $payload = [
3530 - 'model' => $model,
3531 - 'messages' => [
3532 - ['role' => 'system', 'content' => $system_prompt],
3533 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3534 - ],
3535 - $token_key => 20,
3536 - ];
3537 - if ($this->mxchat_openai_supports_temperature_for($model)) {
3538 - $payload['temperature'] = 0.2;
3539 - }
3540 1673 $args = [
3541 1674 'headers' => [
3542 1675 'Authorization' => 'Bearer ' . $api_key,
3543 1676 'Content-Type' => 'application/json',
3544 1677 ],
3545 - 'body' => wp_json_encode($payload),
1678 + 'body' => wp_json_encode([
1679 + 'model' => 'gpt-3.5-turbo',
1680 + 'messages' => [
1681 + ['role' => 'system', 'content' => $system_prompt],
1682 + ['role' => 'user', 'content' => sanitize_text_field($user_query)],
1683 + ],
1684 + 'temperature' => 0.2,
1685 + 'max_tokens' => 20,
1686 + ]),
3546 1687 'method' => 'POST',
3547 - 'timeout' => 15,
3548 1688 ];
3549 1689
3550 1690 $response = wp_remote_post($url, $args);
1691 +
3551 1692 if (is_wp_error($response)) {
3552 - return sanitize_text_field($user_query);
1693 + //error_log("OpenAI request failed: " . $response->get_error_message());
1694 + return sanitize_text_field($user_query); // Fallback to the original query if there's an error
3553 1695 }
3554 1696
3555 1697 $body = json_decode(wp_remote_retrieve_body($response), true);
3556 - return isset($body['choices'][0]['message']['content'])
3557 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3558 - : sanitize_text_field($user_query);
3559 -}
3560 1698
3561 -/**
3562 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3563 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3564 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3565 - * stripping. We never send a `thinking` param either, which is required for
3566 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3567 - */
3568 -private function mxchat_claude_omits_temperature($model) {
3569 - // plan-mxchat-20260714-dcb71c: derive from the core model catalog (single
3570 - // source of truth). Every caller here passes a Claude model, so
3571 - // !supports_temperature() reproduces the old 4-id in_array() result exactly.
3572 - // Frozen list kept as fallback for a partial-upgrade window where the
3573 - // catalog method isn't loaded.
3574 - if (class_exists('MxChat_Model_Catalog') && method_exists('MxChat_Model_Catalog', 'supports_temperature')) {
3575 - return !MxChat_Model_Catalog::supports_temperature($model);
1699 + // Check for a valid response and sanitize output
1700 + if (isset($body['choices'][0]['message']['content'])) {
1701 + $interpreted_query = sanitize_text_field(trim($body['choices'][0]['message']['content']));
1702 +
1703 + /*
1704 + // Log the interpreted query for debugging
1705 + if (defined('WP_DEBUG') && WP_DEBUG) {
1706 + //error_log("Interpreted search query: " . $interpreted_query);
1707 + }
1708 + */
1709 +
1710 + return $interpreted_query;
1711 + } else {
1712 + //error_log("Unexpected API response format: " . print_r($body, true));
1713 + return sanitize_text_field($user_query);
3576 1714 }
3577 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5', 'claude-sonnet-5');
3578 - return in_array($model, $no_temp, true);
3579 1715 }
3580 1716
3581 -/**
3582 - * plan-mxchat-20260715-7124f4: OpenAI completion-token key for this model,
3583 - * sourced from the core catalog (gpt-5* → max_completion_tokens; else
3584 - * max_tokens). strpos fallback for a partial-upgrade window where the catalog
3585 - * method isn't loaded.
3586 - *
3587 - * @param string $model OpenAI(-compatible) model id.
3588 - * @return string 'max_completion_tokens' | 'max_tokens'
3589 - */
3590 -private function mxchat_openai_token_param_for($model) {
3591 - if (class_exists('MxChat_Model_Catalog') && method_exists('MxChat_Model_Catalog', 'openai_token_param')) {
3592 - return MxChat_Model_Catalog::openai_token_param($model);
3593 - }
3594 - return strpos((string) $model, 'gpt-5') === 0 ? 'max_completion_tokens' : 'max_tokens';
3595 -}
3596 1717
3597 -/**
3598 - * plan-mxchat-20260715-7124f4: whether a NON-default temperature may be sent to
3599 - * this OpenAI(-compatible) model. gpt-5* accept only the default (1) — sending
3600 - * any other value 400s. Sourced from the core catalog; strpos fallback for a
3601 - * partial-upgrade window.
3602 - *
3603 - * @param string $model OpenAI(-compatible) model id.
3604 - * @return bool
3605 - */
3606 -private function mxchat_openai_supports_temperature_for($model) {
3607 - if (class_exists('MxChat_Model_Catalog') && method_exists('MxChat_Model_Catalog', 'supports_temperature')) {
3608 - return MxChat_Model_Catalog::supports_temperature($model);
3609 - }
3610 - return strpos((string) $model, 'gpt-5') !== 0;
3611 -}
3612 1718
3613 -/**
3614 - * plan-mxchat-20260714-dcb71c: per-surface reasoning_effort, sourced from the
3615 - * core model catalog so a model add propagates automatically. The fallback is
3616 - * the frozen pre-dcb71c inline ladder, used only if the catalog method is
3617 - * unavailable (a partial-upgrade window). Byte-identical to the old inline
3618 - * blocks by construction — proven by the dcb71c equivalence harness.
3619 - *
3620 - * @param string $model Chat model id.
3621 - * @param string $context 'chat' | 'websearch'.
3622 - * @return string|null Effort to send, or null to omit the param.
3623 - */
3624 -private function mxchat_reasoning_effort_for($model, $context) {
3625 - if (class_exists('MxChat_Model_Catalog') && method_exists('MxChat_Model_Catalog', 'reasoning_effort_for')) {
3626 - return MxChat_Model_Catalog::reasoning_effort_for($model, $context);
1719 +private function find_product_in_message($message) {
1720 + global $wpdb;
1721 +
1722 + // Get embedding for the search query
1723 + $query_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
1724 +
1725 + // Check if embedding generation returned an error
1726 + if (is_array($query_embedding) && isset($query_embedding['error'])) {
1727 + $error_message = $query_embedding['error'];
1728 + $error_code = $query_embedding['error_code'] ?? 'embedding_error';
1729 +
1730 + //error_log("Product search embedding error: $error_message (Code: $error_code)");
1731 +
1732 + // Set a user-friendly fallback response
1733 + $this->fallbackResponse['text'] = esc_html__("I'm having trouble processing your product search. Please try again later or contact support if this persists.", 'mxchat');
1734 +
1735 + // Also store the technical error for admin users
1736 + $this->fallbackResponse['admin_error'] = $error_message;
1737 + $this->fallbackResponse['error_code'] = $error_code;
1738 +
1739 + return null;
3627 1740 }
3628 - return $this->mxchat_reasoning_effort_fallback($model, $context);
3629 -}
3630 -
3631 -private function mxchat_reasoning_effort_fallback($model, $context) {
3632 - if (strpos($model, 'gpt-5') !== 0) {
1741 +
1742 + // Check if embedding is valid
1743 + if (!is_array($query_embedding) || empty($query_embedding)) {
1744 + //error_log("Failed to generate embedding for product search");
1745 + $this->fallbackResponse['text'] = esc_html__("I couldn't process your product search. Please try again with different wording.", 'mxchat');
3633 1746 return null;
3634 1747 }
3635 - if ($context === 'websearch') {
3636 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
3637 - if (in_array($model, $no_reasoning_web, true)) return null;
3638 - if ($model === 'gpt-5.1-2025-11-13') return 'low';
3639 - if ($model === 'gpt-5.5') return 'low';
3640 - if ($model === 'gpt-5.4') return 'low';
3641 - if (in_array($model, array('gpt-5.6-sol', 'gpt-5.6-terra', 'gpt-5.6-luna'), true)) return 'low';
1748 +
1749 + // Get relevant content as string
1750 + $relevant_content = $this->mxchat_find_relevant_products($query_embedding);
1751 + if (empty($relevant_content)) {
1752 + // Return null to indicate no results and set fallback response
1753 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Could you please be more specific about the product you're looking for?", 'mxchat');
3642 1754 return null;
3643 1755 }
3644 - // 'chat'
3645 - $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');
3646 - if (in_array($model, $no_reasoning_models, true)) return null;
3647 - if ($model === 'gpt-5.1-2025-11-13') return 'low';
3648 - if ($model === 'gpt-5.5') return 'none';
3649 - if ($model === 'gpt-5.4') return 'none';
3650 - if (in_array($model, array('gpt-5.6-sol', 'gpt-5.6-terra', 'gpt-5.6-luna'), true)) return 'low';
3651 - return 'minimal';
3652 -}
3653 1756
3654 -/**
3655 - * Interpret query using Claude models
3656 - */
3657 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3658 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3659 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3660 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3661 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3662 - $url = 'https://api.anthropic.com/v1/messages';
3663 1757
3664 - $payload = [
3665 - 'model' => $model,
3666 - 'system' => $system_prompt,
3667 - 'messages' => [
3668 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3669 - ],
3670 - 'max_tokens' => 20,
3671 - 'temperature' => 0.2,
3672 - ];
3673 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
1758 + // Extract product URLs from the content
1759 + preg_match_all('/https?:\/\/[^\s<>"\']+?\/product\/[^\s<>"\']+/', $relevant_content, $matches);
3674 1760
3675 - $args = [
3676 - 'headers' => [
3677 - 'Content-Type' => 'application/json',
3678 - 'x-api-key' => $api_key,
3679 - 'anthropic-version' => '2023-06-01',
3680 - ],
3681 - 'body' => wp_json_encode($payload),
3682 - 'method' => 'POST',
3683 - 'timeout' => 15,
3684 - ];
1761 + if (!empty($matches[0])) {
1762 + // Try each URL found
1763 + foreach ($matches[0] as $url) {
1764 + // Clean the URL
1765 + $url = rtrim($url, '/."\']');
3685 1766
3686 - $response = wp_remote_post($url, $args);
3687 - if (is_wp_error($response)) {
3688 - return sanitize_text_field($user_query);
3689 - }
1767 + // Get the product slug
1768 + $path = parse_url($url, PHP_URL_PATH);
1769 + $slug = basename(rtrim($path, '/'));
3690 1770
3691 - $body = json_decode(wp_remote_retrieve_body($response), true);
3692 - // claude-fable-5 prepends a thinking block to content — take the first
3693 - // TEXT block, not content[0].
3694 - foreach ((array) ($body['content'] ?? array()) as $block) {
3695 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3696 - return sanitize_text_field(trim($block['text']));
1771 + // Find product by slug
1772 + $args = array(
1773 + 'post_type' => 'product',
1774 + 'post_status' => 'publish',
1775 + 'name' => $slug,
1776 + 'posts_per_page' => 1
1777 + );
1778 +
1779 + $products = get_posts($args);
1780 +
1781 + if (!empty($products)) {
1782 + $product_id = $products[0]->ID;
1783 + $product = wc_get_product($product_id);
1784 +
1785 + if ($product && $product->is_purchasable()) {
1786 + return $product_id;
1787 + }
1788 + }
3697 1789 }
3698 1790 }
3699 1791
3700 - return sanitize_text_field($user_query);
3701 -}
1792 + // Fallback: Look for product names in the content
1793 + $products = wc_get_products([
1794 + 'status' => 'publish',
1795 + 'limit' => -1,
1796 + 'return' => 'all'
1797 + ]);
3702 1798
3703 -/**
3704 - * Interpret query using Gemini models
3705 - */
3706 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3707 - if ($model === 'gemini-3-pro-preview') {
3708 - $model = 'gemini-3.1-pro-preview';
1799 + foreach ($products as $product) {
1800 + $name = $product->get_name();
1801 + if (stripos($relevant_content, $name) !== false) {
1802 + if ($product->is_purchasable()) {
1803 + return $product->get_id();
1804 + }
1805 + }
3709 1806 }
3710 - // Use v1beta for preview models, v1 for stable models
3711 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3712 1807
3713 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
3714 -
3715 - $args = [
3716 - 'headers' => [
3717 - 'Content-Type' => 'application/json',
3718 - ],
3719 - 'body' => wp_json_encode([
3720 - 'contents' => [
3721 - [
3722 - 'role' => 'user',
3723 - 'parts' => [
3724 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3725 - ]
3726 - ]
3727 - ],
3728 - 'generationConfig' => [
3729 - 'temperature' => 0.2,
3730 - 'maxOutputTokens' => 20,
3731 - ],
3732 - ]),
3733 - 'method' => 'POST',
3734 - 'timeout' => 15,
3735 - ];
3736 -
3737 - $response = wp_remote_post($url, $args);
3738 - if (is_wp_error($response)) {
3739 - return sanitize_text_field($user_query);
3740 - }
3741 -
3742 - $body = json_decode(wp_remote_retrieve_body($response), true);
3743 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3744 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3745 - }
3746 -
3747 - return sanitize_text_field($user_query);
1808 + // If no product is found after all checks, set the fallback response
1809 + $this->fallbackResponse['text'] = esc_html__("I couldn't find any relevant products based on your query. Try to be more specific", 'mxchat');
1810 + return null;
3748 1811 }
3749 1812
3750 -/**
3751 - * Interpret query using X.AI (Grok) models
3752 - */
3753 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3754 - $url = 'https://api.xai.com/v1/chat/completions';
3755 -
3756 - $args = [
3757 - 'headers' => [
3758 - 'Content-Type' => 'application/json',
3759 - 'Authorization' => 'Bearer ' . $api_key,
3760 - ],
3761 - 'body' => wp_json_encode([
3762 - 'model' => $model,
3763 - 'messages' => [
3764 - ['role' => 'system', 'content' => $system_prompt],
3765 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3766 - ],
3767 - 'temperature' => 0.2,
3768 - 'max_tokens' => 20,
3769 - ]),
3770 - 'method' => 'POST',
3771 - 'timeout' => 15,
3772 - ];
3773 -
3774 - $response = wp_remote_post($url, $args);
3775 - if (is_wp_error($response)) {
3776 - return sanitize_text_field($user_query);
3777 - }
3778 -
3779 - $body = json_decode(wp_remote_retrieve_body($response), true);
3780 - if (isset($body['choices'][0]['message']['content'])) {
3781 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3782 - }
3783 -
3784 - return sanitize_text_field($user_query);
1813 +// New method to handle intent responses
1814 +private function generate_intent_response($context_content, $session_id) {
1815 + // Convert the context array to a structured string for the AI
1816 + $context_string = $this->format_intent_context($context_content);
1817 + // Generate AI response using the context
1818 + $response = $this->mxchat_generate_response(
1819 + $context_string,
1820 + $this->options['api_key'],
1821 + $this->options['xai_api_key'],
1822 + $this->options['claude_api_key'],
1823 + $this->options['deepseek_api_key'],
1824 + $this->options['gemini_api_key'], // Added Gemini API key
1825 + $this->mxchat_fetch_conversation_history_for_ai($session_id)
1826 + );
1827 + $this->fallbackResponse['text'] = $response;
1828 + return true;
3785 1829 }
3786 1830
3787 -/**
3788 - * Interpret query using DeepSeek models
3789 - */
3790 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3791 - $url = 'https://api.deepseek.com/v1/chat/completions';
3792 -
3793 - $args = [
3794 - 'headers' => [
3795 - 'Content-Type' => 'application/json',
3796 - 'Authorization' => 'Bearer ' . $api_key,
3797 - ],
3798 - 'body' => wp_json_encode([
3799 - 'model' => $model,
3800 - 'messages' => [
3801 - ['role' => 'system', 'content' => $system_prompt],
3802 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3803 - ],
3804 - 'temperature' => 0.2,
3805 - 'max_tokens' => 20,
3806 - ]),
3807 - 'method' => 'POST',
3808 - 'timeout' => 15,
3809 - ];
3810 -
3811 - $response = wp_remote_post($url, $args);
3812 - if (is_wp_error($response)) {
3813 - return sanitize_text_field($user_query);
1831 +// Helper method to format intent context
1832 +private function format_intent_context($context) {
1833 + $context_string = esc_html__("INTENT CONTEXT:\n", 'mxchat');
1834 +
1835 + switch ($context['intent']) {
1836 + case 'add_to_cart':
1837 + if ($context['status'] === 'success') {
1838 + $context_string .= esc_html__("Action: Successfully added product to cart\n", 'mxchat');
1839 + $context_string .= sprintf(esc_html__("Product: %s\n", 'mxchat'), $context['product']['name']);
1840 + $context_string .= esc_html__("Available actions: ", 'mxchat') . implode(', ', $context['available_actions']) . "\n";
1841 + $context_string .= sprintf(esc_html__("Cart URL: %s\n", 'mxchat'), $context['cart_url']);
1842 + $context_string .= esc_html__("\nPlease inform the user of the successful addition and their available options.", 'mxchat');
1843 + } else {
1844 + $context_string .= esc_html__("Action: Failed to add product to cart\n", 'mxchat');
1845 + $context_string .= sprintf(esc_html__("Reason: %s\n", 'mxchat'), $context['reason']);
1846 + switch ($context['reason']) {
1847 + case 'woocommerce_not_available':
1848 + $context_string .= esc_html__("\nPlease inform the user that shopping features are not available.", 'mxchat');
1849 + break;
1850 + case 'no_product_context':
1851 + $context_string .= esc_html__("\nPlease ask the user to specify which product they want to add.", 'mxchat');
1852 + break;
1853 + case 'product_not_found':
1854 + $context_string .= esc_html__("\nPlease inform the user that the product couldn't be found and ask them to try again.", 'mxchat');
1855 + break;
1856 + case 'add_to_cart_failed':
1857 + $context_string .= esc_html__("\nPlease apologize to the user and suggest they try again or ask for assistance.", 'mxchat');
1858 + break;
1859 + }
1860 + }
1861 + break;
3814 1862 }
3815 -
3816 - $body = json_decode(wp_remote_retrieve_body($response), true);
3817 - if (isset($body['choices'][0]['message']['content'])) {
3818 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3819 - }
3820 -
3821 - return sanitize_text_field($user_query);
1863 +
1864 + return $context_string;
3822 1865 }
3823 1866
1867 +
3824 1868 //very good
3825 1869 private function add_email_to_loops($email) {
3826 1870 // Sanitize the email
3827 1871 $email = sanitize_email($email);
@@ -3907,211 +1951,95 @@
3907 1951 $this->fallbackResponse['text'] = '';
3908 1952 }
3909 1953
3910 1954
3911 -/**
3912 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3913 - */
3914 1955 private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3915 - // CLEAR DEBUG LOGGING
3916 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3917 - //error_log("PDF Source: " . $pdf_source);
3918 - //error_log("Max Pages: " . $max_pages);
3919 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3920 -
3921 - // Check if Advanced Claude Toolbar is available and enabled
3922 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3923 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3924 -
3925 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3926 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3927 -
3928 - if ($claude_available && $claude_enabled) {
3929 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3930 -
3931 - // Attempt Claude processing first
3932 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3933 -
3934 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3935 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3936 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3937 -
3938 - // Log first page details for verification
3939 - if (isset($claude_result[0])) {
3940 - $first_page = $claude_result[0];
3941 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3942 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3943 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3944 - }
3945 -
3946 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3947 - return $claude_result;
3948 - } else {
3949 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3950 - //error_log("Claude result type: " . gettype($claude_result));
3951 - if (is_array($claude_result)) {
3952 - //error_log("Claude result count: " . count($claude_result));
3953 - }
3954 - }
3955 - }
3956 -
3957 - // Fallback to basic processing
3958 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3959 -
3960 1956 $upload_dir = wp_upload_dir();
3961 1957 $temp_file = null;
3962 -
1958 +
3963 1959 try {
3964 - // Your existing basic processing code here...
3965 - // (I'll include the key parts with debug logging)
3966 -
1960 + // Handle URL vs local file
3967 1961 if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3968 - //error_log("Downloading PDF from URL...");
3969 -
3970 - // SECURITY FIX: Validate URL before processing
3971 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3972 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
1962 + // Validate and download the file from URL
1963 + $temp_file = wp_tempnam($pdf_source); // Safe temporary file name
1964 + $response = wp_remote_get($pdf_source, ['timeout' => 60]);
1965 +
1966 + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
1967 + //error_log(esc_html__("Failed to download PDF. Error: ", 'mxchat') . print_r($response, true));
3973 1968 return false;
3974 1969 }
3975 -
3976 - $temp_file = wp_tempnam($pdf_source);
3977 -
3978 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3979 - // Route through the shared MXChat crawler identity (plan bae78f/b6d93c) so
3980 - // every remote-content fetch presents one honest, versioned, filterable,
3981 - // allowlistable User-Agent. function_exists guard keeps the front-end/nopriv
3982 - // path safe if the helper (in the always-loaded main file) is ever unavailable.
3983 - $response = wp_safe_remote_get($pdf_source, [
3984 - 'timeout' => 60,
3985 - 'headers' => ['User-Agent' => function_exists('mxchat_ingest_user_agent') ? mxchat_ingest_user_agent() : 'MxChat PDF Processor']
3986 - ]);
3987 -
3988 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3989 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3990 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
1970 +
1971 + file_put_contents($temp_file, wp_remote_retrieve_body($response));
1972 +
1973 + // Validate that the downloaded file is a PDF
1974 + $mime_type = mime_content_type($temp_file);
1975 + if ($mime_type !== 'application/pdf') {
1976 + //error_log(esc_html__("Invalid MIME type detected for PDF: ", 'mxchat') . $mime_type);
1977 + unlink($temp_file);
3991 1978 return false;
3992 1979 }
3993 -
3994 - global $wp_filesystem;
3995 - if (empty($wp_filesystem)) {
3996 - require_once ABSPATH . 'wp-admin/includes/file.php';
3997 - WP_Filesystem();
3998 - }
3999 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
4000 - //error_log("✅ PDF downloaded successfully");
4001 1980 } else {
1981 + // For local files, use the provided path directly
4002 1982 $temp_file = $pdf_source;
4003 - //error_log("Using local PDF file: " . $temp_file);
4004 1983 }
4005 -
4006 - // Parse PDF
4007 - //error_log("Parsing PDF with basic parser...");
4008 - mxchat_load_pdf_parser();
1984 +
1985 + // Parse and process the PDF
4009 1986 $parser = new \Smalot\PdfParser\Parser();
4010 1987 $pdf = $parser->parseFile($temp_file);
4011 1988 $pages = $pdf->getPages();
4012 -
4013 - //error_log("PDF contains " . count($pages) . " pages");
4014 -
1989 +
4015 1990 if (count($pages) > $max_pages) {
4016 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
4017 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
1991 + //error_log(esc_html__("PDF exceeds the maximum allowed pages: ", 'mxchat') . count($pages));
1992 + if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
4018 1993 unlink($temp_file);
4019 1994 }
4020 - return 'too_many_pages';
1995 + return esc_html__('too_many_pages', 'mxchat');
4021 1996 }
4022 -
1997 +
4023 1998 $embeddings = [];
4024 - $processed_pages = 0;
4025 -
4026 1999 foreach ($pages as $page_number => $page) {
4027 2000 $text = $page->getText();
4028 -
2001 +
2002 + // Ensure text is non-empty before generating embeddings
4029 2003 if (empty(trim($text))) {
4030 - //error_log("Skipping empty page: " . ($page_number + 1));
2004 + //error_log(esc_html__("Skipping empty page: ", 'mxchat') . ($page_number + 1));
4031 2005 continue;
4032 2006 }
4033 -
4034 - $text = $this->mxchat_clean_text($text);
4035 -
2007 +
4036 2008 $embedding = $this->mxchat_generate_embedding(
4037 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
2009 + esc_html__("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
4038 2010 $this->options['api_key']
4039 2011 );
4040 -
2012 +
4041 2013 if ($embedding) {
4042 2014 $embeddings[] = [
4043 2015 'page_number' => $page_number + 1,
4044 2016 'embedding' => $embedding,
4045 2017 'text' => $text,
4046 - 'enhanced' => false, // CLEARLY MARK AS BASIC
4047 - 'processing_method' => 'basic_pdf_parser'
4048 2018 ];
4049 - $processed_pages++;
2019 + } else {
2020 + //error_log(esc_html__("Failed to generate embedding for page ", 'mxchat') . ($page_number + 1));
4050 2021 }
4051 2022 }
4052 -
4053 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
4054 -
4055 - // Cleanup
4056 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
2023 +
2024 + // Clean up downloaded file if it was from URL
2025 + if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
4057 2026 unlink($temp_file);
4058 2027 }
4059 -
4060 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
2028 +
4061 2029 return $embeddings;
4062 -
2030 +
4063 2031 } catch (\Exception $e) {
4064 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
2032 + // //error_log(esc_html__("Error parsing or processing PDF: ", 'mxchat') . $e->getMessage());
2033 +
2034 + // Cleanup in case of exception
4065 2035 if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
4066 2036 unlink($temp_file);
4067 2037 }
4068 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
4069 - return false;
4070 - }
4071 -}
4072 2038
4073 -
4074 -/**
4075 - * Validate PDF URL for security
4076 - * Prevents SSRF attacks by blocking dangerous URLs
4077 - */
4078 -
4079 -private function mxchat_is_safe_pdf_url($url) {
4080 - // Use WordPress core function for comprehensive validation
4081 - // This blocks localhost, private IPs, and reserved IP ranges
4082 - $validated_url = wp_http_validate_url($url);
4083 -
4084 - if ($validated_url === false) {
4085 2039 return false;
4086 2040 }
4087 -
4088 - // Additional check: only allow HTTP/HTTPS schemes
4089 - $parsed = parse_url($url);
4090 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
4091 - return false;
4092 - }
4093 -
4094 - return true;
4095 2041 }
4096 -
4097 -
4098 -private function mxchat_clean_text($text) {
4099 - // Remove excessive whitespace
4100 - $text = preg_replace('/\s+/', ' ', $text);
4101 -
4102 - // Remove control characters except newlines and tabs
4103 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
4104 -
4105 - // Normalize line endings
4106 - $text = str_replace(["\r\n", "\r"], "\n", $text);
4107 -
4108 - // Trim whitespace
4109 - $text = trim($text);
4110 -
4111 - return $text;
4112 -}
4113 -
4114 2042 private function find_relevant_pdf_pages($query_embedding, $embeddings) {
4115 2043 //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
4116 2044
4117 2045 $most_relevant = null;
@@ -4134,14 +2062,11 @@
4134 2062 }
4135 2063
4136 2064 return [];
4137 2065 }
4138 -
4139 -
2066 +// Add this to your class
4140 2067 public function handle_pdf_upload() {
4141 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
4142 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4143 - }
2068 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
4144 2069
4145 2070 if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
4146 2071 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
4147 2072 return;
@@ -4146,29 +2071,12 @@
4146 2071 wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
4147 2072 return;
4148 2073 }
4149 2074
4150 - // SECURITY FIX: Check if PDF uploads are enabled in settings
4151 - $options = get_option('mxchat_options', array());
4152 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
4153 -
4154 - if ($show_pdf_button !== 'on') {
4155 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
4156 - return;
4157 - }
4158 -
4159 2075 $file = $_FILES['pdf_file'];
4160 2076 $session_id = sanitize_text_field($_POST['session_id']);
4161 2077 $original_filename = sanitize_text_field($file['name']);
4162 2078
4163 - // Update session owner if it changed (e.g. IP changed due to network switch)
4164 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
4165 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
4166 -
4167 - if (!$session_owner || $session_owner !== $current_user_identifier) {
4168 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
4169 - }
4170 -
4171 2079 $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
4172 2080 if ($file_type['type'] !== 'application/pdf') {
4173 2081 wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
4174 2082 return;
@@ -4174,12 +2082,9 @@
4174 2082 return;
4175 2083 }
4176 2084
4177 2085 $upload_dir = wp_upload_dir();
4178 -
4179 - // SECURITY FIX: Generate random filename without exposing session_id
4180 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
4181 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
2086 + $pdf_filename = 'mxchat_' . $session_id . '_' . time() . '.pdf';
4182 2087 $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
4183 2088
4184 2089 if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
4185 2090 wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
@@ -4210,9 +2115,8 @@
4210 2115 return;
4211 2116 }
4212 2117
4213 2118 if (!empty($embeddings)) {
4214 - // Store the mapping between session and the random filename
4215 2119 set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
4216 2120 set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
4217 2121 set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
4218 2122 set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
@@ -4233,11 +2137,9 @@
4233 2137 wp_send_json_error($error_message);
4234 2138 return;
4235 2139 }
4236 2140 public function handle_pdf_remove() {
4237 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
4238 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4239 - }
2141 + check_ajax_referer('mxchat_chat_nonce', 'nonce');
4240 2142
4241 2143 if (empty($_POST['session_id'])) {
4242 2144 wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
4243 2145 wp_die();
@@ -4258,8 +2160,10 @@
4258 2160 wp_die();
4259 2161 }
4260 2162
4261 2163
2164 +
2165 +
4262 2166 function mxchat_fetch_new_messages() {
4263 2167 $session_id = sanitize_text_field($_POST['session_id']);
4264 2168 $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
4265 2169 $persistence_enabled = $_POST['persistence_enabled'] === 'true';
@@ -4272,31 +2176,14 @@
4272 2176 }
4273 2177
4274 2178 $history = get_option("mxchat_history_{$session_id}", []);
4275 2179
4276 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
4277 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
4278 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
4279 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
4280 -
4281 2180 $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
4282 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
4283 -
4284 2181 // If persistence is enabled, show all new messages
4285 2182 if ($persistence_enabled) {
4286 - $has_id = !empty($message['id']);
4287 - $is_agent = $message['role'] === 'agent';
4288 -
4289 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
4290 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
4291 - $is_newer = true;
4292 - } else {
4293 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
4294 - }
4295 -
4296 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
4297 -
4298 - return $has_id && $is_newer && $is_agent;
2183 + return !empty($message['id']) &&
2184 + strcmp($message['id'], $last_seen_id) > 0 &&
2185 + $message['role'] === 'agent';
4299 2186 }
4300 2187
4301 2188 // If persistence is disabled, only show messages after initial timestamp
4302 2189 return !empty($message['id']) &&
@@ -4303,30 +2190,21 @@
4303 2190 $message['role'] === 'agent' &&
4304 2191 $message['timestamp'] > $initial_timestamp;
4305 2192 });
4306 2193
4307 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
2194 + //error_log(esc_html__("New agent messages fetched for session $session_id. Last seen ID: $last_seen_id", 'mxchat'));
4308 2195
4309 - // Include current chat mode so frontend can detect agent→AI transitions
4310 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
4311 -
4312 2196 wp_send_json_success([
4313 - 'new_messages' => array_values($new_messages),
4314 - 'chat_mode' => $chat_mode
2197 + 'new_messages' => array_values($new_messages)
4315 2198 ]);
4316 2199 wp_die();
4317 2200 }
2201 +
2202 +
4318 2203 public function mxchat_live_agent_handover($message, $user_id, $session_id) {
4319 - // First check if live agents are available.
4320 - // Outside the SLACK availability schedule this behaves exactly like the
4321 - // manual toggle being off — same away message, same stay-in-AI-mode path
4322 - // (plans 8ccaa2 + 99d7a4: each channel owns its own schedule). The schedule
4323 - // normally stops the tool being offered at all; this is the backstop for
4324 - // any path that calls the handover directly.
2204 + // First check if live agents are available
4325 2205 $live_agent_available = $this->options['live_agent_status'] ?? 'off';
4326 - $within_hours = !class_exists('MxChat_Live_Agent_Schedule')
4327 - || MxChat_Live_Agent_Schedule::is_within_hours('slack');
4328 - if ($live_agent_available !== 'on' || !$within_hours) {
2206 + if ($live_agent_available !== 'on') {
4329 2207 $away_message = $this->options['live_agent_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4330 2208 $this->fallbackResponse = [
4331 2209 'text' => $away_message,
4332 2210 'html' => '',
@@ -4341,101 +2219,18 @@
4341 2219 ]);
4342 2220 wp_die();
4343 2221 }
4344 2222
4345 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4346 -
4347 - if (empty($slack_bot_token)) {
2223 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
2224 + if (empty($slack_webhook_url)) {
4348 2225 return false;
4349 2226 }
4350 2227
4351 - // Check if channel already exists for this session
4352 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4353 -
4354 - if (empty($channel_id)) {
4355 - // Create new channel with session ID as name
4356 - $channel_name = $this->generate_channel_name($session_id);
4357 -
4358 - //error_log("Attempting to create channel: $channel_name");
4359 -
4360 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4361 - 'headers' => [
4362 - 'Content-Type' => 'application/json',
4363 - 'Authorization' => 'Bearer ' . $slack_bot_token
4364 - ],
4365 - 'body' => json_encode([
4366 - 'name' => $channel_name,
4367 - 'is_private' => false // Public channel - anyone in workspace can join
4368 - ])
4369 - ]);
4370 -
4371 - if (!is_wp_error($response)) {
4372 - $response_body = wp_remote_retrieve_body($response);
4373 - $response_data = json_decode($response_body, true);
4374 -
4375 - //error_log("Channel creation response: " . $response_body);
4376 -
4377 - if (isset($response_data['ok']) && $response_data['ok']) {
4378 - $channel_id = $response_data['channel']['id'];
4379 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4380 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4381 - update_option("mxchat_channel_{$session_id}", $channel_id);
4382 -
4383 - // Auto-invite agents to the channel
4384 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4385 -
4386 - if (!empty($agent_user_ids)) {
4387 - // Parse user IDs (one per line)
4388 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4389 -
4390 - foreach ($user_ids as $user_id_to_invite) {
4391 - //error_log("Inviting user to channel: $user_id_to_invite");
4392 -
4393 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4394 - 'headers' => [
4395 - 'Content-Type' => 'application/json',
4396 - 'Authorization' => 'Bearer ' . $slack_bot_token
4397 - ],
4398 - 'body' => json_encode([
4399 - 'channel' => $channel_id,
4400 - 'users' => $user_id_to_invite
4401 - ])
4402 - ]);
4403 -
4404 - if (!is_wp_error($invite_response)) {
4405 - $invite_body = wp_remote_retrieve_body($invite_response);
4406 - $invite_data = json_decode($invite_body, true);
4407 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4408 -
4409 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4410 - //error_log("Successfully invited user $user_id_to_invite to channel");
4411 - } else {
4412 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4413 - }
4414 - } else {
4415 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4416 - }
4417 - }
4418 - } else {
4419 - //error_log("No agent user IDs configured for auto-invite");
4420 - }
4421 - } else {
4422 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4423 - }
4424 - } else {
4425 - //error_log("WP Error creating channel: " . $response->get_error_message());
4426 - }
4427 -
4428 - if (empty($channel_id)) {
4429 - return false; // Failed to create channel
4430 - }
4431 - }
4432 -
4433 - // Get recent chat history
2228 + // Get recent chat history (last 5 messages)
4434 2229 $history = get_option("mxchat_history_{$session_id}", []);
4435 - $recent_history = array_slice($history, -5);
2230 + $recent_history = array_slice($history, -5); // Get last 5 messages
4436 2231
4437 - // Format conversation context
2232 + // Format conversation history
4438 2233 $conversation_context = "";
4439 2234 if (!empty($recent_history)) {
4440 2235 $conversation_context = "*Recent Conversation:*\n";
4441 2236 foreach ($recent_history as $hist_message) {
@@ -4446,301 +2241,84 @@
4446 2241 }
4447 2242
4448 2243 update_option("mxchat_mode_{$session_id}", 'agent');
4449 2244
4450 - // Send message to channel
4451 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4452 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4453 - $channel_message .= "*User ID:* `{$user_id}`\n";
2245 + $webhook_data = [
2246 + 'blocks' => [
2247 + [
2248 + 'type' => 'header',
2249 + 'text' => [
2250 + 'type' => 'plain_text',
2251 + 'text' => '🔔 New Live Agent Request',
2252 + 'emoji' => true
2253 + ]
2254 + ],
2255 + [
2256 + 'type' => 'section',
2257 + 'fields' => [
2258 + [
2259 + 'type' => 'mrkdwn',
2260 + 'text' => sprintf('*User ID:*\n`%s`', $user_id)
2261 + ],
2262 + [
2263 + 'type' => 'mrkdwn',
2264 + 'text' => sprintf('*Session ID:*\n`%s`', $session_id)
2265 + ]
2266 + ]
2267 + ]
2268 + ]
2269 + ];
4454 2270
4455 - // Surface the captured visitor identity so the agent knows who they're talking to —
4456 - // guest User IDs are 0, but the pre-chat gate / login / transcript often has name+email (plan-e2195b).
4457 - $visitor = $this->mxchat_get_visitor_identity($session_id);
4458 - if (!empty($visitor['name']) && !empty($visitor['email'])) {
4459 - $channel_message .= "*Visitor:* {$visitor['name']} <{$visitor['email']}>\n";
4460 - } elseif (!empty($visitor['email'])) {
4461 - $channel_message .= "*Visitor:* <{$visitor['email']}>\n";
4462 - } elseif (!empty($visitor['name'])) {
4463 - $channel_message .= "*Visitor:* {$visitor['name']}\n";
2271 + // Add conversation history if exists
2272 + if (!empty($conversation_context)) {
2273 + $webhook_data['blocks'][] = [
2274 + 'type' => 'section',
2275 + 'text' => [
2276 + 'type' => 'mrkdwn',
2277 + 'text' => $conversation_context
2278 + ]
2279 + ];
4464 2280 }
4465 - $channel_message .= "\n";
4466 2281
4467 - if (!empty($conversation_context)) {
4468 - $channel_message .= $conversation_context;
4469 - }
4470 -
4471 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4472 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
2282 + // Add the current message
2283 + $webhook_data['blocks'][] = [
2284 + 'type' => 'section',
2285 + 'text' => [
2286 + 'type' => 'mrkdwn',
2287 + 'text' => sprintf('*Current Message:*\n%s', $message)
2288 + ]
2289 + ];
4473 2290
4474 - wp_remote_post('https://slack.com/api/chat.postMessage', [
2291 + // Add the reply button
2292 + $webhook_data['blocks'][] = [
2293 + 'type' => 'actions',
2294 + 'elements' => [
2295 + [
2296 + 'type' => 'button',
2297 + 'text' => [
2298 + 'type' => 'plain_text',
2299 + 'text' => '✍️ Reply',
2300 + 'emoji' => true
2301 + ],
2302 + 'value' => $session_id,
2303 + 'action_id' => 'reply_to_user',
2304 + 'style' => 'primary'
2305 + ]
2306 + ]
2307 + ];
2308 +
2309 + $response = wp_remote_post($slack_webhook_url, [
2310 + 'body' => json_encode($webhook_data),
4475 2311 'headers' => [
4476 2312 'Content-Type' => 'application/json',
4477 - 'Authorization' => 'Bearer ' . $slack_bot_token
4478 2313 ],
4479 - 'body' => json_encode([
4480 - 'channel' => $channel_id,
4481 - 'text' => $channel_message,
4482 - 'mrkdwn' => true
4483 - ])
4484 2314 ]);
4485 2315
4486 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4487 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4488 -
4489 - $this->fallbackResponse = [
4490 - 'text' => $success_message,
4491 - 'html' => '',
4492 - 'images' => [],
4493 - 'chat_mode' => 'agent'
4494 - ];
4495 -
4496 - wp_send_json([
4497 - 'success' => true,
4498 - 'text' => $success_message,
4499 - 'html' => '',
4500 - 'chat_mode' => 'agent',
4501 - 'session_id' => $session_id,
4502 - 'fallbackResponse' => $this->fallbackResponse
4503 - ]);
4504 - wp_die();
4505 -}
4506 -
4507 -private function generate_channel_name($session_id) {
4508 - $email = null;
4509 - $name = null;
4510 -
4511 - // 1. First priority: Check if user is logged in and get their info
4512 - if (is_user_logged_in()) {
4513 - $current_user = wp_get_current_user();
4514 - if (!empty($current_user->user_email)) {
4515 - $email = $current_user->user_email;
4516 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4517 - }
4518 - if (!empty($current_user->display_name)) {
4519 - $name = $current_user->display_name;
4520 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4521 - }
4522 - }
4523 -
4524 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4525 - if (empty($email)) {
4526 - $email_option_key = "mxchat_email_{$session_id}";
4527 - $saved_email = get_option($email_option_key);
4528 - if (!empty($saved_email)) {
4529 - $email = $saved_email;
4530 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4531 - }
4532 - }
4533 -
4534 - if (empty($name)) {
4535 - $name_option_key = "mxchat_name_{$session_id}";
4536 - $saved_name = get_option($name_option_key);
4537 - if (!empty($saved_name)) {
4538 - $name = $saved_name;
4539 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4540 - }
4541 - }
4542 -
4543 - // 3. Third priority: Check existing chat transcript for email/name
4544 - if (empty($email) || empty($name)) {
4545 - global $wpdb;
4546 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4547 - $existing_data = $wpdb->get_row($wpdb->prepare(
4548 - "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",
4549 - $session_id
4550 - ));
4551 -
4552 - if ($existing_data) {
4553 - if (empty($email) && !empty($existing_data->user_email)) {
4554 - $email = $existing_data->user_email;
4555 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4556 - }
4557 - if (empty($name) && !empty($existing_data->user_name)) {
4558 - $name = $existing_data->user_name;
4559 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4560 - }
4561 - }
4562 - }
4563 -
4564 - // 4. Generate channel name based on priority: Name > Email > Session ID
4565 - $channel_name = '';
4566 -
4567 - if (!empty($name)) {
4568 - // Convert name to valid Slack channel name
4569 - $base_name = strtolower(trim($name));
4570 - // Replace spaces and invalid characters
4571 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4572 - $base_name = preg_replace('/\s+/', '-', $base_name);
4573 - $base_name = trim($base_name, '-');
4574 -
4575 - // Get last 4 characters of session ID for uniqueness
4576 - $session_suffix = substr($session_id, -4);
4577 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4578 -
4579 - // Slack channel names have a 21 character limit
4580 - if (strlen($channel_name) > 21) {
4581 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4582 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4583 - $truncated_name = substr($base_name, 0, $available_space);
4584 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4585 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4586 - }
4587 -
4588 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4589 -
4590 - } elseif (!empty($email)) {
4591 - // Convert email to valid Slack channel name (your existing logic)
4592 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4593 - // Remove any remaining invalid characters
4594 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4595 - // Ensure it doesn't end with a hyphen
4596 - $channel_name = rtrim($channel_name, '-');
4597 - // Slack channel names have a 21 character limit, so truncate if needed
4598 - if (strlen($channel_name) > 21) {
4599 - $channel_name = substr($channel_name, 0, 21);
4600 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4601 - }
4602 -
4603 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4604 -
4605 - } else {
4606 - // Fallback to session ID if no name or email found
4607 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4608 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4609 - }
4610 -
4611 - // Final validation - ensure channel name meets Slack requirements
4612 - if (strlen($channel_name) > 21) {
4613 - $channel_name = substr($channel_name, 0, 21);
4614 - $channel_name = rtrim($channel_name, '-');
4615 - }
4616 -
4617 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4618 - return $channel_name;
4619 -}
4620 -
4621 -/**
4622 - * Telegram Live Agent Handover
4623 - * Creates a forum topic in the Telegram group and notifies agents
4624 - */
4625 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4626 - // Check if Telegram agents are available. Telegram has its OWN availability
4627 - // schedule, independent of Slack's (plan 99d7a4 — each Integrations tab
4628 - // owns its scheduler). Backstop only; the tool is normally withheld
4629 - // off-hours.
4630 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4631 - $within_hours = !class_exists('MxChat_Live_Agent_Schedule')
4632 - || MxChat_Live_Agent_Schedule::is_within_hours('telegram');
4633 - if ($telegram_available !== 'on' || !$within_hours) {
4634 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4635 - $this->fallbackResponse = [
4636 - 'text' => $away_message,
4637 - 'html' => '',
4638 - 'images' => [],
4639 - 'chat_mode' => 'ai'
4640 - ];
4641 - wp_send_json([
4642 - 'text' => $away_message,
4643 - 'html' => '',
4644 - 'chat_mode' => 'ai',
4645 - 'session_id' => $session_id
4646 - ]);
4647 - wp_die();
4648 - }
4649 -
4650 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4651 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4652 -
4653 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
2316 + if (is_wp_error($response)) {
4654 2317 return false;
4655 2318 }
4656 2319
4657 - // Check if topic already exists for this session
4658 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4659 -
4660 - if (empty($topic_id)) {
4661 - // Generate topic name
4662 - $topic_name = $this->generate_telegram_topic_name($session_id);
4663 -
4664 - // Random icon color (Telegram forum topic colors)
4665 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4666 - $icon_color = $icon_colors[array_rand($icon_colors)];
4667 -
4668 - // Create forum topic
4669 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4670 - 'headers' => ['Content-Type' => 'application/json'],
4671 - 'body' => json_encode([
4672 - 'chat_id' => $telegram_group_id,
4673 - 'name' => $topic_name,
4674 - 'icon_color' => $icon_color
4675 - ])
4676 - ]);
4677 -
4678 - if (!is_wp_error($response)) {
4679 - $response_body = wp_remote_retrieve_body($response);
4680 - $response_data = json_decode($response_body, true);
4681 -
4682 - if (isset($response_data['ok']) && $response_data['ok']) {
4683 - $topic_id = $response_data['result']['message_thread_id'];
4684 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4685 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4686 - }
4687 - }
4688 -
4689 - if (empty($topic_id)) {
4690 - return false; // Failed to create topic
4691 - }
4692 - }
4693 -
4694 - // Get recent chat history
4695 - $history = get_option("mxchat_history_{$session_id}", []);
4696 - $recent_history = array_slice($history, -5);
4697 -
4698 - // Format conversation context for Telegram (HTML format)
4699 - $conversation_context = "";
4700 - if (!empty($recent_history)) {
4701 - $conversation_context = "<b>Recent Conversation:</b>\n";
4702 - foreach ($recent_history as $hist_message) {
4703 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4704 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4705 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4706 - }
4707 - $conversation_context .= "\n";
4708 - }
4709 -
4710 - // Get user info
4711 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4712 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4713 -
4714 - // Update session mode
4715 - update_option("mxchat_mode_{$session_id}", 'agent');
4716 -
4717 - // Send initial message to topic
4718 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4719 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4720 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4721 - $topic_message .= "<b>User:</b> {$user_name}\n";
4722 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4723 -
4724 - if (!empty($conversation_context)) {
4725 - $topic_message .= $conversation_context;
4726 - }
4727 -
4728 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4729 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4730 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4731 -
4732 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4733 - 'headers' => ['Content-Type' => 'application/json'],
4734 - 'body' => json_encode([
4735 - 'chat_id' => $telegram_group_id,
4736 - 'message_thread_id' => $topic_id,
4737 - 'text' => $topic_message,
4738 - 'parse_mode' => 'HTML'
4739 - ])
4740 - ]);
4741 -
4742 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
2320 + $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4743 2321 $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4744 2322
4745 2323 $this->fallbackResponse = [
4746 2324 'text' => $success_message,
@@ -4758,278 +2336,79 @@
4758 2336 'fallbackResponse' => $this->fallbackResponse
4759 2337 ]);
4760 2338 wp_die();
4761 2339 }
4762 -
4763 -/**
4764 - * Generate topic name for Telegram forum
4765 - */
4766 -private function generate_telegram_topic_name($session_id) {
4767 - $name = null;
4768 - $email = null;
4769 -
4770 - // Check logged in user
4771 - if (is_user_logged_in()) {
4772 - $current_user = wp_get_current_user();
4773 - if (!empty($current_user->display_name)) {
4774 - $name = $current_user->display_name;
4775 - }
4776 - if (!empty($current_user->user_email)) {
4777 - $email = $current_user->user_email;
4778 - }
4779 - }
4780 -
4781 - // Check session data
4782 - if (empty($name)) {
4783 - $name = get_option("mxchat_name_{$session_id}");
4784 - }
4785 - if (empty($email)) {
4786 - $email = get_option("mxchat_email_{$session_id}");
4787 - }
4788 -
4789 - // Generate topic name
4790 - $session_suffix = substr($session_id, -6);
4791 -
4792 - if (!empty($name)) {
4793 - // Clean name for topic (max 128 chars in Telegram)
4794 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4795 - $clean_name = trim($clean_name);
4796 - if (strlen($clean_name) > 50) {
4797 - $clean_name = substr($clean_name, 0, 50);
4798 - }
4799 - return "Chat - {$clean_name} ({$session_suffix})";
4800 - } elseif (!empty($email)) {
4801 - // Use email prefix
4802 - $email_prefix = explode('@', $email)[0];
4803 - if (strlen($email_prefix) > 30) {
4804 - $email_prefix = substr($email_prefix, 0, 30);
4805 - }
4806 - return "Chat - {$email_prefix} ({$session_suffix})";
4807 - }
4808 -
4809 - return "Chat - {$session_suffix}";
4810 -}
4811 -
4812 -/**
4813 - * Send user message to Telegram agent
4814 - */
4815 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4816 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4817 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4818 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4819 -
4820 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4821 - return false;
4822 - }
4823 -
4824 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4825 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4826 -
4827 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4828 - 'headers' => ['Content-Type' => 'application/json'],
4829 - 'body' => json_encode([
4830 - 'chat_id' => $group_id,
4831 - 'message_thread_id' => $topic_id,
4832 - 'text' => $user_message,
4833 - 'parse_mode' => 'HTML'
4834 - ])
4835 - ]);
4836 -
4837 - return !is_wp_error($response);
4838 -}
4839 -
4840 -/**
4841 - * Handle incoming Telegram webhook
4842 - */
4843 -public function handle_telegram_webhook(WP_REST_Request $request) {
4844 - $body = $request->get_body();
4845 - $data = json_decode($body, true);
4846 -
4847 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4848 -
4849 - // Handle message events from forum topics
4850 - if (isset($data['message'])) {
4851 - $message_data = $data['message'];
4852 -
4853 - // Skip if not from a forum topic
4854 - if (!isset($message_data['message_thread_id'])) {
4855 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4856 - return new WP_REST_Response(['ok' => true]);
4857 - }
4858 -
4859 - // Skip bot messages
4860 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4861 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4862 - return new WP_REST_Response(['ok' => true]);
4863 - }
4864 -
4865 - $chat_id = $message_data['chat']['id'] ?? '';
4866 - $topic_id = $message_data['message_thread_id'];
4867 - $message_text = $message_data['text'] ?? '';
4868 - $message_id = $message_data['message_id'] ?? '';
4869 - $from = $message_data['from'] ?? [];
4870 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4871 - if (empty($agent_name)) {
4872 - $agent_name = $from['username'] ?? 'Agent';
4873 - }
4874 -
4875 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4876 -
4877 - // Skip empty messages
4878 - if (empty($message_text)) {
4879 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4880 - return new WP_REST_Response(['ok' => true]);
4881 - }
4882 -
4883 - // Find session ID by topic ID - cast to string for comparison
4884 - global $wpdb;
4885 - $topic_id_str = strval($topic_id);
4886 - $session_option = $wpdb->get_var(
4887 - $wpdb->prepare(
4888 - "SELECT option_name FROM {$wpdb->options}
4889 - WHERE option_name LIKE %s
4890 - AND option_value = %s",
4891 - 'mxchat_telegram_topic_%',
4892 - $topic_id_str
4893 - )
4894 - );
4895 -
4896 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4897 -
4898 - if ($session_option) {
4899 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4900 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4901 -
4902 - // Verify the group ID matches
4903 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4904 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4905 -
4906 - if (strval($stored_group_id) != strval($chat_id)) {
4907 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4908 - return new WP_REST_Response(['ok' => true]);
4909 - }
4910 -
4911 - // Check for closure commands
4912 - $lower_text = strtolower(trim($message_text));
4913 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4914 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4915 - // End the live agent session
4916 - update_option("mxchat_mode_{$session_id}", 'ai');
4917 -
4918 - // Save disconnect message
4919 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4920 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4921 -
4922 - // Notify in Telegram
4923 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4924 - if (!empty($telegram_bot_token)) {
4925 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4926 - 'headers' => ['Content-Type' => 'application/json'],
4927 - 'body' => json_encode([
4928 - 'chat_id' => $chat_id,
4929 - 'message_thread_id' => $topic_id,
4930 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4931 - 'parse_mode' => 'HTML'
4932 - ])
4933 - ]);
4934 -
4935 - // Optionally close the topic
4936 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4937 - 'headers' => ['Content-Type' => 'application/json'],
4938 - 'body' => json_encode([
4939 - 'chat_id' => $chat_id,
4940 - 'message_thread_id' => $topic_id
4941 - ])
4942 - ]);
4943 - }
4944 -
4945 - return new WP_REST_Response(['ok' => true]);
4946 - }
4947 -
4948 - // Deduplicate messages
4949 - $message_key = md5($session_id . $message_id . $message_text);
4950 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4951 -
4952 - if (in_array($message_key, $processed_messages)) {
4953 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4954 - return new WP_REST_Response(['ok' => true]);
4955 - }
4956 -
4957 - $processed_messages[] = $message_key;
4958 - if (count($processed_messages) > 50) {
4959 - $processed_messages = array_slice($processed_messages, -50);
4960 - }
4961 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4962 -
4963 - // Save the agent message - format with agent name prefix for proper parsing
4964 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4965 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4966 -
4967 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4968 -
4969 - // Verify the message was saved to history
4970 - $history = get_option("mxchat_history_{$session_id}", []);
4971 - $last_message = end($history);
4972 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4973 -
4974 - // Send confirmation back to Telegram
4975 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4976 - if (!empty($telegram_bot_token)) {
4977 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4978 - if (!get_transient($confirm_key)) {
4979 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4980 - 'headers' => ['Content-Type' => 'application/json'],
4981 - 'body' => json_encode([
4982 - 'chat_id' => $chat_id,
4983 - 'message_thread_id' => $topic_id,
4984 - 'text' => "✅ <i>Message sent to user</i>",
4985 - 'parse_mode' => 'HTML',
4986 - 'reply_to_message_id' => $message_id
4987 - ])
4988 - ]);
4989 - set_transient($confirm_key, true, 300);
4990 - }
4991 - }
4992 - } else {
4993 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4994 - }
4995 - } else {
4996 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4997 - }
4998 -
4999 - return new WP_REST_Response(['ok' => true]);
5000 -}
5001 -
5002 2340 public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
5003 - // Check if this is a Telegram agent session
5004 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
5005 - if (!empty($telegram_topic_id)) {
5006 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
5007 - }
2341 + $slack_webhook_url = $this->options['live_agent_webhook_url'] ?? '';
5008 2342
5009 - // Otherwise, try Slack
5010 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5011 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
5012 -
5013 - if (empty($slack_bot_token) || empty($channel_id)) {
2343 + if (empty($slack_webhook_url)) {
2344 + //error_log(esc_html__('Slack Webhook URL is not configured.', 'mxchat'));
5014 2345 return false;
5015 2346 }
5016 2347
5017 - $user_message = "💬 *User:* {$message}";
2348 + $webhook_data = [
2349 + 'blocks' => [
2350 + [
2351 + 'type' => 'header',
2352 + 'text' => [
2353 + 'type' => 'plain_text',
2354 + 'text' => esc_html__('📩 New Chat Message', 'mxchat'),
2355 + 'emoji' => true
2356 + ]
2357 + ],
2358 + [
2359 + 'type' => 'section',
2360 + 'fields' => [
2361 + [
2362 + 'type' => 'mrkdwn',
2363 + 'text' => sprintf(esc_html__('*User ID:*\n`%s`', 'mxchat'), $user_id)
2364 + ],
2365 + [
2366 + 'type' => 'mrkdwn',
2367 + 'text' => sprintf(esc_html__('*Session ID:*\n`%s`', 'mxchat'), $session_id)
2368 + ]
2369 + ]
2370 + ],
2371 + [
2372 + 'type' => 'section',
2373 + 'text' => [
2374 + 'type' => 'mrkdwn',
2375 + 'text' => sprintf(esc_html__('*Message:*\n%s', 'mxchat'), $message)
2376 + ]
2377 + ],
2378 + [
2379 + 'type' => 'actions',
2380 + 'elements' => [
2381 + [
2382 + 'type' => 'button',
2383 + 'text' => [
2384 + 'type' => 'plain_text',
2385 + 'text' => esc_html__('✍️ Reply', 'mxchat'),
2386 + 'emoji' => true
2387 + ],
2388 + 'value' => $session_id,
2389 + 'action_id' => 'reply_to_user',
2390 + 'style' => 'primary'
2391 + ]
2392 + ]
2393 + ]
2394 + ]
2395 + ];
5018 2396
5019 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
2397 + $response = wp_remote_post($slack_webhook_url, [
2398 + 'body' => json_encode($webhook_data),
5020 2399 'headers' => [
5021 2400 'Content-Type' => 'application/json',
5022 - 'Authorization' => 'Bearer ' . $slack_bot_token
5023 2401 ],
5024 - 'body' => json_encode([
5025 - 'channel' => $channel_id,
5026 - 'text' => $user_message,
5027 - 'mrkdwn' => true
5028 - ])
5029 2402 ]);
5030 2403
5031 - return !is_wp_error($response);
2404 + if (is_wp_error($response)) {
2405 + //error_log(esc_html__('Error sending message to Slack: ', 'mxchat') . $response->get_error_message());
2406 + return false;
2407 + }
2408 +
2409 + //error_log(esc_html__('Message sent to Slack successfully.', 'mxchat'));
2410 + return true;
5032 2411 }
5033 2412 public function handle_slack_interaction(WP_REST_Request $request) {
5034 2413 //error_log('Received Slack interaction');
5035 2414
@@ -5117,8 +2496,9 @@
5117 2496
5118 2497 // Default acknowledgment
5119 2498 return new WP_REST_Response(['ok' => true]);
5120 2499 }
2500 +
5121 2501 public function mxchat_handle_agent_response(WP_REST_Request $request) {
5122 2502 //error_log('Received agent response request');
5123 2503 //error_log('Request data: ' . print_r($request->get_params(), true));
5124 2504 // //error_log('Raw body: ' . file_get_contents('php://input'));
@@ -5163,255 +2543,29 @@
5163 2543 'response_type' => 'in_channel',
5164 2544 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
5165 2545 ], 200);
5166 2546 }
5167 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
5168 - // Update mode to AI
5169 - update_option("mxchat_mode_{$session_id}", 'ai');
5170 -
5171 - // Clear any existing PDF context to start fresh
5172 - $this->clear_pdf_transients($session_id);
5173 -
5174 - // Set the response with explicit chat_mode
5175 - $this->fallbackResponse = [
5176 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
5177 - 'html' => '',
5178 - 'images' => [],
5179 - 'chat_mode' => 'ai' // Ensure this is set
5180 - ];
5181 -
5182 - // Return the complete response array instead of just true
5183 - return $this->fallbackResponse;
5184 -}
5185 2547
5186 -/**
5187 - * Normalize Slack mrkdwn before relaying an agent's message to the web visitor.
5188 - * Slack's Events API auto-wraps URLs as <https://url> or <https://url|Label>, wraps
5189 - * mentions as <@U…>/<#C…|name>, and HTML-escapes &, <, >. Relayed raw, the visitor
5190 - * sees a broken/doubled link with a trailing > (plan-e2195b). Unwrap links FIRST, then
5191 - * unescape entities LAST so extracted URLs (which can contain &amp;) are not corrupted.
5192 - */
5193 -private function normalize_slack_text($text) {
5194 - if (!is_string($text) || $text === '') {
5195 - return $text;
5196 - }
5197 2548
5198 - $text = preg_replace_callback('/<([^>|]+)(?:\|([^>]*))?>/', function ($m) {
5199 - $target = $m[1];
5200 - $label = isset($m[2]) ? $m[2] : '';
2549 +public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
2550 + //error_log(esc_html__("Switching back to chatbot mode via intent.", 'mxchat'));
5201 2551
5202 - // User/channel mentions: <@U…> or <#C…|name> — prefer the human label, else drop the id.
5203 - if (isset($target[0]) && ($target[0] === '@' || $target[0] === '#')) {
5204 - return $label !== '' ? $label : '';
5205 - }
5206 - // mailto:/tel: — strip the scheme for display.
5207 - if (stripos($target, 'mailto:') === 0) {
5208 - $addr = substr($target, 7);
5209 - return ($label !== '' && $label !== $addr) ? "{$label} ({$addr})" : $addr;
5210 - }
5211 - if (stripos($target, 'tel:') === 0) {
5212 - $num = substr($target, 4);
5213 - return ($label !== '' && $label !== $num) ? "{$label} ({$num})" : $num;
5214 - }
5215 - // Regular URL: <url|Label> -> "Label (url)"; bare <url> -> "url".
5216 - if ($label !== '' && $label !== $target) {
5217 - return "{$label} ({$target})";
5218 - }
5219 - return $target;
5220 - }, $text);
2552 + // Just update mode to AI
2553 + update_option("mxchat_mode_{$session_id}", 'ai');
5221 2554
5222 - // Entity-unescape LAST (after link extraction) so &amp; inside URLs is repaired too.
5223 - $text = str_replace(array('&amp;', '&lt;', '&gt;'), array('&', '<', '>'), $text);
2555 + // Initialize states
2556 + $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
2557 + $this->productCardHtml = '';
5224 2558
5225 - return $text;
5226 -}
2559 + // Set the response message
2560 + $this->fallbackResponse['text'] = esc_html__('You are now chatting with the AI chatbot.', 'mxchat');
5227 2561
5228 -/**
5229 - * Resolve the visitor's name + email for a session, mirroring generate_channel_name()'s
5230 - * priority order: logged-in user, then the pre-chat gate options (mxchat_email_/mxchat_name_),
5231 - * then the chat transcript. Returns ['name' => ..., 'email' => ...] (either may be ''). plan-e2195b.
5232 - */
5233 -private function mxchat_get_visitor_identity($session_id) {
5234 - $email = '';
5235 - $name = '';
5236 -
5237 - if (is_user_logged_in()) {
5238 - $current_user = wp_get_current_user();
5239 - if (!empty($current_user->user_email)) { $email = $current_user->user_email; }
5240 - if (!empty($current_user->display_name)) { $name = $current_user->display_name; }
5241 - }
5242 -
5243 - if (empty($email)) {
5244 - $saved_email = get_option("mxchat_email_{$session_id}", '');
5245 - if (!empty($saved_email)) { $email = $saved_email; }
5246 - }
5247 - if (empty($name)) {
5248 - $saved_name = get_option("mxchat_name_{$session_id}", '');
5249 - if (!empty($saved_name)) { $name = $saved_name; }
5250 - }
5251 -
5252 - if (empty($email) || empty($name)) {
5253 - global $wpdb;
5254 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
5255 - $existing_data = $wpdb->get_row($wpdb->prepare(
5256 - "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",
5257 - $session_id
5258 - ));
5259 - if ($existing_data) {
5260 - if (empty($email) && !empty($existing_data->user_email)) { $email = $existing_data->user_email; }
5261 - if (empty($name) && !empty($existing_data->user_name)) { $name = $existing_data->user_name; }
5262 - }
5263 - }
5264 -
5265 - return array('name' => $name, 'email' => $email);
2562 + return true; // Intent was handled
5266 2563 }
5267 2564
5268 -public function handle_slack_messages(WP_REST_Request $request) {
5269 - // Log the incoming request for debugging
5270 - //error_log('Slack events request received: ' . $request->get_body());
5271 -
5272 - $body = $request->get_body();
5273 - $data = json_decode($body, true);
5274 -
5275 - // Handle Slack URL verification
5276 - if (isset($data['type']) && $data['type'] === 'url_verification') {
5277 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
5278 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
5279 - }
5280 -
5281 - // IMPORTANT: Handle Slack's event deduplication
5282 - if (isset($data['event_id'])) {
5283 - $event_id = $data['event_id'];
5284 - $processed_events = get_transient('mxchat_slack_events') ?: [];
5285 -
5286 - // Check if we've already processed this event
5287 - if (in_array($event_id, $processed_events)) {
5288 - //error_log("Duplicate event detected: $event_id");
5289 - return new WP_REST_Response(['ok' => true]);
5290 - }
5291 -
5292 - // Add this event to processed list
5293 - $processed_events[] = $event_id;
5294 - // Keep only last 100 events to prevent memory issues
5295 - if (count($processed_events) > 100) {
5296 - $processed_events = array_slice($processed_events, -100);
5297 - }
5298 - // Store for 1 hour
5299 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
5300 - }
5301 -
5302 - // Handle message events
5303 - if (isset($data['event']) && $data['event']['type'] === 'message') {
5304 - $event = $data['event'];
5305 -
5306 - // Skip bot messages and messages with subtypes (like bot_message)
5307 - if (isset($event['bot_id']) || isset($event['subtype'])) {
5308 - return new WP_REST_Response(['ok' => true]);
5309 - }
5310 -
5311 - // Additional check: Skip if this is a threaded reply to our confirmation
5312 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
5313 - return new WP_REST_Response(['ok' => true]);
5314 - }
5315 -
5316 - $channel_id = $event['channel'];
5317 - $message_text = $event['text'] ?? '';
5318 - $message_ts = $event['ts'] ?? '';
5319 2565
5320 - // Find session ID by looking for matching channel
5321 - global $wpdb;
5322 - $session_option = $wpdb->get_var(
5323 - $wpdb->prepare(
5324 - "SELECT option_name FROM {$wpdb->options}
5325 - WHERE option_name LIKE 'mxchat_channel_%'
5326 - AND option_value = %s",
5327 - $channel_id
5328 - )
5329 - );
5330 2566
5331 - if ($session_option) {
5332 - $session_id = str_replace('mxchat_channel_', '', $session_option);
5333 2567
5334 - // Create a unique key for this specific message
5335 - $message_key = md5($session_id . $message_ts . $message_text);
5336 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
5337 -
5338 - // Check if we've already processed this exact message
5339 - if (in_array($message_key, $processed_messages)) {
5340 - //error_log("Duplicate message detected for session $session_id");
5341 - return new WP_REST_Response(['ok' => true]);
5342 - }
5343 -
5344 - // Add to processed messages
5345 - $processed_messages[] = $message_key;
5346 - // Keep only last 50 messages per session
5347 - if (count($processed_messages) > 50) {
5348 - $processed_messages = array_slice($processed_messages, -50);
5349 - }
5350 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
5351 -
5352 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5353 -
5354 - // Handle agent ending the chat — transfer back to AI
5355 - // Format: "!endchat" or "!endchat <custom message to user>"
5356 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
5357 - update_option("mxchat_mode_{$session_id}", 'ai');
5358 -
5359 - // Extract custom message after !endchat, or use empty string
5360 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
5361 -
5362 - // Send the agent's custom farewell message if provided
5363 - if (!empty($custom_message)) {
5364 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($custom_message));
5365 - }
5366 -
5367 - // Confirm in Slack channel
5368 - if (!empty($slack_bot_token)) {
5369 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5370 - 'headers' => [
5371 - 'Content-Type' => 'application/json',
5372 - 'Authorization' => 'Bearer ' . $slack_bot_token
5373 - ],
5374 - 'body' => json_encode([
5375 - 'channel' => $channel_id,
5376 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
5377 - 'mrkdwn' => true
5378 - ])
5379 - ]);
5380 - }
5381 -
5382 - return new WP_REST_Response(['ok' => true]);
5383 - }
5384 -
5385 - // Save the agent message (normalize Slack link/entity formatting first — plan-e2195b)
5386 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($message_text));
5387 -
5388 - // Send confirmation back to Slack (only once)
5389 - if (!empty($slack_bot_token)) {
5390 - // Use a transient to prevent duplicate confirmations
5391 - $confirm_key = 'mxchat_confirm_' . $message_key;
5392 - if (!get_transient($confirm_key)) {
5393 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5394 - 'headers' => [
5395 - 'Content-Type' => 'application/json',
5396 - 'Authorization' => 'Bearer ' . $slack_bot_token
5397 - ],
5398 - 'body' => json_encode([
5399 - 'channel' => $channel_id,
5400 - 'text' => "✅ _Message sent to user_",
5401 - 'thread_ts' => $event['ts'] // Reply in thread
5402 - ])
5403 - ]);
5404 - // Set transient to prevent duplicate confirmations
5405 - set_transient($confirm_key, true, 300); // 5 minutes
5406 - }
5407 - }
5408 - }
5409 - }
5410 -
5411 - return new WP_REST_Response(['ok' => true]);
5412 -}
5413 -
5414 2568 // For the word upload handler
5415 2569 public function mxchat_handle_word_upload() {
5416 2570 // Delegate to word handler
5417 2571 $this->word_handler->mxchat_handle_word_upload();
@@ -5438,15 +2592,9 @@
5438 2592 try {
5439 2593 // Get options and selected model
5440 2594 $options = get_option('mxchat_options');
5441 2595 $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5442 -
5443 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
5444 - // Off by default so existing sites see byte-identical behavior.
5445 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5446 - return $this->mxchat_generate_embedding_custom($text);
5447 - }
5448 -
2596 +
5449 2597 // Determine endpoint and API key based on model
5450 2598 if (strpos($selected_model, 'voyage') === 0) {
5451 2599 $endpoint = 'https://api.voyageai.com/v1/embeddings';
5452 2600 $api_key = $options['voyage_api_key'] ?? '';
@@ -5458,20 +2606,8 @@
5458 2606 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5459 2607 'error_code' => 'missing_voyage_api_key'
5460 2608 ];
5461 2609 }
5462 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5463 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5464 - $api_key = $options['gemini_api_key'] ?? '';
5465 -
5466 - // Check if Gemini API key is missing
5467 - if (empty($api_key)) {
5468 - //error_log('Gemini API key is missing');
5469 - return [
5470 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5471 - 'error_code' => 'missing_gemini_api_key'
5472 - ];
5473 - }
5474 2610 } else {
5475 2611 $endpoint = 'https://api.openai.com/v1/embeddings';
5476 2612 // Use the passed API key for OpenAI
5477 2613
@@ -5493,49 +2629,26 @@
5493 2629 'error_code' => 'empty_embedding_text'
5494 2630 ];
5495 2631 }
5496 2632
5497 - // Prepare request body based on provider
5498 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5499 - // Gemini API format
5500 - $request_body = [
5501 - 'model' => 'models/' . $selected_model,
5502 - 'content' => [
5503 - 'parts' => [
5504 - ['text' => $text]
5505 - ]
5506 - ],
5507 - 'outputDimensionality' => 1536
5508 - ];
5509 -
5510 - // Prepare headers for Gemini (API key as query parameter)
5511 - $endpoint .= '?key=' . $api_key;
5512 - $headers = [
5513 - 'Content-Type' => 'application/json'
5514 - ];
5515 - } else {
5516 - // OpenAI/Voyage API format
5517 - $request_body = [
5518 - 'input' => $text,
5519 - 'model' => $selected_model
5520 - ];
5521 -
5522 - // Add output_dimension for voyage-3-large
5523 - if ($selected_model === 'voyage-3-large') {
5524 - $request_body['output_dimension'] = 2048;
5525 - }
5526 -
5527 - // Prepare headers for OpenAI/Voyage
5528 - $headers = [
5529 - 'Content-Type' => 'application/json',
5530 - 'Authorization' => 'Bearer ' . $api_key
5531 - ];
2633 + // Prepare request body with conditional output_dimension
2634 + $request_body = [
2635 + 'input' => $text,
2636 + 'model' => $selected_model
2637 + ];
2638 +
2639 + // Add output_dimension for voyage-3-large
2640 + if ($selected_model === 'voyage-3-large') {
2641 + $request_body['output_dimension'] = 2048;
5532 2642 }
5533 2643
5534 2644 // Prepare request arguments
5535 2645 $args = [
5536 2646 'body' => wp_json_encode($request_body),
5537 - 'headers' => $headers,
2647 + 'headers' => [
2648 + 'Content-Type' => 'application/json',
2649 + 'Authorization' => 'Bearer ' . $api_key,
2650 + ],
5538 2651 'timeout' => 60,
5539 2652 'redirection' => 5,
5540 2653 'blocking' => true,
5541 2654 'httpversion' => '1.0',
@@ -5609,31 +2722,16 @@
5609 2722 }
5610 2723
5611 2724 $response_body = json_decode(wp_remote_retrieve_body($response), true);
5612 2725
5613 - // Handle different response formats based on provider
5614 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5615 - // Gemini API response format
5616 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5617 - return $response_body['embedding']['values'];
5618 - } else {
5619 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5620 - return [
5621 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5622 - 'error_code' => 'invalid_gemini_embedding_response'
5623 - ];
5624 - }
2726 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
2727 + return $response_body['data'][0]['embedding'];
5625 2728 } else {
5626 - // OpenAI/Voyage API response format
5627 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5628 - return $response_body['data'][0]['embedding'];
5629 - } else {
5630 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5631 - return [
5632 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5633 - 'error_code' => 'invalid_embedding_response'
5634 - ];
5635 - }
2729 + //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
2730 + return [
2731 + 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
2732 + 'error_code' => 'invalid_embedding_response'
2733 + ];
5636 2734 }
5637 2735 } catch (Exception $e) {
5638 2736 //error_log('Embedding Exception: ' . $e->getMessage());
5639 2737 return [
@@ -5643,714 +2741,203 @@
5643 2741 }
5644 2742 }
5645 2743
5646 2744
5647 -/**
5648 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5649 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5650 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5651 - */
5652 -private function mxchat_generate_embedding_custom($text) {
5653 - if (empty($text)) {
5654 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5655 - }
5656 - $cfg = $this->mxchat_resolve_custom_provider();
5657 - if (empty($cfg['base_url'])) {
5658 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5659 - }
2745 +private function mxchat_find_relevant_content($user_embedding) {
2746 + //error_log('MXChat Vector Search: Starting content search...');
5660 2747
5661 - $options = get_option('mxchat_options');
5662 - $embed_url = $cfg['base_url'] . '/embeddings';
5663 - if (!empty($cfg['api_version'])) {
5664 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5665 - }
5666 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5667 - ? trim((string) $options['custom_provider_embedding_model'])
5668 - : $cfg['model'];
2748 + // Retrieve the add-on settings from the database.
2749 + $addon_options = get_option('mxchat_pinecone_addon_options', array());
5669 2750
5670 - $response = wp_remote_post($embed_url, [
5671 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5672 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5673 - 'timeout' => 60,
5674 - ]);
5675 - if (is_wp_error($response)) {
5676 - return [
5677 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5678 - 'error_code' => 'embedding_custom_connection_error',
5679 - ];
5680 - }
5681 - $status = wp_remote_retrieve_response_code($response);
5682 - $body = json_decode(wp_remote_retrieve_body($response), true);
5683 - if ($status !== 200) {
5684 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5685 - return [
5686 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5687 - 'error_code' => 'embedding_custom_api_error',
5688 - 'status_code' => $status,
5689 - ];
5690 - }
5691 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5692 - return $body['data'][0]['embedding'];
5693 - }
5694 - return [
5695 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5696 - 'error_code' => 'embedding_custom_invalid_response',
5697 - ];
5698 -}
2751 + // Determine whether Pinecone is enabled.
2752 + // We expect the sanitized setting to be a string '1' if enabled, otherwise '0'.
2753 + $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
5699 2754
5700 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5701 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
2755 + //error_log('Pinecone enabled flag: ' . $use_pinecone);
5702 2756
5703 - // Check for OpenAI Vector Store first (takes priority when enabled)
5704 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5705 -
5706 - if ($bot_vectorstore_config['use_vectorstore']) {
5707 - // Get current model to verify it's an OpenAI model
5708 - $bot_options = $this->get_bot_options($bot_id);
5709 - $mxchat_options = get_option('mxchat_options', array());
5710 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5711 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5712 -
5713 - if ($this->is_openai_chat_model($selected_model)) {
5714 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5715 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5716 - } else {
5717 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5718 - }
5719 - }
5720 -
5721 - // Get bot-specific Pinecone configuration
5722 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5723 -
5724 - // Debug: Log the Pinecone configuration
5725 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5726 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5727 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5728 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5729 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5730 -
5731 - // Determine whether to use Pinecone based on bot configuration
5732 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5733 -
5734 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5735 -
5736 - if ($use_pinecone) {
5737 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
2757 + if ($use_pinecone === 1) {
2758 + //error_log('MXChat Vector Search: Using Pinecone database');
2759 + return $this->find_relevant_content_pinecone($user_embedding);
5738 2760 } else {
5739 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
2761 + //error_log('MXChat Vector Search: Using WordPress database');
2762 + return $this->find_relevant_content_wordpress($user_embedding);
5740 2763 }
5741 2764 }
5742 2765
5743 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
2766 +
2767 +private function find_relevant_content_wordpress($user_embedding) {
5744 2768 global $wpdb;
5745 2769 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5746 - // Initialize similarity analysis storage
5747 - $this->last_similarity_analysis = [
5748 - 'knowledge_base_type' => 'WordPress Database',
5749 - 'bot_id' => $bot_id,
5750 - 'top_matches' => [],
5751 - 'threshold_used' => 0,
5752 - 'total_checked' => 0
5753 - ];
2770 + $cache_key = 'mxchat_system_prompt_embeddings';
2771 + $batch_size = 500;
5754 2772
5755 - // NEW: Initialize valid URLs array
5756 - $valid_urls = [];
2773 + // Log start of matching process
2774 + //error_log('[MXCHAT] Starting similarity matching process');
5757 2775
5758 - // Get bot-specific options for similarity threshold
5759 - $bot_options = $this->get_bot_options($bot_id);
5760 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
2776 + // Retrieve embeddings from cache or database
2777 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
2778 + if ($embeddings === false) {
2779 + //error_log('[MXCHAT] Cache miss - loading embeddings from database');
2780 + $embeddings = [];
2781 + $offset = 0;
5761 2782
5762 - // Get knowledge manager instance for role checking
5763 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2783 + // Load in batches and build cache
2784 + do {
2785 + $query = $wpdb->prepare(
2786 + "SELECT id, embedding_vector
2787 + FROM {$system_prompt_table}
2788 + LIMIT %d OFFSET %d",
2789 + $batch_size,
2790 + $offset
2791 + );
5764 2792
5765 - // Get base similarity threshold from bot options or default options
5766 - $similarity_threshold = isset($current_options['similarity_threshold'])
5767 - ? ((int) $current_options['similarity_threshold']) / 100
5768 - : 0.35;
5769 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5770 -
5771 - // Precompute bot_filter once, outside the streaming loop
5772 - $bot_filter = '';
5773 - if ($bot_id !== 'default') {
5774 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5775 - if ($column_exists) {
5776 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5777 - }
5778 - }
5779 -
5780 - // ===== STREAMING TOP-K PASS =====
5781 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5782 - // - top 10 by raw similarity (for the testing/debug display panel)
5783 - // - candidates above threshold with access (capped) for context assembly
5784 - // This bounds peak memory regardless of knowledge base size and avoids loading
5785 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5786 - $batch_size = 250;
5787 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5788 - $top_display = [];
5789 - $candidates = [];
5790 - $total_checked = 0;
5791 - $offset = 0;
5792 -
5793 - do {
5794 - $batch = $wpdb->get_results($wpdb->prepare(
5795 - "SELECT id, embedding_vector, source_url, role_restriction
5796 - FROM {$system_prompt_table}
5797 - WHERE 1=1 {$bot_filter}
5798 - LIMIT %d OFFSET %d",
5799 - $batch_size,
5800 - $offset
5801 - ));
5802 -
5803 - if (empty($batch)) {
5804 - break;
5805 - }
5806 -
5807 - foreach ($batch as $row) {
5808 - $database_embedding = $row->embedding_vector
5809 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5810 - : null;
5811 -
5812 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5813 - unset($database_embedding);
5814 - continue;
2793 + $batch = $wpdb->get_results($query);
2794 + if (empty($batch)) {
2795 + break;
5815 2796 }
5816 2797
5817 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5818 - unset($database_embedding);
2798 + $embeddings = array_merge($embeddings, $batch);
2799 + $offset += $batch_size;
5819 2800
5820 - $role_restriction = $row->role_restriction ?? 'public';
5821 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5822 - $source_url = $row->source_url ?? '';
2801 + // Free memory
2802 + unset($batch);
5823 2803
5824 - // Maintain top 10 display buffer (insert-if-beats-worst)
5825 - if (count($top_display) < 10) {
5826 - $top_display[] = [
5827 - 'id' => $row->id,
5828 - 'similarity' => $similarity,
5829 - 'source_url' => $source_url,
5830 - 'role_restriction' => $role_restriction,
5831 - 'has_access' => $has_access,
5832 - ];
5833 - usort($top_display, function ($a, $b) {
5834 - return $b['similarity'] <=> $a['similarity'];
5835 - });
5836 - } elseif ($similarity > $top_display[9]['similarity']) {
5837 - $top_display[9] = [
5838 - 'id' => $row->id,
5839 - 'similarity' => $similarity,
5840 - 'source_url' => $source_url,
5841 - 'role_restriction' => $role_restriction,
5842 - 'has_access' => $has_access,
5843 - ];
5844 - usort($top_display, function ($a, $b) {
5845 - return $b['similarity'] <=> $a['similarity'];
5846 - });
5847 - }
2804 + } while (true);
5848 2805
5849 - // Track candidates for context assembly (above threshold + has access)
5850 - if ($similarity >= $similarity_threshold && $has_access) {
5851 - $candidates[] = [
5852 - 'id' => $row->id,
5853 - 'similarity' => $similarity,
5854 - 'source_url' => $source_url,
5855 - ];
5856 - }
5857 -
5858 - $total_checked++;
2806 + if (empty($embeddings)) {
2807 + //error_log('[MXCHAT] No embeddings found in database');
2808 + return ''; // Return an empty string if no embeddings found
5859 2809 }
5860 -
5861 - unset($batch);
5862 -
5863 - // Trim candidates periodically to cap memory during long scans
5864 - if (count($candidates) > $max_candidates) {
5865 - usort($candidates, function ($a, $b) {
5866 - return $b['similarity'] <=> $a['similarity'];
5867 - });
5868 - $candidates = array_slice($candidates, 0, $max_candidates);
5869 - }
5870 -
5871 - $offset += $batch_size;
5872 - } while (true);
5873 -
5874 - if ($total_checked === 0) {
5875 - $this->current_valid_urls = [];
5876 - return '';
2810 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
2811 + //error_log('[MXCHAT] Cached ' . count($embeddings) . ' embeddings');
2812 + } else {
2813 + //error_log('[MXCHAT] Using ' . count($embeddings) . ' cached embeddings');
5877 2814 }
5878 2815
5879 - // Final candidates sort (best first)
5880 - if (count($candidates) > 1) {
5881 - usort($candidates, function ($a, $b) {
5882 - return $b['similarity'] <=> $a['similarity'];
5883 - });
5884 - }
5885 -
5886 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5887 - // Gather unique IDs we actually need (top_display + candidates) and pull
5888 - // article_content in bounded IN() batches. This avoids loading content for
5889 - // every row during the similarity scan.
5890 - $needed_ids = [];
5891 - foreach ($top_display as $item) {
5892 - $needed_ids[$item['id']] = true;
5893 - }
5894 - foreach ($candidates as $item) {
5895 - $needed_ids[$item['id']] = true;
5896 - }
5897 - $needed_ids = array_keys($needed_ids);
5898 -
5899 - $content_map = [];
5900 - if (!empty($needed_ids)) {
5901 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5902 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5903 - $rows = $wpdb->get_results($wpdb->prepare(
5904 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5905 - ...$chunk_ids
5906 - ));
5907 - foreach ($rows as $r) {
5908 - $content_map[$r->id] = $r->article_content;
2816 + // Initialize array to store relevant results with similarity scores
2817 + $relevant_results = [];
2818 +
2819 + // Get the similarity threshold from the main options array only
2820 + $main_options = get_option('mxchat_options', []);
2821 + $similarity_threshold = isset($main_options['similarity_threshold'])
2822 + ? ((int) $main_options['similarity_threshold']) / 100
2823 + : 0.8; // Default to 80%
2824 +
2825 + //error_log('[MXCHAT] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
2826 +
2827 + // Iterate through embeddings to calculate similarity
2828 + foreach ($embeddings as $embedding) {
2829 + $database_embedding = $embedding->embedding_vector
2830 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
2831 + : null;
2832 + if (is_array($database_embedding) && is_array($user_embedding)) {
2833 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
2834 +
2835 + // Log each similarity score over 0.5 to reduce log spam
2836 + if ($similarity > 0.1) {
2837 + //error_log(sprintf('[MXCHAT] ID: %d | Similarity Score: %.4f', $embedding->id, $similarity));
5909 2838 }
5910 - unset($rows);
2839 +
2840 + $relevant_results[] = [
2841 + 'id' => $embedding->id,
2842 + 'similarity' => $similarity
2843 + ];
5911 2844 }
2845 + // Free memory
2846 + unset($database_embedding);
5912 2847 }
5913 2848
5914 - // Build the all_similarities display array from the top 10
5915 - $all_similarities = [];
5916 - foreach ($top_display as $item) {
5917 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5918 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5919 - $is_chunk = $parsed_for_display['is_chunked'];
5920 - $chunk_meta = $parsed_for_display['metadata'];
5921 -
5922 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5923 - $source_display = $item['source_url'];
5924 - } else {
5925 - $content_preview = strip_tags($article_content_for_parse);
5926 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5927 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5928 - }
5929 -
5930 - $all_similarities[] = [
5931 - 'document_id' => $item['id'],
5932 - 'similarity' => $item['similarity'],
5933 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5934 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5935 - 'source_display' => $source_display,
5936 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5937 - 'used_for_context' => false,
5938 - 'role_restriction' => $item['role_restriction'],
5939 - 'has_access' => $item['has_access'],
5940 - 'filtered_out' => !$item['has_access'],
5941 - 'is_chunk' => $is_chunk,
5942 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5943 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5944 - ];
5945 - }
5946 -
5947 - // Build url_groups from candidates for chunk reassembly
5948 - $url_groups = array();
5949 - foreach ($candidates as $cand) {
5950 - $article_content = $content_map[$cand['id']] ?? '';
5951 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5952 - $is_chunked = $parsed['is_chunked'];
5953 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5954 - $text_content = $parsed['text'];
5955 -
5956 - $source_url = $cand['source_url'];
5957 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5958 -
5959 - if (!isset($url_groups[$group_key])) {
5960 - $url_groups[$group_key] = array(
5961 - 'source_url' => $source_url,
5962 - 'best_score' => 0,
5963 - 'is_chunked' => $is_chunked,
5964 - 'chunks' => array(),
5965 - 'single_text' => '',
5966 - 'single_id' => null
5967 - );
5968 - }
5969 -
5970 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5971 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5972 - }
5973 -
5974 - if ($is_chunked) {
5975 - $url_groups[$group_key]['is_chunked'] = true;
5976 - $url_groups[$group_key]['chunks'][] = array(
5977 - 'id' => $cand['id'],
5978 - 'score' => $cand['similarity'],
5979 - 'chunk_index' => $chunk_index,
5980 - 'text' => $text_content
5981 - );
5982 - } else {
5983 - $url_groups[$group_key]['single_text'] = $text_content;
5984 - $url_groups[$group_key]['single_id'] = $cand['id'];
5985 - }
5986 - }
5987 -
5988 - // Sort ALL similarities for testing display (highest first)
5989 - usort($all_similarities, function ($a, $b) {
2849 + // Filter and sort relevant results by similarity
2850 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
2851 + return $result['similarity'] >= $similarity_threshold;
2852 + });
2853 + usort($relevant_results, function ($a, $b) {
5990 2854 return $b['similarity'] <=> $a['similarity'];
5991 2855 });
5992 2856
5993 - // Sort URL groups by best score (highest first)
5994 - uasort($url_groups, function($a, $b) {
5995 - return $b['best_score'] <=> $a['best_score'];
5996 - });
2857 + // Log number of results that met threshold
2858 + //error_log('[MXCHAT] ' . count($relevant_results) . ' results met the similarity threshold');
5997 2859
5998 - // Get RAG sources limit from options (default 6, min 3, max 10)
5999 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6000 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6001 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
2860 + // Limit to the top 5 results
2861 + $top_results = array_slice($relevant_results, 0, 5);
6002 2862
6003 - // Take top N unique URLs based on user setting
6004 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
2863 + // Log the top matches
2864 + //error_log('[MXCHAT] Top matching results:');
2865 + foreach ($top_results as $index => $result) {
6005 2866
6006 - // Track which document IDs are used for context
6007 - $used_document_ids = [];
6008 - foreach ($top_urls as $group) {
6009 - if ($group['is_chunked']) {
6010 - foreach ($group['chunks'] as $chunk) {
6011 - $used_document_ids[] = $chunk['id'];
6012 - }
6013 - } elseif ($group['single_id']) {
6014 - $used_document_ids[] = $group['single_id'];
6015 - }
6016 2867 }
6017 2868
6018 - // Update the all_similarities array to mark which were actually used
6019 - foreach ($all_similarities as &$similarity_item) {
6020 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
6021 - }
6022 -
6023 - // Store top 10 for testing panel
6024 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
6025 - $this->last_similarity_analysis['total_checked'] = $total_checked;
6026 -
6027 - // Initialize final content
2869 + // Initialize the final content
6028 2870 $content = '';
6029 - $matches_used = 0;
6030 - $total_chunks_used = 0;
6031 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6032 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6033 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6034 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
6035 2871
6036 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6037 - // Use fresh options to ensure we get the latest setting value
6038 - $fresh_options = get_option('mxchat_options', []);
6039 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6040 -
6041 - // Build content from top sources
6042 - foreach ($top_urls as $group_key => $group) {
6043 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6044 -
6045 - // Stop if we've hit the total chunk limit
6046 - if ($total_chunks_used >= $max_total_chunks) {
6047 - break;
6048 - }
6049 -
6050 - $full_text = '';
6051 - $chunks_in_this_source = 1; // Default for non-chunked content
6052 -
6053 - if ($group['is_chunked']) {
6054 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6055 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6056 -
6057 - // Fetch chunks for this URL with limit
6058 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
6059 -
6060 - // If fetching all chunks fails, fall back to matched chunks
6061 - if (empty($full_text)) {
6062 - // Sort matched chunks by index and concatenate
6063 - usort($group['chunks'], function($a, $b) {
6064 - return $a['chunk_index'] <=> $b['chunk_index'];
6065 - });
6066 -
6067 - $chunk_texts = array();
6068 - $chunks_in_this_source = 0;
6069 - foreach ($group['chunks'] as $chunk) {
6070 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6071 - break;
6072 - }
6073 - $chunk_texts[] = $chunk['text'];
6074 - $chunks_in_this_source++;
6075 - }
6076 - $full_text = implode("\n\n", $chunk_texts);
2872 + // Fetch and combine content for the top results
2873 + foreach ($top_results as $result) {
2874 + $chunk_content = $this->fetch_content_with_product_links($result['id']);
2875 + // Check if the content is PDF-related and add surrounding pages
2876 + if (strpos($chunk_content, '{"document_type":"pdf"') !== false) {
2877 + //error_log('[MXCHAT] ID ' . $result['id'] . ' is PDF content, adding surrounding pages');
2878 + $surrounding_content = $wpdb->get_results($wpdb->prepare(
2879 + "SELECT id, article_content FROM {$system_prompt_table}
2880 + WHERE id IN (
2881 + (SELECT id FROM {$system_prompt_table} WHERE id < %d ORDER BY id DESC LIMIT 1),
2882 + (SELECT id FROM {$system_prompt_table} WHERE id > %d ORDER BY id ASC LIMIT 1)
2883 + )",
2884 + $result['id'],
2885 + $result['id']
2886 + ));
2887 + // Add previous content if it exists
2888 + if (!empty($surrounding_content[0])) {
2889 + $content .= $surrounding_content[0]->article_content . "\n\n";
6077 2890 }
6078 - } else {
6079 - $full_text = $group['single_text'];
6080 - $chunks_in_this_source = 1;
6081 - }
6082 -
6083 - if (!empty($full_text)) {
6084 - // Strip URLs from content if citation links are disabled
6085 - if (!$citation_links_enabled) {
6086 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6087 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
2891 + // Add the main chunk content
2892 + $content .= $chunk_content . "\n\n";
2893 + // Add next content if it exists
2894 + if (!empty($surrounding_content[1])) {
2895 + $content .= $surrounding_content[1]->article_content . "\n\n";
6088 2896 }
6089 -
6090 - // Use numbered reference for URL-based entries, plain info label for manual entries
6091 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6092 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6093 - $matches_used++;
6094 - $content .= "## Reference " . $matches_used . " ##\n";
6095 - $content .= $full_text . "\n\n";
6096 -
6097 - // Only include citation URLs if citation links are enabled
6098 - if ($citation_links_enabled) {
6099 - $valid_urls[] = $source_url;
6100 - $content .= "URL: " . $source_url . "\n\n";
6101 - }
6102 -
6103 - // Video-backed source → queue the consent-safe embed (03ba33)
6104 - $this->maybe_queue_youtube_embed($source_url, $full_text);
6105 - } else {
6106 - // Manual entry — no reference number, no citation
6107 - $content .= "## Information ##\n";
6108 - $content .= $full_text . "\n\n";
6109 - }
6110 -
6111 - // Extract any URLs from the text content itself (only if citation links enabled)
6112 - if ($citation_links_enabled) {
6113 - preg_match_all(
6114 - '#\bhttps?://[^\s<>"\']+#i',
6115 - $full_text,
6116 - $content_urls
6117 - );
6118 - if (!empty($content_urls[0])) {
6119 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6120 - }
6121 - }
6122 -
6123 - $total_chunks_used += $chunks_in_this_source;
6124 - }
6125 - }
6126 -
6127 - // NEW: Store unique valid URLs for validation
6128 - $this->current_valid_urls = array_unique($valid_urls);
6129 -
6130 - // Store sources and chunks counts for testing/transcript display
6131 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6132 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6133 -
6134 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6135 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6136 -
6137 - // Add response guidelines
6138 - if (empty($top_urls)) {
6139 - $content = "No reference information was found for this query.\n\n";
6140 - } else {
6141 - // Build response guidelines based on citation links setting
6142 - $content .= "\n## Response Guidelines ##\n" .
6143 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6144 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6145 - "If you don't have specific information or are uncertain about any details, it's always " .
6146 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6147 - "When information is incomplete, let them know you are unsure.\n\n";
6148 -
6149 - // Only add hyperlink instructions if citation links are enabled
6150 - if ($citation_links_enabled) {
6151 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6152 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6153 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6154 2897 } else {
6155 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6156 - "Simply provide helpful answers based on the reference information without citing sources.";
2898 + // For non-PDF content, add directly
2899 + $content .= $chunk_content . "\n\n";
6157 2900 }
6158 2901 }
6159 2902
2903 + // Log content length
2904 + //error_log('[MXCHAT] Retrieved content length: ' . strlen(trim($content)) . ' characters');
2905 +
6160 2906 return trim($content);
6161 2907 }
6162 2908
6163 -/**
6164 - * plan-mxchat-20260717-03ba33 — if a KB source used for context is a single
6165 - * YouTube video, queue ONE consent-safe embed for the response html channel.
6166 - * Called from BOTH retrieval builders (WordPress DB + Pinecone) inside their
6167 - * real-URL winner branch, in ranked order — so the first (best) video wins and
6168 - * later matches are ignored. Only KB/admin-ingested sources ever reach this
6169 - * point; a URL a visitor pastes in chat never does.
6170 - */
6171 -private function maybe_queue_youtube_embed($source_url, $full_text) {
6172 - if (!empty($this->videoEmbedHtml)) {
6173 - return; // one video per response
6174 - }
6175 - $video_id = MxChat_Utils::parse_youtube_id($source_url);
6176 - if (empty($video_id)) {
6177 - return;
6178 - }
6179 - // Ingestion writes "YouTube Video: {title}" / "Channel: {name}" / "URL: …"
6180 - // header lines into the indexed text. NOTE: when citation links are
6181 - // disabled the winner loop collapses ALL whitespace to single spaces
6182 - // before this runs, so the title must be terminated by the next header
6183 - // label, not by end-of-line. Fall back to a generic label when absent
6184 - // (e.g. a YouTube watch page imported through the plain URL source).
6185 - $title = '';
6186 - if (preg_match('/YouTube Video:\s*(.+?)(?=\s+Channel:\s|\s+URL:\s|\r|\n|$)/i', (string) $full_text, $m)) {
6187 - $title = trim(mb_substr(trim($m[1]), 0, 140));
6188 - if (preg_match('#^https?://#i', $title)) {
6189 - $title = ''; // header carried the URL, not a real title
6190 - }
6191 - }
6192 - $this->videoEmbedHtml = $this->build_youtube_embed_html($video_id, $title, $source_url);
6193 -}
6194 2909
6195 -/**
6196 - * Consent-safe click-to-load YouTube facade. No Google iframe is created until
6197 - * the visitor taps play (chat-script.js swaps the facade for a
6198 - * youtube-nocookie.com iframe). The caption always carries a plain "Watch on
6199 - * YouTube" link, which is also the graceful degrade on strict-CSP sites where
6200 - * third-party frames are blocked.
6201 - */
6202 -private function build_youtube_embed_html($video_id, $title, $watch_url) {
6203 - $video_id = preg_replace('/[^A-Za-z0-9_-]/', '', (string) $video_id);
6204 - if ($video_id === '') {
6205 - return '';
6206 - }
6207 - $thumb = 'https://i.ytimg.com/vi/' . $video_id . '/hqdefault.jpg';
6208 - $label = ($title !== '') ? $title : __('YouTube video', 'mxchat');
6209 -
6210 - $html = '<div class="mxchat-youtube-embed" data-video-id="' . esc_attr($video_id) . '">';
6211 - $html .= '<button type="button" class="mxchat-youtube-facade" aria-label="' . esc_attr(sprintf(__('Play video: %s', 'mxchat'), $label)) . '">';
6212 - $html .= '<img class="mxchat-youtube-thumb" src="' . esc_url($thumb) . '" alt="' . esc_attr($label) . '" loading="lazy" />';
6213 - $html .= '<span class="mxchat-youtube-play" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="22" height="22" fill="currentColor" aria-hidden="true"><path d="M8 5v14l11-7z"/></svg></span>';
6214 - $html .= '</button>';
6215 - $html .= '<div class="mxchat-youtube-caption">';
6216 - $html .= '<span class="mxchat-youtube-title">' . esc_html($label) . '</span>';
6217 - $html .= '<a class="mxchat-youtube-link" href="' . esc_url($watch_url) . '" target="_blank" rel="noopener noreferrer">' . esc_html__('Watch on YouTube', 'mxchat') . '</a>';
6218 - $html .= '</div>';
6219 - $html .= '</div>';
6220 - return $html;
6221 -}
6222 -
6223 -/**
6224 - * Fetch and reassemble chunks for a URL from WordPress database
6225 - *
6226 - * @param string $source_url The source URL to fetch chunks for
6227 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
6228 - * @param int &$chunk_count Reference to store the actual number of chunks returned
6229 - * @return string Reassembled content from chunks
6230 - */
6231 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
6232 - global $wpdb;
6233 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
6234 -
6235 - // Fetch all rows with this source_url
6236 - $rows = $wpdb->get_results($wpdb->prepare(
6237 - "SELECT article_content FROM {$table}
6238 - WHERE source_url = %s
6239 - ORDER BY id ASC",
6240 - $source_url
6241 - ));
6242 -
6243 - if (empty($rows)) {
6244 - $chunk_count = 0;
6245 - return '';
6246 - }
6247 -
6248 - // Parse and sort chunks by index
6249 - $chunks = array();
6250 - foreach ($rows as $row) {
6251 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
6252 -
6253 - if ($parsed['is_chunked']) {
6254 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
6255 - $chunks[$chunk_index] = $parsed['text'];
6256 - } else {
6257 - // Non-chunked content - just return it
6258 - $chunks[] = $parsed['text'];
6259 - }
6260 - }
6261 -
6262 - // Sort by chunk index
6263 - ksort($chunks);
6264 -
6265 - // Apply chunk limit if specified
6266 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6267 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6268 - }
6269 -
6270 - // Store actual chunk count
6271 - $chunk_count = count($chunks);
6272 -
6273 - // Reassemble content
6274 - return implode("\n\n", $chunks);
6275 -}
6276 -
6277 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
6278 - global $wpdb;
2910 +private function find_relevant_content_pinecone($user_embedding) {
2911 + $options = get_option('mxchat_pinecone_addon_options', array());
2912 + $api_key = $options['mxchat_pinecone_api_key'] ?? '';
2913 + $host = $options['mxchat_pinecone_host'] ?? '';
6279 2914
6280 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
6281 - //error_log(" - bot_id: " . $bot_id);
6282 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
6283 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
6284 -
6285 - // Use bot-specific config or fall back to default
6286 - if ($bot_config === null) {
6287 - $bot_config = $this->get_bot_pinecone_config($bot_id);
6288 - }
6289 -
6290 - $api_key = $bot_config['api_key'] ?? '';
6291 - $host = $bot_config['host'] ?? '';
6292 - $namespace = $bot_config['namespace'] ?? '';
6293 -
6294 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
6295 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
6296 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
6297 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
6298 -
6299 - // Initialize similarity analysis storage
6300 - $this->last_similarity_analysis = [
6301 - 'knowledge_base_type' => 'Pinecone',
6302 - 'bot_id' => $bot_id,
6303 - 'namespace' => $namespace,
6304 - 'top_matches' => [],
6305 - 'threshold_used' => 0,
6306 - 'total_checked' => 0
6307 - ];
6308 -
6309 - // NEW: Initialize valid URLs array
6310 - $valid_urls = [];
6311 -
6312 2915 if (empty($host) || empty($api_key)) {
6313 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
6314 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
6315 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
6316 - // Store empty array for valid URLs since we can't proceed
6317 - $this->current_valid_urls = [];
2916 + //error_log('[MXCHAT Debug] Pinecone credentials not properly configured');
6318 2917 return '';
6319 2918 }
6320 2919
6321 - // Get knowledge manager instance for role checking
6322 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
2920 + // Get the similarity threshold from the main options array only
2921 + $main_options = get_option('mxchat_options', []);
2922 + $similarity_threshold = isset($main_options['similarity_threshold'])
2923 + ? ((int) $main_options['similarity_threshold']) / 100
2924 + : 0.8; // Default to 80%
6323 2925
6324 - // Get the similarity threshold from the bot options or main options
6325 - $bot_options = $this->get_bot_options($bot_id);
6326 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
2926 + //error_log('[MXCHAT Debug] Using similarity threshold: ' . ($similarity_threshold * 100) . '%');
6327 2927
6328 - $similarity_threshold = isset($current_options['similarity_threshold'])
6329 - ? ((int) $current_options['similarity_threshold']) / 100
6330 - : 0.35;
6331 -
6332 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
6333 -
6334 2928 // Prepare the query request for Pinecone
6335 2929 $api_endpoint = "https://{$host}/query";
6336 2930
2931 + //error_log('[MXCHAT Debug] Querying Pinecone at: ' . $api_endpoint);
2932 +
6337 2933 $request_body = array(
6338 2934 'vector' => $user_embedding,
6339 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
2935 + 'topK' => 5,
6340 2936 'includeMetadata' => true,
6341 2937 'includeValues' => true
6342 2938 );
6343 2939
6344 - // Add namespace if specified for this bot
6345 - if (!empty($namespace)) {
6346 - $request_body['namespace'] = $namespace;
6347 - }
6348 -
6349 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
6350 - //error_log(" - Endpoint: " . $api_endpoint);
6351 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
6352 -
6353 2940 $response = wp_remote_post($api_endpoint, array(
6354 2941 'headers' => array(
6355 2942 'Api-Key' => $api_key,
6356 2943 'accept' => 'application/json',
@@ -6360,898 +2947,54 @@
6360 2947 'timeout' => 30
6361 2948 ));
6362 2949
6363 2950 if (is_wp_error($response)) {
6364 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
6365 - // Store empty array for valid URLs
6366 - $this->current_valid_urls = [];
2951 + //error_log('[MXCHAT Debug] Pinecone query error: ' . $response->get_error_message());
6367 2952 return '';
6368 2953 }
6369 2954
6370 2955 $response_code = wp_remote_retrieve_response_code($response);
6371 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
6372 -
6373 2956 if ($response_code !== 200) {
6374 - $response_body = wp_remote_retrieve_body($response);
6375 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
6376 - // Store empty array for valid URLs
6377 - $this->current_valid_urls = [];
2957 + //error_log('[MXCHAT Debug] Pinecone API error: ' . wp_remote_retrieve_body($response));
6378 2958 return '';
6379 2959 }
6380 2960
6381 - // ADD DETAILED DEBUG SECTION HERE
6382 - $response_body = wp_remote_retrieve_body($response);
6383 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
6384 -
6385 - $results = json_decode($response_body, true);
6386 -
6387 - if (json_last_error() !== JSON_ERROR_NONE) {
6388 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
6389 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
6390 - // Store empty array for valid URLs
6391 - $this->current_valid_urls = [];
6392 - return '';
6393 - }
6394 -
6395 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
6396 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
6397 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
6398 -
2961 + $results = json_decode(wp_remote_retrieve_body($response), true);
6399 2962 if (empty($results['matches'])) {
6400 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
6401 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
6402 - // Store empty array for valid URLs
6403 - $this->current_valid_urls = [];
2963 + //error_log('[MXCHAT Debug] No matches found in Pinecone response');
6404 2964 return '';
6405 2965 }
6406 2966
6407 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
2967 + //error_log('[MXCHAT Debug] Found ' . count($results['matches']) . ' matches in Pinecone');
6408 2968
6409 - // Log first match details for debugging
6410 - if (!empty($results['matches'][0])) {
6411 - $first_match = $results['matches'][0];
6412 - //error_log("MXCHAT DEBUG: First match details:");
6413 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
6414 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
6415 - if (isset($first_match['metadata'])) {
6416 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
6417 - }
6418 - }
6419 -
6420 2969 // Initialize the final content
6421 2970 $content = '';
6422 - $matches_used = 0;
6423 - $matches_used_for_context = [];
6424 - $total_chunks_used = 0;
6425 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6426 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6427 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6428 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
2971 + $matches_above_threshold = 0;
2972 +
2973 + // Process each match
2974 + foreach ($results['matches'] as $index => $match) {
2975 + // Log score for each match
6429 2976
6430 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6431 - // Use fresh options to ensure we get the latest setting value
6432 - $fresh_options = get_option('mxchat_options', []);
6433 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6434 -
6435 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
6436 - $url_groups = array();
6437 -
6438 - foreach ($results['matches'] as $index => $match) {
6439 2977 // Skip if similarity is below threshold
6440 2978 if ($match['score'] < $similarity_threshold) {
6441 2979 continue;
6442 2980 }
6443 -
6444 - $metadata = $match['metadata'] ?? array();
6445 - $source_url = $metadata['source_url'] ?? '';
6446 - $match_id = $match['id'] ?? '';
6447 -
6448 - // LAZY ROLE CHECK: Only check role for content we're actually considering
6449 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
6450 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6451 -
6452 - // Skip if user doesn't have access
6453 - if (!$has_access) {
6454 - continue;
6455 - }
6456 -
6457 - // Use a unique key for manual entries without a source URL
6458 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
6459 -
6460 - // Group by source URL (or unique key for manual entries)
6461 - if (!isset($url_groups[$group_key])) {
6462 - $url_groups[$group_key] = array(
6463 - 'source_url' => $source_url,
6464 - 'best_score' => 0,
6465 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
6466 - 'chunks' => array(),
6467 - 'single_text' => ''
6468 - );
6469 - }
6470 -
6471 - // Track best score for this group
6472 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
6473 - $url_groups[$group_key]['best_score'] = $match['score'];
6474 - }
6475 -
6476 - // Store chunk info or single text
6477 - if ($url_groups[$group_key]['is_chunked']) {
6478 - $url_groups[$group_key]['chunks'][] = array(
6479 - 'id' => $match_id,
6480 - 'score' => $match['score'],
6481 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
6482 - 'text' => $metadata['text'] ?? ''
6483 - );
6484 - } else {
6485 - // Non-chunked content - just store the text
6486 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
6487 - $url_groups[$group_key]['single_id'] = $match_id;
6488 - }
6489 - }
6490 -
6491 - // Sort URL groups by best score (highest first)
6492 - uasort($url_groups, function($a, $b) {
6493 - return $b['best_score'] <=> $a['best_score'];
6494 - });
6495 -
6496 - // Get RAG sources limit from options (default 6, min 3, max 10)
6497 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6498 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6499 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
6500 -
6501 - // Take top N unique URLs based on user setting
6502 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
6503 -
6504 - // Track which match IDs are actually used for context
6505 - foreach ($top_urls as $group) {
6506 - if ($group['is_chunked']) {
6507 - foreach ($group['chunks'] as $chunk) {
6508 - $matches_used_for_context[] = $chunk['id'];
6509 - }
6510 - } elseif (!empty($group['single_id'])) {
6511 - $matches_used_for_context[] = $group['single_id'];
6512 - }
6513 - }
6514 -
6515 - // Build content from top sources
6516 - foreach ($top_urls as $group_key => $group) {
6517 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6518 -
6519 - // Stop if we've hit the total chunk limit
6520 - if ($total_chunks_used >= $max_total_chunks) {
6521 - break;
6522 - }
6523 -
6524 - $full_text = '';
6525 - $chunks_in_this_source = 1; // Default for non-chunked content
6526 -
6527 - if ($group['is_chunked']) {
6528 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6529 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6530 -
6531 - // Fetch chunks for this URL with limit
6532 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6533 -
6534 - // If fetching all chunks fails, fall back to matched chunks
6535 - if (empty($full_text)) {
6536 - // Sort matched chunks by index and concatenate
6537 - usort($group['chunks'], function($a, $b) {
6538 - return $a['chunk_index'] <=> $b['chunk_index'];
6539 - });
6540 -
6541 - $chunk_texts = array();
6542 - $chunks_in_this_source = 0;
6543 - foreach ($group['chunks'] as $chunk) {
6544 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6545 - break;
6546 - }
6547 - $chunk_texts[] = $chunk['text'];
6548 - $chunks_in_this_source++;
6549 - }
6550 - $full_text = implode("\n\n", $chunk_texts);
6551 - }
6552 - } else {
6553 - $full_text = $group['single_text'];
6554 - $chunks_in_this_source = 1;
6555 - }
6556 -
6557 - if (!empty($full_text)) {
6558 - // Strip URLs from content if citation links are disabled
6559 - if (!$citation_links_enabled) {
6560 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6561 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6562 - }
6563 -
6564 - // Use numbered reference for URL-based entries, plain info label for manual entries
6565 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6566 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6567 - $matches_used++;
6568 - $content .= "## Reference " . $matches_used . " ##\n";
6569 - $content .= $full_text . "\n\n";
6570 -
6571 - // Only include citation URLs if citation links are enabled
6572 - if ($citation_links_enabled) {
6573 - $valid_urls[] = $source_url;
6574 - $content .= "URL: " . $source_url . "\n\n";
6575 - }
6576 -
6577 - // Video-backed source → queue the consent-safe embed (03ba33)
6578 - $this->maybe_queue_youtube_embed($source_url, $full_text);
6579 - } else {
6580 - // Manual entry — no reference number, no citation. Count it as a USED
6581 - // source (plan-mxchat-20260622-c1fe6a): without this, manual/Direct-Content
6582 - // entries (empty or mxchat:// source_url) never increment $matches_used, so
6583 - // the gate below (`if ($matches_used === 0)`) discards manual-only context on
6584 - // the Pinecone backend and the model is told "No reference information was
6585 - // found" — even though the testing panel reports used_for_context:true. It
6586 - // also corrects the cosmetic sources_used:0 the panel/transcript showed. The
6587 - // sibling local/WP-DB builder gates on empty($top_urls), so it never had this
6588 - // bug; this brings Pinecone to parity. Manual entries are still uncited (not
6589 - // added to $valid_urls, no "URL:" line).
6590 - $matches_used++;
6591 - $content .= "## Information ##\n";
6592 - $content .= $full_text . "\n\n";
6593 - }
6594 -
6595 - // Extract any URLs from the text content itself (only if citation links enabled)
6596 - if ($citation_links_enabled) {
6597 - preg_match_all(
6598 - '#\bhttps?://[^\s<>"\']+#i',
6599 - $full_text,
6600 - $content_urls
6601 - );
6602 - if (!empty($content_urls[0])) {
6603 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6604 - }
6605 - }
6606 -
6607 - $total_chunks_used += $chunks_in_this_source;
6608 - }
6609 - }
6610 -
6611 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6612 - $all_matches = [];
6613 - foreach ($results['matches'] as $index => $match) {
6614 - if ($index >= 10) break; // Limit to top 10 for testing
6615 2981
6616 - $match_id = $match['id'] ?? '';
2982 + $matches_above_threshold++;
6617 2983
6618 - // Check role access for testing display (use cache if available)
6619 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6620 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6621 -
6622 - $source_display = '';
6623 - if (!empty($match['metadata']['source_url'])) {
6624 - $source_display = $match['metadata']['source_url'];
6625 - } else {
6626 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6627 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6628 - $source_display = substr(trim($content_preview), 0, 50) . '...';
2984 + if (!empty($match['metadata']['text']) && !empty($match['metadata']['source_url'])) {
2985 + // Add content with citation
2986 + $content .= $match['metadata']['text'] . "\n";
2987 + $content .= "Source: " . $match['metadata']['source_url'] . "\n\n";
6629 2988 }
6630 -
6631 - $match_id_for_display = $match['id'] ?? $index;
6632 -
6633 - // Check for chunk metadata in Pinecone
6634 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6635 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6636 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6637 -
6638 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6639 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6640 - $is_chunk = true;
6641 - }
6642 -
6643 - $all_matches[] = [
6644 - 'document_id' => $match_id_for_display,
6645 - 'similarity' => $match['score'],
6646 - 'similarity_percentage' => round($match['score'] * 100, 2),
6647 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6648 - 'source_display' => $source_display,
6649 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6650 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6651 - 'role_restriction' => $role_restriction,
6652 - 'has_access' => $has_access,
6653 - 'filtered_out' => !$has_access,
6654 - 'is_chunk' => $is_chunk,
6655 - 'chunk_index' => $chunk_index,
6656 - 'total_chunks' => $total_chunks
6657 - ];
6658 2989 }
6659 2990
6660 - // Store for testing panel
6661 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6662 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6663 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6664 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6665 -
6666 - // NEW: Store unique valid URLs for validation
6667 - $this->current_valid_urls = array_unique($valid_urls);
6668 -
6669 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6670 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6671 -
6672 - // Add response guidelines
6673 - if ($matches_used === 0) {
6674 - $content = "No reference information was found for this query.\n\n";
6675 - } else {
6676 - // Build response guidelines based on citation links setting
6677 - $content .= "\n## Response Guidelines ##\n" .
6678 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6679 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6680 - "If you don't have specific information or are uncertain about any details, it's always " .
6681 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6682 - "When information is incomplete, let them know you are unsure.\n\n";
6683 -
6684 - // Only add hyperlink instructions if citation links are enabled
6685 - if ($citation_links_enabled) {
6686 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6687 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6688 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6689 - } else {
6690 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6691 - "Simply provide helpful answers based on the reference information without citing sources.";
6692 - }
6693 - }
6694 -
6695 - return trim($content);
6696 -}
6697 -
6698 -/**
6699 - * Get role restriction for a single vector (with caching)
6700 - */
6701 -private function get_single_vector_role($vector_id, $metadata = array()) {
6702 - global $wpdb;
2991 + //error_log('[MXCHAT Debug] Total matches used (above threshold): ' . $matches_above_threshold);
2992 + //error_log('[MXCHAT Debug] Content length returned: ' . strlen(trim($content)) . ' characters');
6703 2993
6704 - if (empty($vector_id)) {
6705 - return 'public';
6706 - }
6707 -
6708 - // Check cache first
6709 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6710 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6711 -
6712 - if ($cached_role !== false) {
6713 - return $cached_role;
6714 - }
6715 -
6716 - $role_restriction = 'public';
6717 -
6718 - // First try Pinecone metadata
6719 - if (!empty($metadata['role_restriction'])) {
6720 - $role_restriction = $metadata['role_restriction'];
6721 - } else {
6722 - // Check WordPress table for user-modified roles
6723 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6724 - $stored_role = $wpdb->get_var($wpdb->prepare(
6725 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6726 - $vector_id
6727 - ));
6728 -
6729 - if ($stored_role) {
6730 - $role_restriction = $stored_role;
6731 - }
6732 - }
6733 -
6734 - // Cache individual role for 1 hour
6735 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6736 -
6737 - return $role_restriction;
6738 -}
6739 -
6740 -/**
6741 - * Fetch and reassemble all chunks for a URL from Pinecone
6742 - *
6743 - * @param string $source_url The source URL to fetch chunks for
6744 - * @param array $bot_config Bot-specific Pinecone configuration
6745 - * @return string Reassembled content from all chunks
6746 - */
6747 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6748 - $api_key = $bot_config['api_key'] ?? '';
6749 - $host = $bot_config['host'] ?? '';
6750 - $namespace = $bot_config['namespace'] ?? '';
6751 -
6752 - if (empty($host) || empty($api_key)) {
6753 - $chunk_count = 0;
6754 - return '';
6755 - }
6756 -
6757 - $base_hash = md5($source_url);
6758 -
6759 - // Use Pinecone list API to find all chunk vectors with this prefix
6760 - $list_url = "https://{$host}/vectors/list";
6761 -
6762 - // Limit to max_chunks if specified, otherwise fetch up to 100
6763 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6764 -
6765 - $list_body = array(
6766 - 'prefix' => $base_hash . '_chunk_',
6767 - 'limit' => $fetch_limit
6768 - );
6769 -
6770 - if (!empty($namespace)) {
6771 - $list_body['namespace'] = $namespace;
6772 - }
6773 -
6774 - $list_response = wp_remote_post($list_url, array(
6775 - 'headers' => array(
6776 - 'Api-Key' => $api_key,
6777 - 'accept' => 'application/json',
6778 - 'content-type' => 'application/json'
6779 - ),
6780 - 'body' => wp_json_encode($list_body),
6781 - 'timeout' => 30
6782 - ));
6783 -
6784 - if (is_wp_error($list_response)) {
6785 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6786 - return '';
6787 - }
6788 -
6789 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6790 -
6791 - if (empty($list_data['vectors'])) {
6792 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6793 - return '';
6794 - }
6795 -
6796 - // Extract vector IDs
6797 - $vector_ids = array();
6798 - foreach ($list_data['vectors'] as $vector) {
6799 - if (isset($vector['id'])) {
6800 - $vector_ids[] = $vector['id'];
6801 - }
6802 - }
6803 -
6804 - if (empty($vector_ids)) {
6805 - return '';
6806 - }
6807 -
6808 - // Fetch all chunk content
6809 - $fetch_url = "https://{$host}/vectors/fetch";
6810 -
6811 - $fetch_body = array(
6812 - 'ids' => $vector_ids
6813 - );
6814 -
6815 - if (!empty($namespace)) {
6816 - $fetch_body['namespace'] = $namespace;
6817 - }
6818 -
6819 - $fetch_response = wp_remote_post($fetch_url, array(
6820 - 'headers' => array(
6821 - 'Api-Key' => $api_key,
6822 - 'accept' => 'application/json',
6823 - 'content-type' => 'application/json'
6824 - ),
6825 - 'body' => wp_json_encode($fetch_body),
6826 - 'timeout' => 30
6827 - ));
6828 -
6829 - if (is_wp_error($fetch_response)) {
6830 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6831 - return '';
6832 - }
6833 -
6834 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6835 -
6836 - if (empty($fetch_data['vectors'])) {
6837 - return '';
6838 - }
6839 -
6840 - // Sort chunks by index and reassemble
6841 - $chunks = array();
6842 - foreach ($fetch_data['vectors'] as $id => $vector) {
6843 - $metadata = $vector['metadata'] ?? array();
6844 - $chunk_index = $metadata['chunk_index'] ?? 0;
6845 - $text = $metadata['text'] ?? '';
6846 -
6847 - // Store chunk with its index
6848 - $chunks[$chunk_index] = $text;
6849 - }
6850 -
6851 - // Sort by chunk index
6852 - ksort($chunks);
6853 -
6854 - // Apply chunk limit if specified
6855 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6856 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6857 - }
6858 -
6859 - // Store actual chunk count
6860 - $chunk_count = count($chunks);
6861 -
6862 - // Reassemble content
6863 - return implode("\n\n", $chunks);
6864 -}
6865 -
6866 -/**
6867 - * Search for relevant content using OpenAI Vector Store (File Search)
6868 - *
6869 - * @param string $user_query The user's query text
6870 - * @param string $bot_id The bot ID
6871 - * @param array $vectorstore_config Vector Store configuration
6872 - * @return string Formatted context string with references
6873 - */
6874 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6875 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6876 - //error_log(" - bot_id: " . $bot_id);
6877 - //error_log(" - user_query length: " . strlen($user_query));
6878 -
6879 - // Get OpenAI API key
6880 - $mxchat_options = get_option('mxchat_options', array());
6881 - $api_key = $mxchat_options['api_key'] ?? '';
6882 -
6883 - // Reset vectorstore error tracking
6884 - $this->last_vectorstore_error = null;
6885 -
6886 - if (empty($api_key)) {
6887 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6888 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6889 - $this->current_valid_urls = [];
6890 - return '';
6891 - }
6892 -
6893 - // Get Vector Store configuration
6894 - if (empty($vectorstore_config)) {
6895 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6896 - }
6897 -
6898 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6899 - $max_results = $vectorstore_config['max_results'] ?? 5;
6900 -
6901 - if (empty($vectorstore_ids_string)) {
6902 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6903 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6904 - $this->current_valid_urls = [];
6905 - return '';
6906 - }
6907 -
6908 - // Parse Vector Store IDs
6909 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6910 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6911 -
6912 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6913 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6914 -
6915 - // Initialize similarity analysis storage
6916 - $this->last_similarity_analysis = [
6917 - 'knowledge_base_type' => 'OpenAI Vector Store',
6918 - 'bot_id' => $bot_id,
6919 - 'vectorstore_ids' => $vectorstore_ids,
6920 - 'top_matches' => [],
6921 - 'threshold_used' => 0,
6922 - 'total_checked' => 0
6923 - ];
6924 -
6925 - $valid_urls = [];
6926 -
6927 - // Get the selected model
6928 - $bot_options = $this->get_bot_options($bot_id);
6929 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6930 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6931 -
6932 - // Verify it's an OpenAI model
6933 - if (!$this->is_openai_chat_model($selected_model)) {
6934 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6935 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6936 - $this->current_valid_urls = [];
6937 - return '';
6938 - }
6939 -
6940 - // Use OpenAI Responses API with file_search tool
6941 - $request_body = array(
6942 - 'model' => $selected_model,
6943 - 'input' => $user_query,
6944 - 'tools' => array(
6945 - array(
6946 - 'type' => 'file_search',
6947 - 'vector_store_ids' => $vectorstore_ids,
6948 - 'max_num_results' => intval($max_results)
6949 - )
6950 - ),
6951 - 'include' => array('output[*].file_search_call.search_results')
6952 - );
6953 -
6954 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6955 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6956 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6957 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6958 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6959 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6960 -
6961 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6962 - 'headers' => array(
6963 - 'Authorization' => 'Bearer ' . $api_key,
6964 - 'Content-Type' => 'application/json'
6965 - ),
6966 - 'body' => wp_json_encode($request_body),
6967 - 'timeout' => 60
6968 - ));
6969 -
6970 - if (is_wp_error($response)) {
6971 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6972 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6973 - $this->current_valid_urls = [];
6974 - return '';
6975 - }
6976 -
6977 - $response_code = wp_remote_retrieve_response_code($response);
6978 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6979 -
6980 - $response_body = wp_remote_retrieve_body($response);
6981 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6982 -
6983 - if ($response_code !== 200) {
6984 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6985 - $api_error_detail = '';
6986 - $decoded_error = json_decode($response_body, true);
6987 - if (isset($decoded_error['error']['message'])) {
6988 - $api_error_detail = $decoded_error['error']['message'];
6989 - }
6990 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6991 - $this->current_valid_urls = [];
6992 - return '';
6993 - }
6994 - $result = json_decode($response_body, true);
6995 -
6996 - if (json_last_error() !== JSON_ERROR_NONE) {
6997 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6998 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6999 - $this->current_valid_urls = [];
7000 - return '';
7001 - }
7002 -
7003 - // Debug: Log the structure of the result
7004 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
7005 - if (isset($result['output'])) {
7006 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
7007 - foreach ($result['output'] as $idx => $out) {
7008 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
7009 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
7010 - }
7011 - } else {
7012 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
7013 - }
7014 -
7015 - // Extract file search results from the response
7016 - $content = '';
7017 - $matches_used = 0;
7018 - $all_matches = [];
7019 -
7020 - // The Responses API returns output array with tool results
7021 - if (isset($result['output']) && is_array($result['output'])) {
7022 - foreach ($result['output'] as $output_item) {
7023 - // Look for file_search_call results
7024 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
7025 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
7026 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
7027 -
7028 - // Check for search_results in the output item directly
7029 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
7030 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
7031 -
7032 - if (empty($search_results)) {
7033 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
7034 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
7035 - }
7036 -
7037 - foreach ($search_results as $index => $search_result) {
7038 - $filename = $search_result['filename'] ?? '';
7039 - $score = $search_result['score'] ?? 0;
7040 - $text_content = '';
7041 -
7042 - // Extract text content from the result
7043 - // The text can be directly on the result OR nested under content array
7044 - if (isset($search_result['text']) && !empty($search_result['text'])) {
7045 - // Direct text field (OpenAI's actual format)
7046 - $text_content = $search_result['text'];
7047 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
7048 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
7049 - // Nested content array format
7050 - foreach ($search_result['content'] as $content_item) {
7051 - if (isset($content_item['text'])) {
7052 - $text_content .= $content_item['text'] . "\n";
7053 - }
7054 - }
7055 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
7056 - } else {
7057 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
7058 - }
7059 -
7060 - if (!empty($text_content)) {
7061 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
7062 - $content .= trim($text_content) . "\n\n";
7063 -
7064 - if (!empty($filename)) {
7065 - $content .= "Source: " . $filename . "\n\n";
7066 - }
7067 -
7068 - // Extract URLs from content
7069 - preg_match_all(
7070 - '#\bhttps?://[^\s<>"\']+#i',
7071 - $text_content,
7072 - $content_urls
7073 - );
7074 - if (!empty($content_urls[0])) {
7075 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
7076 - }
7077 -
7078 - $matches_used++;
7079 - }
7080 -
7081 - // Store for similarity analysis
7082 - $all_matches[] = [
7083 - 'document_id' => $filename ?: ('result_' . $index),
7084 - 'similarity' => $score,
7085 - 'similarity_percentage' => round($score * 100, 2),
7086 - 'above_threshold' => true,
7087 - 'source_display' => $filename,
7088 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
7089 - 'used_for_context' => true,
7090 - 'role_restriction' => 'public',
7091 - 'has_access' => true,
7092 - 'filtered_out' => false
7093 - ];
7094 - }
7095 - }
7096 -
7097 - // Also check for message content with annotations (citations)
7098 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
7099 - if (isset($output_item['content']) && is_array($output_item['content'])) {
7100 - foreach ($output_item['content'] as $content_block) {
7101 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
7102 - foreach ($content_block['annotations'] as $annotation) {
7103 - if (isset($annotation['filename'])) {
7104 - $filename = $annotation['filename'];
7105 - $score = $annotation['score'] ?? 0;
7106 - $text_content = '';
7107 -
7108 - if (isset($annotation['content']) && is_array($annotation['content'])) {
7109 - foreach ($annotation['content'] as $ann_content) {
7110 - if (isset($ann_content['text'])) {
7111 - $text_content .= $ann_content['text'] . "\n";
7112 - }
7113 - }
7114 - }
7115 -
7116 - if (!empty($text_content) && $matches_used < $max_results) {
7117 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
7118 - $content .= trim($text_content) . "\n\n";
7119 - $content .= "Source: " . $filename . "\n\n";
7120 -
7121 - preg_match_all(
7122 - '#\bhttps?://[^\s<>"\']+#i',
7123 - $text_content,
7124 - $content_urls
7125 - );
7126 - if (!empty($content_urls[0])) {
7127 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
7128 - }
7129 -
7130 - $matches_used++;
7131 -
7132 - $all_matches[] = [
7133 - 'document_id' => $filename,
7134 - 'similarity' => $score,
7135 - 'similarity_percentage' => round($score * 100, 2),
7136 - 'above_threshold' => true,
7137 - 'source_display' => $filename,
7138 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
7139 - 'used_for_context' => true,
7140 - 'role_restriction' => 'public',
7141 - 'has_access' => true,
7142 - 'filtered_out' => false
7143 - ];
7144 - }
7145 - }
7146 - }
7147 - }
7148 - }
7149 - }
7150 - }
7151 - }
7152 - }
7153 -
7154 - // Store for testing panel
7155 - $this->last_similarity_analysis['top_matches'] = $all_matches;
7156 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
7157 -
7158 - // Store unique valid URLs for validation
7159 - $this->current_valid_urls = array_unique($valid_urls);
7160 -
7161 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
7162 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
7163 -
7164 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
7165 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
7166 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
7167 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
7168 - if ($matches_used > 0) {
7169 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
7170 - }
7171 -
7172 - // Check if citation links are enabled
7173 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
7174 -
7175 - // Add response guidelines
7176 - if ($matches_used === 0) {
7177 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
7178 - $content = "No reference information was found for this query.\n\n";
7179 - } else {
7180 - // Build response guidelines based on citation links setting
7181 - $content .= "\n## Response Guidelines ##\n" .
7182 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
7183 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
7184 - "If you don't have specific information or are uncertain about any details, it's always " .
7185 - "better to honestly say you don't know rather than making up or guessing at answers. " .
7186 - "When information is incomplete, let them know you are unsure.\n\n";
7187 -
7188 - // Only add hyperlink instructions if citation links are enabled
7189 - if ($citation_links_enabled) {
7190 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
7191 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
7192 - } else {
7193 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
7194 - "Simply provide helpful answers based on the reference information without citing sources.";
7195 - }
7196 - }
7197 -
7198 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
7199 -
7200 2994 return trim($content);
7201 2995 }
7202 2996
7203 -/**
7204 - * Check if the given model is an OpenAI chat model
7205 - *
7206 - * @param string $model The model ID
7207 - * @return bool True if it's an OpenAI model
7208 - */
7209 -private function is_openai_chat_model($model) {
7210 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
7211 - foreach ($openai_prefixes as $prefix) {
7212 - if (strpos($model, $prefix) === 0) {
7213 - return true;
7214 - }
7215 - }
7216 - return false;
7217 -}
7218 -
7219 -/**
7220 - * Get bot-specific Vector Store configuration
7221 - *
7222 - * @param string $bot_id The bot ID
7223 - * @return array Configuration array
7224 - */
7225 -private function get_bot_vectorstore_config($bot_id = 'default') {
7226 - // Admin Testing tab bot → resolve the DEFAULT bot's backend (see
7227 - // get_bot_pinecone_config). This getter already passes the real default
7228 - // config into the filter, so it was not broken — normalized anyway so the
7229 - // Testing bot can never drift from the front-end default.
7230 - if ($bot_id === 'testing') {
7231 - $bot_id = 'default';
7232 - }
7233 -
7234 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
7235 -
7236 - // Default global settings
7237 - $default_config = array(
7238 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
7239 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
7240 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
7241 - );
7242 -
7243 - // Allow multi-bot plugin to override with bot-specific settings
7244 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
7245 -
7246 - // Preserve max_results from global settings if not set in bot config
7247 - if (!isset($bot_config['max_results'])) {
7248 - $bot_config['max_results'] = $default_config['max_results'];
7249 - }
7250 -
7251 - return $bot_config;
7252 -}
7253 -
7254 2997 private function mxchat_find_relevant_products($user_embedding) {
7255 2998 //error_log('MXChat Vector Search: Starting product search...');
7256 2999
7257 3000 // Retrieve the add-on settings from the database
@@ -7269,78 +3012,77 @@
7269 3012 //error_log('MXChat Vector Search: Using WordPress database for products');
7270 3013 return $this->find_relevant_products_wordpress($user_embedding);
7271 3014 }
7272 3015 }
3016 +
7273 3017 private function find_relevant_products_wordpress($user_embedding) {
7274 3018 global $wpdb;
7275 3019 $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
3020 + $cache_key = 'mxchat_system_prompt_embeddings';
3021 + $batch_size = 500;
7276 3022
7277 - if (!is_array($user_embedding)) {
7278 - return '';
7279 - }
3023 + // Original WordPress database search logic
3024 + // [Previous implementation remains the same]
3025 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
3026 + if ($embeddings === false) {
3027 + $embeddings = [];
3028 + $offset = 0;
7280 3029
7281 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
7282 - // results above the similarity threshold. Peak memory is bounded by
7283 - // $batch_size embedding rows plus a 3-element top list.
7284 - $batch_size = 250;
7285 - $similarity_threshold = 0.85;
7286 - $top_k = 3;
7287 - $top_results = [];
7288 - $offset = 0;
3030 + do {
3031 + $query = $wpdb->prepare(
3032 + "SELECT id, embedding_vector
3033 + FROM {$system_prompt_table}
3034 + LIMIT %d OFFSET %d",
3035 + $batch_size,
3036 + $offset
3037 + );
7289 3038
7290 - do {
7291 - $batch = $wpdb->get_results($wpdb->prepare(
7292 - "SELECT id, embedding_vector
7293 - FROM {$system_prompt_table}
7294 - LIMIT %d OFFSET %d",
7295 - $batch_size,
7296 - $offset
7297 - ));
3039 + $batch = $wpdb->get_results($query);
3040 + if (empty($batch)) {
3041 + break;
3042 + }
7298 3043
7299 - if (empty($batch)) {
7300 - break;
7301 - }
3044 + $embeddings = array_merge($embeddings, $batch);
3045 + $offset += $batch_size;
7302 3046
7303 - foreach ($batch as $row) {
7304 - $database_embedding = $row->embedding_vector
7305 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
7306 - : null;
3047 + unset($batch);
7307 3048
7308 - if (!is_array($database_embedding)) {
7309 - unset($database_embedding);
7310 - continue;
7311 - }
3049 + } while (true);
7312 3050
3051 + if (empty($embeddings)) {
3052 + return '';
3053 + }
3054 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600);
3055 + }
3056 +
3057 + $relevant_results = [];
3058 + foreach ($embeddings as $embedding) {
3059 + $database_embedding = $embedding->embedding_vector
3060 + ? unserialize($embedding->embedding_vector, ['allowed_classes' => false])
3061 + : null;
3062 + if (is_array($database_embedding) && is_array($user_embedding)) {
7313 3063 $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
7314 - unset($database_embedding);
7315 -
7316 - if ($similarity < $similarity_threshold) {
7317 - continue;
7318 - }
7319 -
7320 - // Insert into bounded top-K (kept sorted descending)
7321 - if (count($top_results) < $top_k) {
7322 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
7323 - usort($top_results, function ($a, $b) {
7324 - return $b['similarity'] <=> $a['similarity'];
7325 - });
7326 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
7327 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
7328 - usort($top_results, function ($a, $b) {
7329 - return $b['similarity'] <=> $a['similarity'];
7330 - });
7331 - }
3064 + $relevant_results[] = [
3065 + 'id' => $embedding->id,
3066 + 'similarity' => $similarity
3067 + ];
7332 3068 }
3069 + unset($database_embedding);
3070 + }
7333 3071
7334 - unset($batch);
7335 - $offset += $batch_size;
7336 - } while (true);
3072 + // Use fixed threshold for products
3073 + $similarity_threshold = 0.85;
7337 3074
7338 - if (empty($top_results)) {
7339 - return '';
7340 - }
3075 + $relevant_results = array_filter($relevant_results, function ($result) use ($similarity_threshold) {
3076 + return $result['similarity'] >= $similarity_threshold;
3077 + });
3078 + usort($relevant_results, function ($a, $b) {
3079 + return $b['similarity'] <=> $a['similarity'];
3080 + });
7341 3081
3082 + $top_results = array_slice($relevant_results, 0, 5);
7342 3083 $content = '';
3084 +
7343 3085 foreach ($top_results as $result) {
7344 3086 $chunk_content = $this->fetch_content_with_product_links($result['id']);
7345 3087 $content .= $chunk_content . "\n\n";
7346 3088 }
@@ -7347,9 +3089,9 @@
7347 3089
7348 3090 return trim($content);
7349 3091 }
7350 3092
7351 -
3093 +// Modified search function with correct filter syntax
7352 3094 private function find_relevant_products_pinecone($user_embedding) {
7353 3095 //error_log('Starting Pinecone product search...');
7354 3096
7355 3097 $options = get_option('mxchat_pinecone_addon_options', array());
@@ -7446,655 +3188,25 @@
7446 3188
7447 3189 return null;
7448 3190 }
7449 3191
7450 -/**
7451 - * Get system instructions for a specific bot or default
7452 - * Checks for multi-bot add-on and uses bot-specific instructions if available
7453 - * Automatically strips URLs if citation links are disabled
7454 - * Replaces {visitor_name} placeholder with actual visitor name if available
7455 - *
7456 - * @param string $bot_id The bot ID to get instructions for
7457 - * @param string $session_id Optional session ID to lookup visitor name
7458 - */
7459 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
7460 - $instructions = '';
7461 -
7462 - // Check if multi-bot add-on is active
7463 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
7464 - // Get bot-specific options from multi-bot add-on
7465 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
7466 -
7467 - // If bot has custom system instructions, use those
7468 - if (!empty($bot_options['system_prompt_instructions'])) {
7469 - $instructions = $bot_options['system_prompt_instructions'];
7470 - }
7471 - }
7472 -
7473 - // Fall back to default system instructions
7474 - if (empty($instructions)) {
7475 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7476 - }
7477 -
7478 - // Check if citation links are disabled - if so, strip URLs from instructions
7479 - $fresh_options = get_option('mxchat_options', []);
7480 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
7481 -
7482 - if (!$citation_links_enabled && !empty($instructions)) {
7483 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
7484 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
7485 - }
7486 -
7487 - // Replace {visitor_name} placeholder with actual visitor name if available
7488 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
7489 - $name_option_key = "mxchat_name_{$session_id}";
7490 - $visitor_name = get_option($name_option_key, '');
7491 -
7492 - if (!empty($visitor_name)) {
7493 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
7494 - } else {
7495 - // Remove placeholder if no name is available
7496 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
7497 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
7498 - }
7499 - }
7500 -
7501 - // Allow developers to filter system instructions and process shortcodes
7502 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
7503 - $instructions = do_shortcode($instructions);
7504 -
7505 - return $instructions;
7506 -}
7507 -/**
7508 - * Get the current bot ID from session or request context
7509 - */
7510 -private function get_current_bot_id($session_id = '') {
7511 - // First, check if bot_id is passed in the current request
7512 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
7513 - return sanitize_key($_POST['bot_id']);
7514 - }
7515 -
7516 - // If not in POST, try to get it from session data
7517 - if (!empty($session_id)) {
7518 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
7519 - if (!empty($bot_id)) {
7520 - return $bot_id;
7521 - }
7522 - }
7523 -
7524 - // Fall back to default
7525 - return 'default';
7526 -}
7527 -/* ====================================================================== *
7528 - * Native function-calling loop (plan-mxchat-20260617-a41dee)
7529 - *
7530 - * Model-driven tool use. The model is offered MxChat's enabled callbacks as
7531 - * tools (sourced from MxChat_Tool_Registry, the single source the admin AI
7532 - * Tools checklist also reads). When the model calls a tool, the matching
7533 - * callback runs through its EXISTING permission checks, its output is fed
7534 - * back, and the loop continues up to a depth cap. INDEPENDENT of the
7535 - * intent→callback router — it runs only after intents miss, and works with
7536 - * ZERO Actions created.
7537 - *
7538 - * Entered ONLY when: function calling is enabled + the active model is
7539 - * tool-capable + at least one tool is enabled. Default-off, so existing
7540 - * installs never enter this branch (byte-for-byte unchanged behavior). The
7541 - * tool round is buffered (non-streaming) per the plan; the final answer is
7542 - * emitted via the same SSE/JSON envelopes the normal path uses.
7543 - * ====================================================================== */
7544 -
7545 -/** Gate: should the function-calling loop handle this turn? */
7546 -private function mxchat_fc_should_run($selected_model) {
7547 - if (!class_exists('MxChat_Tool_Registry') || !MxChat_Tool_Registry::is_enabled()) {
7548 - return false;
7549 - }
7550 - if (class_exists('MxChat_Model_Catalog') && !MxChat_Model_Catalog::supports_tools($selected_model)) {
7551 - return false;
7552 - }
7553 - $tools = MxChat_Tool_Registry::enabled_tools();
7554 - return !empty($tools);
7555 -}
7556 -
7557 -private function mxchat_fc_log($msg) {
7558 - if (defined('MXCHAT_DEV_MODE') && MXCHAT_DEV_MODE) {
7559 - error_log('[MxChat FC] ' . $msg);
7560 - }
7561 -}
7562 -
7563 -/**
7564 - * Resolve provider transport details. Returns null when FC can't run for this
7565 - * model/config (missing key, unsupported provider) so the caller falls back to
7566 - * the normal path. OpenAI/xAI/DeepSeek/OpenRouter/Custom share the
7567 - * OpenAI-compatible 'openai' family; Claude and Gemini are distinct.
7568 - */
7569 -private function mxchat_fc_resolve_provider($selected_model, $opts) {
7570 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
7571 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
7572 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
7573 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
7574 - if ($selected_model === 'openrouter') {
7575 - $model = isset($opts['openrouter_selected_model']) ? $opts['openrouter_selected_model'] : '';
7576 - $key = isset($opts['openrouter_api_key']) ? $opts['openrouter_api_key'] : '';
7577 - if ($model === '' || $key === '') return null;
7578 - return array('family'=>'openai','model'=>$model,'url'=>'https://openrouter.ai/api/v1/chat/completions',
7579 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7580 - }
7581 - $prefix = strtolower(explode('-', $selected_model)[0]);
7582 - switch ($prefix) {
7583 - case 'gpt': case 'o1': case 'o3': case 'o4':
7584 - $key = isset($opts['api_key']) ? $opts['api_key'] : '';
7585 - if ($key === '') return null;
7586 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.openai.com/v1/chat/completions',
7587 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7588 - case 'claude':
7589 - $key = isset($opts['claude_api_key']) ? $opts['claude_api_key'] : '';
7590 - if ($key === '') return null;
7591 - return array('family'=>'anthropic','model'=>$selected_model,'url'=>'https://api.anthropic.com/v1/messages',
7592 - 'headers'=>array('Content-Type'=>'application/json','x-api-key'=>$key,'anthropic-version'=>'2023-06-01'),'tag'=>'anthropic');
7593 - case 'gemini':
7594 - $key = isset($opts['gemini_api_key']) ? $opts['gemini_api_key'] : '';
7595 - if ($key === '') return null;
7596 - return array('family'=>'gemini','model'=>$selected_model,'key'=>$key,'tag'=>'gemini');
7597 - case 'grok': case 'xai':
7598 - $key = isset($opts['xai_api_key']) ? $opts['xai_api_key'] : '';
7599 - if ($key === '') return null;
7600 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.x.ai/v1/chat/completions',
7601 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'xai');
7602 - case 'deepseek':
7603 - $key = isset($opts['deepseek_api_key']) ? $opts['deepseek_api_key'] : '';
7604 - if ($key === '') return null;
7605 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.deepseek.com/v1/chat/completions',
7606 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7607 - case 'custom':
7608 - $base = isset($opts['custom_provider_base_url']) ? rtrim($opts['custom_provider_base_url'], '/') : '';
7609 - $key = isset($opts['custom_provider_api_key']) ? $opts['custom_provider_api_key'] : '';
7610 - $model = isset($opts['custom_provider_model']) ? $opts['custom_provider_model'] : '';
7611 - if ($base === '' || $model === '') return null;
7612 - $url = (strpos($base, 'chat/completions') !== false) ? $base : $base . '/chat/completions';
7613 - $headers = array('Content-Type'=>'application/json');
7614 - if ($key !== '') $headers['Authorization'] = 'Bearer '.$key;
7615 - return array('family'=>'openai','model'=>$model,'url'=>$url,'headers'=>$headers,'tag'=>'openai');
7616 - }
7617 - return null;
7618 -}
7619 -
7620 -/**
7621 - * Top-level function-calling attempt. Returns:
7622 - * ['handled'=>true, 'text'=>'<final answer>'] when the model used ≥1 tool
7623 - * ['handled'=>false] otherwise (caller falls back
7624 - * to the normal streamed path)
7625 - */
7626 -private function mxchat_fc_attempt($message, $relevant_content, $conversation_history, $selected_model, $opts, $session_id, $user_id) {
7627 - $prov = $this->mxchat_fc_resolve_provider($selected_model, $opts);
7628 - if (!$prov) {
7629 - return array('handled' => false);
7630 - }
7631 - $tools = MxChat_Tool_Registry::enabled_tools();
7632 - if (empty($tools)) {
7633 - return array('handled' => false);
7634 - }
7635 -
7636 - $bot_id = $this->get_current_bot_id($session_id);
7637 - $system = $this->get_system_instructions($bot_id, $session_id);
7638 -
7639 - // Force callbacks into return-mode (some echo SSE directly when streaming);
7640 - // we buffer the whole tool round, then emit once. Restored in finally.
7641 - $prev_streaming = $this->is_streaming;
7642 - $this->is_streaming = false;
3192 +private function mxchat_generate_response($relevant_content, $api_key, $xai_api_key, $claude_api_key, $deepseek_api_key, $gemini_api_key, $conversation_history) {
7643 3193 try {
7644 - if ($prov['family'] === 'anthropic') {
7645 - return $this->mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7646 - } elseif ($prov['family'] === 'gemini') {
7647 - return $this->mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7648 - }
7649 - return $this->mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7650 - } catch (\Throwable $e) {
7651 - $this->mxchat_fc_log('attempt threw: ' . $e->getMessage());
7652 - return array('handled' => false);
7653 - } finally {
7654 - $this->is_streaming = $prev_streaming;
7655 - }
7656 -}
7657 -
7658 -/** Normalize MxChat history rows to [{role:user|assistant, content}]. */
7659 -private function mxchat_fc_normalize_history($conversation_history) {
7660 - $out = array();
7661 - if (!is_array($conversation_history)) return $out;
7662 - foreach ($conversation_history as $m) {
7663 - if (!is_array($m) || !isset($m['role']) || !isset($m['content'])) continue;
7664 - $role = $m['role'];
7665 - if ($role === 'bot' || $role === 'agent') $role = 'assistant';
7666 - if (!in_array($role, array('user', 'assistant'), true)) $role = 'user';
7667 - $out[] = array('role' => $role, 'content' => (string) $m['content']);
7668 - }
7669 - return $out;
7670 -}
7671 -
7672 -/** Execute the matched callback for a tool call. Returns ['ok'=>bool,'content'=>string]. */
7673 -private function mxchat_fc_execute_tool($tool_name, $args, $orig_message, $user_id, $session_id) {
7674 - $tool = MxChat_Tool_Registry::tool_by_name($tool_name, true); // enabled-only
7675 - if (!$tool) {
7676 - return array('ok' => false, 'content' => 'This tool is not available or not enabled.');
7677 - }
7678 - $fn = $tool['callback'];
7679 -
7680 - // MxChat callbacks are message-driven: hand them the model's `query`
7681 - // (falling back to the original user message).
7682 - $query = '';
7683 - if (is_array($args) && isset($args['query']) && is_string($args['query'])) {
7684 - $query = $args['query'];
7685 - }
7686 - if ($query === '') $query = $orig_message;
7687 -
7688 - // Synthetic intent row (matches wp_mxchat_intents columns → no undefined-prop warnings).
7689 - $synthetic_intent = (object) array(
7690 - 'id' => 0, 'intent_label' => $tool['label'], 'phrases' => '',
7691 - 'embedding_vector' => '', 'callback_function' => $fn,
7692 - 'similarity_threshold' => 0.0, 'enabled' => 1, 'enabled_bots' => null,
7693 - );
7694 -
7695 - try {
7696 - if (!empty($tool['is_addon'])) {
7697 - $result = apply_filters($fn, false, $query, $user_id, $session_id, $synthetic_intent);
7698 - } elseif (method_exists($this, $fn)) {
7699 - $result = call_user_func(array($this, $fn), $query, $user_id, $session_id, $synthetic_intent, null);
7700 - } else {
7701 - return array('ok' => false, 'content' => 'Tool implementation not found.');
7702 - }
7703 - } catch (\Throwable $e) {
7704 - $this->mxchat_fc_log("tool {$fn} threw: " . $e->getMessage());
7705 - return array('ok' => false, 'content' => 'The tool failed to run.');
7706 - }
7707 -
7708 - // plan-mxchat-20260617-48a57a — surface UI-bearing tool output.
7709 - // If the callback produced a UI element (generated image, product card, image
7710 - // gallery), its html MUST reach the FRONTEND as a real rendered bot message —
7711 - // NOT be stripped to text and handed to the model to paraphrase (that was the
7712 - // bug: under function calling, UI-bearing actions rendered nothing). Capture
7713 - // the html here; the FC outcome handler emits it in the response envelope.
7714 - $ui = $this->mxchat_fc_ui_payload_from($result);
7715 - if ($ui['html'] !== '' || !empty($ui['images'])) {
7716 - if ($ui['html'] !== '') {
7717 - $this->fc_ui_html .= ($this->fc_ui_html !== '' ? "\n" : '') . $ui['html'];
7718 - }
7719 - if (!empty($ui['images']) && is_array($ui['images'])) {
7720 - $this->fc_ui_images = array_merge($this->fc_ui_images, $ui['images']);
7721 - }
7722 - $this->fc_ui_captured = true;
7723 -
7724 - // Persist the html to the transcript ONLY if the callback did not already
7725 - // do so itself. Core image/search callbacks self-save (text + html);
7726 - // add-on callbacks (e.g. woo product cards) return html for the caller to
7727 - // save. ui_self_saves carries this from the registry; default by source
7728 - // (core self-saves, add-on does not) when a tool predates the flag.
7729 - $self_saves = array_key_exists('ui_self_saves', $tool)
7730 - ? !empty($tool['ui_self_saves'])
7731 - : empty($tool['is_addon']);
7732 - if ($ui['html'] !== '' && !$self_saves) {
7733 - $this->mxchat_save_chat_message($session_id, 'bot', $ui['html']);
7734 - }
7735 -
7736 - // Hand the MODEL a short acknowledgment (never the raw or stripped html)
7737 - // so the loop can add a one-line caption without trying to re-describe a
7738 - // visual it cannot see and without duplicating the displayed element.
7739 - $summary = isset($ui['text']) ? trim((string) $ui['text']) : '';
7740 - $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');
7741 - $content = $summary !== '' ? ($ack . ' ' . $summary) : $ack;
7742 - $this->mxchat_fc_log("executed {$fn} → [ui payload surfaced] " . substr($content, 0, 120));
7743 - return array('ok' => true, 'content' => $content);
7744 - }
7745 -
7746 - $content = $this->mxchat_fc_stringify_result($result);
7747 - $this->mxchat_fc_log("executed {$fn} → " . substr($content, 0, 160));
7748 - return array('ok' => true, 'content' => $content);
7749 -}
7750 -
7751 -/**
7752 - * Extract a UI payload (html + images + text) from a tool callback's return,
7753 - * falling back to $this->fallbackResponse for callbacks that return true after
7754 - * setting it. plan-mxchat-20260617-48a57a.
7755 - *
7756 - * @return array{html:string,images:array,text:string}
7757 - */
7758 -private function mxchat_fc_ui_payload_from($result) {
7759 - $src = null;
7760 - if (is_array($result)) {
7761 - $src = $result;
7762 - } elseif ($result === true && isset($this->fallbackResponse) && is_array($this->fallbackResponse)) {
7763 - $src = $this->fallbackResponse;
7764 - }
7765 - $html = (is_array($src) && isset($src['html']) && is_string($src['html'])) ? $src['html'] : '';
7766 - $images = (is_array($src) && isset($src['images']) && is_array($src['images'])) ? $src['images'] : array();
7767 - $text = (is_array($src) && isset($src['text'])) ? (string) $src['text'] : '';
7768 - return array('html' => $html, 'images' => $images, 'text' => $text);
7769 -}
7770 -
7771 -/** Coerce a callback's return (string|array|true|false) into a tool-result string. */
7772 -private function mxchat_fc_stringify_result($result) {
7773 - if (is_string($result)) {
7774 - return $result === '' ? 'No result.' : $result;
7775 - }
7776 - if ($result === true) {
7777 - // Callbacks that set fallbackResponse and return true.
7778 - $fb = isset($this->fallbackResponse) ? $this->fallbackResponse : null;
7779 - if (is_array($fb)) {
7780 - if (!empty($fb['text'])) return (string) $fb['text'];
7781 - if (!empty($fb['html'])) return wp_strip_all_tags((string) $fb['html']);
7782 - }
7783 - return 'Done.';
7784 - }
7785 - if ($result === false || $result === null) {
7786 - return 'No result.';
7787 - }
7788 - if (is_array($result)) {
7789 - if (isset($result['text']) && $result['text'] !== '') return (string) $result['text'];
7790 - if (isset($result['html']) && $result['html'] !== '') return wp_strip_all_tags((string) $result['html']);
7791 - $json = wp_json_encode($result);
7792 - return $json !== false ? $json : 'No result.';
7793 - }
7794 - return (string) $result;
7795 -}
7796 -
7797 -/** HTTP code + decoded body for a function-calling request. */
7798 -private function mxchat_fc_post($url, $body, $headers, $tag) {
7799 - $args = array(
7800 - 'body' => wp_json_encode($body),
7801 - 'headers' => $headers,
7802 - 'timeout' => 60,
7803 - 'redirection' => 5,
7804 - 'blocking' => true,
7805 - 'httpversion' => '1.0',
7806 - 'sslverify' => true,
7807 - );
7808 - $response = $this->mxchat_provider_call_with_retry($url, $args, $tag);
7809 - if (is_wp_error($response)) {
7810 - return array('code' => 0, 'data' => null, 'error' => $response->get_error_message());
7811 - }
7812 - $code = (int) wp_remote_retrieve_response_code($response);
7813 - $data = json_decode(wp_remote_retrieve_body($response), true);
7814 - return array('code' => $code, 'data' => $data, 'error' => null);
7815 -}
7816 -
7817 -/* ---------------- OpenAI-compatible loop (OpenAI/xAI/DeepSeek/OpenRouter/Custom) -------------- */
7818 -private function mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7819 - $messages = array();
7820 - $messages[] = array('role' => 'system', 'content' => $system . ' ' . $relevant_content);
7821 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7822 - $messages[] = $m;
7823 - }
7824 -
7825 - $depth = MxChat_Tool_Registry::max_depth();
7826 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7827 - $tool_schema = MxChat_Tool_Registry::to_openai_tools($tools);
7828 - $used_tool = false;
7829 - $calls_made = 0;
7830 -
7831 - for ($step = 0; $step <= $depth; $step++) {
7832 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7833 - $body = array('model' => $prov['model'], 'messages' => $messages, 'temperature' => 1, 'stream' => false);
7834 - if ($offer_tools) {
7835 - $body['tools'] = $tool_schema;
7836 - $body['tool_choice'] = 'auto';
7837 - }
7838 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7839 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7840 - $this->mxchat_fc_log('openai call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7841 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7842 - }
7843 - $msg = isset($r['data']['choices'][0]['message']) ? $r['data']['choices'][0]['message'] : null;
7844 - if (!$msg) {
7845 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7846 - }
7847 - $tool_calls = isset($msg['tool_calls']) && is_array($msg['tool_calls']) ? $msg['tool_calls'] : array();
7848 - if (empty($tool_calls)) {
7849 - $text = isset($msg['content']) ? trim((string) $msg['content']) : '';
7850 - if (!$used_tool) return array('handled' => false); // model never used a tool → normal path
7851 - return array('handled' => true, 'text' => ($text !== '' ? $text : $this->mxchat_fc_giveup_text()));
7852 - }
7853 - // Append the assistant tool-call turn verbatim, then a tool result per call.
7854 - $used_tool = true;
7855 - $messages[] = $msg;
7856 - foreach ($tool_calls as $tc) {
7857 - if ($calls_made >= $budget) break;
7858 - $calls_made++;
7859 - $name = isset($tc['function']['name']) ? $tc['function']['name'] : '';
7860 - $args = array();
7861 - if (isset($tc['function']['arguments'])) {
7862 - $decoded = json_decode($tc['function']['arguments'], true);
7863 - if (is_array($decoded)) $args = $decoded;
7864 - }
7865 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7866 - $messages[] = array(
7867 - 'role' => 'tool',
7868 - 'tool_call_id' => isset($tc['id']) ? $tc['id'] : '',
7869 - 'content' => $exec['content'],
7870 - );
7871 - }
7872 - }
7873 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7874 -}
7875 -
7876 -/* ---------------- Anthropic Claude loop ---------------- */
7877 -private function mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7878 - $messages = $this->mxchat_fc_normalize_history($conversation_history);
7879 - $messages[] = array('role' => 'user', 'content' => $relevant_content);
7880 -
7881 - $depth = MxChat_Tool_Registry::max_depth();
7882 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7883 - $tool_schema = MxChat_Tool_Registry::to_anthropic_tools($tools);
7884 - $omit_temp = $this->mxchat_claude_omits_temperature($prov['model']);
7885 - $used_tool = false;
7886 - $calls_made = 0;
7887 -
7888 - for ($step = 0; $step <= $depth; $step++) {
7889 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7890 - $body = array('model' => $prov['model'], 'max_tokens' => 1024, 'temperature' => 0.8,
7891 - 'messages' => $messages, 'system' => $system);
7892 - if ($omit_temp) unset($body['temperature']);
7893 - if ($offer_tools) {
7894 - $body['tools'] = $tool_schema;
7895 - $body['tool_choice'] = array('type' => 'auto');
7896 - }
7897 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7898 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7899 - $this->mxchat_fc_log('anthropic call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7900 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7901 - }
7902 - $content = isset($r['data']['content']) && is_array($r['data']['content']) ? $r['data']['content'] : array();
7903 - $tool_uses = array();
7904 - $text_out = '';
7905 - foreach ($content as $block) {
7906 - if (!isset($block['type'])) continue;
7907 - if ($block['type'] === 'tool_use') {
7908 - $tool_uses[] = $block;
7909 - } elseif ($block['type'] === 'text' && isset($block['text'])) {
7910 - $text_out .= $block['text'];
7911 - }
7912 - }
7913 - if (empty($tool_uses)) {
7914 - if (!$used_tool) return array('handled' => false);
7915 - $text_out = trim($text_out);
7916 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7917 - }
7918 - // Append the assistant turn (the full content array), then a user turn of tool_result blocks.
7919 - $used_tool = true;
7920 - $messages[] = array('role' => 'assistant', 'content' => $content);
7921 - $results = array();
7922 - foreach ($tool_uses as $tu) {
7923 - if ($calls_made >= $budget) break;
7924 - $calls_made++;
7925 - $name = isset($tu['name']) ? $tu['name'] : '';
7926 - $args = isset($tu['input']) && is_array($tu['input']) ? $tu['input'] : array();
7927 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7928 - $results[] = array(
7929 - 'type' => 'tool_result',
7930 - 'tool_use_id' => isset($tu['id']) ? $tu['id'] : '',
7931 - 'content' => $exec['content'],
7932 - );
7933 - }
7934 - $messages[] = array('role' => 'user', 'content' => $results);
7935 - }
7936 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7937 -}
7938 -
7939 -/* ---------------- Google Gemini loop ---------------- */
7940 -private function mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7941 - $contents = array();
7942 - $contents[] = array('role' => 'user', 'parts' => array(array('text' => '[System Instructions] ' . $system . ' ' . $relevant_content)));
7943 - $contents[] = array('role' => 'model', 'parts' => array(array('text' => 'I understand and will follow these instructions.')));
7944 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7945 - $contents[] = array('role' => ($m['role'] === 'assistant' ? 'model' : 'user'),
7946 - 'parts' => array(array('text' => $m['content'])));
7947 - }
7948 -
7949 - $depth = MxChat_Tool_Registry::max_depth();
7950 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7951 - $tool_schema = MxChat_Tool_Registry::to_gemini_tools($tools);
7952 - // Function calling (tools + functionDeclarations + toolConfig) is a v1beta feature on the
7953 - // Generative Language REST API. The v1 endpoint silently ignores the tools array, so a
7954 - // non-preview model (e.g. gemini-2.5-pro, gemini-3.5-flash, gemini-3.1-flash-lite) would
7955 - // just answer in text and never emit a tool call. Always use v1beta for the FC loop —
7956 - // confirmed against Google's function-calling docs (their REST example targets
7957 - // v1beta/models/gemini-3.5-flash:generateContent). v1beta is a superset, so every model
7958 - // reachable on v1 is also reachable here.
7959 - $api_version = 'v1beta';
7960 - $url = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $prov['model'] . ':generateContent?key=' . $prov['key'];
7961 - $headers = array('Content-Type' => 'application/json');
7962 - $used_tool = false;
7963 - $calls_made = 0;
7964 -
7965 - for ($step = 0; $step <= $depth; $step++) {
7966 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7967 - $body = array(
7968 - 'contents' => $contents,
7969 - 'generationConfig' => array('temperature' => 0.7, 'topP' => 0.95, 'topK' => 40, 'maxOutputTokens' => 8192),
7970 - );
7971 - if ($offer_tools) {
7972 - $body['tools'] = $tool_schema;
7973 - $body['toolConfig'] = array('functionCallingConfig' => array('mode' => 'AUTO'));
7974 - }
7975 - $r = $this->mxchat_fc_post($url, $body, $headers, 'gemini');
7976 - if ($r['code'] !== 200 || !is_array($r['data']) || isset($r['data']['error'])) {
7977 - $this->mxchat_fc_log('gemini call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7978 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7979 - }
7980 - $parts = isset($r['data']['candidates'][0]['content']['parts']) && is_array($r['data']['candidates'][0]['content']['parts'])
7981 - ? $r['data']['candidates'][0]['content']['parts'] : array();
7982 - $fn_calls = array();
7983 - $text_out = '';
7984 - foreach ($parts as $p) {
7985 - if (isset($p['functionCall'])) {
7986 - $fn_calls[] = $p['functionCall'];
7987 - } elseif (isset($p['text'])) {
7988 - $text_out .= $p['text'];
7989 - }
7990 - }
7991 - if (empty($fn_calls)) {
7992 - if (!$used_tool) return array('handled' => false);
7993 - $text_out = trim($text_out);
7994 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7995 - }
7996 - // Append the model turn (its parts) then a user turn of functionResponse parts.
7997 - $used_tool = true;
7998 - $contents[] = array('role' => 'model', 'parts' => $parts);
7999 - $resp_parts = array();
8000 - foreach ($fn_calls as $fcall) {
8001 - if ($calls_made >= $budget) break;
8002 - $calls_made++;
8003 - $name = isset($fcall['name']) ? $fcall['name'] : '';
8004 - $args = isset($fcall['args']) && is_array($fcall['args']) ? $fcall['args'] : array();
8005 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
8006 - $fr = array('name' => $name, 'response' => array('result' => $exec['content']));
8007 - // Gemini 3 function calls carry a unique id; echo the matching id back in the
8008 - // functionResponse so the model maps the result to the right call (Google REST
8009 - // guidance). Older models omit the id — then we send none, exactly as before.
8010 - if (isset($fcall['id']) && $fcall['id'] !== '') { $fr['id'] = $fcall['id']; }
8011 - $resp_parts[] = array('functionResponse' => $fr);
8012 - }
8013 - $contents[] = array('role' => 'user', 'parts' => $resp_parts);
8014 - }
8015 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
8016 -}
8017 -
8018 -private function mxchat_fc_giveup_text() {
8019 - return esc_html__('I looked into that but could not put together a final answer. Please try rephrasing your request.', 'mxchat');
8020 -}
8021 -
8022 -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') {
8023 - try {
8024 3194 if (!$relevant_content) {
8025 - $error_response = [
3195 + return [
8026 3196 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
8027 3197 'error_code' => 'no_relevant_content'
8028 3198 ];
8029 -
8030 - if ($testing_data !== null) {
8031 - $error_response['testing_data'] = $testing_data;
8032 - }
8033 -
8034 - return $error_response;
8035 3199 }
8036 3200
3201 + // Ensure conversation_history is an array
8037 3202 if (!is_array($conversation_history)) {
8038 3203 $conversation_history = array();
8039 3204 }
8040 3205
8041 - // Check if this is an OpenRouter model
8042 - if ($selected_model === 'openrouter') {
8043 - // Get the actual OpenRouter model from options
8044 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
8045 -
8046 - if (empty($openrouter_selected_model)) {
8047 - $error_response = [
8048 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
8049 - 'error_code' => 'no_openrouter_model_selected'
8050 - ];
8051 - if ($testing_data !== null) {
8052 - $error_response['testing_data'] = $testing_data;
8053 - }
8054 - return $error_response;
8055 - }
8056 -
8057 - if (empty($openrouter_api_key)) {
8058 - $error_response = [
8059 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
8060 - 'error_code' => 'missing_openrouter_api_key'
8061 - ];
8062 - if ($testing_data !== null) {
8063 - $error_response['testing_data'] = $testing_data;
8064 - }
8065 - return $error_response;
8066 - }
8067 -
8068 - if ($streaming) {
8069 - return $this->mxchat_generate_response_openrouter_stream(
8070 - $openrouter_selected_model,
8071 - $openrouter_api_key,
8072 - $conversation_history,
8073 - $relevant_content,
8074 - $session_id,
8075 - $testing_data
8076 - );
8077 - } else {
8078 - $response = $this->mxchat_generate_response_openrouter(
8079 - $openrouter_selected_model,
8080 - $openrouter_api_key,
8081 - $conversation_history,
8082 - $relevant_content,
8083 - $session_id
8084 - );
8085 - }
8086 -
8087 - if (is_array($response) && isset($response['error'])) {
8088 - if ($testing_data !== null) {
8089 - $response['testing_data'] = $testing_data;
8090 - }
8091 - return $response;
8092 - }
8093 -
8094 - return $response;
8095 - }
8096 -
3206 + // Get selected model with default fallback
3207 + $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-4o';
3208 +
8097 3209 // Extract model prefix to determine the provider
8098 3210 $model_parts = explode('-', $selected_model);
8099 3211 $provider = strtolower($model_parts[0]);
8100 3212
@@ -8101,1986 +3213,147 @@
8101 3213 // Handle model selection based on provider prefix
8102 3214 switch ($provider) {
8103 3215 case 'gemini':
8104 3216 if (empty($gemini_api_key)) {
8105 - $error_response = [
3217 + return [
8106 3218 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
8107 3219 'error_code' => 'missing_gemini_api_key'
8108 3220 ];
8109 - if ($testing_data !== null) {
8110 - $error_response['testing_data'] = $testing_data;
8111 - }
8112 - return $error_response;
8113 3221 }
8114 3222 $response = $this->mxchat_generate_response_gemini(
8115 3223 $selected_model,
8116 3224 $gemini_api_key,
8117 3225 $conversation_history,
8118 - $relevant_content,
8119 - $session_id
3226 + $relevant_content
8120 3227 );
8121 3228 break;
8122 3229
8123 3230 case 'claude':
8124 3231 if (empty($claude_api_key)) {
8125 - $error_response = [
3232 + return [
8126 3233 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
8127 3234 'error_code' => 'missing_claude_api_key'
8128 3235 ];
8129 - if ($testing_data !== null) {
8130 - $error_response['testing_data'] = $testing_data;
8131 - }
8132 - return $error_response;
8133 3236 }
8134 - if ($streaming) {
8135 - return $this->mxchat_generate_response_claude_stream(
8136 - $selected_model,
8137 - $claude_api_key,
8138 - $conversation_history,
8139 - $relevant_content,
8140 - $session_id,
8141 - $testing_data
8142 - );
8143 - } else {
8144 - $response = $this->mxchat_generate_response_claude(
8145 - $selected_model,
8146 - $claude_api_key,
8147 - $conversation_history,
8148 - $relevant_content,
8149 - $session_id
8150 - );
8151 - }
3237 + $response = $this->mxchat_generate_response_claude(
3238 + $selected_model,
3239 + $claude_api_key,
3240 + $conversation_history,
3241 + $relevant_content
3242 + );
8152 3243 break;
8153 3244
8154 3245 case 'grok':
8155 3246 if (empty($xai_api_key)) {
8156 - $error_response = [
3247 + return [
8157 3248 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
8158 3249 'error_code' => 'missing_xai_api_key'
8159 3250 ];
8160 - if ($testing_data !== null) {
8161 - $error_response['testing_data'] = $testing_data;
8162 - }
8163 - return $error_response;
8164 3251 }
8165 - if ($streaming) {
8166 - return $this->mxchat_generate_response_xai_stream(
8167 - $selected_model,
8168 - $xai_api_key,
8169 - $conversation_history,
8170 - $relevant_content,
8171 - $session_id,
8172 - $testing_data
8173 - );
8174 - } else {
8175 - $response = $this->mxchat_generate_response_xai(
8176 - $selected_model,
8177 - $xai_api_key,
8178 - $conversation_history,
8179 - $relevant_content,
8180 - $session_id
8181 - );
8182 - }
3252 + $response = $this->mxchat_generate_response_xai(
3253 + $selected_model,
3254 + $xai_api_key,
3255 + $conversation_history,
3256 + $relevant_content
3257 + );
8183 3258 break;
8184 3259
8185 3260 case 'deepseek':
8186 3261 if (empty($deepseek_api_key)) {
8187 - $error_response = [
3262 + return [
8188 3263 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
8189 3264 'error_code' => 'missing_deepseek_api_key'
8190 3265 ];
8191 - if ($testing_data !== null) {
8192 - $error_response['testing_data'] = $testing_data;
8193 - }
8194 - return $error_response;
8195 3266 }
8196 - if ($streaming) {
8197 - return $this->mxchat_generate_response_deepseek_stream(
8198 - $selected_model,
8199 - $deepseek_api_key,
8200 - $conversation_history,
8201 - $relevant_content,
8202 - $session_id,
8203 - $testing_data
8204 - );
8205 - } else {
8206 - $response = $this->mxchat_generate_response_deepseek(
8207 - $selected_model,
8208 - $deepseek_api_key,
8209 - $conversation_history,
8210 - $relevant_content,
8211 - $session_id
8212 - );
8213 - }
3267 + $response = $this->mxchat_generate_response_deepseek(
3268 + $selected_model,
3269 + $deepseek_api_key,
3270 + $conversation_history,
3271 + $relevant_content
3272 + );
8214 3273 break;
8215 3274
8216 - case 'custom':
8217 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
8218 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
8219 - if (empty($cp_base_url)) {
8220 - $error_response = [
8221 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
8222 - 'error_code' => 'missing_custom_provider_base_url'
8223 - ];
8224 - if ($testing_data !== null) {
8225 - $error_response['testing_data'] = $testing_data;
8226 - }
8227 - return $error_response;
8228 - }
8229 - if ($streaming) {
8230 - return $this->mxchat_generate_response_custom_stream(
8231 - $selected_model,
8232 - $conversation_history,
8233 - $relevant_content,
8234 - $session_id,
8235 - $testing_data
8236 - );
8237 - } else {
8238 - $response = $this->mxchat_generate_response_custom(
8239 - $selected_model,
8240 - $conversation_history,
8241 - $relevant_content
8242 - );
8243 - }
8244 - break;
8245 -
8246 3275 case 'gpt':
8247 - case 'o1':
8248 3276 if (empty($api_key)) {
8249 - $error_response = [
3277 + return [
8250 3278 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
8251 3279 'error_code' => 'missing_openai_api_key'
8252 3280 ];
8253 - if ($testing_data !== null) {
8254 - $error_response['testing_data'] = $testing_data;
8255 - }
8256 - return $error_response;
8257 3281 }
8258 -
8259 - // Check if web search is enabled for this OpenAI model
8260 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8261 - // Models that don't support web search
8262 - $unsupported_web_search_models = array('gpt-4.1-nano');
8263 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
8264 -
8265 - if ($web_search_enabled && $model_supports_web_search) {
8266 - // Use Responses API (required for some models, or when web search is enabled)
8267 - return $this->mxchat_generate_response_openai_web_search(
8268 - $selected_model,
8269 - $api_key,
8270 - $conversation_history,
8271 - $relevant_content,
8272 - $session_id,
8273 - $testing_data,
8274 - $streaming
8275 - );
8276 - } elseif ($streaming) {
8277 - return $this->mxchat_generate_response_openai_stream(
8278 - $selected_model,
8279 - $api_key,
8280 - $conversation_history,
8281 - $relevant_content,
8282 - $session_id,
8283 - $testing_data
8284 - );
8285 - } else {
8286 - $response = $this->mxchat_generate_response_openai(
8287 - $selected_model,
8288 - $api_key,
8289 - $conversation_history,
8290 - $relevant_content,
8291 - $session_id
8292 - );
8293 - }
3282 + $response = $this->mxchat_generate_response_openai(
3283 + $selected_model,
3284 + $api_key,
3285 + $conversation_history,
3286 + $relevant_content
3287 + );
8294 3288 break;
8295 3289
8296 3290 default:
3291 + // Default to OpenAI for custom models or unrecognized prefixes
8297 3292 if (empty($api_key)) {
8298 - $error_response = [
3293 + return [
8299 3294 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
8300 3295 'error_code' => 'missing_openai_api_key'
8301 3296 ];
8302 - if ($testing_data !== null) {
8303 - $error_response['testing_data'] = $testing_data;
8304 - }
8305 - return $error_response;
8306 3297 }
8307 -
8308 - // Check if web search is enabled (default case also handles OpenAI models)
8309 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8310 - $unsupported_web_search_models = array('gpt-4.1-nano');
8311 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
8312 -
8313 - if ($web_search_enabled && $model_supports_web_search) {
8314 - return $this->mxchat_generate_response_openai_web_search(
8315 - $selected_model,
8316 - $api_key,
8317 - $conversation_history,
8318 - $relevant_content,
8319 - $session_id,
8320 - $testing_data,
8321 - $streaming
8322 - );
8323 - } elseif ($streaming) {
8324 - return $this->mxchat_generate_response_openai_stream(
8325 - $selected_model,
8326 - $api_key,
8327 - $conversation_history,
8328 - $relevant_content,
8329 - $session_id,
8330 - $testing_data
8331 - );
8332 - } else {
8333 - $response = $this->mxchat_generate_response_openai(
8334 - $selected_model,
8335 - $api_key,
8336 - $conversation_history,
8337 - $relevant_content,
8338 - $session_id
8339 - );
8340 - }
3298 + $response = $this->mxchat_generate_response_openai(
3299 + $selected_model,
3300 + $api_key,
3301 + $conversation_history,
3302 + $relevant_content
3303 + );
8341 3304 break;
8342 3305 }
8343 3306
3307 + // Check if the response is an error array from the provider-specific function
8344 3308 if (is_array($response) && isset($response['error'])) {
8345 - if ($testing_data !== null) {
8346 - $response['testing_data'] = $testing_data;
8347 - }
8348 - return $response;
3309 + return $response; // Pass through the error
8349 3310 }
8350 3311
8351 3312 return $response;
8352 3313
8353 3314 } catch (Exception $e) {
8354 - $error_response = [
3315 + //error_log('MXChat Error: ' . $e->getMessage());
3316 + return [
8355 3317 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
8356 3318 'error_code' => 'system_exception',
8357 3319 'exception_details' => $e->getMessage()
8358 3320 ];
8359 -
8360 - if ($testing_data !== null) {
8361 - $error_response['testing_data'] = $testing_data;
8362 - }
8363 -
8364 - return $error_response;
8365 3321 }
8366 3322 }
8367 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8368 - try {
8369 - $bot_id = $this->get_current_bot_id($session_id);
8370 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8371 -
8372 - if (!is_array($conversation_history)) {
8373 - $conversation_history = array();
8374 - }
8375 3323
8376 - $formatted_conversation = array();
8377 -
8378 - $formatted_conversation[] = array(
8379 - 'role' => 'system',
8380 - 'content' => $system_prompt_instructions . " " . $relevant_content
8381 - );
8382 -
8383 - foreach ($conversation_history as $message) {
8384 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8385 - $role = $message['role'];
8386 - if ($role === 'bot' || $role === 'agent') {
8387 - $role = 'assistant';
8388 - }
8389 - if (!in_array($role, ['system', 'assistant', 'user'])) {
8390 - $role = 'user';
8391 - }
8392 - $formatted_conversation[] = array(
8393 - 'role' => $role,
8394 - 'content' => $message['content']
8395 - );
8396 - }
8397 - }
8398 -
8399 - if (headers_sent() || !function_exists('curl_init')) {
8400 - $regular_response = $this->mxchat_generate_response_openrouter(
8401 - $selected_model,
8402 - $openrouter_api_key,
8403 - $conversation_history,
8404 - $relevant_content,
8405 - $session_id
8406 - );
8407 -
8408 - // Save bot response to transcript
8409 - if (!empty($regular_response) && !empty($session_id)) {
8410 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8411 - }
8412 -
8413 - $response_data = [
8414 - 'text' => $regular_response,
8415 - 'html' => '',
8416 - 'session_id' => $session_id
8417 - ];
8418 -
8419 - if ($testing_data !== null) {
8420 - $response_data['testing_data'] = $testing_data;
8421 - }
8422 -
8423 - header('Content-Type: application/json');
8424 - echo json_encode($response_data);
8425 - return true;
8426 - }
8427 -
8428 - $body = json_encode([
8429 - 'model' => $selected_model,
8430 - 'messages' => $formatted_conversation,
8431 - 'temperature' => 1,
8432 - 'stream' => true
8433 - ]);
8434 -
8435 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
8436 - // inside WRITEFUNCTION on first byte of a successful upstream.
8437 -
8438 - $captured_status_code = 0;
8439 - $captured_body_pre_stream = '';
8440 - $full_response = '';
8441 - $stream_started = false;
8442 - $buffer = '';
8443 - $errno = 0;
8444 - $last_curl_error = '';
8445 - $http_code = 0;
8446 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8447 - $backoff_ms = array(0, 750, 2000);
8448 -
8449 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8450 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8451 - usleep($backoff_ms[$attempt] * 1000);
8452 - }
8453 -
8454 - $captured_status_code = 0;
8455 - $captured_body_pre_stream = '';
8456 - $full_response = '';
8457 - $stream_started = false;
8458 - $buffer = '';
8459 -
8460 - $ch = curl_init();
8461 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
8462 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8463 - curl_setopt($ch, CURLOPT_POST, true);
8464 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8465 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8466 - 'Content-Type: application/json',
8467 - 'Authorization: Bearer ' . $openrouter_api_key,
8468 - 'HTTP-Referer: ' . home_url(),
8469 - 'X-Title: ' . get_bloginfo('name')
8470 - ));
8471 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8472 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8473 -
8474 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8475 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8476 - $captured_status_code = (int) $m[1];
8477 - }
8478 - return strlen($header);
8479 - });
8480 -
8481 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8482 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8483 - $captured_body_pre_stream .= $data;
8484 - return strlen($data);
8485 - }
8486 -
8487 - if (!$this->streaming_headers_sent) {
8488 - $this->setup_streaming_headers();
8489 - }
8490 -
8491 - if (!$stream_started && $testing_data !== null) {
8492 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8493 - flush();
8494 - $stream_started = true;
8495 - }
8496 -
8497 - $buffer .= $data;
8498 - $lines = explode("\n", $buffer);
8499 - $buffer = array_pop($lines);
8500 -
8501 - foreach ($lines as $line) {
8502 - if (trim($line) === '') {
8503 - continue;
8504 - }
8505 - if (strpos($line, 'data: ') !== 0) {
8506 - continue;
8507 - }
8508 -
8509 - $json_str = substr($line, 6);
8510 -
8511 - if (trim($json_str) === '[DONE]') {
8512 - echo "data: [DONE]\n\n";
8513 - flush();
8514 - continue;
8515 - }
8516 -
8517 - $json = json_decode(trim($json_str), true);
8518 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8519 - $content = $json['choices'][0]['delta']['content'];
8520 - $full_response .= $content;
8521 -
8522 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8523 - flush();
8524 - }
8525 - }
8526 -
8527 - return strlen($data);
8528 - });
8529 -
8530 - $response = curl_exec($ch);
8531 - $errno = curl_errno($ch);
8532 - $last_curl_error = curl_error($ch);
8533 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8534 - curl_close($ch);
8535 -
8536 - if (!$errno && $http_code === 200) {
8537 - break;
8538 - }
8539 -
8540 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8541 - $can_retry = !$this->streaming_headers_sent
8542 - && ($attempt + 1) < $max_attempts
8543 - && $is_transient;
8544 -
8545 - if (defined('WP_DEBUG') && WP_DEBUG) {
8546 - error_log(sprintf(
8547 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8548 - $attempt + 1, $max_attempts, $http_code, $errno,
8549 - $is_transient ? 'yes' : 'no',
8550 - $can_retry ? 'Retrying.' : 'Giving up.'
8551 - ));
8552 - }
8553 -
8554 - if (!$can_retry) {
8555 - break;
8556 - }
8557 - }
8558 -
8559 - if (!$errno && $http_code === 200) {
8560 - if (!empty($full_response) && !empty($session_id)) {
8561 - $rag_context_for_storage = null;
8562 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8563 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8564 -
8565 - if ($has_rag_data || $has_action_data) {
8566 - $rag_context_for_storage = [];
8567 -
8568 - if ($has_rag_data) {
8569 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8570 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8571 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8572 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8573 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8574 - }
8575 -
8576 - if ($has_action_data) {
8577 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8578 - }
8579 - }
8580 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8581 - }
8582 - return true;
8583 - }
8584 -
8585 - return $this->mxchat_stream_emit_fallback(
8586 - 'openai',
8587 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8588 - $session_id,
8589 - $testing_data
8590 - );
8591 -
8592 - } catch (Exception $e) {
8593 - return $this->mxchat_stream_emit_fallback(
8594 - 'openai',
8595 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8596 - $session_id,
8597 - $testing_data
8598 - );
8599 - }
8600 -}
8601 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
3324 +private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content) {
8602 3325 try {
8603 - $bot_id = $this->get_current_bot_id($session_id);
8604 -
8605 - // Get system prompt instructions using centralized function
8606 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8607 -
8608 3326 // Ensure conversation_history is an array
8609 3327 if (!is_array($conversation_history)) {
8610 3328 $conversation_history = array();
8611 3329 }
8612 3330
8613 - // Format conversation history for OpenAI
3331 + // Get system prompt instructions from options
3332 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3333 +
3334 + // Create a new array for the formatted conversation
8614 3335 $formatted_conversation = array();
8615 3336
3337 + // Add system message first
8616 3338 $formatted_conversation[] = array(
8617 3339 'role' => 'system',
8618 3340 'content' => $system_prompt_instructions . " " . $relevant_content
8619 3341 );
8620 3342
3343 + // Add the rest of the conversation history
8621 3344 foreach ($conversation_history as $message) {
8622 3345 if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8623 3346 $role = $message['role'];
8624 - if ($role === 'bot' || $role === 'agent') {
8625 - $role = 'assistant';
8626 - }
8627 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8628 - $role = 'user';
8629 - }
8630 - $formatted_conversation[] = array(
8631 - 'role' => $role,
8632 - 'content' => $message['content']
8633 - );
8634 - }
8635 - }
8636 3347
8637 - // Check if we can actually stream
8638 - if (headers_sent() || !function_exists('curl_init')) {
8639 - // Fallback to regular response with testing data
8640 - $regular_response = $this->mxchat_generate_response_openai(
8641 - $selected_model,
8642 - $api_key,
8643 - $conversation_history,
8644 - $relevant_content,
8645 - $session_id
8646 - );
8647 -
8648 - // Save bot response to transcript
8649 - if (!empty($regular_response) && !empty($session_id)) {
8650 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8651 - }
8652 -
8653 - $response_data = [
8654 - 'text' => $regular_response,
8655 - 'html' => '',
8656 - 'session_id' => $session_id
8657 - ];
8658 -
8659 - if ($testing_data !== null) {
8660 - $response_data['testing_data'] = $testing_data;
8661 - }
8662 -
8663 - header('Content-Type: application/json');
8664 - echo json_encode($response_data);
8665 - return true;
8666 - }
8667 -
8668 - // Build request body with optimal settings for fast streaming
8669 - $request_body = [
8670 - 'model' => $selected_model,
8671 - 'messages' => $formatted_conversation,
8672 - 'temperature' => 1,
8673 - 'stream' => true
8674 - ];
8675 -
8676 - // reasoning_effort — sourced from the core model catalog (plan-dcb71c);
8677 - // frozen inline ladder lives in mxchat_reasoning_effort_fallback().
8678 - $effort = $this->mxchat_reasoning_effort_for($selected_model, 'chat');
8679 - if ($effort !== null) {
8680 - $request_body['reasoning_effort'] = $effort;
8681 - }
8682 -
8683 - $body = json_encode($request_body);
8684 -
8685 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
8686 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
8687 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
8688 -
8689 - $captured_status_code = 0;
8690 - $captured_body_pre_stream = '';
8691 - $full_response = '';
8692 - $stream_started = false;
8693 - $buffer = '';
8694 - $errno = 0;
8695 - $last_curl_error = '';
8696 - $http_code = 0;
8697 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8698 - $backoff_ms = array(0, 750, 2000);
8699 -
8700 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8701 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8702 - usleep($backoff_ms[$attempt] * 1000);
8703 - }
8704 -
8705 - // Reset per-attempt capture state.
8706 - $captured_status_code = 0;
8707 - $captured_body_pre_stream = '';
8708 - $full_response = '';
8709 - $stream_started = false;
8710 - $buffer = '';
8711 -
8712 - $ch = curl_init();
8713 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
8714 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8715 - curl_setopt($ch, CURLOPT_POST, true);
8716 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8717 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8718 - 'Content-Type: application/json',
8719 - 'Authorization: Bearer ' . $api_key
8720 - ));
8721 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8722 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8723 -
8724 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
8725 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8726 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8727 - $captured_status_code = (int) $m[1];
8728 - }
8729 - return strlen($header);
8730 - });
8731 -
8732 - // Buffer control for real-time streaming
8733 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8734 - // V2 guard: if upstream returned non-200, buffer body for transient
8735 - // classification and DO NOT emit to client. Stream channel must NOT open.
8736 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8737 - $captured_body_pre_stream .= $data;
8738 - return strlen($data);
8739 - }
8740 -
8741 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
8742 - // After this point streaming_headers_sent === true → retry is structurally blocked.
8743 - if (!$this->streaming_headers_sent) {
8744 - $this->setup_streaming_headers();
8745 - }
8746 -
8747 - // Send testing data as the first event if available
8748 - if (!$stream_started && $testing_data !== null) {
8749 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8750 - flush();
8751 - $stream_started = true;
8752 - }
8753 -
8754 - // CRITICAL FIX: Append new data to buffer
8755 - $buffer .= $data;
8756 -
8757 - // Process complete lines only
8758 - $lines = explode("\n", $buffer);
8759 -
8760 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8761 - $buffer = array_pop($lines);
8762 -
8763 - foreach ($lines as $line) {
8764 - if (trim($line) === '') {
8765 - continue;
8766 - }
8767 - if (strpos($line, 'data: ') !== 0) {
8768 - continue;
8769 - }
8770 -
8771 - $json_str = substr($line, 6);
8772 -
8773 - if (trim($json_str) === '[DONE]') {
8774 - echo "data: [DONE]\n\n";
8775 - flush();
8776 - continue;
8777 - }
8778 -
8779 - $json = json_decode(trim($json_str), true);
8780 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8781 - $content = $json['choices'][0]['delta']['content'];
8782 - $full_response .= $content;
8783 -
8784 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8785 - flush();
8786 - }
8787 - }
8788 -
8789 - return strlen($data);
8790 - });
8791 -
8792 - $response = curl_exec($ch);
8793 - $errno = curl_errno($ch);
8794 - $last_curl_error = curl_error($ch);
8795 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8796 - curl_close($ch);
8797 -
8798 - if (!$errno && $http_code === 200) {
8799 - break; // Happy path — WRITEFUNCTION already streamed everything.
8800 - }
8801 -
8802 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8803 - $can_retry = !$this->streaming_headers_sent
8804 - && ($attempt + 1) < $max_attempts
8805 - && $is_transient;
8806 -
8807 - if (defined('WP_DEBUG') && WP_DEBUG) {
8808 - error_log(sprintf(
8809 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8810 - $attempt + 1, $max_attempts, $http_code, $errno,
8811 - $is_transient ? 'yes' : 'no',
8812 - $can_retry ? 'Retrying.' : 'Giving up.'
8813 - ));
8814 - }
8815 -
8816 - if (!$can_retry) {
8817 - break;
8818 - }
8819 - }
8820 -
8821 - // Post-loop branch.
8822 - if (!$errno && $http_code === 200) {
8823 - // Happy path — save the complete response to maintain chat persistence.
8824 - if (!empty($full_response) && !empty($session_id)) {
8825 - $rag_context_for_storage = null;
8826 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8827 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8828 -
8829 - if ($has_rag_data || $has_action_data) {
8830 - $rag_context_for_storage = [];
8831 -
8832 - if ($has_rag_data) {
8833 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8834 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8835 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8836 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8837 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8838 - }
8839 -
8840 - if ($has_action_data) {
8841 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8842 - }
8843 - }
8844 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8845 - }
8846 -
8847 - return true;
8848 - }
8849 -
8850 - // Failure path — branch on whether SSE channel was opened.
8851 - return $this->mxchat_stream_emit_fallback(
8852 - 'openai',
8853 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8854 - $session_id,
8855 - $testing_data
8856 - );
8857 -
8858 - } catch (Exception $e) {
8859 - return $this->mxchat_stream_emit_fallback(
8860 - 'openai',
8861 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8862 - $session_id,
8863 - $testing_data
8864 - );
8865 - }
8866 -}
8867 -
8868 -/**
8869 - * Shared fallback emitter for streaming chat functions. Two outcomes:
8870 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
8871 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
8872 - * normal bot bubble. Transcript row is persisted.
8873 - * - streaming_headers_sent === false: SSE channel never opened (retries
8874 - * exhausted on initial connect). Emit a clean JSON response — the path
8875 - * the widget would normally hit if streaming wasn't even attempted.
8876 - *
8877 - * Used by all six *_stream functions after their per-attempt retry loop.
8878 - */
8879 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
8880 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
8881 -
8882 - if ($this->streaming_headers_sent) {
8883 - if ($is_error_array) {
8884 - echo "data: " . json_encode([
8885 - 'error' => true,
8886 - 'error_message' => $regular_response['error'],
8887 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8888 - 'text' => $regular_response['error'],
8889 - 'message' => $regular_response['error']
8890 - ]) . "\n\n";
8891 - echo "data: [DONE]\n\n";
8892 - flush();
8893 - return true;
8894 - }
8895 - $fallback_message = (string) $regular_response;
8896 - if (!empty($fallback_message) && !empty($session_id)) {
8897 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8898 - }
8899 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
8900 - echo "data: [DONE]\n\n";
8901 - flush();
8902 - return true;
8903 - }
8904 -
8905 - // SSE channel never opened — clean JSON fallback.
8906 - if ($is_error_array) {
8907 - header('Content-Type: application/json');
8908 - echo json_encode(array(
8909 - 'error' => true,
8910 - 'error_message' => $regular_response['error'],
8911 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8912 - 'text' => $regular_response['error'],
8913 - 'message' => $regular_response['error'],
8914 - ));
8915 - return true;
8916 - }
8917 -
8918 - $fallback_message = (string) $regular_response;
8919 - if (!empty($fallback_message) && !empty($session_id)) {
8920 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8921 - }
8922 - $response_data = array(
8923 - 'text' => $fallback_message,
8924 - 'html' => '',
8925 - 'session_id' => $session_id,
8926 - );
8927 - if ($testing_data !== null) {
8928 - $response_data['testing_data'] = $testing_data;
8929 - }
8930 - header('Content-Type: application/json');
8931 - echo json_encode($response_data);
8932 - return true;
8933 -}
8934 -
8935 -/**
8936 - * Resolve custom (OpenAI-compatible) provider config from settings.
8937 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
8938 - */
8939 -private function mxchat_resolve_custom_provider() {
8940 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
8941 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
8942 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
8943 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
8944 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
8945 -
8946 - $chat_url = $base_url . '/chat/completions';
8947 - if (!empty($api_version)) {
8948 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
8949 - }
8950 -
8951 - $headers = array('Content-Type: application/json');
8952 - if (!empty($api_key)) {
8953 - if ($auth_scheme === 'api-key') {
8954 - $headers[] = 'api-key: ' . $api_key;
8955 - } else {
8956 - $headers[] = 'Authorization: Bearer ' . $api_key;
8957 - }
8958 - }
8959 -
8960 - return array(
8961 - 'base_url' => $base_url,
8962 - 'api_key' => $api_key,
8963 - 'model' => $model !== '' ? $model : 'default',
8964 - 'auth_scheme' => $auth_scheme,
8965 - 'api_version' => $api_version,
8966 - 'chat_url' => $chat_url,
8967 - 'headers' => $headers,
8968 - );
8969 -}
8970 -
8971 -/**
8972 - * Streaming chat completion against an OpenAI-compatible custom provider
8973 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
8974 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
8975 - */
8976 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8977 - try {
8978 - $cfg = $this->mxchat_resolve_custom_provider();
8979 - if (empty($cfg['base_url'])) {
8980 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8981 - }
8982 -
8983 - $bot_id = $this->get_current_bot_id($session_id);
8984 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8985 - if (!is_array($conversation_history)) {
8986 - $conversation_history = array();
8987 - }
8988 -
8989 - $formatted_conversation = array();
8990 - $formatted_conversation[] = array(
8991 - 'role' => 'system',
8992 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8993 - );
8994 - foreach ($conversation_history as $message) {
8995 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8996 - $role = $message['role'];
8997 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8998 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8999 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
9000 - }
9001 - }
9002 -
9003 - if (headers_sent() || !function_exists('curl_init')) {
9004 - // No streaming capability — fall through to non-stream wrapper
9005 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
9006 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
9007 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
9008 - }
9009 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
9010 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
9011 - header('Content-Type: application/json');
9012 - echo json_encode($response_data);
9013 - return true;
9014 - }
9015 -
9016 - $request_body = array(
9017 - 'model' => $cfg['model'],
9018 - 'messages' => $formatted_conversation,
9019 - 'stream' => true,
9020 - );
9021 - $body = json_encode($request_body);
9022 -
9023 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9024 -
9025 - $captured_status_code = 0;
9026 - $captured_body_pre_stream = '';
9027 - $full_response = '';
9028 - $stream_started = false;
9029 - $buffer = '';
9030 - $errno = 0;
9031 - $http_code = 0;
9032 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9033 - $backoff_ms = array(0, 750, 2000);
9034 -
9035 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9036 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9037 - usleep($backoff_ms[$attempt] * 1000);
9038 - }
9039 -
9040 - $captured_status_code = 0;
9041 - $captured_body_pre_stream = '';
9042 - $full_response = '';
9043 - $stream_started = false;
9044 - $buffer = '';
9045 -
9046 - $ch = curl_init();
9047 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
9048 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9049 - curl_setopt($ch, CURLOPT_POST, true);
9050 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9051 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
9052 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9053 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
9054 -
9055 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9056 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9057 - $captured_status_code = (int) $m[1];
9058 - }
9059 - return strlen($header);
9060 - });
9061 -
9062 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9063 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9064 - $captured_body_pre_stream .= $data;
9065 - return strlen($data);
9066 - }
9067 -
9068 - if (!$this->streaming_headers_sent) {
9069 - $this->setup_streaming_headers();
9070 - }
9071 -
9072 - if (!$stream_started && $testing_data !== null) {
9073 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
9074 - flush();
9075 - $stream_started = true;
9076 - }
9077 - $buffer .= $data;
9078 - $lines = explode("\n", $buffer);
9079 - $buffer = array_pop($lines);
9080 - foreach ($lines as $line) {
9081 - if (trim($line) === '') { continue; }
9082 - if (strpos($line, 'data: ') !== 0) { continue; }
9083 - $json_str = substr($line, 6);
9084 - if (trim($json_str) === '[DONE]') {
9085 - echo "data: [DONE]\n\n";
9086 - flush();
9087 - continue;
9088 - }
9089 - $json = json_decode(trim($json_str), true);
9090 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9091 - $content = $json['choices'][0]['delta']['content'];
9092 - $full_response .= $content;
9093 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
9094 - flush();
9095 - }
9096 - }
9097 - return strlen($data);
9098 - });
9099 -
9100 - $response = curl_exec($ch);
9101 - $errno = curl_errno($ch);
9102 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9103 - curl_close($ch);
9104 -
9105 - if (!$errno && $http_code === 200) {
9106 - break;
9107 - }
9108 -
9109 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9110 - $can_retry = !$this->streaming_headers_sent
9111 - && ($attempt + 1) < $max_attempts
9112 - && $is_transient;
9113 -
9114 - if (defined('WP_DEBUG') && WP_DEBUG) {
9115 - error_log(sprintf(
9116 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9117 - $attempt + 1, $max_attempts, $http_code, $errno,
9118 - $is_transient ? 'yes' : 'no',
9119 - $can_retry ? 'Retrying.' : 'Giving up.'
9120 - ));
9121 - }
9122 -
9123 - if (!$can_retry) {
9124 - break;
9125 - }
9126 - }
9127 -
9128 - if (!$errno && $http_code === 200) {
9129 - if (!empty($full_response) && !empty($session_id)) {
9130 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
9131 - }
9132 - return true;
9133 - }
9134 -
9135 - return $this->mxchat_stream_emit_fallback(
9136 - 'openai',
9137 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
9138 - $session_id,
9139 - $testing_data
9140 - );
9141 -
9142 - } catch (Exception $e) {
9143 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
9144 - }
9145 -}
9146 -
9147 -/**
9148 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
9149 - * Returns string content on success, array['error'=>...] on failure.
9150 - */
9151 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
9152 - $cfg = $this->mxchat_resolve_custom_provider();
9153 - if (empty($cfg['base_url'])) {
9154 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
9155 - }
9156 -
9157 - $bot_id = $this->get_current_bot_id(null);
9158 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
9159 - if (!is_array($conversation_history)) {
9160 - $conversation_history = array();
9161 - }
9162 -
9163 - $messages = array(array(
9164 - 'role' => 'system',
9165 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
9166 - ));
9167 - foreach ($conversation_history as $message) {
9168 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9169 - $role = $message['role'];
9170 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
9171 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
9172 - $messages[] = array('role' => $role, 'content' => $message['content']);
9173 - }
9174 - }
9175 -
9176 - $headers_assoc = array('Content-Type' => 'application/json');
9177 - if (!empty($cfg['api_key'])) {
9178 - if ($cfg['auth_scheme'] === 'api-key') {
9179 - $headers_assoc['api-key'] = $cfg['api_key'];
9180 - } else {
9181 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
9182 - }
9183 - }
9184 -
9185 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
9186 - 'headers' => $headers_assoc,
9187 - 'body' => wp_json_encode(array(
9188 - 'model' => $cfg['model'],
9189 - 'messages' => $messages,
9190 - )),
9191 - 'timeout' => 120,
9192 - ), 'openai');
9193 -
9194 - if (is_wp_error($response)) {
9195 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
9196 - }
9197 - $code = (int) wp_remote_retrieve_response_code($response);
9198 - if ($code < 200 || $code >= 300) {
9199 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
9200 - }
9201 - $body = json_decode(wp_remote_retrieve_body($response), true);
9202 - if (isset($body['choices'][0]['message']['content'])) {
9203 - return (string) $body['choices'][0]['message']['content'];
9204 - }
9205 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
9206 -}
9207 -
9208 -/**
9209 - * Generate response using OpenAI Responses API with web search tool
9210 - * This uses the newer Responses API which supports web search functionality
9211 - */
9212 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
9213 - try {
9214 - $bot_id = $this->get_current_bot_id($session_id);
9215 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9216 -
9217 - if (!is_array($conversation_history)) {
9218 - $conversation_history = array();
9219 - }
9220 -
9221 - // Build the input for Responses API
9222 - // The Responses API uses a different format - we need to construct the input properly
9223 - $input_parts = [];
9224 -
9225 - // Add system instructions as context
9226 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
9227 -
9228 - // Build conversation as input items for Responses API
9229 - foreach ($conversation_history as $message) {
9230 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9231 - $role = $message['role'];
3348 + // Convert roles to supported format
9232 3349 if ($role === 'bot' || $role === 'agent') {
9233 3350 $role = 'assistant';
9234 3351 }
9235 - if (!in_array($role, ['assistant', 'user'])) {
9236 - $role = 'user';
9237 - }
9238 - $input_parts[] = [
9239 - 'type' => 'message',
9240 - 'role' => $role,
9241 - 'content' => $message['content']
9242 - ];
9243 - }
9244 - }
9245 -
9246 - // Build request body for Responses API
9247 - $request_body = [
9248 - 'model' => $selected_model,
9249 - 'input' => $input_parts,
9250 - 'instructions' => $system_context,
9251 - 'stream' => $streaming
9252 - ];
9253 -
9254 - // Only add web search tool if web search is enabled in settings
9255 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
9256 - if ($web_search_enabled) {
9257 - $request_body['tools'] = [
9258 - ['type' => 'web_search']
9259 - ];
9260 - }
9261 -
9262 - // reasoning.effort — sourced from the core model catalog (plan-dcb71c),
9263 - // 'websearch' surface; frozen inline ladder in mxchat_reasoning_effort_fallback().
9264 - $effort = $this->mxchat_reasoning_effort_for($selected_model, 'websearch');
9265 - if ($effort !== null) {
9266 - $request_body['reasoning'] = ['effort' => $effort];
9267 - }
9268 -
9269 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
9270 -
9271 - if ($streaming) {
9272 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
9273 - } else {
9274 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
9275 - }
9276 -
9277 - } catch (Exception $e) {
9278 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
9279 - return [
9280 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
9281 - 'error_code' => 'web_search_exception'
9282 - ];
9283 - }
9284 -}
9285 -
9286 -/**
9287 - * Handle non-streaming web search response
9288 - */
9289 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9290 - $request_body['stream'] = false;
9291 -
9292 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
9293 - 'headers' => array(
9294 - 'Authorization' => 'Bearer ' . $api_key,
9295 - 'Content-Type' => 'application/json'
9296 - ),
9297 - 'body' => json_encode($request_body),
9298 - 'timeout' => 90
9299 - ), 'openai');
9300 -
9301 - if (is_wp_error($response)) {
9302 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
9303 - return [
9304 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
9305 - 'error_code' => 'web_search_connection_error'
9306 - ];
9307 - }
9308 -
9309 - $response_code = wp_remote_retrieve_response_code($response);
9310 - $response_body = wp_remote_retrieve_body($response);
9311 -
9312 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
9313 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
9314 -
9315 - if ($response_code !== 200) {
9316 - $error_data = json_decode($response_body, true);
9317 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
9318 - return [
9319 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
9320 - 'error_code' => 'web_search_api_error'
9321 - ];
9322 - }
9323 -
9324 - $result = json_decode($response_body, true);
9325 -
9326 - if (json_last_error() !== JSON_ERROR_NONE) {
9327 - return [
9328 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
9329 - 'error_code' => 'web_search_json_error'
9330 - ];
9331 - }
9332 -
9333 - // Extract the response text and citations from Responses API format
9334 - $output_text = '';
9335 - $citations = [];
9336 -
9337 - if (isset($result['output'])) {
9338 - foreach ($result['output'] as $output_item) {
9339 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
9340 - foreach ($output_item['content'] as $content_item) {
9341 - if ($content_item['type'] === 'output_text') {
9342 - $output_text .= $content_item['text'];
9343 -
9344 - // Extract citations/annotations
9345 - if (isset($content_item['annotations'])) {
9346 - foreach ($content_item['annotations'] as $annotation) {
9347 - if ($annotation['type'] === 'url_citation') {
9348 - $citations[] = [
9349 - 'url' => $annotation['url'],
9350 - 'title' => $annotation['title'] ?? ''
9351 - ];
9352 - }
9353 - }
9354 - }
9355 - }
9356 - }
9357 - }
9358 - }
9359 - }
9360 -
9361 - // If we have citations, append them to the response
9362 - if (!empty($citations)) {
9363 - $output_text .= "\n\n**Sources:**\n";
9364 - $seen_urls = [];
9365 - foreach ($citations as $citation) {
9366 - if (!in_array($citation['url'], $seen_urls)) {
9367 - $seen_urls[] = $citation['url'];
9368 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9369 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9370 - }
9371 - }
9372 - }
9373 -
9374 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
9375 - // which includes rag_context for the "sources" link in transcripts.
9376 -
9377 - return $output_text;
9378 -}
9379 -
9380 -/**
9381 - * Handle streaming web search response using Responses API
9382 - */
9383 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9384 - $request_body['stream'] = true;
9385 -
9386 - // Check if we can stream
9387 - if (headers_sent() || !function_exists('curl_init')) {
9388 - // Fallback to non-streaming
9389 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
9390 - }
9391 -
9392 - // Setup streaming headers
9393 - $this->setup_streaming_headers();
9394 -
9395 - $ch = curl_init();
9396 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
9397 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9398 - curl_setopt($ch, CURLOPT_POST, true);
9399 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
9400 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9401 - 'Content-Type: application/json',
9402 - 'Authorization: Bearer ' . $api_key
9403 - ));
9404 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9405 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
9406 -
9407 - $full_response = '';
9408 - $stream_started = false;
9409 - $buffer = '';
9410 - $citations = [];
9411 -
9412 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
9413 - // Send testing data as first event if available
9414 - if (!$stream_started && $testing_data !== null) {
9415 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9416 - flush();
9417 - $stream_started = true;
9418 - }
9419 -
9420 - $buffer .= $data;
9421 - $lines = explode("\n", $buffer);
9422 - $buffer = array_pop($lines);
9423 -
9424 - foreach ($lines as $line) {
9425 - if (trim($line) === '') continue;
9426 - if (strpos($line, 'data: ') !== 0) continue;
9427 -
9428 - $json_str = substr($line, 6);
9429 -
9430 - if (trim($json_str) === '[DONE]') {
9431 - // Append citations if we have any
9432 - if (!empty($citations)) {
9433 - $citation_text = "\n\n**Sources:**\n";
9434 - $seen_urls = [];
9435 - foreach ($citations as $citation) {
9436 - if (!in_array($citation['url'], $seen_urls)) {
9437 - $seen_urls[] = $citation['url'];
9438 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9439 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9440 - }
9441 - }
9442 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
9443 - $full_response .= $citation_text;
9444 - flush();
9445 - }
9446 - echo "data: [DONE]\n\n";
9447 - flush();
9448 - continue;
9449 - }
9450 -
9451 - $json = json_decode(trim($json_str), true);
9452 - if (!$json) continue;
9453 -
9454 - // Handle Responses API streaming events
9455 - // The format is different from Chat Completions
9456 - if (isset($json['type'])) {
9457 - switch ($json['type']) {
9458 - case 'response.output_text.delta':
9459 - // Text content delta
9460 - if (isset($json['delta'])) {
9461 - $content = $json['delta'];
9462 - $full_response .= $content;
9463 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9464 - flush();
9465 - }
9466 - break;
9467 -
9468 - case 'response.output_item.done':
9469 - // Check for citations in completed items
9470 - if (isset($json['item']['content'])) {
9471 - foreach ($json['item']['content'] as $content_item) {
9472 - if (isset($content_item['annotations'])) {
9473 - foreach ($content_item['annotations'] as $annotation) {
9474 - if ($annotation['type'] === 'url_citation') {
9475 - $citations[] = [
9476 - 'url' => $annotation['url'],
9477 - 'title' => $annotation['title'] ?? ''
9478 - ];
9479 - }
9480 - }
9481 - }
9482 - }
9483 - }
9484 - break;
9485 - }
9486 - }
9487 - }
9488 -
9489 - return strlen($data);
9490 - });
9491 -
9492 - $response = curl_exec($ch);
9493 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9494 -
9495 - if (curl_errno($ch) || $http_code !== 200) {
9496 - $curl_error = curl_error($ch);
9497 - curl_close($ch);
9498 -
9499 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
9500 -
9501 - return $this->mxchat_stream_emit_fallback(
9502 - 'web_search',
9503 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
9504 - $session_id,
9505 - $testing_data
9506 - );
9507 - }
9508 -
9509 - curl_close($ch);
9510 -
9511 - // Save the complete response with RAG context so the "sources" link
9512 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
9513 - if (!empty($full_response) && !empty($session_id)) {
9514 - $rag_context_for_storage = null;
9515 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9516 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9517 -
9518 - if ($has_rag_data || $has_action_data) {
9519 - $rag_context_for_storage = [];
9520 -
9521 - if ($has_rag_data) {
9522 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9523 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9524 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9525 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9526 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9527 - }
9528 -
9529 - if ($has_action_data) {
9530 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9531 - }
9532 - }
9533 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9534 - }
9535 -
9536 - return true;
9537 -}
9538 -
9539 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9540 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
9541 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
9542 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
9543 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
9544 - try {
9545 - // Get bot ID from session or request
9546 - $bot_id = $this->get_current_bot_id($session_id);
9547 -
9548 - // Get system prompt instructions using centralized function
9549 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9550 - // Ensure conversation_history is an array
9551 - if (!is_array($conversation_history)) {
9552 - $conversation_history = array();
9553 - }
9554 -
9555 - // Clean and validate conversation history
9556 - foreach ($conversation_history as &$message) {
9557 - // Convert bot and agent roles to assistant
9558 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
9559 - $message['role'] = 'assistant';
9560 - }
9561 -
9562 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
9563 - if (!in_array($message['role'], ['assistant', 'user'])) {
9564 - $message['role'] = 'user';
9565 - }
9566 -
9567 - // Ensure content field exists
9568 - if (!isset($message['content']) || empty($message['content'])) {
9569 - $message['content'] = '';
9570 - }
9571 -
9572 - // Remove any unsupported fields
9573 - $message = array_intersect_key($message, array_flip(['role', 'content']));
9574 - }
9575 -
9576 - // Add relevant content as the latest user message
9577 - $conversation_history[] = [
9578 - 'role' => 'user',
9579 - 'content' => $relevant_content
9580 - ];
9581 -
9582 - // Prepare the request body with stream: true
9583 - $payload = [
9584 - 'model' => $selected_model,
9585 - 'messages' => $conversation_history,
9586 - 'max_tokens' => 1000,
9587 - 'temperature' => 0.8,
9588 - 'system' => $system_prompt_instructions,
9589 - 'stream' => true
9590 - ];
9591 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9592 - $body = json_encode($payload);
9593 -
9594 - // Check if we can actually stream (headers not sent, etc.)
9595 - if (headers_sent() || !function_exists('curl_init')) {
9596 - // Fallback to regular response with testing data
9597 - //error_log("MxChat: Streaming not possible, falling back to regular response");
9598 - $regular_response = $this->mxchat_generate_response_claude(
9599 - $selected_model,
9600 - $claude_api_key,
9601 - array_slice($conversation_history, 0, -1), // Remove the added content
9602 - $relevant_content,
9603 - $session_id
9604 - );
9605 -
9606 - // Save bot response to transcript
9607 - if (!empty($regular_response) && !empty($session_id)) {
9608 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9609 - }
9610 -
9611 - // Return as JSON with testing data
9612 - $response_data = [
9613 - 'text' => $regular_response,
9614 - 'html' => '',
9615 - 'session_id' => $session_id
9616 - ];
9617 -
9618 - if ($testing_data !== null) {
9619 - $response_data['testing_data'] = $testing_data;
9620 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
9621 - }
9622 -
9623 - // Clear any streaming headers and send JSON
9624 - if (headers_sent() === false) {
9625 - header('Content-Type: application/json');
9626 - }
9627 - echo json_encode($response_data);
9628 - return true; // Indicate we handled the response
9629 - }
9630 -
9631 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9632 -
9633 - $captured_status_code = 0;
9634 - $captured_body_pre_stream = '';
9635 - $full_response = '';
9636 - $stream_started = false;
9637 - $buffer = '';
9638 - $errno = 0;
9639 - $http_code = 0;
9640 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9641 - $backoff_ms = array(0, 750, 2000);
9642 -
9643 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9644 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9645 - usleep($backoff_ms[$attempt] * 1000);
9646 - }
9647 -
9648 - $captured_status_code = 0;
9649 - $captured_body_pre_stream = '';
9650 - $full_response = '';
9651 - $stream_started = false;
9652 - $buffer = '';
9653 -
9654 - $ch = curl_init();
9655 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
9656 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9657 - curl_setopt($ch, CURLOPT_POST, true);
9658 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9659 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9660 - 'Content-Type: application/json',
9661 - 'x-api-key: ' . $claude_api_key,
9662 - 'anthropic-version: 2023-06-01'
9663 - ));
9664 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9665 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9666 -
9667 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9668 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9669 - $captured_status_code = (int) $m[1];
9670 - }
9671 - return strlen($header);
9672 - });
9673 -
9674 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9675 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9676 - $captured_body_pre_stream .= $data;
9677 - return strlen($data);
9678 - }
9679 -
9680 - if (!$this->streaming_headers_sent) {
9681 - $this->setup_streaming_headers();
9682 - }
9683 -
9684 - if (!$stream_started && $testing_data !== null) {
9685 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9686 - flush();
9687 - $stream_started = true;
9688 - }
9689 -
9690 - $buffer .= $data;
9691 - $lines = explode("\n", $buffer);
9692 - $buffer = array_pop($lines);
9693 -
9694 - foreach ($lines as $line) {
9695 - if (trim($line) === '') {
9696 - continue;
9697 - }
9698 -
9699 - if (strpos($line, 'event: ') === 0) {
9700 - continue;
9701 - }
9702 -
9703 - if (strpos($line, 'data: ') === 0) {
9704 - $json_str = substr($line, 6);
9705 -
9706 - $json = json_decode(trim($json_str), true);
9707 - if (json_last_error() !== JSON_ERROR_NONE) {
9708 - continue;
9709 - }
9710 -
9711 - if (isset($json['type'])) {
9712 - switch ($json['type']) {
9713 - case 'content_block_delta':
9714 - if (isset($json['delta']['text'])) {
9715 - $content = $json['delta']['text'];
9716 - $full_response .= $content;
9717 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9718 - flush();
9719 - }
9720 - break;
9721 -
9722 - case 'message_stop':
9723 - echo "data: [DONE]\n\n";
9724 - flush();
9725 - break;
9726 -
9727 - case 'error':
9728 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
9729 - flush();
9730 - break;
9731 - }
9732 - }
9733 - }
9734 - }
9735 -
9736 - return strlen($data);
9737 - });
9738 -
9739 - $response = curl_exec($ch);
9740 - $errno = curl_errno($ch);
9741 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9742 - curl_close($ch);
9743 -
9744 - if (!$errno && $http_code === 200) {
9745 - break;
9746 - }
9747 -
9748 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
9749 - $can_retry = !$this->streaming_headers_sent
9750 - && ($attempt + 1) < $max_attempts
9751 - && $is_transient;
9752 -
9753 - if (defined('WP_DEBUG') && WP_DEBUG) {
9754 - error_log(sprintf(
9755 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9756 - $attempt + 1, $max_attempts, $http_code, $errno,
9757 - $is_transient ? 'yes' : 'no',
9758 - $can_retry ? 'Retrying.' : 'Giving up.'
9759 - ));
9760 - }
9761 -
9762 - if (!$can_retry) {
9763 - break;
9764 - }
9765 - }
9766 -
9767 - if ($errno || $http_code !== 200) {
9768 - return $this->mxchat_stream_emit_fallback(
9769 - 'anthropic',
9770 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content, $session_id),
9771 - $session_id,
9772 - $testing_data
9773 - );
9774 - }
9775 -
9776 - // Save the complete response to maintain chat persistence
9777 - if (!empty($full_response) && !empty($session_id)) {
9778 - // Prepare RAG context for streaming response
9779 - $rag_context_for_storage = null;
9780 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9781 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9782 -
9783 - if ($has_rag_data || $has_action_data) {
9784 - $rag_context_for_storage = [];
9785 -
9786 - if ($has_rag_data) {
9787 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9788 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9789 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9790 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9791 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9792 - }
9793 -
9794 - if ($has_action_data) {
9795 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9796 - }
9797 - }
9798 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9799 - }
9800 -
9801 - return true; // Indicate streaming completed successfully
9802 -
9803 - } catch (Exception $e) {
9804 - return $this->mxchat_stream_emit_fallback(
9805 - 'anthropic',
9806 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id),
9807 - $session_id,
9808 - $testing_data
9809 - );
9810 - }
9811 -}
9812 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9813 - try {
9814 - // Get bot ID from session or request
9815 - $bot_id = $this->get_current_bot_id($session_id);
9816 -
9817 - // Get system prompt instructions using centralized function
9818 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9819 -
9820 - // Ensure conversation_history is an array
9821 - if (!is_array($conversation_history)) {
9822 - $conversation_history = array();
9823 - }
9824 -
9825 - // Format conversation history for X.AI (same as OpenAI format)
9826 - $formatted_conversation = array();
9827 -
9828 - $formatted_conversation[] = array(
9829 - 'role' => 'system',
9830 - 'content' => $system_prompt_instructions . " " . $relevant_content
9831 - );
9832 -
9833 - foreach ($conversation_history as $message) {
9834 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9835 - $role = $message['role'];
9836 - if ($role === 'bot' || $role === 'agent') {
9837 - $role = 'assistant';
9838 - }
9839 3352 if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9840 3353 $role = 'user';
9841 3354 }
9842 - $formatted_conversation[] = array(
9843 - 'role' => $role,
9844 - 'content' => $message['content']
9845 - );
9846 - }
9847 - }
9848 3355
9849 - // Check if we can actually stream
9850 - if (headers_sent() || !function_exists('curl_init')) {
9851 - // Fallback to regular response with testing data
9852 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
9853 - $regular_response = $this->mxchat_generate_response_xai(
9854 - $selected_model,
9855 - $xai_api_key,
9856 - $conversation_history,
9857 - $relevant_content,
9858 - $session_id
9859 - );
9860 -
9861 - // Save bot response to transcript
9862 - if (!empty($regular_response) && !empty($session_id)) {
9863 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9864 - }
9865 -
9866 - $response_data = [
9867 - 'text' => $regular_response,
9868 - 'html' => '',
9869 - 'session_id' => $session_id
9870 - ];
9871 -
9872 - if ($testing_data !== null) {
9873 - $response_data['testing_data'] = $testing_data;
9874 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
9875 - }
9876 -
9877 - header('Content-Type: application/json');
9878 - echo json_encode($response_data);
9879 - return true;
9880 - }
9881 -
9882 - // Prepare the request body with stream: true
9883 - $body = json_encode([
9884 - 'model' => $selected_model,
9885 - 'messages' => $formatted_conversation,
9886 - 'temperature' => 0.8,
9887 - 'stream' => true
9888 - ]);
9889 -
9890 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9891 -
9892 - $captured_status_code = 0;
9893 - $captured_body_pre_stream = '';
9894 - $full_response = '';
9895 - $stream_started = false;
9896 - $buffer = '';
9897 - $errno = 0;
9898 - $http_code = 0;
9899 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9900 - $backoff_ms = array(0, 750, 2000);
9901 -
9902 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9903 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9904 - usleep($backoff_ms[$attempt] * 1000);
9905 - }
9906 -
9907 - $captured_status_code = 0;
9908 - $captured_body_pre_stream = '';
9909 - $full_response = '';
9910 - $stream_started = false;
9911 - $buffer = '';
9912 -
9913 - $ch = curl_init();
9914 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
9915 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9916 - curl_setopt($ch, CURLOPT_POST, true);
9917 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9918 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9919 - 'Content-Type: application/json',
9920 - 'Authorization: Bearer ' . $xai_api_key
9921 - ));
9922 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9923 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9924 -
9925 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9926 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9927 - $captured_status_code = (int) $m[1];
9928 - }
9929 - return strlen($header);
9930 - });
9931 -
9932 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9933 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9934 - $captured_body_pre_stream .= $data;
9935 - return strlen($data);
9936 - }
9937 -
9938 - if (!$this->streaming_headers_sent) {
9939 - $this->setup_streaming_headers();
9940 - }
9941 -
9942 - if (!$stream_started && $testing_data !== null) {
9943 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9944 - flush();
9945 - $stream_started = true;
9946 - }
9947 -
9948 - $buffer .= $data;
9949 - $lines = explode("\n", $buffer);
9950 - $buffer = array_pop($lines);
9951 -
9952 - foreach ($lines as $line) {
9953 - if (trim($line) === '') {
9954 - continue;
9955 - }
9956 - if (strpos($line, 'data: ') !== 0) {
9957 - continue;
9958 - }
9959 -
9960 - $json_str = substr($line, 6);
9961 -
9962 - if (trim($json_str) === '[DONE]') {
9963 - echo "data: [DONE]\n\n";
9964 - flush();
9965 - continue;
9966 - }
9967 -
9968 - $json = json_decode(trim($json_str), true);
9969 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9970 - $content = $json['choices'][0]['delta']['content'];
9971 - $full_response .= $content;
9972 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9973 - flush();
9974 - }
9975 - }
9976 -
9977 - return strlen($data);
9978 - });
9979 -
9980 - $response = curl_exec($ch);
9981 - $errno = curl_errno($ch);
9982 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9983 - curl_close($ch);
9984 -
9985 - if (!$errno && $http_code === 200) {
9986 - break;
9987 - }
9988 -
9989 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
9990 - $can_retry = !$this->streaming_headers_sent
9991 - && ($attempt + 1) < $max_attempts
9992 - && $is_transient;
9993 -
9994 - if (defined('WP_DEBUG') && WP_DEBUG) {
9995 - error_log(sprintf(
9996 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9997 - $attempt + 1, $max_attempts, $http_code, $errno,
9998 - $is_transient ? 'yes' : 'no',
9999 - $can_retry ? 'Retrying.' : 'Giving up.'
10000 - ));
10001 - }
10002 -
10003 - if (!$can_retry) {
10004 - break;
10005 - }
10006 - }
10007 -
10008 - if ($errno || $http_code !== 200) {
10009 - return $this->mxchat_stream_emit_fallback(
10010 - 'xai',
10011 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id),
10012 - $session_id,
10013 - $testing_data
10014 - );
10015 - }
10016 -
10017 - // Save the complete response to maintain chat persistence
10018 - if (!empty($full_response) && !empty($session_id)) {
10019 - // Prepare RAG context for streaming response
10020 - $rag_context_for_storage = null;
10021 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
10022 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
10023 -
10024 - if ($has_rag_data || $has_action_data) {
10025 - $rag_context_for_storage = [];
10026 -
10027 - if ($has_rag_data) {
10028 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
10029 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
10030 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
10031 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
10032 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
10033 - }
10034 -
10035 - if ($has_action_data) {
10036 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
10037 - }
10038 - }
10039 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
10040 - }
10041 -
10042 - return true; // Indicate streaming completed successfully
10043 -
10044 - } catch (Exception $e) {
10045 - return $this->mxchat_stream_emit_fallback(
10046 - 'xai',
10047 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
10048 - $session_id,
10049 - $testing_data
10050 - );
10051 - }
10052 -}
10053 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
10054 - try {
10055 - // Get bot ID from session or request
10056 - $bot_id = $this->get_current_bot_id($session_id);
10057 -
10058 - // Get system prompt instructions using centralized function
10059 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10060 -
10061 - // Ensure conversation_history is an array
10062 - if (!is_array($conversation_history)) {
10063 - $conversation_history = array();
10064 - }
10065 -
10066 - // Format conversation history for DeepSeek
10067 - $formatted_conversation = array();
10068 -
10069 - $formatted_conversation[] = array(
10070 - 'role' => 'system',
10071 - 'content' => $system_prompt_instructions . " " . $relevant_content
10072 - );
10073 -
10074 - foreach ($conversation_history as $message) {
10075 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10076 - $role = $message['role'];
10077 - if ($role === 'bot' || $role === 'agent') {
10078 - $role = 'assistant';
10079 - }
10080 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10081 - $role = 'user';
10082 - }
10083 3356 $formatted_conversation[] = array(
10084 3357 'role' => $role,
10085 3358 'content' => $message['content']
10086 3359 );
@@ -10086,261 +3359,20 @@
10086 3359 );
10087 3360 }
10088 3361 }
10089 3362
10090 - // Check if we can actually stream
10091 - if (headers_sent() || !function_exists('curl_init')) {
10092 - // Fallback to regular response with testing data
10093 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
10094 - $regular_response = $this->mxchat_generate_response_deepseek(
10095 - $selected_model,
10096 - $deepseek_api_key,
10097 - $conversation_history,
10098 - $relevant_content,
10099 - $session_id
10100 - );
10101 -
10102 - // Save bot response to transcript
10103 - if (!empty($regular_response) && !empty($session_id)) {
10104 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
10105 - }
10106 -
10107 - $response_data = [
10108 - 'text' => $regular_response,
10109 - 'html' => '',
10110 - 'session_id' => $session_id
10111 - ];
10112 -
10113 - if ($testing_data !== null) {
10114 - $response_data['testing_data'] = $testing_data;
10115 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
10116 - }
10117 -
10118 - header('Content-Type: application/json');
10119 - echo json_encode($response_data);
10120 - return true;
10121 - }
10122 -
10123 - // Prepare the request body with stream: true
10124 3363 $body = json_encode([
10125 3364 'model' => $selected_model,
10126 3365 'messages' => $formatted_conversation,
10127 3366 'temperature' => 0.8,
10128 - 'stream' => true
3367 + 'stream' => false
10129 3368 ]);
10130 3369
10131 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
10132 -
10133 - $captured_status_code = 0;
10134 - $captured_body_pre_stream = '';
10135 - $full_response = '';
10136 - $stream_started = false;
10137 - $buffer = '';
10138 - $errno = 0;
10139 - $http_code = 0;
10140 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
10141 - $backoff_ms = array(0, 750, 2000);
10142 -
10143 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
10144 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
10145 - usleep($backoff_ms[$attempt] * 1000);
10146 - }
10147 -
10148 - $captured_status_code = 0;
10149 - $captured_body_pre_stream = '';
10150 - $full_response = '';
10151 - $stream_started = false;
10152 - $buffer = '';
10153 -
10154 - $ch = curl_init();
10155 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
10156 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
10157 - curl_setopt($ch, CURLOPT_POST, true);
10158 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
10159 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
10160 - 'Content-Type: application/json',
10161 - 'Authorization: Bearer ' . $deepseek_api_key
10162 - ));
10163 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
10164 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
10165 -
10166 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
10167 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
10168 - $captured_status_code = (int) $m[1];
10169 - }
10170 - return strlen($header);
10171 - });
10172 -
10173 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
10174 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
10175 - $captured_body_pre_stream .= $data;
10176 - return strlen($data);
10177 - }
10178 -
10179 - if (!$this->streaming_headers_sent) {
10180 - $this->setup_streaming_headers();
10181 - }
10182 -
10183 - if (!$stream_started && $testing_data !== null) {
10184 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
10185 - flush();
10186 - $stream_started = true;
10187 - }
10188 -
10189 - $buffer .= $data;
10190 - $lines = explode("\n", $buffer);
10191 - $buffer = array_pop($lines);
10192 -
10193 - foreach ($lines as $line) {
10194 - if (trim($line) === '') {
10195 - continue;
10196 - }
10197 - if (strpos($line, 'data: ') !== 0) {
10198 - continue;
10199 - }
10200 -
10201 - $json_str = substr($line, 6);
10202 -
10203 - if (trim($json_str) === '[DONE]') {
10204 - echo "data: [DONE]\n\n";
10205 - flush();
10206 - continue;
10207 - }
10208 -
10209 - $json = json_decode(trim($json_str), true);
10210 - if ($json && isset($json['choices'][0]['delta']['content'])) {
10211 - $content = $json['choices'][0]['delta']['content'];
10212 - $full_response .= $content;
10213 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
10214 - flush();
10215 - }
10216 - }
10217 -
10218 - return strlen($data);
10219 - });
10220 -
10221 - $response = curl_exec($ch);
10222 - $errno = curl_errno($ch);
10223 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
10224 - curl_close($ch);
10225 -
10226 - if (!$errno && $http_code === 200) {
10227 - break;
10228 - }
10229 -
10230 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
10231 - $can_retry = !$this->streaming_headers_sent
10232 - && ($attempt + 1) < $max_attempts
10233 - && $is_transient;
10234 -
10235 - if (defined('WP_DEBUG') && WP_DEBUG) {
10236 - error_log(sprintf(
10237 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
10238 - $attempt + 1, $max_attempts, $http_code, $errno,
10239 - $is_transient ? 'yes' : 'no',
10240 - $can_retry ? 'Retrying.' : 'Giving up.'
10241 - ));
10242 - }
10243 -
10244 - if (!$can_retry) {
10245 - break;
10246 - }
10247 - }
10248 -
10249 - if ($errno || $http_code !== 200) {
10250 - return $this->mxchat_stream_emit_fallback(
10251 - 'openai',
10252 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id),
10253 - $session_id,
10254 - $testing_data
10255 - );
10256 - }
10257 -
10258 - // Save the complete response to maintain chat persistence
10259 - if (!empty($full_response) && !empty($session_id)) {
10260 - // Prepare RAG context for streaming response
10261 - $rag_context_for_storage = null;
10262 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
10263 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
10264 -
10265 - if ($has_rag_data || $has_action_data) {
10266 - $rag_context_for_storage = [];
10267 -
10268 - if ($has_rag_data) {
10269 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
10270 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
10271 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
10272 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
10273 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
10274 - }
10275 -
10276 - if ($has_action_data) {
10277 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
10278 - }
10279 - }
10280 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
10281 - }
10282 -
10283 - return true; // Indicate streaming completed successfully
10284 -
10285 - } catch (Exception $e) {
10286 - return $this->mxchat_stream_emit_fallback(
10287 - 'openai',
10288 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
10289 - $session_id,
10290 - $testing_data
10291 - );
10292 - }
10293 -}
10294 -
10295 -
10296 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id = '') {
10297 - try {
10298 - if (!is_array($conversation_history)) {
10299 - $conversation_history = array();
10300 - }
10301 -
10302 - $bot_id = $this->get_current_bot_id($session_id);
10303 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10304 -
10305 - $formatted_conversation = array();
10306 -
10307 - $formatted_conversation[] = array(
10308 - 'role' => 'system',
10309 - 'content' => $system_prompt_instructions . " " . $relevant_content
10310 - );
10311 -
10312 - foreach ($conversation_history as $message) {
10313 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10314 - $role = $message['role'];
10315 -
10316 - if ($role === 'bot' || $role === 'agent') {
10317 - $role = 'assistant';
10318 - }
10319 - if (!in_array($role, ['system', 'assistant', 'user'])) {
10320 - $role = 'user';
10321 - }
10322 -
10323 - $formatted_conversation[] = array(
10324 - 'role' => $role,
10325 - 'content' => $message['content']
10326 - );
10327 - }
10328 - }
10329 -
10330 - $body = json_encode([
10331 - 'model' => $selected_model,
10332 - 'messages' => $formatted_conversation,
10333 - 'temperature' => 1,
10334 - ]);
10335 -
10336 3370 $args = [
10337 3371 'body' => $body,
10338 3372 'headers' => [
10339 3373 'Content-Type' => 'application/json',
10340 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
10341 - 'HTTP-Referer' => home_url(),
10342 - 'X-Title' => get_bloginfo('name'),
3374 + 'Authorization' => 'Bearer ' . $deepseek_api_key,
10343 3375 ],
10344 3376 'timeout' => 60,
10345 3377 'redirection' => 5,
10346 3378 'blocking' => true,
@@ -10347,16 +3379,17 @@
10347 3379 'httpversion' => '1.0',
10348 3380 'sslverify' => true,
10349 3381 ];
10350 3382
10351 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
3383 + $response = wp_remote_post('https://api.deepseek.com/v1/chat/completions', $args);
10352 3384
10353 3385 if (is_wp_error($response)) {
10354 3386 $error_message = $response->get_error_message();
3387 + //error_log('DeepSeek API Error: ' . $error_message);
10355 3388 return [
10356 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenRouter'),
10357 - 'error_code' => 'openrouter_connection_error',
10358 - 'provider' => 'openrouter'
3389 + 'error' => esc_html__('Connection error when contacting DeepSeek: ', 'mxchat') . esc_html($error_message),
3390 + 'error_code' => 'deepseek_connection_error',
3391 + 'provider' => 'deepseek'
10359 3392 ];
10360 3393 }
10361 3394
10362 3395 $status_code = wp_remote_retrieve_response_code($response);
@@ -10362,17 +3395,69 @@
10362 3395 $status_code = wp_remote_retrieve_response_code($response);
10363 3396 if ($status_code !== 200) {
10364 3397 $response_body = wp_remote_retrieve_body($response);
10365 3398 $decoded_response = json_decode($response_body, true);
10366 -
3399 +
10367 3400 $error_message = isset($decoded_response['error']['message'])
10368 3401 ? $decoded_response['error']['message']
10369 3402 : 'HTTP Error ' . $status_code;
10370 -
3403 +
3404 + $error_type = isset($decoded_response['error']['type'])
3405 + ? $decoded_response['error']['type']
3406 + : 'unknown';
3407 +
3408 + //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
3409 +
3410 + // Handle specific error types
3411 + switch ($status_code) {
3412 + case 401:
3413 + return [
3414 + 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
3415 + 'error_code' => 'deepseek_auth_error',
3416 + 'provider' => 'deepseek'
3417 + ];
3418 +
3419 + case 400:
3420 + if (strpos($error_message, 'API key') !== false) {
3421 + return [
3422 + 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
3423 + 'error_code' => 'deepseek_invalid_api_key',
3424 + 'provider' => 'deepseek'
3425 + ];
3426 + }
3427 + break;
3428 +
3429 + case 429:
3430 + if (strpos($error_message, 'quota') !== false) {
3431 + return [
3432 + 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
3433 + 'error_code' => 'deepseek_quota_exceeded',
3434 + 'provider' => 'deepseek'
3435 + ];
3436 + } else {
3437 + return [
3438 + 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
3439 + 'error_code' => 'deepseek_rate_limit',
3440 + 'provider' => 'deepseek'
3441 + ];
3442 + }
3443 +
3444 + case 500:
3445 + case 502:
3446 + case 503:
3447 + case 504:
3448 + return [
3449 + 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
3450 + 'error_code' => 'deepseek_service_unavailable',
3451 + 'provider' => 'deepseek'
3452 + ];
3453 + }
3454 +
3455 + // Generic error fallback
10371 3456 return [
10372 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
10373 - 'error_code' => 'openrouter_api_error',
10374 - 'provider' => 'openrouter',
3457 + 'error' => esc_html__('DeepSeek API error: ', 'mxchat') . esc_html($error_message),
3458 + 'error_code' => 'deepseek_api_error',
3459 + 'provider' => 'deepseek',
10375 3460 'status_code' => $status_code
10376 3461 ];
10377 3462 }
10378 3463
@@ -10381,199 +3466,26 @@
10381 3466
10382 3467 if (isset($decoded_response['choices'][0]['message']['content'])) {
10383 3468 return trim($decoded_response['choices'][0]['message']['content']);
10384 3469 } else {
3470 + //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10385 3471 return [
10386 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
10387 - 'error_code' => 'openrouter_response_format_error',
10388 - 'provider' => 'openrouter'
3472 + 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
3473 + 'error_code' => 'deepseek_response_format_error',
3474 + 'provider' => 'deepseek'
10389 3475 ];
10390 3476 }
10391 3477 } catch (Exception $e) {
3478 + //error_log('DeepSeek Exception: ' . $e->getMessage());
10392 3479 return [
10393 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
10394 - 'error_code' => 'openrouter_exception',
10395 - 'provider' => 'openrouter'
3480 + 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
3481 + 'error_code' => 'deepseek_exception',
3482 + 'provider' => 'deepseek'
10396 3483 ];
10397 3484 }
10398 3485 }
10399 3486
10400 -/**
10401 - * Build a chat-bubble-safe message for a non-200 provider (chat) error.
10402 - *
10403 - * Visitors must NEVER see raw API internals (model names, key/billing/quota
10404 - * text). Admins (manage_options) get an actionable hint — and, for the common
10405 - * "model not available on this key" case, a direct pointer to change the model
10406 - * (the site owner can fix it in one click). Anthropic returns model-access as a
10407 - * 4xx with a message like "Claude Fable 5 is not available. Please use Opus 4.8."
10408 - *
10409 - * Provider-agnostic by design (reusable for the xai/gemini/deepseek branches),
10410 - * but Anthropic is the confirmed, reproduced case wired up here (plan 1d3b0f).
10411 - *
10412 - * @param int $http_code HTTP status from the provider.
10413 - * @param string $error_message Raw provider error.message (may be empty).
10414 - * @param string $provider_label Human provider name, e.g. 'Anthropic'.
10415 - * @return string Message safe to render as a chat bubble.
10416 - */
10417 -private function mxchat_friendly_chat_error($http_code, $error_message, $provider_label = '') {
10418 - $raw = trim((string) $error_message);
10419 -
10420 - // Detect a model-access / availability problem the site owner can fix by
10421 - // choosing a different model. (Anthropic phrasing + the common API shapes.)
10422 - $low = strtolower($raw);
10423 - $is_model_access = (strpos($low, 'not available') !== false)
10424 - || (strpos($low, 'does not have access') !== false)
10425 - || (strpos($low, 'do not have access') !== false)
10426 - || (strpos($low, 'does not exist') !== false) // OpenAI: "model `x` does not exist or you do not have access"
10427 - || (strpos($low, 'model_not_found') !== false)
10428 - || (strpos($low, 'not_found_error') !== false)
10429 - || (strpos($low, 'model not found') !== false) // xAI
10430 - || (strpos($low, 'not found') !== false) // Gemini: "models/x is not found for API version ..."
10431 - || (strpos($low, 'permission_denied') !== false) // Gemini gated model
10432 - || (strpos($low, 'permission denied') !== false);
10433 -
10434 - if (current_user_can('manage_options')) {
10435 - if ($is_model_access) {
10436 - return $raw !== ''
10437 - ? sprintf(
10438 - /* translators: %s: raw provider error detail */
10439 - esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings. (Details: %s)', 'mxchat'),
10440 - $raw
10441 - )
10442 - : esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings.', 'mxchat');
10443 - }
10444 - return $raw !== ''
10445 - ? sprintf(
10446 - /* translators: 1: provider label, 2: raw provider error detail */
10447 - esc_html__('The AI provider (%1$s) returned an error: %2$s. Check your model and API key in MxChat → Settings.', 'mxchat'),
10448 - $provider_label !== '' ? $provider_label : esc_html__('AI', 'mxchat'),
10449 - $raw
10450 - )
10451 - : esc_html__('The AI provider returned an error. Check your model and API key in MxChat → Settings.', 'mxchat');
10452 - }
10453 -
10454 - // Visitors: friendly, generic, no internals leaked.
10455 - return esc_html__('Sorry, I\'m having trouble responding right now. Please try again in a moment.', 'mxchat');
10456 -}
10457 -
10458 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id = '') {
10459 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
10460 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
10461 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
10462 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
10463 -
10464 - // Get bot ID from session or request
10465 - $bot_id = $this->get_current_bot_id($session_id);
10466 -
10467 - // Get system prompt instructions using centralized function
10468 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10469 -
10470 - // Clean and validate conversation history
10471 - foreach ($conversation_history as &$message) {
10472 - // Convert bot and agent roles to assistant
10473 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
10474 - $message['role'] = 'assistant';
10475 - }
10476 -
10477 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
10478 - if (!in_array($message['role'], ['assistant', 'user'])) {
10479 - $message['role'] = 'user';
10480 - }
10481 -
10482 - // Ensure content field exists
10483 - if (!isset($message['content']) || empty($message['content'])) {
10484 - $message['content'] = '';
10485 - }
10486 -
10487 - // Remove any unsupported fields
10488 - $message = array_intersect_key($message, array_flip(['role', 'content']));
10489 - }
10490 -
10491 - // Add relevant content as the latest user message
10492 - $conversation_history[] = [
10493 - 'role' => 'user',
10494 - 'content' => $relevant_content
10495 - ];
10496 -
10497 - // Build request body
10498 - $payload = [
10499 - 'model' => $selected_model,
10500 - 'max_tokens' => 1000,
10501 - 'temperature' => 0.8,
10502 - 'messages' => $conversation_history,
10503 - 'system' => $system_prompt_instructions
10504 - ];
10505 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
10506 - $body = json_encode($payload);
10507 -
10508 - // Set up API request
10509 - $args = [
10510 - 'body' => $body,
10511 - 'headers' => [
10512 - 'Content-Type' => 'application/json',
10513 - 'x-api-key' => $claude_api_key,
10514 - 'anthropic-version' => '2023-06-01'
10515 - ],
10516 - 'timeout' => 60,
10517 - 'redirection' => 5,
10518 - 'blocking' => true,
10519 - 'httpversion' => '1.0',
10520 - 'sslverify' => true,
10521 - ];
10522 -
10523 - // Make API request
10524 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
10525 -
10526 - // Check for WordPress errors
10527 - if (is_wp_error($response)) {
10528 - //error_log("Claude API request error: " . $response->get_error_message());
10529 - return "Sorry, there was an error connecting to the API.";
10530 - }
10531 -
10532 - // Check HTTP response code
10533 - $http_code = wp_remote_retrieve_response_code($response);
10534 - if ($http_code !== 200) {
10535 - $error_body = wp_remote_retrieve_body($response);
10536 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
10537 -
10538 - // Try to extract error message from response
10539 - $error_data = json_decode($error_body, true);
10540 - $error_message = isset($error_data['error']['message']) ?
10541 - $error_data['error']['message'] :
10542 - "HTTP error " . $http_code;
10543 -
10544 - // Surface an admin-actionable message (and a model-change pointer for the
10545 - // model-access case) without leaking raw API internals to visitors. This
10546 - // is the single chokepoint for BOTH the non-streaming and streaming Claude
10547 - // paths (the stream's non-200 fallback re-enters this method). plan 1d3b0f.
10548 - return $this->mxchat_friendly_chat_error($http_code, $error_message, 'Anthropic');
10549 - }
10550 -
10551 - // Parse response
10552 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
10553 -
10554 - // Check for JSON decode errors
10555 - if (json_last_error() !== JSON_ERROR_NONE) {
10556 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
10557 - return "Sorry, there was an error processing the API response.";
10558 - }
10559 -
10560 - // Extract and validate response content. claude-fable-5 prepends a
10561 - // thinking block to content even with no thinking param — take the first
10562 - // TEXT block rather than content[0].
10563 - if (isset($response_body['content']) && is_array($response_body['content'])) {
10564 - foreach ($response_body['content'] as $block) {
10565 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
10566 - return trim($block['text']);
10567 - }
10568 - }
10569 - }
10570 -
10571 - // Log unexpected response format
10572 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
10573 - return "Sorry, I received an unexpected response format from the API.";
10574 -}
10575 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id = '') {
3487 +private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content) {
10576 3488 try {
10577 3489 // Ensure conversation_history is an array
10578 3490 if (!is_array($conversation_history)) {
10579 3491 $conversation_history = array();
@@ -10578,16 +3490,11 @@
10578 3490 if (!is_array($conversation_history)) {
10579 3491 $conversation_history = array();
10580 3492 }
10581 3493
10582 - // Get bot ID from session or request. plan eb9c38: resolve the real bot
10583 - // from the session (was hardcoded '' → always default bot on multi-bot
10584 - // installs) and fix the undefined $session_id that fed get_system_instructions.
10585 - $bot_id = $this->get_current_bot_id($session_id);
3494 + // Get system prompt instructions from options
3495 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10586 3496
10587 - // Get system prompt instructions using centralized function
10588 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10589 -
10590 3497 // Create a new array for the formatted conversation
10591 3498 $formatted_conversation = array();
10592 3499
10593 3500 // Add system message first
@@ -10615,25 +3522,15 @@
10615 3522 );
10616 3523 }
10617 3524 }
10618 3525
10619 - // Build request body with optimal settings for fast responses
10620 - $request_body = [
3526 + $body = json_encode([
10621 3527 'model' => $selected_model,
10622 3528 'messages' => $formatted_conversation,
10623 - 'temperature' => 1,
3529 + 'temperature' => 0.8,
10624 3530 'stream' => false
10625 - ];
3531 + ]);
10626 3532
10627 - // reasoning_effort — sourced from the core model catalog (plan-dcb71c);
10628 - // frozen inline ladder lives in mxchat_reasoning_effort_fallback().
10629 - $effort = $this->mxchat_reasoning_effort_for($selected_model, 'chat');
10630 - if ($effort !== null) {
10631 - $request_body['reasoning_effort'] = $effort;
10632 - }
10633 -
10634 - $body = json_encode($request_body);
10635 -
10636 3533 $args = [
10637 3534 'body' => $body,
10638 3535 'headers' => [
10639 3536 'Content-Type' => 'application/json',
@@ -10645,14 +3542,15 @@
10645 3542 'httpversion' => '1.0',
10646 3543 'sslverify' => true,
10647 3544 ];
10648 3545
10649 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
3546 + $response = wp_remote_post('https://api.openai.com/v1/chat/completions', $args);
10650 3547
10651 3548 if (is_wp_error($response)) {
10652 3549 $error_message = $response->get_error_message();
3550 + //error_log('OpenAI API Error: ' . $error_message);
10653 3551 return [
10654 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenAI'),
3552 + 'error' => esc_html__('Connection error when contacting OpenAI: ', 'mxchat') . esc_html($error_message),
10655 3553 'error_code' => 'openai_connection_error',
10656 3554 'provider' => 'openai'
10657 3555 ];
10658 3556 }
@@ -10669,8 +3567,10 @@
10669 3567 $error_type = isset($decoded_response['error']['type'])
10670 3568 ? $decoded_response['error']['type']
10671 3569 : 'unknown';
10672 3570
3571 + //error_log('OpenAI API HTTP Error: ' . $status_code . ' - ' . $error_message);
3572 +
10673 3573 // Handle specific error types
10674 3574 switch ($error_type) {
10675 3575 case 'invalid_request_error':
10676 3576 if (strpos($error_message, 'API key') !== false) {
@@ -10703,13 +3603,11 @@
10703 3603 'provider' => 'openai'
10704 3604 ];
10705 3605 }
10706 3606
10707 - // Generic error fallback only — the typed cases above already produce
10708 - // clean messages. Route the raw-tail generic case through the leak-safe
10709 - // helper so visitors never see provider internals. plan 5da59a.
3607 + // Generic error fallback
10710 3608 return [
10711 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'OpenAI'),
3609 + 'error' => esc_html__('OpenAI API error: ', 'mxchat') . esc_html($error_message),
10712 3610 'error_code' => 'openai_api_error',
10713 3611 'provider' => 'openai',
10714 3612 'status_code' => $status_code
10715 3613 ];
@@ -10720,8 +3618,9 @@
10720 3618
10721 3619 if (isset($decoded_response['choices'][0]['message']['content'])) {
10722 3620 return trim($decoded_response['choices'][0]['message']['content']);
10723 3621 } else {
3622 + //error_log('OpenAI API Response Format Error: ' . print_r($decoded_response, true));
10724 3623 return [
10725 3624 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
10726 3625 'error_code' => 'openai_response_format_error',
10727 3626 'provider' => 'openai'
@@ -10727,8 +3626,9 @@
10727 3626 'provider' => 'openai'
10728 3627 ];
10729 3628 }
10730 3629 } catch (Exception $e) {
3630 + //error_log('OpenAI Exception: ' . $e->getMessage());
10731 3631 return [
10732 3632 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
10733 3633 'error_code' => 'openai_exception',
10734 3634 'provider' => 'openai'
@@ -10734,17 +3634,13 @@
10734 3634 'provider' => 'openai'
10735 3635 ];
10736 3636 }
10737 3637 }
3638 +private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content) {
3639 + try {
3640 + // Get system prompt instructions from options
3641 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10738 3642
10739 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id = '') {
10740 - try {
10741 - // Get bot ID from session or request
10742 - $bot_id = $this->get_current_bot_id($session_id);
10743 -
10744 - // Get system prompt instructions using centralized function
10745 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10746 -
10747 3643 // Add system prompt to relevant content
10748 3644 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10749 3645
10750 3646 // Prepend system instructions to the conversation history
@@ -10793,9 +3689,9 @@
10793 3689 'sslverify' => true,
10794 3690 ];
10795 3691
10796 3692 // Make the API request
10797 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
3693 + $response = wp_remote_post('https://api.x.ai/v1/chat/completions', $args);
10798 3694
10799 3695 // Process the response
10800 3696 if (is_wp_error($response)) {
10801 3697 $error_message = $response->get_error_message();
@@ -10800,9 +3696,9 @@
10800 3696 if (is_wp_error($response)) {
10801 3697 $error_message = $response->get_error_message();
10802 3698 //error_log('X.AI API Error: ' . $error_message);
10803 3699 return [
10804 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'X.AI'),
3700 + 'error' => esc_html__('Connection error when contacting X.AI: ', 'mxchat') . esc_html($error_message),
10805 3701 'error_code' => 'xai_connection_error',
10806 3702 'provider' => 'xai'
10807 3703 ];
10808 3704 }
@@ -10896,14 +3792,11 @@
10896 3792 'provider' => 'xai'
10897 3793 ];
10898 3794 }
10899 3795
10900 - // Generic error fallback. Route the user-facing text through the
10901 - // leak-safe helper (admins get an actionable hint, visitors a generic
10902 - // fallback) instead of echoing raw provider internals. Preserve the
10903 - // structured contract (error_code/provider/status_code) for logging. plan 5da59a.
3796 + // Generic error fallback with the actual error message
10904 3797 return [
10905 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'xAI'),
3798 + 'error' => esc_html__('X.AI API error: ', 'mxchat') . esc_html($error_message),
10906 3799 'error_code' => 'xai_api_error',
10907 3800 'provider' => 'xai',
10908 3801 'status_code' => $status_code
10909 3802 ];
@@ -10929,188 +3822,113 @@
10929 3822 'error_code' => 'xai_exception',
10930 3823 'provider' => 'xai'
10931 3824 ];
10932 3825 }
3826 +}
3827 +private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content) {
3828 + // Get system prompt instructions from options
3829 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
10933 3830
10934 -
10935 -}
10936 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id = '') {
10937 - try {
10938 - // Ensure conversation_history is an array
10939 - if (!is_array($conversation_history)) {
10940 - $conversation_history = array();
3831 + // Clean and validate conversation history
3832 + foreach ($conversation_history as &$message) {
3833 + // Convert bot and agent roles to assistant
3834 + if ($message['role'] === 'bot' || $message['role'] === 'agent') {
3835 + $message['role'] = 'assistant';
10941 3836 }
10942 -
10943 - // Get bot ID from session or request
10944 - $bot_id = $this->get_current_bot_id($session_id);
10945 3837
10946 - // Get system prompt instructions using centralized function
10947 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10948 -
10949 - // Create a new array for the formatted conversation
10950 - $formatted_conversation = array();
3838 + // Remove unsupported roles - Claude only supports 'assistant' and 'user'
3839 + if (!in_array($message['role'], ['assistant', 'user'])) {
3840 + $message['role'] = 'user';
3841 + }
10951 3842
10952 - // Add system message first
10953 - $formatted_conversation[] = array(
10954 - 'role' => 'system',
10955 - 'content' => $system_prompt_instructions . " " . $relevant_content
10956 - );
3843 + // Ensure content field exists
3844 + if (!isset($message['content']) || empty($message['content'])) {
3845 + $message['content'] = '';
3846 + }
10957 3847
10958 - // Add the rest of the conversation history
10959 - foreach ($conversation_history as $message) {
10960 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10961 - $role = $message['role'];
3848 + // Remove any unsupported fields
3849 + $message = array_intersect_key($message, array_flip(['role', 'content']));
3850 + }
10962 3851
10963 - // Convert roles to supported format
10964 - if ($role === 'bot' || $role === 'agent') {
10965 - $role = 'assistant';
10966 - }
10967 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10968 - $role = 'user';
10969 - }
3852 + // Add relevant content as the latest user message
3853 + $conversation_history[] = [
3854 + 'role' => 'user',
3855 + 'content' => $relevant_content
3856 + ];
10970 3857
10971 - $formatted_conversation[] = array(
10972 - 'role' => $role,
10973 - 'content' => $message['content']
10974 - );
10975 - }
10976 - }
3858 + // Build request body
3859 + $body = json_encode([
3860 + 'model' => $selected_model,
3861 + 'max_tokens' => 1000,
3862 + 'temperature' => 0.8,
3863 + 'messages' => $conversation_history,
3864 + 'system' => $system_prompt_instructions
3865 + ]);
10977 3866
10978 - $body = json_encode([
10979 - 'model' => $selected_model,
10980 - 'messages' => $formatted_conversation,
10981 - 'temperature' => 0.8,
10982 - 'stream' => false
10983 - ]);
10984 -
10985 - $args = [
10986 - 'body' => $body,
10987 - 'headers' => [
3867 + // Set up API request
3868 + $args = [
3869 + 'body' => $body,
3870 + 'headers' => [
10988 3871 'Content-Type' => 'application/json',
10989 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
3872 + 'x-api-key' => $claude_api_key,
3873 + 'anthropic-version' => '2023-06-01'
10990 3874 ],
10991 - 'timeout' => 60,
10992 - 'redirection' => 5,
10993 - 'blocking' => true,
10994 - 'httpversion' => '1.0',
10995 - 'sslverify' => true,
10996 - ];
3875 + 'timeout' => 60,
3876 + 'redirection' => 5,
3877 + 'blocking' => true,
3878 + 'httpversion' => '1.0',
3879 + 'sslverify' => true,
3880 + ];
10997 3881
10998 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
3882 + // Make API request
3883 + $response = wp_remote_post('https://api.anthropic.com/v1/messages', $args);
10999 3884
11000 - if (is_wp_error($response)) {
11001 - $error_message = $response->get_error_message();
11002 - //error_log('DeepSeek API Error: ' . $error_message);
11003 - return [
11004 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'DeepSeek'),
11005 - 'error_code' => 'deepseek_connection_error',
11006 - 'provider' => 'deepseek'
11007 - ];
11008 - }
3885 + // Check for WordPress errors
3886 + if (is_wp_error($response)) {
3887 + //error_log("Claude API request error: " . $response->get_error_message());
3888 + return "Sorry, there was an error connecting to the API.";
3889 + }
11009 3890
11010 - $status_code = wp_remote_retrieve_response_code($response);
11011 - if ($status_code !== 200) {
11012 - $response_body = wp_remote_retrieve_body($response);
11013 - $decoded_response = json_decode($response_body, true);
3891 + // Check HTTP response code
3892 + $http_code = wp_remote_retrieve_response_code($response);
3893 + if ($http_code !== 200) {
3894 + $error_body = wp_remote_retrieve_body($response);
3895 + //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
3896 +
3897 + // Try to extract error message from response
3898 + $error_data = json_decode($error_body, true);
3899 + $error_message = isset($error_data['error']['message']) ?
3900 + $error_data['error']['message'] :
3901 + "HTTP error " . $http_code;
3902 +
3903 + return "Sorry, the API returned an error: " . $error_message;
3904 + }
11014 3905
11015 - $error_message = isset($decoded_response['error']['message'])
11016 - ? $decoded_response['error']['message']
11017 - : 'HTTP Error ' . $status_code;
11018 -
11019 - $error_type = isset($decoded_response['error']['type'])
11020 - ? $decoded_response['error']['type']
11021 - : 'unknown';
11022 -
11023 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
3906 + // Parse response
3907 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
3908 +
3909 + // Check for JSON decode errors
3910 + if (json_last_error() !== JSON_ERROR_NONE) {
3911 + //error_log("Claude API JSON decode error: " . json_last_error_msg());
3912 + return "Sorry, there was an error processing the API response.";
3913 + }
11024 3914
11025 - // Handle specific error types
11026 - switch ($status_code) {
11027 - case 401:
11028 - return [
11029 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
11030 - 'error_code' => 'deepseek_auth_error',
11031 - 'provider' => 'deepseek'
11032 - ];
11033 -
11034 - case 400:
11035 - if (strpos($error_message, 'API key') !== false) {
11036 - return [
11037 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
11038 - 'error_code' => 'deepseek_invalid_api_key',
11039 - 'provider' => 'deepseek'
11040 - ];
11041 - }
11042 - break;
11043 -
11044 - case 429:
11045 - if (strpos($error_message, 'quota') !== false) {
11046 - return [
11047 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
11048 - 'error_code' => 'deepseek_quota_exceeded',
11049 - 'provider' => 'deepseek'
11050 - ];
11051 - } else {
11052 - return [
11053 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
11054 - 'error_code' => 'deepseek_rate_limit',
11055 - 'provider' => 'deepseek'
11056 - ];
11057 - }
11058 -
11059 - case 500:
11060 - case 502:
11061 - case 503:
11062 - case 504:
11063 - return [
11064 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
11065 - 'error_code' => 'deepseek_service_unavailable',
11066 - 'provider' => 'deepseek'
11067 - ];
11068 - }
3915 + // Extract and validate response content
3916 + if (isset($response_body['content']) &&
3917 + is_array($response_body['content']) &&
3918 + !empty($response_body['content']) &&
3919 + isset($response_body['content'][0]['text'])) {
3920 + return trim($response_body['content'][0]['text']);
3921 + }
11069 3922
11070 - // Generic error fallback — leak-safe helper (see plan 5da59a / 1d3b0f).
11071 - return [
11072 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'DeepSeek'),
11073 - 'error_code' => 'deepseek_api_error',
11074 - 'provider' => 'deepseek',
11075 - 'status_code' => $status_code
11076 - ];
11077 - }
11078 -
11079 - $response_body = wp_remote_retrieve_body($response);
11080 - $decoded_response = json_decode($response_body, true);
11081 -
11082 - if (isset($decoded_response['choices'][0]['message']['content'])) {
11083 - return trim($decoded_response['choices'][0]['message']['content']);
11084 - } else {
11085 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
11086 - return [
11087 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
11088 - 'error_code' => 'deepseek_response_format_error',
11089 - 'provider' => 'deepseek'
11090 - ];
11091 - }
11092 - } catch (Exception $e) {
11093 - //error_log('DeepSeek Exception: ' . $e->getMessage());
11094 - return [
11095 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
11096 - 'error_code' => 'deepseek_exception',
11097 - 'provider' => 'deepseek'
11098 - ];
11099 - }
3923 + // Log unexpected response format
3924 + //error_log("Claude API unexpected response format: " . print_r($response_body, true));
3925 + return "Sorry, I received an unexpected response format from the API.";
11100 3926 }
11101 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content, $session_id = '') {
11102 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
11103 - // Auto-rescue existing installs whose saved model is the dead ID.
11104 - if ($selected_model === 'gemini-3-pro-preview') {
11105 - $selected_model = 'gemini-3.1-pro-preview';
11106 - }
11107 - // Get bot ID from session or request
11108 - $bot_id = $this->get_current_bot_id($session_id);
11109 -
11110 - // Get system prompt instructions using centralized function
11111 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
11112 -
3927 +private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content) {
3928 + // Get system prompt instructions from options
3929 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
3930 +
11113 3931 // Add system prompt to relevant content
11114 3932 $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
11115 3933
11116 3934 // Format messages for Gemini API
@@ -11175,24 +3993,10 @@
11175 3993 'parts' => $current_parts
11176 3994 ];
11177 3995 }
11178 3996
11179 - // Built-in Web Search grounding for Gemini (plan 46b9ea).
11180 - // The enable_web_search toggle historically routed ONLY to OpenAI's web_search
11181 - // tool; for a Gemini chat model it was a silent no-op. Gemini grounds natively
11182 - // (and free) via the Google Search tool, so when the toggle is on we attach it
11183 - // here on the PLAIN dispatch path. The function-calling loop (mxchat_fc_loop_gemini)
11184 - // is a SEPARATE path reached only when AI Tools are active, so grounding here
11185 - // never double-fires with function calling.
11186 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
11187 - // Gemini ids that do NOT support Google Search grounding (none today — every
11188 - // shipped chat model is 2.x/3.x and grounds natively). Kept as the explicit
11189 - // opt-out list mirroring the OpenAI $unsupported_web_search_models pattern.
11190 - $gemini_unsupported_grounding = array();
11191 - $grounding_active = $web_search_enabled && !in_array($selected_model, $gemini_unsupported_grounding, true);
11192 -
11193 3997 // Build the request body
11194 - $request_payload = [
3998 + $body = json_encode([
11195 3999 'contents' => $formatted_messages,
11196 4000 'generationConfig' => [
11197 4001 'temperature' => 0.7,
11198 4002 'topP' => 0.95,
@@ -11216,30 +4020,12 @@
11216 4020 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
11217 4021 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
11218 4022 ]
11219 4023 ]
11220 - ];
11221 -
11222 - if ($grounding_active) {
11223 - // Gemini 1.5 used the older google_search_retrieval shape; 2.0+ uses the
11224 - // bare google_search tool. Branch by model family so a future 1.5 id still
11225 - // grounds (no 1.5 ships today, so this resolves to google_search). The empty
11226 - // tool config must serialize as a JSON object {}, not an array [].
11227 - if (strpos($selected_model, 'gemini-1.5') !== false) {
11228 - $request_payload['tools'] = [ ['google_search_retrieval' => new \stdClass()] ];
11229 - } else {
11230 - $request_payload['tools'] = [ ['google_search' => new \stdClass()] ];
11231 - }
11232 - }
11233 -
11234 - $body = json_encode($request_payload);
11235 -
4024 + ]);
4025 +
11236 4026 // Prepare the API endpoint
11237 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models.
11238 - // Grounding (the google_search tool) is a v1beta feature, so force v1beta whenever
11239 - // it's active — otherwise a stable model on v1 would silently drop the tool.
11240 - $api_version = ($grounding_active || strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
11241 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
4027 + $api_endpoint = 'https://generativelanguage.googleapis.com/v1/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
11242 4028
11243 4029 // Set up the API request
11244 4030 $args = [
11245 4031 'body' => $body,
@@ -11253,31 +4039,22 @@
11253 4039 'sslverify' => true,
11254 4040 ];
11255 4041
11256 4042 // Make the API request
11257 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
11258 -
4043 + $response = wp_remote_post($api_endpoint, $args);
4044 +
11259 4045 // Process the response
11260 4046 if (is_wp_error($response)) {
11261 - // plan b13282: route the transport-error string through the leak-safe helper
11262 - // (admin-actionable, generic for visitors) instead of echoing the raw WP HTTP
11263 - // error. http_code 0 = no HTTP response, so the helper uses the generic branch.
11264 - return $this->mxchat_friendly_chat_error(0, $response->get_error_message(), 'Gemini');
4047 + return "Sorry, there was an error processing your request: " . $response->get_error_message();
11265 4048 }
11266 4049
11267 4050 $response_body = json_decode(wp_remote_retrieve_body($response), true);
11268 4051
11269 - // Handle potential errors in the response. Gemini surfaces errors as a
11270 - // 200/non-200 body with an `error` envelope; route the user-facing text
11271 - // through the leak-safe helper (admin-actionable, no visitor leak) rather
11272 - // than echoing the raw provider message. plan 5da59a.
4052 + // Handle potential errors in the response
11273 4053 if (isset($response_body['error'])) {
11274 4054 //error_log('Gemini API Error: ' . json_encode($response_body['error']));
11275 - $gemini_error_message = isset($response_body['error']['message'])
11276 - ? $response_body['error']['message']
11277 - : 'Unknown error';
11278 - $gemini_http_code = wp_remote_retrieve_response_code($response);
11279 - return $this->mxchat_friendly_chat_error($gemini_http_code, $gemini_error_message, 'Gemini');
4055 + return "Sorry, there was an error with the Gemini API: " .
4056 + (isset($response_body['error']['message']) ? $response_body['error']['message'] : 'Unknown error');
11280 4057 }
11281 4058
11282 4059 // Extract the response text
11283 4060 if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
@@ -11288,139 +4065,9 @@
11288 4065 }
11289 4066 }
11290 4067
11291 4068
11292 -public function test_streaming_request() {
11293 - $options = get_option('mxchat_options', []);
11294 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
11295 4069
11296 - // Detect provider from model prefix
11297 - $provider = strtolower(explode('-', $model)[0]);
11298 -
11299 - $sample_prompt = 'Hello! Can you stream this response back to me?';
11300 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
11301 - $headers = [];
11302 - $body = [];
11303 - $url = '';
11304 - $api_key = '';
11305 -
11306 - switch ($provider) {
11307 - case 'gpt':
11308 - case 'o1':
11309 - $api_key = $options['api_key'] ?? '';
11310 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
11311 - $url = 'https://api.openai.com/v1/chat/completions';
11312 - $headers = [
11313 - 'Content-Type: application/json',
11314 - 'Authorization: Bearer ' . $api_key
11315 - ];
11316 - $body = [
11317 - 'model' => $model,
11318 - 'messages' => $messages,
11319 - 'stream' => true
11320 - ];
11321 - break;
11322 -
11323 - case 'claude':
11324 - $api_key = $options['claude_api_key'] ?? '';
11325 - if (empty($api_key)) return '❌ Missing API key for Claude';
11326 - $url = 'https://api.anthropic.com/v1/messages';
11327 - $headers = [
11328 - 'Content-Type: application/json',
11329 - 'x-api-key: ' . $api_key,
11330 - 'anthropic-version: 2023-06-01'
11331 - ];
11332 - $body = [
11333 - 'model' => $model,
11334 - 'messages' => $messages,
11335 - 'max_tokens' => 100,
11336 - 'stream' => true
11337 - ];
11338 - break;
11339 -
11340 - case 'grok':
11341 - $api_key = $options['xai_api_key'] ?? '';
11342 - if (empty($api_key)) return '❌ Missing API key for X.AI';
11343 - $url = 'https://api.x.ai/v1/chat/completions';
11344 - $headers = [
11345 - 'Content-Type: application/json',
11346 - 'Authorization: Bearer ' . $api_key
11347 - ];
11348 - $body = [
11349 - 'model' => $model,
11350 - 'messages' => $messages,
11351 - 'stream' => true
11352 - ];
11353 - break;
11354 -
11355 - case 'deepseek':
11356 - if (empty($deepseek_api_key)) {
11357 - $error_response = [
11358 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
11359 - 'error_code' => 'missing_deepseek_api_key'
11360 - ];
11361 - if ($testing_data !== null) {
11362 - $error_response['testing_data'] = $testing_data;
11363 - }
11364 - return $error_response;
11365 - }
11366 - if ($streaming) {
11367 - return $this->mxchat_generate_response_deepseek_stream(
11368 - $selected_model,
11369 - $deepseek_api_key,
11370 - $conversation_history,
11371 - $relevant_content,
11372 - $session_id,
11373 - $testing_data // Pass testing data
11374 - );
11375 - } else {
11376 - $response = $this->mxchat_generate_response_deepseek(
11377 - $selected_model,
11378 - $deepseek_api_key,
11379 - $conversation_history,
11380 - $relevant_content,
11381 - $session_id
11382 - );
11383 - }
11384 - break;
11385 -
11386 - case 'gemini':
11387 - $api_key = $options['gemini_api_key'] ?? '';
11388 - if (empty($api_key)) return '❌ Missing API key for Gemini';
11389 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
11390 - $headers = ['Content-Type: application/json'];
11391 - $body = [
11392 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
11393 - 'generationConfig' => ['temperature' => 0.7]
11394 - ];
11395 - break;
11396 -
11397 - default:
11398 - return '❌ Unsupported provider: ' . $provider;
11399 - }
11400 -
11401 - // Do the actual streaming test
11402 - $ch = curl_init($url);
11403 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
11404 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
11405 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
11406 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
11407 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
11408 -
11409 - $response = curl_exec($ch);
11410 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
11411 - $error = curl_error($ch);
11412 - curl_close($ch);
11413 -
11414 - if ($error) return "❌ cURL error: $error";
11415 - if ($http_code !== 200) {
11416 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
11417 - return "❌ HTTP $http_code: $error_message";
11418 - }
11419 -
11420 - return true;
11421 -}
11422 -
11423 4070 public function mxchat_dismiss_pre_chat_message() {
11424 4071 // Get and sanitize the user identifier
11425 4072 $user_id = $this->mxchat_get_user_identifier();
11426 4073 $user_id = sanitize_key($user_id);
@@ -11474,63 +4121,40 @@
11474 4121
11475 4122 return $dotProduct / ($normA * $normB);
11476 4123 }
11477 4124
4125 +public function mxchat_enqueue_scripts_styles() {
4126 + // Define version numbers for the styles and scripts
4127 + $chat_style_version = '2.1.7';
4128 + $chat_script_version = '2.1.7';
11478 4129
11479 -public function mxchat_enqueue_scripts_styles() {
11480 - // Fetch options from the database first to check loading strategy
11481 - $this->options = get_option('mxchat_options');
11482 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
4130 + // Enqueue the script
4131 + wp_enqueue_script(
4132 + 'mxchat-chat-js',
4133 + plugin_dir_url(__FILE__) . '../js/chat-script.js',
4134 + array('jquery'),
4135 + $chat_script_version,
4136 + true
4137 + );
11483 4138
11484 - // Always enqueue CSS immediately
4139 + // Enqueue the CSS
11485 4140 wp_enqueue_style(
11486 4141 'mxchat-chat-css',
11487 4142 plugin_dir_url(__FILE__) . '../css/chat-style.css',
11488 4143 array(),
11489 - MXCHAT_VERSION
4144 + $chat_style_version
11490 4145 );
11491 4146
11492 - // Handle script loading based on strategy
11493 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11494 - // Enqueue the script normally
11495 - wp_enqueue_script(
11496 - 'mxchat-chat-js',
11497 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
11498 - array('jquery'),
11499 - MXCHAT_VERSION,
11500 - true
11501 - );
11502 -
11503 - // Add defer attribute if strategy is 'defer'
11504 - if ($loading_strategy === 'defer') {
11505 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
11506 - }
11507 - } else {
11508 - // For delay or interaction-based loading, we'll use a custom loader
11509 - // Don't enqueue the main script - we'll load it dynamically
11510 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
11511 - }
11512 -
4147 + // Fetch options from the database
4148 + $this->options = get_option('mxchat_options');
11513 4149 $prompts_options = get_option('mxchat_prompts_options', array());
11514 4150
11515 - // Check if AI theme is active - if so, skip inline colors in JavaScript
11516 - $theme_options = get_option('mxchat_theme_options', array());
11517 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11518 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11519 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11520 -
11521 4151 // Prepare settings for JavaScript
11522 4152 $style_settings = array(
11523 4153 'ajax_url' => admin_url('admin-ajax.php'),
11524 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
11525 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
11526 - // as a one-shot fallback for the first interaction on a fresh page load
11527 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
11528 - // but the widget refetches before each subsequent send.
11529 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11530 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11531 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
4154 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'),
11532 4155 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
4156 + 'rate_limit_message' => $this->options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
11533 4157 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11534 4158 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11535 4159 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11536 4160 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
@@ -11542,416 +4166,188 @@
11542 4166 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11543 4167 'icon_color' => $this->options['icon_color'] ?? '#fff',
11544 4168 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11545 4169 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11546 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
4170 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off', // Example for consistency
4171 +
11547 4172 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11548 4173 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
4174 + 'chat_toolbar_toggle' => $this->options['chat_toolbar_toggle'] ?? 'off',
11549 4175 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11550 4176 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11551 4177 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
4178 +
11552 4179 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11553 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
11554 - 'initial_email_state' => null, // Also fixed this undefined variable
11555 - 'skip_email_check' => true,
11556 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11557 - 'skip_inline_colors' => $skip_inline_colors,
11558 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
4180 + 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1'
11559 4181 );
11560 4182
11561 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11562 - // print/transcript, satisfaction rating) come from the shared
11563 - // dynamic-settings method so this inline payload and the first-open
11564 - // refresh endpoint can never drift (plan-32db95).
11565 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11566 -
11567 - // For normal/defer loading, use wp_localize_script
11568 - // For delayed loading, we store settings in a transient to be output inline
11569 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11570 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11571 - } else {
11572 - // Store settings for the delayed loader to use
11573 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
11574 - }
4183 + // Pass the settings to the script
4184 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11575 4185 }
11576 4186
11577 -/**
11578 - * Output the delayed script loader for performance optimization
11579 - */
11580 -public function mxchat_output_delayed_script_loader() {
11581 - $this->options = get_option('mxchat_options');
11582 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11583 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
11584 4187
11585 - // Get the stored settings
11586 - $prompts_options = get_option('mxchat_prompts_options', array());
11587 - $theme_options = get_option('mxchat_theme_options', array());
11588 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11589 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11590 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11591 -
11592 - $style_settings = array(
11593 - 'ajax_url' => admin_url('admin-ajax.php'),
11594 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
11595 - // before each send. This inline value is a one-shot fallback for the first interaction.
11596 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11597 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11598 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11599 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11600 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11601 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11602 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11603 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11604 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11605 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11606 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11607 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11608 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11609 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11610 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11611 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11612 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11613 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11614 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11615 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11616 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11617 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11618 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11619 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11620 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
11621 - 'initial_email_state' => null,
11622 - 'skip_email_check' => true,
11623 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11624 - 'skip_inline_colors' => $skip_inline_colors,
11625 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11626 - );
11627 -
11628 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11629 - // print/transcript, satisfaction rating) come from the shared
11630 - // dynamic-settings method so this inline payload and the first-open
11631 - // refresh endpoint can never drift (plan-32db95).
11632 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11633 -
11634 - // Determine delay time based on strategy
11635 - $delay_ms = 0;
11636 - switch ($loading_strategy) {
11637 - case 'delay_1s':
11638 - $delay_ms = 1000;
11639 - break;
11640 - case 'delay_3s':
11641 - $delay_ms = 3000;
11642 - break;
11643 - case 'delay_5s':
11644 - $delay_ms = 5000;
11645 - break;
11646 - }
11647 -
11648 - ?>
11649 - <script type="text/javascript">
11650 - (function() {
11651 - var mxchatLoaded = false;
11652 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
11653 - window.mxchatChat = mxchatChat;
11654 -
11655 - function loadMxChatScript() {
11656 - if (mxchatLoaded) return;
11657 - mxchatLoaded = true;
11658 -
11659 - function appendChatScript() {
11660 - var script = document.createElement('script');
11661 - script.src = <?php echo wp_json_encode($script_url); ?>;
11662 - script.type = 'text/javascript';
11663 - document.body.appendChild(script);
11664 - }
11665 -
11666 - if (typeof jQuery !== 'undefined') {
11667 - appendChatScript();
11668 - } else {
11669 - var jq = document.createElement('script');
11670 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
11671 - jq.onload = appendChatScript;
11672 - document.body.appendChild(jq);
11673 - }
11674 - }
11675 -
11676 - <?php if ($loading_strategy === 'on_interaction'): ?>
11677 - // Load on user interaction
11678 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
11679 - events.forEach(function(evt) {
11680 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
11681 - });
11682 - // Fallback: load after 8 seconds if no interaction
11683 - setTimeout(loadMxChatScript, 8000);
11684 - <?php else: ?>
11685 - // Load after specified delay
11686 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
11687 - <?php endif; ?>
11688 - })();
11689 - </script>
11690 - <?php
11691 -}
11692 -
11693 -/**
11694 - * Setup the cron jobs for rate limits with guard against multiple calls
11695 - */
11696 -public function setup_rate_limit_cron_jobs() {
11697 - // Add a guard to prevent multiple rapid calls
11698 - $last_setup = get_transient('mxchat_cron_setup_guard');
11699 - if ($last_setup && (time() - $last_setup) < 60) {
11700 - // Don't run again if we ran less than 60 seconds ago
11701 - return;
11702 - }
4188 +// Modify the mxchat_reset_rate_limits function to handle different timeframes
4189 +public function mxchat_reset_rate_limits() {
4190 + global $wpdb;
4191 + $all_options = get_option('mxchat_options', []);
4192 + $current_time = time();
11703 4193
11704 - // Set the guard
11705 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
4194 + // Get all rate limit options
4195 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
11706 4196
11707 - try {
11708 - // First, check if WordPress cron is disabled
11709 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
11710 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
11711 - $this->setup_fallback_rate_limit_system();
11712 - return;
11713 - }
4197 + foreach ($option_names as $option_name) {
4198 + // Parse the option name to extract role and user ID
4199 + // Format: mxchat_chat_limit_ROLE_USERID or mxchat_chat_limit_logged_out_IP
4200 + $parts = explode('_', $option_name);
11714 4201
11715 - // Check if cron is already scheduled - if so, don't mess with it
11716 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
11717 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
11718 - return;
4202 + // Skip if the option name doesn't match our expected format
4203 + if (count($parts) < 4) {
4204 + continue;
11719 4205 }
11720 4206
11721 - // Clear any orphaned hooks (but don't loop indefinitely)
11722 - $hooks_to_clear = [
11723 - 'mxchat_reset_rate_limits',
11724 - 'mxchat_reset_hourly_rate_limits',
11725 - 'mxchat_reset_daily_rate_limits',
11726 - 'mxchat_reset_weekly_rate_limits',
11727 - 'mxchat_reset_monthly_rate_limits'
11728 - ];
4207 + // Extract role (may be multiple parts like 'shop_manager')
4208 + $role_parts = array_slice($parts, 3, -1); // Get all parts between 'mxchat_chat_limit_' and the last part (user ID)
4209 + $role = implode('_', $role_parts);
11729 4210
11730 - foreach ($hooks_to_clear as $hook) {
11731 - // Only clear a maximum of 3 instances to prevent infinite loops
11732 - $cleared = 0;
11733 - while (wp_next_scheduled($hook) && $cleared < 3) {
11734 - wp_clear_scheduled_hook($hook);
11735 - $cleared++;
11736 - }
4211 + // Skip if role doesn't exist in our settings
4212 + if (!isset($all_options['rate_limits'][$role])) {
4213 + continue;
11737 4214 }
11738 4215
11739 - // Small delay after clearing
11740 - usleep(100000); // 0.1 seconds
4216 + $timeframe = $all_options['rate_limits'][$role]['timeframe'];
4217 + $limit_data = get_option($option_name);
11741 4218
11742 - // Try to schedule the event
11743 - $initial_time = time() + 300; // Start in 5 minutes
11744 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
11745 -
11746 - if ($result === false) {
11747 - //error_log('MxChat: Failed to schedule cron, using fallback system');
11748 - $this->setup_fallback_rate_limit_system();
11749 - } else {
11750 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
4219 + if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
4220 + continue;
11751 4221 }
11752 4222
11753 - } catch (Exception $e) {
11754 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
11755 - $this->setup_fallback_rate_limit_system();
11756 - }
11757 -}
11758 -
11759 -/**
11760 - * Try alternative cron scheduling methods
11761 - */
11762 -private function try_alternative_cron_scheduling($initial_time) {
11763 - try {
11764 - // Method 1: Try with current time instead of future time
11765 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
11766 - if ($result1 !== false) {
11767 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
11768 - return true;
11769 - }
4223 + $timestamp = $limit_data['timestamp'];
4224 + $should_reset = false;
11770 4225
11771 - // Method 2: Try with a different interval
11772 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
11773 - if ($result2 !== false) {
11774 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
11775 - return true;
4226 + // Determine if we should reset based on the timeframe
4227 + switch ($timeframe) {
4228 + case 'hourly':
4229 + $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
4230 + break;
4231 + case 'daily':
4232 + $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
4233 + break;
4234 + case 'weekly':
4235 + $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
4236 + break;
4237 + case 'monthly':
4238 + $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
4239 + break;
11776 4240 }
11777 4241
11778 - // Method 3: Try wp_schedule_single_event first, then recurring
11779 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
11780 - if ($result3 !== false) {
11781 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
11782 - // Schedule the next one manually in the handler
11783 - return true;
4242 + // Reset the counter if the timeframe has passed
4243 + if ($should_reset) {
4244 + delete_option($option_name);
4245 + wp_cache_delete($option_name, 'options');
11784 4246 }
11785 -
11786 - return false;
11787 -
11788 - } catch (Exception $e) {
11789 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
11790 - return false;
11791 4247 }
4248 +
4249 + // Clean up any orphaned entries
4250 + wp_cache_delete('mxchat_all_chat_limits', 'options');
11792 4251 }
4252 +private function mxchat_fetch_woocommerce_products() {
4253 + // Ensure WooCommerce is active
4254 + if (!class_exists('WooCommerce')) {
4255 + return [];
4256 + }
11793 4257
11794 -/**
11795 - * Enhanced fallback rate limit system
11796 - */
11797 -private function setup_fallback_rate_limit_system() {
11798 - // Set a flag to use database-based rate limit cleanup
11799 - update_option('mxchat_use_fallback_rate_limits', true);
11800 -
11801 - // Schedule a one-time check to happen on the next plugin load
11802 - update_option('mxchat_next_rate_limit_check', time() + 3600);
11803 -
11804 - // Also set up a more frequent fallback check (every 4 hours)
11805 - update_option('mxchat_fallback_check_interval', 4 * 3600);
11806 -
11807 - //error_log('MxChat: Fallback rate limit system activated');
11808 -}
4258 + $args = array(
4259 + 'post_type' => 'product',
4260 + 'post_status' => 'publish',
4261 + 'posts_per_page' => -1,
4262 + );
11809 4263
11810 -/**
11811 - * Enhanced fallback check method
11812 - */
11813 -public function check_fallback_rate_limits() {
11814 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11815 -
11816 - if (!$use_fallback) {
11817 - return; // Regular cron is working
4264 + $products = get_posts($args);
4265 + $product_data = [];
4266 +
4267 + foreach ($products as $product) {
4268 + $product_id = $product->ID;
4269 + $product_obj = wc_get_product($product_id);
4270 +
4271 + $product_data[] = array(
4272 + 'id' => $product_id,
4273 + 'name' => $product_obj->get_name(),
4274 + 'description' => $product_obj->get_description(),
4275 + 'short_description' => $product_obj->get_short_description(),
4276 + 'url' => get_permalink($product_id),
4277 + 'price' => $product_obj->get_regular_price(),
4278 + 'sale_price' => $product_obj->get_sale_price(),
4279 + 'stock_status' => $product_obj->get_stock_status(),
4280 + 'sku' => $product_obj->get_sku(),
4281 + 'in_stock' => $product_obj->is_in_stock(),
4282 + 'total_sales' => $product_obj->get_total_sales(),
4283 + );
11818 4284 }
11819 -
11820 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11821 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
11822 -
11823 - if (time() >= $next_check) {
11824 - //error_log('MxChat: Running fallback rate limit cleanup');
11825 - $this->mxchat_reset_rate_limits();
11826 -
11827 - // Schedule next check
11828 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
11829 - }
4285 +
4286 + return $product_data;
11830 4287 }
4288 +
4289 +
11831 4290 /**
11832 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
4291 + * Check if the current user has exceeded their rate limit based on role
4292 + *
4293 + * @return true|array True if limit not exceeded, or array with error message if exceeded
11833 4294 */
11834 4295 public function check_rate_limit() {
11835 - // Check if we need to run fallback cleanup
11836 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11837 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
4296 + $all_options = get_option('mxchat_options', []);
4297 + //error_log('MXChat Rate Limit: Starting check');
4298 + //error_log('MXChat Rate Limit: Options: ' . print_r($all_options, true));
11838 4299
11839 - if ($use_fallback && time() >= $next_check) {
11840 - $this->mxchat_reset_rate_limits();
11841 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
11842 - }
11843 -
11844 - // Get bot ID from current request context
11845 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11846 -
11847 - // Get bot-specific options (includes rate limits if overridden)
11848 - $bot_options = $this->get_bot_options($bot_id);
11849 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
11850 -
11851 - // Use bot-specific rate limits if available, otherwise fall back to default
11852 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
11853 -
11854 - // -------------------------------------------------------------------
11855 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
11856 - // it acts as a hard ceiling across all users + all roles. Default is
11857 - // 'unlimited' so existing installs are unchanged. Counter key drops
11858 - // both <role> and <user_id> segments — single pool per bot.
11859 - // -------------------------------------------------------------------
11860 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
11861 - ? $current_options['rate_limits_global']
11862 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
11863 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
11864 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
11865 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
11866 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11867 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
11868 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
11869 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
11870 - if ((int) $global_data['count'] === 0) {
11871 - $global_data['timestamp'] = time();
11872 - update_option($global_option, $global_data);
11873 - }
11874 - $now = time();
11875 - $ts = (int) $global_data['timestamp'];
11876 - $reset = false;
11877 - switch ($global_timeframe) {
11878 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
11879 - case 'daily': $reset = ($now - $ts) >= 86400; break;
11880 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
11881 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
11882 - }
11883 - if ($reset) {
11884 - $global_data = ['count' => 0, 'timestamp' => $now];
11885 - update_option($global_option, $global_data);
11886 - }
11887 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
11888 - $global_msg = !empty($global_cfg['message'])
11889 - ? $global_cfg['message']
11890 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
11891 - return [
11892 - 'error' => true,
11893 - 'message' => $this->process_rate_limit_message_html($global_msg),
11894 - ];
11895 - }
11896 - // Reserve the slot for this request. Per-role check below also increments
11897 - // its own counter — that is intentional, both ceilings apply independently.
11898 - $global_data['count']++;
11899 - update_option($global_option, $global_data);
11900 - }
11901 -
11902 4300 // Determine user role or if logged out
11903 4301 if (is_user_logged_in()) {
11904 4302 $user = wp_get_current_user();
11905 4303 $user_id = $user->ID;
11906 4304
11907 - // Get the user's primary role using reset() to safely get the first element
4305 + // Get the user's primary role (first in the array)
11908 4306 $user_roles = $user->roles;
11909 -
11910 - // Safely get the first role regardless of array key structure
11911 - if (!empty($user_roles) && is_array($user_roles)) {
11912 - $role = reset($user_roles); // This safely gets the first element regardless of key
11913 - } else {
11914 - $role = 'subscriber'; // Default to subscriber if no role found
11915 - }
4307 + $role = !empty($user_roles) ? $user_roles[0] : 'subscriber'; // Default to subscriber if no role found
4308 + //error_log('MXChat Rate Limit: User ID: ' . $user_id . ', Role: ' . $role);
11916 4309 } else {
11917 4310 $role = 'logged_out';
11918 4311 // Use IP address for non-logged-in users
11919 4312 $user_id = $this->get_client_ip();
4313 + //error_log('MXChat Rate Limit: Logged out user IP: ' . $user_id);
11920 4314 }
11921 4315
11922 4316 // Check if rate limits are configured for this role
11923 - if (!isset($rate_limits_source[$role])) {
4317 + if (!isset($all_options['rate_limits'][$role])) {
4318 + //error_log('MXChat Rate Limit: No rate limit configured for role: ' . $role);
11924 4319 return true; // No limit set for this role
11925 4320 }
11926 4321
11927 - $limit = $rate_limits_source[$role]['limit'];
4322 + $limit = $all_options['rate_limits'][$role]['limit'];
4323 + //error_log('MXChat Rate Limit: Limit for role ' . $role . ': ' . $limit);
11928 4324
11929 4325 // If unlimited, return true immediately
11930 4326 if ($limit === 'unlimited') {
4327 + //error_log('MXChat Rate Limit: Unlimited setting, no limit applied');
11931 4328 return true;
11932 4329 }
11933 4330
11934 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
11935 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
11936 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
11937 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
4331 + // Get the option name for this user/role
4332 + $option_name = 'mxchat_chat_limit_' . $role . '_' . $user_id;
4333 + //error_log('MXChat Rate Limit: Option name: ' . $option_name);
11938 4334
11939 - // Include bot_id in option name so each bot has separate rate limits
11940 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
11941 -
11942 4335 // Get the counter data
11943 4336 $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
4337 + //error_log('MXChat Rate Limit: Current limit data: ' . print_r($limit_data, true));
11944 4338
11945 4339 // If first request or counter reset needed, set the initial timestamp
11946 4340 if ($limit_data['count'] === 0) {
11947 4341 $limit_data['timestamp'] = time();
11948 4342 update_option($option_name, $limit_data);
4343 + //error_log('MXChat Rate Limit: First request, initialized timestamp');
11949 4344 }
11950 4345
11951 4346 // Get the timeframe
11952 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
11953 - $rate_limits_source[$role]['timeframe'] : 'daily';
4347 + $timeframe = isset($all_options['rate_limits'][$role]['timeframe']) ?
4348 + $all_options['rate_limits'][$role]['timeframe'] : 'daily';
4349 + //error_log('MXChat Rate Limit: Timeframe: ' . $timeframe);
11954 4350
11955 4351 // Check if the counter needs to be reset based on timeframe
11956 4352 $current_time = time();
11957 4353 $timestamp = $limit_data['timestamp'];
@@ -11971,19 +4367,24 @@
11971 4367 $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
11972 4368 break;
11973 4369 }
11974 4370
4371 + //error_log('MXChat Rate Limit: Current time: ' . $current_time . ', Last timestamp: ' . $timestamp);
4372 + //error_log('MXChat Rate Limit: Time elapsed: ' . ($current_time - $timestamp) . ' seconds');
4373 + //error_log('MXChat Rate Limit: Should reset: ' . ($should_reset ? 'Yes' : 'No'));
4374 +
11975 4375 // Reset the counter if the timeframe has passed
11976 4376 if ($should_reset) {
11977 4377 $limit_data = ['count' => 0, 'timestamp' => $current_time];
11978 4378 update_option($option_name, $limit_data);
4379 + //error_log('MXChat Rate Limit: Reset counter to 0');
11979 4380 }
11980 4381
11981 4382 // Check if user has exceeded their limit
11982 4383 if ($limit_data['count'] >= intval($limit)) {
11983 4384 // Get the custom message for this role
11984 - $message = !empty($rate_limits_source[$role]['message'])
11985 - ? $rate_limits_source[$role]['message']
4385 + $message = !empty($all_options['rate_limits'][$role]['message'])
4386 + ? $all_options['rate_limits'][$role]['message']
11986 4387 : __('Rate limit exceeded. Please try again later.', 'mxchat');
11987 4388
11988 4389 // Add timeframe information to the message if placeholders exist
11989 4390 $timeframe_label = '';
@@ -12008,12 +4409,11 @@
12008 4409 [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
12009 4410 $message
12010 4411 );
12011 4412
12012 - // Process HTML links in the message
12013 - $message = $this->process_rate_limit_message_html($message);
4413 + //error_log('MXChat Rate Limit: Limit exceeded. Message: ' . $message);
12014 4414
12015 - // Return error with the processed message
4415 + // Return error with the custom message
12016 4416 return [
12017 4417 'error' => true,
12018 4418 'message' => $message
12019 4419 ];
@@ -12021,235 +4421,13 @@
12021 4421
12022 4422 // Increment the counter
12023 4423 $limit_data['count']++;
12024 4424 update_option($option_name, $limit_data);
4425 + //error_log('MXChat Rate Limit: Incremented counter to ' . $limit_data['count']);
12025 4426
12026 4427 return true;
12027 4428 }
12028 4429
12029 -/**
12030 - * Enhanced rate limit reset with better error handling
12031 - */
12032 -public function mxchat_reset_rate_limits() {
12033 - try {
12034 - global $wpdb;
12035 - $all_options = get_option('mxchat_options', []);
12036 - $current_time = time();
12037 -
12038 - // Get rate limit options with a safer query and limit
12039 - $option_names = $wpdb->get_col(
12040 - $wpdb->prepare(
12041 - "SELECT option_name FROM {$wpdb->options}
12042 - WHERE option_name LIKE %s
12043 - LIMIT 1000",
12044 - 'mxchat_chat_limit_%'
12045 - )
12046 - );
12047 -
12048 - if (empty($option_names)) {
12049 - return;
12050 - }
12051 -
12052 - $processed_count = 0;
12053 - $max_processing_time = 30; // Maximum 30 seconds
12054 - $start_time = time();
12055 -
12056 - foreach ($option_names as $option_name) {
12057 - // Check processing time limit
12058 - if ((time() - $start_time) > $max_processing_time) {
12059 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
12060 - break;
12061 - }
12062 -
12063 - // Parse the option name more safely
12064 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
12065 - continue;
12066 - }
12067 -
12068 - $role_and_user = $matches[1] . '_' . $matches[2];
12069 - $parts = explode('_', $role_and_user);
12070 -
12071 - if (count($parts) < 2) {
12072 - continue;
12073 - }
12074 -
12075 - // Extract role (everything except the last part which is user ID)
12076 - $user_id_part = array_pop($parts);
12077 - $role = implode('_', $parts);
12078 -
12079 - // Skip if role doesn't exist in our settings
12080 - if (!isset($all_options['rate_limits'][$role])) {
12081 - // Clean up orphaned entries
12082 - delete_option($option_name);
12083 - continue;
12084 - }
12085 -
12086 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
12087 - $limit_data = get_option($option_name);
12088 -
12089 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
12090 - // Clean up invalid entries
12091 - delete_option($option_name);
12092 - continue;
12093 - }
12094 -
12095 - $timestamp = $limit_data['timestamp'];
12096 - $should_reset = false;
12097 -
12098 - // Determine if we should reset based on the timeframe
12099 - switch ($timeframe) {
12100 - case 'hourly':
12101 - $should_reset = ($current_time - $timestamp) >= 3600;
12102 - break;
12103 - case 'daily':
12104 - $should_reset = ($current_time - $timestamp) >= 86400;
12105 - break;
12106 - case 'weekly':
12107 - $should_reset = ($current_time - $timestamp) >= 604800;
12108 - break;
12109 - case 'monthly':
12110 - $should_reset = ($current_time - $timestamp) >= 2592000;
12111 - break;
12112 - }
12113 -
12114 - // Reset the counter if the timeframe has passed
12115 - if ($should_reset) {
12116 - delete_option($option_name);
12117 - wp_cache_delete($option_name, 'options');
12118 - $processed_count++;
12119 - }
12120 - }
12121 -
12122 - // Clean up any orphaned cache entries
12123 - wp_cache_delete('mxchat_all_chat_limits', 'options');
12124 -
12125 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
12126 -
12127 - } catch (Exception $e) {
12128 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
12129 - }
12130 -}
12131 -
12132 -
12133 -/**
12134 - * Process HTML links in rate limit messages
12135 - *
12136 - * @param string $message The rate limit message
12137 - * @return string The processed message with safe HTML links
12138 - */
12139 -private function process_rate_limit_message_html($message) {
12140 - // Return original message if empty
12141 - if (empty($message)) {
12142 - return $message;
12143 - }
12144 -
12145 - // First, convert markdown links to HTML
12146 - $message = $this->convert_markdown_links($message);
12147 -
12148 - // Then, auto-convert any remaining plain URLs to links
12149 - $message = $this->auto_link_urls($message);
12150 -
12151 - // Allow basic HTML tags for links and formatting
12152 - $allowed_tags = [
12153 - 'a' => [
12154 - 'href' => true,
12155 - 'target' => true,
12156 - 'rel' => true,
12157 - 'title' => true,
12158 - 'class' => true
12159 - ],
12160 - 'strong' => [],
12161 - 'em' => [],
12162 - 'br' => [],
12163 - 'b' => [],
12164 - 'i' => [],
12165 - 'span' => ['class' => true]
12166 - ];
12167 -
12168 - // Sanitize but allow the specified HTML tags
12169 - $processed_message = wp_kses($message, $allowed_tags);
12170 -
12171 - // If wp_kses stripped everything, return the original message as plain text
12172 - if (empty($processed_message) && !empty($message)) {
12173 - // Strip all HTML and return plain text as fallback
12174 - return wp_strip_all_tags($message);
12175 - }
12176 -
12177 - return $processed_message;
12178 -}
12179 -
12180 -/**
12181 - * Convert markdown links to HTML
12182 - *
12183 - * @param string $text The text to process
12184 - * @return string The text with markdown links converted to HTML
12185 - */
12186 -private function convert_markdown_links($text) {
12187 - // Return original text if empty
12188 - if (empty($text)) {
12189 - return $text;
12190 - }
12191 -
12192 - // Pattern to match markdown links: [text](url)
12193 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
12194 -
12195 - $processed_text = preg_replace_callback($pattern, function($matches) {
12196 - $link_text = $matches[1];
12197 - $url = $matches[2];
12198 -
12199 - // Clean up any trailing punctuation from the URL
12200 - $url = rtrim($url, '.,;:!?');
12201 -
12202 - // Sanitize the link text and URL
12203 - $safe_text = esc_html($link_text);
12204 - $safe_url = esc_url($url);
12205 -
12206 - // Create the HTML link
12207 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
12208 - }, $text);
12209 -
12210 - // If preg_replace_callback failed, return original text
12211 - if ($processed_text === null) {
12212 - return $text;
12213 - }
12214 -
12215 - return $processed_text;
12216 -}
12217 -
12218 -/**
12219 - * Auto-convert plain URLs to clickable links
12220 - *
12221 - * @param string $text The text to process
12222 - * @return string The text with URLs converted to links
12223 - */
12224 -private function auto_link_urls($text) {
12225 - // Return original text if empty
12226 - if (empty($text)) {
12227 - return $text;
12228 - }
12229 -
12230 - // Simple pattern that avoids complex lookbehinds
12231 - // This will match URLs that are not already inside href attributes or markdown links
12232 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
12233 -
12234 - $processed_text = preg_replace_callback($pattern, function($matches) {
12235 - $url = $matches[0];
12236 - // Clean up any trailing punctuation that might have been captured
12237 - $url = rtrim($url, '.,;:!?');
12238 -
12239 - // Add target="_blank" and rel="noopener noreferrer" for security
12240 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
12241 - }, $text);
12242 -
12243 - // If preg_replace_callback failed, return original text
12244 - if ($processed_text === null) {
12245 - return $text;
12246 - }
12247 -
12248 - return $processed_text;
12249 -}
12250 -
12251 -
12252 4430 // Helper function to get client IP address
12253 4431 private function get_client_ip() {
12254 4432 // Check for shared internet/ISP IP
12255 4433 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
@@ -12269,625 +4447,7 @@
12269 4447
12270 4448 // Fallback
12271 4449 return 'unknown';
12272 4450 }
12273 -
12274 -/**
12275 - * AJAX handler to get system information for testing panel
12276 - */
12277 -/**
12278 - * AJAX handler to get system information for testing panel
12279 - */
12280 -public function mxchat_get_system_info() {
12281 - // Verify nonce for security
12282 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12283 - wp_send_json_error(['message' => 'Invalid nonce']);
12284 - return;
12285 - }
12286 -
12287 - // Only allow admin users
12288 - if (!current_user_can('administrator')) {
12289 - wp_send_json_error(['message' => 'Unauthorized']);
12290 - return;
12291 - }
12292 -
12293 - // Get system prompt from options
12294 - $system_prompt = isset($this->options['system_prompt_instructions'])
12295 - ? $this->options['system_prompt_instructions']
12296 - : 'No system prompt configured';
12297 -
12298 - // Get selected model
12299 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
12300 -
12301 - // Check if OpenRouter is being used
12302 - $is_openrouter = ($selected_model === 'openrouter');
12303 - $openrouter_model = '';
12304 -
12305 - if ($is_openrouter) {
12306 - // Get the actual OpenRouter model that's selected
12307 - $openrouter_model = isset($this->options['openrouter_selected_model'])
12308 - ? $this->options['openrouter_selected_model']
12309 - : 'No OpenRouter model selected';
12310 -
12311 - // Update selected_model display to show both
12312 - $selected_model = 'OpenRouter: ' . $openrouter_model;
12313 - }
12314 -
12315 - // Get API key status (just check if they exist, don't expose the keys)
12316 - $api_status = [];
12317 - $api_status['openai'] = !empty($this->options['api_key']);
12318 - $api_status['claude'] = !empty($this->options['claude_api_key']);
12319 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
12320 - $api_status['xai'] = !empty($this->options['xai_api_key']);
12321 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
12322 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
12323 -
12324 - wp_send_json_success([
12325 - 'system_prompt' => $system_prompt,
12326 - 'selected_model' => $selected_model,
12327 - 'is_openrouter' => $is_openrouter,
12328 - 'openrouter_model' => $openrouter_model,
12329 - 'api_status' => $api_status
12330 - ]);
12331 -}
12332 -
12333 -/**
12334 - * AJAX handler to get similarity threshold
12335 - */
12336 -public function mxchat_get_similarity_threshold() {
12337 - // Verify nonce for security
12338 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12339 - wp_send_json_error(['message' => 'Invalid nonce']);
12340 - return;
12341 - }
12342 -
12343 - // Only allow admin users
12344 - if (!current_user_can('administrator')) {
12345 - wp_send_json_error(['message' => 'Unauthorized']);
12346 - return;
12347 - }
12348 -
12349 - // Get similarity threshold from main options (default 35%)
12350 - $similarity_threshold = isset($this->options['similarity_threshold'])
12351 - ? ((int) $this->options['similarity_threshold']) / 100
12352 - : 0.35;
12353 -
12354 - wp_send_json_success([
12355 - 'threshold' => $similarity_threshold,
12356 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
12357 - ]);
12358 -}
12359 -
12360 -/**
12361 - * AJAX handler to get knowledge base status
12362 - */
12363 -public function mxchat_get_kb_status() {
12364 - // Verify nonce for security
12365 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12366 - wp_send_json_error(['message' => 'Invalid nonce']);
12367 - return;
12368 - }
12369 -
12370 - // Only allow admin users
12371 - if (!current_user_can('administrator')) {
12372 - wp_send_json_error(['message' => 'Unauthorized']);
12373 - return;
12374 - }
12375 -
12376 - // Check OpenAI Vector Store first (takes priority)
12377 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
12378 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
12379 -
12380 - if ($use_vectorstore) {
12381 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
12382 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
12383 -
12384 - $kb_info = [
12385 - 'type' => 'OpenAI Vector Store',
12386 - 'status' => 'Active',
12387 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
12388 - ];
12389 -
12390 - wp_send_json_success($kb_info);
12391 - return;
12392 - }
12393 -
12394 - // Check Pinecone vs WordPress
12395 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12396 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12397 -
12398 - $kb_info = [
12399 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
12400 - 'status' => 'Active'
12401 - ];
12402 -
12403 - // Get document count
12404 - if ($use_pinecone) {
12405 - $kb_info['documents'] = 'Connected to Pinecone';
12406 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
12407 - } else {
12408 - // Count documents in WordPress database
12409 - global $wpdb;
12410 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
12411 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
12412 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
12413 - }
12414 -
12415 - wp_send_json_success($kb_info);
12416 -}
12417 -
12418 -/**
12419 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
12420 - */
12421 -public function mxchat_start_fresh_session() {
12422 - // Verify nonce for security
12423 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12424 - wp_send_json_error(['message' => 'Invalid nonce']);
12425 - return;
12426 - }
12427 -
12428 - // Only allow admin users
12429 - if (!current_user_can('administrator')) {
12430 - wp_send_json_error(['message' => 'Unauthorized']);
12431 - return;
12432 - }
12433 -
12434 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
12435 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
12436 -
12437 - if (empty($old_session_id)) {
12438 - wp_send_json_error(['message' => 'Old session ID required']);
12439 - return;
12440 - }
12441 -
12442 - // If no new session ID provided, generate one
12443 - if (empty($new_session_id)) {
12444 - // Cryptographically strong session id (plan-0c17b5). Prefix preserved
12445 - // exactly (other code pattern-matches on 'mxchat_chat_'). random_bytes
12446 - // is guaranteed on all supported PHP (7+).
12447 - $new_session_id = 'mxchat_chat_' . bin2hex(random_bytes(16));
12448 - }
12449 -
12450 - // Clear ALL data associated with the old session
12451 - $this->clear_complete_session_data($old_session_id);
12452 -
12453 - // Initialize the new session
12454 - $this->initialize_fresh_session($new_session_id);
12455 -
12456 - wp_send_json_success([
12457 - 'message' => 'Fresh session started successfully',
12458 - 'new_session_id' => $new_session_id,
12459 - 'old_session_id' => $old_session_id
12460 - ]);
12461 -}
12462 -
12463 -/**
12464 - * Clear ALL data associated with a session (ENHANCED)
12465 - */
12466 -private function clear_complete_session_data($session_id) {
12467 - // Clear chat history
12468 - delete_option("mxchat_history_{$session_id}");
12469 -
12470 - // Clear chat mode
12471 - delete_option("mxchat_mode_{$session_id}");
12472 -
12473 - // Clear any PDF/Word transients
12474 - $this->clear_pdf_transients($session_id);
12475 - if (method_exists($this, 'clear_word_transients')) {
12476 - $this->clear_word_transients($session_id);
12477 - }
12478 -
12479 - // Clear agent-related data
12480 - delete_option("mxchat_channel_{$session_id}");
12481 - delete_option("mxchat_agent_name_{$session_id}");
12482 - delete_option("mxchat_email_{$session_id}");
12483 -
12484 - // Clear any recommendation flow state
12485 - delete_option("mxchat_sr_flow_state_{$session_id}");
12486 -
12487 - // Clear any cached embeddings or context
12488 - delete_transient("mxchat_context_{$session_id}");
12489 - delete_transient("mxchat_last_query_{$session_id}");
12490 -
12491 - // Clear any testing data
12492 - delete_transient("mxchat_testing_data_{$session_id}");
12493 -
12494 - // Clear any rate limiting data for this session
12495 - delete_transient("mxchat_rate_limit_{$session_id}");
12496 -
12497 - // Clear any other session-specific transients
12498 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
12499 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
12500 - delete_transient("mxchat_include_word_in_context_{$session_id}");
12501 -
12502 - // Clear form addon state (pending forms and submitted forms)
12503 - delete_option("mxchat_pending_form_{$session_id}");
12504 - delete_option("mxchat_submitted_forms_{$session_id}");
12505 -
12506 - //error_log("MxChat: Cleared all data for session: {$session_id}");
12507 -}
12508 -
12509 -/**
12510 - * Initialize a fresh session with default data
12511 - */
12512 -private function initialize_fresh_session($session_id) {
12513 - // Set default chat mode
12514 - update_option("mxchat_mode_{$session_id}", 'ai');
12515 -
12516 - //error_log("MxChat: Initialized fresh session: {$session_id}");
12517 -}
12518 -
12519 -/**
12520 - * Helper method to clear Word document transients (if you have Word support)
12521 - */
12522 -private function clear_word_transients($session_id) {
12523 - delete_transient('mxchat_word_url_' . $session_id);
12524 - delete_transient('mxchat_word_filename_' . $session_id);
12525 - delete_transient('mxchat_word_embeddings_' . $session_id);
12526 - delete_transient('mxchat_include_word_in_context_' . $session_id);
12527 -}
12528 -
12529 -/**
12530 - * Simplified testing data capture method (CLEANED UP)
12531 - */
12532 -private function capture_testing_data($user_embedding, $message, $session_id) {
12533 - // Only capture for admin users
12534 - if (!current_user_can('administrator')) {
12535 - return null;
12536 - }
12537 -
12538 - $testing_data = [
12539 - 'query' => $message,
12540 - 'timestamp' => time(),
12541 - 'top_matches' => [],
12542 - 'action_matches' => [] // Add action matches
12543 - ];
12544 -
12545 - // Get similarity threshold
12546 - $similarity_threshold = isset($this->options['similarity_threshold'])
12547 - ? ((int) $this->options['similarity_threshold']) / 100
12548 - : 0.35;
12549 -
12550 - $testing_data['similarity_threshold'] = $similarity_threshold;
12551 -
12552 - // Use the real similarity analysis if available
12553 - if ($this->last_similarity_analysis !== null) {
12554 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
12555 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
12556 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
12557 - } else {
12558 - // Fallback: determine knowledge base type
12559 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12560 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12561 -
12562 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
12563 - }
12564 -
12565 - // Include action analysis if available
12566 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
12567 - $testing_data['action_matches'] = $this->last_action_analysis;
12568 -
12569 - // Clear it after capturing to avoid stale data
12570 - $this->last_action_analysis = null;
12571 - }
12572 -
12573 - return $testing_data;
12574 -}
12575 -
12576 -
12577 -/**
12578 - * Track URL clicks from chatbot responses
12579 - */
12580 -public function mxchat_track_url_click() {
12581 - // Verify nonce for security
12582 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12583 - wp_send_json_error(['message' => 'Invalid nonce']);
12584 - wp_die();
12585 - }
12586 -
12587 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12588 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
12589 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
12590 -
12591 - if (empty($session_id) || empty($clicked_url)) {
12592 - wp_send_json_error(['message' => 'Missing required data']);
12593 - wp_die();
12594 - }
12595 -
12596 - global $wpdb;
12597 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12598 -
12599 - // Insert click tracking record
12600 - $wpdb->insert(
12601 - $table_name,
12602 - [
12603 - 'session_id' => $session_id,
12604 - 'clicked_url' => $clicked_url,
12605 - 'message_context' => $message_context,
12606 - 'click_timestamp' => current_time('mysql', 1),
12607 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
12608 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
12609 - ]
12610 - );
12611 -
12612 - wp_send_json_success(['message' => 'Click tracked']);
12613 - wp_die();
12614 -}
12615 -
12616 -/**
12617 - * Get URL click analytics for a session
12618 - */
12619 -public function mxchat_get_url_clicks($session_id) {
12620 - global $wpdb;
12621 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12622 -
12623 - $clicks = $wpdb->get_results($wpdb->prepare(
12624 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
12625 - $session_id
12626 - ));
12627 -
12628 - return $clicks;
12629 -}
12630 -/**
12631 - * Track the originating page where chat was started
12632 - */
12633 -public function mxchat_track_originating_page() {
12634 - // Verify nonce
12635 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12636 - wp_send_json_error(['message' => 'Invalid nonce']);
12637 - wp_die();
12638 - }
12639 -
12640 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12641 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
12642 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
12643 -
12644 - if (empty($session_id)) {
12645 - wp_send_json_error(['message' => 'Missing session ID']);
12646 - wp_die();
12647 - }
12648 -
12649 - global $wpdb;
12650 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
12651 -
12652 - // Check if we've already tracked for this session
12653 - $existing = $wpdb->get_var($wpdb->prepare(
12654 - "SELECT COUNT(*) FROM $table_name
12655 - WHERE session_id = %s
12656 - AND originating_page_url IS NOT NULL",
12657 - $session_id
12658 - ));
12659 -
12660 - if ($existing > 0) {
12661 - wp_send_json_success(['message' => 'Already tracked']);
12662 - wp_die();
12663 - }
12664 -
12665 - // Update the first message in this session with originating page info
12666 - $wpdb->query($wpdb->prepare(
12667 - "UPDATE $table_name
12668 - SET originating_page_url = %s,
12669 - originating_page_title = %s
12670 - WHERE session_id = %s
12671 - ORDER BY timestamp ASC
12672 - LIMIT 1",
12673 - $page_url,
12674 - $page_title,
12675 - $session_id
12676 - ));
12677 -
12678 - wp_send_json_success(['message' => 'Originating page tracked']);
12679 - wp_die();
12680 -}
12681 -
12682 -/**
12683 - * Validate and clean URLs from AI response
12684 - * Removes any URLs that aren't in the knowledge base
12685 - *
12686 - * @param string $response_text The AI-generated response
12687 - * @param array $valid_urls Array of URLs from the knowledge base
12688 - * @return string Cleaned response with invalid URLs removed/flagged
12689 - */
12690 -private function validate_and_clean_urls($response_text, $valid_urls, $session_id = null, $bot_id = null) {
12691 - /**
12692 - * Filter the list of URLs treated as valid (allowlisted) BEFORE the
12693 - * response URL sanitizer strips any link not in the list. Lets a site
12694 - * owner / developer whitelist links their custom function-calling tools
12695 - * return (e.g. session or speaker pages), which are otherwise absent from
12696 - * the RAG/system-prompt-derived list and get stripped to plain text.
12697 - *
12698 - * Purely additive: with no hook registered, apply_filters returns
12699 - * $valid_urls untouched, so there is zero behavior change for anyone who
12700 - * does not use the filter. Applied before the empty-check so a hooked
12701 - * allowlist can participate. (plan-mxchat-20260710-13a471)
12702 - *
12703 - * @param array $valid_urls URLs already known-valid (RAG + system prompt).
12704 - * @param string|null $session_id Current chat session id, if available.
12705 - * @param string|null $bot_id Current bot id, if available.
12706 - */
12707 - $valid_urls = apply_filters('mxchat_valid_urls', $valid_urls, $session_id, $bot_id);
12708 -
12709 - // A bad mu-plugin returning a non-array (or non-string entries) must never
12710 - // fatal the response path — coerce defensively before any use.
12711 - if (!is_array($valid_urls)) {
12712 - $valid_urls = array();
12713 - }
12714 - $valid_urls = array_values(array_filter($valid_urls, static function ($u) {
12715 - return is_string($u) && $u !== '';
12716 - }));
12717 -
12718 - // If no valid URLs provided or empty response, return as-is
12719 - if (empty($valid_urls) || empty($response_text)) {
12720 - //error_log("Validation skipped - empty valid_urls or response");
12721 - return $response_text;
12722 - }
12723 -
12724 - // Extract all URLs from the AI response
12725 - // This regex matches http:// and https:// URLs
12726 - preg_match_all(
12727 - '#\bhttps?://[^\s<>"\')\]]+#i',
12728 - $response_text,
12729 - $matches
12730 - );
12731 -
12732 - // If no URLs found in response, return as-is
12733 - if (empty($matches[0])) {
12734 - //error_log("No URLs found in response");
12735 - return $response_text;
12736 - }
12737 -
12738 - $found_urls = $matches[0];
12739 - $cleaned_response = $response_text;
12740 - $removed_count = 0;
12741 -
12742 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
12743 - $normalized_valid_urls = array_map(function($url) {
12744 - // Remove trailing slash
12745 - $url = rtrim($url, '/');
12746 - // Remove URL fragments (#section)
12747 - $url = preg_replace('/#.*$/', '', $url);
12748 - // Remove trailing punctuation that might have been captured
12749 - $url = rtrim($url, '.,;:!?');
12750 - return $url;
12751 - }, $valid_urls);
12752 -
12753 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
12754 -
12755 - foreach ($found_urls as $found_url) {
12756 - // Clean up the found URL (remove trailing punctuation that might have been captured)
12757 - $clean_found_url = rtrim($found_url, '.,;:!?)');
12758 -
12759 - // DEBUG: Log each URL being checked
12760 - //error_log("Checking found URL: " . $found_url);
12761 -
12762 - // Normalize for comparison
12763 - $normalized_found = rtrim($clean_found_url, '/');
12764 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
12765 -
12766 - //error_log("Normalized found URL: " . $normalized_found);
12767 -
12768 - // Check if this URL exists in our valid URLs list
12769 - $is_valid = false;
12770 -
12771 - //error_log("Starting validation checks for: " . $normalized_found);
12772 -
12773 - // First, try exact match
12774 - if (in_array($normalized_found, $normalized_valid_urls)) {
12775 - $is_valid = true;
12776 - //error_log("EXACT MATCH FOUND");
12777 - } else {
12778 - //error_log("No exact match, checking variations...");
12779 - // If no exact match, check if it's a variation (with query params, etc.)
12780 - foreach ($normalized_valid_urls as $valid_url) {
12781 - //error_log(" Comparing against valid URL: " . $valid_url);
12782 -
12783 - // Check if the found URL starts with a valid URL (handles query params)
12784 - if (strpos($normalized_found, $valid_url) === 0) {
12785 - // Check what comes after the valid URL
12786 - $remainder = substr($normalized_found, strlen($valid_url));
12787 -
12788 - // Only valid if:
12789 - // 1. Exact match (remainder is empty)
12790 - // 2. Query params (starts with ?)
12791 - // 3. Fragment (starts with #)
12792 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
12793 - $is_valid = true;
12794 - //error_log(" MATCH: Found URL is valid variation of base URL");
12795 - break;
12796 - } else {
12797 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
12798 - }
12799 - }
12800 - // Also check the reverse (in case valid URL has query params)
12801 - if (strpos($valid_url, $normalized_found) === 0) {
12802 - $is_valid = true;
12803 - //error_log(" MATCH: Valid URL starts with found URL");
12804 - break;
12805 - }
12806 - }
12807 -
12808 - if (!$is_valid) {
12809 - //error_log("NO MATCH FOUND - URL should be removed");
12810 - }
12811 - }
12812 -
12813 - // If URL is not valid, remove it from the response
12814 - if (!$is_valid) {
12815 - // Log the removal for debugging
12816 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
12817 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
12818 -
12819 - $removed_count++;
12820 -
12821 - // Check if URL is part of a markdown link: [text](url)
12822 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
12823 - if (preg_match($markdown_pattern, $cleaned_response)) {
12824 - //error_log("Found markdown link, removing but keeping text");
12825 - // Remove the markdown link but keep the text
12826 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
12827 - }
12828 - // Check if URL is part of an HTML link: <a href="url">text</a>
12829 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
12830 - //error_log("Found HTML link, removing but keeping text");
12831 - // Remove the HTML link but keep the text
12832 - $link_text = $link_match[1];
12833 - $cleaned_response = preg_replace(
12834 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
12835 - $link_text,
12836 - $cleaned_response
12837 - );
12838 - }
12839 - // Otherwise just remove the bare URL
12840 - else {
12841 - //error_log("Removing bare URL");
12842 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
12843 - }
12844 - }
12845 - }
12846 -
12847 - // Log summary if any URLs were removed
12848 - if ($removed_count > 0) {
12849 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
12850 - } else {
12851 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
12852 - }
12853 -
12854 - // Clean up any double spaces or awkward punctuation left behind
12855 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
12856 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
12857 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
12858 -
12859 - //error_log("Final cleaned response: " . $cleaned_response);
12860 -
12861 - return trim($cleaned_response);
12862 -}
12863 -
12864 -/**
12865 - * AJAX handler to get current chat mode for a session
12866 - */
12867 -public function mxchat_get_current_chat_mode() {
12868 - // Verify nonce for security
12869 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12870 - wp_send_json_error(['message' => 'Invalid nonce']);
12871 - wp_die();
12872 - }
12873 -
12874 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12875 -
12876 - if (empty($session_id)) {
12877 - wp_send_json_error(['message' => 'Session ID missing']);
12878 - wp_die();
12879 - }
12880 -
12881 - // Get the current chat mode for this session
12882 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
12883 -
12884 - wp_send_json_success([
12885 - 'chat_mode' => $chat_mode
12886 - ]);
12887 - wp_die();
12888 -}
12889 -
12890 -
12891 4451
12892 4452 }
12893 4453 ?>