← All changes
|
app/Services/Integrations/FluentBot/FluentBotAPI.php
+6
-15
2.2.1
→
2.1.2
View file →
| @@ -22,10 +22,9 @@ | ||
| 22 | 22 | $code = $response->get_error_code(); |
| 23 | 23 | return new \WP_Error($code, $message); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - $rawBody = wp_remote_retrieve_body($response); | |
| 27 | - $responseBody = json_decode($rawBody, true) ?? []; | |
| 26 | + $responseBody = json_decode(wp_remote_retrieve_body($response), true) ?? []; | |
| 28 | 27 | |
| 29 | 28 | if (!$responseBody || !is_array($responseBody)) { |
| 30 | 29 | return new \WP_Error('fluent_bot_error', __('Invalid or empty response from API', 'fluent-support')); |
| 31 | 30 | } |
| @@ -47,11 +46,10 @@ | ||
| 47 | 46 | if (empty($content)) { |
| 48 | 47 | return new \WP_Error('fluent_bot_error', __('No AI response found in the API response.', 'fluent-support')); |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | - $tokenUsage = $responseBody['token_usage'] ?? []; | |
| 52 | - $totalTokens = ($tokenUsage['input_tokens'] ?? 0) + ($tokenUsage['output_tokens'] ?? 0); | |
| 53 | - do_action('fluent_support/ai_response_success', $ticketId, $prompt, $totalTokens, "FluentBot"); | |
| 50 | + $totalTokens = $responseBody['token_usage']['total_tokens'] ?? $responseBody['totalTokens'] ?? 0; | |
| 51 | + do_action('fluent_support/ai_response_success', $ticketId, $prompt, $totalTokens, "Fluent Bot"); | |
| 54 | 52 | |
| 55 | 53 | // Return both content and chat_id if available |
| 56 | 54 | return [ |
| 57 | 55 | 'content' => $content, |
| @@ -81,11 +79,10 @@ | ||
| 81 | 79 | ]); |
| 82 | 80 | |
| 83 | 81 | $buffer = ''; |
| 84 | 82 | $conversationId = null; |
| 85 | - $streamTokens = 0; | |
| 86 | 83 | |
| 87 | - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$buffer, &$conversationId, &$streamTokens) { | |
| 84 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$buffer, &$conversationId) { | |
| 88 | 85 | $buffer .= $data; |
| 89 | 86 | |
| 90 | 87 | // Process complete SSE events from the AI API |
| 91 | 88 | $events = explode("\n\n", $buffer); |
| @@ -119,10 +116,9 @@ | ||
| 119 | 116 | |
| 120 | 117 | // Handle multiple data lines properly |
| 121 | 118 | if (!empty($eventDataLines)) { |
| 122 | 119 | foreach ($eventDataLines as $dataLine) { |
| 123 | - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- raw SSE payload from trusted bot endpoint; rendered output sanitized client-side | |
| 124 | - echo "data: ".$dataLine."\n"; | |
| 120 | + echo "data: ".esc_html($dataLine)."\n"; | |
| 125 | 121 | } |
| 126 | 122 | } else { |
| 127 | 123 | echo "data: \n"; |
| 128 | 124 | } |
| @@ -131,13 +127,8 @@ | ||
| 131 | 127 | |
| 132 | 128 | // Store chat_id for later use |
| 133 | 129 | if ($eventType === 'chat_id' && !empty($eventDataLines)) { |
| 134 | 130 | $conversationId = $eventDataLines[0]; |
| 135 | - } elseif ($eventType === 'token_usage' && !empty($eventDataLines)) { | |
| 136 | - $usage = json_decode($eventDataLines[0], true); | |
| 137 | - if (is_array($usage)) { | |
| 138 | - $streamTokens = ($usage['input_tokens'] ?? 0) + ($usage['output_tokens'] ?? 0); | |
| 139 | - } | |
| 140 | 131 | } |
| 141 | 132 | |
| 142 | 133 | flush(); |
| 143 | 134 | } |
| @@ -169,9 +160,9 @@ | ||
| 169 | 160 | // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_getinfo |
| 170 | 161 | // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_error |
| 171 | 162 | |
| 172 | 163 | if ($httpCode === 200) { |
| 173 | - do_action('fluent_support/ai_response_success', $ticketId, $prompt, $streamTokens, "FluentBot"); | |
| 164 | + do_action('fluent_support/ai_response_success', $ticketId, $prompt, 0, "Fluent Bot"); | |
| 174 | 165 | } |
| 175 | 166 | } |
| 176 | 167 | |
| 177 | 168 | protected function sendRequest(array $payload) |