PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 1.0.8
MxChat – AI Chatbot & Content Generation for WordPress v1.0.8
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/class-mxchat-integrator.php +568 -12607 3.2.121.0.8 View file →
@@ -1,12607 +1,568 @@
1 -<?php
2 -if (!defined('ABSPATH')) {
3 - exit;
4 -}
5 -
6 -class MxChat_Integrator {
7 - private $options;
8 - private $prompts_options;
9 - private $chat_count;
10 - private $fallbackResponse;
11 - private $productCardHtml;
12 - // plan-mxchat-20260617-48a57a — function-calling UI payload capture. When a
13 - // model-invoked tool yields a UI element (generated image, woo product card,
14 - // image-search gallery), the FC loop stashes its html here so the FC outcome
15 - // handler can SURFACE it to the frontend the same way the intent path does,
16 - // instead of stripping it to text for the model (the bug: UI-bearing actions
17 - // rendered nothing under function calling).
18 - private $fc_ui_html = '';
19 - private $fc_ui_images = array();
20 - private $fc_ui_captured = false;
21 - private $word_handler;
22 - private $last_similarity_analysis = null;
23 - private $current_valid_urls = [];
24 - private $last_vectorstore_error = null;
25 - private $is_streaming = false; // ADDED: Track if current request is streaming
26 - private $streaming_headers_sent = false; // Track if streaming headers have been sent
27 - private $pending_originating_page = null; // Originating page captured at session start, consumed on row insert
28 - private $current_action_instruction = null; // Success-message instruction injected into the next system context
29 - private $last_action_analysis = null; // Last action-match analysis for testing_data payloads
30 -
31 -/**
32 - * Setup streaming headers - call this right before actually streaming
33 - * This delays header setup to allow actions/forms to return JSON responses
34 - */
35 -/**
36 - * Auto-retry wrapper around wp_remote_post for chat-send provider calls.
37 - *
38 - * Retries up to twice (750ms then 2000ms backoff) when the upstream provider
39 - * returns a TRANSIENT error: WP timeout, 429, 502, 503, 504, or a provider-
40 - * specific "overloaded" / "rate limit" body string. Returns immediately on
41 - * permanent errors (401/403/404/422) so misconfiguration surfaces fast.
42 - *
43 - * Drop-in replacement for wp_remote_post — returns the same shape
44 - * (WP_Error or response array) so the caller's existing error-handling
45 - * code path is unchanged.
46 - *
47 - * STREAMING PATH NOTE: this helper is ONLY for non-streaming chat-send
48 - * paths (the *_response_openai / *_response_claude / etc functions).
49 - * For the *_stream variants, the cURL initial-connect happens inside a
50 - * read-chunks loop — retrying there safely (without re-emitting partial
51 - * stream chunks to the client) is a separate problem. Streaming paths
52 - * are NOT wrapped in this build; tracked as a follow-on.
53 - *
54 - * Honors the `mxchat_options['auto_retry_on_transient_error']` toggle
55 - * (default true). When false, behavior is identical to plain wp_remote_post.
56 - */
57 -private function mxchat_provider_call_with_retry($url, $args, $provider_hint = '') {
58 - $opts = is_array($this->options ?? null) ? $this->options : array();
59 - $enabled = !isset($opts['auto_retry_on_transient_error']) ||
60 - (string) $opts['auto_retry_on_transient_error'] !== '0';
61 -
62 - if (!$enabled) {
63 - return wp_remote_post($url, $args);
64 - }
65 -
66 - $backoffs = array(0, 750, 2000); // ms — first attempt 0, then retry waits
67 - $last_response = null;
68 -
69 - foreach ($backoffs as $i => $delay_ms) {
70 - if ($delay_ms > 0) {
71 - usleep($delay_ms * 1000);
72 - }
73 - $response = wp_remote_post($url, $args);
74 - $last_response = $response;
75 -
76 - if (!$this->mxchat_is_transient_provider_error($response, $provider_hint)) {
77 - return $response;
78 - }
79 -
80 - if (defined('WP_DEBUG') && WP_DEBUG) {
81 - $code_for_log = is_wp_error($response) ? 'wp_error:' . $response->get_error_code()
82 - : (int) wp_remote_retrieve_response_code($response);
83 - error_log(sprintf(
84 - '[MxChat] Transient provider error (provider=%s, attempt=%d/3, status=%s). %s',
85 - $provider_hint ?: 'unknown',
86 - $i + 1,
87 - $code_for_log,
88 - ($i + 1) < count($backoffs) ? 'Retrying.' : 'Giving up.'
89 - ));
90 - }
91 - }
92 -
93 - return $last_response;
94 -}
95 -
96 -/**
97 - * Returns true if a wp_remote_post response represents a TRANSIENT
98 - * provider error worth retrying. Conservative — only retries on signals
99 - * that are very likely to clear within a few seconds.
100 - *
101 - * Transient signals:
102 - * - WP_Error with timeout / connection / dns / ssl
103 - * - HTTP 429, 502, 503, 504
104 - * - Provider-specific overload bodies (gemini "overloaded", openai
105 - * "server_error", anthropic "overloaded_error", xai/grok "Rate limit")
106 - *
107 - * NOT transient (return false — fail-fast):
108 - * - 200/2xx (success)
109 - * - 401, 403, 404, 422 (auth / config errors — retrying wastes the
110 - * budget; the user needs to fix something)
111 - * - Any other 4xx (assume permanent unless explicitly listed above)
112 - * - 5xx other than the four listed above (e.g. 500 generic server error
113 - * is often a malformed request on our side, not a transient outage)
114 - */
115 -private function mxchat_is_transient_provider_error($response, $provider_hint = '') {
116 - if (is_wp_error($response)) {
117 - $code = $response->get_error_code();
118 - return in_array($code, array('http_request_failed', 'connection_failed', 'connection_timeout'), true)
119 - || stripos((string) $response->get_error_message(), 'timed out') !== false
120 - || stripos((string) $response->get_error_message(), 'timeout') !== false;
121 - }
122 -
123 - $status = (int) wp_remote_retrieve_response_code($response);
124 - if (in_array($status, array(429, 502, 503, 504), true)) {
125 - return true;
126 - }
127 - if ($status >= 200 && $status < 300) {
128 - return false;
129 - }
130 - // Permanent 4xx that should fail fast — even with no body.
131 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
132 - return false;
133 - }
134 -
135 - // Provider-specific body inspection for the cases where the upstream
136 - // returns 200 with an error envelope (gemini does this for overload).
137 - $body = (string) wp_remote_retrieve_body($response);
138 - if ($body === '') {
139 - return false;
140 - }
141 - $lower = strtolower($body);
142 - $hint = strtolower((string) $provider_hint);
143 -
144 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
145 - || strpos($lower, 'high demand') !== false
146 - || strpos($lower, 'model is overloaded') !== false)) {
147 - return true;
148 - }
149 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
150 - || strpos($lower, '"type":"server_error"') !== false
151 - || strpos($lower, '"code":"server_error"') !== false)) {
152 - return true;
153 - }
154 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
155 - || strpos($lower, 'overloaded_error') !== false)) {
156 - return true;
157 - }
158 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
159 - return true;
160 - }
161 -
162 - return false;
163 -}
164 -
165 -/**
166 - * Streaming-path classifier: same rules as mxchat_is_transient_provider_error
167 - * but takes a raw (http_code, body, provider_hint, curl_errno) tuple as
168 - * captured during a cURL streaming exec. cURL's WRITEFUNCTION/HEADERFUNCTION
169 - * collect status separately from a plain wp_remote_post array shape, so the
170 - * non-streaming helper above can't be called directly. This delegate keeps
171 - * the classification rules identical across both paths.
172 - */
173 -private function mxchat_is_transient_provider_error_raw($http_code, $body, $provider_hint = '', $curl_errno = 0) {
174 - if ($curl_errno) {
175 - // cURL transport-level error (timeout, connection failure, DNS, etc.)
176 - // Match the same WP_Error timeout/connection signals the array variant treats as transient.
177 - return in_array($curl_errno, array(
178 - CURLE_OPERATION_TIMEDOUT,
179 - CURLE_COULDNT_CONNECT,
180 - CURLE_COULDNT_RESOLVE_HOST,
181 - CURLE_SSL_CONNECT_ERROR,
182 - CURLE_GOT_NOTHING,
183 - CURLE_SEND_ERROR,
184 - CURLE_RECV_ERROR,
185 - ), true);
186 - }
187 -
188 - $status = (int) $http_code;
189 - if (in_array($status, array(429, 502, 503, 504), true)) {
190 - return true;
191 - }
192 - if ($status >= 200 && $status < 300) {
193 - return false;
194 - }
195 - if (in_array($status, array(401, 403, 404, 405, 422), true)) {
196 - return false;
197 - }
198 -
199 - $body = (string) $body;
200 - if ($body === '') {
201 - return false;
202 - }
203 - $lower = strtolower($body);
204 - $hint = strtolower((string) $provider_hint);
205 -
206 - if ($hint === 'gemini' && (strpos($lower, 'overloaded') !== false
207 - || strpos($lower, 'high demand') !== false
208 - || strpos($lower, 'model is overloaded') !== false)) {
209 - return true;
210 - }
211 - if ($hint === 'openai' && (strpos($lower, 'rate limit reached') !== false
212 - || strpos($lower, '"type":"server_error"') !== false
213 - || strpos($lower, '"code":"server_error"') !== false)) {
214 - return true;
215 - }
216 - if ($hint === 'anthropic' && (strpos($lower, '"type":"overloaded_error"') !== false
217 - || strpos($lower, 'overloaded_error') !== false)) {
218 - return true;
219 - }
220 - if (($hint === 'xai' || $hint === 'grok') && strpos($lower, 'rate limit') !== false) {
221 - return true;
222 - }
223 -
224 - return false;
225 -}
226 -
227 -/**
228 - * Whether transient-error auto-retry is enabled in admin settings.
229 - * Default true unless explicitly set to '0'. Used by both wp_remote_post
230 - * (mxchat_provider_call_with_retry) and cURL streaming paths.
231 - */
232 -private function mxchat_retry_enabled() {
233 - $opts = is_array($this->options ?? null) ? $this->options : array();
234 - return !isset($opts['auto_retry_on_transient_error']) ||
235 - (string) $opts['auto_retry_on_transient_error'] !== '0';
236 -}
237 -
238 -private function setup_streaming_headers() {
239 - if ($this->streaming_headers_sent || headers_sent()) {
240 - return false;
241 - }
242 -
243 - // Disable output buffering
244 - while (ob_get_level()) {
245 - ob_end_flush();
246 - }
247 -
248 - // Set headers for SSE
249 - header('Content-Type: text/event-stream');
250 - header('Cache-Control: no-cache');
251 - header('Connection: keep-alive');
252 - header('X-Accel-Buffering: no');
253 -
254 - ob_implicit_flush(true);
255 - flush();
256 -
257 - $this->streaming_headers_sent = true;
258 - return true;
259 -}
260 -
261 -/**
262 - * Class constructor
263 - */
264 -public function __construct() {
265 - $this->options = get_option('mxchat_options');
266 - $this->prompts_options = get_option('mxchat_prompts_options', array());
267 - $this->chat_count = get_option('mxchat_chat_count', 0);
268 - $this->word_handler = new MXChat_Word_Handler($this->options);
269 -
270 - // Add all action hooks
271 - add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
272 - add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
273 - add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
274 - add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
275 - add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
276 -
277 - // Add the AJAX actions for checking if the pre-chat message was dismissed
278 - add_action('wp_ajax_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
279 - add_action('wp_ajax_nopriv_mxchat_check_pre_chat_message_status', array($this, 'mxchat_check_pre_chat_message_status'));
280 - add_action('wp_ajax_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
281 - add_action('wp_ajax_nopriv_mxchat_fetch_conversation_history', [$this, 'mxchat_fetch_conversation_history']);
282 - add_action('wp_ajax_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
283 - add_action('wp_ajax_nopriv_mxchat_add_to_cart', [$this, 'mxchat_add_to_cart']);
284 -
285 - // Add REST API routes registration
286 - add_action('rest_api_init', array($this, 'register_routes'));
287 - add_action('wp_ajax_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
288 - add_action('wp_ajax_nopriv_mxchat_fetch_new_messages', array($this, 'mxchat_fetch_new_messages'));
289 -
290 - // Rate limit action - notice we removed the old schedule setup
291 - add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
292 -
293 - // File upload and handling actions
294 - add_action('wp_ajax_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
295 - add_action('wp_ajax_nopriv_mxchat_upload_pdf', [$this, 'handle_pdf_upload']);
296 - add_action('wp_ajax_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
297 - add_action('wp_ajax_nopriv_mxchat_remove_pdf', [$this, 'handle_pdf_remove']);
298 -
299 - // Word document handling actions
300 - add_action('wp_ajax_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
301 - add_action('wp_ajax_nopriv_mxchat_upload_word', array($this, 'mxchat_handle_word_upload'));
302 - add_action('wp_ajax_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
303 - add_action('wp_ajax_nopriv_mxchat_remove_word', array($this, 'mxchat_handle_word_remove'));
304 - add_action('wp_ajax_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
305 - add_action('wp_ajax_nopriv_mxchat_check_word_status', array($this, 'mxchat_check_word_status'));
306 -
307 - // Email handling actions
308 - add_action('wp_ajax_nopriv_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
309 - add_action('wp_ajax_mxchat_handle_save_email_and_response', [$this, 'mxchat_handle_save_email_and_response']);
310 - add_action('wp_ajax_nopriv_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
311 - add_action('wp_ajax_mxchat_check_email_provided', [$this, 'mxchat_check_email_provided']);
312 -
313 - add_action('wp_ajax_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
314 - add_action('wp_ajax_nopriv_mxchat_stream_chat', array($this, 'mxchat_handle_chat_request'));
315 -
316 - // Testing panel AJAX actions
317 - add_action('wp_ajax_mxchat_get_system_info', array($this, 'mxchat_get_system_info'));
318 - add_action('wp_ajax_mxchat_get_similarity_threshold', array($this, 'mxchat_get_similarity_threshold'));
319 - add_action('wp_ajax_mxchat_get_kb_status', array($this, 'mxchat_get_kb_status'));
320 - add_action('wp_ajax_mxchat_start_fresh_session', array($this, 'mxchat_start_fresh_session'));
321 - // Add to your existing constructor, in the section with other AJAX actions:
322 - add_action('wp_ajax_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
323 - add_action('wp_ajax_nopriv_mxchat_track_url_click', array($this, 'mxchat_track_url_click'));
324 - add_action('wp_ajax_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
325 - add_action('wp_ajax_nopriv_mxchat_track_originating_page', array($this, 'mxchat_track_originating_page'));
326 - // Add chat mode checking actions
327 - add_action('wp_ajax_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
328 - add_action('wp_ajax_nopriv_mxchat_get_current_chat_mode', array($this, 'mxchat_get_current_chat_mode'));
329 -
330 - // Nonce refresh for page-cache compatibility (WP Rocket, LiteSpeed, etc.)
331 - add_action('wp_ajax_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
332 - add_action('wp_ajax_nopriv_mxchat_refresh_nonce', array($this, 'mxchat_refresh_nonce'));
333 -
334 - // Auto-email transcript action
335 - add_action('mxchat_send_delayed_transcript', array($this, 'mxchat_send_delayed_transcript'), 10, 1);
336 -
337 - add_filter('mxchat_check_actions_only', array($this, 'check_actions_for_addons'), 10, 4);
338 -
339 -
340 -}
341 -
342 -/**
343 - * Return a fresh nonce so cached pages can replace the stale one.
344 - * With `with_settings`, also returns the current behavior-gate settings so
345 - * the widget can correct stale inline-localized values (plan-32db95).
346 - */
347 -public function mxchat_refresh_nonce() {
348 - nocache_headers();
349 - $payload = array('nonce' => wp_create_nonce('mxchat_chat_nonce'));
350 - if (!empty($_REQUEST['with_settings'])) {
351 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
352 - }
353 - wp_send_json_success($payload);
354 -}
355 -
356 -/**
357 - * Behavior-gate settings the widget may re-fetch at runtime (plan-32db95).
358 - *
359 - * Every widget setting ships inline in page HTML via wp_localize_script, so
360 - * full-page caches (host caches, WP Rocket, LiteSpeed, W3TC, FlyingPress,
361 - * WP Super Cache, Cloudflare APO, the browser itself) keep serving a stale
362 - * snapshot after an admin changes a setting. MxChat_Cache_Purge clears the
363 - * caches PHP can reach; this payload covers the rest — the widget requests
364 - * it on first open (via the nonce-refresh endpoints) and merges it over
365 - * `mxchatChat`, the same distrust-cached-HTML pattern the 3.2.7 per-request
366 - * nonce uses.
367 - *
368 - * Behavior gates + labels ONLY — colors stay inline because they're also
369 - * server-inline-styled, and a runtime swap would visibly flash.
370 - *
371 - * Both wp_localize_script blocks merge this exact array, so the inline and
372 - * refreshed payloads cannot drift.
373 - *
374 - * @param bool $fresh Re-read mxchat_options from the DB (endpoint paths)
375 - * instead of trusting the instance copy.
376 - * @return array
377 - */
378 -public function get_dynamic_widget_settings($fresh = false) {
379 - $options = $fresh ? get_option('mxchat_options', array()) : $this->options;
380 - if (!is_array($options)) {
381 - $options = array();
382 - }
383 - return array(
384 - 'model' => isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest',
385 - 'enable_streaming_toggle' => isset($options['enable_streaming_toggle']) ? $options['enable_streaming_toggle'] : 'on',
386 - 'rate_limit_message' => $options['rate_limit_message'] ?? 'Rate limit exceeded. Please try again later.',
387 - 'chat_toolbar_toggle' => $options['chat_toolbar_toggle'] ?? 'off',
388 - 'print_button_enabled' => $options['print_button_enabled'] ?? 'on',
389 - 'print_button_label' => esc_html__('Download Transcript', 'mxchat'),
390 - // "Start new chat" header-menu item (plan ac2e81). Default OFF.
391 - 'reset_chat_enabled' => $options['reset_chat_enabled'] ?? 'off',
392 - 'reset_chat_label' => !empty($options['reset_chat_label']) ? esc_html($options['reset_chat_label']) : esc_html__('Start new chat', 'mxchat'),
393 - 'reset_chat_confirm' => esc_html__('Start a new chat? This clears the current conversation.', 'mxchat'),
394 - 'stop_button_label' => esc_html__('Stop response', 'mxchat'),
395 - 'print_header_title' => esc_html(get_bloginfo('name')) . ' — ' . esc_html__('Chat transcript', 'mxchat'),
396 - // Emit 'on'/'off' STRINGS, never booleans: wp_localize_script casts
397 - // scalars to string, and (string) false === '' — which the widget's
398 - // old gate read as enabled (plan-4bba64). The filter keeps its
399 - // boolean contract; only the emitted value is stringified.
400 - 'satisfaction_rating_enabled' => apply_filters(
401 - 'mxchat_satisfaction_rating_enabled',
402 - ($options['satisfaction_rating_enabled'] ?? 'off') === 'on'
403 - ) ? 'on' : 'off',
404 - 'satisfaction_rating_idle_seconds' => max(5, min(600, intval($options['satisfaction_rating_idle_seconds'] ?? 60))),
405 - 'satisfaction_rating_copy' => array(
406 - 'question' => !empty($options['satisfaction_rating_question']) ? esc_html($options['satisfaction_rating_question']) : esc_html__('Was this helpful?', 'mxchat'),
407 - 'helpful' => esc_html__('Helpful', 'mxchat'),
408 - 'not_helpful' => esc_html__('Not helpful', 'mxchat'),
409 - 'dismiss' => esc_html__('Dismiss', 'mxchat'),
410 - 'thanks' => !empty($options['satisfaction_rating_thanks']) ? esc_html($options['satisfaction_rating_thanks']) : esc_html__('Thanks! Anything we should improve? (optional)', 'mxchat'),
411 - 'placeholder' => !empty($options['satisfaction_rating_placeholder']) ? esc_html($options['satisfaction_rating_placeholder']) : esc_html__('Tell us what could be better…', 'mxchat'),
412 - 'send' => esc_html__('Send', 'mxchat'),
413 - 'skip' => esc_html__('Skip', 'mxchat'),
414 - 'saved' => !empty($options['satisfaction_rating_saved']) ? esc_html($options['satisfaction_rating_saved']) : esc_html__('Thanks for the feedback.', 'mxchat'),
415 - ),
416 - );
417 -}
418 -
419 -// In your core plugin's check_actions_for_addons method:
420 -public function check_actions_for_addons($default, $message, $user_id, $session_id) {
421 - //error_log('MxChat Core: check_actions_for_addons called with message: ' . $message);
422 -
423 - $result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
424 -
425 - //error_log('MxChat Core: Intent check result = ' . ($result === false ? 'false' : 'true'));
426 -
427 - return $result;
428 -}
429 -
430 - private function mxchat_increment_chat_count() {
431 - $chat_count = get_option('mxchat_chat_count', 0);
432 - $chat_count++;
433 - update_option('mxchat_chat_count', $chat_count);
434 - }
435 -
436 -function mxchat_fetch_conversation_history() {
437 - if (empty($_POST['session_id'])) {
438 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
439 - wp_die();
440 - }
441 -
442 - $session_id = sanitize_text_field($_POST['session_id']);
443 -
444 - // SECURITY FIX: Verify session ownership before retrieving data
445 - // If IP/user changed, signal frontend to reset session instead of blocking
446 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
447 -
448 - // Check if this session has an owner recorded
449 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
450 -
451 - // Update session owner if it changed (e.g. IP changed due to network switch)
452 - // The session ID itself is the authentication — if the client has it, they own it
453 - if (!$session_owner || $session_owner !== $current_user_identifier) {
454 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
455 - }
456 -
457 - $history = get_option("mxchat_history_{$session_id}", []); // Retrieve stored history
458 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai'); // Get current chat mode
459 -
460 - if (empty($history)) {
461 - // Even if history is empty, return the chat mode
462 - wp_send_json_success([
463 - 'conversation' => [],
464 - 'chat_mode' => $chat_mode
465 - ]);
466 - wp_die();
467 - }
468 -
469 - wp_send_json_success([
470 - 'conversation' => $history,
471 - 'chat_mode' => $chat_mode
472 - ]);
473 - wp_die();
474 -}
475 -private function mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp = 0) {
476 - $history = get_option("mxchat_history_{$session_id}", []);
477 -
478 - // Check persistence setting - when OFF, only include messages from current page load
479 - $options = get_option('mxchat_options', []);
480 - $persistence_enabled = isset($options['chat_persistence_toggle']) && $options['chat_persistence_toggle'] === 'on';
481 -
482 - // Filter history when persistence is OFF to match what the user sees
483 - if (!$persistence_enabled && $session_start_timestamp > 0) {
484 - $history = array_filter($history, function($entry) use ($session_start_timestamp) {
485 - // Include messages from this page load onwards
486 - return isset($entry['timestamp']) && $entry['timestamp'] >= $session_start_timestamp;
487 - });
488 - // Re-index array after filtering
489 - $history = array_values($history);
490 - }
491 -
492 - $formatted_history = [];
493 -
494 - // Adjusted for code-heavy conversations
495 - $max_tokens = 120000; // Context window size
496 - $reserved_tokens = 5000; // Space for system prompts + current query
497 - $current_token_count = 0;
498 -
499 - // Allowed HTML tags for content sanitization
500 - $allowed_tags = [
501 - 'pre' => ['class' => true],
502 - 'code' => ['class' => true],
503 - 'span' => ['class' => true],
504 - 'div' => ['class' => true],
505 - 'strong' => [],
506 - 'em' => []
507 - ];
508 -
509 - foreach (array_reverse($history) as $entry) {
510 - // Preserve code blocks while sanitizing other HTML
511 - $clean_content = wp_kses($entry['content'], $allowed_tags);
512 -
513 - // Detect code blocks in content
514 - $has_code = false;
515 -// Replace the HTML check with:
516 -// Allow messages that contain code blocks or are plain text
517 -if (strpos($clean_content, '<pre') === false &&
518 - strpos($clean_content, '<code') === false &&
519 - $clean_content !== strip_tags($entry['content'])) {
520 - continue;
521 -}
522 -
523 - // Skip entries that lost significant content during sanitization
524 - if (!$has_code && $clean_content !== strip_tags($entry['content'])) {
525 - continue;
526 - }
527 -
528 - // More accurate token estimation (1 token ≈ 4 characters)
529 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
530 -
531 - // Check token budget with the new estimate
532 - if (($current_token_count + $token_estimate + $reserved_tokens) > $max_tokens) {
533 - // Try to fit partial content if it's the first entry
534 - if (empty($formatted_history)) {
535 - $clean_content = mb_substr($clean_content, 0, ($max_tokens - $reserved_tokens) * 4);
536 - $token_estimate = ceil(mb_strlen($clean_content, 'UTF-8') / 4);
537 - } else {
538 - break;
539 - }
540 - }
541 -
542 - // Add to formatted history
543 - $formatted_history[] = [
544 - 'role' => $entry['role'],
545 - 'content' => $clean_content
546 - ];
547 -
548 - $current_token_count += $token_estimate;
549 - }
550 -
551 - // Reverse back to maintain chronological order
552 - $formatted_history = array_reverse($formatted_history);
553 -
554 - // Add system message about code context
555 - array_unshift($formatted_history, [
556 - 'role' => 'system',
557 - 'content' => 'Preserved code blocks are marked with [CODE BLOCK PRESERVED]. '
558 - . 'Maintain formatting and syntax highlighting when referencing code.'
559 - ]);
560 -
561 - return $formatted_history;
562 -}
563 -
564 -public function register_routes() {
565 - //error_log(esc_html__('Registering MxChat REST routes', 'mxchat'));
566 -
567 - // Per-request chat-send nonce endpoint — issues a fresh nonce on demand
568 - // so the chat widget never depends on a stale nonce embedded in cached HTML.
569 - // Public (no auth), rate-limited (1 call / IP / second via a transient).
570 - register_rest_route('mxchat/v1', '/nonce', [
571 - 'methods' => 'GET',
572 - 'callback' => [$this, 'mxchat_issue_chat_send_nonce'],
573 - 'permission_callback' => '__return_true',
574 - ]);
575 -
576 - register_rest_route('mxchat/v1', '/stream', [
577 - 'methods' => 'GET',
578 - 'callback' => [$this, 'mxchat_stream_events'],
579 - 'permission_callback' => [$this, 'verify_chat_session'],
580 - ]);
581 -
582 - register_rest_route('mxchat/v1', '/agent-response', [
583 - 'methods' => 'POST',
584 - 'callback' => [$this, 'mxchat_handle_agent_response'],
585 - 'permission_callback' => [$this, 'verify_slack_request'],
586 - ]);
587 -
588 - register_rest_route('mxchat/v1', '/slack-interaction', [
589 - 'methods' => 'POST',
590 - 'callback' => [$this, 'handle_slack_interaction'],
591 - 'permission_callback' => [$this, 'verify_slack_request'],
592 - ]);
593 -
594 - register_rest_route('mxchat/v1', '/slack-messages', [
595 - 'methods' => 'POST',
596 - 'callback' => [$this, 'handle_slack_messages'],
597 - 'permission_callback' => [$this, 'verify_slack_request'],
598 - ]);
599 -
600 - // Telegram webhook endpoint
601 - register_rest_route('mxchat/v1', '/telegram-webhook', [
602 - 'methods' => 'POST',
603 - 'callback' => [$this, 'handle_telegram_webhook'],
604 - 'permission_callback' => [$this, 'verify_telegram_request'],
605 - ]);
606 -
607 - //error_log(esc_html__('MxChat REST routes registered', 'mxchat'));
608 -}
609 -
610 -/**
611 - * Issue a fresh per-request nonce for chat-send. Returned to the widget which
612 - * caches it for the session and includes it on every chat-send / stream-send /
613 - * upload call. By moving the nonce out of inline `window.mxchatChat = {...}` HTML
614 - * we eliminate the entire class of "first-message Access denied" failures that
615 - * plague WP installs behind a full-page cache (WP Rocket, LiteSpeed, FlyingPress,
616 - * W3 Total Cache, Cloudflare APO) — the nonce is never cached because it never
617 - * lives in the HTML body.
618 - *
619 - * Public endpoint. Rate-limited to 1 call / IP / 1s via a transient so a single
620 - * client browser can't be used to flood the nonce-issuance path.
621 - *
622 - * Nonce action: `mxchat_chat_send` (new). The chat-send AJAX handlers accept
623 - * BOTH this action AND the legacy `mxchat_chat_nonce` action for a 30-day
624 - * backwards-compat window so cached pages still in users' browsers don't break
625 - * mid-session.
626 - *
627 - * @since 3.2.7
628 - */
629 -public function mxchat_issue_chat_send_nonce(WP_REST_Request $request) {
630 - $ip = '';
631 - if (!empty($_SERVER['REMOTE_ADDR'])) {
632 - $ip = preg_replace('#[^0-9a-fA-F:\.]#', '', wp_unslash((string) $_SERVER['REMOTE_ADDR']));
633 - }
634 - if ($ip !== '') {
635 - // Best-effort rate limit. WP transients with sub-second TTL are racy
636 - // (parallel bursts can squeak through before set_transient completes);
637 - // we use 2s to make the gate slightly more reliable. Real production
638 - // rate-limiting at sub-second granularity needs Redis or DB row locks
639 - // — out of scope for this endpoint, which is already cheap.
640 - $key = 'mxchat_nonce_rl_' . md5($ip);
641 - if (get_transient($key)) {
642 - return new WP_REST_Response(array(
643 - 'error' => 'rate_limited',
644 - 'message' => __('Too many nonce requests. Try again shortly.', 'mxchat'),
645 - ), 429);
646 - }
647 - set_transient($key, 1, 2);
648 - }
649 -
650 - // The widget calls this endpoint without an X-WP-Nonce header, so WordPress does not
651 - // honor the auth cookie and the request runs as uid=0 even for logged-in users. That
652 - // makes wp_create_nonce() bind the nonce to uid=0, which then fails wp_verify_nonce()
653 - // at admin-ajax (which runs as the real uid) -> logged-in users get a 403 on upload.
654 - // Resolve the real user from the logged_in cookie so the nonce binds to the correct uid.
655 - if ( ! is_user_logged_in() ) {
656 - $maybe_uid = wp_validate_auth_cookie( '', 'logged_in' );
657 - if ( $maybe_uid ) {
658 - wp_set_current_user( $maybe_uid );
659 - }
660 - }
661 -
662 - $payload = array(
663 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
664 - 'expires_in' => 86400, // WP nonces live 24h; widget caches for 12h conservatively.
665 - );
666 -
667 - // plan-32db95: the widget's first-open refresh asks for current behavior
668 - // settings in the same round-trip, so stale inline-localized values on
669 - // cached pages get corrected without a second request. All values in
670 - // this payload already ship in public page HTML — nothing sensitive.
671 - if ($request->get_param('with_settings')) {
672 - $payload['settings'] = $this->get_dynamic_widget_settings(true);
673 - }
674 -
675 - return new WP_REST_Response($payload, 200);
676 -}
677 -
678 -/**
679 - * Verify a chat-send nonce. Accepts BOTH the new `mxchat_chat_send` action
680 - * (issued by /wp-json/mxchat/v1/nonce) AND the legacy `mxchat_chat_nonce`
681 - * action (inline-localized in older cached HTML). The legacy acceptance is
682 - * a 30-day backwards-compat window — to be removed in a follow-up release
683 - * after 2026-06-27.
684 - *
685 - * @param string $posted_nonce
686 - * @return bool
687 - */
688 -public static function mxchat_verify_chat_send_nonce($posted_nonce) {
689 - if (!is_string($posted_nonce) || $posted_nonce === '') {
690 - return false;
691 - }
692 - return (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_send')
693 - || (bool) wp_verify_nonce($posted_nonce, 'mxchat_chat_nonce');
694 -}
695 -
696 -/**
697 - * Verify valid chat session
698 - */
699 -public function verify_chat_session($request) {
700 - $session_id = $request->get_param('session_id');
701 - if (empty($session_id)) {
702 - //error_log(esc_html__('Empty session ID in chat request', 'mxchat'));
703 - return false;
704 - }
705 -
706 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
707 - return $chat_mode === 'agent';
708 -}
709 -
710 -/**
711 - * Verify request is coming from Slack.
712 - *
713 - * @param WP_REST_Request $request
714 - * @return bool True if valid, false otherwise.
715 - */
716 -public function verify_slack_request($request) {
717 - // Get the Slack signing secret from your plugin options
718 - $valid_key = $this->options['live_agent_secret_key'] ?? '';
719 -
720 - if (empty($valid_key)) {
721 - //error_log(esc_html__('Slack signing secret not configured', 'mxchat'));
722 - return false;
723 - }
724 -
725 - $timestamp = $request->get_header('X-Slack-Request-Timestamp');
726 - $slack_signature = $request->get_header('X-Slack-Signature');
727 -
728 - // Verify timestamp to prevent replay attacks
729 - if (abs(time() - intval($timestamp)) > 300) {
730 - //error_log(esc_html__('Slack request timestamp too old', 'mxchat'));
731 - return false;
732 - }
733 -
734 - // Get raw request body from the WP_REST_Request object
735 - // (php://input may already be consumed by WordPress at this point)
736 - $request_body = $request->get_body();
737 -
738 - // Create the signature base string
739 - $sig_basestring = "v0:{$timestamp}:{$request_body}";
740 -
741 - // Calculate expected signature
742 - $my_signature = 'v0=' . hash_hmac('sha256', $sig_basestring, $valid_key);
743 -
744 - // Compare signatures
745 - return hash_equals($my_signature, $slack_signature);
746 -}
747 -
748 -/**
749 - * Verify request is coming from Telegram.
750 - *
751 - * @param WP_REST_Request $request
752 - * @return bool True if valid, false otherwise.
753 - */
754 -public function verify_telegram_request($request) {
755 - $secret_token = $this->options['telegram_webhook_secret'] ?? '';
756 -
757 - //error_log('[MxChat Telegram DEBUG] verify_telegram_request called');
758 - //error_log('[MxChat Telegram DEBUG] Stored secret: ' . (empty($secret_token) ? 'EMPTY' : substr($secret_token, 0, 10) . '...'));
759 -
760 - if (empty($secret_token)) {
761 - // If no secret is configured, allow the request (for initial setup)
762 - //error_log('[MxChat Telegram DEBUG] No secret configured, allowing request');
763 - return true;
764 - }
765 -
766 - // Telegram sends the secret token in the X-Telegram-Bot-Api-Secret-Token header
767 - $request_token = $request->get_header('X-Telegram-Bot-Api-Secret-Token');
768 -
769 - //error_log('[MxChat Telegram DEBUG] Request token: ' . (empty($request_token) ? 'EMPTY' : substr($request_token, 0, 10) . '...'));
770 -
771 - if (empty($request_token)) {
772 - //error_log('[MxChat Telegram DEBUG] Request rejected: No token in header');
773 - return false;
774 - }
775 -
776 - // Timing-safe comparison
777 - $result = hash_equals($secret_token, $request_token);
778 - //error_log('[MxChat Telegram DEBUG] Token comparison result: ' . ($result ? 'MATCH' : 'MISMATCH'));
779 - return $result;
780 -}
781 -
782 -public function mxchat_stream_events(WP_REST_Request $request) {
783 - header('Content-Type: text/event-stream');
784 - header('Cache-Control: no-cache');
785 - header('Connection: keep-alive');
786 -
787 - $session_id = sanitize_text_field($request->get_param('session_id'));
788 - $last_seen_id = sanitize_text_field($request->get_param('last_seen_id')) ?: '';
789 -
790 - if (empty($session_id)) {
791 - echo esc_html__("event: error\ndata: ", 'mxchat') . esc_html__('Missing session_id', 'mxchat') . "\n\n";
792 - flush();
793 - exit;
794 - }
795 -
796 - $history = get_option("mxchat_history_{$session_id}", []);
797 -
798 - // Filter only new messages
799 - $new_messages = array_filter($history, function ($message) use ($last_seen_id) {
800 - return !empty($message['id']) && $message['id'] > $last_seen_id;
801 - });
802 -
803 - // Send new messages if available
804 - if (!empty($new_messages)) {
805 - echo esc_html__("event: newMessages\ndata: ", 'mxchat') . json_encode(array_values($new_messages)) . "\n\n";
806 - } else {
807 - // Keep the connection alive
808 - echo esc_html__("event: keepAlive\ndata: ", 'mxchat') . "{}\n\n";
809 - }
810 - flush();
811 - exit;
812 -}
813 -
814 -
815 -
816 -
817 -private function mxchat_save_chat_message($session_id, $role, $message, $originating_page = null, $rag_context = null) {
818 - global $wpdb;
819 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
820 - //error_log("[DEBUG] mxchat_save_chat_message -> START for session_id: {$session_id}, role: {$role}");
821 -
822 - // Check if this is the first message in a new session (before any other database operations)
823 - $is_new_session = false;
824 - if ($role === 'user') { // Only check for user messages, not bot responses
825 - $existing_messages = $wpdb->get_var($wpdb->prepare(
826 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
827 - $session_id
828 - ));
829 - $is_new_session = ($existing_messages == 0);
830 -
831 - // Log for debugging
832 - if ($is_new_session) {
833 - //error_log("[DEBUG] This is a NEW session - first message");
834 - }
835 - }
836 -
837 - // SECURITY FIX: Set session ownership for new sessions
838 - if ($is_new_session && $role === 'user') {
839 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
840 - $session_owner_key = "mxchat_session_owner_{$session_id}";
841 -
842 - // Only set ownership if not already set
843 - if (!get_option($session_owner_key)) {
844 - update_option($session_owner_key, $current_user_identifier, 'no');
845 - //error_log("[DEBUG] Set session ownership for {$session_id} to {$current_user_identifier}");
846 - }
847 - }
848 -
849 - // 1) Extract agent name if present
850 - $agent_name = '';
851 - if (preg_match('/^Agent: (.*?) - /', $message, $matches)) {
852 - $agent_name = $matches[1];
853 - $message = str_replace("Agent: $agent_name - ", '', $message);
854 - $session_meta_key = "mxchat_agent_name_{$session_id}";
855 - if (empty(get_option($session_meta_key))) {
856 - update_option($session_meta_key, $agent_name);
857 - //error_log("[DEBUG] mxchat_save_chat_message -> Stored agent_name in option: {$session_meta_key} => {$agent_name}");
858 - }
859 - }
860 -
861 - // 2) Generate unique message_id
862 - $message_id = uniqid();
863 - //error_log("[DEBUG] mxchat_save_chat_message -> Generated message_id: {$message_id}");
864 -
865 - // 3) Determine user_id
866 - $user_id = is_user_logged_in() ? get_current_user_id() : 0;
867 -
868 - // 4) Determine user_identifier
869 - $user_identifier = $agent_name
870 - ? $agent_name
871 - : MxChat_User::mxchat_get_user_identifier();
872 -
873 - // 5) Determine displayed_name
874 - $user_email = MxChat_User::mxchat_get_user_email();
875 - $displayed_name = $agent_name ? $agent_name : ($user_email ?: $user_identifier);
876 -
877 - // 6) Check for a saved email in wp_options
878 - $email_option_key = "mxchat_email_{$session_id}";
879 - $saved_email = get_option($email_option_key);
880 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for email_option_key: {$email_option_key}, found: {$saved_email}");
881 -
882 - // Check for a saved name in wp_options
883 - $name_option_key = "mxchat_name_{$session_id}";
884 - $saved_name = get_option($name_option_key);
885 - //error_log("[DEBUG] mxchat_save_chat_message -> Checking wp_options for name_option_key: {$name_option_key}, found: {$saved_name}");
886 -
887 - // If found, update DB user_email and user_name
888 - if ($saved_email || $saved_name) {
889 - $update_data = [];
890 - if ($saved_email) {
891 - $update_data['user_email'] = $saved_email;
892 - }
893 - if ($saved_name) {
894 - $update_data['user_name'] = $saved_name;
895 - }
896 -
897 - if (!empty($update_data)) {
898 - $update_res = $wpdb->update(
899 - $table_name,
900 - $update_data,
901 - ['session_id' => $session_id],
902 - array_fill(0, count($update_data), '%s'),
903 - ['%s']
904 - );
905 - //error_log("[DEBUG] mxchat_save_chat_message -> Attempted DB user_email/user_name update for session_id {$session_id}. update_res: {$update_res}");
906 - }
907 - }
908 -
909 - // 7) Save to session history in wp_options
910 - $history_key = "mxchat_history_{$session_id}";
911 - $history = get_option($history_key, []);
912 - $history[] = [
913 - 'id' => $message_id,
914 - 'role' => $role,
915 - 'content' => $message,
916 - 'timestamp' => round(microtime(true) * 1000),
917 - 'agent_name' => $displayed_name,
918 - ];
919 - update_option($history_key, $history, 'no');
920 - //error_log("[DEBUG] mxchat_save_chat_message -> Updated session history in option: {$history_key}");
921 -
922 - // 8) Save the message to DB (INSERT)
923 - $insert_data = [
924 - 'user_id' => $user_id,
925 - 'user_identifier'=> $user_identifier,
926 - 'user_email' => $saved_email ?: $user_email,
927 - 'user_name' => $saved_name ?: '', // Add name to insert data
928 - 'session_id' => $session_id,
929 - 'role' => $role,
930 - 'message' => $message,
931 - 'timestamp' => current_time('mysql', 1),
932 - ];
933 -
934 - // IMPROVED: Handle originating page data
935 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
936 -
937 - if ($columns_exist) {
938 - if ($is_new_session && $role === 'user') {
939 - // For the first user message, set originating page data
940 -
941 - // First check if we have it from the parameter
942 - if ($originating_page && !empty($originating_page['url'])) {
943 - $insert_data['originating_page_url'] = $originating_page['url'];
944 - $insert_data['originating_page_title'] = $originating_page['title'] ?? '';
945 -
946 - //error_log("[DEBUG] Setting originating page from parameter: " . $originating_page['url']);
947 - }
948 - // Otherwise check if it's stored in the instance property
949 - else if (isset($this->pending_originating_page) && !empty($this->pending_originating_page['url'])) {
950 - $insert_data['originating_page_url'] = $this->pending_originating_page['url'];
951 - $insert_data['originating_page_title'] = $this->pending_originating_page['title'] ?? '';
952 -
953 - //error_log("[DEBUG] Setting originating page from pending_originating_page: " . $this->pending_originating_page['url']);
954 -
955 - // Clear after using (= null, not unset(): unset() undeclares the property
956 - // and the next assignment recreates it dynamic, re-triggering the PHP 8.2 deprecation)
957 - $this->pending_originating_page = null;
958 - }
959 - // Fallback to HTTP_REFERER if nothing else is available
960 - else if (isset($_SERVER['HTTP_REFERER'])) {
961 - $referer_url = esc_url_raw($_SERVER['HTTP_REFERER']);
962 - $insert_data['originating_page_url'] = $referer_url;
963 -
964 - // Generate title from URL
965 - $parsed_url = parse_url($referer_url);
966 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
967 -
968 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
969 - $insert_data['originating_page_title'] = 'Homepage';
970 - } else {
971 - $title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
972 - $insert_data['originating_page_title'] = ucwords(trim($title));
973 - }
974 -
975 - //error_log("[DEBUG] Setting originating page from HTTP_REFERER: " . $referer_url);
976 - }
977 -
978 - // Store for this session so all messages have the same originating page
979 - if (!empty($insert_data['originating_page_url'])) {
980 - update_option("mxchat_originating_page_{$session_id}", [
981 - 'url' => $insert_data['originating_page_url'],
982 - 'title' => $insert_data['originating_page_title']
983 - ], 'no');
984 - }
985 - } else {
986 - // For subsequent messages in the session, use the stored originating page
987 - $stored_originating = get_option("mxchat_originating_page_{$session_id}");
988 - if ($stored_originating && !empty($stored_originating['url'])) {
989 - $insert_data['originating_page_url'] = $stored_originating['url'];
990 - $insert_data['originating_page_title'] = $stored_originating['title'] ?? '';
991 - }
992 - }
993 - }
994 -
995 - // Add RAG context if provided (for bot messages)
996 - if ($rag_context !== null && $role === 'bot') {
997 - $rag_context_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'rag_context'");
998 - if ($rag_context_column_exists) {
999 - $insert_data['rag_context'] = is_array($rag_context) ? wp_json_encode($rag_context) : $rag_context;
1000 - }
1001 - }
1002 -
1003 - $wpdb->insert($table_name, $insert_data);
1004 - //error_log("[DEBUG] mxchat_save_chat_message -> Inserted message into DB. row_id: {$wpdb->insert_id}, data: " . print_r($insert_data, true));
1005 -
1006 - // 9) Send notification email if this is the first user message in a new session
1007 - if ($wpdb->insert_id && $is_new_session && $role === 'user') {
1008 - $this->send_new_chat_notification($session_id, array(
1009 - 'identifier' => $user_identifier,
1010 - 'email' => $saved_email ?: $user_email,
1011 - 'ip' => $_SERVER['REMOTE_ADDR']
1012 - ));
1013 - }
1014 -
1015 - // 10) Schedule delayed transcript email if enabled and message is from user
1016 - if ($wpdb->insert_id && $role === 'user') {
1017 - $this->schedule_delayed_transcript_email($session_id);
1018 - }
1019 -
1020 - //error_log("[DEBUG] mxchat_save_chat_message -> END for session_id: {$session_id}");
1021 - return $message_id;
1022 -}
1023 -
1024 -private function send_new_chat_notification($session_id, $user_info = array()) {
1025 - $options = get_option('mxchat_transcripts_options');
1026 -
1027 - // Check if notifications are enabled
1028 - if (empty($options['mxchat_enable_notifications'])) {
1029 - return false;
1030 - }
1031 -
1032 - // Get notification email
1033 - $to = !empty($options['mxchat_notification_email']) ?
1034 - $options['mxchat_notification_email'] :
1035 - get_option('admin_email');
1036 -
1037 - if (!is_email($to)) {
1038 - return false;
1039 - }
1040 -
1041 - // Prepare email content
1042 - $subject = sprintf('[%s] New Chat Session Started', get_bloginfo('name'));
1043 -
1044 - $user_identifier = isset($user_info['identifier']) ? $user_info['identifier'] : 'Guest';
1045 - $user_email = isset($user_info['email']) ? $user_info['email'] : 'Not provided';
1046 - $user_ip = isset($user_info['ip']) ? $user_info['ip'] : $_SERVER['REMOTE_ADDR'];
1047 -
1048 - $message = sprintf(
1049 - "A new chat session has started on your website.\n\n" .
1050 - "Session ID: %s\n" .
1051 - "User: %s\n" .
1052 - "Email: %s\n" .
1053 - "IP Address: %s\n" .
1054 - "Time: %s\n\n" .
1055 - "View transcripts: %s",
1056 - $session_id,
1057 - $user_identifier,
1058 - $user_email,
1059 - $user_ip,
1060 - current_time('mysql'),
1061 - admin_url('admin.php?page=mxchat-transcripts')
1062 - );
1063 -
1064 - // Send email
1065 - return wp_mail($to, $subject, $message);
1066 -}
1067 -
1068 -/**
1069 - * Schedule delayed transcript email for a session
1070 - * Reschedules if a new user message is received
1071 - */
1072 -private function schedule_delayed_transcript_email($session_id) {
1073 - $options = get_option('mxchat_transcripts_options');
1074 -
1075 - // Check if auto-email is enabled
1076 - if (empty($options['mxchat_auto_email_transcript_enabled'])) {
1077 - return;
1078 - }
1079 -
1080 - // Get notification email
1081 - $email = !empty($options['mxchat_notification_email']) ?
1082 - $options['mxchat_notification_email'] :
1083 - get_option('admin_email');
1084 -
1085 - if (!is_email($email)) {
1086 - return;
1087 - }
1088 -
1089 - // Get delay in minutes (default 30)
1090 - $delay_minutes = isset($options['mxchat_auto_email_transcript_delay']) ?
1091 - intval($options['mxchat_auto_email_transcript_delay']) : 30;
1092 -
1093 - // Clear any existing scheduled event for this session
1094 - $hook = 'mxchat_send_delayed_transcript';
1095 - $args = array($session_id);
1096 - $timestamp = wp_next_scheduled($hook, $args);
1097 -
1098 - if ($timestamp) {
1099 - wp_unschedule_event($timestamp, $hook, $args);
1100 - }
1101 -
1102 - // Schedule new event
1103 - $schedule_time = time() + ($delay_minutes * 60);
1104 - wp_schedule_single_event($schedule_time, $hook, $args);
1105 -}
1106 -
1107 -/**
1108 - * Check if chat messages contain contact information (email or phone number)
1109 - *
1110 - * @param array $messages Array of message objects with 'message' property
1111 - * @param object|null $session_data Session data object with user_email property
1112 - * @return bool True if contact info found, false otherwise
1113 - */
1114 -private function chat_contains_contact_info($messages, $session_data = null) {
1115 - // Check if session already has a stored email
1116 - if ($session_data && !empty($session_data->user_email)) {
1117 - return true;
1118 - }
1119 -
1120 - // Email regex pattern
1121 - $email_pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
1122 -
1123 - // Phone number patterns (covers various formats including international, WhatsApp style)
1124 - // Matches: +1234567890, (123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 234 567 8900, etc.
1125 - $phone_pattern = '/(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{2,4}[-.\s]?\d{2,4}(?:[-.\s]?\d{1,4})?/';
1126 -
1127 - // Only check user messages (not assistant responses)
1128 - foreach ($messages as $msg) {
1129 - if ($msg->role !== 'user') {
1130 - continue;
1131 - }
1132 -
1133 - $message_text = $msg->message;
1134 -
1135 - // Check for email
1136 - if (preg_match($email_pattern, $message_text)) {
1137 - return true;
1138 - }
1139 -
1140 - // Check for phone number (must be at least 7 digits total to avoid false positives)
1141 - if (preg_match($phone_pattern, $message_text, $matches)) {
1142 - // Count actual digits to avoid matching short numbers
1143 - $digits_only = preg_replace('/\D/', '', $matches[0]);
1144 - if (strlen($digits_only) >= 7) {
1145 - return true;
1146 - }
1147 - }
1148 - }
1149 -
1150 - return false;
1151 -}
1152 -
1153 -/**
1154 - * Send the delayed transcript email with .txt attachment
1155 - */
1156 -public function mxchat_send_delayed_transcript($session_id) {
1157 - global $wpdb;
1158 -
1159 - $options = get_option('mxchat_transcripts_options');
1160 -
1161 - // Get notification email
1162 - $to = !empty($options['mxchat_notification_email']) ?
1163 - $options['mxchat_notification_email'] :
1164 - get_option('admin_email');
1165 -
1166 - if (!is_email($to)) {
1167 - return false;
1168 - }
1169 -
1170 - // Get all messages for this session
1171 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1172 - $messages = $wpdb->get_results($wpdb->prepare(
1173 - "SELECT role, message, timestamp FROM {$table_name}
1174 - WHERE session_id = %s
1175 - ORDER BY timestamp ASC",
1176 - $session_id
1177 - ));
1178 -
1179 - if (empty($messages)) {
1180 - return false;
1181 - }
1182 -
1183 - // Get session metadata
1184 - $sessions_table = $wpdb->prefix . 'mxchat_sessions';
1185 - $session_data = $wpdb->get_row($wpdb->prepare(
1186 - "SELECT * FROM {$sessions_table} WHERE session_id = %s",
1187 - $session_id
1188 - ));
1189 -
1190 - // Check if contact info is required and if it's present
1191 - $require_contact = !empty($options['mxchat_auto_email_transcript_require_contact']);
1192 - if ($require_contact && !$this->chat_contains_contact_info($messages, $session_data)) {
1193 - // Contact info required but not found - skip sending
1194 - return false;
1195 - }
1196 -
1197 - // Build transcript content
1198 - $transcript_content = "Chat Transcript\n";
1199 - $transcript_content .= "================\n\n";
1200 - $transcript_content .= "Session ID: " . $session_id . "\n";
1201 -
1202 - if ($session_data) {
1203 - $transcript_content .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1204 - $transcript_content .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1205 - $transcript_content .= "Started: " . $session_data->created_at . "\n";
1206 - }
1207 -
1208 - $transcript_content .= "\n" . str_repeat("=", 50) . "\n\n";
1209 -
1210 - // Add messages
1211 - foreach ($messages as $msg) {
1212 - $role_label = ($msg->role === 'user') ? 'User' : 'Assistant';
1213 - $transcript_content .= "[{$msg->timestamp}] {$role_label}:\n";
1214 - $transcript_content .= $msg->message . "\n\n";
1215 - }
1216 -
1217 - // Create temporary file for attachment using WP_Filesystem
1218 - $upload_dir = wp_upload_dir();
1219 - $temp_file = $upload_dir['basedir'] . '/mxchat-transcript-' . $session_id . '.txt';
1220 - global $wp_filesystem;
1221 - if (empty($wp_filesystem)) {
1222 - require_once ABSPATH . 'wp-admin/includes/file.php';
1223 - WP_Filesystem();
1224 - }
1225 - $wp_filesystem->put_contents($temp_file, $transcript_content, FS_CHMOD_FILE);
1226 -
1227 - // Prepare email
1228 - $subject = sprintf('[%s] Chat Transcript - Session %s', get_bloginfo('name'), substr($session_id, 0, 8));
1229 -
1230 - $message = "Please find attached the full chat transcript.\n\n";
1231 - $message .= "Session ID: {$session_id}\n";
1232 -
1233 - if ($session_data) {
1234 - $message .= "User: " . ($session_data->user_identifier ?: 'Guest') . "\n";
1235 - $message .= "Email: " . ($session_data->user_email ?: 'Not provided') . "\n";
1236 - }
1237 -
1238 - $message .= "\nView online: " . admin_url('admin.php?page=mxchat-transcripts');
1239 -
1240 - // Send email with attachment
1241 - $attachments = array($temp_file);
1242 - $result = wp_mail($to, $subject, $message, '', $attachments);
1243 -
1244 - // Clean up temporary file
1245 - if (file_exists($temp_file)) {
1246 - unlink($temp_file);
1247 - }
1248 -
1249 - return $result;
1250 -}
1251 -
1252 -
1253 -
1254 -public function mxchat_handle_save_email_and_response() {
1255 - //error_log('[DEBUG] ---------- mxchat_handle_save_email_and_response START ----------');
1256 - //error_log('DEBUG: POST data: ' . print_r($_POST, true));
1257 -
1258 - nocache_headers();
1259 -
1260 - // Validate nonce
1261 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1262 - //error_log(esc_html__('[ERROR] Invalid nonce in mxchat_handle_save_email_and_response', 'mxchat'));
1263 - wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'mxchat')]);
1264 - wp_die();
1265 - }
1266 -
1267 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1268 - $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
1269 - $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
1270 -
1271 - //error_log("[DEBUG] handle_save_email_and_response -> session_id: {$session_id}, email: {$email}, name: {$name}");
1272 -
1273 - if (empty($session_id) || $session_id === 'null' || empty($email)) {
1274 - //error_log("[ERROR] Missing session_id or email: session_id={$session_id}, email={$email}");
1275 - wp_send_json_error(['message' => esc_html__('Session ID or email is missing.', 'mxchat')]);
1276 - wp_die();
1277 - }
1278 -
1279 - // Validate name if provided (check if name field is enabled and name is required)
1280 - $options = get_option('mxchat_options', []);
1281 - $name_field_enabled = isset($options['enable_name_field']) &&
1282 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1283 -
1284 - if ($name_field_enabled && (empty($name) || strlen(trim($name)) < 2 || strlen(trim($name)) > 100)) {
1285 - //error_log("[ERROR] Invalid name: {$name} (enabled: {$name_field_enabled})");
1286 - wp_send_json_error(['message' => esc_html__('Name must be between 2 and 100 characters.', 'mxchat')]);
1287 - wp_die();
1288 - }
1289 -
1290 - // 1) Always store email in wp_options
1291 - $email_option_key = "mxchat_email_{$session_id}";
1292 - update_option($email_option_key, $email, 'no');
1293 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$email_option_key} => {$email}");
1294 -
1295 - // Store name in wp_options if provided
1296 - if (!empty($name)) {
1297 - $name_option_key = "mxchat_name_{$session_id}";
1298 - update_option($name_option_key, $name, 'no');
1299 - //error_log("[DEBUG] handle_save_email_and_response -> updated option: {$name_option_key} => {$name}");
1300 - }
1301 -
1302 - // 2) (Optional) Also store in DB if a row already exists
1303 - global $wpdb;
1304 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1305 -
1306 - // Make sure we have a valid placeholder in prepare
1307 - $sql = $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE session_id = %s", $session_id);
1308 - $session_count = $wpdb->get_var($sql);
1309 -
1310 - //error_log("[DEBUG] handle_save_email_and_response -> session_count for {$session_id}: {$session_count} (SQL: {$sql})");
1311 -
1312 - if ($session_count) {
1313 - // Update both user_email and user_name if row(s) exist
1314 - if (!empty($name)) {
1315 - $update_sql = $wpdb->prepare(
1316 - "UPDATE {$table_name} SET user_email = %s, user_name = %s WHERE session_id = %s",
1317 - $email,
1318 - $name,
1319 - $session_id
1320 - );
1321 - } else {
1322 - $update_sql = $wpdb->prepare(
1323 - "UPDATE {$table_name} SET user_email = %s WHERE session_id = %s",
1324 - $email,
1325 - $session_id
1326 - );
1327 - }
1328 - $wpdb->query($update_sql);
1329 - //error_log("[DEBUG] handle_save_email_and_response -> DB updated: {$update_sql}");
1330 - } else {
1331 - //error_log("[INFO] handle_save_email_and_response -> No DB entry for {$session_id}, so email/name is only in wp_options.");
1332 - }
1333 -
1334 - // Provide success response (same as original)
1335 - $bot_message = __('Thanks for providing your email! You can continue chatting now.', 'mxchat');
1336 - //error_log("[DEBUG] handle_save_email_and_response -> success, returning bot_message: {$bot_message}");
1337 - wp_send_json_success(['message' => $bot_message]);
1338 - wp_die();
1339 -}
1340 -
1341 -public function mxchat_check_email_provided() {
1342 - //error_log('[DEBUG] ---------- mxchat_check_email_provided START ----------');
1343 -
1344 - nocache_headers();
1345 -
1346 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
1347 - //error_log('[ERROR] Invalid nonce in mxchat_check_email_provided');
1348 - wp_send_json_error(['message' => esc_html__('Invalid nonce', 'mxchat')]);
1349 - }
1350 -
1351 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1352 - if (empty($session_id) || $session_id === 'null') {
1353 - //error_log('[ERROR] No session ID provided in mxchat_check_email_provided');
1354 - wp_send_json_error(['message' => esc_html__('No session ID provided', 'mxchat')]);
1355 - }
1356 -
1357 - // Check if the user is logged in
1358 - if (is_user_logged_in()) {
1359 - $current_user = wp_get_current_user();
1360 - //error_log("[DEBUG] User is logged in as {$current_user->user_email}");
1361 -
1362 - // Get user's display name for logged in users
1363 - $user_name = !empty($current_user->display_name) ? $current_user->display_name :
1364 - (!empty($current_user->first_name) ? $current_user->first_name : '');
1365 -
1366 - $response_data = ['logged_in' => true, 'email' => $current_user->user_email];
1367 - if (!empty($user_name)) {
1368 - $response_data['name'] = $user_name;
1369 - }
1370 -
1371 - wp_send_json_success($response_data);
1372 - }
1373 -
1374 - // Check if name field is required
1375 - $options = get_option('mxchat_options', []);
1376 - $name_field_enabled = isset($options['enable_name_field']) &&
1377 - ($options['enable_name_field'] === '1' || $options['enable_name_field'] === 'on');
1378 -
1379 - $email_option_key = "mxchat_email_{$session_id}";
1380 - $stored_email = get_option($email_option_key, '');
1381 -
1382 - // Check for stored name
1383 - $name_option_key = "mxchat_name_{$session_id}";
1384 - $stored_name = get_option($name_option_key, '');
1385 -
1386 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking email option: {$email_option_key}, found: {$stored_email}");
1387 - //error_log("[DEBUG] mxchat_check_email_provided -> Checking name option: {$name_option_key}, found: {$stored_name}, required: " . ($name_field_enabled ? 'yes' : 'no'));
1388 -
1389 - // Check if we have email and name (if name is required)
1390 - $has_required_info = !empty($stored_email);
1391 -
1392 - if ($name_field_enabled) {
1393 - $has_required_info = $has_required_info && !empty($stored_name);
1394 - }
1395 -
1396 - if ($has_required_info) {
1397 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info found, returning success");
1398 -
1399 - $response_data = ['email' => $stored_email];
1400 - if (!empty($stored_name)) {
1401 - $response_data['name'] = $stored_name;
1402 - }
1403 -
1404 - wp_send_json_success($response_data);
1405 - } else {
1406 - //error_log("[DEBUG] mxchat_check_email_provided -> Required info missing, returning error");
1407 - wp_send_json_error(['message' => esc_html__('No email found', 'mxchat')]);
1408 - }
1409 -}
1410 -
1411 -/**
1412 - * Send error response in appropriate format based on streaming mode
1413 - * ADDED: Helper method to consistently handle errors in both streaming and non-streaming modes
1414 - *
1415 - * @param string $error_message The error message to display
1416 - * @param string $error_code Optional error code for debugging
1417 - */
1418 -private function send_error_response($error_message, $error_code = 'api_error') {
1419 - if ($this->is_streaming) {
1420 - echo "data: " . json_encode([
1421 - 'error' => true,
1422 - 'error_message' => $error_message,
1423 - 'error_code' => $error_code,
1424 - 'text' => $error_message,
1425 - 'message' => $error_message
1426 - ]) . "\n\n";
1427 - echo "data: [DONE]\n\n";
1428 - flush();
1429 - } else {
1430 - wp_send_json_error([
1431 - 'error_message' => $error_message,
1432 - 'error_code' => $error_code
1433 - ]);
1434 - }
1435 - wp_die();
1436 -}
1437 -
1438 -public function mxchat_handle_chat_request() {
1439 - global $wpdb;
1440 -
1441 - // Debug: Log incoming bot_id
1442 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
1443 - //error_log("=== MXCHAT DEBUG: Starting chat request ===");
1444 - //error_log("MXCHAT DEBUG: Bot ID received: " . $bot_id);
1445 -
1446 - // Get bot-specific options
1447 - $bot_options = $this->get_bot_options($bot_id);
1448 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
1449 -
1450 - // Check if this is a streaming request
1451 - // Allow force_streaming_test parameter to bypass the setting check (for admin compatibility testing)
1452 - $force_streaming_test = isset($_POST['force_streaming_test']) && $_POST['force_streaming_test'] === '1' && current_user_can('administrator');
1453 - $is_streaming = isset($_POST['action']) && $_POST['action'] === 'mxchat_stream_chat' &&
1454 - ($force_streaming_test || (isset($current_options['enable_streaming_toggle']) && $current_options['enable_streaming_toggle'] === 'on'));
1455 -
1456 - // ADDED: Store streaming state in class property for use in private methods
1457 - $this->is_streaming = $is_streaming;
1458 -
1459 - // NOTE: Streaming headers are now set later via setup_streaming_headers()
1460 - // This allows actions/forms to return JSON responses without header conflicts
1461 -
1462 - // Check if MX Chat Moderation is active
1463 - if (class_exists('MX_Chat_Moderation')) {
1464 - // Get user email and IP
1465 - $user_email = '';
1466 - $user_ip = $_SERVER['REMOTE_ADDR'];
1467 -
1468 - // If user is logged in, get their email
1469 - if (is_user_logged_in()) {
1470 - $current_user = wp_get_current_user();
1471 - $user_email = $current_user->user_email;
1472 - }
1473 -
1474 - // Create ban handler instance
1475 - $ban_handler = new MX_Chat_Ban_Handler();
1476 -
1477 - // Check if user is banned by IP
1478 - if ($ban_handler->check_ban($user_ip, 'ip')) {
1479 - wp_send_json([
1480 - 'success' => false,
1481 - 'message' => esc_html__('Access denied. Your IP address has been banned.', 'mxchat'),
1482 - 'status' => 'banned'
1483 - ]);
1484 - wp_die();
1485 - }
1486 -
1487 - // If user is logged in, also check email
1488 - if (!empty($user_email) && $ban_handler->check_ban($user_email, 'email')) {
1489 - wp_send_json([
1490 - 'success' => false,
1491 - 'message' => esc_html__('Access denied. Your email address has been banned.', 'mxchat'),
1492 - 'status' => 'banned'
1493 - ]);
1494 - wp_die();
1495 - }
1496 - }
1497 -
1498 - $this->fallbackResponse = ['text' => '', 'html' => '', 'images' => []];
1499 - $this->productCardHtml = '';
1500 - // Reset the per-turn function-calling UI capture (plan 48a57a).
1501 - $this->fc_ui_html = '';
1502 - $this->fc_ui_images = array();
1503 - $this->fc_ui_captured = false;
1504 -
1505 - // Get the actual WordPress user ID if logged in
1506 - $is_logged_in = is_user_logged_in();
1507 - if ($is_logged_in) {
1508 - $user_id = get_current_user_id(); // This will get the actual WordPress user ID
1509 - } else {
1510 - // For logged-out users, use your existing identifier method
1511 - $user_id = $this->mxchat_get_user_identifier();
1512 - }
1513 -
1514 - // Get and sanitize the user identifier
1515 - $user_id = sanitize_key($user_id);
1516 -
1517 - // Check rate limit using new settings structure
1518 - $rate_limit_result = $this->check_rate_limit();
1519 -
1520 - if ($rate_limit_result !== true) {
1521 - wp_send_json([
1522 - 'success' => false,
1523 - 'message' => $rate_limit_result['message'],
1524 - 'status' => 'rate_limit_exceeded'
1525 - ]);
1526 - wp_die();
1527 - }
1528 -
1529 - // Rest of your existing code...
1530 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
1531 -
1532 - // Treat the literal strings 'null' / 'undefined' as missing too. Browser edge cases
1533 - // (Safari ITP, private mode, cross-origin iframes with partitioned storage) can cause
1534 - // the frontend FormData.append() to stringify a null session_id into the literal
1535 - // "null", which would otherwise pass empty() and pollute the transcripts table with
1536 - // ghost sessions that group every visitor's first message under one row.
1537 - if ($session_id === 'null' || $session_id === 'undefined') {
1538 - $session_id = '';
1539 - }
1540 -
1541 - if (empty($session_id)) {
1542 - wp_send_json_error(esc_html__('Session ID is missing.', 'mxchat'));
1543 - wp_die();
1544 - }
1545 -
1546 - // Update session owner if it changed (e.g. IP changed due to network switch)
1547 - // The session ID itself is the authentication — if the client has it, they own it
1548 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
1549 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
1550 -
1551 - if (!$session_owner || $session_owner !== $current_user_identifier) {
1552 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
1553 - }
1554 -
1555 - // Validate and sanitize the incoming message
1556 - if (empty($_POST['message'])) {
1557 - wp_send_json_error(esc_html__('No message received.', 'mxchat'));
1558 - wp_die();
1559 - }
1560 -
1561 - // Enforce the configurable max input length (plan a3fae2 part C). 0 = unlimited.
1562 - // Server-side guard backing the textarea's client-side maxlength (which is bypassable).
1563 - // Reads the global core setting and measures characters (mb_strlen on the unslashed
1564 - // raw POST), matching the maxlength semantics.
1565 - $mxchat_max_input_length = isset($this->options['max_input_length']) ? intval($this->options['max_input_length']) : 0;
1566 - if ($mxchat_max_input_length > 0) {
1567 - $mxchat_incoming_raw = is_string($_POST['message']) ? wp_unslash($_POST['message']) : '';
1568 - if (mb_strlen($mxchat_incoming_raw) > $mxchat_max_input_length) {
1569 - wp_send_json([
1570 - 'success' => false,
1571 - /* translators: %d: maximum allowed characters */
1572 - 'message' => sprintf(esc_html__('Your message is too long. Please keep it under %d characters.', 'mxchat'), $mxchat_max_input_length),
1573 - 'status' => 'message_too_long'
1574 - ]);
1575 - wp_die();
1576 - }
1577 - }
1578 -
1579 -
1580 - // Track originating page for first message in session
1581 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
1582 -
1583 - // Check if originating page columns exist
1584 - $columns_exist = $wpdb->get_var("SHOW COLUMNS FROM $table_name LIKE 'originating_page_url'");
1585 -
1586 - if ($columns_exist) {
1587 - // Check if this session already has messages
1588 - $message_count = $wpdb->get_var($wpdb->prepare(
1589 - "SELECT COUNT(*) FROM $table_name WHERE session_id = %s",
1590 - $session_id
1591 - ));
1592 -
1593 - // If this is the first message in the session
1594 - if ($message_count == 0) {
1595 - // Get originating page from JavaScript (preferred) or HTTP_REFERER (fallback)
1596 - $originating_url = '';
1597 - $originating_title = '';
1598 -
1599 - // Try to get from POST data first (sent by JavaScript)
1600 - if (isset($_POST['current_page_url'])) {
1601 - $originating_url = esc_url_raw($_POST['current_page_url']);
1602 - $originating_title = isset($_POST['current_page_title'])
1603 - ? sanitize_text_field($_POST['current_page_title'])
1604 - : '';
1605 - }
1606 - // Fallback to HTTP_REFERER if not provided by JavaScript
1607 - else if (isset($_SERVER['HTTP_REFERER'])) {
1608 - $originating_url = esc_url_raw($_SERVER['HTTP_REFERER']);
1609 - }
1610 -
1611 - // Generate title if we have URL but no title
1612 - if ($originating_url && empty($originating_title)) {
1613 - $parsed_url = parse_url($originating_url);
1614 - $path = isset($parsed_url['path']) ? trim($parsed_url['path'], '/') : '';
1615 -
1616 - if (empty($path) || $path === 'index.php' || $path === 'index.html') {
1617 - $originating_title = 'Homepage';
1618 - } else {
1619 - // Clean up the path to make a readable title
1620 - $originating_title = str_replace(['-', '_', '/', '.php', '.html'], ' ', $path);
1621 - $originating_title = ucwords(trim($originating_title));
1622 - }
1623 - }
1624 -
1625 - // Store for later use when saving the message
1626 - $this->pending_originating_page = [
1627 - 'url' => $originating_url,
1628 - 'title' => $originating_title
1629 - ];
1630 - }
1631 - }
1632 -
1633 -
1634 -
1635 - // Get page context if provided
1636 - $page_context = null;
1637 - if (isset($_POST['page_context']) && !empty($_POST['page_context'])) {
1638 - $page_context_raw = stripslashes($_POST['page_context']);
1639 - $page_context = json_decode($page_context_raw, true);
1640 -
1641 - // Validate page context structure
1642 - if (is_array($page_context) &&
1643 - isset($page_context['url']) &&
1644 - isset($page_context['title']) &&
1645 - isset($page_context['content'])) {
1646 -
1647 - // Sanitize page context
1648 - $page_context['url'] = esc_url_raw($page_context['url']);
1649 - $page_context['title'] = sanitize_text_field($page_context['title']);
1650 - $page_context['content'] = wp_kses_post($page_context['content']);
1651 - } else {
1652 - $page_context = null;
1653 - }
1654 - }
1655 -
1656 - // Modify the message sanitization to preserve PHP tags in code blocks
1657 - $allowed_tags = [
1658 - 'pre' => [],
1659 - 'code' => ['class' => true],
1660 - 'span' => ['class' => true],
1661 - 'div' => ['class' => true],
1662 - ];
1663 -
1664 - // First preserve code blocks
1665 - $message = preg_replace_callback('/<pre><code.*?>.*?<\/code><\/pre>/s', function($matches) {
1666 - return htmlspecialchars_decode($matches[0]);
1667 - }, $_POST['message']);
1668 -
1669 - // Then apply sanitization
1670 - $message = wp_kses($message, $allowed_tags);
1671 -
1672 - // Preserve code blocks from markdown conversion
1673 - $message = preg_replace('/```(\w+)?\s*([\s\S]+?)```/s', '<pre><code class="$1">$2</code></pre>', $message);
1674 - $message = apply_filters('mxchat_filter_message', $message, 'prompt', $session_id);
1675 -
1676 - // ===== SIMPLIFIED TESTING PANEL INITIALIZATION =====
1677 - // Always initialize testing data for admins (no toggle needed)
1678 - $testing_data = null;
1679 - if (current_user_can('administrator')) {
1680 - // For vision messages, use the original user message for the query display
1681 - $query_for_testing = $message;
1682 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1683 - $query_for_testing = sanitize_textarea_field($_POST['original_user_message']);
1684 - }
1685 -
1686 - $testing_data = [
1687 - 'query' => $query_for_testing,
1688 - 'timestamp' => time(),
1689 - 'top_matches' => [],
1690 - 'action_matches' => [], // Initialize action matches array
1691 - 'page_context' => $page_context, // Include page context in testing data
1692 - 'is_vision' => isset($_POST['vision_processed']) && $_POST['vision_processed'],
1693 - 'bot_id' => $bot_id // Include bot ID in testing data
1694 - ];
1695 -
1696 - // Get similarity threshold from bot options or default options
1697 - $similarity_threshold = isset($current_options['similarity_threshold'])
1698 - ? ((int) $current_options['similarity_threshold']) / 100
1699 - : 0.35;
1700 -
1701 - $testing_data['similarity_threshold'] = $similarity_threshold;
1702 -
1703 - // Determine knowledge base type using bot-specific config
1704 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
1705 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
1706 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
1707 - }
1708 - // ===== END SIMPLIFIED TESTING INITIALIZATION =====
1709 -
1710 - // Add debug before and after:
1711 - //error_log('MxChat Core: About to call mxchat_pre_process_message filter with message: ' . $message);
1712 - $pre_processed_result = apply_filters('mxchat_pre_process_message', $message, $user_id, $session_id);
1713 - //error_log('MxChat Core: Filter returned: ' . (is_array($pre_processed_result) ? 'array' : $pre_processed_result));
1714 -
1715 -
1716 - // If the pre-processing returned a result (not the original message), use it directly
1717 - if (is_array($pre_processed_result) && isset($pre_processed_result['text'])) {
1718 - // Save the AI response
1719 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['text']);
1720 -
1721 - // Save HTML content if provided
1722 - if (!empty($pre_processed_result['html'])) {
1723 - $this->mxchat_save_chat_message($session_id, 'bot', $pre_processed_result['html']);
1724 - }
1725 -
1726 - // Add testing data if admin
1727 - $response_data = [
1728 - 'text' => $pre_processed_result['text'],
1729 - 'html' => $pre_processed_result['html'] ?? '',
1730 - 'session_id' => $session_id
1731 - ];
1732 -
1733 - if ($testing_data !== null) {
1734 - $response_data['testing_data'] = $testing_data;
1735 - }
1736 -
1737 - wp_send_json($response_data);
1738 - wp_die();
1739 - }
1740 -
1741 - // Save the user's message - handle vision processed messages differently
1742 - if (isset($_POST['vision_processed']) && $_POST['vision_processed'] && isset($_POST['original_user_message'])) {
1743 - // For vision messages, save the original user message with image indicator
1744 - $original_message = sanitize_textarea_field($_POST['original_user_message']);
1745 - if (isset($_POST['vision_images_count']) && $_POST['vision_images_count'] > 0) {
1746 - $image_count = intval($_POST['vision_images_count']);
1747 - $original_message .= " [{$image_count} image(s)]";
1748 - }
1749 - $this->mxchat_save_chat_message($session_id, 'user', $original_message);
1750 - } else {
1751 - // Regular message - save as normal
1752 - $this->mxchat_save_chat_message($session_id, 'user', $message);
1753 - }
1754 -
1755 -
1756 - if (is_email($message)) {
1757 - // Add the email to Loops
1758 - $this->add_email_to_loops($message);
1759 -
1760 - // Get the user's success message instruction using current_options
1761 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1762 -
1763 - // Set instruction for AI using the user's success message
1764 - $this->current_action_instruction = $user_success_message;
1765 -
1766 - // Clear the email capture transient since we got the email
1767 - delete_transient('mxchat_email_capture_' . $user_id);
1768 - }
1769 -
1770 - // Check if we're in an email capture flow but user hasn't provided email yet
1771 - elseif (get_transient('mxchat_email_capture_' . $user_id)) {
1772 - // Check if the message contains an email (not the whole message being an email)
1773 - if (preg_match('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', $message, $matches)) {
1774 - $extracted_email = $matches[0];
1775 -
1776 - // Add the extracted email to Loops
1777 - $this->add_email_to_loops($extracted_email);
1778 -
1779 - // Get the user's success message instruction using current_options
1780 - $user_success_message = $current_options['email_capture_response'] ?? __('Thank you for providing your email! You\'ve been added to our list.', 'mxchat');
1781 -
1782 - // Set instruction for AI using the user's success message
1783 - $this->current_action_instruction = $user_success_message;
1784 -
1785 - // Clear the email capture transient since we got the email
1786 - delete_transient('mxchat_email_capture_' . $user_id);
1787 - }
1788 - // If no email found but we're in capture mode, remind them
1789 - else {
1790 - // Get the original instruction to remind them using current_options
1791 - $original_instruction = $current_options['triggered_phrase_response'] ?? __("Please provide your email address.", 'mxchat');
1792 - $this->current_action_instruction = $original_instruction;
1793 - }
1794 - }
1795 -
1796 - $intent_info = '';
1797 -
1798 - // Check chat mode
1799 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
1800 -
1801 - // Handle agent mode
1802 - // Handle agent mode
1803 - if ($chat_mode === 'agent') {
1804 - // First, check for switch intent before doing anything else
1805 - $intent_matched = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1806 -
1807 - // Capture action analysis for testing panel after intent check
1808 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1809 - $testing_data['action_matches'] = $this->last_action_analysis;
1810 - }
1811 -
1812 - // Around line 506, in the agent mode handling section:
1813 - if ($intent_matched && !empty($this->fallbackResponse['text'])) {
1814 - // Update chat mode first
1815 - update_option("mxchat_mode_{$session_id}", 'ai');
1816 -
1817 - // Clear any existing PDF context to start fresh
1818 - $this->clear_pdf_transients($session_id);
1819 -
1820 - // Prepare clean switch response with explicit chat_mode
1821 - $response_data = [
1822 - 'text' => $this->fallbackResponse['text'],
1823 - 'html' => $this->fallbackResponse['html'] ?? '',
1824 - 'session_id' => $session_id,
1825 - 'chat_mode' => 'ai' // EXPLICITLY SET THIS
1826 - ];
1827 -
1828 - if ($testing_data !== null) {
1829 - $response_data['testing_data'] = $testing_data;
1830 - }
1831 -
1832 - // Save the mode switch message
1833 - $this->mxchat_save_chat_message($session_id, 'system', esc_html__('Switched to AI chat mode', 'mxchat'));
1834 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1835 -
1836 - // Send response and exit
1837 - wp_send_json($response_data);
1838 - wp_die();
1839 - } elseif (!$intent_matched) {
1840 - // No intent matched, handle live agent message
1841 - try {
1842 - $this->mxchat_send_user_message_to_agent($message, $user_id, $session_id);
1843 -
1844 - $agent_response = [
1845 - 'status' => 'waiting_for_agent',
1846 - 'message' => esc_html__('Message sent to live agent.', 'mxchat')
1847 - ];
1848 -
1849 - if ($testing_data !== null) {
1850 - $agent_response['testing_data'] = $testing_data;
1851 - }
1852 -
1853 - wp_send_json_success($agent_response);
1854 - } catch (\Exception $e) {
1855 - wp_send_json_error(esc_html__('Failed to send message to agent', 'mxchat'));
1856 - }
1857 - wp_die();
1858 - }
1859 - }
1860 -
1861 - // Step 1: Check for new PDF URL in the message
1862 - if (!isset($_POST['vision_processed']) && preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
1863 - $new_pdf_url = $matches[0];
1864 -
1865 - // Check if this is likely a PDF-related request
1866 - $pdf_keywords = ['pdf', 'document', 'read', 'analyze'];
1867 - $is_pdf_request = false;
1868 -
1869 - foreach ($pdf_keywords as $keyword) {
1870 - if (stripos($message, $keyword) !== false) {
1871 - $is_pdf_request = true;
1872 - break;
1873 - }
1874 - }
1875 -
1876 - // If it looks like a PDF request or we're waiting for a PDF URL
1877 - if ($is_pdf_request || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
1878 - // Validate HTTPS
1879 - if (wp_http_validate_url($new_pdf_url) && parse_url($new_pdf_url, PHP_URL_SCHEME) === 'https') {
1880 - // Extract filename from URL
1881 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1882 -
1883 - // Clear previous PDF transients
1884 - $this->clear_pdf_transients($session_id);
1885 -
1886 - // Process new PDF using current_options
1887 - $max_pages = $current_options['pdf_max_pages'] ?? 69;
1888 - $embeddings = $this->fetch_and_split_pdf_pages($new_pdf_url, $max_pages);
1889 -
1890 - if ($embeddings === 'too_many_pages') {
1891 - $error_text = sprintf(
1892 - $current_options['pdf_intent_error_text'] ??
1893 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
1894 - $max_pages
1895 - );
1896 - $this->fallbackResponse['text'] = $error_text;
1897 - } elseif ($embeddings) {
1898 - // Store new PDF information
1899 - $pdf_filename = basename(parse_url($new_pdf_url, PHP_URL_PATH));
1900 -
1901 - // If the filename is generic, create a more descriptive one
1902 - if (in_array($pdf_filename, ['results_download.php', 'download.php', 'view.php', 'pdf.php']) ||
1903 - strpos($pdf_filename, '.php') !== false) {
1904 - $pdf_filename = 'Document_' . date('Y-m-d_H-i') . '.pdf';
1905 - }
1906 -
1907 - set_transient('mxchat_pdf_url_' . $session_id, $new_pdf_url, HOUR_IN_SECONDS);
1908 - set_transient('mxchat_pdf_filename_' . $session_id, $pdf_filename, HOUR_IN_SECONDS);
1909 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
1910 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
1911 -
1912 - $success_text = $current_options['pdf_intent_success_text'] ??
1913 - esc_html__("I've processed the new PDF '{$pdf_filename}'. What questions do you have about it?", 'mxchat');
1914 -
1915 - $pdf_response = [
1916 - 'success' => true,
1917 - 'message' => $success_text,
1918 - 'data' => [
1919 - 'filename' => $pdf_filename
1920 - ]
1921 - ];
1922 -
1923 - if ($testing_data !== null) {
1924 - $pdf_response['testing_data'] = $testing_data;
1925 - }
1926 -
1927 - wp_send_json($pdf_response);
1928 - wp_die();
1929 - } else {
1930 - $error_text = $current_options['pdf_intent_error_text'] ??
1931 - esc_html__("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
1932 - $this->fallbackResponse['text'] = $error_text;
1933 - }
1934 -
1935 - $pdf_error_response = [
1936 - 'success' => false,
1937 - 'message' => $this->fallbackResponse['text']
1938 - ];
1939 -
1940 - if ($testing_data !== null) {
1941 - $pdf_error_response['testing_data'] = $testing_data;
1942 - }
1943 -
1944 - wp_send_json($pdf_error_response);
1945 - wp_die();
1946 - }
1947 - }
1948 - }
1949 -
1950 -
1951 - // Step 2: Detect intent and handle intent-based responses
1952 - $intent_result = $this->mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id);
1953 -
1954 - // Capture action analysis for testing panel after intent check
1955 - if ($testing_data !== null && isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
1956 - $testing_data['action_matches'] = $this->last_action_analysis;
1957 - }
1958 -
1959 - // Step 3: Handle the intent result appropriately
1960 - if ($intent_result !== false) {
1961 - // Intent was matched - ALWAYS send as JSON response, never streaming
1962 -
1963 - if (is_array($intent_result) && (isset($intent_result['text']) || isset($intent_result['html']))) {
1964 - // Intent returned a direct response array
1965 - $response_data = [
1966 - 'text' => $intent_result['text'] ?? '',
1967 - 'html' => $intent_result['html'] ?? '',
1968 - 'session_id' => $session_id
1969 - ];
1970 -
1971 - // IMPORTANT: Include chat_mode if present (for WhatsApp, Slack, etc.)
1972 - if (isset($intent_result['chat_mode'])) {
1973 - $response_data['chat_mode'] = $intent_result['chat_mode'];
1974 - }
1975 -
1976 - if ($testing_data !== null) {
1977 - $response_data['testing_data'] = $testing_data;
1978 - }
1979 -
1980 - wp_send_json($response_data);
1981 - wp_die();
1982 - } else if ($intent_result === true && (!empty($this->fallbackResponse['text']) || !empty($this->fallbackResponse['html']))) {
1983 - // Intent returned true and set fallbackResponse
1984 -
1985 - // SAVE TO TRANSCRIPT
1986 - if (!empty($this->fallbackResponse['text'])) {
1987 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['text']);
1988 - }
1989 - // Save action HTML (product cards, featured products, etc.) so it renders in transcripts
1990 - if (!empty($this->fallbackResponse['html'])) {
1991 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
1992 - }
1993 -
1994 - $response_data = [
1995 - 'text' => $this->fallbackResponse['text'] ?? '',
1996 - 'html' => $this->fallbackResponse['html'] ?? '',
1997 - 'session_id' => $session_id
1998 - ];
1999 -
2000 - if (isset($this->fallbackResponse['chat_mode'])) {
2001 - $response_data['chat_mode'] = $this->fallbackResponse['chat_mode'];
2002 - }
2003 -
2004 - if ($testing_data !== null) {
2005 - $response_data['testing_data'] = $testing_data;
2006 - }
2007 -
2008 - wp_send_json($response_data);
2009 - wp_die();
2010 - }
2011 - }
2012 -
2013 - // If we get here, no intent matched OR the intent didn't provide a usable response
2014 -
2015 - // Step 4: Generate AI response
2016 - // Get session start timestamp - when persistence is OFF, only include messages from this page load
2017 - $session_start_timestamp = isset($_POST['session_start_timestamp']) ? intval($_POST['session_start_timestamp']) : 0;
2018 - $conversation_history = $this->mxchat_fetch_conversation_history_for_ai($session_id, $session_start_timestamp);
2019 - $this->mxchat_increment_chat_count();
2020 -
2021 - // Generate embedding for the user's query - USE BOT-SPECIFIC API KEY
2022 - $api_key = $current_options['api_key'] ?? $this->options['api_key'];
2023 - $user_message_embedding = $this->mxchat_generate_embedding($message, $api_key);
2024 -
2025 - // Check if the embedding generation returned an error
2026 - if (is_array($user_message_embedding) && isset($user_message_embedding['error'])) {
2027 - $error_message = $user_message_embedding['error'];
2028 - $error_code = $user_message_embedding['error_code'] ?? 'embedding_error';
2029 -
2030 - // FIXED: Send error in appropriate format based on streaming mode
2031 - if ($is_streaming) {
2032 - echo "data: " . json_encode([
2033 - 'error' => true,
2034 - 'error_message' => $error_message,
2035 - 'error_code' => $error_code,
2036 - 'text' => $error_message,
2037 - 'message' => $error_message
2038 - ]) . "\n\n";
2039 - echo "data: [DONE]\n\n";
2040 - flush();
2041 - } else {
2042 - wp_send_json_error([
2043 - 'error_message' => $error_message,
2044 - 'error_code' => $error_code
2045 - ]);
2046 - }
2047 - wp_die();
2048 - }
2049 -
2050 - // Check if the embedding is valid
2051 - if (!is_array($user_message_embedding) || empty($user_message_embedding)) {
2052 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2053 -
2054 - // FIXED: Send error in appropriate format based on streaming mode
2055 - if ($is_streaming) {
2056 - echo "data: " . json_encode([
2057 - 'error' => true,
2058 - 'error_message' => $error_message,
2059 - 'error_code' => 'invalid_embedding',
2060 - 'text' => $error_message,
2061 - 'message' => $error_message
2062 - ]) . "\n\n";
2063 - echo "data: [DONE]\n\n";
2064 - flush();
2065 - } else {
2066 - wp_send_json_error([
2067 - 'error_message' => $error_message,
2068 - 'error_code' => 'invalid_embedding'
2069 - ]);
2070 - }
2071 - wp_die();
2072 - }
2073 -
2074 - // Build context with both knowledge base and PDF content if available
2075 - $context_content = "User asked: '{$message}'\n\n";
2076 -
2077 - // Add action instruction if present (add this right after the above line)
2078 - if (!empty($this->current_action_instruction)) {
2079 - $context_content .= "===== SPECIAL INSTRUCTION =====\n";
2080 - $context_content .= "IMPORTANT: " . $this->current_action_instruction . "\n";
2081 - $context_content .= "Respond naturally and conversationally while following this instruction.\n";
2082 - $context_content .= "===== END SPECIAL INSTRUCTION =====\n\n";
2083 -
2084 - // Clear the instruction after using it
2085 - $this->current_action_instruction = null;
2086 - }
2087 -
2088 -
2089 - // Add page context if available and contextual awareness is enabled using current_options
2090 - if ($page_context && isset($current_options['contextual_awareness_toggle']) && $current_options['contextual_awareness_toggle'] === 'on') {
2091 - $context_content .= "===== CURRENT PAGE CONTEXT =====\n";
2092 - $context_content .= "Page URL: " . $page_context['url'] . "\n";
2093 - $context_content .= "Page Title: " . $page_context['title'] . "\n";
2094 - $context_content .= "Page Content: " . $page_context['content'] . "\n";
2095 - $context_content .= "===== END CURRENT PAGE CONTEXT =====\n\n";
2096 - }
2097 -
2098 - // Get relevant content from knowledge base - PASS BOT_ID and MESSAGE for Vector Store
2099 - $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding, $bot_id, $message);
2100 -
2101 - // NEW: Also extract URLs from system instructions (only if citation links enabled)
2102 - // Use fresh options to ensure we get the latest setting value
2103 - $fresh_options = get_option('mxchat_options', []);
2104 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
2105 -
2106 - $system_instructions = $this->get_system_instructions($bot_id, $session_id);
2107 - if ($citation_links_enabled && !empty($system_instructions)) {
2108 - preg_match_all(
2109 - '#\bhttps?://[^\s<>"\']+#i',
2110 - $system_instructions,
2111 - $system_instruction_urls
2112 - );
2113 -
2114 - if (!empty($system_instruction_urls[0])) {
2115 - // Merge with existing valid URLs
2116 - $this->current_valid_urls = array_merge(
2117 - $this->current_valid_urls,
2118 - $system_instruction_urls[0]
2119 - );
2120 - // Remove duplicates
2121 - $this->current_valid_urls = array_unique($this->current_valid_urls);
2122 -
2123 - //error_log("Added " . count($system_instruction_urls[0]) . " URLs from system instructions");
2124 - }
2125 - }
2126 -
2127 -// ===== CAPTURE REAL SIMILARITY DATA FOR ADMINS =====
2128 -if ($testing_data !== null && $this->last_similarity_analysis !== null) {
2129 - // Update testing data with the REAL similarity analysis
2130 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
2131 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2132 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
2133 - $testing_data['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2134 - $testing_data['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2135 -}
2136 -// ===== END SIMILARITY DATA CAPTURE =====
2137 -
2138 -// NEW: Add valid URLs to testing data for admin panel display (AFTER similarity data)
2139 -if ($testing_data !== null && !empty($this->current_valid_urls)) {
2140 - $testing_data['approved_urls'] = array_values($this->current_valid_urls);
2141 - //error_log("Added " . count($this->current_valid_urls) . " approved URLs to testing data");
2142 -}
2143 -
2144 - if (!empty($relevant_content)) {
2145 - $context_content .= "===== OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n" . $relevant_content . "\n===== END OF OFFICIAL KNOWLEDGE DATABASE CONTENT =====\n\n";
2146 - } else {
2147 - $context_content .= "===== NO RELEVANT CONTENT FOUND IN KNOWLEDGE DATABASE =====\n";
2148 - }
2149 -
2150 - // NEW: Add approved URLs list to context for AI (only if citation links enabled)
2151 - if ($citation_links_enabled && !empty($this->current_valid_urls)) {
2152 - $context_content .= "===== APPROVED URLS FOR CITATIONS =====\n";
2153 - $context_content .= "You may ONLY use these exact URLs in your response:\n";
2154 - foreach ($this->current_valid_urls as $url) {
2155 - $context_content .= "- " . $url . "\n";
2156 - }
2157 - $context_content .= "\nCRITICAL: Do NOT create, modify, extend, or invent any other URLs. ";
2158 - $context_content .= "===== END APPROVED URLS =====\n\n";
2159 - }
2160 -
2161 - // Check for and include PDF content
2162 - $pdf_url = get_transient('mxchat_pdf_url_' . $session_id);
2163 - $pdf_embeddings = get_transient('mxchat_pdf_embeddings_' . $session_id);
2164 - $pdf_filename = get_transient('mxchat_pdf_filename_' . $session_id);
2165 - if ($pdf_url && $pdf_embeddings && get_transient('mxchat_include_pdf_in_context_' . $session_id)) {
2166 - $relevant_pdf_pages = $this->find_relevant_pdf_pages($user_message_embedding, $pdf_embeddings);
2167 - if (!empty($relevant_pdf_pages)) {
2168 - $context_content .= "Relevant content from PDF document '{$pdf_filename}':\n";
2169 - foreach ($relevant_pdf_pages as $page_data) {
2170 - $context_content .= "Page {$page_data['page_number']} of '{$pdf_filename}': {$page_data['text']}\n";
2171 - }
2172 - $context_content .= "\n";
2173 - }
2174 - }
2175 -
2176 - // Check for and include Word content
2177 - $word_url = get_transient('mxchat_word_url_' . $session_id);
2178 - $word_embeddings = get_transient('mxchat_word_embeddings_' . $session_id);
2179 - $word_filename = get_transient('mxchat_word_filename_' . $session_id);
2180 - if ($word_url && $word_embeddings && get_transient('mxchat_include_word_in_context_' . $session_id)) {
2181 - $relevant_word_chunks = $this->word_handler->mxchat_find_relevant_word_chunks($user_message_embedding, $word_embeddings);
2182 - if (!empty($relevant_word_chunks)) {
2183 - $context_content .= "Relevant content from Word document '{$word_filename}':\n";
2184 - foreach ($relevant_word_chunks as $chunk_data) {
2185 - $context_content .= "Section {$chunk_data['chunk_number']} of '{$word_filename}': {$chunk_data['text']}\n";
2186 - }
2187 - $context_content .= "\n";
2188 - }
2189 - }
2190 -
2191 - $context_content = apply_filters('mxchat_prepare_context', $context_content, $session_id);
2192 -
2193 - // Extract model from current options for bot-specific model support
2194 - $selected_model = isset($current_options['model']) ? $current_options['model'] : 'gpt-5.1-chat-latest';
2195 -
2196 - // ===== Native function-calling fallback (plan-mxchat-20260617-a41dee) =====
2197 - // Intents already missed (we're past the intent router). If function
2198 - // calling is enabled and the active model is tool-capable, let the model
2199 - // SELECT and run registered callbacks as tools — independent of intents,
2200 - // works with zero Actions. The tool round is buffered; the final answer is
2201 - // emitted via the SAME envelopes the normal path uses. Default-off, so
2202 - // existing installs never enter this branch.
2203 - if ($this->mxchat_fc_should_run($selected_model)) {
2204 - $fc_outcome = $this->mxchat_fc_attempt(
2205 - $message,
2206 - $context_content,
2207 - $conversation_history,
2208 - $selected_model,
2209 - $current_options,
2210 - $session_id,
2211 - $user_id
2212 - );
2213 - if (is_array($fc_outcome) && !empty($fc_outcome['handled'])) {
2214 - $fc_text = isset($fc_outcome['text']) ? $fc_outcome['text'] : '';
2215 - if (!empty($this->current_valid_urls)) {
2216 - $fc_text = $this->validate_and_clean_urls($fc_text, $this->current_valid_urls);
2217 - }
2218 - // plan-mxchat-20260617-48a57a — surface any UI element a tool
2219 - // produced (generated image / product card / image gallery) so the
2220 - // widget RENDERS it, instead of emitting only the model's text.
2221 - // The html was already saved to the transcript in
2222 - // mxchat_fc_execute_tool (or by the callback itself for self-saving
2223 - // core tools), so we persist ONLY the model's caption text here.
2224 - $fc_html = isset($this->fc_ui_html) ? $this->fc_ui_html : '';
2225 -
2226 - if ($fc_text !== '') {
2227 - $this->mxchat_save_chat_message($session_id, 'bot', $fc_text, null, null);
2228 - }
2229 -
2230 - if ($is_streaming) {
2231 - // The frontend SSE reader routes any event carrying text/html
2232 - // to handleNonStreamResponse(), which renders text + html in a
2233 - // single bot message — so emit one complete event (mirrors the
2234 - // intent path's text/html envelope).
2235 - $sse = array('session_id' => $session_id);
2236 - if ($fc_text !== '') $sse['text'] = $fc_text;
2237 - if ($fc_html !== '') $sse['html'] = $fc_html;
2238 - if ($fc_text === '' && $fc_html === '') $sse['text'] = $this->mxchat_fc_giveup_text();
2239 - echo "data: " . wp_json_encode($sse) . "\n\n";
2240 - echo "data: [DONE]\n\n";
2241 - flush();
2242 - } else {
2243 - $fc_response_data = array('text' => $fc_text, 'html' => $fc_html, 'session_id' => $session_id);
2244 - if ($testing_data !== null) {
2245 - $fc_response_data['testing_data'] = $testing_data;
2246 - }
2247 - wp_send_json($fc_response_data);
2248 - }
2249 - wp_die();
2250 - }
2251 - }
2252 - // ===== end function-calling fallback =====
2253 -
2254 - $response = $this->mxchat_generate_response(
2255 - $context_content,
2256 - $current_options['api_key'] ?? $this->options['api_key'],
2257 - $current_options['xai_api_key'] ?? $this->options['xai_api_key'],
2258 - $current_options['claude_api_key'] ?? $this->options['claude_api_key'],
2259 - $current_options['deepseek_api_key'] ?? $this->options['deepseek_api_key'],
2260 - $current_options['gemini_api_key'] ?? $this->options['gemini_api_key'],
2261 - $current_options['openrouter_api_key'] ?? $this->options['openrouter_api_key'],
2262 - $conversation_history,
2263 - $is_streaming,
2264 - $session_id,
2265 - $testing_data,
2266 - $selected_model
2267 - );
2268 -
2269 - // Handle streaming vs non-streaming responses
2270 - if ($is_streaming) {
2271 - // Check if streaming actually happened or if it fell back to regular response
2272 - if ($response === true) {
2273 - wp_die();
2274 - }
2275 - // If we get here, streaming fell back to regular response, continue
2276 - // But if there's an error, we need to send it as SSE format since headers are already set
2277 - if (is_array($response) && isset($response['error'])) {
2278 - $error_message = $response['error'];
2279 - $error_code = $response['error_code'] ?? 'api_error';
2280 - // Send error in SSE format that the client JS can handle
2281 - echo "data: " . json_encode([
2282 - 'error' => true,
2283 - 'error_message' => $error_message,
2284 - 'error_code' => $error_code,
2285 - 'text' => $error_message, // Also include as text for fallback handling
2286 - 'message' => $error_message
2287 - ]) . "\n\n";
2288 - echo "data: [DONE]\n\n";
2289 - flush();
2290 - wp_die();
2291 - }
2292 - }
2293 -
2294 - // Check if the response is an error array (non-streaming mode)
2295 - if (is_array($response) && isset($response['error'])) {
2296 - wp_send_json_error([
2297 - 'error_message' => $response['error'],
2298 - 'error_code' => $response['error_code'] ?? 'api_error'
2299 - ]);
2300 - wp_die();
2301 - }
2302 -
2303 - // DEBUG: Check what we have
2304 - //error_log("=== BEFORE URL VALIDATION ===");
2305 - //error_log("current_valid_urls is empty? " . (empty($this->current_valid_urls) ? 'YES' : 'NO'));
2306 - //error_log("current_valid_urls count: " . count($this->current_valid_urls));
2307 - //error_log("current_valid_urls content: " . print_r($this->current_valid_urls, true));
2308 -
2309 - // If we get here, the response is valid text - now validate URLs
2310 - if (!empty($this->current_valid_urls)) {
2311 - //error_log("CALLING validate_and_clean_urls");
2312 - $response = $this->validate_and_clean_urls($response, $this->current_valid_urls);
2313 - } else {
2314 - //error_log("SKIPPING validation - current_valid_urls is empty");
2315 - }
2316 - // ===== END URL VALIDATION =====
2317 -
2318 - // Prepare RAG context data for storage (only include documents used for context)
2319 - $rag_context_for_storage = null;
2320 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
2321 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
2322 -
2323 - if ($has_rag_data || $has_action_data) {
2324 - $rag_context_for_storage = [];
2325 -
2326 - // Add RAG/source data if available
2327 - if ($has_rag_data) {
2328 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
2329 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
2330 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
2331 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
2332 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
2333 - $rag_context_for_storage['sources_used'] = $this->last_similarity_analysis['sources_used'] ?? 0;
2334 - $rag_context_for_storage['total_chunks_used'] = $this->last_similarity_analysis['total_chunks_used'] ?? 0;
2335 - }
2336 -
2337 - // Add action analysis data if available
2338 - if ($has_action_data) {
2339 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
2340 - }
2341 - }
2342 -
2343 - // Save the cleaned response with RAG context
2344 - $this->mxchat_save_chat_message($session_id, 'bot', $response, null, $rag_context_for_storage);
2345 -
2346 - // Step 5: Save additional content if available
2347 - if (!empty($this->productCardHtml)) {
2348 - $this->mxchat_save_chat_message($session_id, 'bot', $this->productCardHtml);
2349 - }
2350 -
2351 - if (!empty($this->fallbackResponse['html'])) {
2352 - $this->mxchat_save_chat_message($session_id, 'bot', $this->fallbackResponse['html']);
2353 - }
2354 -
2355 - // Step 6: Return the response
2356 - // DEBUG: Check if newlines exist in the response
2357 - //error_log("=== MXCHAT NON-STREAMING RESPONSE DEBUG ===");
2358 - //error_log("Response has newlines: " . (strpos($response, "\n") !== false ? 'YES' : 'NO'));
2359 - //error_log("Response first 500 chars: " . substr($response, 0, 500));
2360 -
2361 - $response_data = [
2362 - 'text' => $response,
2363 - 'html' => !empty($this->productCardHtml) ? $this->productCardHtml : ($this->fallbackResponse['html'] ?? ''),
2364 - 'session_id' => $session_id
2365 - ];
2366 -
2367 - // Include vectorstore error info for admin debugging (only visible to admins via testing_data)
2368 - if (!empty($this->last_vectorstore_error) && $testing_data !== null) {
2369 - $testing_data['vectorstore_error'] = $this->last_vectorstore_error;
2370 - }
2371 -
2372 - // Also pass it as a top-level field so JS can show a better error message to admins
2373 - if (!empty($this->last_vectorstore_error) && current_user_can('manage_options')) {
2374 - $response_data['vectorstore_error'] = $this->last_vectorstore_error;
2375 - }
2376 -
2377 - // Always add testing data for admins (no toggle needed)
2378 - if ($testing_data !== null) {
2379 - $response_data['testing_data'] = $testing_data;
2380 - }
2381 -
2382 - wp_send_json($response_data);
2383 - wp_die();
2384 -}
2385 -
2386 -/**
2387 - * Get bot-specific options for multi-bot functionality
2388 - * Falls back to default options if bot_id is 'default' or multi-bot add-on is not active
2389 - */
2390 -// Also debug the bot options retrieval
2391 -private function get_bot_options($bot_id = 'default') {
2392 - //error_log("MXCHAT DEBUG: get_bot_options called for bot: " . $bot_id);
2393 -
2394 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2395 - //error_log("MXCHAT DEBUG: Using default options (no multi-bot or bot is 'default')");
2396 - return array();
2397 - }
2398 -
2399 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
2400 -
2401 - if (!empty($bot_options)) {
2402 - //error_log("MXCHAT DEBUG: Got bot-specific options from filter");
2403 - if (isset($bot_options['similarity_threshold'])) {
2404 - //error_log(" - similarity_threshold: " . $bot_options['similarity_threshold']);
2405 - }
2406 - }
2407 -
2408 - return is_array($bot_options) ? $bot_options : array();
2409 -}
2410 -
2411 -/**
2412 - * Get bot-specific Pinecone configuration
2413 - * Used in the knowledge retrieval functions
2414 - */
2415 -// Also add debugging to your get_bot_pinecone_config function
2416 -private function get_bot_pinecone_config($bot_id = 'default') {
2417 - //error_log("MXCHAT DEBUG: get_bot_pinecone_config called for bot: " . $bot_id);
2418 -
2419 - // If default bot or multi-bot add-on not active, use default Pinecone config
2420 - if ($bot_id === 'default' || !class_exists('MxChat_Multi_Bot_Manager')) {
2421 - //error_log("MXCHAT DEBUG: Using default Pinecone config (no multi-bot or bot is 'default')");
2422 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
2423 - $config = array(
2424 - 'use_pinecone' => (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1'),
2425 - 'api_key' => $addon_options['mxchat_pinecone_api_key'] ?? '',
2426 - 'host' => $addon_options['mxchat_pinecone_host'] ?? '',
2427 - 'namespace' => $addon_options['mxchat_pinecone_namespace'] ?? ''
2428 - );
2429 - //error_log("MXCHAT DEBUG: Default config - use_pinecone: " . ($config['use_pinecone'] ? 'true' : 'false'));
2430 - return $config;
2431 - }
2432 -
2433 - //error_log("MXCHAT DEBUG: Calling filter 'mxchat_get_bot_pinecone_config' for bot: " . $bot_id);
2434 -
2435 - // Hook for multi-bot add-on to provide bot-specific Pinecone config
2436 - $bot_pinecone_config = apply_filters('mxchat_get_bot_pinecone_config', array(), $bot_id);
2437 -
2438 - if (!empty($bot_pinecone_config)) {
2439 - //error_log("MXCHAT DEBUG: Got bot-specific config from filter");
2440 - //error_log(" - use_pinecone: " . (isset($bot_pinecone_config['use_pinecone']) ? ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false') : 'not set'));
2441 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'not set'));
2442 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'not set'));
2443 - } else {
2444 - //error_log("MXCHAT DEBUG: Filter returned empty config!");
2445 - }
2446 -
2447 - return is_array($bot_pinecone_config) ? $bot_pinecone_config : array();
2448 -}
2449 -
2450 -
2451 -// Updated function to check intents and invoke the callback function
2452 -private function mxchat_check_intent_and_invoke_callback($message, $user_id, $session_id) {
2453 - global $wpdb;
2454 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
2455 -
2456 - // Get the current bot_id
2457 - $current_bot_id = $this->get_current_bot_id($session_id);
2458 -
2459 - // Generate the user embedding
2460 - $user_embedding = $this->mxchat_generate_embedding($message, $this->options['api_key']);
2461 -
2462 - // Check if embedding generation returned an error
2463 - if (is_array($user_embedding) && isset($user_embedding['error'])) {
2464 - $error_message = $user_embedding['error'];
2465 - $error_code = $user_embedding['error_code'] ?? 'embedding_error';
2466 -
2467 - // FIXED: Send error in appropriate format based on streaming mode
2468 - if ($this->is_streaming) {
2469 - echo "data: " . json_encode([
2470 - 'error' => true,
2471 - 'error_message' => $error_message,
2472 - 'error_code' => $error_code,
2473 - 'text' => $error_message,
2474 - 'message' => $error_message
2475 - ]) . "\n\n";
2476 - echo "data: [DONE]\n\n";
2477 - flush();
2478 - } else {
2479 - wp_send_json_error([
2480 - 'error_message' => $error_message,
2481 - 'error_code' => $error_code
2482 - ]);
2483 - }
2484 - wp_die();
2485 - }
2486 -
2487 - // Check if embedding is valid
2488 - if (!is_array($user_embedding) || empty($user_embedding)) {
2489 - $error_message = esc_html__('Unable to process your message. The embedding service is not responding correctly.', 'mxchat');
2490 -
2491 - // FIXED: Send error in appropriate format based on streaming mode
2492 - if ($this->is_streaming) {
2493 - echo "data: " . json_encode([
2494 - 'error' => true,
2495 - 'error_message' => $error_message,
2496 - 'error_code' => 'invalid_embedding',
2497 - 'text' => $error_message,
2498 - 'message' => $error_message
2499 - ]) . "\n\n";
2500 - echo "data: [DONE]\n\n";
2501 - flush();
2502 - } else {
2503 - wp_send_json_error([
2504 - 'error_message' => $error_message,
2505 - 'error_code' => 'invalid_embedding'
2506 - ]);
2507 - }
2508 - wp_die();
2509 - }
2510 -
2511 - // Fetch intents from the database
2512 - $table_name = $wpdb->prefix . 'mxchat_intents';
2513 - if ($chat_mode === 'agent') {
2514 - $query = $wpdb->prepare(
2515 - "SELECT * FROM $table_name WHERE callback_function = %s AND (enabled = 1 OR enabled IS NULL)",
2516 - 'mxchat_handle_switch_to_chatbot_intent'
2517 - );
2518 - $intents = $wpdb->get_results($query);
2519 - } else {
2520 - $intents = $wpdb->get_results("SELECT * FROM $table_name WHERE enabled = 1 OR enabled IS NULL");
2521 - }
2522 -
2523 - if (empty($intents)) {
2524 - return false;
2525 - }
2526 -
2527 - // Prefetch individual phrase vectors from wp_mxchat_intent_phrases (grouped by intent_id)
2528 - $phrases_table = $wpdb->prefix . 'mxchat_intent_phrases';
2529 - $phrases_by_intent = [];
2530 - if ($wpdb->get_var("SHOW TABLES LIKE '$phrases_table'") === $phrases_table) {
2531 - $all_phrases = $wpdb->get_results("SELECT intent_id, phrase, embedding_vector FROM $phrases_table");
2532 - foreach ($all_phrases as $p) {
2533 - $phrases_by_intent[$p->intent_id][] = $p;
2534 - }
2535 - }
2536 -
2537 - $highest_similarity = -INF;
2538 - $matched_intent = null;
2539 -
2540 - // Array to store action analysis for testing panel
2541 - $action_analysis = [];
2542 -
2543 - foreach ($intents as $intent) {
2544 - // Additional check for enabled state
2545 - $is_enabled = isset($intent->enabled) ? (bool)$intent->enabled : true;
2546 - if (!$is_enabled) {
2547 - continue;
2548 - }
2549 -
2550 - // Check if this action is enabled for the current bot
2551 - if (!$this->is_action_enabled_for_bot($intent, $current_bot_id)) {
2552 - continue;
2553 - }
2554 -
2555 - $best_similarity = -INF;
2556 - $matched_phrase_text = '';
2557 -
2558 - // Check legacy embedding vector (existing behavior)
2559 - $intent_embedding_serialized = $intent->embedding_vector;
2560 - $intent_embedding = $intent_embedding_serialized
2561 - ? unserialize($intent_embedding_serialized, ['allowed_classes' => false])
2562 - : null;
2563 -
2564 - if (is_array($intent_embedding) && !empty($intent_embedding)) {
2565 - $legacy_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $intent_embedding);
2566 - if ($legacy_similarity > $best_similarity) {
2567 - $best_similarity = $legacy_similarity;
2568 - $matched_phrase_text = 'legacy';
2569 - }
2570 - }
2571 -
2572 - // Check individual phrase vectors
2573 - if (isset($phrases_by_intent[$intent->id])) {
2574 - foreach ($phrases_by_intent[$intent->id] as $phrase_row) {
2575 - $phrase_embedding = $phrase_row->embedding_vector
2576 - ? unserialize($phrase_row->embedding_vector, ['allowed_classes' => false])
2577 - : null;
2578 - if (!is_array($phrase_embedding)) {
2579 - continue;
2580 - }
2581 - $phrase_similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $phrase_embedding);
2582 - if ($phrase_similarity > $best_similarity) {
2583 - $best_similarity = $phrase_similarity;
2584 - $matched_phrase_text = $phrase_row->phrase;
2585 - }
2586 - }
2587 - }
2588 -
2589 - // Skip if no valid embedding was found at all
2590 - if ($best_similarity === -INF) {
2591 - continue;
2592 - }
2593 -
2594 - $similarity = $best_similarity;
2595 - $intent_threshold = isset($intent->similarity_threshold) ? $intent->similarity_threshold : 0.85;
2596 -
2597 - // Store action analysis data for testing panel
2598 - $action_analysis[] = [
2599 - 'intent_label' => $intent->intent_label,
2600 - 'callback_function' => $intent->callback_function,
2601 - 'similarity' => round($similarity, 4),
2602 - 'similarity_percentage' => round($similarity * 100, 2),
2603 - 'threshold' => $intent_threshold,
2604 - 'threshold_percentage' => round($intent_threshold * 100, 2),
2605 - 'above_threshold' => $similarity >= $intent_threshold,
2606 - 'matched_phrase' => $matched_phrase_text,
2607 - 'triggered' => false // Will be updated below if this intent is triggered
2608 - ];
2609 -
2610 - if ($similarity >= $intent_threshold && $similarity > $highest_similarity) {
2611 - $highest_similarity = $similarity;
2612 - $matched_intent = $intent;
2613 - }
2614 - }
2615 -
2616 - // Mark the triggered action if any
2617 - if ($matched_intent) {
2618 - foreach ($action_analysis as &$action) {
2619 - if ($action['intent_label'] === $matched_intent->intent_label) {
2620 - $action['triggered'] = true;
2621 - break;
2622 - }
2623 - }
2624 - }
2625 -
2626 - // Sort actions by similarity (highest first) and store for testing panel
2627 - usort($action_analysis, function($a, $b) {
2628 - return $b['similarity'] <=> $a['similarity'];
2629 - });
2630 -
2631 - // Store action analysis for testing panel capture
2632 - $this->last_action_analysis = $action_analysis;
2633 -
2634 - // Around line 715 in your mxchat_check_intent_and_invoke_callback function
2635 - if ($matched_intent) {
2636 - // If the callback is a method on this instance (core callback), call it directly
2637 - if (method_exists($this, $matched_intent->callback_function)) {
2638 - $callback_result = call_user_func(
2639 - [$this, $matched_intent->callback_function],
2640 - $message,
2641 - $user_id,
2642 - $session_id,
2643 - $matched_intent,
2644 - $user_context ?? null
2645 - );
2646 - } else {
2647 - // Otherwise, use apply_filters for add-on callbacks
2648 - $callback_result = apply_filters(
2649 - $matched_intent->callback_function,
2650 - false,
2651 - $message,
2652 - $user_id,
2653 - $session_id,
2654 - $matched_intent
2655 - );
2656 - }
2657 -
2658 - // Handle the callback result properly
2659 - if ($callback_result !== false) {
2660 - // If callback returned an array with chat_mode, use it directly
2661 - if (is_array($callback_result) && isset($callback_result['chat_mode'])) {
2662 - $this->fallbackResponse = $callback_result;
2663 - return $callback_result; // Return the full array
2664 - } else {
2665 - $this->fallbackResponse = $callback_result;
2666 - return true;
2667 - }
2668 - }
2669 - }
2670 -
2671 - return false;
2672 -}
2673 -
2674 -/**
2675 - * Check if an action is enabled for a specific bot
2676 - */
2677 -private function is_action_enabled_for_bot($intent, $bot_id) {
2678 - // If enabled_bots column doesn't exist or is null, assume it's enabled for all bots (backward compatibility)
2679 - if (!isset($intent->enabled_bots) || empty($intent->enabled_bots)) {
2680 - return true;
2681 - }
2682 -
2683 - $enabled_bots = json_decode($intent->enabled_bots, true);
2684 -
2685 - // If JSON decode fails or returns empty array, assume enabled for all (backward compatibility)
2686 - if (!is_array($enabled_bots) || empty($enabled_bots)) {
2687 - return true;
2688 - }
2689 -
2690 - // Admin testing tab uses bot_id "testing" — treat it as "default" so all
2691 - // default-bot actions are testable from the admin panel
2692 - if ($bot_id === 'testing') {
2693 - $bot_id = 'default';
2694 - }
2695 -
2696 - // Check if the current bot is in the enabled bots list
2697 - return in_array($bot_id, $enabled_bots);
2698 -}
2699 -
2700 -// Helper function to clear PDF and Word document related transients
2701 -private function clear_pdf_transients($session_id) {
2702 - // PDF transients
2703 - delete_transient('mxchat_pdf_url_' . $session_id);
2704 - delete_transient('mxchat_pdf_embeddings_' . $session_id);
2705 - delete_transient('mxchat_include_pdf_in_context_' . $session_id);
2706 - delete_transient('mxchat_waiting_for_pdf_url_' . $session_id);
2707 -
2708 - // Word document transients
2709 - delete_transient('mxchat_word_url_' . $session_id);
2710 - delete_transient('mxchat_word_filename_' . $session_id);
2711 - delete_transient('mxchat_word_embeddings_' . $session_id);
2712 - delete_transient('mxchat_include_word_in_context_' . $session_id);
2713 - delete_transient('mxchat_waiting_for_word_' . $session_id);
2714 -}
2715 -
2716 -
2717 -
2718 -//verified good
2719 -public function mxchat_handle_email_capture($message, $user_id, $session_id) {
2720 - // Get the user's original instruction/message
2721 - $user_instruction = esc_html($this->options['triggered_phrase_response'] ?? esc_html__("Please provide your email address.", 'mxchat'));
2722 -
2723 - // Set instruction for AI - just pass along what the user wanted to say
2724 - $this->current_action_instruction = $user_instruction;
2725 -
2726 - // Set the transient to track email capture flow
2727 - set_transient('mxchat_email_capture_' . $user_id, true, 5 * MINUTE_IN_SECONDS);
2728 -
2729 - // Return false to let the AI generate the response
2730 - return false;
2731 -}
2732 -
2733 -public function mxchat_generate_image($message, $user_id, $session_id) {
2734 - //error_log("Starting image generation for message: " . $message);
2735 -
2736 - // Prepare a prompt for OpenAI image generation
2737 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2738 -
2739 - // Opt-in routing: when 'custom_provider_for_images' is on, route image gen
2740 - // through the configured Custom (OpenAI-compatible) /images/generations route.
2741 - if (!empty($this->options['custom_provider_for_images']) && $this->options['custom_provider_for_images'] === 'on') {
2742 - $image_response = $this->mxchat_generate_custom_image($prompt);
2743 - } else {
2744 - // Use the existing OpenAI API key
2745 - $openai_api_key = sanitize_text_field($this->options['api_key']);
2746 - // Call OpenAI GPT Image to generate an image
2747 - $image_response = $this->mxchat_generate_openai_image($prompt, $openai_api_key);
2748 - }
2749 -
2750 - // Check if the response contains an image URL
2751 - if (isset($image_response['imageUrl'])) {
2752 - $image_url = esc_url_raw($image_response['imageUrl']);
2753 -
2754 - // Construct the HTML with a CSS class instead of inline styles
2755 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2756 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2757 -
2758 - // Save the bot message with both text and HTML
2759 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2760 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2761 -
2762 - // Set the fallback response for the chat handler
2763 - $this->fallbackResponse = [
2764 - 'text' => $response_text,
2765 - 'html' => $response_html,
2766 - 'images' => [$image_url]
2767 - ];
2768 -
2769 - // For debugging/verification - Use json_encode to verify what's being set
2770 - //error_log("Image generation successful - fallbackResponse set: " . json_encode($this->fallbackResponse));
2771 -
2772 - // Return the response directly instead of relying on the property
2773 - return $this->fallbackResponse;
2774 - } else {
2775 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2776 -
2777 - // Save the error message
2778 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2779 -
2780 - // Set the fallback response for the chat handler
2781 - $this->fallbackResponse = [
2782 - 'text' => $response_text,
2783 - 'html' => '',
2784 - 'images' => []
2785 - ];
2786 -
2787 - //error_log("DALL-E image generation error: " . esc_html($image_response['error'] ?? 'Unknown error.'));
2788 - //error_log("Error fallbackResponse set: " . json_encode($this->fallbackResponse));
2789 -
2790 - // Return the response directly instead of relying on the property
2791 - return $this->fallbackResponse;
2792 - }
2793 -}
2794 -
2795 -public function mxchat_generate_gemini_image($message, $user_id, $session_id) {
2796 - $prompt = esc_html__('Create an image of ', 'mxchat') . sanitize_text_field($message);
2797 -
2798 - $gemini_api_key = sanitize_text_field($this->options['gemini_api_key'] ?? '');
2799 - if (empty($gemini_api_key)) {
2800 - $response_text = esc_html__("Gemini API key is not configured.", 'mxchat');
2801 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2802 - return ['text' => $response_text, 'html' => '', 'images' => []];
2803 - }
2804 -
2805 - $image_response = $this->mxchat_generate_imagen_image($prompt, $gemini_api_key);
2806 -
2807 - if (isset($image_response['imageUrl'])) {
2808 - $image_url = esc_url_raw($image_response['imageUrl']);
2809 -
2810 - $response_html = '<img src="' . esc_url($image_url) . '" alt="' . esc_attr__('Generated Image', 'mxchat') . '" class="mxchat-generated-image" />';
2811 - $response_text = esc_html__('Here is the image I generated:', 'mxchat');
2812 -
2813 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2814 - $this->mxchat_save_chat_message($session_id, 'bot', $response_html);
2815 -
2816 - $this->fallbackResponse = [
2817 - 'text' => $response_text,
2818 - 'html' => $response_html,
2819 - 'images' => [$image_url]
2820 - ];
2821 -
2822 - return $this->fallbackResponse;
2823 - } else {
2824 - $response_text = esc_html__("I'm sorry, but I couldn't generate an image based on your request.", 'mxchat');
2825 -
2826 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
2827 -
2828 - $this->fallbackResponse = [
2829 - 'text' => $response_text,
2830 - 'html' => '',
2831 - 'images' => []
2832 - ];
2833 -
2834 - return $this->fallbackResponse;
2835 - }
2836 -}
2837 -
2838 -private function mxchat_save_generated_image($base64_data, $mime_type = 'image/png', $prefix = 'mxchat-generated') {
2839 - // Map the real mime type to a matching file extension so the saved file's
2840 - // extension always agrees with its bytes. A mismatch (e.g. Imagen returning
2841 - // webp bytes that were written into a ".png" file) makes the browser refuse
2842 - // to render the image even though the file saved successfully and the bot
2843 - // reported success — that was the Gemini/Imagen "image never renders" bug.
2844 - // OpenAI + custom-provider paths pass 'image/png' explicitly, so they are
2845 - // unaffected; this only matters for providers that return another type.
2846 - $mime_to_ext = [
2847 - 'image/jpeg' => 'jpg',
2848 - 'image/jpg' => 'jpg',
2849 - 'image/png' => 'png',
2850 - 'image/webp' => 'webp',
2851 - 'image/gif' => 'gif',
2852 - ];
2853 - $mime_type = strtolower(trim((string) $mime_type));
2854 - if (isset($mime_to_ext[$mime_type])) {
2855 - $extension = $mime_to_ext[$mime_type];
2856 - } else {
2857 - // Unknown/unsupported type: fall back to png and normalize the stored
2858 - // mime so the attachment record and the file extension stay consistent.
2859 - $extension = 'png';
2860 - $mime_type = 'image/png';
2861 - }
2862 - $filename = sanitize_file_name($prefix . '-' . wp_generate_uuid4() . '.' . $extension);
2863 - $decoded = base64_decode($base64_data);
2864 -
2865 - if ($decoded === false) {
2866 - return new \WP_Error('decode_failed', esc_html__('Failed to decode image data.', 'mxchat'));
2867 - }
2868 -
2869 - $upload = wp_upload_bits($filename, null, $decoded);
2870 -
2871 - if (!empty($upload['error'])) {
2872 - return new \WP_Error('upload_failed', $upload['error']);
2873 - }
2874 -
2875 - $attach_id = wp_insert_attachment([
2876 - 'post_mime_type' => $mime_type,
2877 - 'post_title' => $prefix,
2878 - 'post_content' => '',
2879 - 'post_status' => 'inherit',
2880 - ], $upload['file']);
2881 -
2882 - if (is_wp_error($attach_id)) {
2883 - return $attach_id;
2884 - }
2885 -
2886 - require_once ABSPATH . 'wp-admin/includes/image.php';
2887 - $metadata = wp_generate_attachment_metadata($attach_id, $upload['file']);
2888 - wp_update_attachment_metadata($attach_id, $metadata);
2889 -
2890 - return esc_url_raw(wp_get_attachment_url($attach_id));
2891 -}
2892 -
2893 -private function mxchat_generate_openai_image($prompt, $api_key, $model = 'gpt-image-1', $timeout = 60) {
2894 - $api_url = 'https://api.openai.com/v1/images/generations';
2895 - $body = json_encode([
2896 - 'prompt' => sanitize_text_field($prompt),
2897 - 'n' => 1,
2898 - 'size' => '1024x1024',
2899 - 'quality' => 'medium',
2900 - 'output_format' => 'png',
2901 - 'model' => sanitize_text_field($model),
2902 - ]);
2903 -
2904 - $args = [
2905 - 'body' => $body,
2906 - 'headers' => [
2907 - 'Content-Type' => 'application/json',
2908 - 'Authorization' => 'Bearer ' . sanitize_text_field($api_key),
2909 - ],
2910 - 'method' => 'POST',
2911 - 'timeout' => absint($timeout),
2912 - ];
2913 -
2914 - $response = wp_remote_post($api_url, $args);
2915 -
2916 - if (is_wp_error($response)) {
2917 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
2918 - }
2919 -
2920 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
2921 -
2922 - $b64 = $response_body['data'][0]['b64_json'] ?? $response_body['data'][0]['b64'] ?? null;
2923 - if ($b64) {
2924 - $saved_url = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-openai');
2925 - if (is_wp_error($saved_url)) {
2926 - return ['error' => $saved_url->get_error_message()];
2927 - }
2928 - return ['imageUrl' => $saved_url];
2929 - } else {
2930 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
2931 - }
2932 -}
2933 -
2934 -/**
2935 - * Generate an image via a Custom (OpenAI-compatible) provider's /images/generations route.
2936 - * Only called when the opt-in 'custom_provider_for_images' setting is on.
2937 - */
2938 -private function mxchat_generate_custom_image($prompt, $timeout = 90) {
2939 - $cfg = $this->mxchat_resolve_custom_provider();
2940 - if (empty($cfg['base_url'])) {
2941 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat')];
2942 - }
2943 - $url = $cfg['base_url'] . '/images/generations';
2944 - if (!empty($cfg['api_version'])) {
2945 - $url .= (strpos($url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
2946 - }
2947 - $body = wp_json_encode([
2948 - 'prompt' => sanitize_text_field($prompt),
2949 - 'n' => 1,
2950 - 'size' => '1024x1024',
2951 - 'model' => $cfg['model'],
2952 - ]);
2953 - $response = wp_remote_post($url, [
2954 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
2955 - 'body' => $body,
2956 - 'method' => 'POST',
2957 - 'timeout' => absint($timeout),
2958 - ]);
2959 - if (is_wp_error($response)) {
2960 - return ['error' => esc_html__('Error generating image (custom provider): ', 'mxchat') . $response->get_error_message()];
2961 - }
2962 - $resp = json_decode(wp_remote_retrieve_body($response), true);
2963 - // Try b64 first (matches OpenAI shape), then url-based fallback.
2964 - $b64 = $resp['data'][0]['b64_json'] ?? $resp['data'][0]['b64'] ?? null;
2965 - if ($b64) {
2966 - $saved = $this->mxchat_save_generated_image($b64, 'image/png', 'mxchat-custom');
2967 - if (is_wp_error($saved)) {
2968 - return ['error' => $saved->get_error_message()];
2969 - }
2970 - return ['imageUrl' => $saved];
2971 - }
2972 - $remote_url = $resp['data'][0]['url'] ?? null;
2973 - if ($remote_url) {
2974 - return ['imageUrl' => esc_url_raw($remote_url)];
2975 - }
2976 - $err_msg = $resp['error']['message'] ?? esc_html__('Custom provider did not return an image.', 'mxchat');
2977 - return ['error' => esc_html($err_msg)];
2978 -}
2979 -
2980 -private function mxchat_generate_imagen_image($prompt, $api_key, $timeout = 60) {
2981 - $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict';
2982 -
2983 - $body = json_encode([
2984 - 'instances' => [['prompt' => sanitize_text_field($prompt)]],
2985 - 'parameters' => [
2986 - 'sampleCount' => 1,
2987 - 'aspectRatio' => '1:1',
2988 - ],
2989 - ]);
2990 -
2991 - $args = [
2992 - 'body' => $body,
2993 - 'headers' => [
2994 - 'Content-Type' => 'application/json',
2995 - 'x-goog-api-key' => sanitize_text_field($api_key),
2996 - ],
2997 - 'method' => 'POST',
2998 - 'timeout' => absint($timeout),
2999 - ];
3000 -
3001 - $response = wp_remote_post($api_url, $args);
3002 -
3003 - if (is_wp_error($response)) {
3004 - return ['error' => esc_html__('Error generating image: ', 'mxchat') . $response->get_error_message()];
3005 - }
3006 -
3007 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
3008 -
3009 - $b64 = $response_body['predictions'][0]['bytesBase64Encoded'] ?? $response_body['predictions'][0]['imageBytes'] ?? null;
3010 - if ($b64) {
3011 - $mime = $response_body['predictions'][0]['mimeType'] ?? 'image/png';
3012 - $saved_url = $this->mxchat_save_generated_image($b64, $mime, 'mxchat-gemini');
3013 - if (is_wp_error($saved_url)) {
3014 - return ['error' => $saved_url->get_error_message()];
3015 - }
3016 - return ['imageUrl' => $saved_url];
3017 - } else {
3018 - return ['error' => esc_html__('Failed to generate image.', 'mxchat')];
3019 - }
3020 -}
3021 -
3022 -/**
3023 - * Handle web search requests.
3024 - *
3025 - * Sends the refined search query to the Brave Search API and uses the
3026 - * results to generate a conversational response with the AI model.
3027 - *
3028 - * @since 1.0.0
3029 - * @param string $message The user's search query.
3030 - * @param string $user_id The user identifier.
3031 - * @param string $session_id The current session ID.
3032 - * @return array Response array containing text with embedded HTML links
3033 - */
3034 -public function mxchat_handle_search_request($message, $user_id, $session_id) {
3035 - // Step 1: Interpret and refine the search query
3036 - $refined_search_query = $this->mxchat_interpret_search_query($message);
3037 - if (empty($refined_search_query)) {
3038 - return array(
3039 - 'text' => esc_html__('I apologize, but could you please rephrase your search request?', 'mxchat'),
3040 - 'html' => ''
3041 - );
3042 - }
3043 -
3044 - // Retrieve and validate API settings
3045 - $options = get_option('mxchat_options');
3046 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3047 - $results_count = isset($options['brave_results_count']) ? absint($options['brave_results_count']) : 5;
3048 -
3049 - if (empty($api_key)) {
3050 - return array(
3051 - 'text' => esc_html__('Search functionality is temporarily unavailable. Please try again later.', 'mxchat'),
3052 - 'html' => ''
3053 - );
3054 - }
3055 -
3056 - // Build the API request URL
3057 - $api_url = add_query_arg(
3058 - array(
3059 - 'q' => rawurlencode($refined_search_query),
3060 - 'count' => $results_count,
3061 - 'text_decorations' => 'true',
3062 - 'rich_data' => 'true',
3063 - ),
3064 - 'https://api.search.brave.com/res/v1/web/search'
3065 - );
3066 -
3067 - // Attempt to retrieve cached results first
3068 - $transient_key = 'mxchat_search_' . md5($refined_search_query);
3069 - $results = get_transient($transient_key);
3070 -
3071 - if (false === $results) {
3072 - // SECURITY FIX: Changed to wp_safe_remote_get
3073 - $response = wp_safe_remote_get(
3074 - $api_url,
3075 - array(
3076 - 'headers' => array(
3077 - 'Accept' => 'application/json',
3078 - 'Accept-Encoding' => 'gzip',
3079 - 'X-Subscription-Token'=> $api_key,
3080 - ),
3081 - 'timeout' => 10,
3082 - )
3083 - );
3084 -
3085 - if (is_wp_error($response)) {
3086 - return array(
3087 - 'text' => esc_html__('I encountered an error while searching. Please try again.', 'mxchat'),
3088 - 'html' => ''
3089 - );
3090 - }
3091 -
3092 - $results = json_decode(wp_remote_retrieve_body($response), true);
3093 -
3094 - if (json_last_error() !== JSON_ERROR_NONE) {
3095 - return array(
3096 - 'text' => esc_html__('I received an invalid response from the search service.', 'mxchat'),
3097 - 'html' => ''
3098 - );
3099 - }
3100 -
3101 - // Cache results for one hour
3102 - set_transient($transient_key, $results, HOUR_IN_SECONDS);
3103 - }
3104 -
3105 - // Process results
3106 - if (!empty($results['web']['results']) && is_array($results['web']['results'])) {
3107 - // Create a more straightforward summary with HTML links
3108 - $search_results_text = '';
3109 -
3110 - // Add a simple intro
3111 - $search_results_text .= sprintf(
3112 - esc_html__("Here's what I found about '%s':", 'mxchat'),
3113 - esc_html($refined_search_query)
3114 - );
3115 -
3116 - // Add the top results with HTML links
3117 - foreach (array_slice($results['web']['results'], 0, 5) as $result) {
3118 - $title = isset($result['title']) ? wp_strip_all_tags($result['title']) : '';
3119 - $url = isset($result['url']) ? esc_url($result['url']) : '';
3120 - $description = isset($result['description']) ? wp_strip_all_tags($result['description']) : '';
3121 -
3122 - // Add a line break after the intro
3123 - $search_results_text .= '<br><br>';
3124 -
3125 - // Add title as a link
3126 - $search_results_text .= sprintf(
3127 - '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a><br>',
3128 - $url,
3129 - $title
3130 - );
3131 -
3132 - // Add a condensed description
3133 - $search_results_text .= sprintf("%s", $description);
3134 - }
3135 -
3136 - // Save to chat history
3137 - $this->mxchat_save_chat_message($session_id, 'bot', $search_results_text);
3138 -
3139 - // Return the formatted text with embedded HTML links
3140 - return array(
3141 - 'text' => $search_results_text,
3142 - 'html' => ''
3143 - );
3144 - } else {
3145 - return array(
3146 - 'text' => sprintf(
3147 - esc_html__('I searched for "%s" but couldn\'t find any relevant results. Would you like to try different search terms?', 'mxchat'),
3148 - esc_html($refined_search_query)
3149 - ),
3150 - 'html' => ''
3151 - );
3152 - }
3153 -}
3154 -
3155 -//very good
3156 -/**
3157 - * Handle image search requests from the chatbot
3158 - *
3159 - * @param string $message The user's search query
3160 - * @param int $user_id The user's ID
3161 - * @param string $session_id The chat session ID
3162 - * @return array Response array with text and HTML content
3163 - */
3164 -public function mxchat_handle_image_search_request($message, $user_id, $session_id) {
3165 - // Step 1: Interpret the search query using the user's selected AI model
3166 - $refined_search_query = $this->mxchat_interpret_search_query($message);
3167 -
3168 - // If no query was interpreted, return a fallback message
3169 - if (empty($refined_search_query)) {
3170 - return array(
3171 - 'text' => __("I'm sorry, I couldn't interpret your search query. Please specify what you'd like to see images of.", 'mxchat'),
3172 - 'html' => "",
3173 - );
3174 - }
3175 -
3176 - // Brave API URL
3177 - $api_url = 'https://api.search.brave.com/res/v1/images/search';
3178 -
3179 - // Retrieve Brave API settings
3180 - $options = get_option('mxchat_options');
3181 - $api_key = isset($options['brave_api_key']) ? sanitize_text_field($options['brave_api_key']) : '';
3182 -
3183 - if (empty($api_key)) {
3184 - return array(
3185 - 'text' => __("API key is not configured. Please set it in the Brave Search Settings.", 'mxchat'),
3186 - 'html' => "",
3187 - );
3188 - }
3189 -
3190 - $image_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3191 - $safe_search = isset($options['brave_safe_search']) ? sanitize_text_field($options['brave_safe_search']) : 'strict';
3192 -
3193 - // Append query parameters based on settings
3194 - $api_url = add_query_arg([
3195 - 'q' => rawurlencode($refined_search_query),
3196 - 'count' => $image_count,
3197 - 'safesearch' => $safe_search,
3198 - ], $api_url);
3199 -
3200 - // Implement caching
3201 - $transient_key = 'mxchat_image_search_' . md5($refined_search_query);
3202 - $body = get_transient($transient_key);
3203 -
3204 - if (false === $body) {
3205 - $args = [
3206 - 'headers' => [
3207 - 'Accept' => 'application/json',
3208 - 'Accept-Encoding' => 'gzip',
3209 - 'X-Subscription-Token' => $api_key,
3210 - ],
3211 - 'timeout' => 10,
3212 - ];
3213 -
3214 - // SECURITY FIX: Changed to wp_safe_remote_get
3215 - $response = wp_safe_remote_get($api_url, $args);
3216 -
3217 - if (is_wp_error($response)) {
3218 - return array(
3219 - 'text' => __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat'),
3220 - 'html' => "",
3221 - );
3222 - }
3223 -
3224 - $body = json_decode(wp_remote_retrieve_body($response), true);
3225 - set_transient($transient_key, $body, HOUR_IN_SECONDS);
3226 - }
3227 -
3228 - // Process the API response
3229 - if (isset($body['results']) && is_array($body['results']) && count($body['results']) > 0) {
3230 - $html_output = '<div class="mxchat-image-gallery">';
3231 -
3232 - // Get the configured image count (1-6)
3233 - $display_count = isset($options['brave_image_count']) ? intval($options['brave_image_count']) : 4;
3234 - $display_count = min($display_count, count($body['results'])); // Make sure we don't exceed available images
3235 -
3236 - // Use only the requested number of images
3237 - for ($i = 0; $i < $display_count; $i++) {
3238 - $image = $body['results'][$i];
3239 - $image_url = isset($image['url']) ? esc_url($image['url']) : '';
3240 - $thumbnail_url = isset($image['thumbnail']['src']) ? esc_url($image['thumbnail']['src']) : '';
3241 - $title = isset($image['title']) ? esc_html($image['title']) : esc_html__('Image', 'mxchat');
3242 -
3243 - if ($image_url && $thumbnail_url) {
3244 - $html_output .= '<div class="mxchat-image-item">';
3245 - $html_output .= '<strong class="mxchat-image-title">' . $title . '</strong>';
3246 - $html_output .= '<a href="' . $image_url . '" target="_blank" rel="noopener noreferrer" class="mxchat-image-link">';
3247 - $html_output .= '<img src="' . $thumbnail_url . '" alt="' . $title . '" class="mxchat-image-thumbnail">';
3248 - $html_output .= '</a></div>';
3249 - }
3250 - }
3251 -
3252 - $html_output .= '</div>';
3253 -
3254 - // Create response text
3255 - $response_text = sprintf(__("Here are some images of %s:", 'mxchat'), $refined_search_query);
3256 -
3257 - // Save both response text and HTML to chat history
3258 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3259 - $this->mxchat_save_chat_message($session_id, 'bot', $html_output);
3260 -
3261 - // Return the combined response
3262 - return array(
3263 - 'text' => $response_text,
3264 - 'html' => $html_output,
3265 - );
3266 - } else {
3267 - $response_text = __("I'm sorry, I couldn't retrieve any images based on your request.", 'mxchat');
3268 -
3269 - // Save the error message to chat history
3270 - $this->mxchat_save_chat_message($session_id, 'bot', $response_text);
3271 -
3272 - return array(
3273 - 'text' => $response_text,
3274 - 'html' => "",
3275 - );
3276 - }
3277 -}
3278 -
3279 -/**
3280 - * Interpret the search query using the user's selected AI model
3281 - *
3282 - * @param string $user_query The original query from the user
3283 - * @return string The refined search query
3284 - */
3285 -public function mxchat_interpret_search_query($user_query) {
3286 - $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');
3287 -
3288 - // Get options and determine the selected model
3289 - $options = $this->options ?? get_option('mxchat_options');
3290 - $selected_model = isset($options['model']) ? $options['model'] : 'gpt-5.1-chat-latest';
3291 -
3292 - // Custom (OpenAI-compatible) provider routes by model id, not prefix.
3293 - if ($selected_model === 'custom-provider') {
3294 - return $this->interpret_query_with_custom($user_query, $system_prompt);
3295 - }
3296 -
3297 - // Extract model prefix to determine the provider
3298 - $model_parts = explode('-', $selected_model);
3299 - $provider = strtolower($model_parts[0]);
3300 -
3301 - // Determine which API key to use based on the provider
3302 - switch ($provider) {
3303 - case 'gemini':
3304 - $api_key = isset($options['gemini_api_key']) ? sanitize_text_field($options['gemini_api_key']) : '';
3305 - if (empty($api_key)) {
3306 - return sanitize_text_field($user_query); // Default to original query if API key missing
3307 - }
3308 - return $this->interpret_query_with_gemini($user_query, $system_prompt, $api_key, $selected_model);
3309 -
3310 - case 'claude':
3311 - $api_key = isset($options['claude_api_key']) ? sanitize_text_field($options['claude_api_key']) : '';
3312 - if (empty($api_key)) {
3313 - return sanitize_text_field($user_query);
3314 - }
3315 - return $this->interpret_query_with_claude($user_query, $system_prompt, $api_key, $selected_model);
3316 -
3317 - case 'grok':
3318 - $api_key = isset($options['xai_api_key']) ? sanitize_text_field($options['xai_api_key']) : '';
3319 - if (empty($api_key)) {
3320 - return sanitize_text_field($user_query);
3321 - }
3322 - return $this->interpret_query_with_xai($user_query, $system_prompt, $api_key, $selected_model);
3323 -
3324 - case 'deepseek':
3325 - $api_key = isset($options['deepseek_api_key']) ? sanitize_text_field($options['deepseek_api_key']) : '';
3326 - if (empty($api_key)) {
3327 - return sanitize_text_field($user_query);
3328 - }
3329 - return $this->interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $selected_model);
3330 -
3331 - case 'gpt':
3332 - default:
3333 - // Default to OpenAI for custom models or unrecognized prefixes
3334 - $api_key = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
3335 - if (empty($api_key)) {
3336 - return sanitize_text_field($user_query);
3337 - }
3338 - return $this->interpret_query_with_openai($user_query, $system_prompt, $api_key, $selected_model);
3339 - }
3340 -}
3341 -
3342 -/**
3343 - * Interpret query against the configured Custom (OpenAI-compatible) provider.
3344 - * Uses the same base URL + auth scheme as the chat dispatcher.
3345 - */
3346 -private function interpret_query_with_custom($user_query, $system_prompt) {
3347 - $cfg = $this->mxchat_resolve_custom_provider();
3348 - if (empty($cfg['base_url'])) {
3349 - return sanitize_text_field($user_query);
3350 - }
3351 - $args = [
3352 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
3353 - 'body' => wp_json_encode([
3354 - 'model' => $cfg['model'],
3355 - 'messages' => [
3356 - ['role' => 'system', 'content' => $system_prompt],
3357 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3358 - ],
3359 - 'temperature' => 0.2,
3360 - 'max_tokens' => 20,
3361 - ]),
3362 - 'method' => 'POST',
3363 - 'timeout' => 15,
3364 - ];
3365 - $response = wp_remote_post($cfg['chat_url'], $args);
3366 - if (is_wp_error($response)) {
3367 - return sanitize_text_field($user_query);
3368 - }
3369 - $body = json_decode(wp_remote_retrieve_body($response), true);
3370 - return isset($body['choices'][0]['message']['content'])
3371 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3372 - : sanitize_text_field($user_query);
3373 -}
3374 -
3375 -/**
3376 - * Convert the colon-style header list returned by mxchat_resolve_custom_provider
3377 - * into the assoc-array form wp_remote_post expects.
3378 - */
3379 -private function mxchat_custom_provider_assoc_headers($cfg) {
3380 - $headers = ['Content-Type' => 'application/json'];
3381 - if (!empty($cfg['api_key'])) {
3382 - if (($cfg['auth_scheme'] ?? 'bearer') === 'api-key') {
3383 - $headers['api-key'] = $cfg['api_key'];
3384 - } else {
3385 - $headers['Authorization'] = 'Bearer ' . $cfg['api_key'];
3386 - }
3387 - }
3388 - return $headers;
3389 -}
3390 -
3391 -/**
3392 - * Interpret query using OpenAI models
3393 - */
3394 -private function interpret_query_with_openai($user_query, $system_prompt, $api_key, $model = 'gpt-5.1-chat-latest') {
3395 - $url = 'https://api.openai.com/v1/chat/completions';
3396 - $args = [
3397 - 'headers' => [
3398 - 'Authorization' => 'Bearer ' . $api_key,
3399 - 'Content-Type' => 'application/json',
3400 - ],
3401 - 'body' => wp_json_encode([
3402 - 'model' => $model,
3403 - 'messages' => [
3404 - ['role' => 'system', 'content' => $system_prompt],
3405 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3406 - ],
3407 - 'temperature' => 0.2,
3408 - 'max_tokens' => 20,
3409 - ]),
3410 - 'method' => 'POST',
3411 - 'timeout' => 15,
3412 - ];
3413 -
3414 - $response = wp_remote_post($url, $args);
3415 - if (is_wp_error($response)) {
3416 - return sanitize_text_field($user_query);
3417 - }
3418 -
3419 - $body = json_decode(wp_remote_retrieve_body($response), true);
3420 - return isset($body['choices'][0]['message']['content'])
3421 - ? sanitize_text_field(trim($body['choices'][0]['message']['content']))
3422 - : sanitize_text_field($user_query);
3423 -}
3424 -
3425 -/**
3426 - * Anthropic removed temperature/top_p/top_k starting with Opus 4.7 (the API
3427 - * returns 400 if sent) — add new flagship model ids here. (We don't send
3428 - * top_p/top_k in any Claude body, so the list only needs to gate temperature
3429 - * stripping. We never send a `thinking` param either, which is required for
3430 - * claude-fable-5: it rejects an explicit thinking "disabled" — omit only.)
3431 - */
3432 -private function mxchat_claude_omits_temperature($model) {
3433 - $no_temp = array('claude-opus-4-7', 'claude-opus-4-8', 'claude-fable-5');
3434 - return in_array($model, $no_temp, true);
3435 -}
3436 -
3437 -/**
3438 - * Interpret query using Claude models
3439 - */
3440 -private function interpret_query_with_claude($user_query, $system_prompt, $api_key, $model) {
3441 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
3442 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
3443 - if ($model === 'claude-opus-4-20250514') { $model = 'claude-opus-4-8'; }
3444 - elseif ($model === 'claude-sonnet-4-20250514') { $model = 'claude-sonnet-4-6'; }
3445 - $url = 'https://api.anthropic.com/v1/messages';
3446 -
3447 - $payload = [
3448 - 'model' => $model,
3449 - 'system' => $system_prompt,
3450 - 'messages' => [
3451 - ['role' => 'user', 'content' => sanitize_text_field($user_query)]
3452 - ],
3453 - 'max_tokens' => 20,
3454 - 'temperature' => 0.2,
3455 - ];
3456 - if ($this->mxchat_claude_omits_temperature($model)) { unset($payload['temperature']); }
3457 -
3458 - $args = [
3459 - 'headers' => [
3460 - 'Content-Type' => 'application/json',
3461 - 'x-api-key' => $api_key,
3462 - 'anthropic-version' => '2023-06-01',
3463 - ],
3464 - 'body' => wp_json_encode($payload),
3465 - 'method' => 'POST',
3466 - 'timeout' => 15,
3467 - ];
3468 -
3469 - $response = wp_remote_post($url, $args);
3470 - if (is_wp_error($response)) {
3471 - return sanitize_text_field($user_query);
3472 - }
3473 -
3474 - $body = json_decode(wp_remote_retrieve_body($response), true);
3475 - // claude-fable-5 prepends a thinking block to content — take the first
3476 - // TEXT block, not content[0].
3477 - foreach ((array) ($body['content'] ?? array()) as $block) {
3478 - if (isset($block['type'], $block['text']) && $block['type'] === 'text' && trim($block['text']) !== '') {
3479 - return sanitize_text_field(trim($block['text']));
3480 - }
3481 - }
3482 -
3483 - return sanitize_text_field($user_query);
3484 -}
3485 -
3486 -/**
3487 - * Interpret query using Gemini models
3488 - */
3489 -private function interpret_query_with_gemini($user_query, $system_prompt, $api_key, $model) {
3490 - if ($model === 'gemini-3-pro-preview') {
3491 - $model = 'gemini-3.1-pro-preview';
3492 - }
3493 - // Use v1beta for preview models, v1 for stable models
3494 - $api_version = (strpos($model, 'preview') !== false || strpos($model, 'exp') !== false) ? 'v1beta' : 'v1';
3495 -
3496 - $url = "https://generativelanguage.googleapis.com/{$api_version}/models/{$model}:generateContent?key=" . urlencode($api_key);
3497 -
3498 - $args = [
3499 - 'headers' => [
3500 - 'Content-Type' => 'application/json',
3501 - ],
3502 - 'body' => wp_json_encode([
3503 - 'contents' => [
3504 - [
3505 - 'role' => 'user',
3506 - 'parts' => [
3507 - ['text' => $system_prompt . "\n\nQuery: " . sanitize_text_field($user_query)]
3508 - ]
3509 - ]
3510 - ],
3511 - 'generationConfig' => [
3512 - 'temperature' => 0.2,
3513 - 'maxOutputTokens' => 20,
3514 - ],
3515 - ]),
3516 - 'method' => 'POST',
3517 - 'timeout' => 15,
3518 - ];
3519 -
3520 - $response = wp_remote_post($url, $args);
3521 - if (is_wp_error($response)) {
3522 - return sanitize_text_field($user_query);
3523 - }
3524 -
3525 - $body = json_decode(wp_remote_retrieve_body($response), true);
3526 - if (!empty($body['candidates'][0]['content']['parts'][0]['text'])) {
3527 - return sanitize_text_field(trim($body['candidates'][0]['content']['parts'][0]['text']));
3528 - }
3529 -
3530 - return sanitize_text_field($user_query);
3531 -}
3532 -
3533 -/**
3534 - * Interpret query using X.AI (Grok) models
3535 - */
3536 -private function interpret_query_with_xai($user_query, $system_prompt, $api_key, $model) {
3537 - $url = 'https://api.xai.com/v1/chat/completions';
3538 -
3539 - $args = [
3540 - 'headers' => [
3541 - 'Content-Type' => 'application/json',
3542 - 'Authorization' => 'Bearer ' . $api_key,
3543 - ],
3544 - 'body' => wp_json_encode([
3545 - 'model' => $model,
3546 - 'messages' => [
3547 - ['role' => 'system', 'content' => $system_prompt],
3548 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3549 - ],
3550 - 'temperature' => 0.2,
3551 - 'max_tokens' => 20,
3552 - ]),
3553 - 'method' => 'POST',
3554 - 'timeout' => 15,
3555 - ];
3556 -
3557 - $response = wp_remote_post($url, $args);
3558 - if (is_wp_error($response)) {
3559 - return sanitize_text_field($user_query);
3560 - }
3561 -
3562 - $body = json_decode(wp_remote_retrieve_body($response), true);
3563 - if (isset($body['choices'][0]['message']['content'])) {
3564 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3565 - }
3566 -
3567 - return sanitize_text_field($user_query);
3568 -}
3569 -
3570 -/**
3571 - * Interpret query using DeepSeek models
3572 - */
3573 -private function interpret_query_with_deepseek($user_query, $system_prompt, $api_key, $model) {
3574 - $url = 'https://api.deepseek.com/v1/chat/completions';
3575 -
3576 - $args = [
3577 - 'headers' => [
3578 - 'Content-Type' => 'application/json',
3579 - 'Authorization' => 'Bearer ' . $api_key,
3580 - ],
3581 - 'body' => wp_json_encode([
3582 - 'model' => $model,
3583 - 'messages' => [
3584 - ['role' => 'system', 'content' => $system_prompt],
3585 - ['role' => 'user', 'content' => sanitize_text_field($user_query)],
3586 - ],
3587 - 'temperature' => 0.2,
3588 - 'max_tokens' => 20,
3589 - ]),
3590 - 'method' => 'POST',
3591 - 'timeout' => 15,
3592 - ];
3593 -
3594 - $response = wp_remote_post($url, $args);
3595 - if (is_wp_error($response)) {
3596 - return sanitize_text_field($user_query);
3597 - }
3598 -
3599 - $body = json_decode(wp_remote_retrieve_body($response), true);
3600 - if (isset($body['choices'][0]['message']['content'])) {
3601 - return sanitize_text_field(trim($body['choices'][0]['message']['content']));
3602 - }
3603 -
3604 - return sanitize_text_field($user_query);
3605 -}
3606 -
3607 -//very good
3608 -private function add_email_to_loops($email) {
3609 - // Sanitize the email
3610 - $email = sanitize_email($email);
3611 -
3612 - // Retrieve and sanitize options
3613 - $api_key = isset($this->options['loops_api_key']) ? sanitize_text_field($this->options['loops_api_key']) : '';
3614 - $mailing_list_id = isset($this->options['loops_mailing_list']) ? sanitize_text_field($this->options['loops_mailing_list']) : '';
3615 -
3616 - // Check for missing API key or mailing list ID
3617 - if (empty($api_key) || empty($mailing_list_id)) {
3618 - //error_log(esc_html__('Loops API key or mailing list ID is missing.', 'mxchat'));
3619 - return;
3620 - }
3621 -
3622 - $data = array(
3623 - 'email' => $email,
3624 - 'subscribed' => true,
3625 - 'source' => __('MxChat AI Chatbot', 'mxchat'),
3626 - 'mailingLists' => array($mailing_list_id => true),
3627 - );
3628 -
3629 - $url = 'https://app.loops.so/api/v1/contacts/create';
3630 - $args = array(
3631 - 'body' => wp_json_encode($data),
3632 - 'headers' => array(
3633 - 'Authorization' => 'Bearer ' . $api_key,
3634 - 'Content-Type' => 'application/json',
3635 - ),
3636 - 'method' => 'POST',
3637 - 'timeout' => 45,
3638 - );
3639 -
3640 - $response = wp_remote_post($url, $args);
3641 -
3642 - // Handle errors in the API request
3643 - if (is_wp_error($response)) {
3644 - //error_log(esc_html__('Error adding email to Loops: ', 'mxchat') . $response->get_error_message());
3645 - return;
3646 - }
3647 -
3648 - // Check for non-200 HTTP responses
3649 - $response_code = wp_remote_retrieve_response_code($response);
3650 - if ($response_code != 200) {
3651 - $response_body = wp_remote_retrieve_body($response);
3652 - //error_log(esc_html__('Loops API responded with code ', 'mxchat') . $response_code . ': ' . $response_body);
3653 - }
3654 -}
3655 -
3656 -public function mxchat_handle_pdf_discussion($message, $user_id, $session_id) {
3657 - // Get the maximum number of pages allowed from admin settings
3658 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3659 -
3660 - // Retrieve options for dynamic texts
3661 - $trigger_text = $this->options['pdf_intent_trigger_text'] ?? __("Please provide the URL to the PDF you'd like to discuss.", 'mxchat');
3662 - $success_text = $this->options['pdf_intent_success_text'] ?? __("I've processed the PDF. What questions do you have about it?", 'mxchat');
3663 - $error_text = $this->options['pdf_intent_error_text'] ?? __("Sorry, I couldn't process the PDF. Please ensure it's a valid file.", 'mxchat');
3664 -
3665 - // Check for explicit request for new PDF
3666 - $new_pdf_requested = stripos($message, 'new') !== false ||
3667 - stripos($message, 'another') !== false ||
3668 - stripos($message, 'different') !== false;
3669 -
3670 - // If user mentions adding/reading a PDF, set waiting flag
3671 - if (stripos($message, 'pdf') !== false ||
3672 - stripos($message, 'document') !== false ||
3673 - stripos($message, 'read') !== false) {
3674 - set_transient('mxchat_waiting_for_pdf_url_' . $session_id, true, HOUR_IN_SECONDS);
3675 - $this->fallbackResponse['text'] = $trigger_text;
3676 - return;
3677 - }
3678 -
3679 - // If we're waiting for a URL or user requested new PDF
3680 - if ($new_pdf_requested || get_transient('mxchat_waiting_for_pdf_url_' . $session_id)) {
3681 - if (preg_match('/https?:\/\/[^\s"]+/i', $message, $matches)) {
3682 - // Process URL... (rest of your existing URL processing code)
3683 - } else {
3684 - $this->fallbackResponse['text'] = $trigger_text;
3685 - }
3686 - return;
3687 - }
3688 -
3689 - // Default to proceeding with conversation if no specific PDF action is needed
3690 - $this->fallbackResponse['text'] = '';
3691 -}
3692 -
3693 -
3694 -/**
3695 - * Enhanced fetch_and_split_pdf_pages with SSRF protection
3696 - */
3697 -private function fetch_and_split_pdf_pages($pdf_source, $max_pages) {
3698 - // CLEAR DEBUG LOGGING
3699 - //error_log("=== MXCHAT PDF PROCESSING START ===");
3700 - //error_log("PDF Source: " . $pdf_source);
3701 - //error_log("Max Pages: " . $max_pages);
3702 - //error_log("Session ID: " . ($this->session_id ?? 'not set'));
3703 -
3704 - // Check if Advanced Claude Toolbar is available and enabled
3705 - $claude_available = function_exists('mxchatACT_is_advanced_claude_enabled');
3706 - $claude_enabled = $claude_available ? mxchatACT_is_advanced_claude_enabled() : false;
3707 -
3708 - //error_log("Claude Function Available: " . ($claude_available ? 'YES' : 'NO'));
3709 - //error_log("Claude Enabled: " . ($claude_enabled ? 'YES' : 'NO'));
3710 -
3711 - if ($claude_available && $claude_enabled) {
3712 - //error_log("🚀 ATTEMPTING CLAUDE PROCESSING...");
3713 -
3714 - // Attempt Claude processing first
3715 - $claude_result = apply_filters('mxchat_process_pdf_advanced', false, $pdf_source, $max_pages, $this->session_id);
3716 -
3717 - if ($claude_result !== false && is_array($claude_result) && !empty($claude_result)) {
3718 - //error_log("✅ CLAUDE PROCESSING SUCCESSFUL!");
3719 - //error_log("Claude returned " . count($claude_result) . " processed pages");
3720 -
3721 - // Log first page details for verification
3722 - if (isset($claude_result[0])) {
3723 - $first_page = $claude_result[0];
3724 - //error_log("First page enhanced: " . (isset($first_page['enhanced']) && $first_page['enhanced'] ? 'YES' : 'NO'));
3725 - //error_log("Processing method: " . ($first_page['processing_method'] ?? 'not set'));
3726 - //error_log("First page text preview: " . substr($first_page['text'] ?? '', 0, 100) . "...");
3727 - }
3728 -
3729 - //error_log("=== MXCHAT PDF PROCESSING END (CLAUDE) ===");
3730 - return $claude_result;
3731 - } else {
3732 - //error_log("❌ CLAUDE PROCESSING FAILED or returned invalid result");
3733 - //error_log("Claude result type: " . gettype($claude_result));
3734 - if (is_array($claude_result)) {
3735 - //error_log("Claude result count: " . count($claude_result));
3736 - }
3737 - }
3738 - }
3739 -
3740 - // Fallback to basic processing
3741 - //error_log("🔄 FALLING BACK TO BASIC PDF PROCESSING...");
3742 -
3743 - $upload_dir = wp_upload_dir();
3744 - $temp_file = null;
3745 -
3746 - try {
3747 - // Your existing basic processing code here...
3748 - // (I'll include the key parts with debug logging)
3749 -
3750 - if (filter_var($pdf_source, FILTER_VALIDATE_URL)) {
3751 - //error_log("Downloading PDF from URL...");
3752 -
3753 - // SECURITY FIX: Validate URL before processing
3754 - if (!$this->mxchat_is_safe_pdf_url($pdf_source)) {
3755 - //error_log("❌ SECURITY: Blocked unsafe PDF URL");
3756 - return false;
3757 - }
3758 -
3759 - $temp_file = wp_tempnam($pdf_source);
3760 -
3761 - // SECURITY FIX: Changed from wp_remote_get to wp_safe_remote_get
3762 - $response = wp_safe_remote_get($pdf_source, [
3763 - 'timeout' => 60,
3764 - 'headers' => ['User-Agent' => 'MxChat PDF Processor']
3765 - ]);
3766 -
3767 - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
3768 - $error_message = is_wp_error($response) ? $response->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code($response);
3769 - //error_log("❌ BASIC PROCESSING: Failed to download PDF: " . $error_message);
3770 - return false;
3771 - }
3772 -
3773 - global $wp_filesystem;
3774 - if (empty($wp_filesystem)) {
3775 - require_once ABSPATH . 'wp-admin/includes/file.php';
3776 - WP_Filesystem();
3777 - }
3778 - $wp_filesystem->put_contents($temp_file, wp_remote_retrieve_body($response), FS_CHMOD_FILE);
3779 - //error_log("✅ PDF downloaded successfully");
3780 - } else {
3781 - $temp_file = $pdf_source;
3782 - //error_log("Using local PDF file: " . $temp_file);
3783 - }
3784 -
3785 - // Parse PDF
3786 - //error_log("Parsing PDF with basic parser...");
3787 - mxchat_load_pdf_parser();
3788 - $parser = new \Smalot\PdfParser\Parser();
3789 - $pdf = $parser->parseFile($temp_file);
3790 - $pages = $pdf->getPages();
3791 -
3792 - //error_log("PDF contains " . count($pages) . " pages");
3793 -
3794 - if (count($pages) > $max_pages) {
3795 - //error_log("❌ BASIC PROCESSING: Too many pages (" . count($pages) . " > " . $max_pages . ")");
3796 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file) {
3797 - unlink($temp_file);
3798 - }
3799 - return 'too_many_pages';
3800 - }
3801 -
3802 - $embeddings = [];
3803 - $processed_pages = 0;
3804 -
3805 - foreach ($pages as $page_number => $page) {
3806 - $text = $page->getText();
3807 -
3808 - if (empty(trim($text))) {
3809 - //error_log("Skipping empty page: " . ($page_number + 1));
3810 - continue;
3811 - }
3812 -
3813 - $text = $this->mxchat_clean_text($text);
3814 -
3815 - $embedding = $this->mxchat_generate_embedding(
3816 - __("Page ", 'mxchat') . ($page_number + 1) . ": " . $text,
3817 - $this->options['api_key']
3818 - );
3819 -
3820 - if ($embedding) {
3821 - $embeddings[] = [
3822 - 'page_number' => $page_number + 1,
3823 - 'embedding' => $embedding,
3824 - 'text' => $text,
3825 - 'enhanced' => false, // CLEARLY MARK AS BASIC
3826 - 'processing_method' => 'basic_pdf_parser'
3827 - ];
3828 - $processed_pages++;
3829 - }
3830 - }
3831 -
3832 - //error_log("✅ BASIC PROCESSING COMPLETE: " . $processed_pages . " pages processed");
3833 -
3834 - // Cleanup
3835 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3836 - unlink($temp_file);
3837 - }
3838 -
3839 - //error_log("=== MXCHAT PDF PROCESSING END (BASIC) ===");
3840 - return $embeddings;
3841 -
3842 - } catch (\Exception $e) {
3843 - //error_log("❌ BASIC PROCESSING ERROR: " . $e->getMessage());
3844 - if (filter_var($pdf_source, FILTER_VALIDATE_URL) && $temp_file && file_exists($temp_file)) {
3845 - unlink($temp_file);
3846 - }
3847 - //error_log("=== MXCHAT PDF PROCESSING END (ERROR) ===");
3848 - return false;
3849 - }
3850 -}
3851 -
3852 -
3853 -/**
3854 - * Validate PDF URL for security
3855 - * Prevents SSRF attacks by blocking dangerous URLs
3856 - */
3857 -
3858 -private function mxchat_is_safe_pdf_url($url) {
3859 - // Use WordPress core function for comprehensive validation
3860 - // This blocks localhost, private IPs, and reserved IP ranges
3861 - $validated_url = wp_http_validate_url($url);
3862 -
3863 - if ($validated_url === false) {
3864 - return false;
3865 - }
3866 -
3867 - // Additional check: only allow HTTP/HTTPS schemes
3868 - $parsed = parse_url($url);
3869 - if (!isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
3870 - return false;
3871 - }
3872 -
3873 - return true;
3874 -}
3875 -
3876 -
3877 -private function mxchat_clean_text($text) {
3878 - // Remove excessive whitespace
3879 - $text = preg_replace('/\s+/', ' ', $text);
3880 -
3881 - // Remove control characters except newlines and tabs
3882 - $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $text);
3883 -
3884 - // Normalize line endings
3885 - $text = str_replace(["\r\n", "\r"], "\n", $text);
3886 -
3887 - // Trim whitespace
3888 - $text = trim($text);
3889 -
3890 - return $text;
3891 -}
3892 -
3893 -private function find_relevant_pdf_pages($query_embedding, $embeddings) {
3894 - //error_log(esc_html__("find_relevant_pdf_pages called.", 'mxchat'));
3895 -
3896 - $most_relevant = null;
3897 - $highest_similarity = -INF;
3898 -
3899 - foreach ($embeddings as $page_data) {
3900 - $similarity = $this->mxchat_calculate_cosine_similarity($query_embedding, $page_data['embedding']);
3901 -
3902 - if ($similarity > $highest_similarity) {
3903 - $highest_similarity = $similarity;
3904 - $most_relevant = $page_data['page_number'];
3905 - }
3906 - }
3907 -
3908 - if (!is_null($most_relevant)) {
3909 - $page_numbers = range(max(1, $most_relevant - 1), min(count($embeddings), $most_relevant + 1));
3910 - return array_filter($embeddings, function ($page) use ($page_numbers) {
3911 - return in_array($page['page_number'], $page_numbers);
3912 - });
3913 - }
3914 -
3915 - return [];
3916 -}
3917 -
3918 -
3919 -public function handle_pdf_upload() {
3920 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
3921 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
3922 - }
3923 -
3924 - if (!isset($_FILES['pdf_file']) || !isset($_POST['session_id'])) {
3925 - wp_send_json_error(esc_html__('Missing required parameters.', 'mxchat'));
3926 - return;
3927 - }
3928 -
3929 - // SECURITY FIX: Check if PDF uploads are enabled in settings
3930 - $options = get_option('mxchat_options', array());
3931 - $show_pdf_button = isset($options['show_pdf_upload_button']) ? $options['show_pdf_upload_button'] : 'on';
3932 -
3933 - if ($show_pdf_button !== 'on') {
3934 - wp_send_json_error(esc_html__('PDF uploads are currently disabled.', 'mxchat'));
3935 - return;
3936 - }
3937 -
3938 - $file = $_FILES['pdf_file'];
3939 - $session_id = sanitize_text_field($_POST['session_id']);
3940 - $original_filename = sanitize_text_field($file['name']);
3941 -
3942 - // Update session owner if it changed (e.g. IP changed due to network switch)
3943 - $current_user_identifier = MxChat_User::mxchat_get_user_identifier();
3944 - $session_owner = get_option("mxchat_session_owner_{$session_id}");
3945 -
3946 - if (!$session_owner || $session_owner !== $current_user_identifier) {
3947 - update_option("mxchat_session_owner_{$session_id}", $current_user_identifier, 'no');
3948 - }
3949 -
3950 - $file_type = wp_check_filetype($file['name'], ['pdf' => 'application/pdf']);
3951 - if ($file_type['type'] !== 'application/pdf') {
3952 - wp_send_json_error(esc_html__('Invalid file type. Only PDF files are allowed.', 'mxchat'));
3953 - return;
3954 - }
3955 -
3956 - $upload_dir = wp_upload_dir();
3957 -
3958 - // SECURITY FIX: Generate random filename without exposing session_id
3959 - $random_string = wp_generate_password(20, false, false); // 20 char alphanumeric string
3960 - $pdf_filename = 'mxchat_' . $random_string . '_' . time() . '.pdf';
3961 - $pdf_path = $upload_dir['path'] . '/' . $pdf_filename;
3962 -
3963 - if (!move_uploaded_file($file['tmp_name'], $pdf_path)) {
3964 - wp_send_json_error(esc_html__('Failed to upload file.', 'mxchat'));
3965 - return;
3966 - }
3967 -
3968 - $this->clear_pdf_transients($session_id);
3969 -
3970 - $max_pages = isset($this->options['pdf_max_pages']) ? intval($this->options['pdf_max_pages']) : 69;
3971 - $embeddings = $this->fetch_and_split_pdf_pages($pdf_path, $max_pages);
3972 -
3973 - if ($embeddings === 'too_many_pages') {
3974 - unlink($pdf_path);
3975 - $error_message = sprintf(
3976 - $this->options['pdf_intent_error_text'] ??
3977 - esc_html__("The provided PDF exceeds the maximum allowed limit of %d pages. Please provide a smaller document.", 'mxchat'),
3978 - $max_pages
3979 - );
3980 - wp_send_json_error($error_message);
3981 - return;
3982 - }
3983 -
3984 - if ($embeddings === false || empty($embeddings)) {
3985 - unlink($pdf_path);
3986 - $error_message = $this->options['pdf_intent_error_text'] ??
3987 - esc_html__('The uploaded PDF appears to be empty or contains unsupported content.', 'mxchat');
3988 - wp_send_json_error($error_message);
3989 - return;
3990 - }
3991 -
3992 - if (!empty($embeddings)) {
3993 - // Store the mapping between session and the random filename
3994 - set_transient('mxchat_pdf_url_' . $session_id, $pdf_path, HOUR_IN_SECONDS);
3995 - set_transient('mxchat_pdf_filename_' . $session_id, $original_filename, HOUR_IN_SECONDS);
3996 - set_transient('mxchat_pdf_embeddings_' . $session_id, $embeddings, HOUR_IN_SECONDS);
3997 - set_transient('mxchat_include_pdf_in_context_' . $session_id, true, HOUR_IN_SECONDS);
3998 -
3999 - $success_message = $this->options['pdf_intent_success_text'] ??
4000 - esc_html__("I've processed the PDF. What questions do you have about it?", 'mxchat');
4001 -
4002 - wp_send_json_success([
4003 - 'message' => $success_message,
4004 - 'filename' => $original_filename
4005 - ]);
4006 - return;
4007 - }
4008 -
4009 - unlink($pdf_path);
4010 - $error_message = $this->options['pdf_intent_error_text'] ??
4011 - esc_html__('Sorry, I couldn\'t process the PDF. Please ensure it\'s a valid file.', 'mxchat');
4012 - wp_send_json_error($error_message);
4013 - return;
4014 -}
4015 -public function handle_pdf_remove() {
4016 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce(wp_unslash((string) $_POST['nonce']))) {
4017 - wp_send_json_error(array('message' => esc_html__('Invalid nonce.', 'mxchat')), 403);
4018 - }
4019 -
4020 - if (empty($_POST['session_id'])) {
4021 - wp_send_json_error(esc_html__('Session ID missing.', 'mxchat'));
4022 - wp_die();
4023 - }
4024 -
4025 - $session_id = sanitize_text_field($_POST['session_id']);
4026 - $pdf_path = get_transient('mxchat_pdf_url_' . $session_id);
4027 -
4028 - if ($pdf_path && file_exists($pdf_path)) {
4029 - unlink($pdf_path);
4030 - }
4031 -
4032 - $this->clear_pdf_transients($session_id);
4033 -
4034 - wp_send_json_success([
4035 - 'message' => esc_html__('PDF removed successfully.', 'mxchat')
4036 - ]);
4037 - wp_die();
4038 -}
4039 -
4040 -
4041 -function mxchat_fetch_new_messages() {
4042 - $session_id = sanitize_text_field($_POST['session_id']);
4043 - $last_seen_id = sanitize_text_field($_POST['last_seen_id']);
4044 - $persistence_enabled = $_POST['persistence_enabled'] === 'true';
4045 - $initial_timestamp = isset($_POST['initial_timestamp']) ? intval($_POST['initial_timestamp']) : 0;
4046 -
4047 - if (empty($session_id)) {
4048 - //error_log(esc_html__('Fetch new messages error: Session ID missing.', 'mxchat'));
4049 - wp_send_json_error(['message' => esc_html__('Session ID missing.', 'mxchat')]);
4050 - wp_die();
4051 - }
4052 -
4053 - $history = get_option("mxchat_history_{$session_id}", []);
4054 -
4055 - //error_log("MxChat WhatsApp DEBUG: Fetch new messages for session {$session_id}");
4056 - //error_log("MxChat WhatsApp DEBUG: last_seen_id = " . var_export($last_seen_id, true));
4057 - //error_log("MxChat WhatsApp DEBUG: History count = " . count($history));
4058 - //error_log("MxChat WhatsApp DEBUG: Full history = " . print_r($history, true));
4059 -
4060 - $new_messages = array_filter($history, function ($message) use ($last_seen_id, $persistence_enabled, $initial_timestamp) {
4061 - //error_log("MxChat WhatsApp DEBUG: Checking message - ID: " . ($message['id'] ?? 'NO_ID') . ", Role: " . ($message['role'] ?? 'NO_ROLE'));
4062 -
4063 - // If persistence is enabled, show all new messages
4064 - if ($persistence_enabled) {
4065 - $has_id = !empty($message['id']);
4066 - $is_agent = $message['role'] === 'agent';
4067 -
4068 - // If last_seen_id is empty, 'NaN', or invalid, show all agent messages
4069 - if (empty($last_seen_id) || $last_seen_id === 'NaN' || $last_seen_id === 'undefined') {
4070 - $is_newer = true;
4071 - } else {
4072 - $is_newer = strcmp($message['id'] ?? '', $last_seen_id) > 0;
4073 - }
4074 -
4075 - //error_log("MxChat WhatsApp DEBUG: has_id={$has_id}, is_newer={$is_newer}, is_agent={$is_agent}");
4076 -
4077 - return $has_id && $is_newer && $is_agent;
4078 - }
4079 -
4080 - // If persistence is disabled, only show messages after initial timestamp
4081 - return !empty($message['id']) &&
4082 - $message['role'] === 'agent' &&
4083 - $message['timestamp'] > $initial_timestamp;
4084 - });
4085 -
4086 - //error_log("MxChat WhatsApp DEBUG: Filtered messages count = " . count($new_messages));
4087 -
4088 - // Include current chat mode so frontend can detect agent→AI transitions
4089 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
4090 -
4091 - wp_send_json_success([
4092 - 'new_messages' => array_values($new_messages),
4093 - 'chat_mode' => $chat_mode
4094 - ]);
4095 - wp_die();
4096 -}
4097 -public function mxchat_live_agent_handover($message, $user_id, $session_id) {
4098 - // First check if live agents are available
4099 - $live_agent_available = $this->options['live_agent_status'] ?? 'off';
4100 - if ($live_agent_available !== 'on') {
4101 - $away_message = $this->options['live_agent_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4102 - $this->fallbackResponse = [
4103 - 'text' => $away_message,
4104 - 'html' => '',
4105 - 'images' => [],
4106 - 'chat_mode' => 'ai'
4107 - ];
4108 - wp_send_json([
4109 - 'text' => $away_message,
4110 - 'html' => '',
4111 - 'chat_mode' => 'ai',
4112 - 'session_id' => $session_id
4113 - ]);
4114 - wp_die();
4115 - }
4116 -
4117 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4118 -
4119 - if (empty($slack_bot_token)) {
4120 - return false;
4121 - }
4122 -
4123 - // Check if channel already exists for this session
4124 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4125 -
4126 - if (empty($channel_id)) {
4127 - // Create new channel with session ID as name
4128 - $channel_name = $this->generate_channel_name($session_id);
4129 -
4130 - //error_log("Attempting to create channel: $channel_name");
4131 -
4132 - $response = wp_remote_post('https://slack.com/api/conversations.create', [
4133 - 'headers' => [
4134 - 'Content-Type' => 'application/json',
4135 - 'Authorization' => 'Bearer ' . $slack_bot_token
4136 - ],
4137 - 'body' => json_encode([
4138 - 'name' => $channel_name,
4139 - 'is_private' => false // Public channel - anyone in workspace can join
4140 - ])
4141 - ]);
4142 -
4143 - if (!is_wp_error($response)) {
4144 - $response_body = wp_remote_retrieve_body($response);
4145 - $response_data = json_decode($response_body, true);
4146 -
4147 - //error_log("Channel creation response: " . $response_body);
4148 -
4149 - if (isset($response_data['ok']) && $response_data['ok']) {
4150 - $channel_id = $response_data['channel']['id'];
4151 - $actual_channel_name = $response_data['channel']['name'] ?? 'unknown';
4152 - //error_log("Channel created successfully: ID=$channel_id, Name=$actual_channel_name");
4153 - update_option("mxchat_channel_{$session_id}", $channel_id);
4154 -
4155 - // Auto-invite agents to the channel
4156 - $agent_user_ids = $this->options['live_agent_user_ids'] ?? '';
4157 -
4158 - if (!empty($agent_user_ids)) {
4159 - // Parse user IDs (one per line)
4160 - $user_ids = array_filter(array_map('trim', explode("\n", $agent_user_ids)));
4161 -
4162 - foreach ($user_ids as $user_id_to_invite) {
4163 - //error_log("Inviting user to channel: $user_id_to_invite");
4164 -
4165 - $invite_response = wp_remote_post('https://slack.com/api/conversations.invite', [
4166 - 'headers' => [
4167 - 'Content-Type' => 'application/json',
4168 - 'Authorization' => 'Bearer ' . $slack_bot_token
4169 - ],
4170 - 'body' => json_encode([
4171 - 'channel' => $channel_id,
4172 - 'users' => $user_id_to_invite
4173 - ])
4174 - ]);
4175 -
4176 - if (!is_wp_error($invite_response)) {
4177 - $invite_body = wp_remote_retrieve_body($invite_response);
4178 - $invite_data = json_decode($invite_body, true);
4179 - //error_log("Invite response for $user_id_to_invite: " . $invite_body);
4180 -
4181 - if (isset($invite_data['ok']) && $invite_data['ok']) {
4182 - //error_log("Successfully invited user $user_id_to_invite to channel");
4183 - } else {
4184 - //error_log("Failed to invite user $user_id_to_invite: " . ($invite_data['error'] ?? 'Unknown error'));
4185 - }
4186 - } else {
4187 - //error_log("WP Error inviting user $user_id_to_invite: " . $invite_response->get_error_message());
4188 - }
4189 - }
4190 - } else {
4191 - //error_log("No agent user IDs configured for auto-invite");
4192 - }
4193 - } else {
4194 - //error_log("Channel creation failed: " . ($response_data['error'] ?? 'Unknown error'));
4195 - }
4196 - } else {
4197 - //error_log("WP Error creating channel: " . $response->get_error_message());
4198 - }
4199 -
4200 - if (empty($channel_id)) {
4201 - return false; // Failed to create channel
4202 - }
4203 - }
4204 -
4205 - // Get recent chat history
4206 - $history = get_option("mxchat_history_{$session_id}", []);
4207 - $recent_history = array_slice($history, -5);
4208 -
4209 - // Format conversation context
4210 - $conversation_context = "";
4211 - if (!empty($recent_history)) {
4212 - $conversation_context = "*Recent Conversation:*\n";
4213 - foreach ($recent_history as $hist_message) {
4214 - $role_display = $hist_message['role'] === 'user' ? 'User' : 'AI';
4215 - $conversation_context .= ">{$role_display}: {$hist_message['content']}\n";
4216 - }
4217 - $conversation_context .= "\n";
4218 - }
4219 -
4220 - update_option("mxchat_mode_{$session_id}", 'agent');
4221 -
4222 - // Send message to channel
4223 - $channel_message = "🔔 *New Live Agent Request*\n\n";
4224 - $channel_message .= "*Session ID:* `{$session_id}`\n";
4225 - $channel_message .= "*User ID:* `{$user_id}`\n";
4226 -
4227 - // Surface the captured visitor identity so the agent knows who they're talking to —
4228 - // guest User IDs are 0, but the pre-chat gate / login / transcript often has name+email (plan-e2195b).
4229 - $visitor = $this->mxchat_get_visitor_identity($session_id);
4230 - if (!empty($visitor['name']) && !empty($visitor['email'])) {
4231 - $channel_message .= "*Visitor:* {$visitor['name']} <{$visitor['email']}>\n";
4232 - } elseif (!empty($visitor['email'])) {
4233 - $channel_message .= "*Visitor:* <{$visitor['email']}>\n";
4234 - } elseif (!empty($visitor['name'])) {
4235 - $channel_message .= "*Visitor:* {$visitor['name']}\n";
4236 - }
4237 - $channel_message .= "\n";
4238 -
4239 - if (!empty($conversation_context)) {
4240 - $channel_message .= $conversation_context;
4241 - }
4242 -
4243 - $channel_message .= "*Current Message:*\n{$message}\n\n";
4244 - $channel_message .= "_Reply directly in this channel - all messages will go to the user_";
4245 -
4246 - wp_remote_post('https://slack.com/api/chat.postMessage', [
4247 - 'headers' => [
4248 - 'Content-Type' => 'application/json',
4249 - 'Authorization' => 'Bearer ' . $slack_bot_token
4250 - ],
4251 - 'body' => json_encode([
4252 - 'channel' => $channel_id,
4253 - 'text' => $channel_message,
4254 - 'mrkdwn' => true
4255 - ])
4256 - ]);
4257 -
4258 - $success_message = $this->options['live_agent_notification_message'] ?? 'Live agent has been notified.';
4259 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4260 -
4261 - $this->fallbackResponse = [
4262 - 'text' => $success_message,
4263 - 'html' => '',
4264 - 'images' => [],
4265 - 'chat_mode' => 'agent'
4266 - ];
4267 -
4268 - wp_send_json([
4269 - 'success' => true,
4270 - 'text' => $success_message,
4271 - 'html' => '',
4272 - 'chat_mode' => 'agent',
4273 - 'session_id' => $session_id,
4274 - 'fallbackResponse' => $this->fallbackResponse
4275 - ]);
4276 - wp_die();
4277 -}
4278 -
4279 -private function generate_channel_name($session_id) {
4280 - $email = null;
4281 - $name = null;
4282 -
4283 - // 1. First priority: Check if user is logged in and get their info
4284 - if (is_user_logged_in()) {
4285 - $current_user = wp_get_current_user();
4286 - if (!empty($current_user->user_email)) {
4287 - $email = $current_user->user_email;
4288 - //error_log("[DEBUG] Using logged-in user email for channel: {$email}");
4289 - }
4290 - if (!empty($current_user->display_name)) {
4291 - $name = $current_user->display_name;
4292 - //error_log("[DEBUG] Using logged-in user name for channel: {$name}");
4293 - }
4294 - }
4295 -
4296 - // 2. Second priority: Check for saved email/name from "require email to chat" option
4297 - if (empty($email)) {
4298 - $email_option_key = "mxchat_email_{$session_id}";
4299 - $saved_email = get_option($email_option_key);
4300 - if (!empty($saved_email)) {
4301 - $email = $saved_email;
4302 - //error_log("[DEBUG] Using saved email from session for channel: {$email}");
4303 - }
4304 - }
4305 -
4306 - if (empty($name)) {
4307 - $name_option_key = "mxchat_name_{$session_id}";
4308 - $saved_name = get_option($name_option_key);
4309 - if (!empty($saved_name)) {
4310 - $name = $saved_name;
4311 - //error_log("[DEBUG] Using saved name from session for channel: {$name}");
4312 - }
4313 - }
4314 -
4315 - // 3. Third priority: Check existing chat transcript for email/name
4316 - if (empty($email) || empty($name)) {
4317 - global $wpdb;
4318 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
4319 - $existing_data = $wpdb->get_row($wpdb->prepare(
4320 - "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",
4321 - $session_id
4322 - ));
4323 -
4324 - if ($existing_data) {
4325 - if (empty($email) && !empty($existing_data->user_email)) {
4326 - $email = $existing_data->user_email;
4327 - //error_log("[DEBUG] Using email from chat transcript for channel: {$email}");
4328 - }
4329 - if (empty($name) && !empty($existing_data->user_name)) {
4330 - $name = $existing_data->user_name;
4331 - //error_log("[DEBUG] Using name from chat transcript for channel: {$name}");
4332 - }
4333 - }
4334 - }
4335 -
4336 - // 4. Generate channel name based on priority: Name > Email > Session ID
4337 - $channel_name = '';
4338 -
4339 - if (!empty($name)) {
4340 - // Convert name to valid Slack channel name
4341 - $base_name = strtolower(trim($name));
4342 - // Replace spaces and invalid characters
4343 - $base_name = preg_replace('/[^a-z0-9\s]/', '', $base_name);
4344 - $base_name = preg_replace('/\s+/', '-', $base_name);
4345 - $base_name = trim($base_name, '-');
4346 -
4347 - // Get last 4 characters of session ID for uniqueness
4348 - $session_suffix = substr($session_id, -4);
4349 - $channel_name = 'chat-' . $base_name . '-' . strtolower($session_suffix);
4350 -
4351 - // Slack channel names have a 21 character limit
4352 - if (strlen($channel_name) > 21) {
4353 - // Calculate available space for name (21 - 'chat-' - '-' - session_suffix)
4354 - $available_space = 21 - 5 - 1 - strlen($session_suffix); // 'chat-' = 5, '-' = 1
4355 - $truncated_name = substr($base_name, 0, $available_space);
4356 - $truncated_name = rtrim($truncated_name, '-'); // Remove trailing hyphen
4357 - $channel_name = 'chat-' . $truncated_name . '-' . strtolower($session_suffix);
4358 - }
4359 -
4360 - //error_log("[DEBUG] Using name for channel: {$channel_name} (from name: {$name})");
4361 -
4362 - } elseif (!empty($email)) {
4363 - // Convert email to valid Slack channel name (your existing logic)
4364 - $channel_name = 'chat-' . strtolower(str_replace(['@', '.', '+', '_'], ['-at-', '-', '-plus-', '-'], $email));
4365 - // Remove any remaining invalid characters
4366 - $channel_name = preg_replace('/[^a-z0-9\-]/', '', $channel_name);
4367 - // Ensure it doesn't end with a hyphen
4368 - $channel_name = rtrim($channel_name, '-');
4369 - // Slack channel names have a 21 character limit, so truncate if needed
4370 - if (strlen($channel_name) > 21) {
4371 - $channel_name = substr($channel_name, 0, 21);
4372 - $channel_name = rtrim($channel_name, '-'); // Remove trailing hyphen if truncation created one
4373 - }
4374 -
4375 - //error_log("[DEBUG] Using email for channel: {$channel_name} (from email: {$email})");
4376 -
4377 - } else {
4378 - // Fallback to session ID if no name or email found
4379 - $channel_name = 'chat-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $session_id));
4380 - //error_log("[DEBUG] No name or email found, using session ID for channel: {$channel_name}");
4381 - }
4382 -
4383 - // Final validation - ensure channel name meets Slack requirements
4384 - if (strlen($channel_name) > 21) {
4385 - $channel_name = substr($channel_name, 0, 21);
4386 - $channel_name = rtrim($channel_name, '-');
4387 - }
4388 -
4389 - //error_log("[DEBUG] Generated channel name: {$channel_name}");
4390 - return $channel_name;
4391 -}
4392 -
4393 -/**
4394 - * Telegram Live Agent Handover
4395 - * Creates a forum topic in the Telegram group and notifies agents
4396 - */
4397 -public function mxchat_telegram_live_agent_handover($message, $user_id, $session_id) {
4398 - // Check if Telegram agents are available
4399 - $telegram_available = $this->options['telegram_status'] ?? 'off';
4400 - if ($telegram_available !== 'on') {
4401 - $away_message = $this->options['telegram_away_message'] ?? 'Sorry, live agents are currently unavailable. I can continue helping you as an AI assistant.';
4402 - $this->fallbackResponse = [
4403 - 'text' => $away_message,
4404 - 'html' => '',
4405 - 'images' => [],
4406 - 'chat_mode' => 'ai'
4407 - ];
4408 - wp_send_json([
4409 - 'text' => $away_message,
4410 - 'html' => '',
4411 - 'chat_mode' => 'ai',
4412 - 'session_id' => $session_id
4413 - ]);
4414 - wp_die();
4415 - }
4416 -
4417 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4418 - $telegram_group_id = $this->options['telegram_group_id'] ?? '';
4419 -
4420 - if (empty($telegram_bot_token) || empty($telegram_group_id)) {
4421 - return false;
4422 - }
4423 -
4424 - // Check if topic already exists for this session
4425 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4426 -
4427 - if (empty($topic_id)) {
4428 - // Generate topic name
4429 - $topic_name = $this->generate_telegram_topic_name($session_id);
4430 -
4431 - // Random icon color (Telegram forum topic colors)
4432 - $icon_colors = [0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F];
4433 - $icon_color = $icon_colors[array_rand($icon_colors)];
4434 -
4435 - // Create forum topic
4436 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/createForumTopic", [
4437 - 'headers' => ['Content-Type' => 'application/json'],
4438 - 'body' => json_encode([
4439 - 'chat_id' => $telegram_group_id,
4440 - 'name' => $topic_name,
4441 - 'icon_color' => $icon_color
4442 - ])
4443 - ]);
4444 -
4445 - if (!is_wp_error($response)) {
4446 - $response_body = wp_remote_retrieve_body($response);
4447 - $response_data = json_decode($response_body, true);
4448 -
4449 - if (isset($response_data['ok']) && $response_data['ok']) {
4450 - $topic_id = $response_data['result']['message_thread_id'];
4451 - update_option("mxchat_telegram_topic_{$session_id}", $topic_id);
4452 - update_option("mxchat_telegram_group_{$session_id}", $telegram_group_id);
4453 - }
4454 - }
4455 -
4456 - if (empty($topic_id)) {
4457 - return false; // Failed to create topic
4458 - }
4459 - }
4460 -
4461 - // Get recent chat history
4462 - $history = get_option("mxchat_history_{$session_id}", []);
4463 - $recent_history = array_slice($history, -5);
4464 -
4465 - // Format conversation context for Telegram (HTML format)
4466 - $conversation_context = "";
4467 - if (!empty($recent_history)) {
4468 - $conversation_context = "<b>Recent Conversation:</b>\n";
4469 - foreach ($recent_history as $hist_message) {
4470 - $role_display = $hist_message['role'] === 'user' ? '👤 User' : '🤖 AI';
4471 - $escaped_content = htmlspecialchars($hist_message['content'], ENT_QUOTES, 'UTF-8');
4472 - $conversation_context .= "{$role_display}: {$escaped_content}\n";
4473 - }
4474 - $conversation_context .= "\n";
4475 - }
4476 -
4477 - // Get user info
4478 - $user_email = get_option("mxchat_email_{$session_id}", 'Not provided');
4479 - $user_name = get_option("mxchat_name_{$session_id}", 'Anonymous');
4480 -
4481 - // Update session mode
4482 - update_option("mxchat_mode_{$session_id}", 'agent');
4483 -
4484 - // Send initial message to topic
4485 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4486 - $topic_message = "🔔 <b>New Live Agent Request</b>\n\n";
4487 - $topic_message .= "<b>Session ID:</b> <code>{$session_id}</code>\n";
4488 - $topic_message .= "<b>User:</b> {$user_name}\n";
4489 - $topic_message .= "<b>Email:</b> {$user_email}\n\n";
4490 -
4491 - if (!empty($conversation_context)) {
4492 - $topic_message .= $conversation_context;
4493 - }
4494 -
4495 - $topic_message .= "<b>Current Message:</b>\n{$escaped_message}\n\n";
4496 - $topic_message .= "<i>Reply in this topic - messages will be sent to the user</i>\n";
4497 - $topic_message .= "<i>Type #close, #end, #disconnect, or #done to end the session</i>";
4498 -
4499 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4500 - 'headers' => ['Content-Type' => 'application/json'],
4501 - 'body' => json_encode([
4502 - 'chat_id' => $telegram_group_id,
4503 - 'message_thread_id' => $topic_id,
4504 - 'text' => $topic_message,
4505 - 'parse_mode' => 'HTML'
4506 - ])
4507 - ]);
4508 -
4509 - $success_message = $this->options['telegram_notification_message'] ?? "I've notified a support agent. Please allow a moment for them to respond.";
4510 - $this->mxchat_save_chat_message($session_id, 'bot', $success_message);
4511 -
4512 - $this->fallbackResponse = [
4513 - 'text' => $success_message,
4514 - 'html' => '',
4515 - 'images' => [],
4516 - 'chat_mode' => 'agent'
4517 - ];
4518 -
4519 - wp_send_json([
4520 - 'success' => true,
4521 - 'text' => $success_message,
4522 - 'html' => '',
4523 - 'chat_mode' => 'agent',
4524 - 'session_id' => $session_id,
4525 - 'fallbackResponse' => $this->fallbackResponse
4526 - ]);
4527 - wp_die();
4528 -}
4529 -
4530 -/**
4531 - * Generate topic name for Telegram forum
4532 - */
4533 -private function generate_telegram_topic_name($session_id) {
4534 - $name = null;
4535 - $email = null;
4536 -
4537 - // Check logged in user
4538 - if (is_user_logged_in()) {
4539 - $current_user = wp_get_current_user();
4540 - if (!empty($current_user->display_name)) {
4541 - $name = $current_user->display_name;
4542 - }
4543 - if (!empty($current_user->user_email)) {
4544 - $email = $current_user->user_email;
4545 - }
4546 - }
4547 -
4548 - // Check session data
4549 - if (empty($name)) {
4550 - $name = get_option("mxchat_name_{$session_id}");
4551 - }
4552 - if (empty($email)) {
4553 - $email = get_option("mxchat_email_{$session_id}");
4554 - }
4555 -
4556 - // Generate topic name
4557 - $session_suffix = substr($session_id, -6);
4558 -
4559 - if (!empty($name)) {
4560 - // Clean name for topic (max 128 chars in Telegram)
4561 - $clean_name = preg_replace('/[^\p{L}\p{N}\s\-]/u', '', $name);
4562 - $clean_name = trim($clean_name);
4563 - if (strlen($clean_name) > 50) {
4564 - $clean_name = substr($clean_name, 0, 50);
4565 - }
4566 - return "Chat - {$clean_name} ({$session_suffix})";
4567 - } elseif (!empty($email)) {
4568 - // Use email prefix
4569 - $email_prefix = explode('@', $email)[0];
4570 - if (strlen($email_prefix) > 30) {
4571 - $email_prefix = substr($email_prefix, 0, 30);
4572 - }
4573 - return "Chat - {$email_prefix} ({$session_suffix})";
4574 - }
4575 -
4576 - return "Chat - {$session_suffix}";
4577 -}
4578 -
4579 -/**
4580 - * Send user message to Telegram agent
4581 - */
4582 -public function mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id) {
4583 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4584 - $topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4585 - $group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4586 -
4587 - if (empty($telegram_bot_token) || empty($topic_id) || empty($group_id)) {
4588 - return false;
4589 - }
4590 -
4591 - $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
4592 - $user_message = "👤 <b>User:</b> {$escaped_message}";
4593 -
4594 - $response = wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4595 - 'headers' => ['Content-Type' => 'application/json'],
4596 - 'body' => json_encode([
4597 - 'chat_id' => $group_id,
4598 - 'message_thread_id' => $topic_id,
4599 - 'text' => $user_message,
4600 - 'parse_mode' => 'HTML'
4601 - ])
4602 - ]);
4603 -
4604 - return !is_wp_error($response);
4605 -}
4606 -
4607 -/**
4608 - * Handle incoming Telegram webhook
4609 - */
4610 -public function handle_telegram_webhook(WP_REST_Request $request) {
4611 - $body = $request->get_body();
4612 - $data = json_decode($body, true);
4613 -
4614 - //error_log('[MxChat Telegram DEBUG] Webhook received: ' . $body);
4615 -
4616 - // Handle message events from forum topics
4617 - if (isset($data['message'])) {
4618 - $message_data = $data['message'];
4619 -
4620 - // Skip if not from a forum topic
4621 - if (!isset($message_data['message_thread_id'])) {
4622 - //error_log('[MxChat Telegram DEBUG] Skipped: No message_thread_id (not a forum topic message)');
4623 - return new WP_REST_Response(['ok' => true]);
4624 - }
4625 -
4626 - // Skip bot messages
4627 - if (isset($message_data['from']['is_bot']) && $message_data['from']['is_bot']) {
4628 - //error_log('[MxChat Telegram DEBUG] Skipped: Message from bot');
4629 - return new WP_REST_Response(['ok' => true]);
4630 - }
4631 -
4632 - $chat_id = $message_data['chat']['id'] ?? '';
4633 - $topic_id = $message_data['message_thread_id'];
4634 - $message_text = $message_data['text'] ?? '';
4635 - $message_id = $message_data['message_id'] ?? '';
4636 - $from = $message_data['from'] ?? [];
4637 - $agent_name = trim(($from['first_name'] ?? '') . ' ' . ($from['last_name'] ?? ''));
4638 - if (empty($agent_name)) {
4639 - $agent_name = $from['username'] ?? 'Agent';
4640 - }
4641 -
4642 - //error_log("[MxChat Telegram DEBUG] Parsed: chat_id={$chat_id}, topic_id={$topic_id}, agent={$agent_name}, text={$message_text}");
4643 -
4644 - // Skip empty messages
4645 - if (empty($message_text)) {
4646 - //error_log('[MxChat Telegram DEBUG] Skipped: Empty message text');
4647 - return new WP_REST_Response(['ok' => true]);
4648 - }
4649 -
4650 - // Find session ID by topic ID - cast to string for comparison
4651 - global $wpdb;
4652 - $topic_id_str = strval($topic_id);
4653 - $session_option = $wpdb->get_var(
4654 - $wpdb->prepare(
4655 - "SELECT option_name FROM {$wpdb->options}
4656 - WHERE option_name LIKE %s
4657 - AND option_value = %s",
4658 - 'mxchat_telegram_topic_%',
4659 - $topic_id_str
4660 - )
4661 - );
4662 -
4663 - //error_log("[MxChat Telegram DEBUG] Looking for topic_id={$topic_id_str} in options, found: " . ($session_option ?: 'NULL'));
4664 -
4665 - if ($session_option) {
4666 - $session_id = str_replace('mxchat_telegram_topic_', '', $session_option);
4667 - //error_log("[MxChat Telegram DEBUG] Session ID: {$session_id}");
4668 -
4669 - // Verify the group ID matches
4670 - $stored_group_id = get_option("mxchat_telegram_group_{$session_id}", '');
4671 - //error_log("[MxChat Telegram DEBUG] Stored group_id={$stored_group_id}, received chat_id={$chat_id}");
4672 -
4673 - if (strval($stored_group_id) != strval($chat_id)) {
4674 - //error_log('[MxChat Telegram DEBUG] Skipped: Group ID mismatch');
4675 - return new WP_REST_Response(['ok' => true]);
4676 - }
4677 -
4678 - // Check for closure commands
4679 - $lower_text = strtolower(trim($message_text));
4680 - if (in_array($lower_text, ['#close', '#end', '#disconnect', '#done'])) {
4681 - //error_log("[MxChat Telegram DEBUG] Closure command received: {$lower_text}");
4682 - // End the live agent session
4683 - update_option("mxchat_mode_{$session_id}", 'ai');
4684 -
4685 - // Save disconnect message
4686 - $disconnect_message = "Live agent session ended. You're now chatting with the AI assistant.";
4687 - $this->mxchat_save_chat_message($session_id, 'bot', $disconnect_message);
4688 -
4689 - // Notify in Telegram
4690 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4691 - if (!empty($telegram_bot_token)) {
4692 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4693 - 'headers' => ['Content-Type' => 'application/json'],
4694 - 'body' => json_encode([
4695 - 'chat_id' => $chat_id,
4696 - 'message_thread_id' => $topic_id,
4697 - 'text' => "✅ Session closed. User returned to AI chatbot.",
4698 - 'parse_mode' => 'HTML'
4699 - ])
4700 - ]);
4701 -
4702 - // Optionally close the topic
4703 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/closeForumTopic", [
4704 - 'headers' => ['Content-Type' => 'application/json'],
4705 - 'body' => json_encode([
4706 - 'chat_id' => $chat_id,
4707 - 'message_thread_id' => $topic_id
4708 - ])
4709 - ]);
4710 - }
4711 -
4712 - return new WP_REST_Response(['ok' => true]);
4713 - }
4714 -
4715 - // Deduplicate messages
4716 - $message_key = md5($session_id . $message_id . $message_text);
4717 - $processed_messages = get_transient('mxchat_telegram_messages_' . $session_id) ?: [];
4718 -
4719 - if (in_array($message_key, $processed_messages)) {
4720 - //error_log('[MxChat Telegram DEBUG] Skipped: Duplicate message');
4721 - return new WP_REST_Response(['ok' => true]);
4722 - }
4723 -
4724 - $processed_messages[] = $message_key;
4725 - if (count($processed_messages) > 50) {
4726 - $processed_messages = array_slice($processed_messages, -50);
4727 - }
4728 - set_transient('mxchat_telegram_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
4729 -
4730 - // Save the agent message - format with agent name prefix for proper parsing
4731 - $formatted_message = "Agent: {$agent_name} - {$message_text}";
4732 - //error_log("[MxChat Telegram DEBUG] Saving agent message: {$formatted_message}");
4733 -
4734 - $this->mxchat_save_chat_message($session_id, 'agent', $formatted_message);
4735 -
4736 - // Verify the message was saved to history
4737 - $history = get_option("mxchat_history_{$session_id}", []);
4738 - $last_message = end($history);
4739 - //error_log("[MxChat Telegram DEBUG] History after save - count: " . count($history) . ", last message role: " . ($last_message['role'] ?? 'none'));
4740 -
4741 - // Send confirmation back to Telegram
4742 - $telegram_bot_token = $this->options['telegram_bot_token'] ?? '';
4743 - if (!empty($telegram_bot_token)) {
4744 - $confirm_key = 'mxchat_telegram_confirm_' . $message_key;
4745 - if (!get_transient($confirm_key)) {
4746 - wp_remote_post("https://api.telegram.org/bot{$telegram_bot_token}/sendMessage", [
4747 - 'headers' => ['Content-Type' => 'application/json'],
4748 - 'body' => json_encode([
4749 - 'chat_id' => $chat_id,
4750 - 'message_thread_id' => $topic_id,
4751 - 'text' => "✅ <i>Message sent to user</i>",
4752 - 'parse_mode' => 'HTML',
4753 - 'reply_to_message_id' => $message_id
4754 - ])
4755 - ]);
4756 - set_transient($confirm_key, true, 300);
4757 - }
4758 - }
4759 - } else {
4760 - //error_log("[MxChat Telegram DEBUG] No session found for topic_id={$topic_id}");
4761 - }
4762 - } else {
4763 - //error_log('[MxChat Telegram DEBUG] No message in webhook data');
4764 - }
4765 -
4766 - return new WP_REST_Response(['ok' => true]);
4767 -}
4768 -
4769 -public function mxchat_send_user_message_to_agent($message, $user_id, $session_id) {
4770 - // Check if this is a Telegram agent session
4771 - $telegram_topic_id = get_option("mxchat_telegram_topic_{$session_id}", '');
4772 - if (!empty($telegram_topic_id)) {
4773 - return $this->mxchat_send_user_message_to_telegram_agent($message, $user_id, $session_id);
4774 - }
4775 -
4776 - // Otherwise, try Slack
4777 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
4778 - $channel_id = get_option("mxchat_channel_{$session_id}", '');
4779 -
4780 - if (empty($slack_bot_token) || empty($channel_id)) {
4781 - return false;
4782 - }
4783 -
4784 - $user_message = "💬 *User:* {$message}";
4785 -
4786 - $response = wp_remote_post('https://slack.com/api/chat.postMessage', [
4787 - 'headers' => [
4788 - 'Content-Type' => 'application/json',
4789 - 'Authorization' => 'Bearer ' . $slack_bot_token
4790 - ],
4791 - 'body' => json_encode([
4792 - 'channel' => $channel_id,
4793 - 'text' => $user_message,
4794 - 'mrkdwn' => true
4795 - ])
4796 - ]);
4797 -
4798 - return !is_wp_error($response);
4799 -}
4800 -public function handle_slack_interaction(WP_REST_Request $request) {
4801 - //error_log('Received Slack interaction');
4802 -
4803 - $payload = json_decode($request->get_param('payload'), true);
4804 - //error_log('Payload: ' . print_r($payload, true));
4805 -
4806 - // Handle button click
4807 - if ($payload['type'] === 'block_actions' && $payload['actions'][0]['action_id'] === 'reply_to_user') {
4808 - $session_id = $payload['actions'][0]['value'];
4809 - $trigger_id = $payload['trigger_id'];
4810 -
4811 - // Get Bot Token from settings
4812 - $slack_token = $this->options['live_agent_bot_token'] ?? '';
4813 -
4814 - if (empty($slack_token)) {
4815 - //error_log('Slack Bot Token not configured');
4816 - return new WP_REST_Response(['error' => esc_html__('Bot token not configured', 'mxchat')], 400);
4817 - }
4818 - $response = wp_remote_post('https://slack.com/api/views.open', [
4819 - 'headers' => [
4820 - 'Content-Type' => 'application/json',
4821 - 'Authorization' => 'Bearer ' . $slack_token
4822 - ],
4823 - 'body' => json_encode([
4824 - 'trigger_id' => $trigger_id,
4825 - 'view' => [
4826 - 'type' => 'modal',
4827 - 'callback_id' => 'reply_modal',
4828 - 'title' => [
4829 - 'type' => 'plain_text',
4830 - 'text' => __('Reply to User', 'mxchat')
4831 - ],
4832 - 'submit' => [
4833 - 'type' => 'plain_text',
4834 - 'text' => __('Send', 'mxchat')
4835 - ],
4836 - 'close' => [
4837 - 'type' => 'plain_text',
4838 - 'text' => __('Cancel', 'mxchat')
4839 - ],
4840 - 'blocks' => [
4841 - [
4842 - 'type' => 'input',
4843 - 'block_id' => 'reply_block',
4844 - 'label' => [
4845 - 'type' => 'plain_text',
4846 - 'text' => sprintf(__('Reply to session: %s', 'mxchat'), $session_id)
4847 - ],
4848 - 'element' => [
4849 - 'type' => 'plain_text_input',
4850 - 'action_id' => 'message',
4851 - 'multiline' => true,
4852 - 'placeholder' => [
4853 - 'type' => 'plain_text',
4854 - 'text' => __('Type your message here...', 'mxchat')
4855 - ]
4856 - ]
4857 - ]
4858 - ],
4859 - 'private_metadata' => $session_id
4860 - ]
4861 - ])
4862 - ]);
4863 -
4864 - //error_log('Views.open response: ' . print_r($response, true));
4865 -
4866 - // Return immediate acknowledgment
4867 - return new WP_REST_Response(['ok' => true]);
4868 - }
4869 -
4870 - // Handle modal submission
4871 -// Handle modal submission
4872 -if ($payload['type'] === 'view_submission') {
4873 - $session_id = $payload['view']['private_metadata'];
4874 - $message = $payload['view']['state']['values']['reply_block']['message']['value'];
4875 -
4876 - // Save the message (keep the message_id but don't include in response)
4877 - $this->mxchat_save_chat_message($session_id, 'agent', $message);
4878 -
4879 - // Keep the original response format for Slack
4880 - return new WP_REST_Response([
4881 - 'response_action' => 'clear'
4882 - ]);
4883 -}
4884 -
4885 - // Default acknowledgment
4886 - return new WP_REST_Response(['ok' => true]);
4887 -}
4888 -public function mxchat_handle_agent_response(WP_REST_Request $request) {
4889 - //error_log('Received agent response request');
4890 - //error_log('Request data: ' . print_r($request->get_params(), true));
4891 - // //error_log('Raw body: ' . file_get_contents('php://input'));
4892 -
4893 - // Get the data from Slack's slash command format
4894 - $command_text = $request->get_param('text');
4895 - // //error_log('Command text: ' . $command_text);
4896 -
4897 - if (empty($command_text)) {
4898 - //error_log(esc_html__('Agent response error: No command text received', 'mxchat'));
4899 - return new WP_REST_Response([
4900 - 'error' => esc_html__('Command text is required. Format: /reply session_id message', 'mxchat')
4901 - ], 400);
4902 - }
4903 -
4904 - // Split the command text into session_id and message
4905 - $parts = explode(' ', $command_text, 2);
4906 - if (count($parts) !== 2) {
4907 - //error_log('Agent response error: Invalid command format');
4908 - return new WP_REST_Response([
4909 - 'error' => esc_html__('Invalid format. Use: /reply session_id message', 'mxchat')
4910 - ], 400);
4911 - }
4912 -
4913 - $session_id = sanitize_text_field($parts[0]);
4914 - $message = sanitize_text_field($parts[1]);
4915 -
4916 - //error_log("Processing agent response - Session ID: $session_id, Message: $message");
4917 -
4918 - // Save the message
4919 - $message_id = $this->mxchat_save_chat_message($session_id, 'agent', $message);
4920 -
4921 - if (!$message_id) {
4922 - // //error_log('Failed to save agent message');
4923 - return new WP_REST_Response([
4924 - 'error' => esc_html__('Failed to save message', 'mxchat')
4925 - ], 500);
4926 - }
4927 -
4928 - // Return success response in Slack's expected format
4929 - return new WP_REST_Response([
4930 - 'response_type' => 'in_channel',
4931 - 'text' => esc_html__("Message sent successfully to session $session_id", 'mxchat')
4932 - ], 200);
4933 -}
4934 -public function mxchat_handle_switch_to_chatbot_intent($message, $user_id, $session_id) {
4935 - // Update mode to AI
4936 - update_option("mxchat_mode_{$session_id}", 'ai');
4937 -
4938 - // Clear any existing PDF context to start fresh
4939 - $this->clear_pdf_transients($session_id);
4940 -
4941 - // Set the response with explicit chat_mode
4942 - $this->fallbackResponse = [
4943 - 'text' => esc_html__('You are now chatting with the AI chatbot.', 'mxchat'),
4944 - 'html' => '',
4945 - 'images' => [],
4946 - 'chat_mode' => 'ai' // Ensure this is set
4947 - ];
4948 -
4949 - // Return the complete response array instead of just true
4950 - return $this->fallbackResponse;
4951 -}
4952 -
4953 -/**
4954 - * Normalize Slack mrkdwn before relaying an agent's message to the web visitor.
4955 - * Slack's Events API auto-wraps URLs as <https://url> or <https://url|Label>, wraps
4956 - * mentions as <@U…>/<#C…|name>, and HTML-escapes &, <, >. Relayed raw, the visitor
4957 - * sees a broken/doubled link with a trailing > (plan-e2195b). Unwrap links FIRST, then
4958 - * unescape entities LAST so extracted URLs (which can contain &amp;) are not corrupted.
4959 - */
4960 -private function normalize_slack_text($text) {
4961 - if (!is_string($text) || $text === '') {
4962 - return $text;
4963 - }
4964 -
4965 - $text = preg_replace_callback('/<([^>|]+)(?:\|([^>]*))?>/', function ($m) {
4966 - $target = $m[1];
4967 - $label = isset($m[2]) ? $m[2] : '';
4968 -
4969 - // User/channel mentions: <@U…> or <#C…|name> — prefer the human label, else drop the id.
4970 - if (isset($target[0]) && ($target[0] === '@' || $target[0] === '#')) {
4971 - return $label !== '' ? $label : '';
4972 - }
4973 - // mailto:/tel: — strip the scheme for display.
4974 - if (stripos($target, 'mailto:') === 0) {
4975 - $addr = substr($target, 7);
4976 - return ($label !== '' && $label !== $addr) ? "{$label} ({$addr})" : $addr;
4977 - }
4978 - if (stripos($target, 'tel:') === 0) {
4979 - $num = substr($target, 4);
4980 - return ($label !== '' && $label !== $num) ? "{$label} ({$num})" : $num;
4981 - }
4982 - // Regular URL: <url|Label> -> "Label (url)"; bare <url> -> "url".
4983 - if ($label !== '' && $label !== $target) {
4984 - return "{$label} ({$target})";
4985 - }
4986 - return $target;
4987 - }, $text);
4988 -
4989 - // Entity-unescape LAST (after link extraction) so &amp; inside URLs is repaired too.
4990 - $text = str_replace(array('&amp;', '&lt;', '&gt;'), array('&', '<', '>'), $text);
4991 -
4992 - return $text;
4993 -}
4994 -
4995 -/**
4996 - * Resolve the visitor's name + email for a session, mirroring generate_channel_name()'s
4997 - * priority order: logged-in user, then the pre-chat gate options (mxchat_email_/mxchat_name_),
4998 - * then the chat transcript. Returns ['name' => ..., 'email' => ...] (either may be ''). plan-e2195b.
4999 - */
5000 -private function mxchat_get_visitor_identity($session_id) {
5001 - $email = '';
5002 - $name = '';
5003 -
5004 - if (is_user_logged_in()) {
5005 - $current_user = wp_get_current_user();
5006 - if (!empty($current_user->user_email)) { $email = $current_user->user_email; }
5007 - if (!empty($current_user->display_name)) { $name = $current_user->display_name; }
5008 - }
5009 -
5010 - if (empty($email)) {
5011 - $saved_email = get_option("mxchat_email_{$session_id}", '');
5012 - if (!empty($saved_email)) { $email = $saved_email; }
5013 - }
5014 - if (empty($name)) {
5015 - $saved_name = get_option("mxchat_name_{$session_id}", '');
5016 - if (!empty($saved_name)) { $name = $saved_name; }
5017 - }
5018 -
5019 - if (empty($email) || empty($name)) {
5020 - global $wpdb;
5021 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
5022 - $existing_data = $wpdb->get_row($wpdb->prepare(
5023 - "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",
5024 - $session_id
5025 - ));
5026 - if ($existing_data) {
5027 - if (empty($email) && !empty($existing_data->user_email)) { $email = $existing_data->user_email; }
5028 - if (empty($name) && !empty($existing_data->user_name)) { $name = $existing_data->user_name; }
5029 - }
5030 - }
5031 -
5032 - return array('name' => $name, 'email' => $email);
5033 -}
5034 -
5035 -public function handle_slack_messages(WP_REST_Request $request) {
5036 - // Log the incoming request for debugging
5037 - //error_log('Slack events request received: ' . $request->get_body());
5038 -
5039 - $body = $request->get_body();
5040 - $data = json_decode($body, true);
5041 -
5042 - // Handle Slack URL verification
5043 - if (isset($data['type']) && $data['type'] === 'url_verification') {
5044 - //error_log('Slack URL verification challenge: ' . $data['challenge']);
5045 - return new WP_REST_Response($data['challenge'], 200, ['Content-Type' => 'text/plain']);
5046 - }
5047 -
5048 - // IMPORTANT: Handle Slack's event deduplication
5049 - if (isset($data['event_id'])) {
5050 - $event_id = $data['event_id'];
5051 - $processed_events = get_transient('mxchat_slack_events') ?: [];
5052 -
5053 - // Check if we've already processed this event
5054 - if (in_array($event_id, $processed_events)) {
5055 - //error_log("Duplicate event detected: $event_id");
5056 - return new WP_REST_Response(['ok' => true]);
5057 - }
5058 -
5059 - // Add this event to processed list
5060 - $processed_events[] = $event_id;
5061 - // Keep only last 100 events to prevent memory issues
5062 - if (count($processed_events) > 100) {
5063 - $processed_events = array_slice($processed_events, -100);
5064 - }
5065 - // Store for 1 hour
5066 - set_transient('mxchat_slack_events', $processed_events, HOUR_IN_SECONDS);
5067 - }
5068 -
5069 - // Handle message events
5070 - if (isset($data['event']) && $data['event']['type'] === 'message') {
5071 - $event = $data['event'];
5072 -
5073 - // Skip bot messages and messages with subtypes (like bot_message)
5074 - if (isset($event['bot_id']) || isset($event['subtype'])) {
5075 - return new WP_REST_Response(['ok' => true]);
5076 - }
5077 -
5078 - // Additional check: Skip if this is a threaded reply to our confirmation
5079 - if (isset($event['thread_ts']) && $event['thread_ts'] !== $event['ts']) {
5080 - return new WP_REST_Response(['ok' => true]);
5081 - }
5082 -
5083 - $channel_id = $event['channel'];
5084 - $message_text = $event['text'] ?? '';
5085 - $message_ts = $event['ts'] ?? '';
5086 -
5087 - // Find session ID by looking for matching channel
5088 - global $wpdb;
5089 - $session_option = $wpdb->get_var(
5090 - $wpdb->prepare(
5091 - "SELECT option_name FROM {$wpdb->options}
5092 - WHERE option_name LIKE 'mxchat_channel_%'
5093 - AND option_value = %s",
5094 - $channel_id
5095 - )
5096 - );
5097 -
5098 - if ($session_option) {
5099 - $session_id = str_replace('mxchat_channel_', '', $session_option);
5100 -
5101 - // Create a unique key for this specific message
5102 - $message_key = md5($session_id . $message_ts . $message_text);
5103 - $processed_messages = get_transient('mxchat_processed_messages_' . $session_id) ?: [];
5104 -
5105 - // Check if we've already processed this exact message
5106 - if (in_array($message_key, $processed_messages)) {
5107 - //error_log("Duplicate message detected for session $session_id");
5108 - return new WP_REST_Response(['ok' => true]);
5109 - }
5110 -
5111 - // Add to processed messages
5112 - $processed_messages[] = $message_key;
5113 - // Keep only last 50 messages per session
5114 - if (count($processed_messages) > 50) {
5115 - $processed_messages = array_slice($processed_messages, -50);
5116 - }
5117 - set_transient('mxchat_processed_messages_' . $session_id, $processed_messages, HOUR_IN_SECONDS);
5118 -
5119 - $slack_bot_token = $this->options['live_agent_bot_token'] ?? '';
5120 -
5121 - // Handle agent ending the chat — transfer back to AI
5122 - // Format: "!endchat" or "!endchat <custom message to user>"
5123 - if (preg_match('/^!endchat\b/i', trim($message_text))) {
5124 - update_option("mxchat_mode_{$session_id}", 'ai');
5125 -
5126 - // Extract custom message after !endchat, or use empty string
5127 - $custom_message = trim(preg_replace('/^!endchat\s*/i', '', trim($message_text)));
5128 -
5129 - // Send the agent's custom farewell message if provided
5130 - if (!empty($custom_message)) {
5131 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($custom_message));
5132 - }
5133 -
5134 - // Confirm in Slack channel
5135 - if (!empty($slack_bot_token)) {
5136 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5137 - 'headers' => [
5138 - 'Content-Type' => 'application/json',
5139 - 'Authorization' => 'Bearer ' . $slack_bot_token
5140 - ],
5141 - 'body' => json_encode([
5142 - 'channel' => $channel_id,
5143 - 'text' => "✅ *Chat ended.* User has been transferred back to AI mode.",
5144 - 'mrkdwn' => true
5145 - ])
5146 - ]);
5147 - }
5148 -
5149 - return new WP_REST_Response(['ok' => true]);
5150 - }
5151 -
5152 - // Save the agent message (normalize Slack link/entity formatting first — plan-e2195b)
5153 - $this->mxchat_save_chat_message($session_id, 'agent', $this->normalize_slack_text($message_text));
5154 -
5155 - // Send confirmation back to Slack (only once)
5156 - if (!empty($slack_bot_token)) {
5157 - // Use a transient to prevent duplicate confirmations
5158 - $confirm_key = 'mxchat_confirm_' . $message_key;
5159 - if (!get_transient($confirm_key)) {
5160 - wp_remote_post('https://slack.com/api/chat.postMessage', [
5161 - 'headers' => [
5162 - 'Content-Type' => 'application/json',
5163 - 'Authorization' => 'Bearer ' . $slack_bot_token
5164 - ],
5165 - 'body' => json_encode([
5166 - 'channel' => $channel_id,
5167 - 'text' => "✅ _Message sent to user_",
5168 - 'thread_ts' => $event['ts'] // Reply in thread
5169 - ])
5170 - ]);
5171 - // Set transient to prevent duplicate confirmations
5172 - set_transient($confirm_key, true, 300); // 5 minutes
5173 - }
5174 - }
5175 - }
5176 - }
5177 -
5178 - return new WP_REST_Response(['ok' => true]);
5179 -}
5180 -
5181 -// For the word upload handler
5182 -public function mxchat_handle_word_upload() {
5183 - // Delegate to word handler
5184 - $this->word_handler->mxchat_handle_word_upload();
5185 -}
5186 -
5187 -// For the word removal handler
5188 -public function mxchat_handle_word_remove() {
5189 - // Delegate to word handler
5190 - $this->word_handler->mxchat_handle_word_remove();
5191 -}
5192 -
5193 -// For the word status check
5194 -public function mxchat_check_word_status() {
5195 - // Delegate to word handler
5196 - $this->word_handler->mxchat_check_word_status();
5197 -}
5198 -
5199 -
5200 -private function mxchat_get_user_identifier() {
5201 - return MxChat_User::mxchat_get_user_identifier();
5202 -}
5203 -
5204 -private function mxchat_generate_embedding($text, $api_key) {
5205 - try {
5206 - // Get options and selected model
5207 - $options = get_option('mxchat_options');
5208 - $selected_model = $options['embedding_model'] ?? 'text-embedding-ada-002';
5209 -
5210 - // Opt-in: route embeddings through the Custom (OpenAI-compatible) provider.
5211 - // Off by default so existing sites see byte-identical behavior.
5212 - if (!empty($options['custom_provider_for_embeddings']) && $options['custom_provider_for_embeddings'] === 'on') {
5213 - return $this->mxchat_generate_embedding_custom($text);
5214 - }
5215 -
5216 - // Determine endpoint and API key based on model
5217 - if (strpos($selected_model, 'voyage') === 0) {
5218 - $endpoint = 'https://api.voyageai.com/v1/embeddings';
5219 - $api_key = $options['voyage_api_key'] ?? '';
5220 -
5221 - // Check if Voyage API key is missing
5222 - if (empty($api_key)) {
5223 - //error_log('Voyage API key is missing');
5224 - return [
5225 - 'error' => esc_html__('Voyage AI API key is not configured', 'mxchat'),
5226 - 'error_code' => 'missing_voyage_api_key'
5227 - ];
5228 - }
5229 - } elseif (strpos($selected_model, 'gemini-embedding') === 0) {
5230 - $endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . $selected_model . ':embedContent';
5231 - $api_key = $options['gemini_api_key'] ?? '';
5232 -
5233 - // Check if Gemini API key is missing
5234 - if (empty($api_key)) {
5235 - //error_log('Gemini API key is missing');
5236 - return [
5237 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
5238 - 'error_code' => 'missing_gemini_api_key'
5239 - ];
5240 - }
5241 - } else {
5242 - $endpoint = 'https://api.openai.com/v1/embeddings';
5243 - // Use the passed API key for OpenAI
5244 -
5245 - // Check if OpenAI API key is missing
5246 - if (empty($api_key)) {
5247 - //error_log('OpenAI API key is missing');
5248 - return [
5249 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
5250 - 'error_code' => 'missing_openai_api_key'
5251 - ];
5252 - }
5253 - }
5254 -
5255 - // Check if text is empty
5256 - if (empty($text)) {
5257 - //error_log('Empty text provided for embedding generation');
5258 - return [
5259 - 'error' => esc_html__('No text provided for embedding generation', 'mxchat'),
5260 - 'error_code' => 'empty_embedding_text'
5261 - ];
5262 - }
5263 -
5264 - // Prepare request body based on provider
5265 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5266 - // Gemini API format
5267 - $request_body = [
5268 - 'model' => 'models/' . $selected_model,
5269 - 'content' => [
5270 - 'parts' => [
5271 - ['text' => $text]
5272 - ]
5273 - ],
5274 - 'outputDimensionality' => 1536
5275 - ];
5276 -
5277 - // Prepare headers for Gemini (API key as query parameter)
5278 - $endpoint .= '?key=' . $api_key;
5279 - $headers = [
5280 - 'Content-Type' => 'application/json'
5281 - ];
5282 - } else {
5283 - // OpenAI/Voyage API format
5284 - $request_body = [
5285 - 'input' => $text,
5286 - 'model' => $selected_model
5287 - ];
5288 -
5289 - // Add output_dimension for voyage-3-large
5290 - if ($selected_model === 'voyage-3-large') {
5291 - $request_body['output_dimension'] = 2048;
5292 - }
5293 -
5294 - // Prepare headers for OpenAI/Voyage
5295 - $headers = [
5296 - 'Content-Type' => 'application/json',
5297 - 'Authorization' => 'Bearer ' . $api_key
5298 - ];
5299 - }
5300 -
5301 - // Prepare request arguments
5302 - $args = [
5303 - 'body' => wp_json_encode($request_body),
5304 - 'headers' => $headers,
5305 - 'timeout' => 60,
5306 - 'redirection' => 5,
5307 - 'blocking' => true,
5308 - 'httpversion' => '1.0',
5309 - 'sslverify' => true,
5310 - ];
5311 -
5312 - // Make the request
5313 - $response = wp_remote_post($endpoint, $args);
5314 -
5315 - // Handle WordPress errors
5316 - if (is_wp_error($response)) {
5317 - $error_message = $response->get_error_message();
5318 - //error_log('Embedding Generation Error: ' . $error_message);
5319 - return [
5320 - 'error' => esc_html__('Connection error when generating embeddings: ', 'mxchat') . esc_html($error_message),
5321 - 'error_code' => 'embedding_connection_error'
5322 - ];
5323 - }
5324 -
5325 - // Check HTTP status code
5326 - $status_code = wp_remote_retrieve_response_code($response);
5327 - if ($status_code !== 200) {
5328 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5329 -
5330 - $error_message = isset($response_body['error']['message'])
5331 - ? $response_body['error']['message']
5332 - : 'HTTP Error ' . $status_code;
5333 -
5334 - $error_type = isset($response_body['error']['type'])
5335 - ? $response_body['error']['type']
5336 - : 'unknown';
5337 -
5338 - //error_log('Embedding API HTTP Error: ' . $status_code . ' - ' . $error_message);
5339 -
5340 - // Handle specific error types
5341 - switch ($error_type) {
5342 - case 'invalid_request_error':
5343 - if (strpos($error_message, 'API key') !== false) {
5344 - return [
5345 - 'error' => esc_html__('Invalid API key for embedding generation. Please check your API key configuration.', 'mxchat'),
5346 - 'error_code' => 'embedding_invalid_api_key'
5347 - ];
5348 - }
5349 - break;
5350 -
5351 - case 'authentication_error':
5352 - return [
5353 - 'error' => esc_html__('Authentication failed for embedding generation. Please check your API key.', 'mxchat'),
5354 - 'error_code' => 'embedding_auth_error'
5355 - ];
5356 -
5357 - case 'rate_limit_exceeded':
5358 - return [
5359 - 'error' => esc_html__('Rate limit exceeded for embedding generation. Please try again later.', 'mxchat'),
5360 - 'error_code' => 'embedding_rate_limit'
5361 - ];
5362 -
5363 - case 'quota_exceeded':
5364 - return [
5365 - 'error' => esc_html__('API quota exceeded for embedding generation. Please check your billing details.', 'mxchat'),
5366 - 'error_code' => 'embedding_quota_exceeded'
5367 - ];
5368 - }
5369 -
5370 - // Generic error fallback
5371 - return [
5372 - 'error' => esc_html__('Embedding API error - check embedding API key.: ', 'mxchat') . esc_html($error_message),
5373 - 'error_code' => 'embedding_api_error',
5374 - 'status_code' => $status_code
5375 - ];
5376 - }
5377 -
5378 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
5379 -
5380 - // Handle different response formats based on provider
5381 - if (strpos($selected_model, 'gemini-embedding') === 0) {
5382 - // Gemini API response format
5383 - if (isset($response_body['embedding']['values']) && is_array($response_body['embedding']['values'])) {
5384 - return $response_body['embedding']['values'];
5385 - } else {
5386 - //error_log('Invalid Gemini embedding response: ' . wp_json_encode($response_body));
5387 - return [
5388 - 'error' => esc_html__('Received invalid embedding data from the Gemini API.', 'mxchat'),
5389 - 'error_code' => 'invalid_gemini_embedding_response'
5390 - ];
5391 - }
5392 - } else {
5393 - // OpenAI/Voyage API response format
5394 - if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
5395 - return $response_body['data'][0]['embedding'];
5396 - } else {
5397 - //error_log('Invalid embedding response: ' . wp_json_encode($response_body));
5398 - return [
5399 - 'error' => esc_html__('Received invalid embedding data from the API.', 'mxchat'),
5400 - 'error_code' => 'invalid_embedding_response'
5401 - ];
5402 - }
5403 - }
5404 - } catch (Exception $e) {
5405 - //error_log('Embedding Exception: ' . $e->getMessage());
5406 - return [
5407 - 'error' => esc_html__('System error when generating embeddings: ', 'mxchat') . esc_html($e->getMessage()),
5408 - 'error_code' => 'embedding_exception'
5409 - ];
5410 - }
5411 -}
5412 -
5413 -
5414 -/**
5415 - * Generate embedding via a Custom (OpenAI-compatible) provider's /embeddings route.
5416 - * Only called when the opt-in 'custom_provider_for_embeddings' setting is on.
5417 - * Returns a numeric array (the embedding vector) on success, or ['error','error_code'] on failure.
5418 - */
5419 -private function mxchat_generate_embedding_custom($text) {
5420 - if (empty($text)) {
5421 - return ['error' => esc_html__('No text provided for embedding generation', 'mxchat'), 'error_code' => 'empty_embedding_text'];
5422 - }
5423 - $cfg = $this->mxchat_resolve_custom_provider();
5424 - if (empty($cfg['base_url'])) {
5425 - return ['error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url'];
5426 - }
5427 -
5428 - $options = get_option('mxchat_options');
5429 - $embed_url = $cfg['base_url'] . '/embeddings';
5430 - if (!empty($cfg['api_version'])) {
5431 - $embed_url .= (strpos($embed_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($cfg['api_version']);
5432 - }
5433 - $model = isset($options['custom_provider_embedding_model']) && trim((string) $options['custom_provider_embedding_model']) !== ''
5434 - ? trim((string) $options['custom_provider_embedding_model'])
5435 - : $cfg['model'];
5436 -
5437 - $response = wp_remote_post($embed_url, [
5438 - 'headers' => $this->mxchat_custom_provider_assoc_headers($cfg),
5439 - 'body' => wp_json_encode(['input' => $text, 'model' => $model]),
5440 - 'timeout' => 60,
5441 - ]);
5442 - if (is_wp_error($response)) {
5443 - return [
5444 - 'error' => esc_html__('Connection error when generating embeddings (custom provider): ', 'mxchat') . esc_html($response->get_error_message()),
5445 - 'error_code' => 'embedding_custom_connection_error',
5446 - ];
5447 - }
5448 - $status = wp_remote_retrieve_response_code($response);
5449 - $body = json_decode(wp_remote_retrieve_body($response), true);
5450 - if ($status !== 200) {
5451 - $msg = isset($body['error']['message']) ? $body['error']['message'] : 'HTTP ' . $status;
5452 - return [
5453 - 'error' => esc_html__('Custom embedding endpoint error: ', 'mxchat') . esc_html($msg),
5454 - 'error_code' => 'embedding_custom_api_error',
5455 - 'status_code' => $status,
5456 - ];
5457 - }
5458 - if (isset($body['data'][0]['embedding']) && is_array($body['data'][0]['embedding'])) {
5459 - return $body['data'][0]['embedding'];
5460 - }
5461 - return [
5462 - 'error' => esc_html__('Invalid embedding response from custom provider.', 'mxchat'),
5463 - 'error_code' => 'embedding_custom_invalid_response',
5464 - ];
5465 -}
5466 -
5467 -private function mxchat_find_relevant_content($user_embedding, $bot_id = 'default', $user_query = '') {
5468 - //error_log("MXCHAT DEBUG: find_relevant_content called with bot_id: " . $bot_id);
5469 -
5470 - // Check for OpenAI Vector Store first (takes priority when enabled)
5471 - $bot_vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
5472 -
5473 - if ($bot_vectorstore_config['use_vectorstore']) {
5474 - // Get current model to verify it's an OpenAI model
5475 - $bot_options = $this->get_bot_options($bot_id);
5476 - $mxchat_options = get_option('mxchat_options', array());
5477 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
5478 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
5479 -
5480 - if ($this->is_openai_chat_model($selected_model)) {
5481 - //error_log("MXCHAT DEBUG: Using OpenAI Vector Store for knowledge retrieval");
5482 - return $this->find_relevant_content_openai_vectorstore($user_query, $bot_id, $bot_vectorstore_config);
5483 - } else {
5484 - //error_log("MXCHAT DEBUG: Vector Store enabled but model is not OpenAI (" . $selected_model . "), skipping Vector Store");
5485 - }
5486 - }
5487 -
5488 - // Get bot-specific Pinecone configuration
5489 - $bot_pinecone_config = $this->get_bot_pinecone_config($bot_id);
5490 -
5491 - // Debug: Log the Pinecone configuration
5492 - //error_log("MXCHAT DEBUG: Pinecone config for bot '$bot_id':");
5493 - //error_log(" - use_pinecone: " . ($bot_pinecone_config['use_pinecone'] ? 'true' : 'false'));
5494 - //error_log(" - api_key: " . (empty($bot_pinecone_config['api_key']) ? 'EMPTY' : 'SET (hidden)'));
5495 - //error_log(" - host: " . ($bot_pinecone_config['host'] ?? 'NOT SET'));
5496 - //error_log(" - namespace: " . ($bot_pinecone_config['namespace'] ?? 'NOT SET'));
5497 -
5498 - // Determine whether to use Pinecone based on bot configuration
5499 - $use_pinecone = isset($bot_pinecone_config['use_pinecone']) ? $bot_pinecone_config['use_pinecone'] : false;
5500 -
5501 - //error_log("MXCHAT DEBUG: Using " . ($use_pinecone ? "Pinecone" : "WordPress Database") . " for knowledge retrieval");
5502 -
5503 - if ($use_pinecone) {
5504 - return $this->find_relevant_content_pinecone($user_embedding, $bot_id, $bot_pinecone_config);
5505 - } else {
5506 - return $this->find_relevant_content_wordpress($user_embedding, $bot_id);
5507 - }
5508 -}
5509 -
5510 -private function find_relevant_content_wordpress($user_embedding, $bot_id = 'default') {
5511 - global $wpdb;
5512 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
5513 - // Initialize similarity analysis storage
5514 - $this->last_similarity_analysis = [
5515 - 'knowledge_base_type' => 'WordPress Database',
5516 - 'bot_id' => $bot_id,
5517 - 'top_matches' => [],
5518 - 'threshold_used' => 0,
5519 - 'total_checked' => 0
5520 - ];
5521 -
5522 - // NEW: Initialize valid URLs array
5523 - $valid_urls = [];
5524 -
5525 - // Get bot-specific options for similarity threshold
5526 - $bot_options = $this->get_bot_options($bot_id);
5527 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
5528 -
5529 - // Get knowledge manager instance for role checking
5530 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
5531 -
5532 - // Get base similarity threshold from bot options or default options
5533 - $similarity_threshold = isset($current_options['similarity_threshold'])
5534 - ? ((int) $current_options['similarity_threshold']) / 100
5535 - : 0.35;
5536 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
5537 -
5538 - // Precompute bot_filter once, outside the streaming loop
5539 - $bot_filter = '';
5540 - if ($bot_id !== 'default') {
5541 - $column_exists = $wpdb->get_var("SHOW COLUMNS FROM {$system_prompt_table} LIKE 'bot_metadata'");
5542 - if ($column_exists) {
5543 - $bot_filter = $wpdb->prepare(" AND (bot_metadata = %s OR bot_metadata IS NULL OR bot_metadata = '')", $bot_id);
5544 - }
5545 - }
5546 -
5547 - // ===== STREAMING TOP-K PASS =====
5548 - // Stream rows in small batches, compute cosine similarity per row, and keep only:
5549 - // - top 10 by raw similarity (for the testing/debug display panel)
5550 - // - candidates above threshold with access (capped) for context assembly
5551 - // This bounds peak memory regardless of knowledge base size and avoids loading
5552 - // article_content for every row. article_content is fetched in Phase 2 for winners only.
5553 - $batch_size = 250;
5554 - $max_candidates = 200; // safety cap, well above rag_sources_limit * max_chunks_per_source
5555 - $top_display = [];
5556 - $candidates = [];
5557 - $total_checked = 0;
5558 - $offset = 0;
5559 -
5560 - do {
5561 - $batch = $wpdb->get_results($wpdb->prepare(
5562 - "SELECT id, embedding_vector, source_url, role_restriction
5563 - FROM {$system_prompt_table}
5564 - WHERE 1=1 {$bot_filter}
5565 - LIMIT %d OFFSET %d",
5566 - $batch_size,
5567 - $offset
5568 - ));
5569 -
5570 - if (empty($batch)) {
5571 - break;
5572 - }
5573 -
5574 - foreach ($batch as $row) {
5575 - $database_embedding = $row->embedding_vector
5576 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
5577 - : null;
5578 -
5579 - if (!is_array($database_embedding) || !is_array($user_embedding)) {
5580 - unset($database_embedding);
5581 - continue;
5582 - }
5583 -
5584 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
5585 - unset($database_embedding);
5586 -
5587 - $role_restriction = $row->role_restriction ?? 'public';
5588 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
5589 - $source_url = $row->source_url ?? '';
5590 -
5591 - // Maintain top 10 display buffer (insert-if-beats-worst)
5592 - if (count($top_display) < 10) {
5593 - $top_display[] = [
5594 - 'id' => $row->id,
5595 - 'similarity' => $similarity,
5596 - 'source_url' => $source_url,
5597 - 'role_restriction' => $role_restriction,
5598 - 'has_access' => $has_access,
5599 - ];
5600 - usort($top_display, function ($a, $b) {
5601 - return $b['similarity'] <=> $a['similarity'];
5602 - });
5603 - } elseif ($similarity > $top_display[9]['similarity']) {
5604 - $top_display[9] = [
5605 - 'id' => $row->id,
5606 - 'similarity' => $similarity,
5607 - 'source_url' => $source_url,
5608 - 'role_restriction' => $role_restriction,
5609 - 'has_access' => $has_access,
5610 - ];
5611 - usort($top_display, function ($a, $b) {
5612 - return $b['similarity'] <=> $a['similarity'];
5613 - });
5614 - }
5615 -
5616 - // Track candidates for context assembly (above threshold + has access)
5617 - if ($similarity >= $similarity_threshold && $has_access) {
5618 - $candidates[] = [
5619 - 'id' => $row->id,
5620 - 'similarity' => $similarity,
5621 - 'source_url' => $source_url,
5622 - ];
5623 - }
5624 -
5625 - $total_checked++;
5626 - }
5627 -
5628 - unset($batch);
5629 -
5630 - // Trim candidates periodically to cap memory during long scans
5631 - if (count($candidates) > $max_candidates) {
5632 - usort($candidates, function ($a, $b) {
5633 - return $b['similarity'] <=> $a['similarity'];
5634 - });
5635 - $candidates = array_slice($candidates, 0, $max_candidates);
5636 - }
5637 -
5638 - $offset += $batch_size;
5639 - } while (true);
5640 -
5641 - if ($total_checked === 0) {
5642 - $this->current_valid_urls = [];
5643 - return '';
5644 - }
5645 -
5646 - // Final candidates sort (best first)
5647 - if (count($candidates) > 1) {
5648 - usort($candidates, function ($a, $b) {
5649 - return $b['similarity'] <=> $a['similarity'];
5650 - });
5651 - }
5652 -
5653 - // ===== PHASE 2: FETCH ARTICLE CONTENT ONLY FOR WINNERS =====
5654 - // Gather unique IDs we actually need (top_display + candidates) and pull
5655 - // article_content in bounded IN() batches. This avoids loading content for
5656 - // every row during the similarity scan.
5657 - $needed_ids = [];
5658 - foreach ($top_display as $item) {
5659 - $needed_ids[$item['id']] = true;
5660 - }
5661 - foreach ($candidates as $item) {
5662 - $needed_ids[$item['id']] = true;
5663 - }
5664 - $needed_ids = array_keys($needed_ids);
5665 -
5666 - $content_map = [];
5667 - if (!empty($needed_ids)) {
5668 - foreach (array_chunk($needed_ids, 250) as $chunk_ids) {
5669 - $placeholders = implode(',', array_fill(0, count($chunk_ids), '%d'));
5670 - $rows = $wpdb->get_results($wpdb->prepare(
5671 - "SELECT id, article_content FROM {$system_prompt_table} WHERE id IN ($placeholders)",
5672 - ...$chunk_ids
5673 - ));
5674 - foreach ($rows as $r) {
5675 - $content_map[$r->id] = $r->article_content;
5676 - }
5677 - unset($rows);
5678 - }
5679 - }
5680 -
5681 - // Build the all_similarities display array from the top 10
5682 - $all_similarities = [];
5683 - foreach ($top_display as $item) {
5684 - $article_content_for_parse = $content_map[$item['id']] ?? '';
5685 - $parsed_for_display = MxChat_Chunker::parse_stored_chunk($article_content_for_parse);
5686 - $is_chunk = $parsed_for_display['is_chunked'];
5687 - $chunk_meta = $parsed_for_display['metadata'];
5688 -
5689 - if (!empty($item['source_url']) && $item['source_url'] !== '#') {
5690 - $source_display = $item['source_url'];
5691 - } else {
5692 - $content_preview = strip_tags($article_content_for_parse);
5693 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
5694 - $source_display = substr(trim($content_preview), 0, 50) . '...';
5695 - }
5696 -
5697 - $all_similarities[] = [
5698 - 'document_id' => $item['id'],
5699 - 'similarity' => $item['similarity'],
5700 - 'similarity_percentage' => round($item['similarity'] * 100, 2),
5701 - 'above_threshold' => $item['similarity'] >= $similarity_threshold,
5702 - 'source_display' => $source_display,
5703 - 'content_preview' => substr(strip_tags($parsed_for_display['text'] ?? ''), 0, 100) . '...',
5704 - 'used_for_context' => false,
5705 - 'role_restriction' => $item['role_restriction'],
5706 - 'has_access' => $item['has_access'],
5707 - 'filtered_out' => !$item['has_access'],
5708 - 'is_chunk' => $is_chunk,
5709 - 'chunk_index' => $is_chunk ? ($chunk_meta['chunk_index'] ?? 0) : null,
5710 - 'total_chunks' => $is_chunk ? ($chunk_meta['total_chunks'] ?? 1) : null
5711 - ];
5712 - }
5713 -
5714 - // Build url_groups from candidates for chunk reassembly
5715 - $url_groups = array();
5716 - foreach ($candidates as $cand) {
5717 - $article_content = $content_map[$cand['id']] ?? '';
5718 - $parsed = MxChat_Chunker::parse_stored_chunk($article_content);
5719 - $is_chunked = $parsed['is_chunked'];
5720 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5721 - $text_content = $parsed['text'];
5722 -
5723 - $source_url = $cand['source_url'];
5724 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $cand['id'];
5725 -
5726 - if (!isset($url_groups[$group_key])) {
5727 - $url_groups[$group_key] = array(
5728 - 'source_url' => $source_url,
5729 - 'best_score' => 0,
5730 - 'is_chunked' => $is_chunked,
5731 - 'chunks' => array(),
5732 - 'single_text' => '',
5733 - 'single_id' => null
5734 - );
5735 - }
5736 -
5737 - if ($cand['similarity'] > $url_groups[$group_key]['best_score']) {
5738 - $url_groups[$group_key]['best_score'] = $cand['similarity'];
5739 - }
5740 -
5741 - if ($is_chunked) {
5742 - $url_groups[$group_key]['is_chunked'] = true;
5743 - $url_groups[$group_key]['chunks'][] = array(
5744 - 'id' => $cand['id'],
5745 - 'score' => $cand['similarity'],
5746 - 'chunk_index' => $chunk_index,
5747 - 'text' => $text_content
5748 - );
5749 - } else {
5750 - $url_groups[$group_key]['single_text'] = $text_content;
5751 - $url_groups[$group_key]['single_id'] = $cand['id'];
5752 - }
5753 - }
5754 -
5755 - // Sort ALL similarities for testing display (highest first)
5756 - usort($all_similarities, function ($a, $b) {
5757 - return $b['similarity'] <=> $a['similarity'];
5758 - });
5759 -
5760 - // Sort URL groups by best score (highest first)
5761 - uasort($url_groups, function($a, $b) {
5762 - return $b['best_score'] <=> $a['best_score'];
5763 - });
5764 -
5765 - // Get RAG sources limit from options (default 6, min 3, max 10)
5766 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
5767 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
5768 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
5769 -
5770 - // Take top N unique URLs based on user setting
5771 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
5772 -
5773 - // Track which document IDs are used for context
5774 - $used_document_ids = [];
5775 - foreach ($top_urls as $group) {
5776 - if ($group['is_chunked']) {
5777 - foreach ($group['chunks'] as $chunk) {
5778 - $used_document_ids[] = $chunk['id'];
5779 - }
5780 - } elseif ($group['single_id']) {
5781 - $used_document_ids[] = $group['single_id'];
5782 - }
5783 - }
5784 -
5785 - // Update the all_similarities array to mark which were actually used
5786 - foreach ($all_similarities as &$similarity_item) {
5787 - $similarity_item['used_for_context'] = in_array($similarity_item['document_id'], $used_document_ids);
5788 - }
5789 -
5790 - // Store top 10 for testing panel
5791 - $this->last_similarity_analysis['top_matches'] = array_slice($all_similarities, 0, 10);
5792 - $this->last_similarity_analysis['total_checked'] = $total_checked;
5793 -
5794 - // Initialize final content
5795 - $content = '';
5796 - $matches_used = 0;
5797 - $total_chunks_used = 0;
5798 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
5799 - if ($max_total_chunks < 8) $max_total_chunks = 8;
5800 - if ($max_total_chunks > 20) $max_total_chunks = 20;
5801 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
5802 -
5803 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
5804 - // Use fresh options to ensure we get the latest setting value
5805 - $fresh_options = get_option('mxchat_options', []);
5806 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
5807 -
5808 - // Build content from top sources
5809 - foreach ($top_urls as $group_key => $group) {
5810 - $source_url = $group['source_url']; // Use actual source_url, not the group key
5811 -
5812 - // Stop if we've hit the total chunk limit
5813 - if ($total_chunks_used >= $max_total_chunks) {
5814 - break;
5815 - }
5816 -
5817 - $full_text = '';
5818 - $chunks_in_this_source = 1; // Default for non-chunked content
5819 -
5820 - if ($group['is_chunked']) {
5821 - // Calculate how many chunks we can still use (respect both total and per-source caps)
5822 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
5823 -
5824 - // Fetch chunks for this URL with limit
5825 - $full_text = $this->reassemble_chunks_from_wordpress($source_url, $chunks_remaining, $chunks_in_this_source);
5826 -
5827 - // If fetching all chunks fails, fall back to matched chunks
5828 - if (empty($full_text)) {
5829 - // Sort matched chunks by index and concatenate
5830 - usort($group['chunks'], function($a, $b) {
5831 - return $a['chunk_index'] <=> $b['chunk_index'];
5832 - });
5833 -
5834 - $chunk_texts = array();
5835 - $chunks_in_this_source = 0;
5836 - foreach ($group['chunks'] as $chunk) {
5837 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
5838 - break;
5839 - }
5840 - $chunk_texts[] = $chunk['text'];
5841 - $chunks_in_this_source++;
5842 - }
5843 - $full_text = implode("\n\n", $chunk_texts);
5844 - }
5845 - } else {
5846 - $full_text = $group['single_text'];
5847 - $chunks_in_this_source = 1;
5848 - }
5849 -
5850 - if (!empty($full_text)) {
5851 - // Strip URLs from content if citation links are disabled
5852 - if (!$citation_links_enabled) {
5853 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
5854 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
5855 - }
5856 -
5857 - // Use numbered reference for URL-based entries, plain info label for manual entries
5858 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
5859 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
5860 - $matches_used++;
5861 - $content .= "## Reference " . $matches_used . " ##\n";
5862 - $content .= $full_text . "\n\n";
5863 -
5864 - // Only include citation URLs if citation links are enabled
5865 - if ($citation_links_enabled) {
5866 - $valid_urls[] = $source_url;
5867 - $content .= "URL: " . $source_url . "\n\n";
5868 - }
5869 - } else {
5870 - // Manual entry — no reference number, no citation
5871 - $content .= "## Information ##\n";
5872 - $content .= $full_text . "\n\n";
5873 - }
5874 -
5875 - // Extract any URLs from the text content itself (only if citation links enabled)
5876 - if ($citation_links_enabled) {
5877 - preg_match_all(
5878 - '#\bhttps?://[^\s<>"\']+#i',
5879 - $full_text,
5880 - $content_urls
5881 - );
5882 - if (!empty($content_urls[0])) {
5883 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
5884 - }
5885 - }
5886 -
5887 - $total_chunks_used += $chunks_in_this_source;
5888 - }
5889 - }
5890 -
5891 - // NEW: Store unique valid URLs for validation
5892 - $this->current_valid_urls = array_unique($valid_urls);
5893 -
5894 - // Store sources and chunks counts for testing/transcript display
5895 - $this->last_similarity_analysis['sources_used'] = $matches_used;
5896 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
5897 -
5898 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
5899 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
5900 -
5901 - // Add response guidelines
5902 - if (empty($top_urls)) {
5903 - $content = "No reference information was found for this query.\n\n";
5904 - } else {
5905 - // Build response guidelines based on citation links setting
5906 - $content .= "\n## Response Guidelines ##\n" .
5907 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
5908 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
5909 - "If you don't have specific information or are uncertain about any details, it's always " .
5910 - "better to honestly say you don't know rather than making up or guessing at answers. " .
5911 - "When information is incomplete, let them know you are unsure.\n\n";
5912 -
5913 - // Only add hyperlink instructions if citation links are enabled
5914 - if ($citation_links_enabled) {
5915 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
5916 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
5917 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
5918 - } else {
5919 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
5920 - "Simply provide helpful answers based on the reference information without citing sources.";
5921 - }
5922 - }
5923 -
5924 - return trim($content);
5925 -}
5926 -
5927 -/**
5928 - * Fetch and reassemble chunks for a URL from WordPress database
5929 - *
5930 - * @param string $source_url The source URL to fetch chunks for
5931 - * @param int $max_chunks Maximum number of chunks to return (0 = unlimited)
5932 - * @param int &$chunk_count Reference to store the actual number of chunks returned
5933 - * @return string Reassembled content from chunks
5934 - */
5935 -private function reassemble_chunks_from_wordpress($source_url, $max_chunks = 0, &$chunk_count = 0) {
5936 - global $wpdb;
5937 - $table = $wpdb->prefix . 'mxchat_system_prompt_content';
5938 -
5939 - // Fetch all rows with this source_url
5940 - $rows = $wpdb->get_results($wpdb->prepare(
5941 - "SELECT article_content FROM {$table}
5942 - WHERE source_url = %s
5943 - ORDER BY id ASC",
5944 - $source_url
5945 - ));
5946 -
5947 - if (empty($rows)) {
5948 - $chunk_count = 0;
5949 - return '';
5950 - }
5951 -
5952 - // Parse and sort chunks by index
5953 - $chunks = array();
5954 - foreach ($rows as $row) {
5955 - $parsed = MxChat_Chunker::parse_stored_chunk($row->article_content);
5956 -
5957 - if ($parsed['is_chunked']) {
5958 - $chunk_index = $parsed['metadata']['chunk_index'] ?? 0;
5959 - $chunks[$chunk_index] = $parsed['text'];
5960 - } else {
5961 - // Non-chunked content - just return it
5962 - $chunks[] = $parsed['text'];
5963 - }
5964 - }
5965 -
5966 - // Sort by chunk index
5967 - ksort($chunks);
5968 -
5969 - // Apply chunk limit if specified
5970 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
5971 - $chunks = array_slice($chunks, 0, $max_chunks, true);
5972 - }
5973 -
5974 - // Store actual chunk count
5975 - $chunk_count = count($chunks);
5976 -
5977 - // Reassemble content
5978 - return implode("\n\n", $chunks);
5979 -}
5980 -
5981 -private function find_relevant_content_pinecone($user_embedding, $bot_id = 'default', $bot_config = null) {
5982 - global $wpdb;
5983 -
5984 - //error_log("MXCHAT DEBUG: find_relevant_content_pinecone called");
5985 - //error_log(" - bot_id: " . $bot_id);
5986 - //error_log(" - user_embedding is array: " . (is_array($user_embedding) ? 'yes' : 'no'));
5987 - //error_log(" - user_embedding count: " . (is_array($user_embedding) ? count($user_embedding) : 'N/A'));
5988 -
5989 - // Use bot-specific config or fall back to default
5990 - if ($bot_config === null) {
5991 - $bot_config = $this->get_bot_pinecone_config($bot_id);
5992 - }
5993 -
5994 - $api_key = $bot_config['api_key'] ?? '';
5995 - $host = $bot_config['host'] ?? '';
5996 - $namespace = $bot_config['namespace'] ?? '';
5997 -
5998 - //error_log("MXCHAT DEBUG: Pinecone query parameters:");
5999 - //error_log(" - API Key: " . (empty($api_key) ? 'EMPTY - ERROR!' : 'Present (length: ' . strlen($api_key) . ')'));
6000 - //error_log(" - Host: " . (empty($host) ? 'EMPTY - ERROR!' : $host));
6001 - //error_log(" - Namespace: " . (empty($namespace) ? 'EMPTY (will use default)' : $namespace));
6002 -
6003 - // Initialize similarity analysis storage
6004 - $this->last_similarity_analysis = [
6005 - 'knowledge_base_type' => 'Pinecone',
6006 - 'bot_id' => $bot_id,
6007 - 'namespace' => $namespace,
6008 - 'top_matches' => [],
6009 - 'threshold_used' => 0,
6010 - 'total_checked' => 0
6011 - ];
6012 -
6013 - // NEW: Initialize valid URLs array
6014 - $valid_urls = [];
6015 -
6016 - if (empty($host) || empty($api_key)) {
6017 - //error_log("MXCHAT DEBUG ERROR: Missing Pinecone host or API key!");
6018 - //error_log(" - Host empty: " . (empty($host) ? 'YES' : 'NO'));
6019 - //error_log(" - API key empty: " . (empty($api_key) ? 'YES' : 'NO'));
6020 - // Store empty array for valid URLs since we can't proceed
6021 - $this->current_valid_urls = [];
6022 - return '';
6023 - }
6024 -
6025 - // Get knowledge manager instance for role checking
6026 - $knowledge_manager = MxChat_Knowledge_Manager::get_instance();
6027 -
6028 - // Get the similarity threshold from the bot options or main options
6029 - $bot_options = $this->get_bot_options($bot_id);
6030 - $current_options = !empty($bot_options) ? $bot_options : get_option('mxchat_options', []);
6031 -
6032 - $similarity_threshold = isset($current_options['similarity_threshold'])
6033 - ? ((int) $current_options['similarity_threshold']) / 100
6034 - : 0.35;
6035 -
6036 - $this->last_similarity_analysis['threshold_used'] = $similarity_threshold;
6037 -
6038 - // Prepare the query request for Pinecone
6039 - $api_endpoint = "https://{$host}/query";
6040 -
6041 - $request_body = array(
6042 - 'vector' => $user_embedding,
6043 - 'topK' => 50, // Increased for chunked content grouping - need more candidates to find top N unique URLs
6044 - 'includeMetadata' => true,
6045 - 'includeValues' => true
6046 - );
6047 -
6048 - // Add namespace if specified for this bot
6049 - if (!empty($namespace)) {
6050 - $request_body['namespace'] = $namespace;
6051 - }
6052 -
6053 - //error_log("MXCHAT DEBUG: About to call Pinecone API");
6054 - //error_log(" - Endpoint: " . $api_endpoint);
6055 - //error_log(" - Namespace in request: " . (!empty($namespace) ? $namespace : 'NOT SET'));
6056 -
6057 - $response = wp_remote_post($api_endpoint, array(
6058 - 'headers' => array(
6059 - 'Api-Key' => $api_key,
6060 - 'accept' => 'application/json',
6061 - 'content-type' => 'application/json'
6062 - ),
6063 - 'body' => wp_json_encode($request_body),
6064 - 'timeout' => 30
6065 - ));
6066 -
6067 - if (is_wp_error($response)) {
6068 - //error_log("MXCHAT DEBUG ERROR: WP Error in Pinecone request: " . $response->get_error_message());
6069 - // Store empty array for valid URLs
6070 - $this->current_valid_urls = [];
6071 - return '';
6072 - }
6073 -
6074 - $response_code = wp_remote_retrieve_response_code($response);
6075 - //error_log("MXCHAT DEBUG: Pinecone response code: " . $response_code);
6076 -
6077 - if ($response_code !== 200) {
6078 - $response_body = wp_remote_retrieve_body($response);
6079 - //error_log("MXCHAT DEBUG ERROR: Pinecone API error response: " . substr($response_body, 0, 500));
6080 - // Store empty array for valid URLs
6081 - $this->current_valid_urls = [];
6082 - return '';
6083 - }
6084 -
6085 - // ADD DETAILED DEBUG SECTION HERE
6086 - $response_body = wp_remote_retrieve_body($response);
6087 - //error_log("MXCHAT DEBUG: Raw Pinecone response length: " . strlen($response_body));
6088 -
6089 - $results = json_decode($response_body, true);
6090 -
6091 - if (json_last_error() !== JSON_ERROR_NONE) {
6092 - //error_log("MXCHAT DEBUG ERROR: JSON decode error: " . json_last_error_msg());
6093 - //error_log("MXCHAT DEBUG: First 500 chars of response: " . substr($response_body, 0, 500));
6094 - // Store empty array for valid URLs
6095 - $this->current_valid_urls = [];
6096 - return '';
6097 - }
6098 -
6099 - //error_log("MXCHAT DEBUG: Pinecone response structure:");
6100 - //error_log(" - Has 'matches' key: " . (isset($results['matches']) ? 'yes' : 'no'));
6101 - //error_log(" - Has 'namespace' key: " . (isset($results['namespace']) ? 'yes (' . $results['namespace'] . ')' : 'no'));
6102 -
6103 - if (empty($results['matches'])) {
6104 - //error_log("MXCHAT DEBUG: No matches found in Pinecone response");
6105 - //error_log("MXCHAT DEBUG: Response keys: " . implode(', ', array_keys($results)));
6106 - // Store empty array for valid URLs
6107 - $this->current_valid_urls = [];
6108 - return '';
6109 - }
6110 -
6111 - //error_log("MXCHAT DEBUG: Found " . count($results['matches']) . " matches in Pinecone");
6112 -
6113 - // Log first match details for debugging
6114 - if (!empty($results['matches'][0])) {
6115 - $first_match = $results['matches'][0];
6116 - //error_log("MXCHAT DEBUG: First match details:");
6117 - //error_log(" - Score: " . ($first_match['score'] ?? 'no score'));
6118 - //error_log(" - Has metadata: " . (isset($first_match['metadata']) ? 'yes' : 'no'));
6119 - if (isset($first_match['metadata'])) {
6120 - //error_log(" - Metadata keys: " . implode(', ', array_keys($first_match['metadata'])));
6121 - }
6122 - }
6123 -
6124 - // Initialize the final content
6125 - $content = '';
6126 - $matches_used = 0;
6127 - $matches_used_for_context = [];
6128 - $total_chunks_used = 0;
6129 - $max_total_chunks = isset($current_options['rag_chunks_limit']) ? intval($current_options['rag_chunks_limit']) : 15;
6130 - if ($max_total_chunks < 8) $max_total_chunks = 8;
6131 - if ($max_total_chunks > 20) $max_total_chunks = 20;
6132 - $max_chunks_per_source = 5; // Cap per individual source to limit token usage
6133 -
6134 - // Check if citation links are enabled (default to 'on' for backwards compatibility)
6135 - // Use fresh options to ensure we get the latest setting value
6136 - $fresh_options = get_option('mxchat_options', []);
6137 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
6138 -
6139 - // NEW CHUNKING LOGIC: Group results by source_url for chunk reassembly
6140 - $url_groups = array();
6141 -
6142 - foreach ($results['matches'] as $index => $match) {
6143 - // Skip if similarity is below threshold
6144 - if ($match['score'] < $similarity_threshold) {
6145 - continue;
6146 - }
6147 -
6148 - $metadata = $match['metadata'] ?? array();
6149 - $source_url = $metadata['source_url'] ?? '';
6150 - $match_id = $match['id'] ?? '';
6151 -
6152 - // LAZY ROLE CHECK: Only check role for content we're actually considering
6153 - $role_restriction = $this->get_single_vector_role($match_id, $metadata);
6154 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6155 -
6156 - // Skip if user doesn't have access
6157 - if (!$has_access) {
6158 - continue;
6159 - }
6160 -
6161 - // Use a unique key for manual entries without a source URL
6162 - $group_key = !empty($source_url) ? $source_url : '_manual_' . $match_id;
6163 -
6164 - // Group by source URL (or unique key for manual entries)
6165 - if (!isset($url_groups[$group_key])) {
6166 - $url_groups[$group_key] = array(
6167 - 'source_url' => $source_url,
6168 - 'best_score' => 0,
6169 - 'is_chunked' => isset($metadata['is_chunked']) && $metadata['is_chunked'],
6170 - 'chunks' => array(),
6171 - 'single_text' => ''
6172 - );
6173 - }
6174 -
6175 - // Track best score for this group
6176 - if ($match['score'] > $url_groups[$group_key]['best_score']) {
6177 - $url_groups[$group_key]['best_score'] = $match['score'];
6178 - }
6179 -
6180 - // Store chunk info or single text
6181 - if ($url_groups[$group_key]['is_chunked']) {
6182 - $url_groups[$group_key]['chunks'][] = array(
6183 - 'id' => $match_id,
6184 - 'score' => $match['score'],
6185 - 'chunk_index' => $metadata['chunk_index'] ?? 0,
6186 - 'text' => $metadata['text'] ?? ''
6187 - );
6188 - } else {
6189 - // Non-chunked content - just store the text
6190 - $url_groups[$group_key]['single_text'] = $metadata['text'] ?? '';
6191 - $url_groups[$group_key]['single_id'] = $match_id;
6192 - }
6193 - }
6194 -
6195 - // Sort URL groups by best score (highest first)
6196 - uasort($url_groups, function($a, $b) {
6197 - return $b['best_score'] <=> $a['best_score'];
6198 - });
6199 -
6200 - // Get RAG sources limit from options (default 6, min 3, max 10)
6201 - $rag_sources_limit = isset($current_options['rag_sources_limit']) ? intval($current_options['rag_sources_limit']) : 3;
6202 - if ($rag_sources_limit < 3) $rag_sources_limit = 3;
6203 - if ($rag_sources_limit > 10) $rag_sources_limit = 10;
6204 -
6205 - // Take top N unique URLs based on user setting
6206 - $top_urls = array_slice($url_groups, 0, $rag_sources_limit, true);
6207 -
6208 - // Track which match IDs are actually used for context
6209 - foreach ($top_urls as $group) {
6210 - if ($group['is_chunked']) {
6211 - foreach ($group['chunks'] as $chunk) {
6212 - $matches_used_for_context[] = $chunk['id'];
6213 - }
6214 - } elseif (!empty($group['single_id'])) {
6215 - $matches_used_for_context[] = $group['single_id'];
6216 - }
6217 - }
6218 -
6219 - // Build content from top sources
6220 - foreach ($top_urls as $group_key => $group) {
6221 - $source_url = $group['source_url']; // Use actual source_url, not the group key
6222 -
6223 - // Stop if we've hit the total chunk limit
6224 - if ($total_chunks_used >= $max_total_chunks) {
6225 - break;
6226 - }
6227 -
6228 - $full_text = '';
6229 - $chunks_in_this_source = 1; // Default for non-chunked content
6230 -
6231 - if ($group['is_chunked']) {
6232 - // Calculate how many chunks we can still use (respect both total and per-source caps)
6233 - $chunks_remaining = min($max_chunks_per_source, $max_total_chunks - $total_chunks_used);
6234 -
6235 - // Fetch chunks for this URL with limit
6236 - $full_text = $this->reassemble_chunks_from_pinecone($source_url, $bot_config, $chunks_remaining, $chunks_in_this_source);
6237 -
6238 - // If fetching all chunks fails, fall back to matched chunks
6239 - if (empty($full_text)) {
6240 - // Sort matched chunks by index and concatenate
6241 - usort($group['chunks'], function($a, $b) {
6242 - return $a['chunk_index'] <=> $b['chunk_index'];
6243 - });
6244 -
6245 - $chunk_texts = array();
6246 - $chunks_in_this_source = 0;
6247 - foreach ($group['chunks'] as $chunk) {
6248 - if ($total_chunks_used + $chunks_in_this_source >= $max_total_chunks) {
6249 - break;
6250 - }
6251 - $chunk_texts[] = $chunk['text'];
6252 - $chunks_in_this_source++;
6253 - }
6254 - $full_text = implode("\n\n", $chunk_texts);
6255 - }
6256 - } else {
6257 - $full_text = $group['single_text'];
6258 - $chunks_in_this_source = 1;
6259 - }
6260 -
6261 - if (!empty($full_text)) {
6262 - // Strip URLs from content if citation links are disabled
6263 - if (!$citation_links_enabled) {
6264 - $full_text = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $full_text);
6265 - $full_text = preg_replace('/\s+/', ' ', trim($full_text)); // Clean up extra spaces
6266 - }
6267 -
6268 - // Use numbered reference for URL-based entries, plain info label for manual entries
6269 - // Manual entries are stored with an internal mxchat:// placeholder URL — never expose them as citations
6270 - if (!empty($source_url) && $source_url !== '#' && strpos($source_url, 'mxchat://') !== 0) {
6271 - $matches_used++;
6272 - $content .= "## Reference " . $matches_used . " ##\n";
6273 - $content .= $full_text . "\n\n";
6274 -
6275 - // Only include citation URLs if citation links are enabled
6276 - if ($citation_links_enabled) {
6277 - $valid_urls[] = $source_url;
6278 - $content .= "URL: " . $source_url . "\n\n";
6279 - }
6280 - } else {
6281 - // Manual entry — no reference number, no citation. Count it as a USED
6282 - // source (plan-mxchat-20260622-c1fe6a): without this, manual/Direct-Content
6283 - // entries (empty or mxchat:// source_url) never increment $matches_used, so
6284 - // the gate below (`if ($matches_used === 0)`) discards manual-only context on
6285 - // the Pinecone backend and the model is told "No reference information was
6286 - // found" — even though the testing panel reports used_for_context:true. It
6287 - // also corrects the cosmetic sources_used:0 the panel/transcript showed. The
6288 - // sibling local/WP-DB builder gates on empty($top_urls), so it never had this
6289 - // bug; this brings Pinecone to parity. Manual entries are still uncited (not
6290 - // added to $valid_urls, no "URL:" line).
6291 - $matches_used++;
6292 - $content .= "## Information ##\n";
6293 - $content .= $full_text . "\n\n";
6294 - }
6295 -
6296 - // Extract any URLs from the text content itself (only if citation links enabled)
6297 - if ($citation_links_enabled) {
6298 - preg_match_all(
6299 - '#\bhttps?://[^\s<>"\']+#i',
6300 - $full_text,
6301 - $content_urls
6302 - );
6303 - if (!empty($content_urls[0])) {
6304 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6305 - }
6306 - }
6307 -
6308 - $total_chunks_used += $chunks_in_this_source;
6309 - }
6310 - }
6311 -
6312 - // Process ALL matches for testing data (top 10) - with role checking for testing display
6313 - $all_matches = [];
6314 - foreach ($results['matches'] as $index => $match) {
6315 - if ($index >= 10) break; // Limit to top 10 for testing
6316 -
6317 - $match_id = $match['id'] ?? '';
6318 -
6319 - // Check role access for testing display (use cache if available)
6320 - $role_restriction = $this->get_single_vector_role($match_id, $match['metadata']);
6321 - $has_access = $knowledge_manager->mxchat_user_has_content_access($role_restriction);
6322 -
6323 - $source_display = '';
6324 - if (!empty($match['metadata']['source_url'])) {
6325 - $source_display = $match['metadata']['source_url'];
6326 - } else {
6327 - $content_preview = strip_tags($match['metadata']['text'] ?? '');
6328 - $content_preview = preg_replace('/\s+/', ' ', $content_preview);
6329 - $source_display = substr(trim($content_preview), 0, 50) . '...';
6330 - }
6331 -
6332 - $match_id_for_display = $match['id'] ?? $index;
6333 -
6334 - // Check for chunk metadata in Pinecone
6335 - $is_chunk = isset($match['metadata']['is_chunked']) && $match['metadata']['is_chunked'];
6336 - $chunk_index = isset($match['metadata']['chunk_index']) ? intval($match['metadata']['chunk_index']) : null;
6337 - $total_chunks = isset($match['metadata']['total_chunks']) ? intval($match['metadata']['total_chunks']) : null;
6338 -
6339 - // Also detect chunk from vector ID pattern: {hash}_chunk_{index}
6340 - if (!$is_chunk && MxChat_Chunker::is_chunk_vector_id($match_id_for_display)) {
6341 - $is_chunk = true;
6342 - }
6343 -
6344 - $all_matches[] = [
6345 - 'document_id' => $match_id_for_display,
6346 - 'similarity' => $match['score'],
6347 - 'similarity_percentage' => round($match['score'] * 100, 2),
6348 - 'above_threshold' => $match['score'] >= $similarity_threshold,
6349 - 'source_display' => $source_display,
6350 - 'content_preview' => substr(strip_tags($match['metadata']['text'] ?? ''), 0, 100) . '...',
6351 - 'used_for_context' => in_array($match_id_for_display, $matches_used_for_context),
6352 - 'role_restriction' => $role_restriction,
6353 - 'has_access' => $has_access,
6354 - 'filtered_out' => !$has_access,
6355 - 'is_chunk' => $is_chunk,
6356 - 'chunk_index' => $chunk_index,
6357 - 'total_chunks' => $total_chunks
6358 - ];
6359 - }
6360 -
6361 - // Store for testing panel
6362 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6363 - $this->last_similarity_analysis['total_checked'] = count($results['matches']);
6364 - $this->last_similarity_analysis['sources_used'] = $matches_used;
6365 - $this->last_similarity_analysis['total_chunks_used'] = $total_chunks_used;
6366 -
6367 - // NEW: Store unique valid URLs for validation
6368 - $this->current_valid_urls = array_unique($valid_urls);
6369 -
6370 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6371 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6372 -
6373 - // Add response guidelines
6374 - if ($matches_used === 0) {
6375 - $content = "No reference information was found for this query.\n\n";
6376 - } else {
6377 - // Build response guidelines based on citation links setting
6378 - $content .= "\n## Response Guidelines ##\n" .
6379 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6380 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6381 - "If you don't have specific information or are uncertain about any details, it's always " .
6382 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6383 - "When information is incomplete, let them know you are unsure.\n\n";
6384 -
6385 - // Only add hyperlink instructions if citation links are enabled
6386 - if ($citation_links_enabled) {
6387 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6388 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about. " .
6389 - "Only cite references that have a URL. Do not cite or add source labels to Information sections that have no URL.";
6390 - } else {
6391 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6392 - "Simply provide helpful answers based on the reference information without citing sources.";
6393 - }
6394 - }
6395 -
6396 - return trim($content);
6397 -}
6398 -
6399 -/**
6400 - * Get role restriction for a single vector (with caching)
6401 - */
6402 -private function get_single_vector_role($vector_id, $metadata = array()) {
6403 - global $wpdb;
6404 -
6405 - if (empty($vector_id)) {
6406 - return 'public';
6407 - }
6408 -
6409 - // Check cache first
6410 - $cache_key = 'mxchat_vector_role_' . $vector_id;
6411 - $cached_role = wp_cache_get($cache_key, 'mxchat_vector_roles');
6412 -
6413 - if ($cached_role !== false) {
6414 - return $cached_role;
6415 - }
6416 -
6417 - $role_restriction = 'public';
6418 -
6419 - // First try Pinecone metadata
6420 - if (!empty($metadata['role_restriction'])) {
6421 - $role_restriction = $metadata['role_restriction'];
6422 - } else {
6423 - // Check WordPress table for user-modified roles
6424 - $roles_table = $wpdb->prefix . 'mxchat_pinecone_roles';
6425 - $stored_role = $wpdb->get_var($wpdb->prepare(
6426 - "SELECT role_restriction FROM {$roles_table} WHERE vector_id = %s",
6427 - $vector_id
6428 - ));
6429 -
6430 - if ($stored_role) {
6431 - $role_restriction = $stored_role;
6432 - }
6433 - }
6434 -
6435 - // Cache individual role for 1 hour
6436 - wp_cache_set($cache_key, $role_restriction, 'mxchat_vector_roles', 3600);
6437 -
6438 - return $role_restriction;
6439 -}
6440 -
6441 -/**
6442 - * Fetch and reassemble all chunks for a URL from Pinecone
6443 - *
6444 - * @param string $source_url The source URL to fetch chunks for
6445 - * @param array $bot_config Bot-specific Pinecone configuration
6446 - * @return string Reassembled content from all chunks
6447 - */
6448 -private function reassemble_chunks_from_pinecone($source_url, $bot_config, $max_chunks = 0, &$chunk_count = 0) {
6449 - $api_key = $bot_config['api_key'] ?? '';
6450 - $host = $bot_config['host'] ?? '';
6451 - $namespace = $bot_config['namespace'] ?? '';
6452 -
6453 - if (empty($host) || empty($api_key)) {
6454 - $chunk_count = 0;
6455 - return '';
6456 - }
6457 -
6458 - $base_hash = md5($source_url);
6459 -
6460 - // Use Pinecone list API to find all chunk vectors with this prefix
6461 - $list_url = "https://{$host}/vectors/list";
6462 -
6463 - // Limit to max_chunks if specified, otherwise fetch up to 100
6464 - $fetch_limit = ($max_chunks > 0 && $max_chunks < 100) ? $max_chunks : 100;
6465 -
6466 - $list_body = array(
6467 - 'prefix' => $base_hash . '_chunk_',
6468 - 'limit' => $fetch_limit
6469 - );
6470 -
6471 - if (!empty($namespace)) {
6472 - $list_body['namespace'] = $namespace;
6473 - }
6474 -
6475 - $list_response = wp_remote_post($list_url, array(
6476 - 'headers' => array(
6477 - 'Api-Key' => $api_key,
6478 - 'accept' => 'application/json',
6479 - 'content-type' => 'application/json'
6480 - ),
6481 - 'body' => wp_json_encode($list_body),
6482 - 'timeout' => 30
6483 - ));
6484 -
6485 - if (is_wp_error($list_response)) {
6486 - //error_log('[MXCHAT-CHUNK] List API error: ' . $list_response->get_error_message());
6487 - return '';
6488 - }
6489 -
6490 - $list_data = json_decode(wp_remote_retrieve_body($list_response), true);
6491 -
6492 - if (empty($list_data['vectors'])) {
6493 - //error_log('[MXCHAT-CHUNK] No chunk vectors found for URL: ' . $source_url);
6494 - return '';
6495 - }
6496 -
6497 - // Extract vector IDs
6498 - $vector_ids = array();
6499 - foreach ($list_data['vectors'] as $vector) {
6500 - if (isset($vector['id'])) {
6501 - $vector_ids[] = $vector['id'];
6502 - }
6503 - }
6504 -
6505 - if (empty($vector_ids)) {
6506 - return '';
6507 - }
6508 -
6509 - // Fetch all chunk content
6510 - $fetch_url = "https://{$host}/vectors/fetch";
6511 -
6512 - $fetch_body = array(
6513 - 'ids' => $vector_ids
6514 - );
6515 -
6516 - if (!empty($namespace)) {
6517 - $fetch_body['namespace'] = $namespace;
6518 - }
6519 -
6520 - $fetch_response = wp_remote_post($fetch_url, array(
6521 - 'headers' => array(
6522 - 'Api-Key' => $api_key,
6523 - 'accept' => 'application/json',
6524 - 'content-type' => 'application/json'
6525 - ),
6526 - 'body' => wp_json_encode($fetch_body),
6527 - 'timeout' => 30
6528 - ));
6529 -
6530 - if (is_wp_error($fetch_response)) {
6531 - //error_log('[MXCHAT-CHUNK] Fetch API error: ' . $fetch_response->get_error_message());
6532 - return '';
6533 - }
6534 -
6535 - $fetch_data = json_decode(wp_remote_retrieve_body($fetch_response), true);
6536 -
6537 - if (empty($fetch_data['vectors'])) {
6538 - return '';
6539 - }
6540 -
6541 - // Sort chunks by index and reassemble
6542 - $chunks = array();
6543 - foreach ($fetch_data['vectors'] as $id => $vector) {
6544 - $metadata = $vector['metadata'] ?? array();
6545 - $chunk_index = $metadata['chunk_index'] ?? 0;
6546 - $text = $metadata['text'] ?? '';
6547 -
6548 - // Store chunk with its index
6549 - $chunks[$chunk_index] = $text;
6550 - }
6551 -
6552 - // Sort by chunk index
6553 - ksort($chunks);
6554 -
6555 - // Apply chunk limit if specified
6556 - if ($max_chunks > 0 && count($chunks) > $max_chunks) {
6557 - $chunks = array_slice($chunks, 0, $max_chunks, true);
6558 - }
6559 -
6560 - // Store actual chunk count
6561 - $chunk_count = count($chunks);
6562 -
6563 - // Reassemble content
6564 - return implode("\n\n", $chunks);
6565 -}
6566 -
6567 -/**
6568 - * Search for relevant content using OpenAI Vector Store (File Search)
6569 - *
6570 - * @param string $user_query The user's query text
6571 - * @param string $bot_id The bot ID
6572 - * @param array $vectorstore_config Vector Store configuration
6573 - * @return string Formatted context string with references
6574 - */
6575 -private function find_relevant_content_openai_vectorstore($user_query, $bot_id = 'default', $vectorstore_config = array()) {
6576 - //error_log("MXCHAT DEBUG: find_relevant_content_openai_vectorstore called");
6577 - //error_log(" - bot_id: " . $bot_id);
6578 - //error_log(" - user_query length: " . strlen($user_query));
6579 -
6580 - // Get OpenAI API key
6581 - $mxchat_options = get_option('mxchat_options', array());
6582 - $api_key = $mxchat_options['api_key'] ?? '';
6583 -
6584 - // Reset vectorstore error tracking
6585 - $this->last_vectorstore_error = null;
6586 -
6587 - if (empty($api_key)) {
6588 - //error_log("MXCHAT DEBUG ERROR: OpenAI API key not configured");
6589 - $this->last_vectorstore_error = 'Vector Store search failed: OpenAI API key is not configured.';
6590 - $this->current_valid_urls = [];
6591 - return '';
6592 - }
6593 -
6594 - // Get Vector Store configuration
6595 - if (empty($vectorstore_config)) {
6596 - $vectorstore_config = $this->get_bot_vectorstore_config($bot_id);
6597 - }
6598 -
6599 - $vectorstore_ids_string = $vectorstore_config['vectorstore_ids'] ?? '';
6600 - $max_results = $vectorstore_config['max_results'] ?? 5;
6601 -
6602 - if (empty($vectorstore_ids_string)) {
6603 - //error_log("MXCHAT DEBUG ERROR: No Vector Store IDs configured");
6604 - $this->last_vectorstore_error = 'Vector Store search failed: No Vector Store IDs are configured for this bot.';
6605 - $this->current_valid_urls = [];
6606 - return '';
6607 - }
6608 -
6609 - // Parse Vector Store IDs
6610 - $vectorstore_ids = array_map('trim', explode(',', $vectorstore_ids_string));
6611 - $vectorstore_ids = array_filter($vectorstore_ids); // Remove empty values
6612 -
6613 - //error_log("MXCHAT DEBUG: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6614 - //error_log("MXCHAT DEBUG: Max results: " . $max_results);
6615 -
6616 - // Initialize similarity analysis storage
6617 - $this->last_similarity_analysis = [
6618 - 'knowledge_base_type' => 'OpenAI Vector Store',
6619 - 'bot_id' => $bot_id,
6620 - 'vectorstore_ids' => $vectorstore_ids,
6621 - 'top_matches' => [],
6622 - 'threshold_used' => 0,
6623 - 'total_checked' => 0
6624 - ];
6625 -
6626 - $valid_urls = [];
6627 -
6628 - // Get the selected model
6629 - $bot_options = $this->get_bot_options($bot_id);
6630 - $current_options = !empty($bot_options) ? $bot_options : $mxchat_options;
6631 - $selected_model = $current_options['model'] ?? 'gpt-5.1-chat-latest';
6632 -
6633 - // Verify it's an OpenAI model
6634 - if (!$this->is_openai_chat_model($selected_model)) {
6635 - //error_log("MXCHAT DEBUG ERROR: Vector Store search requires OpenAI model. Current: " . $selected_model);
6636 - $this->last_vectorstore_error = 'Vector Store search requires an OpenAI model. Current model: ' . $selected_model;
6637 - $this->current_valid_urls = [];
6638 - return '';
6639 - }
6640 -
6641 - // Use OpenAI Responses API with file_search tool
6642 - $request_body = array(
6643 - 'model' => $selected_model,
6644 - 'input' => $user_query,
6645 - 'tools' => array(
6646 - array(
6647 - 'type' => 'file_search',
6648 - 'vector_store_ids' => $vectorstore_ids,
6649 - 'max_num_results' => intval($max_results)
6650 - )
6651 - ),
6652 - 'include' => array('output[*].file_search_call.search_results')
6653 - );
6654 -
6655 - //error_log("MXCHAT VECTORSTORE: ========== REQUEST START ==========");
6656 - //error_log("MXCHAT VECTORSTORE: Model: " . $selected_model);
6657 - //error_log("MXCHAT VECTORSTORE: Query: " . substr($user_query, 0, 200));
6658 - //error_log("MXCHAT VECTORSTORE: Vector Store IDs: " . implode(', ', $vectorstore_ids));
6659 - //error_log("MXCHAT VECTORSTORE: Max Results: " . $max_results);
6660 - //error_log("MXCHAT VECTORSTORE: Request body: " . wp_json_encode($request_body));
6661 -
6662 - $response = wp_remote_post('https://api.openai.com/v1/responses', array(
6663 - 'headers' => array(
6664 - 'Authorization' => 'Bearer ' . $api_key,
6665 - 'Content-Type' => 'application/json'
6666 - ),
6667 - 'body' => wp_json_encode($request_body),
6668 - 'timeout' => 60
6669 - ));
6670 -
6671 - if (is_wp_error($response)) {
6672 - //error_log("MXCHAT VECTORSTORE ERROR: WP Error: " . $response->get_error_message());
6673 - $this->last_vectorstore_error = 'Vector Store API request failed: ' . $response->get_error_message();
6674 - $this->current_valid_urls = [];
6675 - return '';
6676 - }
6677 -
6678 - $response_code = wp_remote_retrieve_response_code($response);
6679 - //error_log("MXCHAT VECTORSTORE: Response code: " . $response_code);
6680 -
6681 - $response_body = wp_remote_retrieve_body($response);
6682 - //error_log("MXCHAT VECTORSTORE: Raw response (first 2000 chars): " . substr($response_body, 0, 2000));
6683 -
6684 - if ($response_code !== 200) {
6685 - //error_log("MXCHAT VECTORSTORE ERROR: API error response: " . $response_body);
6686 - $api_error_detail = '';
6687 - $decoded_error = json_decode($response_body, true);
6688 - if (isset($decoded_error['error']['message'])) {
6689 - $api_error_detail = $decoded_error['error']['message'];
6690 - }
6691 - $this->last_vectorstore_error = 'Vector Store API returned HTTP ' . $response_code . ($api_error_detail ? ': ' . $api_error_detail : '');
6692 - $this->current_valid_urls = [];
6693 - return '';
6694 - }
6695 - $result = json_decode($response_body, true);
6696 -
6697 - if (json_last_error() !== JSON_ERROR_NONE) {
6698 - //error_log("MXCHAT VECTORSTORE ERROR: JSON decode error: " . json_last_error_msg());
6699 - $this->last_vectorstore_error = 'Vector Store response could not be parsed: ' . json_last_error_msg();
6700 - $this->current_valid_urls = [];
6701 - return '';
6702 - }
6703 -
6704 - // Debug: Log the structure of the result
6705 - //error_log("MXCHAT VECTORSTORE: Result keys: " . implode(', ', array_keys($result)));
6706 - if (isset($result['output'])) {
6707 - //error_log("MXCHAT VECTORSTORE: Output count: " . count($result['output']));
6708 - foreach ($result['output'] as $idx => $out) {
6709 - //error_log("MXCHAT VECTORSTORE: Output[$idx] type: " . ($out['type'] ?? 'unknown'));
6710 - //error_log("MXCHAT VECTORSTORE: Output[$idx] keys: " . implode(', ', array_keys($out)));
6711 - }
6712 - } else {
6713 - //error_log("MXCHAT VECTORSTORE: No 'output' key in result!");
6714 - }
6715 -
6716 - // Extract file search results from the response
6717 - $content = '';
6718 - $matches_used = 0;
6719 - $all_matches = [];
6720 -
6721 - // The Responses API returns output array with tool results
6722 - if (isset($result['output']) && is_array($result['output'])) {
6723 - foreach ($result['output'] as $output_item) {
6724 - // Look for file_search_call results
6725 - if (isset($output_item['type']) && $output_item['type'] === 'file_search_call') {
6726 - //error_log("MXCHAT VECTORSTORE: Found file_search_call output item");
6727 - //error_log("MXCHAT VECTORSTORE: file_search_call keys: " . implode(', ', array_keys($output_item)));
6728 -
6729 - // Check for search_results in the output item directly
6730 - $search_results = $output_item['search_results'] ?? $output_item['results'] ?? [];
6731 - //error_log("MXCHAT VECTORSTORE: Search results count: " . count($search_results));
6732 -
6733 - if (empty($search_results)) {
6734 - //error_log("MXCHAT VECTORSTORE: No search results found in file_search_call");
6735 - //error_log("MXCHAT VECTORSTORE: file_search_call content: " . wp_json_encode($output_item));
6736 - }
6737 -
6738 - foreach ($search_results as $index => $search_result) {
6739 - $filename = $search_result['filename'] ?? '';
6740 - $score = $search_result['score'] ?? 0;
6741 - $text_content = '';
6742 -
6743 - // Extract text content from the result
6744 - // The text can be directly on the result OR nested under content array
6745 - if (isset($search_result['text']) && !empty($search_result['text'])) {
6746 - // Direct text field (OpenAI's actual format)
6747 - $text_content = $search_result['text'];
6748 - //error_log("MXCHAT VECTORSTORE: Found text directly on result[$index], length: " . strlen($text_content));
6749 - } elseif (isset($search_result['content']) && is_array($search_result['content'])) {
6750 - // Nested content array format
6751 - foreach ($search_result['content'] as $content_item) {
6752 - if (isset($content_item['text'])) {
6753 - $text_content .= $content_item['text'] . "\n";
6754 - }
6755 - }
6756 - //error_log("MXCHAT VECTORSTORE: Found text in content array for result[$index], length: " . strlen($text_content));
6757 - } else {
6758 - //error_log("MXCHAT VECTORSTORE: No text found for result[$index]. Keys: " . implode(', ', array_keys($search_result)));
6759 - }
6760 -
6761 - if (!empty($text_content)) {
6762 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6763 - $content .= trim($text_content) . "\n\n";
6764 -
6765 - if (!empty($filename)) {
6766 - $content .= "Source: " . $filename . "\n\n";
6767 - }
6768 -
6769 - // Extract URLs from content
6770 - preg_match_all(
6771 - '#\bhttps?://[^\s<>"\']+#i',
6772 - $text_content,
6773 - $content_urls
6774 - );
6775 - if (!empty($content_urls[0])) {
6776 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6777 - }
6778 -
6779 - $matches_used++;
6780 - }
6781 -
6782 - // Store for similarity analysis
6783 - $all_matches[] = [
6784 - 'document_id' => $filename ?: ('result_' . $index),
6785 - 'similarity' => $score,
6786 - 'similarity_percentage' => round($score * 100, 2),
6787 - 'above_threshold' => true,
6788 - 'source_display' => $filename,
6789 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6790 - 'used_for_context' => true,
6791 - 'role_restriction' => 'public',
6792 - 'has_access' => true,
6793 - 'filtered_out' => false
6794 - ];
6795 - }
6796 - }
6797 -
6798 - // Also check for message content with annotations (citations)
6799 - if (isset($output_item['type']) && $output_item['type'] === 'message') {
6800 - if (isset($output_item['content']) && is_array($output_item['content'])) {
6801 - foreach ($output_item['content'] as $content_block) {
6802 - if (isset($content_block['annotations']) && is_array($content_block['annotations'])) {
6803 - foreach ($content_block['annotations'] as $annotation) {
6804 - if (isset($annotation['filename'])) {
6805 - $filename = $annotation['filename'];
6806 - $score = $annotation['score'] ?? 0;
6807 - $text_content = '';
6808 -
6809 - if (isset($annotation['content']) && is_array($annotation['content'])) {
6810 - foreach ($annotation['content'] as $ann_content) {
6811 - if (isset($ann_content['text'])) {
6812 - $text_content .= $ann_content['text'] . "\n";
6813 - }
6814 - }
6815 - }
6816 -
6817 - if (!empty($text_content) && $matches_used < $max_results) {
6818 - $content .= "## Reference " . ($matches_used + 1) . " ##\n";
6819 - $content .= trim($text_content) . "\n\n";
6820 - $content .= "Source: " . $filename . "\n\n";
6821 -
6822 - preg_match_all(
6823 - '#\bhttps?://[^\s<>"\']+#i',
6824 - $text_content,
6825 - $content_urls
6826 - );
6827 - if (!empty($content_urls[0])) {
6828 - $valid_urls = array_merge($valid_urls, $content_urls[0]);
6829 - }
6830 -
6831 - $matches_used++;
6832 -
6833 - $all_matches[] = [
6834 - 'document_id' => $filename,
6835 - 'similarity' => $score,
6836 - 'similarity_percentage' => round($score * 100, 2),
6837 - 'above_threshold' => true,
6838 - 'source_display' => $filename,
6839 - 'content_preview' => substr(strip_tags($text_content), 0, 100) . '...',
6840 - 'used_for_context' => true,
6841 - 'role_restriction' => 'public',
6842 - 'has_access' => true,
6843 - 'filtered_out' => false
6844 - ];
6845 - }
6846 - }
6847 - }
6848 - }
6849 - }
6850 - }
6851 - }
6852 - }
6853 - }
6854 -
6855 - // Store for testing panel
6856 - $this->last_similarity_analysis['top_matches'] = $all_matches;
6857 - $this->last_similarity_analysis['total_checked'] = count($all_matches);
6858 -
6859 - // Store unique valid URLs for validation
6860 - $this->current_valid_urls = array_unique($valid_urls);
6861 -
6862 - // Allow add-ons to act on similarity results (e.g. WooCommerce product card display)
6863 - do_action('mxchat_similarity_results', $this->last_similarity_analysis['top_matches'], $bot_id);
6864 -
6865 - //error_log("MXCHAT VECTORSTORE: ========== SEARCH COMPLETE ==========");
6866 - //error_log("MXCHAT VECTORSTORE: Matches used: " . $matches_used);
6867 - //error_log("MXCHAT VECTORSTORE: All matches count: " . count($all_matches));
6868 - //error_log("MXCHAT VECTORSTORE: Content length: " . strlen($content));
6869 - if ($matches_used > 0) {
6870 - //error_log("MXCHAT VECTORSTORE: Content preview: " . substr($content, 0, 500));
6871 - }
6872 -
6873 - // Check if citation links are enabled
6874 - $citation_links_enabled = ($mxchat_options['citation_links_toggle'] ?? 'on') === 'on';
6875 -
6876 - // Add response guidelines
6877 - if ($matches_used === 0) {
6878 - //error_log("MXCHAT VECTORSTORE: No matches found - returning empty reference message");
6879 - $content = "No reference information was found for this query.\n\n";
6880 - } else {
6881 - // Build response guidelines based on citation links setting
6882 - $content .= "\n## Response Guidelines ##\n" .
6883 - "You are an AI Chatbot. Answer naturally and helpfully using only the information from the references above. " .
6884 - "Be conversational and friendly, but never mention your knowledge base or training data. " .
6885 - "If you don't have specific information or are uncertain about any details, it's always " .
6886 - "better to honestly say you don't know rather than making up or guessing at answers. " .
6887 - "When information is incomplete, let them know you are unsure.\n\n";
6888 -
6889 - // Only add hyperlink instructions if citation links are enabled
6890 - if ($citation_links_enabled) {
6891 - $content .= "CRITICAL: When creating hyperlinks, always use proper markdown format with descriptive text: " .
6892 - "[descriptive text](url). NEVER use empty brackets like [](url). The text in brackets must describe what the link is about.";
6893 - } else {
6894 - $content .= "IMPORTANT: Do not include any citation links, source URLs, or hyperlinks in your responses. " .
6895 - "Simply provide helpful answers based on the reference information without citing sources.";
6896 - }
6897 - }
6898 -
6899 - //error_log("MXCHAT DEBUG: Vector Store search complete. Matches used: " . $matches_used);
6900 -
6901 - return trim($content);
6902 -}
6903 -
6904 -/**
6905 - * Check if the given model is an OpenAI chat model
6906 - *
6907 - * @param string $model The model ID
6908 - * @return bool True if it's an OpenAI model
6909 - */
6910 -private function is_openai_chat_model($model) {
6911 - $openai_prefixes = array('gpt-', 'o1-', 'o3-');
6912 - foreach ($openai_prefixes as $prefix) {
6913 - if (strpos($model, $prefix) === 0) {
6914 - return true;
6915 - }
6916 - }
6917 - return false;
6918 -}
6919 -
6920 -/**
6921 - * Get bot-specific Vector Store configuration
6922 - *
6923 - * @param string $bot_id The bot ID
6924 - * @return array Configuration array
6925 - */
6926 -private function get_bot_vectorstore_config($bot_id = 'default') {
6927 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
6928 -
6929 - // Default global settings
6930 - $default_config = array(
6931 - 'use_vectorstore' => ($vectorstore_options['mxchat_use_openai_vectorstore'] ?? '0') === '1',
6932 - 'vectorstore_ids' => $vectorstore_options['mxchat_vectorstore_ids'] ?? '',
6933 - 'max_results' => $vectorstore_options['mxchat_vectorstore_max_results'] ?? 5
6934 - );
6935 -
6936 - // Allow multi-bot plugin to override with bot-specific settings
6937 - $bot_config = apply_filters('mxchat_get_bot_vectorstore_config', $default_config, $bot_id);
6938 -
6939 - // Preserve max_results from global settings if not set in bot config
6940 - if (!isset($bot_config['max_results'])) {
6941 - $bot_config['max_results'] = $default_config['max_results'];
6942 - }
6943 -
6944 - return $bot_config;
6945 -}
6946 -
6947 -private function mxchat_find_relevant_products($user_embedding) {
6948 - //error_log('MXChat Vector Search: Starting product search...');
6949 -
6950 - // Retrieve the add-on settings from the database
6951 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
6952 -
6953 - // Determine whether Pinecone is enabled
6954 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1') ? 1 : 0;
6955 -
6956 - //error_log('Pinecone enabled flag: ' . $use_pinecone);
6957 -
6958 - if ($use_pinecone === 1) {
6959 - //error_log('MXChat Vector Search: Using Pinecone database for products');
6960 - return $this->find_relevant_products_pinecone($user_embedding);
6961 - } else {
6962 - //error_log('MXChat Vector Search: Using WordPress database for products');
6963 - return $this->find_relevant_products_wordpress($user_embedding);
6964 - }
6965 -}
6966 -private function find_relevant_products_wordpress($user_embedding) {
6967 - global $wpdb;
6968 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
6969 -
6970 - if (!is_array($user_embedding)) {
6971 - return '';
6972 - }
6973 -
6974 - // Streaming top-K pass: scan rows in small batches, keep only the top 3
6975 - // results above the similarity threshold. Peak memory is bounded by
6976 - // $batch_size embedding rows plus a 3-element top list.
6977 - $batch_size = 250;
6978 - $similarity_threshold = 0.85;
6979 - $top_k = 3;
6980 - $top_results = [];
6981 - $offset = 0;
6982 -
6983 - do {
6984 - $batch = $wpdb->get_results($wpdb->prepare(
6985 - "SELECT id, embedding_vector
6986 - FROM {$system_prompt_table}
6987 - LIMIT %d OFFSET %d",
6988 - $batch_size,
6989 - $offset
6990 - ));
6991 -
6992 - if (empty($batch)) {
6993 - break;
6994 - }
6995 -
6996 - foreach ($batch as $row) {
6997 - $database_embedding = $row->embedding_vector
6998 - ? unserialize($row->embedding_vector, ['allowed_classes' => false])
6999 - : null;
7000 -
7001 - if (!is_array($database_embedding)) {
7002 - unset($database_embedding);
7003 - continue;
7004 - }
7005 -
7006 - $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
7007 - unset($database_embedding);
7008 -
7009 - if ($similarity < $similarity_threshold) {
7010 - continue;
7011 - }
7012 -
7013 - // Insert into bounded top-K (kept sorted descending)
7014 - if (count($top_results) < $top_k) {
7015 - $top_results[] = ['id' => $row->id, 'similarity' => $similarity];
7016 - usort($top_results, function ($a, $b) {
7017 - return $b['similarity'] <=> $a['similarity'];
7018 - });
7019 - } elseif ($similarity > $top_results[$top_k - 1]['similarity']) {
7020 - $top_results[$top_k - 1] = ['id' => $row->id, 'similarity' => $similarity];
7021 - usort($top_results, function ($a, $b) {
7022 - return $b['similarity'] <=> $a['similarity'];
7023 - });
7024 - }
7025 - }
7026 -
7027 - unset($batch);
7028 - $offset += $batch_size;
7029 - } while (true);
7030 -
7031 - if (empty($top_results)) {
7032 - return '';
7033 - }
7034 -
7035 - $content = '';
7036 - foreach ($top_results as $result) {
7037 - $chunk_content = $this->fetch_content_with_product_links($result['id']);
7038 - $content .= $chunk_content . "\n\n";
7039 - }
7040 -
7041 - return trim($content);
7042 -}
7043 -
7044 -
7045 -private function find_relevant_products_pinecone($user_embedding) {
7046 - //error_log('Starting Pinecone product search...');
7047 -
7048 - $options = get_option('mxchat_pinecone_addon_options', array());
7049 - $api_key = $options['mxchat_pinecone_api_key'] ?? '';
7050 - $host = $options['mxchat_pinecone_host'] ?? '';
7051 -
7052 - if (empty($host) || empty($api_key)) {
7053 - //error_log('Pinecone credentials not properly configured for product search');
7054 - return '';
7055 - }
7056 -
7057 - $similarity_threshold = 0.85;
7058 - $api_endpoint = "https://{$host}/query";
7059 -
7060 - $request_body = array(
7061 - 'vector' => $user_embedding,
7062 - 'topK' => 5,
7063 - 'includeMetadata' => true,
7064 - 'includeValues' => true,
7065 - 'filter' => array(
7066 - 'type' => 'product'
7067 - )
7068 - );
7069 -
7070 - //error_log('Sending request to Pinecone with body: ' . wp_json_encode($request_body));
7071 -
7072 - $response = wp_remote_post($api_endpoint, array(
7073 - 'headers' => array(
7074 - 'Api-Key' => $api_key,
7075 - 'accept' => 'application/json',
7076 - 'content-type' => 'application/json'
7077 - ),
7078 - 'body' => wp_json_encode($request_body),
7079 - 'timeout' => 30
7080 - ));
7081 -
7082 - if (is_wp_error($response)) {
7083 - //error_log('Pinecone product query error: ' . $response->get_error_message());
7084 - return '';
7085 - }
7086 -
7087 - $response_code = wp_remote_retrieve_response_code($response);
7088 - //error_log('Pinecone response code: ' . $response_code);
7089 -
7090 - if ($response_code !== 200) {
7091 - //error_log('Pinecone API error during product search: ' . wp_remote_retrieve_body($response));
7092 - return '';
7093 - }
7094 -
7095 - $results = json_decode(wp_remote_retrieve_body($response), true);
7096 - //error_log('Pinecone raw response: ' . wp_remote_retrieve_body($response));
7097 -
7098 - if (empty($results['matches'])) {
7099 - //error_log('No matches found in Pinecone response');
7100 - return '';
7101 - }
7102 -
7103 - $content = '';
7104 - foreach ($results['matches'] as $match) {
7105 - if ($match['score'] < $similarity_threshold) {
7106 - //error_log("Match below threshold: " . $match['score']);
7107 - continue;
7108 - }
7109 -
7110 - if (!empty($match['metadata']['text'])) {
7111 - $content .= $match['metadata']['text'];
7112 - if (!empty($match['metadata']['source_url'])) {
7113 - $content .= "\n\nFor more details, check out this product: " . esc_url($match['metadata']['source_url']);
7114 - }
7115 - $content .= "\n\n";
7116 - }
7117 - }
7118 -
7119 - return trim($content);
7120 -}
7121 -
7122 -
7123 -private function fetch_content_with_product_links($most_relevant_id) {
7124 - global $wpdb;
7125 - $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
7126 -
7127 - // Fetch the article content and associated product URL
7128 - $query = $wpdb->prepare("SELECT article_content, source_url FROM {$system_prompt_table} WHERE id = %d", $most_relevant_id);
7129 - $result = $wpdb->get_row($query);
7130 -
7131 - if ($result) {
7132 - // Append the product link to the content if available
7133 - $content = $result->article_content;
7134 - if (!empty($result->source_url)) {
7135 - $content .= "\n\nFor more details, check out this product: " . esc_url($result->source_url);
7136 - }
7137 - return $content;
7138 - }
7139 -
7140 - return null;
7141 -}
7142 -
7143 -/**
7144 - * Get system instructions for a specific bot or default
7145 - * Checks for multi-bot add-on and uses bot-specific instructions if available
7146 - * Automatically strips URLs if citation links are disabled
7147 - * Replaces {visitor_name} placeholder with actual visitor name if available
7148 - *
7149 - * @param string $bot_id The bot ID to get instructions for
7150 - * @param string $session_id Optional session ID to lookup visitor name
7151 - */
7152 -private function get_system_instructions($bot_id = 'default', $session_id = '') {
7153 - $instructions = '';
7154 -
7155 - // Check if multi-bot add-on is active
7156 - if (class_exists('MxChat_Multi_Bot_Core_Manager') && $bot_id !== 'default') {
7157 - // Get bot-specific options from multi-bot add-on
7158 - $bot_options = apply_filters('mxchat_get_bot_options', array(), $bot_id);
7159 -
7160 - // If bot has custom system instructions, use those
7161 - if (!empty($bot_options['system_prompt_instructions'])) {
7162 - $instructions = $bot_options['system_prompt_instructions'];
7163 - }
7164 - }
7165 -
7166 - // Fall back to default system instructions
7167 - if (empty($instructions)) {
7168 - $instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
7169 - }
7170 -
7171 - // Check if citation links are disabled - if so, strip URLs from instructions
7172 - $fresh_options = get_option('mxchat_options', []);
7173 - $citation_links_enabled = isset($fresh_options['citation_links_toggle']) ? ($fresh_options['citation_links_toggle'] === 'on') : true;
7174 -
7175 - if (!$citation_links_enabled && !empty($instructions)) {
7176 - $instructions = preg_replace('#\bhttps?://[^\s<>"\']+#i', '', $instructions);
7177 - $instructions = preg_replace('/\s+/', ' ', trim($instructions)); // Clean up extra spaces
7178 - }
7179 -
7180 - // Replace {visitor_name} placeholder with actual visitor name if available
7181 - if (!empty($instructions) && !empty($session_id) && stripos($instructions, '{visitor_name}') !== false) {
7182 - $name_option_key = "mxchat_name_{$session_id}";
7183 - $visitor_name = get_option($name_option_key, '');
7184 -
7185 - if (!empty($visitor_name)) {
7186 - $instructions = str_ireplace('{visitor_name}', sanitize_text_field($visitor_name), $instructions);
7187 - } else {
7188 - // Remove placeholder if no name is available
7189 - $instructions = str_ireplace('{visitor_name}', '', $instructions);
7190 - $instructions = preg_replace('/\s{2,}/', ' ', trim($instructions)); // Clean up extra spaces
7191 - }
7192 - }
7193 -
7194 - // Allow developers to filter system instructions and process shortcodes
7195 - $instructions = apply_filters('mxchat_system_instructions', $instructions, $bot_id, $session_id);
7196 - $instructions = do_shortcode($instructions);
7197 -
7198 - return $instructions;
7199 -}
7200 -/**
7201 - * Get the current bot ID from session or request context
7202 - */
7203 -private function get_current_bot_id($session_id = '') {
7204 - // First, check if bot_id is passed in the current request
7205 - if (isset($_POST['bot_id']) && !empty($_POST['bot_id'])) {
7206 - return sanitize_key($_POST['bot_id']);
7207 - }
7208 -
7209 - // If not in POST, try to get it from session data
7210 - if (!empty($session_id)) {
7211 - $bot_id = get_option("mxchat_session_bot_{$session_id}", '');
7212 - if (!empty($bot_id)) {
7213 - return $bot_id;
7214 - }
7215 - }
7216 -
7217 - // Fall back to default
7218 - return 'default';
7219 -}
7220 -/* ====================================================================== *
7221 - * Native function-calling loop (plan-mxchat-20260617-a41dee)
7222 - *
7223 - * Model-driven tool use. The model is offered MxChat's enabled callbacks as
7224 - * tools (sourced from MxChat_Tool_Registry, the single source the admin AI
7225 - * Tools checklist also reads). When the model calls a tool, the matching
7226 - * callback runs through its EXISTING permission checks, its output is fed
7227 - * back, and the loop continues up to a depth cap. INDEPENDENT of the
7228 - * intent→callback router — it runs only after intents miss, and works with
7229 - * ZERO Actions created.
7230 - *
7231 - * Entered ONLY when: function calling is enabled + the active model is
7232 - * tool-capable + at least one tool is enabled. Default-off, so existing
7233 - * installs never enter this branch (byte-for-byte unchanged behavior). The
7234 - * tool round is buffered (non-streaming) per the plan; the final answer is
7235 - * emitted via the same SSE/JSON envelopes the normal path uses.
7236 - * ====================================================================== */
7237 -
7238 -/** Gate: should the function-calling loop handle this turn? */
7239 -private function mxchat_fc_should_run($selected_model) {
7240 - if (!class_exists('MxChat_Tool_Registry') || !MxChat_Tool_Registry::is_enabled()) {
7241 - return false;
7242 - }
7243 - if (class_exists('MxChat_Model_Catalog') && !MxChat_Model_Catalog::supports_tools($selected_model)) {
7244 - return false;
7245 - }
7246 - $tools = MxChat_Tool_Registry::enabled_tools();
7247 - return !empty($tools);
7248 -}
7249 -
7250 -private function mxchat_fc_log($msg) {
7251 - if (defined('MXCHAT_DEV_MODE') && MXCHAT_DEV_MODE) {
7252 - error_log('[MxChat FC] ' . $msg);
7253 - }
7254 -}
7255 -
7256 -/**
7257 - * Resolve provider transport details. Returns null when FC can't run for this
7258 - * model/config (missing key, unsupported provider) so the caller falls back to
7259 - * the normal path. OpenAI/xAI/DeepSeek/OpenRouter/Custom share the
7260 - * OpenAI-compatible 'openai' family; Claude and Gemini are distinct.
7261 - */
7262 -private function mxchat_fc_resolve_provider($selected_model, $opts) {
7263 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
7264 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
7265 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
7266 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
7267 - if ($selected_model === 'openrouter') {
7268 - $model = isset($opts['openrouter_selected_model']) ? $opts['openrouter_selected_model'] : '';
7269 - $key = isset($opts['openrouter_api_key']) ? $opts['openrouter_api_key'] : '';
7270 - if ($model === '' || $key === '') return null;
7271 - return array('family'=>'openai','model'=>$model,'url'=>'https://openrouter.ai/api/v1/chat/completions',
7272 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7273 - }
7274 - $prefix = strtolower(explode('-', $selected_model)[0]);
7275 - switch ($prefix) {
7276 - case 'gpt': case 'o1': case 'o3': case 'o4':
7277 - $key = isset($opts['api_key']) ? $opts['api_key'] : '';
7278 - if ($key === '') return null;
7279 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.openai.com/v1/chat/completions',
7280 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7281 - case 'claude':
7282 - $key = isset($opts['claude_api_key']) ? $opts['claude_api_key'] : '';
7283 - if ($key === '') return null;
7284 - return array('family'=>'anthropic','model'=>$selected_model,'url'=>'https://api.anthropic.com/v1/messages',
7285 - 'headers'=>array('Content-Type'=>'application/json','x-api-key'=>$key,'anthropic-version'=>'2023-06-01'),'tag'=>'anthropic');
7286 - case 'gemini':
7287 - $key = isset($opts['gemini_api_key']) ? $opts['gemini_api_key'] : '';
7288 - if ($key === '') return null;
7289 - return array('family'=>'gemini','model'=>$selected_model,'key'=>$key,'tag'=>'gemini');
7290 - case 'grok': case 'xai':
7291 - $key = isset($opts['xai_api_key']) ? $opts['xai_api_key'] : '';
7292 - if ($key === '') return null;
7293 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.x.ai/v1/chat/completions',
7294 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'xai');
7295 - case 'deepseek':
7296 - $key = isset($opts['deepseek_api_key']) ? $opts['deepseek_api_key'] : '';
7297 - if ($key === '') return null;
7298 - return array('family'=>'openai','model'=>$selected_model,'url'=>'https://api.deepseek.com/v1/chat/completions',
7299 - 'headers'=>array('Content-Type'=>'application/json','Authorization'=>'Bearer '.$key),'tag'=>'openai');
7300 - case 'custom':
7301 - $base = isset($opts['custom_provider_base_url']) ? rtrim($opts['custom_provider_base_url'], '/') : '';
7302 - $key = isset($opts['custom_provider_api_key']) ? $opts['custom_provider_api_key'] : '';
7303 - $model = isset($opts['custom_provider_model']) ? $opts['custom_provider_model'] : '';
7304 - if ($base === '' || $model === '') return null;
7305 - $url = (strpos($base, 'chat/completions') !== false) ? $base : $base . '/chat/completions';
7306 - $headers = array('Content-Type'=>'application/json');
7307 - if ($key !== '') $headers['Authorization'] = 'Bearer '.$key;
7308 - return array('family'=>'openai','model'=>$model,'url'=>$url,'headers'=>$headers,'tag'=>'openai');
7309 - }
7310 - return null;
7311 -}
7312 -
7313 -/**
7314 - * Top-level function-calling attempt. Returns:
7315 - * ['handled'=>true, 'text'=>'<final answer>'] when the model used ≥1 tool
7316 - * ['handled'=>false] otherwise (caller falls back
7317 - * to the normal streamed path)
7318 - */
7319 -private function mxchat_fc_attempt($message, $relevant_content, $conversation_history, $selected_model, $opts, $session_id, $user_id) {
7320 - $prov = $this->mxchat_fc_resolve_provider($selected_model, $opts);
7321 - if (!$prov) {
7322 - return array('handled' => false);
7323 - }
7324 - $tools = MxChat_Tool_Registry::enabled_tools();
7325 - if (empty($tools)) {
7326 - return array('handled' => false);
7327 - }
7328 -
7329 - $bot_id = $this->get_current_bot_id($session_id);
7330 - $system = $this->get_system_instructions($bot_id, $session_id);
7331 -
7332 - // Force callbacks into return-mode (some echo SSE directly when streaming);
7333 - // we buffer the whole tool round, then emit once. Restored in finally.
7334 - $prev_streaming = $this->is_streaming;
7335 - $this->is_streaming = false;
7336 - try {
7337 - if ($prov['family'] === 'anthropic') {
7338 - return $this->mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7339 - } elseif ($prov['family'] === 'gemini') {
7340 - return $this->mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7341 - }
7342 - return $this->mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $message, $user_id, $session_id);
7343 - } catch (\Throwable $e) {
7344 - $this->mxchat_fc_log('attempt threw: ' . $e->getMessage());
7345 - return array('handled' => false);
7346 - } finally {
7347 - $this->is_streaming = $prev_streaming;
7348 - }
7349 -}
7350 -
7351 -/** Normalize MxChat history rows to [{role:user|assistant, content}]. */
7352 -private function mxchat_fc_normalize_history($conversation_history) {
7353 - $out = array();
7354 - if (!is_array($conversation_history)) return $out;
7355 - foreach ($conversation_history as $m) {
7356 - if (!is_array($m) || !isset($m['role']) || !isset($m['content'])) continue;
7357 - $role = $m['role'];
7358 - if ($role === 'bot' || $role === 'agent') $role = 'assistant';
7359 - if (!in_array($role, array('user', 'assistant'), true)) $role = 'user';
7360 - $out[] = array('role' => $role, 'content' => (string) $m['content']);
7361 - }
7362 - return $out;
7363 -}
7364 -
7365 -/** Execute the matched callback for a tool call. Returns ['ok'=>bool,'content'=>string]. */
7366 -private function mxchat_fc_execute_tool($tool_name, $args, $orig_message, $user_id, $session_id) {
7367 - $tool = MxChat_Tool_Registry::tool_by_name($tool_name, true); // enabled-only
7368 - if (!$tool) {
7369 - return array('ok' => false, 'content' => 'This tool is not available or not enabled.');
7370 - }
7371 - $fn = $tool['callback'];
7372 -
7373 - // MxChat callbacks are message-driven: hand them the model's `query`
7374 - // (falling back to the original user message).
7375 - $query = '';
7376 - if (is_array($args) && isset($args['query']) && is_string($args['query'])) {
7377 - $query = $args['query'];
7378 - }
7379 - if ($query === '') $query = $orig_message;
7380 -
7381 - // Synthetic intent row (matches wp_mxchat_intents columns → no undefined-prop warnings).
7382 - $synthetic_intent = (object) array(
7383 - 'id' => 0, 'intent_label' => $tool['label'], 'phrases' => '',
7384 - 'embedding_vector' => '', 'callback_function' => $fn,
7385 - 'similarity_threshold' => 0.0, 'enabled' => 1, 'enabled_bots' => null,
7386 - );
7387 -
7388 - try {
7389 - if (!empty($tool['is_addon'])) {
7390 - $result = apply_filters($fn, false, $query, $user_id, $session_id, $synthetic_intent);
7391 - } elseif (method_exists($this, $fn)) {
7392 - $result = call_user_func(array($this, $fn), $query, $user_id, $session_id, $synthetic_intent, null);
7393 - } else {
7394 - return array('ok' => false, 'content' => 'Tool implementation not found.');
7395 - }
7396 - } catch (\Throwable $e) {
7397 - $this->mxchat_fc_log("tool {$fn} threw: " . $e->getMessage());
7398 - return array('ok' => false, 'content' => 'The tool failed to run.');
7399 - }
7400 -
7401 - // plan-mxchat-20260617-48a57a — surface UI-bearing tool output.
7402 - // If the callback produced a UI element (generated image, product card, image
7403 - // gallery), its html MUST reach the FRONTEND as a real rendered bot message —
7404 - // NOT be stripped to text and handed to the model to paraphrase (that was the
7405 - // bug: under function calling, UI-bearing actions rendered nothing). Capture
7406 - // the html here; the FC outcome handler emits it in the response envelope.
7407 - $ui = $this->mxchat_fc_ui_payload_from($result);
7408 - if ($ui['html'] !== '' || !empty($ui['images'])) {
7409 - if ($ui['html'] !== '') {
7410 - $this->fc_ui_html .= ($this->fc_ui_html !== '' ? "\n" : '') . $ui['html'];
7411 - }
7412 - if (!empty($ui['images']) && is_array($ui['images'])) {
7413 - $this->fc_ui_images = array_merge($this->fc_ui_images, $ui['images']);
7414 - }
7415 - $this->fc_ui_captured = true;
7416 -
7417 - // Persist the html to the transcript ONLY if the callback did not already
7418 - // do so itself. Core image/search callbacks self-save (text + html);
7419 - // add-on callbacks (e.g. woo product cards) return html for the caller to
7420 - // save. ui_self_saves carries this from the registry; default by source
7421 - // (core self-saves, add-on does not) when a tool predates the flag.
7422 - $self_saves = array_key_exists('ui_self_saves', $tool)
7423 - ? !empty($tool['ui_self_saves'])
7424 - : empty($tool['is_addon']);
7425 - if ($ui['html'] !== '' && !$self_saves) {
7426 - $this->mxchat_save_chat_message($session_id, 'bot', $ui['html']);
7427 - }
7428 -
7429 - // Hand the MODEL a short acknowledgment (never the raw or stripped html)
7430 - // so the loop can add a one-line caption without trying to re-describe a
7431 - // visual it cannot see and without duplicating the displayed element.
7432 - $summary = isset($ui['text']) ? trim((string) $ui['text']) : '';
7433 - $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');
7434 - $content = $summary !== '' ? ($ack . ' ' . $summary) : $ack;
7435 - $this->mxchat_fc_log("executed {$fn} → [ui payload surfaced] " . substr($content, 0, 120));
7436 - return array('ok' => true, 'content' => $content);
7437 - }
7438 -
7439 - $content = $this->mxchat_fc_stringify_result($result);
7440 - $this->mxchat_fc_log("executed {$fn} → " . substr($content, 0, 160));
7441 - return array('ok' => true, 'content' => $content);
7442 -}
7443 -
7444 -/**
7445 - * Extract a UI payload (html + images + text) from a tool callback's return,
7446 - * falling back to $this->fallbackResponse for callbacks that return true after
7447 - * setting it. plan-mxchat-20260617-48a57a.
7448 - *
7449 - * @return array{html:string,images:array,text:string}
7450 - */
7451 -private function mxchat_fc_ui_payload_from($result) {
7452 - $src = null;
7453 - if (is_array($result)) {
7454 - $src = $result;
7455 - } elseif ($result === true && isset($this->fallbackResponse) && is_array($this->fallbackResponse)) {
7456 - $src = $this->fallbackResponse;
7457 - }
7458 - $html = (is_array($src) && isset($src['html']) && is_string($src['html'])) ? $src['html'] : '';
7459 - $images = (is_array($src) && isset($src['images']) && is_array($src['images'])) ? $src['images'] : array();
7460 - $text = (is_array($src) && isset($src['text'])) ? (string) $src['text'] : '';
7461 - return array('html' => $html, 'images' => $images, 'text' => $text);
7462 -}
7463 -
7464 -/** Coerce a callback's return (string|array|true|false) into a tool-result string. */
7465 -private function mxchat_fc_stringify_result($result) {
7466 - if (is_string($result)) {
7467 - return $result === '' ? 'No result.' : $result;
7468 - }
7469 - if ($result === true) {
7470 - // Callbacks that set fallbackResponse and return true.
7471 - $fb = isset($this->fallbackResponse) ? $this->fallbackResponse : null;
7472 - if (is_array($fb)) {
7473 - if (!empty($fb['text'])) return (string) $fb['text'];
7474 - if (!empty($fb['html'])) return wp_strip_all_tags((string) $fb['html']);
7475 - }
7476 - return 'Done.';
7477 - }
7478 - if ($result === false || $result === null) {
7479 - return 'No result.';
7480 - }
7481 - if (is_array($result)) {
7482 - if (isset($result['text']) && $result['text'] !== '') return (string) $result['text'];
7483 - if (isset($result['html']) && $result['html'] !== '') return wp_strip_all_tags((string) $result['html']);
7484 - $json = wp_json_encode($result);
7485 - return $json !== false ? $json : 'No result.';
7486 - }
7487 - return (string) $result;
7488 -}
7489 -
7490 -/** HTTP code + decoded body for a function-calling request. */
7491 -private function mxchat_fc_post($url, $body, $headers, $tag) {
7492 - $args = array(
7493 - 'body' => wp_json_encode($body),
7494 - 'headers' => $headers,
7495 - 'timeout' => 60,
7496 - 'redirection' => 5,
7497 - 'blocking' => true,
7498 - 'httpversion' => '1.0',
7499 - 'sslverify' => true,
7500 - );
7501 - $response = $this->mxchat_provider_call_with_retry($url, $args, $tag);
7502 - if (is_wp_error($response)) {
7503 - return array('code' => 0, 'data' => null, 'error' => $response->get_error_message());
7504 - }
7505 - $code = (int) wp_remote_retrieve_response_code($response);
7506 - $data = json_decode(wp_remote_retrieve_body($response), true);
7507 - return array('code' => $code, 'data' => $data, 'error' => null);
7508 -}
7509 -
7510 -/* ---------------- OpenAI-compatible loop (OpenAI/xAI/DeepSeek/OpenRouter/Custom) -------------- */
7511 -private function mxchat_fc_loop_openai($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7512 - $messages = array();
7513 - $messages[] = array('role' => 'system', 'content' => $system . ' ' . $relevant_content);
7514 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7515 - $messages[] = $m;
7516 - }
7517 -
7518 - $depth = MxChat_Tool_Registry::max_depth();
7519 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7520 - $tool_schema = MxChat_Tool_Registry::to_openai_tools($tools);
7521 - $used_tool = false;
7522 - $calls_made = 0;
7523 -
7524 - for ($step = 0; $step <= $depth; $step++) {
7525 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7526 - $body = array('model' => $prov['model'], 'messages' => $messages, 'temperature' => 1, 'stream' => false);
7527 - if ($offer_tools) {
7528 - $body['tools'] = $tool_schema;
7529 - $body['tool_choice'] = 'auto';
7530 - }
7531 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7532 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7533 - $this->mxchat_fc_log('openai call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7534 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7535 - }
7536 - $msg = isset($r['data']['choices'][0]['message']) ? $r['data']['choices'][0]['message'] : null;
7537 - if (!$msg) {
7538 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7539 - }
7540 - $tool_calls = isset($msg['tool_calls']) && is_array($msg['tool_calls']) ? $msg['tool_calls'] : array();
7541 - if (empty($tool_calls)) {
7542 - $text = isset($msg['content']) ? trim((string) $msg['content']) : '';
7543 - if (!$used_tool) return array('handled' => false); // model never used a tool → normal path
7544 - return array('handled' => true, 'text' => ($text !== '' ? $text : $this->mxchat_fc_giveup_text()));
7545 - }
7546 - // Append the assistant tool-call turn verbatim, then a tool result per call.
7547 - $used_tool = true;
7548 - $messages[] = $msg;
7549 - foreach ($tool_calls as $tc) {
7550 - if ($calls_made >= $budget) break;
7551 - $calls_made++;
7552 - $name = isset($tc['function']['name']) ? $tc['function']['name'] : '';
7553 - $args = array();
7554 - if (isset($tc['function']['arguments'])) {
7555 - $decoded = json_decode($tc['function']['arguments'], true);
7556 - if (is_array($decoded)) $args = $decoded;
7557 - }
7558 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7559 - $messages[] = array(
7560 - 'role' => 'tool',
7561 - 'tool_call_id' => isset($tc['id']) ? $tc['id'] : '',
7562 - 'content' => $exec['content'],
7563 - );
7564 - }
7565 - }
7566 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7567 -}
7568 -
7569 -/* ---------------- Anthropic Claude loop ---------------- */
7570 -private function mxchat_fc_loop_anthropic($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7571 - $messages = $this->mxchat_fc_normalize_history($conversation_history);
7572 - $messages[] = array('role' => 'user', 'content' => $relevant_content);
7573 -
7574 - $depth = MxChat_Tool_Registry::max_depth();
7575 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7576 - $tool_schema = MxChat_Tool_Registry::to_anthropic_tools($tools);
7577 - $omit_temp = $this->mxchat_claude_omits_temperature($prov['model']);
7578 - $used_tool = false;
7579 - $calls_made = 0;
7580 -
7581 - for ($step = 0; $step <= $depth; $step++) {
7582 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7583 - $body = array('model' => $prov['model'], 'max_tokens' => 1024, 'temperature' => 0.8,
7584 - 'messages' => $messages, 'system' => $system);
7585 - if ($omit_temp) unset($body['temperature']);
7586 - if ($offer_tools) {
7587 - $body['tools'] = $tool_schema;
7588 - $body['tool_choice'] = array('type' => 'auto');
7589 - }
7590 - $r = $this->mxchat_fc_post($prov['url'], $body, $prov['headers'], $prov['tag']);
7591 - if ($r['code'] !== 200 || !is_array($r['data'])) {
7592 - $this->mxchat_fc_log('anthropic call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7593 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7594 - }
7595 - $content = isset($r['data']['content']) && is_array($r['data']['content']) ? $r['data']['content'] : array();
7596 - $tool_uses = array();
7597 - $text_out = '';
7598 - foreach ($content as $block) {
7599 - if (!isset($block['type'])) continue;
7600 - if ($block['type'] === 'tool_use') {
7601 - $tool_uses[] = $block;
7602 - } elseif ($block['type'] === 'text' && isset($block['text'])) {
7603 - $text_out .= $block['text'];
7604 - }
7605 - }
7606 - if (empty($tool_uses)) {
7607 - if (!$used_tool) return array('handled' => false);
7608 - $text_out = trim($text_out);
7609 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7610 - }
7611 - // Append the assistant turn (the full content array), then a user turn of tool_result blocks.
7612 - $used_tool = true;
7613 - $messages[] = array('role' => 'assistant', 'content' => $content);
7614 - $results = array();
7615 - foreach ($tool_uses as $tu) {
7616 - if ($calls_made >= $budget) break;
7617 - $calls_made++;
7618 - $name = isset($tu['name']) ? $tu['name'] : '';
7619 - $args = isset($tu['input']) && is_array($tu['input']) ? $tu['input'] : array();
7620 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7621 - $results[] = array(
7622 - 'type' => 'tool_result',
7623 - 'tool_use_id' => isset($tu['id']) ? $tu['id'] : '',
7624 - 'content' => $exec['content'],
7625 - );
7626 - }
7627 - $messages[] = array('role' => 'user', 'content' => $results);
7628 - }
7629 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7630 -}
7631 -
7632 -/* ---------------- Google Gemini loop ---------------- */
7633 -private function mxchat_fc_loop_gemini($prov, $system, $relevant_content, $conversation_history, $tools, $orig_message, $user_id, $session_id) {
7634 - $contents = array();
7635 - $contents[] = array('role' => 'user', 'parts' => array(array('text' => '[System Instructions] ' . $system . ' ' . $relevant_content)));
7636 - $contents[] = array('role' => 'model', 'parts' => array(array('text' => 'I understand and will follow these instructions.')));
7637 - foreach ($this->mxchat_fc_normalize_history($conversation_history) as $m) {
7638 - $contents[] = array('role' => ($m['role'] === 'assistant' ? 'model' : 'user'),
7639 - 'parts' => array(array('text' => $m['content'])));
7640 - }
7641 -
7642 - $depth = MxChat_Tool_Registry::max_depth();
7643 - $budget = MxChat_Tool_Registry::max_tool_calls_per_turn();
7644 - $tool_schema = MxChat_Tool_Registry::to_gemini_tools($tools);
7645 - // Function calling (tools + functionDeclarations + toolConfig) is a v1beta feature on the
7646 - // Generative Language REST API. The v1 endpoint silently ignores the tools array, so a
7647 - // non-preview model (e.g. gemini-2.5-pro, gemini-3.5-flash, gemini-3.1-flash-lite) would
7648 - // just answer in text and never emit a tool call. Always use v1beta for the FC loop —
7649 - // confirmed against Google's function-calling docs (their REST example targets
7650 - // v1beta/models/gemini-3.5-flash:generateContent). v1beta is a superset, so every model
7651 - // reachable on v1 is also reachable here.
7652 - $api_version = 'v1beta';
7653 - $url = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $prov['model'] . ':generateContent?key=' . $prov['key'];
7654 - $headers = array('Content-Type' => 'application/json');
7655 - $used_tool = false;
7656 - $calls_made = 0;
7657 -
7658 - for ($step = 0; $step <= $depth; $step++) {
7659 - $offer_tools = ($step < $depth) && !empty($tool_schema);
7660 - $body = array(
7661 - 'contents' => $contents,
7662 - 'generationConfig' => array('temperature' => 0.7, 'topP' => 0.95, 'topK' => 40, 'maxOutputTokens' => 8192),
7663 - );
7664 - if ($offer_tools) {
7665 - $body['tools'] = $tool_schema;
7666 - $body['toolConfig'] = array('functionCallingConfig' => array('mode' => 'AUTO'));
7667 - }
7668 - $r = $this->mxchat_fc_post($url, $body, $headers, 'gemini');
7669 - if ($r['code'] !== 200 || !is_array($r['data']) || isset($r['data']['error'])) {
7670 - $this->mxchat_fc_log('gemini call failed: code=' . $r['code'] . ' err=' . ($r['error'] ?? ''));
7671 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7672 - }
7673 - $parts = isset($r['data']['candidates'][0]['content']['parts']) && is_array($r['data']['candidates'][0]['content']['parts'])
7674 - ? $r['data']['candidates'][0]['content']['parts'] : array();
7675 - $fn_calls = array();
7676 - $text_out = '';
7677 - foreach ($parts as $p) {
7678 - if (isset($p['functionCall'])) {
7679 - $fn_calls[] = $p['functionCall'];
7680 - } elseif (isset($p['text'])) {
7681 - $text_out .= $p['text'];
7682 - }
7683 - }
7684 - if (empty($fn_calls)) {
7685 - if (!$used_tool) return array('handled' => false);
7686 - $text_out = trim($text_out);
7687 - return array('handled' => true, 'text' => ($text_out !== '' ? $text_out : $this->mxchat_fc_giveup_text()));
7688 - }
7689 - // Append the model turn (its parts) then a user turn of functionResponse parts.
7690 - $used_tool = true;
7691 - $contents[] = array('role' => 'model', 'parts' => $parts);
7692 - $resp_parts = array();
7693 - foreach ($fn_calls as $fcall) {
7694 - if ($calls_made >= $budget) break;
7695 - $calls_made++;
7696 - $name = isset($fcall['name']) ? $fcall['name'] : '';
7697 - $args = isset($fcall['args']) && is_array($fcall['args']) ? $fcall['args'] : array();
7698 - $exec = $this->mxchat_fc_execute_tool($name, $args, $orig_message, $user_id, $session_id);
7699 - $fr = array('name' => $name, 'response' => array('result' => $exec['content']));
7700 - // Gemini 3 function calls carry a unique id; echo the matching id back in the
7701 - // functionResponse so the model maps the result to the right call (Google REST
7702 - // guidance). Older models omit the id — then we send none, exactly as before.
7703 - if (isset($fcall['id']) && $fcall['id'] !== '') { $fr['id'] = $fcall['id']; }
7704 - $resp_parts[] = array('functionResponse' => $fr);
7705 - }
7706 - $contents[] = array('role' => 'user', 'parts' => $resp_parts);
7707 - }
7708 - return $used_tool ? array('handled' => true, 'text' => $this->mxchat_fc_giveup_text()) : array('handled' => false);
7709 -}
7710 -
7711 -private function mxchat_fc_giveup_text() {
7712 - return esc_html__('I looked into that but could not put together a final answer. Please try rephrasing your request.', 'mxchat');
7713 -}
7714 -
7715 -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') {
7716 - try {
7717 - if (!$relevant_content) {
7718 - $error_response = [
7719 - 'error' => esc_html__("I couldn't find relevant information on that topic.", 'mxchat'),
7720 - 'error_code' => 'no_relevant_content'
7721 - ];
7722 -
7723 - if ($testing_data !== null) {
7724 - $error_response['testing_data'] = $testing_data;
7725 - }
7726 -
7727 - return $error_response;
7728 - }
7729 -
7730 - if (!is_array($conversation_history)) {
7731 - $conversation_history = array();
7732 - }
7733 -
7734 - // Check if this is an OpenRouter model
7735 - if ($selected_model === 'openrouter') {
7736 - // Get the actual OpenRouter model from options
7737 - $openrouter_selected_model = $this->options['openrouter_selected_model'] ?? '';
7738 -
7739 - if (empty($openrouter_selected_model)) {
7740 - $error_response = [
7741 - 'error' => esc_html__('No OpenRouter model selected. Please select a model in settings.', 'mxchat'),
7742 - 'error_code' => 'no_openrouter_model_selected'
7743 - ];
7744 - if ($testing_data !== null) {
7745 - $error_response['testing_data'] = $testing_data;
7746 - }
7747 - return $error_response;
7748 - }
7749 -
7750 - if (empty($openrouter_api_key)) {
7751 - $error_response = [
7752 - 'error' => esc_html__('OpenRouter API key is not configured', 'mxchat'),
7753 - 'error_code' => 'missing_openrouter_api_key'
7754 - ];
7755 - if ($testing_data !== null) {
7756 - $error_response['testing_data'] = $testing_data;
7757 - }
7758 - return $error_response;
7759 - }
7760 -
7761 - if ($streaming) {
7762 - return $this->mxchat_generate_response_openrouter_stream(
7763 - $openrouter_selected_model,
7764 - $openrouter_api_key,
7765 - $conversation_history,
7766 - $relevant_content,
7767 - $session_id,
7768 - $testing_data
7769 - );
7770 - } else {
7771 - $response = $this->mxchat_generate_response_openrouter(
7772 - $openrouter_selected_model,
7773 - $openrouter_api_key,
7774 - $conversation_history,
7775 - $relevant_content,
7776 - $session_id
7777 - );
7778 - }
7779 -
7780 - if (is_array($response) && isset($response['error'])) {
7781 - if ($testing_data !== null) {
7782 - $response['testing_data'] = $testing_data;
7783 - }
7784 - return $response;
7785 - }
7786 -
7787 - return $response;
7788 - }
7789 -
7790 - // Extract model prefix to determine the provider
7791 - $model_parts = explode('-', $selected_model);
7792 - $provider = strtolower($model_parts[0]);
7793 -
7794 - // Handle model selection based on provider prefix
7795 - switch ($provider) {
7796 - case 'gemini':
7797 - if (empty($gemini_api_key)) {
7798 - $error_response = [
7799 - 'error' => esc_html__('Google Gemini API key is not configured', 'mxchat'),
7800 - 'error_code' => 'missing_gemini_api_key'
7801 - ];
7802 - if ($testing_data !== null) {
7803 - $error_response['testing_data'] = $testing_data;
7804 - }
7805 - return $error_response;
7806 - }
7807 - $response = $this->mxchat_generate_response_gemini(
7808 - $selected_model,
7809 - $gemini_api_key,
7810 - $conversation_history,
7811 - $relevant_content,
7812 - $session_id
7813 - );
7814 - break;
7815 -
7816 - case 'claude':
7817 - if (empty($claude_api_key)) {
7818 - $error_response = [
7819 - 'error' => esc_html__('Claude API key is not configured', 'mxchat'),
7820 - 'error_code' => 'missing_claude_api_key'
7821 - ];
7822 - if ($testing_data !== null) {
7823 - $error_response['testing_data'] = $testing_data;
7824 - }
7825 - return $error_response;
7826 - }
7827 - if ($streaming) {
7828 - return $this->mxchat_generate_response_claude_stream(
7829 - $selected_model,
7830 - $claude_api_key,
7831 - $conversation_history,
7832 - $relevant_content,
7833 - $session_id,
7834 - $testing_data
7835 - );
7836 - } else {
7837 - $response = $this->mxchat_generate_response_claude(
7838 - $selected_model,
7839 - $claude_api_key,
7840 - $conversation_history,
7841 - $relevant_content,
7842 - $session_id
7843 - );
7844 - }
7845 - break;
7846 -
7847 - case 'grok':
7848 - if (empty($xai_api_key)) {
7849 - $error_response = [
7850 - 'error' => esc_html__('X.AI API key is not configured', 'mxchat'),
7851 - 'error_code' => 'missing_xai_api_key'
7852 - ];
7853 - if ($testing_data !== null) {
7854 - $error_response['testing_data'] = $testing_data;
7855 - }
7856 - return $error_response;
7857 - }
7858 - if ($streaming) {
7859 - return $this->mxchat_generate_response_xai_stream(
7860 - $selected_model,
7861 - $xai_api_key,
7862 - $conversation_history,
7863 - $relevant_content,
7864 - $session_id,
7865 - $testing_data
7866 - );
7867 - } else {
7868 - $response = $this->mxchat_generate_response_xai(
7869 - $selected_model,
7870 - $xai_api_key,
7871 - $conversation_history,
7872 - $relevant_content,
7873 - $session_id
7874 - );
7875 - }
7876 - break;
7877 -
7878 - case 'deepseek':
7879 - if (empty($deepseek_api_key)) {
7880 - $error_response = [
7881 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
7882 - 'error_code' => 'missing_deepseek_api_key'
7883 - ];
7884 - if ($testing_data !== null) {
7885 - $error_response['testing_data'] = $testing_data;
7886 - }
7887 - return $error_response;
7888 - }
7889 - if ($streaming) {
7890 - return $this->mxchat_generate_response_deepseek_stream(
7891 - $selected_model,
7892 - $deepseek_api_key,
7893 - $conversation_history,
7894 - $relevant_content,
7895 - $session_id,
7896 - $testing_data
7897 - );
7898 - } else {
7899 - $response = $this->mxchat_generate_response_deepseek(
7900 - $selected_model,
7901 - $deepseek_api_key,
7902 - $conversation_history,
7903 - $relevant_content,
7904 - $session_id
7905 - );
7906 - }
7907 - break;
7908 -
7909 - case 'custom':
7910 - // Custom (OpenAI-compatible) provider — Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI
7911 - $cp_base_url = isset($this->options['custom_provider_base_url']) ? trim((string) $this->options['custom_provider_base_url']) : '';
7912 - if (empty($cp_base_url)) {
7913 - $error_response = [
7914 - 'error' => esc_html__('Custom provider is not configured. Set Base URL in MxChat → API Keys → Custom Provider.', 'mxchat'),
7915 - 'error_code' => 'missing_custom_provider_base_url'
7916 - ];
7917 - if ($testing_data !== null) {
7918 - $error_response['testing_data'] = $testing_data;
7919 - }
7920 - return $error_response;
7921 - }
7922 - if ($streaming) {
7923 - return $this->mxchat_generate_response_custom_stream(
7924 - $selected_model,
7925 - $conversation_history,
7926 - $relevant_content,
7927 - $session_id,
7928 - $testing_data
7929 - );
7930 - } else {
7931 - $response = $this->mxchat_generate_response_custom(
7932 - $selected_model,
7933 - $conversation_history,
7934 - $relevant_content
7935 - );
7936 - }
7937 - break;
7938 -
7939 - case 'gpt':
7940 - case 'o1':
7941 - if (empty($api_key)) {
7942 - $error_response = [
7943 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7944 - 'error_code' => 'missing_openai_api_key'
7945 - ];
7946 - if ($testing_data !== null) {
7947 - $error_response['testing_data'] = $testing_data;
7948 - }
7949 - return $error_response;
7950 - }
7951 -
7952 - // Check if web search is enabled for this OpenAI model
7953 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
7954 - // Models that don't support web search
7955 - $unsupported_web_search_models = array('gpt-4.1-nano');
7956 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
7957 -
7958 - if ($web_search_enabled && $model_supports_web_search) {
7959 - // Use Responses API (required for some models, or when web search is enabled)
7960 - return $this->mxchat_generate_response_openai_web_search(
7961 - $selected_model,
7962 - $api_key,
7963 - $conversation_history,
7964 - $relevant_content,
7965 - $session_id,
7966 - $testing_data,
7967 - $streaming
7968 - );
7969 - } elseif ($streaming) {
7970 - return $this->mxchat_generate_response_openai_stream(
7971 - $selected_model,
7972 - $api_key,
7973 - $conversation_history,
7974 - $relevant_content,
7975 - $session_id,
7976 - $testing_data
7977 - );
7978 - } else {
7979 - $response = $this->mxchat_generate_response_openai(
7980 - $selected_model,
7981 - $api_key,
7982 - $conversation_history,
7983 - $relevant_content,
7984 - $session_id
7985 - );
7986 - }
7987 - break;
7988 -
7989 - default:
7990 - if (empty($api_key)) {
7991 - $error_response = [
7992 - 'error' => esc_html__('OpenAI API key is not configured', 'mxchat'),
7993 - 'error_code' => 'missing_openai_api_key'
7994 - ];
7995 - if ($testing_data !== null) {
7996 - $error_response['testing_data'] = $testing_data;
7997 - }
7998 - return $error_response;
7999 - }
8000 -
8001 - // Check if web search is enabled (default case also handles OpenAI models)
8002 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8003 - $unsupported_web_search_models = array('gpt-4.1-nano');
8004 - $model_supports_web_search = !in_array($selected_model, $unsupported_web_search_models);
8005 -
8006 - if ($web_search_enabled && $model_supports_web_search) {
8007 - return $this->mxchat_generate_response_openai_web_search(
8008 - $selected_model,
8009 - $api_key,
8010 - $conversation_history,
8011 - $relevant_content,
8012 - $session_id,
8013 - $testing_data,
8014 - $streaming
8015 - );
8016 - } elseif ($streaming) {
8017 - return $this->mxchat_generate_response_openai_stream(
8018 - $selected_model,
8019 - $api_key,
8020 - $conversation_history,
8021 - $relevant_content,
8022 - $session_id,
8023 - $testing_data
8024 - );
8025 - } else {
8026 - $response = $this->mxchat_generate_response_openai(
8027 - $selected_model,
8028 - $api_key,
8029 - $conversation_history,
8030 - $relevant_content,
8031 - $session_id
8032 - );
8033 - }
8034 - break;
8035 - }
8036 -
8037 - if (is_array($response) && isset($response['error'])) {
8038 - if ($testing_data !== null) {
8039 - $response['testing_data'] = $testing_data;
8040 - }
8041 - return $response;
8042 - }
8043 -
8044 - return $response;
8045 -
8046 - } catch (Exception $e) {
8047 - $error_response = [
8048 - 'error' => sprintf(esc_html__('An error occurred: %s', 'mxchat'), esc_html($e->getMessage())),
8049 - 'error_code' => 'system_exception',
8050 - 'exception_details' => $e->getMessage()
8051 - ];
8052 -
8053 - if ($testing_data !== null) {
8054 - $error_response['testing_data'] = $testing_data;
8055 - }
8056 -
8057 - return $error_response;
8058 - }
8059 -}
8060 -private function mxchat_generate_response_openrouter_stream($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8061 - try {
8062 - $bot_id = $this->get_current_bot_id($session_id);
8063 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8064 -
8065 - if (!is_array($conversation_history)) {
8066 - $conversation_history = array();
8067 - }
8068 -
8069 - $formatted_conversation = array();
8070 -
8071 - $formatted_conversation[] = array(
8072 - 'role' => 'system',
8073 - 'content' => $system_prompt_instructions . " " . $relevant_content
8074 - );
8075 -
8076 - foreach ($conversation_history as $message) {
8077 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8078 - $role = $message['role'];
8079 - if ($role === 'bot' || $role === 'agent') {
8080 - $role = 'assistant';
8081 - }
8082 - if (!in_array($role, ['system', 'assistant', 'user'])) {
8083 - $role = 'user';
8084 - }
8085 - $formatted_conversation[] = array(
8086 - 'role' => $role,
8087 - 'content' => $message['content']
8088 - );
8089 - }
8090 - }
8091 -
8092 - if (headers_sent() || !function_exists('curl_init')) {
8093 - $regular_response = $this->mxchat_generate_response_openrouter(
8094 - $selected_model,
8095 - $openrouter_api_key,
8096 - $conversation_history,
8097 - $relevant_content,
8098 - $session_id
8099 - );
8100 -
8101 - // Save bot response to transcript
8102 - if (!empty($regular_response) && !empty($session_id)) {
8103 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8104 - }
8105 -
8106 - $response_data = [
8107 - 'text' => $regular_response,
8108 - 'html' => '',
8109 - 'session_id' => $session_id
8110 - ];
8111 -
8112 - if ($testing_data !== null) {
8113 - $response_data['testing_data'] = $testing_data;
8114 - }
8115 -
8116 - header('Content-Type: application/json');
8117 - echo json_encode($response_data);
8118 - return true;
8119 - }
8120 -
8121 - $body = json_encode([
8122 - 'model' => $selected_model,
8123 - 'messages' => $formatted_conversation,
8124 - 'temperature' => 1,
8125 - 'stream' => true
8126 - ]);
8127 -
8128 - // V2 retry-on-initial-connect: setup_streaming_headers is now lazy-fired
8129 - // inside WRITEFUNCTION on first byte of a successful upstream.
8130 -
8131 - $captured_status_code = 0;
8132 - $captured_body_pre_stream = '';
8133 - $full_response = '';
8134 - $stream_started = false;
8135 - $buffer = '';
8136 - $errno = 0;
8137 - $last_curl_error = '';
8138 - $http_code = 0;
8139 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8140 - $backoff_ms = array(0, 750, 2000);
8141 -
8142 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8143 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8144 - usleep($backoff_ms[$attempt] * 1000);
8145 - }
8146 -
8147 - $captured_status_code = 0;
8148 - $captured_body_pre_stream = '';
8149 - $full_response = '';
8150 - $stream_started = false;
8151 - $buffer = '';
8152 -
8153 - $ch = curl_init();
8154 - curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions');
8155 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8156 - curl_setopt($ch, CURLOPT_POST, true);
8157 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8158 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8159 - 'Content-Type: application/json',
8160 - 'Authorization: Bearer ' . $openrouter_api_key,
8161 - 'HTTP-Referer: ' . home_url(),
8162 - 'X-Title: ' . get_bloginfo('name')
8163 - ));
8164 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8165 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8166 -
8167 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8168 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8169 - $captured_status_code = (int) $m[1];
8170 - }
8171 - return strlen($header);
8172 - });
8173 -
8174 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8175 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8176 - $captured_body_pre_stream .= $data;
8177 - return strlen($data);
8178 - }
8179 -
8180 - if (!$this->streaming_headers_sent) {
8181 - $this->setup_streaming_headers();
8182 - }
8183 -
8184 - if (!$stream_started && $testing_data !== null) {
8185 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8186 - flush();
8187 - $stream_started = true;
8188 - }
8189 -
8190 - $buffer .= $data;
8191 - $lines = explode("\n", $buffer);
8192 - $buffer = array_pop($lines);
8193 -
8194 - foreach ($lines as $line) {
8195 - if (trim($line) === '') {
8196 - continue;
8197 - }
8198 - if (strpos($line, 'data: ') !== 0) {
8199 - continue;
8200 - }
8201 -
8202 - $json_str = substr($line, 6);
8203 -
8204 - if (trim($json_str) === '[DONE]') {
8205 - echo "data: [DONE]\n\n";
8206 - flush();
8207 - continue;
8208 - }
8209 -
8210 - $json = json_decode(trim($json_str), true);
8211 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8212 - $content = $json['choices'][0]['delta']['content'];
8213 - $full_response .= $content;
8214 -
8215 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8216 - flush();
8217 - }
8218 - }
8219 -
8220 - return strlen($data);
8221 - });
8222 -
8223 - $response = curl_exec($ch);
8224 - $errno = curl_errno($ch);
8225 - $last_curl_error = curl_error($ch);
8226 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8227 - curl_close($ch);
8228 -
8229 - if (!$errno && $http_code === 200) {
8230 - break;
8231 - }
8232 -
8233 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8234 - $can_retry = !$this->streaming_headers_sent
8235 - && ($attempt + 1) < $max_attempts
8236 - && $is_transient;
8237 -
8238 - if (defined('WP_DEBUG') && WP_DEBUG) {
8239 - error_log(sprintf(
8240 - '[MxChat] openrouter_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8241 - $attempt + 1, $max_attempts, $http_code, $errno,
8242 - $is_transient ? 'yes' : 'no',
8243 - $can_retry ? 'Retrying.' : 'Giving up.'
8244 - ));
8245 - }
8246 -
8247 - if (!$can_retry) {
8248 - break;
8249 - }
8250 - }
8251 -
8252 - if (!$errno && $http_code === 200) {
8253 - if (!empty($full_response) && !empty($session_id)) {
8254 - $rag_context_for_storage = null;
8255 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8256 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8257 -
8258 - if ($has_rag_data || $has_action_data) {
8259 - $rag_context_for_storage = [];
8260 -
8261 - if ($has_rag_data) {
8262 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8263 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8264 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8265 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8266 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8267 - }
8268 -
8269 - if ($has_action_data) {
8270 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8271 - }
8272 - }
8273 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8274 - }
8275 - return true;
8276 - }
8277 -
8278 - return $this->mxchat_stream_emit_fallback(
8279 - 'openai',
8280 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8281 - $session_id,
8282 - $testing_data
8283 - );
8284 -
8285 - } catch (Exception $e) {
8286 - return $this->mxchat_stream_emit_fallback(
8287 - 'openai',
8288 - $this->mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id),
8289 - $session_id,
8290 - $testing_data
8291 - );
8292 - }
8293 -}
8294 -private function mxchat_generate_response_openai_stream($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8295 - try {
8296 - $bot_id = $this->get_current_bot_id($session_id);
8297 -
8298 - // Get system prompt instructions using centralized function
8299 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8300 -
8301 - // Ensure conversation_history is an array
8302 - if (!is_array($conversation_history)) {
8303 - $conversation_history = array();
8304 - }
8305 -
8306 - // Format conversation history for OpenAI
8307 - $formatted_conversation = array();
8308 -
8309 - $formatted_conversation[] = array(
8310 - 'role' => 'system',
8311 - 'content' => $system_prompt_instructions . " " . $relevant_content
8312 - );
8313 -
8314 - foreach ($conversation_history as $message) {
8315 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8316 - $role = $message['role'];
8317 - if ($role === 'bot' || $role === 'agent') {
8318 - $role = 'assistant';
8319 - }
8320 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
8321 - $role = 'user';
8322 - }
8323 - $formatted_conversation[] = array(
8324 - 'role' => $role,
8325 - 'content' => $message['content']
8326 - );
8327 - }
8328 - }
8329 -
8330 - // Check if we can actually stream
8331 - if (headers_sent() || !function_exists('curl_init')) {
8332 - // Fallback to regular response with testing data
8333 - $regular_response = $this->mxchat_generate_response_openai(
8334 - $selected_model,
8335 - $api_key,
8336 - $conversation_history,
8337 - $relevant_content,
8338 - $session_id
8339 - );
8340 -
8341 - // Save bot response to transcript
8342 - if (!empty($regular_response) && !empty($session_id)) {
8343 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
8344 - }
8345 -
8346 - $response_data = [
8347 - 'text' => $regular_response,
8348 - 'html' => '',
8349 - 'session_id' => $session_id
8350 - ];
8351 -
8352 - if ($testing_data !== null) {
8353 - $response_data['testing_data'] = $testing_data;
8354 - }
8355 -
8356 - header('Content-Type: application/json');
8357 - echo json_encode($response_data);
8358 - return true;
8359 - }
8360 -
8361 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
8362 - $is_gpt5_model = (
8363 - strpos($selected_model, 'gpt-5') === 0 ||
8364 - $selected_model === 'gpt-5.2' ||
8365 - $selected_model === 'gpt-5.1-2025-11-13' ||
8366 - $selected_model === 'gpt-5' ||
8367 - $selected_model === 'gpt-5-mini' ||
8368 - $selected_model === 'gpt-5-nano'
8369 - );
8370 -
8371 - // Build request body with optimal settings for fast streaming
8372 - $request_body = [
8373 - 'model' => $selected_model,
8374 - 'messages' => $formatted_conversation,
8375 - 'temperature' => 1,
8376 - 'stream' => true
8377 - ];
8378 -
8379 - // Add reasoning_effort only for GPT-5 models that support it
8380 - // These chat models don't support reasoning_effort parameter
8381 - $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');
8382 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
8383 - // GPT-5.1 uses 'low' instead of 'minimal'
8384 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8385 - $request_body['reasoning_effort'] = 'low';
8386 - } elseif ($selected_model === 'gpt-5.5') {
8387 - $request_body['reasoning_effort'] = 'none';
8388 - } elseif ($selected_model === 'gpt-5.4') {
8389 - $request_body['reasoning_effort'] = 'none';
8390 - } else {
8391 - $request_body['reasoning_effort'] = 'minimal';
8392 - }
8393 - }
8394 -
8395 - $body = json_encode($request_body);
8396 -
8397 - // V2 retry-on-initial-connect: do NOT call setup_streaming_headers() here.
8398 - // It is now lazy-fired inside the WRITEFUNCTION on the first byte of a
8399 - // SUCCESSFUL upstream response, gated by the captured HTTP status.
8400 -
8401 - $captured_status_code = 0;
8402 - $captured_body_pre_stream = '';
8403 - $full_response = '';
8404 - $stream_started = false;
8405 - $buffer = '';
8406 - $errno = 0;
8407 - $last_curl_error = '';
8408 - $http_code = 0;
8409 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8410 - $backoff_ms = array(0, 750, 2000);
8411 -
8412 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8413 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8414 - usleep($backoff_ms[$attempt] * 1000);
8415 - }
8416 -
8417 - // Reset per-attempt capture state.
8418 - $captured_status_code = 0;
8419 - $captured_body_pre_stream = '';
8420 - $full_response = '';
8421 - $stream_started = false;
8422 - $buffer = '';
8423 -
8424 - $ch = curl_init();
8425 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
8426 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8427 - curl_setopt($ch, CURLOPT_POST, true);
8428 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8429 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8430 - 'Content-Type: application/json',
8431 - 'Authorization: Bearer ' . $api_key
8432 - ));
8433 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8434 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
8435 -
8436 - // Capture HTTP status as soon as response headers arrive — fires before WRITEFUNCTION.
8437 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8438 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8439 - $captured_status_code = (int) $m[1];
8440 - }
8441 - return strlen($header);
8442 - });
8443 -
8444 - // Buffer control for real-time streaming
8445 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8446 - // V2 guard: if upstream returned non-200, buffer body for transient
8447 - // classification and DO NOT emit to client. Stream channel must NOT open.
8448 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8449 - $captured_body_pre_stream .= $data;
8450 - return strlen($data);
8451 - }
8452 -
8453 - // Lazy-fire streaming headers on first byte of a SUCCESSFUL upstream.
8454 - // After this point streaming_headers_sent === true → retry is structurally blocked.
8455 - if (!$this->streaming_headers_sent) {
8456 - $this->setup_streaming_headers();
8457 - }
8458 -
8459 - // Send testing data as the first event if available
8460 - if (!$stream_started && $testing_data !== null) {
8461 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
8462 - flush();
8463 - $stream_started = true;
8464 - }
8465 -
8466 - // CRITICAL FIX: Append new data to buffer
8467 - $buffer .= $data;
8468 -
8469 - // Process complete lines only
8470 - $lines = explode("\n", $buffer);
8471 -
8472 - // CRITICAL FIX: Keep the last incomplete line in the buffer
8473 - $buffer = array_pop($lines);
8474 -
8475 - foreach ($lines as $line) {
8476 - if (trim($line) === '') {
8477 - continue;
8478 - }
8479 - if (strpos($line, 'data: ') !== 0) {
8480 - continue;
8481 - }
8482 -
8483 - $json_str = substr($line, 6);
8484 -
8485 - if (trim($json_str) === '[DONE]') {
8486 - echo "data: [DONE]\n\n";
8487 - flush();
8488 - continue;
8489 - }
8490 -
8491 - $json = json_decode(trim($json_str), true);
8492 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8493 - $content = $json['choices'][0]['delta']['content'];
8494 - $full_response .= $content;
8495 -
8496 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
8497 - flush();
8498 - }
8499 - }
8500 -
8501 - return strlen($data);
8502 - });
8503 -
8504 - $response = curl_exec($ch);
8505 - $errno = curl_errno($ch);
8506 - $last_curl_error = curl_error($ch);
8507 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8508 - curl_close($ch);
8509 -
8510 - if (!$errno && $http_code === 200) {
8511 - break; // Happy path — WRITEFUNCTION already streamed everything.
8512 - }
8513 -
8514 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8515 - $can_retry = !$this->streaming_headers_sent
8516 - && ($attempt + 1) < $max_attempts
8517 - && $is_transient;
8518 -
8519 - if (defined('WP_DEBUG') && WP_DEBUG) {
8520 - error_log(sprintf(
8521 - '[MxChat] openai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8522 - $attempt + 1, $max_attempts, $http_code, $errno,
8523 - $is_transient ? 'yes' : 'no',
8524 - $can_retry ? 'Retrying.' : 'Giving up.'
8525 - ));
8526 - }
8527 -
8528 - if (!$can_retry) {
8529 - break;
8530 - }
8531 - }
8532 -
8533 - // Post-loop branch.
8534 - if (!$errno && $http_code === 200) {
8535 - // Happy path — save the complete response to maintain chat persistence.
8536 - if (!empty($full_response) && !empty($session_id)) {
8537 - $rag_context_for_storage = null;
8538 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
8539 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
8540 -
8541 - if ($has_rag_data || $has_action_data) {
8542 - $rag_context_for_storage = [];
8543 -
8544 - if ($has_rag_data) {
8545 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
8546 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
8547 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
8548 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
8549 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
8550 - }
8551 -
8552 - if ($has_action_data) {
8553 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
8554 - }
8555 - }
8556 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
8557 - }
8558 -
8559 - return true;
8560 - }
8561 -
8562 - // Failure path — branch on whether SSE channel was opened.
8563 - return $this->mxchat_stream_emit_fallback(
8564 - 'openai',
8565 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8566 - $session_id,
8567 - $testing_data
8568 - );
8569 -
8570 - } catch (Exception $e) {
8571 - return $this->mxchat_stream_emit_fallback(
8572 - 'openai',
8573 - $this->mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id),
8574 - $session_id,
8575 - $testing_data
8576 - );
8577 - }
8578 -}
8579 -
8580 -/**
8581 - * Shared fallback emitter for streaming chat functions. Two outcomes:
8582 - * - streaming_headers_sent === true: SSE channel is open. Emit fallback content
8583 - * as `data: {...}\n\n` + `data: [DONE]\n\n` so the widget renders it as a
8584 - * normal bot bubble. Transcript row is persisted.
8585 - * - streaming_headers_sent === false: SSE channel never opened (retries
8586 - * exhausted on initial connect). Emit a clean JSON response — the path
8587 - * the widget would normally hit if streaming wasn't even attempted.
8588 - *
8589 - * Used by all six *_stream functions after their per-attempt retry loop.
8590 - */
8591 -private function mxchat_stream_emit_fallback($provider_hint, $regular_response, $session_id, $testing_data = null) {
8592 - $is_error_array = is_array($regular_response) && isset($regular_response['error']);
8593 -
8594 - if ($this->streaming_headers_sent) {
8595 - if ($is_error_array) {
8596 - echo "data: " . json_encode([
8597 - 'error' => true,
8598 - 'error_message' => $regular_response['error'],
8599 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8600 - 'text' => $regular_response['error'],
8601 - 'message' => $regular_response['error']
8602 - ]) . "\n\n";
8603 - echo "data: [DONE]\n\n";
8604 - flush();
8605 - return true;
8606 - }
8607 - $fallback_message = (string) $regular_response;
8608 - if (!empty($fallback_message) && !empty($session_id)) {
8609 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8610 - }
8611 - echo "data: " . json_encode(['content' => $fallback_message]) . "\n\n";
8612 - echo "data: [DONE]\n\n";
8613 - flush();
8614 - return true;
8615 - }
8616 -
8617 - // SSE channel never opened — clean JSON fallback.
8618 - if ($is_error_array) {
8619 - header('Content-Type: application/json');
8620 - echo json_encode(array(
8621 - 'error' => true,
8622 - 'error_message' => $regular_response['error'],
8623 - 'error_code' => $regular_response['error_code'] ?? 'api_error',
8624 - 'text' => $regular_response['error'],
8625 - 'message' => $regular_response['error'],
8626 - ));
8627 - return true;
8628 - }
8629 -
8630 - $fallback_message = (string) $regular_response;
8631 - if (!empty($fallback_message) && !empty($session_id)) {
8632 - $this->mxchat_save_chat_message($session_id, 'bot', $fallback_message);
8633 - }
8634 - $response_data = array(
8635 - 'text' => $fallback_message,
8636 - 'html' => '',
8637 - 'session_id' => $session_id,
8638 - );
8639 - if ($testing_data !== null) {
8640 - $response_data['testing_data'] = $testing_data;
8641 - }
8642 - header('Content-Type: application/json');
8643 - echo json_encode($response_data);
8644 - return true;
8645 -}
8646 -
8647 -/**
8648 - * Resolve custom (OpenAI-compatible) provider config from settings.
8649 - * Returns ['base_url','api_key','model','auth_scheme','api_version','chat_url','headers'].
8650 - */
8651 -private function mxchat_resolve_custom_provider() {
8652 - $base_url = isset($this->options['custom_provider_base_url']) ? rtrim(trim((string) $this->options['custom_provider_base_url']), '/') : '';
8653 - $api_key = isset($this->options['custom_provider_api_key']) ? trim((string) $this->options['custom_provider_api_key']) : '';
8654 - $model = isset($this->options['custom_provider_model']) ? trim((string) $this->options['custom_provider_model']) : '';
8655 - $auth_scheme = isset($this->options['custom_provider_auth_scheme']) ? $this->options['custom_provider_auth_scheme'] : 'bearer';
8656 - $api_version = isset($this->options['custom_provider_api_version']) ? trim((string) $this->options['custom_provider_api_version']) : '';
8657 -
8658 - $chat_url = $base_url . '/chat/completions';
8659 - if (!empty($api_version)) {
8660 - $chat_url .= (strpos($chat_url, '?') === false ? '?' : '&') . 'api-version=' . rawurlencode($api_version);
8661 - }
8662 -
8663 - $headers = array('Content-Type: application/json');
8664 - if (!empty($api_key)) {
8665 - if ($auth_scheme === 'api-key') {
8666 - $headers[] = 'api-key: ' . $api_key;
8667 - } else {
8668 - $headers[] = 'Authorization: Bearer ' . $api_key;
8669 - }
8670 - }
8671 -
8672 - return array(
8673 - 'base_url' => $base_url,
8674 - 'api_key' => $api_key,
8675 - 'model' => $model !== '' ? $model : 'default',
8676 - 'auth_scheme' => $auth_scheme,
8677 - 'api_version' => $api_version,
8678 - 'chat_url' => $chat_url,
8679 - 'headers' => $headers,
8680 - );
8681 -}
8682 -
8683 -/**
8684 - * Streaming chat completion against an OpenAI-compatible custom provider
8685 - * (Ollama, LM Studio, vLLM, llama.cpp, Azure OpenAI, etc.).
8686 - * Mirrors mxchat_generate_response_openai_stream but with parameterized URL/auth/model.
8687 - */
8688 -private function mxchat_generate_response_custom_stream($selected_model, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
8689 - try {
8690 - $cfg = $this->mxchat_resolve_custom_provider();
8691 - if (empty($cfg['base_url'])) {
8692 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8693 - }
8694 -
8695 - $bot_id = $this->get_current_bot_id($session_id);
8696 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8697 - if (!is_array($conversation_history)) {
8698 - $conversation_history = array();
8699 - }
8700 -
8701 - $formatted_conversation = array();
8702 - $formatted_conversation[] = array(
8703 - 'role' => 'system',
8704 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8705 - );
8706 - foreach ($conversation_history as $message) {
8707 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8708 - $role = $message['role'];
8709 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8710 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8711 - $formatted_conversation[] = array('role' => $role, 'content' => $message['content']);
8712 - }
8713 - }
8714 -
8715 - if (headers_sent() || !function_exists('curl_init')) {
8716 - // No streaming capability — fall through to non-stream wrapper
8717 - $regular = $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content);
8718 - if (!empty($regular) && !empty($session_id) && is_string($regular)) {
8719 - $this->mxchat_save_chat_message($session_id, 'bot', $regular);
8720 - }
8721 - $response_data = array('text' => is_string($regular) ? $regular : '', 'html' => '', 'session_id' => $session_id);
8722 - if ($testing_data !== null) { $response_data['testing_data'] = $testing_data; }
8723 - header('Content-Type: application/json');
8724 - echo json_encode($response_data);
8725 - return true;
8726 - }
8727 -
8728 - $request_body = array(
8729 - 'model' => $cfg['model'],
8730 - 'messages' => $formatted_conversation,
8731 - 'stream' => true,
8732 - );
8733 - $body = json_encode($request_body);
8734 -
8735 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
8736 -
8737 - $captured_status_code = 0;
8738 - $captured_body_pre_stream = '';
8739 - $full_response = '';
8740 - $stream_started = false;
8741 - $buffer = '';
8742 - $errno = 0;
8743 - $http_code = 0;
8744 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
8745 - $backoff_ms = array(0, 750, 2000);
8746 -
8747 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
8748 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
8749 - usleep($backoff_ms[$attempt] * 1000);
8750 - }
8751 -
8752 - $captured_status_code = 0;
8753 - $captured_body_pre_stream = '';
8754 - $full_response = '';
8755 - $stream_started = false;
8756 - $buffer = '';
8757 -
8758 - $ch = curl_init();
8759 - curl_setopt($ch, CURLOPT_URL, $cfg['chat_url']);
8760 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
8761 - curl_setopt($ch, CURLOPT_POST, true);
8762 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8763 - curl_setopt($ch, CURLOPT_HTTPHEADER, $cfg['headers']);
8764 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
8765 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
8766 -
8767 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
8768 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
8769 - $captured_status_code = (int) $m[1];
8770 - }
8771 - return strlen($header);
8772 - });
8773 -
8774 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
8775 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
8776 - $captured_body_pre_stream .= $data;
8777 - return strlen($data);
8778 - }
8779 -
8780 - if (!$this->streaming_headers_sent) {
8781 - $this->setup_streaming_headers();
8782 - }
8783 -
8784 - if (!$stream_started && $testing_data !== null) {
8785 - echo "data: " . json_encode(array('testing_data' => $testing_data)) . "\n\n";
8786 - flush();
8787 - $stream_started = true;
8788 - }
8789 - $buffer .= $data;
8790 - $lines = explode("\n", $buffer);
8791 - $buffer = array_pop($lines);
8792 - foreach ($lines as $line) {
8793 - if (trim($line) === '') { continue; }
8794 - if (strpos($line, 'data: ') !== 0) { continue; }
8795 - $json_str = substr($line, 6);
8796 - if (trim($json_str) === '[DONE]') {
8797 - echo "data: [DONE]\n\n";
8798 - flush();
8799 - continue;
8800 - }
8801 - $json = json_decode(trim($json_str), true);
8802 - if ($json && isset($json['choices'][0]['delta']['content'])) {
8803 - $content = $json['choices'][0]['delta']['content'];
8804 - $full_response .= $content;
8805 - echo "data: " . json_encode(array('content' => $content)) . "\n\n";
8806 - flush();
8807 - }
8808 - }
8809 - return strlen($data);
8810 - });
8811 -
8812 - $response = curl_exec($ch);
8813 - $errno = curl_errno($ch);
8814 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
8815 - curl_close($ch);
8816 -
8817 - if (!$errno && $http_code === 200) {
8818 - break;
8819 - }
8820 -
8821 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
8822 - $can_retry = !$this->streaming_headers_sent
8823 - && ($attempt + 1) < $max_attempts
8824 - && $is_transient;
8825 -
8826 - if (defined('WP_DEBUG') && WP_DEBUG) {
8827 - error_log(sprintf(
8828 - '[MxChat] custom_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
8829 - $attempt + 1, $max_attempts, $http_code, $errno,
8830 - $is_transient ? 'yes' : 'no',
8831 - $can_retry ? 'Retrying.' : 'Giving up.'
8832 - ));
8833 - }
8834 -
8835 - if (!$can_retry) {
8836 - break;
8837 - }
8838 - }
8839 -
8840 - if (!$errno && $http_code === 200) {
8841 - if (!empty($full_response) && !empty($session_id)) {
8842 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response);
8843 - }
8844 - return true;
8845 - }
8846 -
8847 - return $this->mxchat_stream_emit_fallback(
8848 - 'openai',
8849 - $this->mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content),
8850 - $session_id,
8851 - $testing_data
8852 - );
8853 -
8854 - } catch (Exception $e) {
8855 - return array('error' => sprintf(esc_html__('Custom provider error: %s', 'mxchat'), $e->getMessage()), 'error_code' => 'custom_provider_exception');
8856 - }
8857 -}
8858 -
8859 -/**
8860 - * Non-streaming chat completion against a custom OpenAI-compatible provider.
8861 - * Returns string content on success, array['error'=>...] on failure.
8862 - */
8863 -private function mxchat_generate_response_custom($selected_model, $conversation_history, $relevant_content) {
8864 - $cfg = $this->mxchat_resolve_custom_provider();
8865 - if (empty($cfg['base_url'])) {
8866 - return array('error' => esc_html__('Custom provider Base URL is not configured.', 'mxchat'), 'error_code' => 'missing_custom_provider_base_url');
8867 - }
8868 -
8869 - $bot_id = $this->get_current_bot_id(null);
8870 - $system_prompt_instructions = $this->get_system_instructions($bot_id, null);
8871 - if (!is_array($conversation_history)) {
8872 - $conversation_history = array();
8873 - }
8874 -
8875 - $messages = array(array(
8876 - 'role' => 'system',
8877 - 'content' => $system_prompt_instructions . ' ' . $relevant_content,
8878 - ));
8879 - foreach ($conversation_history as $message) {
8880 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8881 - $role = $message['role'];
8882 - if ($role === 'bot' || $role === 'agent') { $role = 'assistant'; }
8883 - if (!in_array($role, array('system', 'assistant', 'user', 'function', 'tool'))) { $role = 'user'; }
8884 - $messages[] = array('role' => $role, 'content' => $message['content']);
8885 - }
8886 - }
8887 -
8888 - $headers_assoc = array('Content-Type' => 'application/json');
8889 - if (!empty($cfg['api_key'])) {
8890 - if ($cfg['auth_scheme'] === 'api-key') {
8891 - $headers_assoc['api-key'] = $cfg['api_key'];
8892 - } else {
8893 - $headers_assoc['Authorization'] = 'Bearer ' . $cfg['api_key'];
8894 - }
8895 - }
8896 -
8897 - $response = $this->mxchat_provider_call_with_retry($cfg['chat_url'], array(
8898 - 'headers' => $headers_assoc,
8899 - 'body' => wp_json_encode(array(
8900 - 'model' => $cfg['model'],
8901 - 'messages' => $messages,
8902 - )),
8903 - 'timeout' => 120,
8904 - ), 'openai');
8905 -
8906 - if (is_wp_error($response)) {
8907 - return array('error' => sprintf(esc_html__('Custom provider request failed: %s', 'mxchat'), $response->get_error_message()), 'error_code' => 'custom_provider_network_error');
8908 - }
8909 - $code = (int) wp_remote_retrieve_response_code($response);
8910 - if ($code < 200 || $code >= 300) {
8911 - return array('error' => sprintf(esc_html__('Custom provider returned HTTP %d.', 'mxchat'), $code), 'error_code' => 'custom_provider_http_error');
8912 - }
8913 - $body = json_decode(wp_remote_retrieve_body($response), true);
8914 - if (isset($body['choices'][0]['message']['content'])) {
8915 - return (string) $body['choices'][0]['message']['content'];
8916 - }
8917 - return array('error' => esc_html__('Custom provider returned an unexpected response shape.', 'mxchat'), 'error_code' => 'custom_provider_response_shape');
8918 -}
8919 -
8920 -/**
8921 - * Generate response using OpenAI Responses API with web search tool
8922 - * This uses the newer Responses API which supports web search functionality
8923 - */
8924 -private function mxchat_generate_response_openai_web_search($selected_model, $api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null, $streaming = false) {
8925 - try {
8926 - $bot_id = $this->get_current_bot_id($session_id);
8927 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
8928 -
8929 - if (!is_array($conversation_history)) {
8930 - $conversation_history = array();
8931 - }
8932 -
8933 - // Build the input for Responses API
8934 - // The Responses API uses a different format - we need to construct the input properly
8935 - $input_parts = [];
8936 -
8937 - // Add system instructions as context
8938 - $system_context = $system_prompt_instructions . "\n\n" . $relevant_content;
8939 -
8940 - // Build conversation as input items for Responses API
8941 - foreach ($conversation_history as $message) {
8942 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
8943 - $role = $message['role'];
8944 - if ($role === 'bot' || $role === 'agent') {
8945 - $role = 'assistant';
8946 - }
8947 - if (!in_array($role, ['assistant', 'user'])) {
8948 - $role = 'user';
8949 - }
8950 - $input_parts[] = [
8951 - 'type' => 'message',
8952 - 'role' => $role,
8953 - 'content' => $message['content']
8954 - ];
8955 - }
8956 - }
8957 -
8958 - // Build request body for Responses API
8959 - $request_body = [
8960 - 'model' => $selected_model,
8961 - 'input' => $input_parts,
8962 - 'instructions' => $system_context,
8963 - 'stream' => $streaming
8964 - ];
8965 -
8966 - // Only add web search tool if web search is enabled in settings
8967 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
8968 - if ($web_search_enabled) {
8969 - $request_body['tools'] = [
8970 - ['type' => 'web_search']
8971 - ];
8972 - }
8973 -
8974 - // Add reasoning effort for supported models
8975 - $is_gpt5_model = strpos($selected_model, 'gpt-5') === 0;
8976 - $no_reasoning_web = array('gpt-5.2', 'gpt-5.3-chat-latest', 'gpt-5.4-mini', 'gpt-5.4-nano');
8977 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_web, true)) {
8978 - if ($selected_model === 'gpt-5.1-2025-11-13') {
8979 - $request_body['reasoning'] = ['effort' => 'low'];
8980 - } elseif ($selected_model === 'gpt-5.5') {
8981 - $request_body['reasoning'] = ['effort' => 'low'];
8982 - } elseif ($selected_model === 'gpt-5.4') {
8983 - $request_body['reasoning'] = ['effort' => 'low'];
8984 - }
8985 - }
8986 -
8987 - //error_log("MXCHAT WEB SEARCH: Request body: " . json_encode($request_body));
8988 -
8989 - if ($streaming) {
8990 - return $this->mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data);
8991 - } else {
8992 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
8993 - }
8994 -
8995 - } catch (Exception $e) {
8996 - //error_log("MXCHAT WEB SEARCH ERROR: " . $e->getMessage());
8997 - return [
8998 - 'error' => sprintf(esc_html__('Web search error: %s', 'mxchat'), esc_html($e->getMessage())),
8999 - 'error_code' => 'web_search_exception'
9000 - ];
9001 - }
9002 -}
9003 -
9004 -/**
9005 - * Handle non-streaming web search response
9006 - */
9007 -private function mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9008 - $request_body['stream'] = false;
9009 -
9010 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/responses', array(
9011 - 'headers' => array(
9012 - 'Authorization' => 'Bearer ' . $api_key,
9013 - 'Content-Type' => 'application/json'
9014 - ),
9015 - 'body' => json_encode($request_body),
9016 - 'timeout' => 90
9017 - ), 'openai');
9018 -
9019 - if (is_wp_error($response)) {
9020 - //error_log("MXCHAT WEB SEARCH ERROR: WP Error: " . $response->get_error_message());
9021 - return [
9022 - 'error' => esc_html__('Failed to connect to OpenAI web search API', 'mxchat'),
9023 - 'error_code' => 'web_search_connection_error'
9024 - ];
9025 - }
9026 -
9027 - $response_code = wp_remote_retrieve_response_code($response);
9028 - $response_body = wp_remote_retrieve_body($response);
9029 -
9030 - //error_log("MXCHAT WEB SEARCH: Response code: " . $response_code);
9031 - //error_log("MXCHAT WEB SEARCH: Response body (first 2000): " . substr($response_body, 0, 2000));
9032 -
9033 - if ($response_code !== 200) {
9034 - $error_data = json_decode($response_body, true);
9035 - $error_message = $error_data['error']['message'] ?? 'Unknown API error';
9036 - return [
9037 - 'error' => sprintf(esc_html__('OpenAI API error: %s', 'mxchat'), esc_html($error_message)),
9038 - 'error_code' => 'web_search_api_error'
9039 - ];
9040 - }
9041 -
9042 - $result = json_decode($response_body, true);
9043 -
9044 - if (json_last_error() !== JSON_ERROR_NONE) {
9045 - return [
9046 - 'error' => esc_html__('Invalid response from OpenAI', 'mxchat'),
9047 - 'error_code' => 'web_search_json_error'
9048 - ];
9049 - }
9050 -
9051 - // Extract the response text and citations from Responses API format
9052 - $output_text = '';
9053 - $citations = [];
9054 -
9055 - if (isset($result['output'])) {
9056 - foreach ($result['output'] as $output_item) {
9057 - if ($output_item['type'] === 'message' && isset($output_item['content'])) {
9058 - foreach ($output_item['content'] as $content_item) {
9059 - if ($content_item['type'] === 'output_text') {
9060 - $output_text .= $content_item['text'];
9061 -
9062 - // Extract citations/annotations
9063 - if (isset($content_item['annotations'])) {
9064 - foreach ($content_item['annotations'] as $annotation) {
9065 - if ($annotation['type'] === 'url_citation') {
9066 - $citations[] = [
9067 - 'url' => $annotation['url'],
9068 - 'title' => $annotation['title'] ?? ''
9069 - ];
9070 - }
9071 - }
9072 - }
9073 - }
9074 - }
9075 - }
9076 - }
9077 - }
9078 -
9079 - // If we have citations, append them to the response
9080 - if (!empty($citations)) {
9081 - $output_text .= "\n\n**Sources:**\n";
9082 - $seen_urls = [];
9083 - foreach ($citations as $citation) {
9084 - if (!in_array($citation['url'], $seen_urls)) {
9085 - $seen_urls[] = $citation['url'];
9086 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9087 - $output_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9088 - }
9089 - }
9090 - }
9091 -
9092 - // Transcript save is handled by the main handler (mxchat_handle_chat_request)
9093 - // which includes rag_context for the "sources" link in transcripts.
9094 -
9095 - return $output_text;
9096 -}
9097 -
9098 -/**
9099 - * Handle streaming web search response using Responses API
9100 - */
9101 -private function mxchat_web_search_streaming_response($request_body, $api_key, $session_id, $testing_data) {
9102 - $request_body['stream'] = true;
9103 -
9104 - // Check if we can stream
9105 - if (headers_sent() || !function_exists('curl_init')) {
9106 - // Fallback to non-streaming
9107 - return $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data);
9108 - }
9109 -
9110 - // Setup streaming headers
9111 - $this->setup_streaming_headers();
9112 -
9113 - $ch = curl_init();
9114 - curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/responses');
9115 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9116 - curl_setopt($ch, CURLOPT_POST, true);
9117 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
9118 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9119 - 'Content-Type: application/json',
9120 - 'Authorization: Bearer ' . $api_key
9121 - ));
9122 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9123 - curl_setopt($ch, CURLOPT_TIMEOUT, 120);
9124 -
9125 - $full_response = '';
9126 - $stream_started = false;
9127 - $buffer = '';
9128 - $citations = [];
9129 -
9130 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$citations, $testing_data) {
9131 - // Send testing data as first event if available
9132 - if (!$stream_started && $testing_data !== null) {
9133 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9134 - flush();
9135 - $stream_started = true;
9136 - }
9137 -
9138 - $buffer .= $data;
9139 - $lines = explode("\n", $buffer);
9140 - $buffer = array_pop($lines);
9141 -
9142 - foreach ($lines as $line) {
9143 - if (trim($line) === '') continue;
9144 - if (strpos($line, 'data: ') !== 0) continue;
9145 -
9146 - $json_str = substr($line, 6);
9147 -
9148 - if (trim($json_str) === '[DONE]') {
9149 - // Append citations if we have any
9150 - if (!empty($citations)) {
9151 - $citation_text = "\n\n**Sources:**\n";
9152 - $seen_urls = [];
9153 - foreach ($citations as $citation) {
9154 - if (!in_array($citation['url'], $seen_urls)) {
9155 - $seen_urls[] = $citation['url'];
9156 - $title = !empty($citation['title']) ? $citation['title'] : $citation['url'];
9157 - $citation_text .= "- [" . $title . "](" . $citation['url'] . ")\n";
9158 - }
9159 - }
9160 - echo "data: " . json_encode(['content' => $citation_text]) . "\n\n";
9161 - $full_response .= $citation_text;
9162 - flush();
9163 - }
9164 - echo "data: [DONE]\n\n";
9165 - flush();
9166 - continue;
9167 - }
9168 -
9169 - $json = json_decode(trim($json_str), true);
9170 - if (!$json) continue;
9171 -
9172 - // Handle Responses API streaming events
9173 - // The format is different from Chat Completions
9174 - if (isset($json['type'])) {
9175 - switch ($json['type']) {
9176 - case 'response.output_text.delta':
9177 - // Text content delta
9178 - if (isset($json['delta'])) {
9179 - $content = $json['delta'];
9180 - $full_response .= $content;
9181 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9182 - flush();
9183 - }
9184 - break;
9185 -
9186 - case 'response.output_item.done':
9187 - // Check for citations in completed items
9188 - if (isset($json['item']['content'])) {
9189 - foreach ($json['item']['content'] as $content_item) {
9190 - if (isset($content_item['annotations'])) {
9191 - foreach ($content_item['annotations'] as $annotation) {
9192 - if ($annotation['type'] === 'url_citation') {
9193 - $citations[] = [
9194 - 'url' => $annotation['url'],
9195 - 'title' => $annotation['title'] ?? ''
9196 - ];
9197 - }
9198 - }
9199 - }
9200 - }
9201 - }
9202 - break;
9203 - }
9204 - }
9205 - }
9206 -
9207 - return strlen($data);
9208 - });
9209 -
9210 - $response = curl_exec($ch);
9211 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
9212 -
9213 - if (curl_errno($ch) || $http_code !== 200) {
9214 - $curl_error = curl_error($ch);
9215 - curl_close($ch);
9216 -
9217 - //error_log("MXCHAT WEB SEARCH STREAM ERROR: HTTP $http_code, cURL error: $curl_error");
9218 -
9219 - return $this->mxchat_stream_emit_fallback(
9220 - 'web_search',
9221 - $this->mxchat_web_search_non_streaming_response($request_body, $api_key, $session_id, $testing_data),
9222 - $session_id,
9223 - $testing_data
9224 - );
9225 - }
9226 -
9227 - curl_close($ch);
9228 -
9229 - // Save the complete response with RAG context so the "sources" link
9230 - // appears in transcripts — mirrors the pattern used by Claude/OpenAI streaming.
9231 - if (!empty($full_response) && !empty($session_id)) {
9232 - $rag_context_for_storage = null;
9233 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9234 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9235 -
9236 - if ($has_rag_data || $has_action_data) {
9237 - $rag_context_for_storage = [];
9238 -
9239 - if ($has_rag_data) {
9240 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9241 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9242 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9243 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9244 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9245 - }
9246 -
9247 - if ($has_action_data) {
9248 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9249 - }
9250 - }
9251 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9252 - }
9253 -
9254 - return true;
9255 -}
9256 -
9257 -private function mxchat_generate_response_claude_stream($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9258 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
9259 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
9260 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
9261 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
9262 - try {
9263 - // Get bot ID from session or request
9264 - $bot_id = $this->get_current_bot_id($session_id);
9265 -
9266 - // Get system prompt instructions using centralized function
9267 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9268 - // Ensure conversation_history is an array
9269 - if (!is_array($conversation_history)) {
9270 - $conversation_history = array();
9271 - }
9272 -
9273 - // Clean and validate conversation history
9274 - foreach ($conversation_history as &$message) {
9275 - // Convert bot and agent roles to assistant
9276 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
9277 - $message['role'] = 'assistant';
9278 - }
9279 -
9280 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
9281 - if (!in_array($message['role'], ['assistant', 'user'])) {
9282 - $message['role'] = 'user';
9283 - }
9284 -
9285 - // Ensure content field exists
9286 - if (!isset($message['content']) || empty($message['content'])) {
9287 - $message['content'] = '';
9288 - }
9289 -
9290 - // Remove any unsupported fields
9291 - $message = array_intersect_key($message, array_flip(['role', 'content']));
9292 - }
9293 -
9294 - // Add relevant content as the latest user message
9295 - $conversation_history[] = [
9296 - 'role' => 'user',
9297 - 'content' => $relevant_content
9298 - ];
9299 -
9300 - // Prepare the request body with stream: true
9301 - $payload = [
9302 - 'model' => $selected_model,
9303 - 'messages' => $conversation_history,
9304 - 'max_tokens' => 1000,
9305 - 'temperature' => 0.8,
9306 - 'system' => $system_prompt_instructions,
9307 - 'stream' => true
9308 - ];
9309 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
9310 - $body = json_encode($payload);
9311 -
9312 - // Check if we can actually stream (headers not sent, etc.)
9313 - if (headers_sent() || !function_exists('curl_init')) {
9314 - // Fallback to regular response with testing data
9315 - //error_log("MxChat: Streaming not possible, falling back to regular response");
9316 - $regular_response = $this->mxchat_generate_response_claude(
9317 - $selected_model,
9318 - $claude_api_key,
9319 - array_slice($conversation_history, 0, -1), // Remove the added content
9320 - $relevant_content,
9321 - $session_id
9322 - );
9323 -
9324 - // Save bot response to transcript
9325 - if (!empty($regular_response) && !empty($session_id)) {
9326 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9327 - }
9328 -
9329 - // Return as JSON with testing data
9330 - $response_data = [
9331 - 'text' => $regular_response,
9332 - 'html' => '',
9333 - 'session_id' => $session_id
9334 - ];
9335 -
9336 - if ($testing_data !== null) {
9337 - $response_data['testing_data'] = $testing_data;
9338 - //error_log("MxChat Testing: Added testing data to Claude fallback response");
9339 - }
9340 -
9341 - // Clear any streaming headers and send JSON
9342 - if (headers_sent() === false) {
9343 - header('Content-Type: application/json');
9344 - }
9345 - echo json_encode($response_data);
9346 - return true; // Indicate we handled the response
9347 - }
9348 -
9349 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9350 -
9351 - $captured_status_code = 0;
9352 - $captured_body_pre_stream = '';
9353 - $full_response = '';
9354 - $stream_started = false;
9355 - $buffer = '';
9356 - $errno = 0;
9357 - $http_code = 0;
9358 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9359 - $backoff_ms = array(0, 750, 2000);
9360 -
9361 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9362 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9363 - usleep($backoff_ms[$attempt] * 1000);
9364 - }
9365 -
9366 - $captured_status_code = 0;
9367 - $captured_body_pre_stream = '';
9368 - $full_response = '';
9369 - $stream_started = false;
9370 - $buffer = '';
9371 -
9372 - $ch = curl_init();
9373 - curl_setopt($ch, CURLOPT_URL, 'https://api.anthropic.com/v1/messages');
9374 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9375 - curl_setopt($ch, CURLOPT_POST, true);
9376 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9377 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9378 - 'Content-Type: application/json',
9379 - 'x-api-key: ' . $claude_api_key,
9380 - 'anthropic-version: 2023-06-01'
9381 - ));
9382 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9383 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9384 -
9385 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9386 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9387 - $captured_status_code = (int) $m[1];
9388 - }
9389 - return strlen($header);
9390 - });
9391 -
9392 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9393 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9394 - $captured_body_pre_stream .= $data;
9395 - return strlen($data);
9396 - }
9397 -
9398 - if (!$this->streaming_headers_sent) {
9399 - $this->setup_streaming_headers();
9400 - }
9401 -
9402 - if (!$stream_started && $testing_data !== null) {
9403 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9404 - flush();
9405 - $stream_started = true;
9406 - }
9407 -
9408 - $buffer .= $data;
9409 - $lines = explode("\n", $buffer);
9410 - $buffer = array_pop($lines);
9411 -
9412 - foreach ($lines as $line) {
9413 - if (trim($line) === '') {
9414 - continue;
9415 - }
9416 -
9417 - if (strpos($line, 'event: ') === 0) {
9418 - continue;
9419 - }
9420 -
9421 - if (strpos($line, 'data: ') === 0) {
9422 - $json_str = substr($line, 6);
9423 -
9424 - $json = json_decode(trim($json_str), true);
9425 - if (json_last_error() !== JSON_ERROR_NONE) {
9426 - continue;
9427 - }
9428 -
9429 - if (isset($json['type'])) {
9430 - switch ($json['type']) {
9431 - case 'content_block_delta':
9432 - if (isset($json['delta']['text'])) {
9433 - $content = $json['delta']['text'];
9434 - $full_response .= $content;
9435 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9436 - flush();
9437 - }
9438 - break;
9439 -
9440 - case 'message_stop':
9441 - echo "data: [DONE]\n\n";
9442 - flush();
9443 - break;
9444 -
9445 - case 'error':
9446 - echo "data: " . json_encode(['error' => $json['error']['message'] ?? 'Unknown error']) . "\n\n";
9447 - flush();
9448 - break;
9449 - }
9450 - }
9451 - }
9452 - }
9453 -
9454 - return strlen($data);
9455 - });
9456 -
9457 - $response = curl_exec($ch);
9458 - $errno = curl_errno($ch);
9459 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9460 - curl_close($ch);
9461 -
9462 - if (!$errno && $http_code === 200) {
9463 - break;
9464 - }
9465 -
9466 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'anthropic', $errno);
9467 - $can_retry = !$this->streaming_headers_sent
9468 - && ($attempt + 1) < $max_attempts
9469 - && $is_transient;
9470 -
9471 - if (defined('WP_DEBUG') && WP_DEBUG) {
9472 - error_log(sprintf(
9473 - '[MxChat] claude_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9474 - $attempt + 1, $max_attempts, $http_code, $errno,
9475 - $is_transient ? 'yes' : 'no',
9476 - $can_retry ? 'Retrying.' : 'Giving up.'
9477 - ));
9478 - }
9479 -
9480 - if (!$can_retry) {
9481 - break;
9482 - }
9483 - }
9484 -
9485 - if ($errno || $http_code !== 200) {
9486 - return $this->mxchat_stream_emit_fallback(
9487 - 'anthropic',
9488 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, array_slice($conversation_history, 0, -1), $relevant_content, $session_id),
9489 - $session_id,
9490 - $testing_data
9491 - );
9492 - }
9493 -
9494 - // Save the complete response to maintain chat persistence
9495 - if (!empty($full_response) && !empty($session_id)) {
9496 - // Prepare RAG context for streaming response
9497 - $rag_context_for_storage = null;
9498 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9499 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9500 -
9501 - if ($has_rag_data || $has_action_data) {
9502 - $rag_context_for_storage = [];
9503 -
9504 - if ($has_rag_data) {
9505 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9506 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9507 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9508 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9509 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9510 - }
9511 -
9512 - if ($has_action_data) {
9513 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9514 - }
9515 - }
9516 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9517 - }
9518 -
9519 - return true; // Indicate streaming completed successfully
9520 -
9521 - } catch (Exception $e) {
9522 - return $this->mxchat_stream_emit_fallback(
9523 - 'anthropic',
9524 - $this->mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id),
9525 - $session_id,
9526 - $testing_data
9527 - );
9528 - }
9529 -}
9530 -private function mxchat_generate_response_xai_stream($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9531 - try {
9532 - // Get bot ID from session or request
9533 - $bot_id = $this->get_current_bot_id($session_id);
9534 -
9535 - // Get system prompt instructions using centralized function
9536 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9537 -
9538 - // Ensure conversation_history is an array
9539 - if (!is_array($conversation_history)) {
9540 - $conversation_history = array();
9541 - }
9542 -
9543 - // Format conversation history for X.AI (same as OpenAI format)
9544 - $formatted_conversation = array();
9545 -
9546 - $formatted_conversation[] = array(
9547 - 'role' => 'system',
9548 - 'content' => $system_prompt_instructions . " " . $relevant_content
9549 - );
9550 -
9551 - foreach ($conversation_history as $message) {
9552 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9553 - $role = $message['role'];
9554 - if ($role === 'bot' || $role === 'agent') {
9555 - $role = 'assistant';
9556 - }
9557 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9558 - $role = 'user';
9559 - }
9560 - $formatted_conversation[] = array(
9561 - 'role' => $role,
9562 - 'content' => $message['content']
9563 - );
9564 - }
9565 - }
9566 -
9567 - // Check if we can actually stream
9568 - if (headers_sent() || !function_exists('curl_init')) {
9569 - // Fallback to regular response with testing data
9570 - //error_log("MxChat: X.AI streaming not possible, falling back to regular response");
9571 - $regular_response = $this->mxchat_generate_response_xai(
9572 - $selected_model,
9573 - $xai_api_key,
9574 - $conversation_history,
9575 - $relevant_content,
9576 - $session_id
9577 - );
9578 -
9579 - // Save bot response to transcript
9580 - if (!empty($regular_response) && !empty($session_id)) {
9581 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9582 - }
9583 -
9584 - $response_data = [
9585 - 'text' => $regular_response,
9586 - 'html' => '',
9587 - 'session_id' => $session_id
9588 - ];
9589 -
9590 - if ($testing_data !== null) {
9591 - $response_data['testing_data'] = $testing_data;
9592 - //error_log("MxChat Testing: Added testing data to X.AI fallback response");
9593 - }
9594 -
9595 - header('Content-Type: application/json');
9596 - echo json_encode($response_data);
9597 - return true;
9598 - }
9599 -
9600 - // Prepare the request body with stream: true
9601 - $body = json_encode([
9602 - 'model' => $selected_model,
9603 - 'messages' => $formatted_conversation,
9604 - 'temperature' => 0.8,
9605 - 'stream' => true
9606 - ]);
9607 -
9608 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9609 -
9610 - $captured_status_code = 0;
9611 - $captured_body_pre_stream = '';
9612 - $full_response = '';
9613 - $stream_started = false;
9614 - $buffer = '';
9615 - $errno = 0;
9616 - $http_code = 0;
9617 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9618 - $backoff_ms = array(0, 750, 2000);
9619 -
9620 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9621 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9622 - usleep($backoff_ms[$attempt] * 1000);
9623 - }
9624 -
9625 - $captured_status_code = 0;
9626 - $captured_body_pre_stream = '';
9627 - $full_response = '';
9628 - $stream_started = false;
9629 - $buffer = '';
9630 -
9631 - $ch = curl_init();
9632 - curl_setopt($ch, CURLOPT_URL, 'https://api.x.ai/v1/chat/completions');
9633 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9634 - curl_setopt($ch, CURLOPT_POST, true);
9635 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9636 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9637 - 'Content-Type: application/json',
9638 - 'Authorization: Bearer ' . $xai_api_key
9639 - ));
9640 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9641 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9642 -
9643 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9644 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9645 - $captured_status_code = (int) $m[1];
9646 - }
9647 - return strlen($header);
9648 - });
9649 -
9650 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9651 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9652 - $captured_body_pre_stream .= $data;
9653 - return strlen($data);
9654 - }
9655 -
9656 - if (!$this->streaming_headers_sent) {
9657 - $this->setup_streaming_headers();
9658 - }
9659 -
9660 - if (!$stream_started && $testing_data !== null) {
9661 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9662 - flush();
9663 - $stream_started = true;
9664 - }
9665 -
9666 - $buffer .= $data;
9667 - $lines = explode("\n", $buffer);
9668 - $buffer = array_pop($lines);
9669 -
9670 - foreach ($lines as $line) {
9671 - if (trim($line) === '') {
9672 - continue;
9673 - }
9674 - if (strpos($line, 'data: ') !== 0) {
9675 - continue;
9676 - }
9677 -
9678 - $json_str = substr($line, 6);
9679 -
9680 - if (trim($json_str) === '[DONE]') {
9681 - echo "data: [DONE]\n\n";
9682 - flush();
9683 - continue;
9684 - }
9685 -
9686 - $json = json_decode(trim($json_str), true);
9687 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9688 - $content = $json['choices'][0]['delta']['content'];
9689 - $full_response .= $content;
9690 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9691 - flush();
9692 - }
9693 - }
9694 -
9695 - return strlen($data);
9696 - });
9697 -
9698 - $response = curl_exec($ch);
9699 - $errno = curl_errno($ch);
9700 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9701 - curl_close($ch);
9702 -
9703 - if (!$errno && $http_code === 200) {
9704 - break;
9705 - }
9706 -
9707 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'xai', $errno);
9708 - $can_retry = !$this->streaming_headers_sent
9709 - && ($attempt + 1) < $max_attempts
9710 - && $is_transient;
9711 -
9712 - if (defined('WP_DEBUG') && WP_DEBUG) {
9713 - error_log(sprintf(
9714 - '[MxChat] xai_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9715 - $attempt + 1, $max_attempts, $http_code, $errno,
9716 - $is_transient ? 'yes' : 'no',
9717 - $can_retry ? 'Retrying.' : 'Giving up.'
9718 - ));
9719 - }
9720 -
9721 - if (!$can_retry) {
9722 - break;
9723 - }
9724 - }
9725 -
9726 - if ($errno || $http_code !== 200) {
9727 - return $this->mxchat_stream_emit_fallback(
9728 - 'xai',
9729 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id),
9730 - $session_id,
9731 - $testing_data
9732 - );
9733 - }
9734 -
9735 - // Save the complete response to maintain chat persistence
9736 - if (!empty($full_response) && !empty($session_id)) {
9737 - // Prepare RAG context for streaming response
9738 - $rag_context_for_storage = null;
9739 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9740 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9741 -
9742 - if ($has_rag_data || $has_action_data) {
9743 - $rag_context_for_storage = [];
9744 -
9745 - if ($has_rag_data) {
9746 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9747 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9748 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9749 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9750 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9751 - }
9752 -
9753 - if ($has_action_data) {
9754 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9755 - }
9756 - }
9757 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9758 - }
9759 -
9760 - return true; // Indicate streaming completed successfully
9761 -
9762 - } catch (Exception $e) {
9763 - return $this->mxchat_stream_emit_fallback(
9764 - 'xai',
9765 - $this->mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content),
9766 - $session_id,
9767 - $testing_data
9768 - );
9769 - }
9770 -}
9771 -private function mxchat_generate_response_deepseek_stream($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id, $testing_data = null) {
9772 - try {
9773 - // Get bot ID from session or request
9774 - $bot_id = $this->get_current_bot_id($session_id);
9775 -
9776 - // Get system prompt instructions using centralized function
9777 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
9778 -
9779 - // Ensure conversation_history is an array
9780 - if (!is_array($conversation_history)) {
9781 - $conversation_history = array();
9782 - }
9783 -
9784 - // Format conversation history for DeepSeek
9785 - $formatted_conversation = array();
9786 -
9787 - $formatted_conversation[] = array(
9788 - 'role' => 'system',
9789 - 'content' => $system_prompt_instructions . " " . $relevant_content
9790 - );
9791 -
9792 - foreach ($conversation_history as $message) {
9793 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
9794 - $role = $message['role'];
9795 - if ($role === 'bot' || $role === 'agent') {
9796 - $role = 'assistant';
9797 - }
9798 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
9799 - $role = 'user';
9800 - }
9801 - $formatted_conversation[] = array(
9802 - 'role' => $role,
9803 - 'content' => $message['content']
9804 - );
9805 - }
9806 - }
9807 -
9808 - // Check if we can actually stream
9809 - if (headers_sent() || !function_exists('curl_init')) {
9810 - // Fallback to regular response with testing data
9811 - //error_log("MxChat: DeepSeek streaming not possible, falling back to regular response");
9812 - $regular_response = $this->mxchat_generate_response_deepseek(
9813 - $selected_model,
9814 - $deepseek_api_key,
9815 - $conversation_history,
9816 - $relevant_content,
9817 - $session_id
9818 - );
9819 -
9820 - // Save bot response to transcript
9821 - if (!empty($regular_response) && !empty($session_id)) {
9822 - $this->mxchat_save_chat_message($session_id, 'bot', $regular_response);
9823 - }
9824 -
9825 - $response_data = [
9826 - 'text' => $regular_response,
9827 - 'html' => '',
9828 - 'session_id' => $session_id
9829 - ];
9830 -
9831 - if ($testing_data !== null) {
9832 - $response_data['testing_data'] = $testing_data;
9833 - //error_log("MxChat Testing: Added testing data to DeepSeek fallback response");
9834 - }
9835 -
9836 - header('Content-Type: application/json');
9837 - echo json_encode($response_data);
9838 - return true;
9839 - }
9840 -
9841 - // Prepare the request body with stream: true
9842 - $body = json_encode([
9843 - 'model' => $selected_model,
9844 - 'messages' => $formatted_conversation,
9845 - 'temperature' => 0.8,
9846 - 'stream' => true
9847 - ]);
9848 -
9849 - // V2 retry-on-initial-connect: setup_streaming_headers is lazy-fired in WRITEFUNCTION.
9850 -
9851 - $captured_status_code = 0;
9852 - $captured_body_pre_stream = '';
9853 - $full_response = '';
9854 - $stream_started = false;
9855 - $buffer = '';
9856 - $errno = 0;
9857 - $http_code = 0;
9858 - $max_attempts = $this->mxchat_retry_enabled() ? 3 : 1;
9859 - $backoff_ms = array(0, 750, 2000);
9860 -
9861 - for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
9862 - if ($attempt > 0 && $backoff_ms[$attempt] > 0) {
9863 - usleep($backoff_ms[$attempt] * 1000);
9864 - }
9865 -
9866 - $captured_status_code = 0;
9867 - $captured_body_pre_stream = '';
9868 - $full_response = '';
9869 - $stream_started = false;
9870 - $buffer = '';
9871 -
9872 - $ch = curl_init();
9873 - curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
9874 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
9875 - curl_setopt($ch, CURLOPT_POST, true);
9876 - curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
9877 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(
9878 - 'Content-Type: application/json',
9879 - 'Authorization: Bearer ' . $deepseek_api_key
9880 - ));
9881 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
9882 - curl_setopt($ch, CURLOPT_TIMEOUT, 60);
9883 -
9884 - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$captured_status_code) {
9885 - if ($captured_status_code === 0 && preg_match('#^HTTP/\S+\s+(\d+)\b#', $header, $m)) {
9886 - $captured_status_code = (int) $m[1];
9887 - }
9888 - return strlen($header);
9889 - });
9890 -
9891 - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$full_response, &$stream_started, &$buffer, &$captured_status_code, &$captured_body_pre_stream, $testing_data) {
9892 - if ($captured_status_code !== 0 && $captured_status_code !== 200) {
9893 - $captured_body_pre_stream .= $data;
9894 - return strlen($data);
9895 - }
9896 -
9897 - if (!$this->streaming_headers_sent) {
9898 - $this->setup_streaming_headers();
9899 - }
9900 -
9901 - if (!$stream_started && $testing_data !== null) {
9902 - echo "data: " . json_encode(['testing_data' => $testing_data]) . "\n\n";
9903 - flush();
9904 - $stream_started = true;
9905 - }
9906 -
9907 - $buffer .= $data;
9908 - $lines = explode("\n", $buffer);
9909 - $buffer = array_pop($lines);
9910 -
9911 - foreach ($lines as $line) {
9912 - if (trim($line) === '') {
9913 - continue;
9914 - }
9915 - if (strpos($line, 'data: ') !== 0) {
9916 - continue;
9917 - }
9918 -
9919 - $json_str = substr($line, 6);
9920 -
9921 - if (trim($json_str) === '[DONE]') {
9922 - echo "data: [DONE]\n\n";
9923 - flush();
9924 - continue;
9925 - }
9926 -
9927 - $json = json_decode(trim($json_str), true);
9928 - if ($json && isset($json['choices'][0]['delta']['content'])) {
9929 - $content = $json['choices'][0]['delta']['content'];
9930 - $full_response .= $content;
9931 - echo "data: " . json_encode(['content' => $content]) . "\n\n";
9932 - flush();
9933 - }
9934 - }
9935 -
9936 - return strlen($data);
9937 - });
9938 -
9939 - $response = curl_exec($ch);
9940 - $errno = curl_errno($ch);
9941 - $http_code = $captured_status_code !== 0 ? $captured_status_code : (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
9942 - curl_close($ch);
9943 -
9944 - if (!$errno && $http_code === 200) {
9945 - break;
9946 - }
9947 -
9948 - $is_transient = $this->mxchat_is_transient_provider_error_raw($http_code, $captured_body_pre_stream, 'openai', $errno);
9949 - $can_retry = !$this->streaming_headers_sent
9950 - && ($attempt + 1) < $max_attempts
9951 - && $is_transient;
9952 -
9953 - if (defined('WP_DEBUG') && WP_DEBUG) {
9954 - error_log(sprintf(
9955 - '[MxChat] deepseek_stream initial-connect failure (attempt=%d/%d, status=%d, errno=%d, transient=%s, %s).',
9956 - $attempt + 1, $max_attempts, $http_code, $errno,
9957 - $is_transient ? 'yes' : 'no',
9958 - $can_retry ? 'Retrying.' : 'Giving up.'
9959 - ));
9960 - }
9961 -
9962 - if (!$can_retry) {
9963 - break;
9964 - }
9965 - }
9966 -
9967 - if ($errno || $http_code !== 200) {
9968 - return $this->mxchat_stream_emit_fallback(
9969 - 'openai',
9970 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id),
9971 - $session_id,
9972 - $testing_data
9973 - );
9974 - }
9975 -
9976 - // Save the complete response to maintain chat persistence
9977 - if (!empty($full_response) && !empty($session_id)) {
9978 - // Prepare RAG context for streaming response
9979 - $rag_context_for_storage = null;
9980 - $has_rag_data = $this->last_similarity_analysis !== null && !empty($this->last_similarity_analysis['top_matches']);
9981 - $has_action_data = isset($this->last_action_analysis) && !empty($this->last_action_analysis);
9982 -
9983 - if ($has_rag_data || $has_action_data) {
9984 - $rag_context_for_storage = [];
9985 -
9986 - if ($has_rag_data) {
9987 - $rag_context_for_storage['top_matches'] = $this->last_similarity_analysis['top_matches'];
9988 - $rag_context_for_storage['approved_urls'] = $this->current_valid_urls ?? [];
9989 - $rag_context_for_storage['similarity_threshold'] = $this->last_similarity_analysis['threshold_used'] ?? 0.35;
9990 - $rag_context_for_storage['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'] ?? 'WordPress Database';
9991 - $rag_context_for_storage['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
9992 - }
9993 -
9994 - if ($has_action_data) {
9995 - $rag_context_for_storage['action_analysis'] = $this->last_action_analysis;
9996 - }
9997 - }
9998 - $this->mxchat_save_chat_message($session_id, 'bot', $full_response, null, $rag_context_for_storage);
9999 - }
10000 -
10001 - return true; // Indicate streaming completed successfully
10002 -
10003 - } catch (Exception $e) {
10004 - return $this->mxchat_stream_emit_fallback(
10005 - 'openai',
10006 - $this->mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content),
10007 - $session_id,
10008 - $testing_data
10009 - );
10010 - }
10011 -}
10012 -
10013 -
10014 -private function mxchat_generate_response_openrouter($selected_model, $openrouter_api_key, $conversation_history, $relevant_content, $session_id = '') {
10015 - try {
10016 - if (!is_array($conversation_history)) {
10017 - $conversation_history = array();
10018 - }
10019 -
10020 - $bot_id = $this->get_current_bot_id($session_id);
10021 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10022 -
10023 - $formatted_conversation = array();
10024 -
10025 - $formatted_conversation[] = array(
10026 - 'role' => 'system',
10027 - 'content' => $system_prompt_instructions . " " . $relevant_content
10028 - );
10029 -
10030 - foreach ($conversation_history as $message) {
10031 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10032 - $role = $message['role'];
10033 -
10034 - if ($role === 'bot' || $role === 'agent') {
10035 - $role = 'assistant';
10036 - }
10037 - if (!in_array($role, ['system', 'assistant', 'user'])) {
10038 - $role = 'user';
10039 - }
10040 -
10041 - $formatted_conversation[] = array(
10042 - 'role' => $role,
10043 - 'content' => $message['content']
10044 - );
10045 - }
10046 - }
10047 -
10048 - $body = json_encode([
10049 - 'model' => $selected_model,
10050 - 'messages' => $formatted_conversation,
10051 - 'temperature' => 1,
10052 - ]);
10053 -
10054 - $args = [
10055 - 'body' => $body,
10056 - 'headers' => [
10057 - 'Content-Type' => 'application/json',
10058 - 'Authorization' => 'Bearer ' . $openrouter_api_key,
10059 - 'HTTP-Referer' => home_url(),
10060 - 'X-Title' => get_bloginfo('name'),
10061 - ],
10062 - 'timeout' => 60,
10063 - 'redirection' => 5,
10064 - 'blocking' => true,
10065 - 'httpversion' => '1.0',
10066 - 'sslverify' => true,
10067 - ];
10068 -
10069 - $response = $this->mxchat_provider_call_with_retry('https://openrouter.ai/api/v1/chat/completions', $args, 'openai');
10070 -
10071 - if (is_wp_error($response)) {
10072 - $error_message = $response->get_error_message();
10073 - return [
10074 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenRouter'),
10075 - 'error_code' => 'openrouter_connection_error',
10076 - 'provider' => 'openrouter'
10077 - ];
10078 - }
10079 -
10080 - $status_code = wp_remote_retrieve_response_code($response);
10081 - if ($status_code !== 200) {
10082 - $response_body = wp_remote_retrieve_body($response);
10083 - $decoded_response = json_decode($response_body, true);
10084 -
10085 - $error_message = isset($decoded_response['error']['message'])
10086 - ? $decoded_response['error']['message']
10087 - : 'HTTP Error ' . $status_code;
10088 -
10089 - return [
10090 - 'error' => esc_html__('OpenRouter API error: ', 'mxchat') . esc_html($error_message),
10091 - 'error_code' => 'openrouter_api_error',
10092 - 'provider' => 'openrouter',
10093 - 'status_code' => $status_code
10094 - ];
10095 - }
10096 -
10097 - $response_body = wp_remote_retrieve_body($response);
10098 - $decoded_response = json_decode($response_body, true);
10099 -
10100 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10101 - return trim($decoded_response['choices'][0]['message']['content']);
10102 - } else {
10103 - return [
10104 - 'error' => esc_html__('Unexpected response format from OpenRouter.', 'mxchat'),
10105 - 'error_code' => 'openrouter_response_format_error',
10106 - 'provider' => 'openrouter'
10107 - ];
10108 - }
10109 - } catch (Exception $e) {
10110 - return [
10111 - 'error' => esc_html__('System error when processing OpenRouter request: ', 'mxchat') . esc_html($e->getMessage()),
10112 - 'error_code' => 'openrouter_exception',
10113 - 'provider' => 'openrouter'
10114 - ];
10115 - }
10116 -}
10117 -
10118 -/**
10119 - * Build a chat-bubble-safe message for a non-200 provider (chat) error.
10120 - *
10121 - * Visitors must NEVER see raw API internals (model names, key/billing/quota
10122 - * text). Admins (manage_options) get an actionable hint — and, for the common
10123 - * "model not available on this key" case, a direct pointer to change the model
10124 - * (the site owner can fix it in one click). Anthropic returns model-access as a
10125 - * 4xx with a message like "Claude Fable 5 is not available. Please use Opus 4.8."
10126 - *
10127 - * Provider-agnostic by design (reusable for the xai/gemini/deepseek branches),
10128 - * but Anthropic is the confirmed, reproduced case wired up here (plan 1d3b0f).
10129 - *
10130 - * @param int $http_code HTTP status from the provider.
10131 - * @param string $error_message Raw provider error.message (may be empty).
10132 - * @param string $provider_label Human provider name, e.g. 'Anthropic'.
10133 - * @return string Message safe to render as a chat bubble.
10134 - */
10135 -private function mxchat_friendly_chat_error($http_code, $error_message, $provider_label = '') {
10136 - $raw = trim((string) $error_message);
10137 -
10138 - // Detect a model-access / availability problem the site owner can fix by
10139 - // choosing a different model. (Anthropic phrasing + the common API shapes.)
10140 - $low = strtolower($raw);
10141 - $is_model_access = (strpos($low, 'not available') !== false)
10142 - || (strpos($low, 'does not have access') !== false)
10143 - || (strpos($low, 'do not have access') !== false)
10144 - || (strpos($low, 'does not exist') !== false) // OpenAI: "model `x` does not exist or you do not have access"
10145 - || (strpos($low, 'model_not_found') !== false)
10146 - || (strpos($low, 'not_found_error') !== false)
10147 - || (strpos($low, 'model not found') !== false) // xAI
10148 - || (strpos($low, 'not found') !== false) // Gemini: "models/x is not found for API version ..."
10149 - || (strpos($low, 'permission_denied') !== false) // Gemini gated model
10150 - || (strpos($low, 'permission denied') !== false);
10151 -
10152 - if (current_user_can('manage_options')) {
10153 - if ($is_model_access) {
10154 - return $raw !== ''
10155 - ? sprintf(
10156 - /* translators: %s: raw provider error detail */
10157 - esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings. (Details: %s)', 'mxchat'),
10158 - $raw
10159 - )
10160 - : esc_html__('The selected AI model isn\'t available on your API key. Choose another model in MxChat → Settings.', 'mxchat');
10161 - }
10162 - return $raw !== ''
10163 - ? sprintf(
10164 - /* translators: 1: provider label, 2: raw provider error detail */
10165 - esc_html__('The AI provider (%1$s) returned an error: %2$s. Check your model and API key in MxChat → Settings.', 'mxchat'),
10166 - $provider_label !== '' ? $provider_label : esc_html__('AI', 'mxchat'),
10167 - $raw
10168 - )
10169 - : esc_html__('The AI provider returned an error. Check your model and API key in MxChat → Settings.', 'mxchat');
10170 - }
10171 -
10172 - // Visitors: friendly, generic, no internals leaked.
10173 - return esc_html__('Sorry, I\'m having trouble responding right now. Please try again in a moment.', 'mxchat');
10174 -}
10175 -
10176 -private function mxchat_generate_response_claude($selected_model, $claude_api_key, $conversation_history, $relevant_content, $session_id = '') {
10177 - // Anthropic retired claude-opus-4-20250514 / claude-sonnet-4-20250514 on 2026-06-15.
10178 - // Read-time rescue: remap a saved dead ID to the current equivalent before the API call.
10179 - if ($selected_model === 'claude-opus-4-20250514') { $selected_model = 'claude-opus-4-8'; }
10180 - elseif ($selected_model === 'claude-sonnet-4-20250514') { $selected_model = 'claude-sonnet-4-6'; }
10181 -
10182 - // Get bot ID from session or request
10183 - $bot_id = $this->get_current_bot_id($session_id);
10184 -
10185 - // Get system prompt instructions using centralized function
10186 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10187 -
10188 - // Clean and validate conversation history
10189 - foreach ($conversation_history as &$message) {
10190 - // Convert bot and agent roles to assistant
10191 - if ($message['role'] === 'bot' || $message['role'] === 'agent') {
10192 - $message['role'] = 'assistant';
10193 - }
10194 -
10195 - // Remove unsupported roles - Claude only supports 'assistant' and 'user'
10196 - if (!in_array($message['role'], ['assistant', 'user'])) {
10197 - $message['role'] = 'user';
10198 - }
10199 -
10200 - // Ensure content field exists
10201 - if (!isset($message['content']) || empty($message['content'])) {
10202 - $message['content'] = '';
10203 - }
10204 -
10205 - // Remove any unsupported fields
10206 - $message = array_intersect_key($message, array_flip(['role', 'content']));
10207 - }
10208 -
10209 - // Add relevant content as the latest user message
10210 - $conversation_history[] = [
10211 - 'role' => 'user',
10212 - 'content' => $relevant_content
10213 - ];
10214 -
10215 - // Build request body
10216 - $payload = [
10217 - 'model' => $selected_model,
10218 - 'max_tokens' => 1000,
10219 - 'temperature' => 0.8,
10220 - 'messages' => $conversation_history,
10221 - 'system' => $system_prompt_instructions
10222 - ];
10223 - if ($this->mxchat_claude_omits_temperature($selected_model)) { unset($payload['temperature']); }
10224 - $body = json_encode($payload);
10225 -
10226 - // Set up API request
10227 - $args = [
10228 - 'body' => $body,
10229 - 'headers' => [
10230 - 'Content-Type' => 'application/json',
10231 - 'x-api-key' => $claude_api_key,
10232 - 'anthropic-version' => '2023-06-01'
10233 - ],
10234 - 'timeout' => 60,
10235 - 'redirection' => 5,
10236 - 'blocking' => true,
10237 - 'httpversion' => '1.0',
10238 - 'sslverify' => true,
10239 - ];
10240 -
10241 - // Make API request
10242 - $response = $this->mxchat_provider_call_with_retry('https://api.anthropic.com/v1/messages', $args, 'anthropic');
10243 -
10244 - // Check for WordPress errors
10245 - if (is_wp_error($response)) {
10246 - //error_log("Claude API request error: " . $response->get_error_message());
10247 - return "Sorry, there was an error connecting to the API.";
10248 - }
10249 -
10250 - // Check HTTP response code
10251 - $http_code = wp_remote_retrieve_response_code($response);
10252 - if ($http_code !== 200) {
10253 - $error_body = wp_remote_retrieve_body($response);
10254 - //error_log("Claude API HTTP error: " . $http_code . " - " . $error_body);
10255 -
10256 - // Try to extract error message from response
10257 - $error_data = json_decode($error_body, true);
10258 - $error_message = isset($error_data['error']['message']) ?
10259 - $error_data['error']['message'] :
10260 - "HTTP error " . $http_code;
10261 -
10262 - // Surface an admin-actionable message (and a model-change pointer for the
10263 - // model-access case) without leaking raw API internals to visitors. This
10264 - // is the single chokepoint for BOTH the non-streaming and streaming Claude
10265 - // paths (the stream's non-200 fallback re-enters this method). plan 1d3b0f.
10266 - return $this->mxchat_friendly_chat_error($http_code, $error_message, 'Anthropic');
10267 - }
10268 -
10269 - // Parse response
10270 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
10271 -
10272 - // Check for JSON decode errors
10273 - if (json_last_error() !== JSON_ERROR_NONE) {
10274 - //error_log("Claude API JSON decode error: " . json_last_error_msg());
10275 - return "Sorry, there was an error processing the API response.";
10276 - }
10277 -
10278 - // Extract and validate response content. claude-fable-5 prepends a
10279 - // thinking block to content even with no thinking param — take the first
10280 - // TEXT block rather than content[0].
10281 - if (isset($response_body['content']) && is_array($response_body['content'])) {
10282 - foreach ($response_body['content'] as $block) {
10283 - if (isset($block['type'], $block['text']) && $block['type'] === 'text') {
10284 - return trim($block['text']);
10285 - }
10286 - }
10287 - }
10288 -
10289 - // Log unexpected response format
10290 - //error_log("Claude API unexpected response format: " . print_r($response_body, true));
10291 - return "Sorry, I received an unexpected response format from the API.";
10292 -}
10293 -private function mxchat_generate_response_openai($selected_model, $api_key, $conversation_history, $relevant_content, $session_id = '') {
10294 - try {
10295 - // Ensure conversation_history is an array
10296 - if (!is_array($conversation_history)) {
10297 - $conversation_history = array();
10298 - }
10299 -
10300 - // Get bot ID from session or request. plan eb9c38: resolve the real bot
10301 - // from the session (was hardcoded '' → always default bot on multi-bot
10302 - // installs) and fix the undefined $session_id that fed get_system_instructions.
10303 - $bot_id = $this->get_current_bot_id($session_id);
10304 -
10305 - // Get system prompt instructions using centralized function
10306 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10307 -
10308 - // Create a new array for the formatted conversation
10309 - $formatted_conversation = array();
10310 -
10311 - // Add system message first
10312 - $formatted_conversation[] = array(
10313 - 'role' => 'system',
10314 - 'content' => $system_prompt_instructions . " " . $relevant_content
10315 - );
10316 -
10317 - // Add the rest of the conversation history
10318 - foreach ($conversation_history as $message) {
10319 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10320 - $role = $message['role'];
10321 -
10322 - // Convert roles to supported format
10323 - if ($role === 'bot' || $role === 'agent') {
10324 - $role = 'assistant';
10325 - }
10326 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10327 - $role = 'user';
10328 - }
10329 -
10330 - $formatted_conversation[] = array(
10331 - 'role' => $role,
10332 - 'content' => $message['content']
10333 - );
10334 - }
10335 - }
10336 -
10337 - // Check if this is a GPT-5 model (supports reasoning_effort parameter)
10338 - $is_gpt5_model = (
10339 - strpos($selected_model, 'gpt-5') === 0 ||
10340 - $selected_model === 'gpt-5.2' ||
10341 - $selected_model === 'gpt-5.1-2025-11-13' ||
10342 - $selected_model === 'gpt-5' ||
10343 - $selected_model === 'gpt-5-mini' ||
10344 - $selected_model === 'gpt-5-nano'
10345 - );
10346 -
10347 - // Build request body with optimal settings for fast responses
10348 - $request_body = [
10349 - 'model' => $selected_model,
10350 - 'messages' => $formatted_conversation,
10351 - 'temperature' => 1,
10352 - 'stream' => false
10353 - ];
10354 -
10355 - // Add reasoning_effort only for GPT-5 models that support it
10356 - // These chat models don't support reasoning_effort parameter
10357 - $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');
10358 - if ($is_gpt5_model && !in_array($selected_model, $no_reasoning_models, true)) {
10359 - // GPT-5.1 uses 'low' instead of 'minimal'
10360 - if ($selected_model === 'gpt-5.1-2025-11-13') {
10361 - $request_body['reasoning_effort'] = 'low';
10362 - } elseif ($selected_model === 'gpt-5.5') {
10363 - $request_body['reasoning_effort'] = 'none';
10364 - } elseif ($selected_model === 'gpt-5.4') {
10365 - $request_body['reasoning_effort'] = 'none';
10366 - } else {
10367 - $request_body['reasoning_effort'] = 'minimal';
10368 - }
10369 - }
10370 -
10371 - $body = json_encode($request_body);
10372 -
10373 - $args = [
10374 - 'body' => $body,
10375 - 'headers' => [
10376 - 'Content-Type' => 'application/json',
10377 - 'Authorization' => 'Bearer ' . $api_key,
10378 - ],
10379 - 'timeout' => 60,
10380 - 'redirection' => 5,
10381 - 'blocking' => true,
10382 - 'httpversion' => '1.0',
10383 - 'sslverify' => true,
10384 - ];
10385 -
10386 - $response = $this->mxchat_provider_call_with_retry('https://api.openai.com/v1/chat/completions', $args, 'openai');
10387 -
10388 - if (is_wp_error($response)) {
10389 - $error_message = $response->get_error_message();
10390 - return [
10391 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'OpenAI'),
10392 - 'error_code' => 'openai_connection_error',
10393 - 'provider' => 'openai'
10394 - ];
10395 - }
10396 -
10397 - $status_code = wp_remote_retrieve_response_code($response);
10398 - if ($status_code !== 200) {
10399 - $response_body = wp_remote_retrieve_body($response);
10400 - $decoded_response = json_decode($response_body, true);
10401 -
10402 - $error_message = isset($decoded_response['error']['message'])
10403 - ? $decoded_response['error']['message']
10404 - : 'HTTP Error ' . $status_code;
10405 -
10406 - $error_type = isset($decoded_response['error']['type'])
10407 - ? $decoded_response['error']['type']
10408 - : 'unknown';
10409 -
10410 - // Handle specific error types
10411 - switch ($error_type) {
10412 - case 'invalid_request_error':
10413 - if (strpos($error_message, 'API key') !== false) {
10414 - return [
10415 - 'error' => esc_html__('Invalid OpenAI API key. Please check your API key configuration.', 'mxchat'),
10416 - 'error_code' => 'openai_invalid_api_key',
10417 - 'provider' => 'openai'
10418 - ];
10419 - }
10420 - break;
10421 -
10422 - case 'authentication_error':
10423 - return [
10424 - 'error' => esc_html__('Authentication failed with OpenAI. Please check your API key.', 'mxchat'),
10425 - 'error_code' => 'openai_auth_error',
10426 - 'provider' => 'openai'
10427 - ];
10428 -
10429 - case 'rate_limit_exceeded':
10430 - return [
10431 - 'error' => esc_html__('OpenAI rate limit exceeded. Please try again later.', 'mxchat'),
10432 - 'error_code' => 'openai_rate_limit',
10433 - 'provider' => 'openai'
10434 - ];
10435 -
10436 - case 'quota_exceeded':
10437 - return [
10438 - 'error' => esc_html__('OpenAI API quota exceeded. Please check your billing details.', 'mxchat'),
10439 - 'error_code' => 'openai_quota_exceeded',
10440 - 'provider' => 'openai'
10441 - ];
10442 - }
10443 -
10444 - // Generic error fallback only — the typed cases above already produce
10445 - // clean messages. Route the raw-tail generic case through the leak-safe
10446 - // helper so visitors never see provider internals. plan 5da59a.
10447 - return [
10448 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'OpenAI'),
10449 - 'error_code' => 'openai_api_error',
10450 - 'provider' => 'openai',
10451 - 'status_code' => $status_code
10452 - ];
10453 - }
10454 -
10455 - $response_body = wp_remote_retrieve_body($response);
10456 - $decoded_response = json_decode($response_body, true);
10457 -
10458 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10459 - return trim($decoded_response['choices'][0]['message']['content']);
10460 - } else {
10461 - return [
10462 - 'error' => esc_html__('Unexpected response format from OpenAI.', 'mxchat'),
10463 - 'error_code' => 'openai_response_format_error',
10464 - 'provider' => 'openai'
10465 - ];
10466 - }
10467 - } catch (Exception $e) {
10468 - return [
10469 - 'error' => esc_html__('System error when processing OpenAI request: ', 'mxchat') . esc_html($e->getMessage()),
10470 - 'error_code' => 'openai_exception',
10471 - 'provider' => 'openai'
10472 - ];
10473 - }
10474 -}
10475 -
10476 -private function mxchat_generate_response_xai($selected_model, $xai_api_key, $conversation_history, $relevant_content, $session_id = '') {
10477 - try {
10478 - // Get bot ID from session or request
10479 - $bot_id = $this->get_current_bot_id($session_id);
10480 -
10481 - // Get system prompt instructions using centralized function
10482 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10483 -
10484 - // Add system prompt to relevant content
10485 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10486 -
10487 - // Prepend system instructions to the conversation history
10488 - array_unshift($conversation_history, [
10489 - 'role' => 'system',
10490 - 'content' => "Here are your instructions: " . $content_with_instructions
10491 - ]);
10492 -
10493 - // Ensure consistency: Replace 'bot' and 'agent' roles with supported values
10494 - foreach ($conversation_history as &$message) {
10495 - if ($message['role'] === 'bot') {
10496 - $message['role'] = 'assistant';
10497 - } elseif ($message['role'] === 'agent') {
10498 - // Tag the message as coming from a live agent
10499 - $message['role'] = 'assistant';
10500 - if (!isset($message['metadata'])) {
10501 - $message['metadata'] = ['source' => 'live_agent'];
10502 - }
10503 - }
10504 -
10505 - // Ensure all roles are valid
10506 - if (!in_array($message['role'], ['system', 'assistant', 'user', 'function', 'tool'])) {
10507 - $message['role'] = 'user'; // Default to 'user'
10508 - }
10509 - }
10510 -
10511 - // Build the request body
10512 - $body = json_encode([
10513 - 'model' => $selected_model,
10514 - 'messages' => $conversation_history,
10515 - 'temperature' => 0.8,
10516 - 'stream' => false
10517 - ]);
10518 -
10519 - // Set up the API request
10520 - $args = [
10521 - 'body' => $body,
10522 - 'headers' => [
10523 - 'Content-Type' => 'application/json',
10524 - 'Authorization' => 'Bearer ' . $xai_api_key,
10525 - ],
10526 - 'timeout' => 60,
10527 - 'redirection' => 5,
10528 - 'blocking' => true,
10529 - 'httpversion' => '1.0',
10530 - 'sslverify' => true,
10531 - ];
10532 -
10533 - // Make the API request
10534 - $response = $this->mxchat_provider_call_with_retry('https://api.x.ai/v1/chat/completions', $args, 'xai');
10535 -
10536 - // Process the response
10537 - if (is_wp_error($response)) {
10538 - $error_message = $response->get_error_message();
10539 - //error_log('X.AI API Error: ' . $error_message);
10540 - return [
10541 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'X.AI'),
10542 - 'error_code' => 'xai_connection_error',
10543 - 'provider' => 'xai'
10544 - ];
10545 - }
10546 -
10547 - $status_code = wp_remote_retrieve_response_code($response);
10548 - if ($status_code !== 200) {
10549 - $response_body = wp_remote_retrieve_body($response);
10550 - $decoded_response = json_decode($response_body, true);
10551 -
10552 - // Log the full response for debugging
10553 - //error_log('X.AI Error Response: ' . print_r($decoded_response, true));
10554 -
10555 - // Extract error message from X.AI's specific format
10556 - $error_message = '';
10557 -
10558 - // Check for direct error string (as seen in your logs)
10559 - if (isset($decoded_response['error']) && is_string($decoded_response['error'])) {
10560 - $error_message = $decoded_response['error'];
10561 - }
10562 - // Check for nested error object (OpenAI style)
10563 - elseif (isset($decoded_response['error']['message'])) {
10564 - $error_message = $decoded_response['error']['message'];
10565 - }
10566 - // Check for top-level message
10567 - elseif (isset($decoded_response['message'])) {
10568 - $error_message = $decoded_response['message'];
10569 - }
10570 - // Fallback
10571 - else {
10572 - $error_message = 'HTTP Error ' . $status_code;
10573 - }
10574 -
10575 - //error_log('X.AI API HTTP Error: ' . $status_code . ' - ' . $error_message);
10576 -
10577 - // Check for API key errors using string matching
10578 - if (stripos($error_message, 'api key') !== false ||
10579 - stripos($error_message, 'incorrect api key') !== false ||
10580 - stripos($error_message, 'invalid api key') !== false) {
10581 - return [
10582 - 'error' => esc_html__('Invalid X.AI API key. Please check your API key configuration.', 'mxchat'),
10583 - 'error_code' => 'xai_invalid_api_key',
10584 - 'provider' => 'xai'
10585 - ];
10586 - }
10587 -
10588 - // Authentication errors
10589 - if ($status_code === 401 || $status_code === 403 ||
10590 - stripos($error_message, 'auth') !== false) {
10591 - return [
10592 - 'error' => esc_html__('Authentication failed with X.AI. Please check your API key.', 'mxchat'),
10593 - 'error_code' => 'xai_auth_error',
10594 - 'provider' => 'xai'
10595 - ];
10596 - }
10597 -
10598 - // Model errors
10599 - if (stripos($error_message, 'model') !== false) {
10600 - return [
10601 - 'error' => esc_html__('Invalid model specified for X.AI. Please check your model configuration.', 'mxchat'),
10602 - 'error_code' => 'xai_invalid_model',
10603 - 'provider' => 'xai'
10604 - ];
10605 - }
10606 -
10607 - // Rate limit errors
10608 - if ($status_code === 429 ||
10609 - stripos($error_message, 'rate') !== false ||
10610 - stripos($error_message, 'limit') !== false) {
10611 - return [
10612 - 'error' => esc_html__('X.AI rate limit exceeded. Please try again later.', 'mxchat'),
10613 - 'error_code' => 'xai_rate_limit',
10614 - 'provider' => 'xai'
10615 - ];
10616 - }
10617 -
10618 - // Quota errors
10619 - if (stripos($error_message, 'quota') !== false ||
10620 - stripos($error_message, 'billing') !== false) {
10621 - return [
10622 - 'error' => esc_html__('X.AI API quota exceeded. Please check your billing details.', 'mxchat'),
10623 - 'error_code' => 'xai_quota_exceeded',
10624 - 'provider' => 'xai'
10625 - ];
10626 - }
10627 -
10628 - // Server errors
10629 - if ($status_code >= 500) {
10630 - return [
10631 - 'error' => esc_html__('X.AI service is currently unavailable. Please try again later.', 'mxchat'),
10632 - 'error_code' => 'xai_service_unavailable',
10633 - 'provider' => 'xai'
10634 - ];
10635 - }
10636 -
10637 - // Generic error fallback. Route the user-facing text through the
10638 - // leak-safe helper (admins get an actionable hint, visitors a generic
10639 - // fallback) instead of echoing raw provider internals. Preserve the
10640 - // structured contract (error_code/provider/status_code) for logging. plan 5da59a.
10641 - return [
10642 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'xAI'),
10643 - 'error_code' => 'xai_api_error',
10644 - 'provider' => 'xai',
10645 - 'status_code' => $status_code
10646 - ];
10647 - }
10648 -
10649 - $response_body = wp_remote_retrieve_body($response);
10650 - $decoded_response = json_decode($response_body, true);
10651 -
10652 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10653 - return trim($decoded_response['choices'][0]['message']['content']);
10654 - } else {
10655 - //error_log('X.AI API Response Format Error: ' . print_r($decoded_response, true));
10656 - return [
10657 - 'error' => esc_html__('Unexpected response format from X.AI.', 'mxchat'),
10658 - 'error_code' => 'xai_response_format_error',
10659 - 'provider' => 'xai'
10660 - ];
10661 - }
10662 -} catch (Exception $e) {
10663 - //error_log('X.AI Exception: ' . $e->getMessage());
10664 - return [
10665 - 'error' => esc_html__('System error when processing X.AI request: ', 'mxchat') . esc_html($e->getMessage()),
10666 - 'error_code' => 'xai_exception',
10667 - 'provider' => 'xai'
10668 - ];
10669 -}
10670 -
10671 -
10672 -}
10673 -private function mxchat_generate_response_deepseek($selected_model, $deepseek_api_key, $conversation_history, $relevant_content, $session_id = '') {
10674 - try {
10675 - // Ensure conversation_history is an array
10676 - if (!is_array($conversation_history)) {
10677 - $conversation_history = array();
10678 - }
10679 -
10680 - // Get bot ID from session or request
10681 - $bot_id = $this->get_current_bot_id($session_id);
10682 -
10683 - // Get system prompt instructions using centralized function
10684 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10685 -
10686 - // Create a new array for the formatted conversation
10687 - $formatted_conversation = array();
10688 -
10689 - // Add system message first
10690 - $formatted_conversation[] = array(
10691 - 'role' => 'system',
10692 - 'content' => $system_prompt_instructions . " " . $relevant_content
10693 - );
10694 -
10695 - // Add the rest of the conversation history
10696 - foreach ($conversation_history as $message) {
10697 - if (is_array($message) && isset($message['role']) && isset($message['content'])) {
10698 - $role = $message['role'];
10699 -
10700 - // Convert roles to supported format
10701 - if ($role === 'bot' || $role === 'agent') {
10702 - $role = 'assistant';
10703 - }
10704 - if (!in_array($role, ['system', 'assistant', 'user', 'function', 'tool'])) {
10705 - $role = 'user';
10706 - }
10707 -
10708 - $formatted_conversation[] = array(
10709 - 'role' => $role,
10710 - 'content' => $message['content']
10711 - );
10712 - }
10713 - }
10714 -
10715 - $body = json_encode([
10716 - 'model' => $selected_model,
10717 - 'messages' => $formatted_conversation,
10718 - 'temperature' => 0.8,
10719 - 'stream' => false
10720 - ]);
10721 -
10722 - $args = [
10723 - 'body' => $body,
10724 - 'headers' => [
10725 - 'Content-Type' => 'application/json',
10726 - 'Authorization' => 'Bearer ' . $deepseek_api_key,
10727 - ],
10728 - 'timeout' => 60,
10729 - 'redirection' => 5,
10730 - 'blocking' => true,
10731 - 'httpversion' => '1.0',
10732 - 'sslverify' => true,
10733 - ];
10734 -
10735 - $response = $this->mxchat_provider_call_with_retry('https://api.deepseek.com/v1/chat/completions', $args, 'openai');
10736 -
10737 - if (is_wp_error($response)) {
10738 - $error_message = $response->get_error_message();
10739 - //error_log('DeepSeek API Error: ' . $error_message);
10740 - return [
10741 - 'error' => $this->mxchat_friendly_chat_error(0, $error_message, 'DeepSeek'),
10742 - 'error_code' => 'deepseek_connection_error',
10743 - 'provider' => 'deepseek'
10744 - ];
10745 - }
10746 -
10747 - $status_code = wp_remote_retrieve_response_code($response);
10748 - if ($status_code !== 200) {
10749 - $response_body = wp_remote_retrieve_body($response);
10750 - $decoded_response = json_decode($response_body, true);
10751 -
10752 - $error_message = isset($decoded_response['error']['message'])
10753 - ? $decoded_response['error']['message']
10754 - : 'HTTP Error ' . $status_code;
10755 -
10756 - $error_type = isset($decoded_response['error']['type'])
10757 - ? $decoded_response['error']['type']
10758 - : 'unknown';
10759 -
10760 - //error_log('DeepSeek API HTTP Error: ' . $status_code . ' - ' . $error_message);
10761 -
10762 - // Handle specific error types
10763 - switch ($status_code) {
10764 - case 401:
10765 - return [
10766 - 'error' => esc_html__('Authentication failed with DeepSeek. Please check your API key.', 'mxchat'),
10767 - 'error_code' => 'deepseek_auth_error',
10768 - 'provider' => 'deepseek'
10769 - ];
10770 -
10771 - case 400:
10772 - if (strpos($error_message, 'API key') !== false) {
10773 - return [
10774 - 'error' => esc_html__('Invalid DeepSeek API key. Please check your API key configuration.', 'mxchat'),
10775 - 'error_code' => 'deepseek_invalid_api_key',
10776 - 'provider' => 'deepseek'
10777 - ];
10778 - }
10779 - break;
10780 -
10781 - case 429:
10782 - if (strpos($error_message, 'quota') !== false) {
10783 - return [
10784 - 'error' => esc_html__('DeepSeek API quota exceeded. Please check your billing details.', 'mxchat'),
10785 - 'error_code' => 'deepseek_quota_exceeded',
10786 - 'provider' => 'deepseek'
10787 - ];
10788 - } else {
10789 - return [
10790 - 'error' => esc_html__('DeepSeek rate limit exceeded. Please try again later.', 'mxchat'),
10791 - 'error_code' => 'deepseek_rate_limit',
10792 - 'provider' => 'deepseek'
10793 - ];
10794 - }
10795 -
10796 - case 500:
10797 - case 502:
10798 - case 503:
10799 - case 504:
10800 - return [
10801 - 'error' => esc_html__('DeepSeek service is currently unavailable. Please try again later.', 'mxchat'),
10802 - 'error_code' => 'deepseek_service_unavailable',
10803 - 'provider' => 'deepseek'
10804 - ];
10805 - }
10806 -
10807 - // Generic error fallback — leak-safe helper (see plan 5da59a / 1d3b0f).
10808 - return [
10809 - 'error' => $this->mxchat_friendly_chat_error($status_code, $error_message, 'DeepSeek'),
10810 - 'error_code' => 'deepseek_api_error',
10811 - 'provider' => 'deepseek',
10812 - 'status_code' => $status_code
10813 - ];
10814 - }
10815 -
10816 - $response_body = wp_remote_retrieve_body($response);
10817 - $decoded_response = json_decode($response_body, true);
10818 -
10819 - if (isset($decoded_response['choices'][0]['message']['content'])) {
10820 - return trim($decoded_response['choices'][0]['message']['content']);
10821 - } else {
10822 - //error_log('DeepSeek API Response Format Error: ' . print_r($decoded_response, true));
10823 - return [
10824 - 'error' => esc_html__('Unexpected response format from DeepSeek.', 'mxchat'),
10825 - 'error_code' => 'deepseek_response_format_error',
10826 - 'provider' => 'deepseek'
10827 - ];
10828 - }
10829 - } catch (Exception $e) {
10830 - //error_log('DeepSeek Exception: ' . $e->getMessage());
10831 - return [
10832 - 'error' => esc_html__('System error when processing DeepSeek request: ', 'mxchat') . esc_html($e->getMessage()),
10833 - 'error_code' => 'deepseek_exception',
10834 - 'provider' => 'deepseek'
10835 - ];
10836 - }
10837 -}
10838 -private function mxchat_generate_response_gemini($selected_model, $gemini_api_key, $conversation_history, $relevant_content, $session_id = '') {
10839 - // Read-time remap: gemini-3-pro-preview was shut down March 9, 2026.
10840 - // Auto-rescue existing installs whose saved model is the dead ID.
10841 - if ($selected_model === 'gemini-3-pro-preview') {
10842 - $selected_model = 'gemini-3.1-pro-preview';
10843 - }
10844 - // Get bot ID from session or request
10845 - $bot_id = $this->get_current_bot_id($session_id);
10846 -
10847 - // Get system prompt instructions using centralized function
10848 - $system_prompt_instructions = $this->get_system_instructions($bot_id, $session_id);
10849 -
10850 - // Add system prompt to relevant content
10851 - $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
10852 -
10853 - // Format messages for Gemini API
10854 - $formatted_messages = [];
10855 -
10856 - // Add system message as the first user message with role prefix
10857 - // Note: Gemini doesn't have a dedicated system role, so we use a prefixed user message
10858 - $formatted_messages[] = [
10859 - 'role' => 'user',
10860 - 'parts' => [
10861 - ['text' => "[System Instructions] " . $content_with_instructions]
10862 - ]
10863 - ];
10864 -
10865 - // Add model response to acknowledge system instructions
10866 - $formatted_messages[] = [
10867 - 'role' => 'model',
10868 - 'parts' => [
10869 - ['text' => "I understand and will follow these instructions."]
10870 - ]
10871 - ];
10872 -
10873 - // Process the rest of the conversation history
10874 - $current_role = null;
10875 - $current_parts = [];
10876 -
10877 - foreach ($conversation_history as $message) {
10878 - // Skip the first system message as we already handled it
10879 - if ($message['role'] === 'system') {
10880 - continue;
10881 - }
10882 -
10883 - // Map roles to Gemini format
10884 - $gemini_role = '';
10885 - if ($message['role'] === 'user') {
10886 - $gemini_role = 'user';
10887 - } else if (in_array($message['role'], ['assistant', 'bot', 'agent'])) {
10888 - $gemini_role = 'model';
10889 - } else {
10890 - // Skip unsupported roles
10891 - continue;
10892 - }
10893 -
10894 - // If we have a new role, add the previous message
10895 - if ($current_role !== null && $current_role !== $gemini_role && !empty($current_parts)) {
10896 - $formatted_messages[] = [
10897 - 'role' => $current_role,
10898 - 'parts' => $current_parts
10899 - ];
10900 - $current_parts = [];
10901 - }
10902 -
10903 - // Set current role and add text to parts
10904 - $current_role = $gemini_role;
10905 - $current_parts[] = ['text' => $message['content']];
10906 - }
10907 -
10908 - // Add the last message if there's content
10909 - if ($current_role !== null && !empty($current_parts)) {
10910 - $formatted_messages[] = [
10911 - 'role' => $current_role,
10912 - 'parts' => $current_parts
10913 - ];
10914 - }
10915 -
10916 - // Built-in Web Search grounding for Gemini (plan 46b9ea).
10917 - // The enable_web_search toggle historically routed ONLY to OpenAI's web_search
10918 - // tool; for a Gemini chat model it was a silent no-op. Gemini grounds natively
10919 - // (and free) via the Google Search tool, so when the toggle is on we attach it
10920 - // here on the PLAIN dispatch path. The function-calling loop (mxchat_fc_loop_gemini)
10921 - // is a SEPARATE path reached only when AI Tools are active, so grounding here
10922 - // never double-fires with function calling.
10923 - $web_search_enabled = isset($this->options['enable_web_search']) && $this->options['enable_web_search'] === 'on';
10924 - // Gemini ids that do NOT support Google Search grounding (none today — every
10925 - // shipped chat model is 2.x/3.x and grounds natively). Kept as the explicit
10926 - // opt-out list mirroring the OpenAI $unsupported_web_search_models pattern.
10927 - $gemini_unsupported_grounding = array();
10928 - $grounding_active = $web_search_enabled && !in_array($selected_model, $gemini_unsupported_grounding, true);
10929 -
10930 - // Build the request body
10931 - $request_payload = [
10932 - 'contents' => $formatted_messages,
10933 - 'generationConfig' => [
10934 - 'temperature' => 0.7,
10935 - 'topP' => 0.95,
10936 - 'topK' => 40,
10937 - 'maxOutputTokens' => 8192,
10938 - ],
10939 - 'safetySettings' => [
10940 - [
10941 - 'category' => 'HARM_CATEGORY_HARASSMENT',
10942 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10943 - ],
10944 - [
10945 - 'category' => 'HARM_CATEGORY_HATE_SPEECH',
10946 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10947 - ],
10948 - [
10949 - 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
10950 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10951 - ],
10952 - [
10953 - 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT',
10954 - 'threshold' => 'BLOCK_MEDIUM_AND_ABOVE'
10955 - ]
10956 - ]
10957 - ];
10958 -
10959 - if ($grounding_active) {
10960 - // Gemini 1.5 used the older google_search_retrieval shape; 2.0+ uses the
10961 - // bare google_search tool. Branch by model family so a future 1.5 id still
10962 - // grounds (no 1.5 ships today, so this resolves to google_search). The empty
10963 - // tool config must serialize as a JSON object {}, not an array [].
10964 - if (strpos($selected_model, 'gemini-1.5') !== false) {
10965 - $request_payload['tools'] = [ ['google_search_retrieval' => new \stdClass()] ];
10966 - } else {
10967 - $request_payload['tools'] = [ ['google_search' => new \stdClass()] ];
10968 - }
10969 - }
10970 -
10971 - $body = json_encode($request_payload);
10972 -
10973 - // Prepare the API endpoint
10974 - // Use v1beta for preview models (Gemini 3, experimental), v1 for stable models.
10975 - // Grounding (the google_search tool) is a v1beta feature, so force v1beta whenever
10976 - // it's active — otherwise a stable model on v1 would silently drop the tool.
10977 - $api_version = ($grounding_active || strpos($selected_model, 'preview') !== false || strpos($selected_model, 'exp') !== false) ? 'v1beta' : 'v1';
10978 - $api_endpoint = 'https://generativelanguage.googleapis.com/' . $api_version . '/models/' . $selected_model . ':generateContent?key=' . $gemini_api_key;
10979 -
10980 - // Set up the API request
10981 - $args = [
10982 - 'body' => $body,
10983 - 'headers' => [
10984 - 'Content-Type' => 'application/json',
10985 - ],
10986 - 'timeout' => 60,
10987 - 'redirection' => 5,
10988 - 'blocking' => true,
10989 - 'httpversion' => '1.0',
10990 - 'sslverify' => true,
10991 - ];
10992 -
10993 - // Make the API request
10994 - $response = $this->mxchat_provider_call_with_retry($api_endpoint, $args, 'gemini');
10995 -
10996 - // Process the response
10997 - if (is_wp_error($response)) {
10998 - // plan b13282: route the transport-error string through the leak-safe helper
10999 - // (admin-actionable, generic for visitors) instead of echoing the raw WP HTTP
11000 - // error. http_code 0 = no HTTP response, so the helper uses the generic branch.
11001 - return $this->mxchat_friendly_chat_error(0, $response->get_error_message(), 'Gemini');
11002 - }
11003 -
11004 - $response_body = json_decode(wp_remote_retrieve_body($response), true);
11005 -
11006 - // Handle potential errors in the response. Gemini surfaces errors as a
11007 - // 200/non-200 body with an `error` envelope; route the user-facing text
11008 - // through the leak-safe helper (admin-actionable, no visitor leak) rather
11009 - // than echoing the raw provider message. plan 5da59a.
11010 - if (isset($response_body['error'])) {
11011 - //error_log('Gemini API Error: ' . json_encode($response_body['error']));
11012 - $gemini_error_message = isset($response_body['error']['message'])
11013 - ? $response_body['error']['message']
11014 - : 'Unknown error';
11015 - $gemini_http_code = wp_remote_retrieve_response_code($response);
11016 - return $this->mxchat_friendly_chat_error($gemini_http_code, $gemini_error_message, 'Gemini');
11017 - }
11018 -
11019 - // Extract the response text
11020 - if (isset($response_body['candidates'][0]['content']['parts'][0]['text'])) {
11021 - return trim($response_body['candidates'][0]['content']['parts'][0]['text']);
11022 - } else {
11023 - //error_log('Unexpected Gemini API response format: ' . json_encode($response_body));
11024 - return "Sorry, I couldn't process that request. The response format was unexpected.";
11025 - }
11026 -}
11027 -
11028 -
11029 -public function test_streaming_request() {
11030 - $options = get_option('mxchat_options', []);
11031 - $model = $options['model'] ?? 'gpt-5.1-chat-latest';
11032 -
11033 - // Detect provider from model prefix
11034 - $provider = strtolower(explode('-', $model)[0]);
11035 -
11036 - $sample_prompt = 'Hello! Can you stream this response back to me?';
11037 - $messages = [['role' => 'user', 'content' => $sample_prompt]];
11038 - $headers = [];
11039 - $body = [];
11040 - $url = '';
11041 - $api_key = '';
11042 -
11043 - switch ($provider) {
11044 - case 'gpt':
11045 - case 'o1':
11046 - $api_key = $options['api_key'] ?? '';
11047 - if (empty($api_key)) return '❌ Missing API key for OpenAI';
11048 - $url = 'https://api.openai.com/v1/chat/completions';
11049 - $headers = [
11050 - 'Content-Type: application/json',
11051 - 'Authorization: Bearer ' . $api_key
11052 - ];
11053 - $body = [
11054 - 'model' => $model,
11055 - 'messages' => $messages,
11056 - 'stream' => true
11057 - ];
11058 - break;
11059 -
11060 - case 'claude':
11061 - $api_key = $options['claude_api_key'] ?? '';
11062 - if (empty($api_key)) return '❌ Missing API key for Claude';
11063 - $url = 'https://api.anthropic.com/v1/messages';
11064 - $headers = [
11065 - 'Content-Type: application/json',
11066 - 'x-api-key: ' . $api_key,
11067 - 'anthropic-version: 2023-06-01'
11068 - ];
11069 - $body = [
11070 - 'model' => $model,
11071 - 'messages' => $messages,
11072 - 'max_tokens' => 100,
11073 - 'stream' => true
11074 - ];
11075 - break;
11076 -
11077 - case 'grok':
11078 - $api_key = $options['xai_api_key'] ?? '';
11079 - if (empty($api_key)) return '❌ Missing API key for X.AI';
11080 - $url = 'https://api.x.ai/v1/chat/completions';
11081 - $headers = [
11082 - 'Content-Type: application/json',
11083 - 'Authorization: Bearer ' . $api_key
11084 - ];
11085 - $body = [
11086 - 'model' => $model,
11087 - 'messages' => $messages,
11088 - 'stream' => true
11089 - ];
11090 - break;
11091 -
11092 - case 'deepseek':
11093 - if (empty($deepseek_api_key)) {
11094 - $error_response = [
11095 - 'error' => esc_html__('DeepSeek API key is not configured', 'mxchat'),
11096 - 'error_code' => 'missing_deepseek_api_key'
11097 - ];
11098 - if ($testing_data !== null) {
11099 - $error_response['testing_data'] = $testing_data;
11100 - }
11101 - return $error_response;
11102 - }
11103 - if ($streaming) {
11104 - return $this->mxchat_generate_response_deepseek_stream(
11105 - $selected_model,
11106 - $deepseek_api_key,
11107 - $conversation_history,
11108 - $relevant_content,
11109 - $session_id,
11110 - $testing_data // Pass testing data
11111 - );
11112 - } else {
11113 - $response = $this->mxchat_generate_response_deepseek(
11114 - $selected_model,
11115 - $deepseek_api_key,
11116 - $conversation_history,
11117 - $relevant_content,
11118 - $session_id
11119 - );
11120 - }
11121 - break;
11122 -
11123 - case 'gemini':
11124 - $api_key = $options['gemini_api_key'] ?? '';
11125 - if (empty($api_key)) return '❌ Missing API key for Gemini';
11126 - $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':streamGenerateContent?key=' . $api_key;
11127 - $headers = ['Content-Type: application/json'];
11128 - $body = [
11129 - 'contents' => [['role' => 'user', 'parts' => [['text' => $sample_prompt]]]],
11130 - 'generationConfig' => ['temperature' => 0.7]
11131 - ];
11132 - break;
11133 -
11134 - default:
11135 - return '❌ Unsupported provider: ' . $provider;
11136 - }
11137 -
11138 - // Do the actual streaming test
11139 - $ch = curl_init($url);
11140 - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
11141 - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
11142 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
11143 - curl_setopt($ch, CURLOPT_TIMEOUT, 15);
11144 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
11145 -
11146 - $response = curl_exec($ch);
11147 - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
11148 - $error = curl_error($ch);
11149 - curl_close($ch);
11150 -
11151 - if ($error) return "❌ cURL error: $error";
11152 - if ($http_code !== 200) {
11153 - $error_message = json_decode($response, true)['error']['message'] ?? 'Unknown';
11154 - return "❌ HTTP $http_code: $error_message";
11155 - }
11156 -
11157 - return true;
11158 -}
11159 -
11160 -public function mxchat_dismiss_pre_chat_message() {
11161 - // Get and sanitize the user identifier
11162 - $user_id = $this->mxchat_get_user_identifier();
11163 - $user_id = sanitize_key($user_id);
11164 -
11165 - // Set a transient to track that the user has dismissed the pre-chat message
11166 - $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
11167 - set_transient($transient_key, true, DAY_IN_SECONDS);
11168 -
11169 - wp_send_json_success();
11170 -}
11171 -
11172 -public function mxchat_check_pre_chat_message_status() {
11173 - // Get and sanitize the user identifier
11174 - $user_id = $this->mxchat_get_user_identifier();
11175 - $user_id = sanitize_key($user_id);
11176 -
11177 - // Check if the transient exists (i.e., if the message was dismissed)
11178 - $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
11179 - $dismissed = get_transient($transient_key);
11180 -
11181 - // Log the result to see if it's being set correctly
11182 - //error_log("Check pre-chat message dismissed for $user_id: " . ($dismissed ? 'Yes' : 'No'));
11183 -
11184 - if ($dismissed) {
11185 - wp_send_json_success(['dismissed' => true]);
11186 - } else {
11187 - wp_send_json_success(['dismissed' => false]);
11188 - }
11189 -
11190 - wp_die();
11191 -}
11192 -
11193 -private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
11194 - if (!is_array($vectorA) || !is_array($vectorB) || empty($vectorA) || empty($vectorB)) {
11195 - return 0;
11196 - }
11197 -
11198 - $dotProduct = array_sum(array_map(function ($a, $b) {
11199 - return $a * $b;
11200 - }, $vectorA, $vectorB));
11201 - $normA = sqrt(array_sum(array_map(function ($a) {
11202 - return $a * $a;
11203 - }, $vectorA)));
11204 - $normB = sqrt(array_sum(array_map(function ($b) {
11205 - return $b * $b;
11206 - }, $vectorB)));
11207 -
11208 - if ($normA == 0 || $normB == 0) {
11209 - return 0;
11210 - }
11211 -
11212 - return $dotProduct / ($normA * $normB);
11213 - }
11214 -
11215 -
11216 -public function mxchat_enqueue_scripts_styles() {
11217 - // Fetch options from the database first to check loading strategy
11218 - $this->options = get_option('mxchat_options');
11219 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11220 -
11221 - // Always enqueue CSS immediately
11222 - wp_enqueue_style(
11223 - 'mxchat-chat-css',
11224 - plugin_dir_url(__FILE__) . '../css/chat-style.css',
11225 - array(),
11226 - MXCHAT_VERSION
11227 - );
11228 -
11229 - // Handle script loading based on strategy
11230 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11231 - // Enqueue the script normally
11232 - wp_enqueue_script(
11233 - 'mxchat-chat-js',
11234 - plugin_dir_url(__FILE__) . '../js/chat-script.js',
11235 - array('jquery'),
11236 - MXCHAT_VERSION,
11237 - true
11238 - );
11239 -
11240 - // Add defer attribute if strategy is 'defer'
11241 - if ($loading_strategy === 'defer') {
11242 - wp_script_add_data('mxchat-chat-js', 'strategy', 'defer');
11243 - }
11244 - } else {
11245 - // For delay or interaction-based loading, we'll use a custom loader
11246 - // Don't enqueue the main script - we'll load it dynamically
11247 - add_action('wp_footer', array($this, 'mxchat_output_delayed_script_loader'), 99);
11248 - }
11249 -
11250 - $prompts_options = get_option('mxchat_prompts_options', array());
11251 -
11252 - // Check if AI theme is active - if so, skip inline colors in JavaScript
11253 - $theme_options = get_option('mxchat_theme_options', array());
11254 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11255 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11256 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11257 -
11258 - // Prepare settings for JavaScript
11259 - $style_settings = array(
11260 - 'ajax_url' => admin_url('admin-ajax.php'),
11261 - // The chat-send nonce is now fetched per-request from /wp-json/mxchat/v1/nonce
11262 - // (plan-6a68c9) so it never sits in cached HTML. We still emit a nonce here
11263 - // as a one-shot fallback for the first interaction on a fresh page load
11264 - // (so the very first chat-send doesn't need to wait for a REST round-trip),
11265 - // but the widget refetches before each subsequent send.
11266 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11267 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11268 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11269 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11270 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11271 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11272 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11273 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11274 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11275 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11276 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11277 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11278 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11279 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11280 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11281 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11282 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11283 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11284 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11285 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11286 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11287 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11288 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11289 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11290 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off', // FIXED
11291 - 'initial_email_state' => null, // Also fixed this undefined variable
11292 - 'skip_email_check' => true,
11293 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11294 - 'skip_inline_colors' => $skip_inline_colors,
11295 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11296 - );
11297 -
11298 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11299 - // print/transcript, satisfaction rating) come from the shared
11300 - // dynamic-settings method so this inline payload and the first-open
11301 - // refresh endpoint can never drift (plan-32db95).
11302 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11303 -
11304 - // For normal/defer loading, use wp_localize_script
11305 - // For delayed loading, we store settings in a transient to be output inline
11306 - if ($loading_strategy === 'default' || $loading_strategy === 'defer') {
11307 - wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
11308 - } else {
11309 - // Store settings for the delayed loader to use
11310 - set_transient('mxchat_delayed_settings_' . get_current_user_id(), $style_settings, 60);
11311 - }
11312 -}
11313 -
11314 -/**
11315 - * Output the delayed script loader for performance optimization
11316 - */
11317 -public function mxchat_output_delayed_script_loader() {
11318 - $this->options = get_option('mxchat_options');
11319 - $loading_strategy = isset($this->options['script_loading_strategy']) ? $this->options['script_loading_strategy'] : 'default';
11320 - $script_url = plugin_dir_url(__FILE__) . '../js/chat-script.js?ver=' . MXCHAT_VERSION;
11321 -
11322 - // Get the stored settings
11323 - $prompts_options = get_option('mxchat_prompts_options', array());
11324 - $theme_options = get_option('mxchat_theme_options', array());
11325 - $ai_theme_active = !empty($theme_options['active_ai_theme_css']);
11326 - $has_bot_theme_assignments = !empty($theme_options['bot_theme_assignments']);
11327 - $skip_inline_colors = $ai_theme_active || $has_bot_theme_assignments;
11328 -
11329 - $style_settings = array(
11330 - 'ajax_url' => admin_url('admin-ajax.php'),
11331 - // Per-request nonce — see plan-6a68c9; widget fetches via /wp-json/mxchat/v1/nonce
11332 - // before each send. This inline value is a one-shot fallback for the first interaction.
11333 - 'nonce' => wp_create_nonce('mxchat_chat_send'),
11334 - 'rest_url' => esc_url_raw(trailingslashit(rest_url('mxchat/v1'))),
11335 - 'contextual_awareness_toggle' => isset($this->options['contextual_awareness_toggle']) ? $this->options['contextual_awareness_toggle'] : 'off',
11336 - 'link_target_toggle' => $this->options['link_target_toggle'] ?? 'off',
11337 - 'complianz_toggle' => isset($this->options['complianz_toggle']) && $this->options['complianz_toggle'] === 'on',
11338 - 'user_message_bg_color' => $this->options['user_message_bg_color'] ?? '#fff',
11339 - 'user_message_font_color' => $this->options['user_message_font_color'] ?? '#212121',
11340 - 'bot_message_bg_color' => $this->options['bot_message_bg_color'] ?? '#212121',
11341 - 'bot_message_font_color' => $this->options['bot_message_font_color'] ?? '#fff',
11342 - 'top_bar_bg_color' => $this->options['top_bar_bg_color'] ?? '#212121',
11343 - 'send_button_font_color' => $this->options['send_button_font_color'] ?? '#212121',
11344 - 'close_button_color' => $this->options['close_button_color'] ?? '#fff',
11345 - 'chatbot_background_color' => $this->options['chatbot_background_color'] ?? '#212121',
11346 - 'chatbot_bg_color' => $this->options['chatbot_bg_color'] ?? '#fff',
11347 - 'icon_color' => $this->options['icon_color'] ?? '#fff',
11348 - 'chat_input_font_color' => $this->options['chat_input_font_color'] ?? '#212121',
11349 - 'chat_persistence_toggle' => $this->options['chat_persistence_toggle'] ?? 'off',
11350 - 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off',
11351 - 'live_agent_message_bg_color' => $this->options['live_agent_message_bg_color'] ?? '#ffffff',
11352 - 'live_agent_message_font_color' => $this->options['live_agent_message_font_color'] ?? '#333333',
11353 - 'mode_indicator_bg_color' => $this->options['mode_indicator_bg_color'] ?? '#767676',
11354 - 'mode_indicator_font_color' => $this->options['mode_indicator_font_color'] ?? '#ffffff',
11355 - 'toolbar_icon_color' => $this->options['toolbar_icon_color'] ?? '#212121',
11356 - 'use_pinecone' => $prompts_options['mxchat_use_pinecone'] ?? '0',
11357 - 'email_collection_enabled' => $this->options['enable_email_block'] ?? 'off',
11358 - 'initial_email_state' => null,
11359 - 'skip_email_check' => true,
11360 - 'pinecone_enabled' => isset($prompts_options['mxchat_use_pinecone']) && $prompts_options['mxchat_use_pinecone'] === '1',
11361 - 'skip_inline_colors' => $skip_inline_colors,
11362 - 'bot_theme_assignments' => $theme_options['bot_theme_assignments'] ?? array(),
11363 - );
11364 -
11365 - // Behavior gates + labels (model, streaming, rate-limit copy, toolbar,
11366 - // print/transcript, satisfaction rating) come from the shared
11367 - // dynamic-settings method so this inline payload and the first-open
11368 - // refresh endpoint can never drift (plan-32db95).
11369 - $style_settings = array_merge($style_settings, $this->get_dynamic_widget_settings());
11370 -
11371 - // Determine delay time based on strategy
11372 - $delay_ms = 0;
11373 - switch ($loading_strategy) {
11374 - case 'delay_1s':
11375 - $delay_ms = 1000;
11376 - break;
11377 - case 'delay_3s':
11378 - $delay_ms = 3000;
11379 - break;
11380 - case 'delay_5s':
11381 - $delay_ms = 5000;
11382 - break;
11383 - }
11384 -
11385 - ?>
11386 - <script type="text/javascript">
11387 - (function() {
11388 - var mxchatLoaded = false;
11389 - var mxchatChat = <?php echo wp_json_encode($style_settings); ?>;
11390 - window.mxchatChat = mxchatChat;
11391 -
11392 - function loadMxChatScript() {
11393 - if (mxchatLoaded) return;
11394 - mxchatLoaded = true;
11395 -
11396 - function appendChatScript() {
11397 - var script = document.createElement('script');
11398 - script.src = <?php echo wp_json_encode($script_url); ?>;
11399 - script.type = 'text/javascript';
11400 - document.body.appendChild(script);
11401 - }
11402 -
11403 - if (typeof jQuery !== 'undefined') {
11404 - appendChatScript();
11405 - } else {
11406 - var jq = document.createElement('script');
11407 - jq.src = <?php echo wp_json_encode(includes_url('js/jquery/jquery.min.js')); ?>;
11408 - jq.onload = appendChatScript;
11409 - document.body.appendChild(jq);
11410 - }
11411 - }
11412 -
11413 - <?php if ($loading_strategy === 'on_interaction'): ?>
11414 - // Load on user interaction
11415 - var events = ['scroll', 'mousemove', 'touchstart', 'keydown', 'click'];
11416 - events.forEach(function(evt) {
11417 - window.addEventListener(evt, loadMxChatScript, {once: true, passive: true});
11418 - });
11419 - // Fallback: load after 8 seconds if no interaction
11420 - setTimeout(loadMxChatScript, 8000);
11421 - <?php else: ?>
11422 - // Load after specified delay
11423 - setTimeout(loadMxChatScript, <?php echo intval($delay_ms); ?>);
11424 - <?php endif; ?>
11425 - })();
11426 - </script>
11427 - <?php
11428 -}
11429 -
11430 -/**
11431 - * Setup the cron jobs for rate limits with guard against multiple calls
11432 - */
11433 -public function setup_rate_limit_cron_jobs() {
11434 - // Add a guard to prevent multiple rapid calls
11435 - $last_setup = get_transient('mxchat_cron_setup_guard');
11436 - if ($last_setup && (time() - $last_setup) < 60) {
11437 - // Don't run again if we ran less than 60 seconds ago
11438 - return;
11439 - }
11440 -
11441 - // Set the guard
11442 - set_transient('mxchat_cron_setup_guard', time(), 300); // 5 minutes
11443 -
11444 - try {
11445 - // First, check if WordPress cron is disabled
11446 - if (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) {
11447 - //error_log('MxChat: WordPress cron is disabled (DISABLE_WP_CRON = true), using fallback system');
11448 - $this->setup_fallback_rate_limit_system();
11449 - return;
11450 - }
11451 -
11452 - // Check if cron is already scheduled - if so, don't mess with it
11453 - if (wp_next_scheduled('mxchat_reset_rate_limits')) {
11454 - //error_log('MxChat: Rate limit cron already scheduled, skipping setup');
11455 - return;
11456 - }
11457 -
11458 - // Clear any orphaned hooks (but don't loop indefinitely)
11459 - $hooks_to_clear = [
11460 - 'mxchat_reset_rate_limits',
11461 - 'mxchat_reset_hourly_rate_limits',
11462 - 'mxchat_reset_daily_rate_limits',
11463 - 'mxchat_reset_weekly_rate_limits',
11464 - 'mxchat_reset_monthly_rate_limits'
11465 - ];
11466 -
11467 - foreach ($hooks_to_clear as $hook) {
11468 - // Only clear a maximum of 3 instances to prevent infinite loops
11469 - $cleared = 0;
11470 - while (wp_next_scheduled($hook) && $cleared < 3) {
11471 - wp_clear_scheduled_hook($hook);
11472 - $cleared++;
11473 - }
11474 - }
11475 -
11476 - // Small delay after clearing
11477 - usleep(100000); // 0.1 seconds
11478 -
11479 - // Try to schedule the event
11480 - $initial_time = time() + 300; // Start in 5 minutes
11481 - $result = wp_schedule_event($initial_time, 'hourly', 'mxchat_reset_rate_limits');
11482 -
11483 - if ($result === false) {
11484 - //error_log('MxChat: Failed to schedule cron, using fallback system');
11485 - $this->setup_fallback_rate_limit_system();
11486 - } else {
11487 - //error_log('MxChat: Successfully scheduled rate limit reset cron');
11488 - }
11489 -
11490 - } catch (Exception $e) {
11491 - //error_log('MxChat: Cron setup exception: ' . $e->getMessage());
11492 - $this->setup_fallback_rate_limit_system();
11493 - }
11494 -}
11495 -
11496 -/**
11497 - * Try alternative cron scheduling methods
11498 - */
11499 -private function try_alternative_cron_scheduling($initial_time) {
11500 - try {
11501 - // Method 1: Try with current time instead of future time
11502 - $result1 = wp_schedule_event(time(), 'hourly', 'mxchat_reset_rate_limits');
11503 - if ($result1 !== false) {
11504 - //error_log('MxChat: Alternative method 1 (current time) succeeded');
11505 - return true;
11506 - }
11507 -
11508 - // Method 2: Try with a different interval
11509 - $result2 = wp_schedule_event($initial_time, 'daily', 'mxchat_reset_rate_limits');
11510 - if ($result2 !== false) {
11511 - //error_log('MxChat: Alternative method 2 (daily interval) succeeded');
11512 - return true;
11513 - }
11514 -
11515 - // Method 3: Try wp_schedule_single_event first, then recurring
11516 - $result3 = wp_schedule_single_event($initial_time, 'mxchat_reset_rate_limits');
11517 - if ($result3 !== false) {
11518 - //error_log('MxChat: Alternative method 3 (single event) succeeded');
11519 - // Schedule the next one manually in the handler
11520 - return true;
11521 - }
11522 -
11523 - return false;
11524 -
11525 - } catch (Exception $e) {
11526 - //error_log('MxChat: Alternative cron scheduling exception: ' . $e->getMessage());
11527 - return false;
11528 - }
11529 -}
11530 -
11531 -/**
11532 - * Enhanced fallback rate limit system
11533 - */
11534 -private function setup_fallback_rate_limit_system() {
11535 - // Set a flag to use database-based rate limit cleanup
11536 - update_option('mxchat_use_fallback_rate_limits', true);
11537 -
11538 - // Schedule a one-time check to happen on the next plugin load
11539 - update_option('mxchat_next_rate_limit_check', time() + 3600);
11540 -
11541 - // Also set up a more frequent fallback check (every 4 hours)
11542 - update_option('mxchat_fallback_check_interval', 4 * 3600);
11543 -
11544 - //error_log('MxChat: Fallback rate limit system activated');
11545 -}
11546 -
11547 -/**
11548 - * Enhanced fallback check method
11549 - */
11550 -public function check_fallback_rate_limits() {
11551 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11552 -
11553 - if (!$use_fallback) {
11554 - return; // Regular cron is working
11555 - }
11556 -
11557 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11558 - $check_interval = get_option('mxchat_fallback_check_interval', 3600);
11559 -
11560 - if (time() >= $next_check) {
11561 - //error_log('MxChat: Running fallback rate limit cleanup');
11562 - $this->mxchat_reset_rate_limits();
11563 -
11564 - // Schedule next check
11565 - update_option('mxchat_next_rate_limit_check', time() + $check_interval);
11566 - }
11567 -}
11568 -/**
11569 - * Enhanced rate limit check that includes fallback cleanup and bot-specific rate limits
11570 - */
11571 -public function check_rate_limit() {
11572 - // Check if we need to run fallback cleanup
11573 - $use_fallback = get_option('mxchat_use_fallback_rate_limits', false);
11574 - $next_check = get_option('mxchat_next_rate_limit_check', 0);
11575 -
11576 - if ($use_fallback && time() >= $next_check) {
11577 - $this->mxchat_reset_rate_limits();
11578 - update_option('mxchat_next_rate_limit_check', time() + 3600); // Next hour
11579 - }
11580 -
11581 - // Get bot ID from current request context
11582 - $bot_id = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11583 -
11584 - // Get bot-specific options (includes rate limits if overridden)
11585 - $bot_options = $this->get_bot_options($bot_id);
11586 - $current_options = !empty($bot_options) ? $bot_options : $this->options;
11587 -
11588 - // Use bot-specific rate limits if available, otherwise fall back to default
11589 - $rate_limits_source = isset($current_options['rate_limits']) ? $current_options['rate_limits'] : get_option('mxchat_options', [])['rate_limits'] ?? [];
11590 -
11591 - // -------------------------------------------------------------------
11592 - // Whole-chatbot global cap (independent of role). Evaluated FIRST so
11593 - // it acts as a hard ceiling across all users + all roles. Default is
11594 - // 'unlimited' so existing installs are unchanged. Counter key drops
11595 - // both <role> and <user_id> segments — single pool per bot.
11596 - // -------------------------------------------------------------------
11597 - $global_cfg = isset($current_options['rate_limits_global']) && is_array($current_options['rate_limits_global'])
11598 - ? $current_options['rate_limits_global']
11599 - : (isset(get_option('mxchat_options', [])['rate_limits_global']) ? get_option('mxchat_options', [])['rate_limits_global'] : []);
11600 - $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
11601 - $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
11602 - if ($global_limit_raw !== '' && $global_limit_raw !== 'unlimited' && (int) $global_limit_raw >= 1) {
11603 - $bot_id_for_global = isset($_POST['bot_id']) ? sanitize_key($_POST['bot_id']) : 'default';
11604 - $safe_bot_global = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id_for_global);
11605 - $global_option = 'mxchat_chat_limit_' . $safe_bot_global . '_global';
11606 - $global_data = get_option($global_option, ['count' => 0, 'timestamp' => time()]);
11607 - if ((int) $global_data['count'] === 0) {
11608 - $global_data['timestamp'] = time();
11609 - update_option($global_option, $global_data);
11610 - }
11611 - $now = time();
11612 - $ts = (int) $global_data['timestamp'];
11613 - $reset = false;
11614 - switch ($global_timeframe) {
11615 - case 'hourly': $reset = ($now - $ts) >= 3600; break;
11616 - case 'daily': $reset = ($now - $ts) >= 86400; break;
11617 - case 'weekly': $reset = ($now - $ts) >= 604800; break;
11618 - case 'monthly': $reset = ($now - $ts) >= 2592000; break;
11619 - }
11620 - if ($reset) {
11621 - $global_data = ['count' => 0, 'timestamp' => $now];
11622 - update_option($global_option, $global_data);
11623 - }
11624 - if ((int) $global_data['count'] >= (int) $global_limit_raw) {
11625 - $global_msg = !empty($global_cfg['message'])
11626 - ? $global_cfg['message']
11627 - : __('This chatbot has reached its message limit. Please try again later.', 'mxchat');
11628 - return [
11629 - 'error' => true,
11630 - 'message' => $this->process_rate_limit_message_html($global_msg),
11631 - ];
11632 - }
11633 - // Reserve the slot for this request. Per-role check below also increments
11634 - // its own counter — that is intentional, both ceilings apply independently.
11635 - $global_data['count']++;
11636 - update_option($global_option, $global_data);
11637 - }
11638 -
11639 - // Determine user role or if logged out
11640 - if (is_user_logged_in()) {
11641 - $user = wp_get_current_user();
11642 - $user_id = $user->ID;
11643 -
11644 - // Get the user's primary role using reset() to safely get the first element
11645 - $user_roles = $user->roles;
11646 -
11647 - // Safely get the first role regardless of array key structure
11648 - if (!empty($user_roles) && is_array($user_roles)) {
11649 - $role = reset($user_roles); // This safely gets the first element regardless of key
11650 - } else {
11651 - $role = 'subscriber'; // Default to subscriber if no role found
11652 - }
11653 - } else {
11654 - $role = 'logged_out';
11655 - // Use IP address for non-logged-in users
11656 - $user_id = $this->get_client_ip();
11657 - }
11658 -
11659 - // Check if rate limits are configured for this role
11660 - if (!isset($rate_limits_source[$role])) {
11661 - return true; // No limit set for this role
11662 - }
11663 -
11664 - $limit = $rate_limits_source[$role]['limit'];
11665 -
11666 - // If unlimited, return true immediately
11667 - if ($limit === 'unlimited') {
11668 - return true;
11669 - }
11670 -
11671 - // Get the option name for this user/role with safer naming (include bot_id for bot-specific limits)
11672 - $safe_role = preg_replace('/[^a-zA-Z0-9_]/', '_', $role);
11673 - $safe_user_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $user_id);
11674 - $safe_bot_id = preg_replace('/[^a-zA-Z0-9_]/', '_', $bot_id);
11675 -
11676 - // Include bot_id in option name so each bot has separate rate limits
11677 - $option_name = 'mxchat_chat_limit_' . $safe_bot_id . '_' . $safe_role . '_' . $safe_user_id;
11678 -
11679 - // Get the counter data
11680 - $limit_data = get_option($option_name, ['count' => 0, 'timestamp' => time()]);
11681 -
11682 - // If first request or counter reset needed, set the initial timestamp
11683 - if ($limit_data['count'] === 0) {
11684 - $limit_data['timestamp'] = time();
11685 - update_option($option_name, $limit_data);
11686 - }
11687 -
11688 - // Get the timeframe
11689 - $timeframe = isset($rate_limits_source[$role]['timeframe']) ?
11690 - $rate_limits_source[$role]['timeframe'] : 'daily';
11691 -
11692 - // Check if the counter needs to be reset based on timeframe
11693 - $current_time = time();
11694 - $timestamp = $limit_data['timestamp'];
11695 - $should_reset = false;
11696 -
11697 - switch ($timeframe) {
11698 - case 'hourly':
11699 - $should_reset = ($current_time - $timestamp) >= 3600; // 1 hour
11700 - break;
11701 - case 'daily':
11702 - $should_reset = ($current_time - $timestamp) >= 86400; // 24 hours
11703 - break;
11704 - case 'weekly':
11705 - $should_reset = ($current_time - $timestamp) >= 604800; // 7 days
11706 - break;
11707 - case 'monthly':
11708 - $should_reset = ($current_time - $timestamp) >= 2592000; // 30 days
11709 - break;
11710 - }
11711 -
11712 - // Reset the counter if the timeframe has passed
11713 - if ($should_reset) {
11714 - $limit_data = ['count' => 0, 'timestamp' => $current_time];
11715 - update_option($option_name, $limit_data);
11716 - }
11717 -
11718 - // Check if user has exceeded their limit
11719 - if ($limit_data['count'] >= intval($limit)) {
11720 - // Get the custom message for this role
11721 - $message = !empty($rate_limits_source[$role]['message'])
11722 - ? $rate_limits_source[$role]['message']
11723 - : __('Rate limit exceeded. Please try again later.', 'mxchat');
11724 -
11725 - // Add timeframe information to the message if placeholders exist
11726 - $timeframe_label = '';
11727 - switch ($timeframe) {
11728 - case 'hourly':
11729 - $timeframe_label = __('hour', 'mxchat');
11730 - break;
11731 - case 'daily':
11732 - $timeframe_label = __('day', 'mxchat');
11733 - break;
11734 - case 'weekly':
11735 - $timeframe_label = __('week', 'mxchat');
11736 - break;
11737 - case 'monthly':
11738 - $timeframe_label = __('month', 'mxchat');
11739 - break;
11740 - }
11741 -
11742 - // Replace placeholders in the message
11743 - $message = str_replace(
11744 - ['{limit}', '{count}', '{remaining}', '{timeframe}'],
11745 - [intval($limit), $limit_data['count'], max(0, intval($limit) - $limit_data['count']), $timeframe_label],
11746 - $message
11747 - );
11748 -
11749 - // Process HTML links in the message
11750 - $message = $this->process_rate_limit_message_html($message);
11751 -
11752 - // Return error with the processed message
11753 - return [
11754 - 'error' => true,
11755 - 'message' => $message
11756 - ];
11757 - }
11758 -
11759 - // Increment the counter
11760 - $limit_data['count']++;
11761 - update_option($option_name, $limit_data);
11762 -
11763 - return true;
11764 -}
11765 -
11766 -/**
11767 - * Enhanced rate limit reset with better error handling
11768 - */
11769 -public function mxchat_reset_rate_limits() {
11770 - try {
11771 - global $wpdb;
11772 - $all_options = get_option('mxchat_options', []);
11773 - $current_time = time();
11774 -
11775 - // Get rate limit options with a safer query and limit
11776 - $option_names = $wpdb->get_col(
11777 - $wpdb->prepare(
11778 - "SELECT option_name FROM {$wpdb->options}
11779 - WHERE option_name LIKE %s
11780 - LIMIT 1000",
11781 - 'mxchat_chat_limit_%'
11782 - )
11783 - );
11784 -
11785 - if (empty($option_names)) {
11786 - return;
11787 - }
11788 -
11789 - $processed_count = 0;
11790 - $max_processing_time = 30; // Maximum 30 seconds
11791 - $start_time = time();
11792 -
11793 - foreach ($option_names as $option_name) {
11794 - // Check processing time limit
11795 - if ((time() - $start_time) > $max_processing_time) {
11796 - //error_log('MxChat: Rate limit reset timeout after processing ' . $processed_count . ' entries');
11797 - break;
11798 - }
11799 -
11800 - // Parse the option name more safely
11801 - if (!preg_match('/^mxchat_chat_limit_(.+)_(.+)$/', $option_name, $matches)) {
11802 - continue;
11803 - }
11804 -
11805 - $role_and_user = $matches[1] . '_' . $matches[2];
11806 - $parts = explode('_', $role_and_user);
11807 -
11808 - if (count($parts) < 2) {
11809 - continue;
11810 - }
11811 -
11812 - // Extract role (everything except the last part which is user ID)
11813 - $user_id_part = array_pop($parts);
11814 - $role = implode('_', $parts);
11815 -
11816 - // Skip if role doesn't exist in our settings
11817 - if (!isset($all_options['rate_limits'][$role])) {
11818 - // Clean up orphaned entries
11819 - delete_option($option_name);
11820 - continue;
11821 - }
11822 -
11823 - $timeframe = $all_options['rate_limits'][$role]['timeframe'] ?? 'daily';
11824 - $limit_data = get_option($option_name);
11825 -
11826 - if (!$limit_data || !is_array($limit_data) || !isset($limit_data['timestamp'])) {
11827 - // Clean up invalid entries
11828 - delete_option($option_name);
11829 - continue;
11830 - }
11831 -
11832 - $timestamp = $limit_data['timestamp'];
11833 - $should_reset = false;
11834 -
11835 - // Determine if we should reset based on the timeframe
11836 - switch ($timeframe) {
11837 - case 'hourly':
11838 - $should_reset = ($current_time - $timestamp) >= 3600;
11839 - break;
11840 - case 'daily':
11841 - $should_reset = ($current_time - $timestamp) >= 86400;
11842 - break;
11843 - case 'weekly':
11844 - $should_reset = ($current_time - $timestamp) >= 604800;
11845 - break;
11846 - case 'monthly':
11847 - $should_reset = ($current_time - $timestamp) >= 2592000;
11848 - break;
11849 - }
11850 -
11851 - // Reset the counter if the timeframe has passed
11852 - if ($should_reset) {
11853 - delete_option($option_name);
11854 - wp_cache_delete($option_name, 'options');
11855 - $processed_count++;
11856 - }
11857 - }
11858 -
11859 - // Clean up any orphaned cache entries
11860 - wp_cache_delete('mxchat_all_chat_limits', 'options');
11861 -
11862 - //error_log("MxChat: Rate limit reset completed. Processed {$processed_count} entries.");
11863 -
11864 - } catch (Exception $e) {
11865 - //error_log('MxChat: Rate limit reset error: ' . $e->getMessage());
11866 - }
11867 -}
11868 -
11869 -
11870 -/**
11871 - * Process HTML links in rate limit messages
11872 - *
11873 - * @param string $message The rate limit message
11874 - * @return string The processed message with safe HTML links
11875 - */
11876 -private function process_rate_limit_message_html($message) {
11877 - // Return original message if empty
11878 - if (empty($message)) {
11879 - return $message;
11880 - }
11881 -
11882 - // First, convert markdown links to HTML
11883 - $message = $this->convert_markdown_links($message);
11884 -
11885 - // Then, auto-convert any remaining plain URLs to links
11886 - $message = $this->auto_link_urls($message);
11887 -
11888 - // Allow basic HTML tags for links and formatting
11889 - $allowed_tags = [
11890 - 'a' => [
11891 - 'href' => true,
11892 - 'target' => true,
11893 - 'rel' => true,
11894 - 'title' => true,
11895 - 'class' => true
11896 - ],
11897 - 'strong' => [],
11898 - 'em' => [],
11899 - 'br' => [],
11900 - 'b' => [],
11901 - 'i' => [],
11902 - 'span' => ['class' => true]
11903 - ];
11904 -
11905 - // Sanitize but allow the specified HTML tags
11906 - $processed_message = wp_kses($message, $allowed_tags);
11907 -
11908 - // If wp_kses stripped everything, return the original message as plain text
11909 - if (empty($processed_message) && !empty($message)) {
11910 - // Strip all HTML and return plain text as fallback
11911 - return wp_strip_all_tags($message);
11912 - }
11913 -
11914 - return $processed_message;
11915 -}
11916 -
11917 -/**
11918 - * Convert markdown links to HTML
11919 - *
11920 - * @param string $text The text to process
11921 - * @return string The text with markdown links converted to HTML
11922 - */
11923 -private function convert_markdown_links($text) {
11924 - // Return original text if empty
11925 - if (empty($text)) {
11926 - return $text;
11927 - }
11928 -
11929 - // Pattern to match markdown links: [text](url)
11930 - $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
11931 -
11932 - $processed_text = preg_replace_callback($pattern, function($matches) {
11933 - $link_text = $matches[1];
11934 - $url = $matches[2];
11935 -
11936 - // Clean up any trailing punctuation from the URL
11937 - $url = rtrim($url, '.,;:!?');
11938 -
11939 - // Sanitize the link text and URL
11940 - $safe_text = esc_html($link_text);
11941 - $safe_url = esc_url($url);
11942 -
11943 - // Create the HTML link
11944 - return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
11945 - }, $text);
11946 -
11947 - // If preg_replace_callback failed, return original text
11948 - if ($processed_text === null) {
11949 - return $text;
11950 - }
11951 -
11952 - return $processed_text;
11953 -}
11954 -
11955 -/**
11956 - * Auto-convert plain URLs to clickable links
11957 - *
11958 - * @param string $text The text to process
11959 - * @return string The text with URLs converted to links
11960 - */
11961 -private function auto_link_urls($text) {
11962 - // Return original text if empty
11963 - if (empty($text)) {
11964 - return $text;
11965 - }
11966 -
11967 - // Simple pattern that avoids complex lookbehinds
11968 - // This will match URLs that are not already inside href attributes or markdown links
11969 - $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
11970 -
11971 - $processed_text = preg_replace_callback($pattern, function($matches) {
11972 - $url = $matches[0];
11973 - // Clean up any trailing punctuation that might have been captured
11974 - $url = rtrim($url, '.,;:!?');
11975 -
11976 - // Add target="_blank" and rel="noopener noreferrer" for security
11977 - return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
11978 - }, $text);
11979 -
11980 - // If preg_replace_callback failed, return original text
11981 - if ($processed_text === null) {
11982 - return $text;
11983 - }
11984 -
11985 - return $processed_text;
11986 -}
11987 -
11988 -
11989 -// Helper function to get client IP address
11990 -private function get_client_ip() {
11991 - // Check for shared internet/ISP IP
11992 - if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
11993 - return sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
11994 - }
11995 -
11996 - // Check for IPs passing through proxies
11997 - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
11998 - // Use the first value in the comma-separated list
11999 - $forwarded_for = explode(',', sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']));
12000 - return trim($forwarded_for[0]);
12001 - }
12002 -
12003 - if (!empty($_SERVER['REMOTE_ADDR'])) {
12004 - return sanitize_text_field($_SERVER['REMOTE_ADDR']);
12005 - }
12006 -
12007 - // Fallback
12008 - return 'unknown';
12009 -}
12010 -
12011 -/**
12012 - * AJAX handler to get system information for testing panel
12013 - */
12014 -/**
12015 - * AJAX handler to get system information for testing panel
12016 - */
12017 -public function mxchat_get_system_info() {
12018 - // Verify nonce for security
12019 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12020 - wp_send_json_error(['message' => 'Invalid nonce']);
12021 - return;
12022 - }
12023 -
12024 - // Only allow admin users
12025 - if (!current_user_can('administrator')) {
12026 - wp_send_json_error(['message' => 'Unauthorized']);
12027 - return;
12028 - }
12029 -
12030 - // Get system prompt from options
12031 - $system_prompt = isset($this->options['system_prompt_instructions'])
12032 - ? $this->options['system_prompt_instructions']
12033 - : 'No system prompt configured';
12034 -
12035 - // Get selected model
12036 - $selected_model = isset($this->options['model']) ? $this->options['model'] : 'gpt-5.1-chat-latest';
12037 -
12038 - // Check if OpenRouter is being used
12039 - $is_openrouter = ($selected_model === 'openrouter');
12040 - $openrouter_model = '';
12041 -
12042 - if ($is_openrouter) {
12043 - // Get the actual OpenRouter model that's selected
12044 - $openrouter_model = isset($this->options['openrouter_selected_model'])
12045 - ? $this->options['openrouter_selected_model']
12046 - : 'No OpenRouter model selected';
12047 -
12048 - // Update selected_model display to show both
12049 - $selected_model = 'OpenRouter: ' . $openrouter_model;
12050 - }
12051 -
12052 - // Get API key status (just check if they exist, don't expose the keys)
12053 - $api_status = [];
12054 - $api_status['openai'] = !empty($this->options['api_key']);
12055 - $api_status['claude'] = !empty($this->options['claude_api_key']);
12056 - $api_status['gemini'] = !empty($this->options['gemini_api_key']);
12057 - $api_status['xai'] = !empty($this->options['xai_api_key']);
12058 - $api_status['deepseek'] = !empty($this->options['deepseek_api_key']);
12059 - $api_status['openrouter'] = !empty($this->options['openrouter_api_key']);
12060 -
12061 - wp_send_json_success([
12062 - 'system_prompt' => $system_prompt,
12063 - 'selected_model' => $selected_model,
12064 - 'is_openrouter' => $is_openrouter,
12065 - 'openrouter_model' => $openrouter_model,
12066 - 'api_status' => $api_status
12067 - ]);
12068 -}
12069 -
12070 -/**
12071 - * AJAX handler to get similarity threshold
12072 - */
12073 -public function mxchat_get_similarity_threshold() {
12074 - // Verify nonce for security
12075 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12076 - wp_send_json_error(['message' => 'Invalid nonce']);
12077 - return;
12078 - }
12079 -
12080 - // Only allow admin users
12081 - if (!current_user_can('administrator')) {
12082 - wp_send_json_error(['message' => 'Unauthorized']);
12083 - return;
12084 - }
12085 -
12086 - // Get similarity threshold from main options (default 35%)
12087 - $similarity_threshold = isset($this->options['similarity_threshold'])
12088 - ? ((int) $this->options['similarity_threshold']) / 100
12089 - : 0.35;
12090 -
12091 - wp_send_json_success([
12092 - 'threshold' => $similarity_threshold,
12093 - 'threshold_percentage' => ($similarity_threshold * 100) . '%'
12094 - ]);
12095 -}
12096 -
12097 -/**
12098 - * AJAX handler to get knowledge base status
12099 - */
12100 -public function mxchat_get_kb_status() {
12101 - // Verify nonce for security
12102 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12103 - wp_send_json_error(['message' => 'Invalid nonce']);
12104 - return;
12105 - }
12106 -
12107 - // Only allow admin users
12108 - if (!current_user_can('administrator')) {
12109 - wp_send_json_error(['message' => 'Unauthorized']);
12110 - return;
12111 - }
12112 -
12113 - // Check OpenAI Vector Store first (takes priority)
12114 - $vectorstore_options = get_option('mxchat_openai_vectorstore_options', array());
12115 - $use_vectorstore = (isset($vectorstore_options['mxchat_use_openai_vectorstore']) && $vectorstore_options['mxchat_use_openai_vectorstore'] === '1');
12116 -
12117 - if ($use_vectorstore) {
12118 - $vectorstore_ids = $vectorstore_options['mxchat_openai_vectorstore_ids'] ?? '';
12119 - $id_count = !empty($vectorstore_ids) ? count(array_filter(array_map('trim', explode(',', $vectorstore_ids)))) : 0;
12120 -
12121 - $kb_info = [
12122 - 'type' => 'OpenAI Vector Store',
12123 - 'status' => 'Active',
12124 - 'documents' => $id_count > 0 ? $id_count . ' vector store' . ($id_count > 1 ? 's' : '') . ' configured' : 'No vector stores configured'
12125 - ];
12126 -
12127 - wp_send_json_success($kb_info);
12128 - return;
12129 - }
12130 -
12131 - // Check Pinecone vs WordPress
12132 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12133 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12134 -
12135 - $kb_info = [
12136 - 'type' => $use_pinecone ? 'Pinecone' : 'WordPress Database',
12137 - 'status' => 'Active'
12138 - ];
12139 -
12140 - // Get document count
12141 - if ($use_pinecone) {
12142 - $kb_info['documents'] = 'Connected to Pinecone';
12143 - $kb_info['api_configured'] = !empty($addon_options['mxchat_pinecone_api_key']);
12144 - } else {
12145 - // Count documents in WordPress database
12146 - global $wpdb;
12147 - $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
12148 - $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
12149 - $kb_info['documents'] = $count ? $count . ' documents' : 'No documents';
12150 - }
12151 -
12152 - wp_send_json_success($kb_info);
12153 -}
12154 -
12155 -/**
12156 - * AJAX handler to start a completely fresh session (NEW - replaces old clear session)
12157 - */
12158 -public function mxchat_start_fresh_session() {
12159 - // Verify nonce for security
12160 - if (!wp_verify_nonce($_POST['nonce'], 'mxchat_test_nonce')) {
12161 - wp_send_json_error(['message' => 'Invalid nonce']);
12162 - return;
12163 - }
12164 -
12165 - // Only allow admin users
12166 - if (!current_user_can('administrator')) {
12167 - wp_send_json_error(['message' => 'Unauthorized']);
12168 - return;
12169 - }
12170 -
12171 - $old_session_id = isset($_POST['old_session_id']) ? sanitize_text_field($_POST['old_session_id']) : '';
12172 - $new_session_id = isset($_POST['new_session_id']) ? sanitize_text_field($_POST['new_session_id']) : '';
12173 -
12174 - if (empty($old_session_id)) {
12175 - wp_send_json_error(['message' => 'Old session ID required']);
12176 - return;
12177 - }
12178 -
12179 - // If no new session ID provided, generate one
12180 - if (empty($new_session_id)) {
12181 - $new_session_id = 'mxchat_chat_' . substr(md5(uniqid()), 0, 9);
12182 - }
12183 -
12184 - // Clear ALL data associated with the old session
12185 - $this->clear_complete_session_data($old_session_id);
12186 -
12187 - // Initialize the new session
12188 - $this->initialize_fresh_session($new_session_id);
12189 -
12190 - wp_send_json_success([
12191 - 'message' => 'Fresh session started successfully',
12192 - 'new_session_id' => $new_session_id,
12193 - 'old_session_id' => $old_session_id
12194 - ]);
12195 -}
12196 -
12197 -/**
12198 - * Clear ALL data associated with a session (ENHANCED)
12199 - */
12200 -private function clear_complete_session_data($session_id) {
12201 - // Clear chat history
12202 - delete_option("mxchat_history_{$session_id}");
12203 -
12204 - // Clear chat mode
12205 - delete_option("mxchat_mode_{$session_id}");
12206 -
12207 - // Clear any PDF/Word transients
12208 - $this->clear_pdf_transients($session_id);
12209 - if (method_exists($this, 'clear_word_transients')) {
12210 - $this->clear_word_transients($session_id);
12211 - }
12212 -
12213 - // Clear agent-related data
12214 - delete_option("mxchat_channel_{$session_id}");
12215 - delete_option("mxchat_agent_name_{$session_id}");
12216 - delete_option("mxchat_email_{$session_id}");
12217 -
12218 - // Clear any recommendation flow state
12219 - delete_option("mxchat_sr_flow_state_{$session_id}");
12220 -
12221 - // Clear any cached embeddings or context
12222 - delete_transient("mxchat_context_{$session_id}");
12223 - delete_transient("mxchat_last_query_{$session_id}");
12224 -
12225 - // Clear any testing data
12226 - delete_transient("mxchat_testing_data_{$session_id}");
12227 -
12228 - // Clear any rate limiting data for this session
12229 - delete_transient("mxchat_rate_limit_{$session_id}");
12230 -
12231 - // Clear any other session-specific transients
12232 - delete_transient("mxchat_waiting_for_pdf_url_{$session_id}");
12233 - delete_transient("mxchat_include_pdf_in_context_{$session_id}");
12234 - delete_transient("mxchat_include_word_in_context_{$session_id}");
12235 -
12236 - // Clear form addon state (pending forms and submitted forms)
12237 - delete_option("mxchat_pending_form_{$session_id}");
12238 - delete_option("mxchat_submitted_forms_{$session_id}");
12239 -
12240 - //error_log("MxChat: Cleared all data for session: {$session_id}");
12241 -}
12242 -
12243 -/**
12244 - * Initialize a fresh session with default data
12245 - */
12246 -private function initialize_fresh_session($session_id) {
12247 - // Set default chat mode
12248 - update_option("mxchat_mode_{$session_id}", 'ai');
12249 -
12250 - //error_log("MxChat: Initialized fresh session: {$session_id}");
12251 -}
12252 -
12253 -/**
12254 - * Helper method to clear Word document transients (if you have Word support)
12255 - */
12256 -private function clear_word_transients($session_id) {
12257 - delete_transient('mxchat_word_url_' . $session_id);
12258 - delete_transient('mxchat_word_filename_' . $session_id);
12259 - delete_transient('mxchat_word_embeddings_' . $session_id);
12260 - delete_transient('mxchat_include_word_in_context_' . $session_id);
12261 -}
12262 -
12263 -/**
12264 - * Simplified testing data capture method (CLEANED UP)
12265 - */
12266 -private function capture_testing_data($user_embedding, $message, $session_id) {
12267 - // Only capture for admin users
12268 - if (!current_user_can('administrator')) {
12269 - return null;
12270 - }
12271 -
12272 - $testing_data = [
12273 - 'query' => $message,
12274 - 'timestamp' => time(),
12275 - 'top_matches' => [],
12276 - 'action_matches' => [] // Add action matches
12277 - ];
12278 -
12279 - // Get similarity threshold
12280 - $similarity_threshold = isset($this->options['similarity_threshold'])
12281 - ? ((int) $this->options['similarity_threshold']) / 100
12282 - : 0.35;
12283 -
12284 - $testing_data['similarity_threshold'] = $similarity_threshold;
12285 -
12286 - // Use the real similarity analysis if available
12287 - if ($this->last_similarity_analysis !== null) {
12288 - $testing_data['knowledge_base_type'] = $this->last_similarity_analysis['knowledge_base_type'];
12289 - $testing_data['top_matches'] = $this->last_similarity_analysis['top_matches'];
12290 - $testing_data['total_documents_checked'] = $this->last_similarity_analysis['total_checked'] ?? 0;
12291 - } else {
12292 - // Fallback: determine knowledge base type
12293 - $addon_options = get_option('mxchat_pinecone_addon_options', array());
12294 - $use_pinecone = (isset($addon_options['mxchat_use_pinecone']) && $addon_options['mxchat_use_pinecone'] === '1');
12295 -
12296 - $testing_data['knowledge_base_type'] = $use_pinecone ? 'Pinecone' : 'WordPress Database';
12297 - }
12298 -
12299 - // Include action analysis if available
12300 - if (isset($this->last_action_analysis) && !empty($this->last_action_analysis)) {
12301 - $testing_data['action_matches'] = $this->last_action_analysis;
12302 -
12303 - // Clear it after capturing to avoid stale data
12304 - $this->last_action_analysis = null;
12305 - }
12306 -
12307 - return $testing_data;
12308 -}
12309 -
12310 -
12311 -/**
12312 - * Track URL clicks from chatbot responses
12313 - */
12314 -public function mxchat_track_url_click() {
12315 - // Verify nonce for security
12316 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12317 - wp_send_json_error(['message' => 'Invalid nonce']);
12318 - wp_die();
12319 - }
12320 -
12321 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12322 - $clicked_url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
12323 - $message_context = isset($_POST['message_context']) ? sanitize_textarea_field($_POST['message_context']) : '';
12324 -
12325 - if (empty($session_id) || empty($clicked_url)) {
12326 - wp_send_json_error(['message' => 'Missing required data']);
12327 - wp_die();
12328 - }
12329 -
12330 - global $wpdb;
12331 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12332 -
12333 - // Insert click tracking record
12334 - $wpdb->insert(
12335 - $table_name,
12336 - [
12337 - 'session_id' => $session_id,
12338 - 'clicked_url' => $clicked_url,
12339 - 'message_context' => $message_context,
12340 - 'click_timestamp' => current_time('mysql', 1),
12341 - 'user_ip' => $_SERVER['REMOTE_ADDR'],
12342 - 'user_agent' => $_SERVER['HTTP_USER_AGENT']
12343 - ]
12344 - );
12345 -
12346 - wp_send_json_success(['message' => 'Click tracked']);
12347 - wp_die();
12348 -}
12349 -
12350 -/**
12351 - * Get URL click analytics for a session
12352 - */
12353 -public function mxchat_get_url_clicks($session_id) {
12354 - global $wpdb;
12355 - $table_name = $wpdb->prefix . 'mxchat_url_clicks';
12356 -
12357 - $clicks = $wpdb->get_results($wpdb->prepare(
12358 - "SELECT * FROM $table_name WHERE session_id = %s ORDER BY click_timestamp ASC",
12359 - $session_id
12360 - ));
12361 -
12362 - return $clicks;
12363 -}
12364 -/**
12365 - * Track the originating page where chat was started
12366 - */
12367 -public function mxchat_track_originating_page() {
12368 - // Verify nonce
12369 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12370 - wp_send_json_error(['message' => 'Invalid nonce']);
12371 - wp_die();
12372 - }
12373 -
12374 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12375 - $page_url = isset($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
12376 - $page_title = isset($_POST['page_title']) ? sanitize_text_field($_POST['page_title']) : '';
12377 -
12378 - if (empty($session_id)) {
12379 - wp_send_json_error(['message' => 'Missing session ID']);
12380 - wp_die();
12381 - }
12382 -
12383 - global $wpdb;
12384 - $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
12385 -
12386 - // Check if we've already tracked for this session
12387 - $existing = $wpdb->get_var($wpdb->prepare(
12388 - "SELECT COUNT(*) FROM $table_name
12389 - WHERE session_id = %s
12390 - AND originating_page_url IS NOT NULL",
12391 - $session_id
12392 - ));
12393 -
12394 - if ($existing > 0) {
12395 - wp_send_json_success(['message' => 'Already tracked']);
12396 - wp_die();
12397 - }
12398 -
12399 - // Update the first message in this session with originating page info
12400 - $wpdb->query($wpdb->prepare(
12401 - "UPDATE $table_name
12402 - SET originating_page_url = %s,
12403 - originating_page_title = %s
12404 - WHERE session_id = %s
12405 - ORDER BY timestamp ASC
12406 - LIMIT 1",
12407 - $page_url,
12408 - $page_title,
12409 - $session_id
12410 - ));
12411 -
12412 - wp_send_json_success(['message' => 'Originating page tracked']);
12413 - wp_die();
12414 -}
12415 -
12416 -/**
12417 - * Validate and clean URLs from AI response
12418 - * Removes any URLs that aren't in the knowledge base
12419 - *
12420 - * @param string $response_text The AI-generated response
12421 - * @param array $valid_urls Array of URLs from the knowledge base
12422 - * @return string Cleaned response with invalid URLs removed/flagged
12423 - */
12424 -private function validate_and_clean_urls($response_text, $valid_urls) {
12425 - // DEBUG: Log what we're working with
12426 - //error_log("=== MxChat URL Validation Debug ===");
12427 - //error_log("Valid URLs count: " . count($valid_urls));
12428 - //error_log("Valid URLs: " . print_r($valid_urls, true));
12429 - //error_log("Response text length: " . strlen($response_text));
12430 - //error_log("Response text preview: " . substr($response_text, 0, 500));
12431 -
12432 - // If no valid URLs provided or empty response, return as-is
12433 - if (empty($valid_urls) || empty($response_text)) {
12434 - //error_log("Validation skipped - empty valid_urls or response");
12435 - return $response_text;
12436 - }
12437 -
12438 - // Extract all URLs from the AI response
12439 - // This regex matches http:// and https:// URLs
12440 - preg_match_all(
12441 - '#\bhttps?://[^\s<>"\')\]]+#i',
12442 - $response_text,
12443 - $matches
12444 - );
12445 -
12446 - // If no URLs found in response, return as-is
12447 - if (empty($matches[0])) {
12448 - //error_log("No URLs found in response");
12449 - return $response_text;
12450 - }
12451 -
12452 - $found_urls = $matches[0];
12453 - $cleaned_response = $response_text;
12454 - $removed_count = 0;
12455 -
12456 - // Normalize valid URLs for comparison (remove trailing slashes, fragments, etc.)
12457 - $normalized_valid_urls = array_map(function($url) {
12458 - // Remove trailing slash
12459 - $url = rtrim($url, '/');
12460 - // Remove URL fragments (#section)
12461 - $url = preg_replace('/#.*$/', '', $url);
12462 - // Remove trailing punctuation that might have been captured
12463 - $url = rtrim($url, '.,;:!?');
12464 - return $url;
12465 - }, $valid_urls);
12466 -
12467 - //error_log("Normalized valid URLs: " . print_r($normalized_valid_urls, true));
12468 -
12469 - foreach ($found_urls as $found_url) {
12470 - // Clean up the found URL (remove trailing punctuation that might have been captured)
12471 - $clean_found_url = rtrim($found_url, '.,;:!?)');
12472 -
12473 - // DEBUG: Log each URL being checked
12474 - //error_log("Checking found URL: " . $found_url);
12475 -
12476 - // Normalize for comparison
12477 - $normalized_found = rtrim($clean_found_url, '/');
12478 - $normalized_found = preg_replace('/#.*$/', '', $normalized_found);
12479 -
12480 - //error_log("Normalized found URL: " . $normalized_found);
12481 -
12482 - // Check if this URL exists in our valid URLs list
12483 - $is_valid = false;
12484 -
12485 - //error_log("Starting validation checks for: " . $normalized_found);
12486 -
12487 - // First, try exact match
12488 - if (in_array($normalized_found, $normalized_valid_urls)) {
12489 - $is_valid = true;
12490 - //error_log("EXACT MATCH FOUND");
12491 - } else {
12492 - //error_log("No exact match, checking variations...");
12493 - // If no exact match, check if it's a variation (with query params, etc.)
12494 - foreach ($normalized_valid_urls as $valid_url) {
12495 - //error_log(" Comparing against valid URL: " . $valid_url);
12496 -
12497 - // Check if the found URL starts with a valid URL (handles query params)
12498 - if (strpos($normalized_found, $valid_url) === 0) {
12499 - // Check what comes after the valid URL
12500 - $remainder = substr($normalized_found, strlen($valid_url));
12501 -
12502 - // Only valid if:
12503 - // 1. Exact match (remainder is empty)
12504 - // 2. Query params (starts with ?)
12505 - // 3. Fragment (starts with #)
12506 - if (empty($remainder) || $remainder[0] === '?' || $remainder[0] === '#') {
12507 - $is_valid = true;
12508 - //error_log(" MATCH: Found URL is valid variation of base URL");
12509 - break;
12510 - } else {
12511 - //error_log(" NOT A MATCH: Found URL extends path beyond valid URL (remainder: " . $remainder . ")");
12512 - }
12513 - }
12514 - // Also check the reverse (in case valid URL has query params)
12515 - if (strpos($valid_url, $normalized_found) === 0) {
12516 - $is_valid = true;
12517 - //error_log(" MATCH: Valid URL starts with found URL");
12518 - break;
12519 - }
12520 - }
12521 -
12522 - if (!$is_valid) {
12523 - //error_log("NO MATCH FOUND - URL should be removed");
12524 - }
12525 - }
12526 -
12527 - // If URL is not valid, remove it from the response
12528 - if (!$is_valid) {
12529 - // Log the removal for debugging
12530 - //error_log("MxChat: Removed hallucinated URL: " . $found_url);
12531 - //error_log("MxChat: Valid URLs were: " . implode(', ', array_slice($normalized_valid_urls, 0, 5)));
12532 -
12533 - $removed_count++;
12534 -
12535 - // Check if URL is part of a markdown link: [text](url)
12536 - $markdown_pattern = '/\[([^\]]+)\]\(' . preg_quote($found_url, '/') . '\)/';
12537 - if (preg_match($markdown_pattern, $cleaned_response)) {
12538 - //error_log("Found markdown link, removing but keeping text");
12539 - // Remove the markdown link but keep the text
12540 - $cleaned_response = preg_replace($markdown_pattern, '$1', $cleaned_response);
12541 - }
12542 - // Check if URL is part of an HTML link: <a href="url">text</a>
12543 - else if (preg_match('/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>(.*?)<\/a>/i', $cleaned_response, $link_match)) {
12544 - //error_log("Found HTML link, removing but keeping text");
12545 - // Remove the HTML link but keep the text
12546 - $link_text = $link_match[1];
12547 - $cleaned_response = preg_replace(
12548 - '/<a[^>]*href=["\']' . preg_quote($found_url, '/') . '["\'][^>]*>.*?<\/a>/i',
12549 - $link_text,
12550 - $cleaned_response
12551 - );
12552 - }
12553 - // Otherwise just remove the bare URL
12554 - else {
12555 - //error_log("Removing bare URL");
12556 - $cleaned_response = str_replace($found_url, '', $cleaned_response);
12557 - }
12558 - }
12559 - }
12560 -
12561 - // Log summary if any URLs were removed
12562 - if ($removed_count > 0) {
12563 - //error_log("MxChat: URL Validation Summary - Removed {$removed_count} hallucinated URL(s)");
12564 - } else {
12565 - //error_log("MxChat: URL Validation Summary - No URLs removed, all were valid");
12566 - }
12567 -
12568 - // Clean up any double spaces or awkward punctuation left behind
12569 - // IMPORTANT: Only collapse horizontal whitespace (spaces/tabs), preserve newlines for markdown formatting
12570 - $cleaned_response = preg_replace('/[^\S\n]+/', ' ', $cleaned_response); // Collapse spaces/tabs but NOT newlines
12571 - $cleaned_response = preg_replace('/[^\S\n]+([.,;:!?])/', '$1', $cleaned_response); // Same for punctuation cleanup
12572 -
12573 - //error_log("Final cleaned response: " . $cleaned_response);
12574 -
12575 - return trim($cleaned_response);
12576 -}
12577 -
12578 -/**
12579 - * AJAX handler to get current chat mode for a session
12580 - */
12581 -public function mxchat_get_current_chat_mode() {
12582 - // Verify nonce for security
12583 - if (!isset($_POST['nonce']) || !MxChat_Integrator::mxchat_verify_chat_send_nonce($_POST['nonce'])) {
12584 - wp_send_json_error(['message' => 'Invalid nonce']);
12585 - wp_die();
12586 - }
12587 -
12588 - $session_id = isset($_POST['session_id']) ? sanitize_text_field($_POST['session_id']) : '';
12589 -
12590 - if (empty($session_id)) {
12591 - wp_send_json_error(['message' => 'Session ID missing']);
12592 - wp_die();
12593 - }
12594 -
12595 - // Get the current chat mode for this session
12596 - $chat_mode = get_option("mxchat_mode_{$session_id}", 'ai');
12597 -
12598 - wp_send_json_success([
12599 - 'chat_mode' => $chat_mode
12600 - ]);
12601 - wp_die();
12602 -}
12603 -
12604 -
12605 -
12606 -}
12607 -?>
1 +<?php
2 +if (!defined('ABSPATH')) {
3 + exit;
4 +}
5 +
6 +class MxChat_Integrator {
7 + private $options;
8 + private $chat_count;
9 +
10 +public function __construct() {
11 + $this->options = get_option('mxchat_options');
12 + $this->chat_count = get_option('mxchat_chat_count', 0);
13 +
14 + // Add WooCommerce hooks
15 + add_action('wp_insert_post', array($this, 'mxchat_handle_product_change'), 10, 3);
16 +
17 + // Ensure embeddings are removed when a product is moved to trash or permanently deleted
18 + add_action('wp_trash_post', array($this, 'mxchat_handle_product_delete'));
19 + add_action('before_delete_post', array($this, 'mxchat_handle_product_delete'));
20 +
21 + add_action('wp_enqueue_scripts', array($this, 'mxchat_enqueue_scripts_styles'));
22 + add_action('wp_ajax_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
23 + add_action('wp_ajax_nopriv_mxchat_handle_chat_request', array($this, 'mxchat_handle_chat_request'));
24 + add_action('wp_ajax_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
25 + add_action('wp_ajax_nopriv_mxchat_dismiss_pre_chat_message', array($this, 'mxchat_dismiss_pre_chat_message'));
26 +
27 + if (!wp_next_scheduled('mxchat_reset_rate_limits')) {
28 + wp_schedule_event(time(), 'daily', 'mxchat_reset_rate_limits');
29 + }
30 +
31 + add_action('mxchat_reset_rate_limits', array($this, 'mxchat_reset_rate_limits'));
32 +}
33 +
34 +public function mxchat_handle_product_change($post_id, $post, $update) {
35 + // Ensure this is a product post type
36 + if ($post->post_type !== 'product') {
37 + return;
38 + }
39 +
40 + // Only generate embeddings if the product is published
41 + if ($post->post_status === 'publish') {
42 + // Delay the embedding slightly to ensure all product data is available
43 + add_action('shutdown', function() use ($post_id) {
44 + $product = wc_get_product($post_id);
45 + if ($product && $product->get_price() !== '') {
46 + $this->mxchat_store_product_embedding($product);
47 + } else {
48 + // Optionally, log or handle the case where product data is incomplete
49 + error_log("Product {$post_id} does not have complete data. Embedding not generated.");
50 + }
51 + });
52 + }
53 +}
54 +
55 +public function mxchat_handle_product_delete($post_id) {
56 + if (get_post_type($post_id) !== 'product') {
57 + return;
58 + }
59 +
60 + global $wpdb;
61 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
62 +
63 + // Delete the embedding associated with this product
64 + $wpdb->delete($table_name, array('source_url' => get_permalink($post_id)), array('%s'));
65 +}
66 +
67 +private function mxchat_store_product_embedding($product) {
68 + if (isset($this->options['enable_woocommerce_integration']) && $this->options['enable_woocommerce_integration'] === '1') {
69 +
70 + $source_url = get_permalink($product->get_id());
71 + $regular_price = $product->get_regular_price();
72 + $sale_price = $product->get_sale_price();
73 + $price = $sale_price ?: $regular_price;
74 +
75 + $description = $product->get_description() . "\n\n" .
76 + "Short Description: " . $product->get_short_description() . "\n" .
77 + "Price: " . $regular_price . "\n" .
78 + "Sale Price: " . ($sale_price ?: 'N/A') . "\n" .
79 + "SKU: " . $product->get_sku();
80 +
81 + global $wpdb;
82 + $table_name = $wpdb->prefix . 'mxchat_system_prompt_content';
83 +
84 + // Delete any existing embedding for this product
85 + $wpdb->delete($table_name, array('source_url' => $source_url), array('%s'));
86 +
87 + // Submit the new content and embedding to the database
88 + MxChat_Utils::submit_content_to_db($description, $source_url, $this->options['api_key']);
89 + }
90 +}
91 +
92 +
93 +
94 +
95 +
96 + private function mxchat_increment_chat_count() {
97 + $chat_count = get_option('mxchat_chat_count', 0);
98 + $chat_count++;
99 + update_option('mxchat_chat_count', $chat_count);
100 + }
101 +
102 +public function mxchat_fetch_conversation_history_for_ajax($session_id) {
103 + global $wpdb;
104 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
105 +
106 + // Prepare and execute the query safely
107 + $chat_transcripts = $wpdb->get_results(
108 + $wpdb->prepare("SELECT * FROM $table_name WHERE session_id = %s ORDER BY timestamp ASC", sanitize_text_field($session_id))
109 + );
110 +
111 + // Check if results are empty
112 + if (empty($chat_transcripts)) {
113 + return [];
114 + }
115 +
116 + // Build the conversation history
117 + $conversation_history = [];
118 + foreach ($chat_transcripts as $transcript) {
119 + $conversation_history[] = [
120 + 'role' => $transcript->role,
121 + 'content' => $transcript->message
122 + ];
123 + }
124 +
125 + return $conversation_history;
126 +}
127 +
128 +
129 +private function mxchat_save_chat_message($session_id, $role, $message) {
130 + global $wpdb;
131 + $table_name = $wpdb->prefix . 'mxchat_chat_transcripts';
132 +
133 + $user_id = is_user_logged_in() ? get_current_user_id() : 0;
134 + $user_identifier = MxChat_User::mxchat_get_user_identifier();
135 + $user_email = MxChat_User::mxchat_get_user_email();
136 +
137 + $wpdb->insert($table_name, [
138 + 'user_id' => $user_id,
139 + 'user_identifier' => $user_identifier,
140 + 'user_email' => $user_email,
141 + 'session_id' => $session_id,
142 + 'role' => $role,
143 + 'message' => $message,
144 + 'timestamp' => current_time('mysql', 1)
145 + ]);
146 +}
147 +
148 +public function mxchat_handle_chat_request() {
149 + global $wpdb;
150 +
151 + // Get and sanitize the user identifier
152 + $user_id = $this->mxchat_get_user_identifier();
153 + $user_id = sanitize_key($user_id);
154 +
155 + // Manage rate limiting
156 + $rate_limit_transient_key = 'mxchat_chat_limit_' . $user_id;
157 + $chat_count = get_transient($rate_limit_transient_key);
158 + $session_transient_key = 'mxchat_chat_session_' . $user_id;
159 + $session_id = get_transient($session_transient_key);
160 +
161 + if ($chat_count === false) {
162 + $chat_count = 0;
163 + }
164 +
165 + if ($session_id === false) {
166 + $session_id = uniqid('mxchat_chat_', true);
167 + set_transient($session_transient_key, $session_id, DAY_IN_SECONDS); // Store session ID for a day
168 + }
169 +
170 + $rate_limit_option = isset($this->options['rate_limit']) ? $this->options['rate_limit'] : 'unlimited';
171 +
172 + // Check if rate limit is not 'unlimited'
173 + if ($rate_limit_option !== 'unlimited') {
174 + $rate_limit = intval($rate_limit_option);
175 +
176 + if ($chat_count >= $rate_limit) {
177 + wp_send_json_error('Rate limit exceeded. Please try again later.');
178 + wp_die();
179 + }
180 + set_transient($rate_limit_transient_key, $chat_count + 1, DAY_IN_SECONDS);
181 + }
182 +
183 + // Validate and sanitize the incoming message
184 + if (!isset($_POST['message'])) {
185 + wp_send_json_error('No message received');
186 + wp_die();
187 + }
188 +
189 + $message = sanitize_text_field($_POST['message']);
190 + if (empty($message)) {
191 + wp_send_json_error('Message is empty or invalid.');
192 + wp_die();
193 + }
194 +
195 + // Initialize the variable with the original message
196 + $message_with_order_details = $message;
197 +
198 + // Check if the user asked about orders
199 + if (MxChat_WooCommerce::mxchat_is_order_related_query($message)) {
200 + $order_details = MxChat_WooCommerce::mxchat_fetch_user_orders_details();
201 +
202 + // If order details are available, append them to the user's message
203 + if (!empty($order_details)) {
204 + $message_with_order_details = $message . "\n\n" . $order_details;
205 + }
206 + }
207 +
208 +
209 + // Save the combined message to the database
210 + $this->mxchat_save_chat_message($session_id, 'user', $message_with_order_details);
211 +
212 + // Generate and validate the embedding
213 + $user_message_embedding = $this->mxchat_generate_embedding($message_with_order_details, $this->options['api_key']);
214 + if (!is_array($user_message_embedding)) {
215 + wp_send_json_error('Error processing your message.');
216 + wp_die();
217 + }
218 +
219 + // Find relevant content based on embedding
220 + $relevant_content = $this->mxchat_find_relevant_content($user_message_embedding);
221 +
222 + // Fetch conversation history from the database
223 + $conversation_history = $this->mxchat_fetch_conversation_history_for_ajax($session_id);
224 +
225 + // Increment the chat count
226 + $this->mxchat_increment_chat_count();
227 +
228 + // Generate a response from the AI model
229 + $response = $this->mxchat_generate_response($relevant_content, $this->options['api_key'], $conversation_history);
230 +
231 + // Save the bot response to the database
232 + $this->mxchat_save_chat_message($session_id, 'bot', $response);
233 +
234 + // Send the response back to the client
235 + wp_send_json(['message' => $response]);
236 +
237 + wp_die();
238 +}
239 +
240 +
241 +private function mxchat_get_user_identifier() {
242 + return MxChat_User::mxchat_get_user_identifier();
243 +}
244 +
245 +
246 +
247 + private function mxchat_generate_embedding($text, $api_key) {
248 + $endpoint = 'https://api.openai.com/v1/embeddings';
249 +
250 + $body = wp_json_encode([
251 + 'input' => $text,
252 + 'model' => 'text-embedding-ada-002'
253 + ]);
254 +
255 + $args = [
256 + 'body' => $body,
257 + 'headers' => [
258 + 'Content-Type' => 'application/json',
259 + 'Authorization' => 'Bearer ' . $api_key,
260 + ],
261 + 'timeout' => 60,
262 + 'redirection' => 5,
263 + 'blocking' => true,
264 + 'httpversion' => '1.0',
265 + 'sslverify' => true,
266 + ];
267 +
268 + $response = wp_remote_post($endpoint, $args);
269 +
270 + if (is_wp_error($response)) {
271 + return null;
272 + }
273 +
274 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
275 +
276 + if (isset($response_body['data'][0]['embedding']) && is_array($response_body['data'][0]['embedding'])) {
277 + return $response_body['data'][0]['embedding'];
278 + } else {
279 + return null;
280 + }
281 + }
282 +
283 +private function mxchat_find_relevant_content($user_embedding) {
284 + global $wpdb;
285 + $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
286 +
287 + // Define a cache key for embeddings
288 + $cache_key = 'mxchat_system_prompt_embeddings';
289 +
290 + // Attempt to get the embeddings from the cache
291 + $embeddings = wp_cache_get($cache_key, 'mxchat_system_prompts');
292 +
293 + if ($embeddings === false) {
294 + // Cache miss, query the database and cache the results
295 + $query = "SELECT id, embedding_vector FROM {$system_prompt_table}";
296 + $embeddings = $wpdb->get_results($query);
297 +
298 + if ($embeddings === null || empty($embeddings)) {
299 + error_log("No embeddings found in the database.");
300 + return null; // Return null to handle no embeddings gracefully
301 + }
302 +
303 + // Cache the results if successful
304 + wp_cache_set($cache_key, $embeddings, 'mxchat_system_prompts', 3600); // Cache for 1 hour
305 + }
306 +
307 + $most_relevant_id = null;
308 + $highest_similarity = -INF;
309 +
310 + foreach ($embeddings as $embedding) {
311 + $database_embedding = maybe_unserialize($embedding->embedding_vector);
312 +
313 + // Debugging: Log the embeddings
314 + // if (!is_array($database_embedding)) {
315 + // error_log("Invalid database embedding format for ID {$embedding->id}: " . print_r($database_embedding, true));
316 + // continue;
317 + // }
318 +
319 + if (is_array($user_embedding)) {
320 + $similarity = $this->mxchat_calculate_cosine_similarity($user_embedding, $database_embedding);
321 +
322 + // Debugging: Log the similarity score
323 + // error_log("Calculated similarity for ID {$embedding->id}: {$similarity}");
324 +
325 + if ($similarity > $highest_similarity) {
326 + $highest_similarity = $similarity;
327 + $most_relevant_id = $embedding->id;
328 + }
329 + } else {
330 + // error_log("User embedding is not an array. Embedding data: " . print_r($user_embedding, true));
331 + }
332 + }
333 +
334 + if ($most_relevant_id !== null) {
335 + // Fetch content with product links
336 + return $this->fetch_content_with_product_links($most_relevant_id);
337 + }
338 +
339 + error_log("No relevant content found. Most relevant ID was null.");
340 + return null; // Return null if no relevant content is found
341 +}
342 +
343 +
344 +private function fetch_content_with_product_links($most_relevant_id) {
345 + global $wpdb;
346 + $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
347 +
348 + // Fetch the article content and associated product URL
349 + $query = $wpdb->prepare("SELECT article_content, source_url FROM {$system_prompt_table} WHERE id = %d", $most_relevant_id);
350 + $result = $wpdb->get_row($query);
351 +
352 + if ($result) {
353 + // Append the product link to the content if available
354 + $content = $result->article_content;
355 + if (!empty($result->source_url)) {
356 + $content .= "\n\nFor more details, check out this product: " . esc_url($result->source_url);
357 + }
358 + return $content;
359 + }
360 +
361 + return null;
362 +}
363 +
364 +
365 + private function mxchat_generate_response($relevant_content, $api_key, $conversation_history) {
366 + if (!$relevant_content) {
367 + return "I'm sorry, I couldn't find relevant information on that topic.";
368 + }
369 +
370 + $system_prompt_instructions = isset($this->options['system_prompt_instructions']) ? $this->options['system_prompt_instructions'] : '';
371 +
372 + $content_with_instructions = $system_prompt_instructions . " " . $relevant_content;
373 +
374 + array_unshift($conversation_history, [
375 + 'role' => 'system',
376 + 'content' => "Here are your instructions: " . $content_with_instructions
377 + ]);
378 +
379 + foreach ($conversation_history as &$message) {
380 + if ($message['role'] === 'bot') {
381 + $message['role'] = 'assistant';
382 + }
383 + }
384 +
385 + $api_url = 'https://api.openai.com/v1/chat/completions';
386 +
387 + $body = json_encode([
388 + 'model' => isset($this->options['model']) ? $this->options['model'] : 'gpt-3.5',
389 + 'messages' => $conversation_history,
390 + ]);
391 +
392 + $args = [
393 + 'body' => $body,
394 + 'headers' => [
395 + 'Content-Type' => 'application/json',
396 + 'Authorization' => 'Bearer ' . $api_key,
397 + ],
398 + 'timeout' => 60,
399 + 'redirection' => 5,
400 + 'blocking' => true,
401 + 'httpversion' => '1.0',
402 + 'sslverify' => true,
403 + ];
404 +
405 + $response = wp_remote_post($api_url, $args);
406 +
407 + if (is_wp_error($response)) {
408 + return "Sorry, there was an error processing your request.";
409 + }
410 +
411 + $response_body = json_decode(wp_remote_retrieve_body($response), true);
412 +
413 + if (isset($response_body['choices'][0]['message']['content'])) {
414 + if (isset($response_body['usage'])) {
415 + $prompt_tokens = $response_body['usage']['prompt_tokens'];
416 + $total_tokens = $response_body['usage']['total_tokens'];
417 + }
418 + return trim($response_body['choices'][0]['message']['content']);
419 + } else {
420 + return "Sorry, I couldn't process that request.";
421 + }
422 +}
423 +
424 +
425 +public function mxchat_dismiss_pre_chat_message() {
426 + // Get and sanitize the user identifier
427 + $user_id = $this->mxchat_get_user_identifier();
428 + $user_id = sanitize_key($user_id);
429 +
430 + // Set a transient to track that the user has dismissed the pre-chat message
431 + $transient_key = 'mxchat_pre_chat_message_dismissed_' . $user_id;
432 + set_transient($transient_key, true, DAY_IN_SECONDS);
433 +
434 + wp_send_json_success();
435 +}
436 +
437 +
438 +
439 + private function mxchat_calculate_cosine_similarity($vectorA, $vectorB) {
440 + if (!is_array($vectorA) || !is_array($vectorB) || empty($vectorA) || empty($vectorB)) {
441 + return 0;
442 + }
443 +
444 + $dotProduct = array_sum(array_map(function ($a, $b) {
445 + return $a * $b;
446 + }, $vectorA, $vectorB));
447 + $normA = sqrt(array_sum(array_map(function ($a) {
448 + return $a * $a;
449 + }, $vectorA)));
450 + $normB = sqrt(array_sum(array_map(function ($b) {
451 + return $b * $b;
452 + }, $vectorB)));
453 +
454 + if ($normA == 0 || $normB == 0) {
455 + return 0;
456 + }
457 +
458 + return $dotProduct / ($normA * $normB);
459 + }
460 +
461 + public function mxchat_enqueue_scripts_styles() {
462 + // Define version numbers for the styles and scripts
463 + $chat_style_version = '1.0.8'; // Replace with your actual version
464 + $chat_script_version = '1.0.8'; // Replace with your actual version
465 +
466 + // Correct path to the script file
467 + wp_enqueue_script(
468 + 'mxchat-chat-js', // Handle for the script
469 + plugin_dir_url(__FILE__) . '../js/chat-script.js', // Correct path using __FILE__
470 + array('jquery'), // Dependencies
471 + $chat_script_version, // Version for cache busting
472 + true // Load script in footer
473 + );
474 +
475 + // Enqueue the CSS file similarly
476 + wp_enqueue_style(
477 + 'mxchat-chat-css', // Handle for the style
478 + plugin_dir_url(__FILE__) . '../css/chat-style.css', // Correct path using __FILE__
479 + array(), // No dependencies
480 + $chat_style_version // Version for cache busting
481 + );
482 +
483 + // Fetch options from the database
484 + $this->options = get_option('mxchat_options');
485 +
486 + // Prepare settings to pass to JavaScript
487 + $style_settings = array(
488 + 'ajax_url' => admin_url('admin-ajax.php'),
489 + 'nonce' => wp_create_nonce('mxchat_chat_nonce'), // Nonce for security
490 + 'rate_limit_message' => 'Rate limit exceeded. Please try again later.',
491 + 'appendWidgetToBody' => $this->options['append_to_body'] ?? 'off'
492 + );
493 +
494 + // Localize the script with necessary data
495 + wp_localize_script('mxchat-chat-js', 'mxchatChat', $style_settings);
496 + }
497 +
498 +
499 +
500 + public function mxchat_reset_rate_limits() {
501 + global $wpdb;
502 +
503 + // Define a cache key pattern for rate limits
504 + $cache_key_pattern = 'mxchat_chat_limit_%';
505 +
506 + // Retrieve all option names matching the pattern
507 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
508 + $option_names = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
509 +
510 + // db call ok; no-cache ok
511 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- db call ok
512 + $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mxchat_chat_limit_%'");
513 +
514 + // Clear the relevant cache entries
515 + foreach ($option_names as $option_name) {
516 + wp_cache_delete($option_name, 'options');
517 + }
518 +
519 + // Optionally, clear a general cache if you have one
520 + wp_cache_delete('mxchat_all_chat_limits', 'options');
521 + }
522 +
523 +
524 +private function mxchat_fetch_woocommerce_products() {
525 + // Ensure WooCommerce is active
526 + if (!class_exists('WooCommerce')) {
527 + return [];
528 + }
529 +
530 + $args = array(
531 + 'post_type' => 'product',
532 + 'post_status' => 'publish',
533 + 'posts_per_page' => -1,
534 + );
535 +
536 + $products = get_posts($args);
537 + $product_data = [];
538 +
539 + foreach ($products as $product) {
540 + $product_id = $product->ID;
541 + $product_obj = wc_get_product($product_id);
542 +
543 + $product_data[] = array(
544 + 'id' => $product_id,
545 + 'name' => $product_obj->get_name(),
546 + 'description' => $product_obj->get_description(),
547 + 'short_description' => $product_obj->get_short_description(),
548 + 'url' => get_permalink($product_id),
549 + 'price' => $product_obj->get_regular_price(),
550 + 'sale_price' => $product_obj->get_sale_price(),
551 + 'stock_status' => $product_obj->get_stock_status(),
552 + 'sku' => $product_obj->get_sku(),
553 + 'in_stock' => $product_obj->is_in_stock(),
554 + 'total_sales' => $product_obj->get_total_sales(),
555 + );
556 + }
557 +
558 + return $product_data;
559 +}
560 +
561 +
562 +
563 +
564 +
565 +
566 +
567 +}
568 +?>