← All changes
|
app/Services/Integrations/FluentBot/FluentBotAPI.php
+175
-66
1.10.5
→
2.4.0
View file →
| @@ -5,15 +5,16 @@ | ||
| 5 | 5 | use WP_Error; |
| 6 | 6 | |
| 7 | 7 | class FluentBotAPI |
| 8 | 8 | { |
| 9 | + protected $apiUrl; | |
| 10 | + | |
| 9 | 11 | protected $apiKey; |
| 10 | - protected $apiUrl; | |
| 11 | 12 | |
| 12 | - public function __construct(?string $apiKey, string $apiUrl) | |
| 13 | + public function __construct(string $apiUrl, string $apiKey = '') | |
| 13 | 14 | { |
| 15 | + $this->apiUrl = $apiUrl; | |
| 14 | 16 | $this->apiKey = $apiKey; |
| 15 | - $this->apiUrl = $apiUrl; | |
| 16 | 17 | } |
| 17 | 18 | |
| 18 | 19 | public function makeRequest(int $ticketId, $prompt, array $args = []) |
| 19 | 20 | { |
| @@ -24,9 +25,10 @@ | ||
| 24 | 25 | $code = $response->get_error_code(); |
| 25 | 26 | return new \WP_Error($code, $message); |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | - $responseBody = json_decode(wp_remote_retrieve_body($response), true) ?? []; | |
| 29 | + $rawBody = wp_remote_retrieve_body($response); | |
| 30 | + $responseBody = json_decode($rawBody, true) ?? []; | |
| 29 | 31 | |
| 30 | 32 | if (!$responseBody || !is_array($responseBody)) { |
| 31 | 33 | return new \WP_Error('fluent_bot_error', __('Invalid or empty response from API', 'fluent-support')); |
| 32 | 34 | } |
| @@ -48,15 +50,16 @@ | ||
| 48 | 50 | if (empty($content)) { |
| 49 | 51 | return new \WP_Error('fluent_bot_error', __('No AI response found in the API response.', 'fluent-support')); |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | - $totalTokens = $responseBody['token_usage']['total_tokens'] ?? $responseBody['totalTokens'] ?? 0; | |
| 53 | - do_action('fluent_support/ai_response_success', $ticketId, $prompt, $totalTokens, "Fluent Bot"); | |
| 54 | + $tokenUsage = $responseBody['token_usage'] ?? []; | |
| 55 | + $totalTokens = ($tokenUsage['input_tokens'] ?? 0) + ($tokenUsage['output_tokens'] ?? 0); | |
| 56 | + do_action('fluent_support/ai_response_success', $ticketId, $prompt, $totalTokens, "FluentBot"); | |
| 54 | 57 | |
| 55 | - // Return both content and conversation_id if available | |
| 58 | + // Return both content and chat_id if available | |
| 56 | 59 | return [ |
| 57 | 60 | 'content' => $content, |
| 58 | - 'conversation_id' => $responseBody['conversation_id'] ?? null | |
| 61 | + 'chat_id' => $responseBody['chat_id'] ?? null | |
| 59 | 62 | ]; |
| 60 | 63 | } |
| 61 | 64 | |
| 62 | 65 | public function makeStreamRequest(int $ticketId, $prompt, array $args = []) |
| @@ -75,67 +78,28 @@ | ||
| 75 | 78 | $ch = curl_init(); |
| 76 | 79 | curl_setopt($ch, CURLOPT_URL, $this->apiUrl); |
| 77 | 80 | curl_setopt($ch, CURLOPT_POST, true); |
| 78 | 81 | curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($args)); |
| 79 | - curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
| 80 | - 'Content-Type: application/json', | |
| 81 | - !empty($this->apiKey) ? 'Authorization: Bearer ' . $this->apiKey : '' | |
| 82 | - ]); | |
| 82 | + $streamHeaders = ['Content-Type: application/json']; | |
| 83 | + if ($this->apiKey !== '') { | |
| 84 | + $streamHeaders[] = 'Authorization: Bearer ' . $this->apiKey; | |
| 85 | + } | |
| 86 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $streamHeaders); | |
| 83 | 87 | |
| 84 | 88 | $buffer = ''; |
| 85 | 89 | $conversationId = null; |
| 90 | + $streamTokens = 0; | |
| 86 | 91 | |
| 87 | - curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$buffer, &$conversationId) { | |
| 88 | - $buffer .= $data; | |
| 89 | - | |
| 90 | - // Process complete SSE events from the AI API | |
| 91 | - $events = explode("\n\n", $buffer); | |
| 92 | - $buffer = array_pop($events); // Keep incomplete event in buffer | |
| 93 | - | |
| 94 | - foreach ($events as $event) { | |
| 95 | - if (trim($event)) { | |
| 96 | - $lines = explode("\n", $event); | |
| 97 | - $eventType = ''; | |
| 98 | - $eventId = ''; | |
| 99 | - $eventDataLines = []; | |
| 100 | - | |
| 101 | - foreach ($lines as $line) { | |
| 102 | - if (strpos($line, 'event: ') === 0) { | |
| 103 | - $eventType = trim(substr($line, 7)); | |
| 104 | - } elseif (strpos($line, 'id: ') === 0) { | |
| 105 | - $eventId = trim(substr($line, 4)); | |
| 106 | - } elseif (strpos($line, 'data: ') === 0) { | |
| 107 | - $eventDataLines[] = substr($line, 6); | |
| 108 | - } | |
| 92 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$buffer, &$conversationId, &$streamTokens) { | |
| 93 | + foreach ($this->drainSseChunk($data, $buffer) as $parsed) { | |
| 94 | + // Store chat_id for later use | |
| 95 | + if ($parsed['event'] === 'chat_id' && !empty($parsed['data'])) { | |
| 96 | + $conversationId = $parsed['data'][0]; | |
| 97 | + } elseif ($parsed['event'] === 'token_usage' && !empty($parsed['data'])) { | |
| 98 | + $usage = json_decode($parsed['data'][0], true); | |
| 99 | + if (is_array($usage)) { | |
| 100 | + $streamTokens = ($usage['input_tokens'] ?? 0) + ($usage['output_tokens'] ?? 0); | |
| 109 | 101 | } |
| 110 | - | |
| 111 | - // Forward the event to the browser with proper formatting | |
| 112 | - if ($eventType) { | |
| 113 | - echo "event: ".esc_html($eventType)."\n"; | |
| 114 | - | |
| 115 | - // Include ID if present | |
| 116 | - if ($eventId !== '') { | |
| 117 | - echo "id: ".esc_html($eventId)."\n"; | |
| 118 | - } | |
| 119 | - | |
| 120 | - // Handle multiple data lines properly | |
| 121 | - if (!empty($eventDataLines)) { | |
| 122 | - foreach ($eventDataLines as $dataLine) { | |
| 123 | - echo "data: ".esc_html($dataLine)."\n"; | |
| 124 | - } | |
| 125 | - } else { | |
| 126 | - echo "data: \n"; | |
| 127 | - } | |
| 128 | - | |
| 129 | - echo "\n"; | |
| 130 | - | |
| 131 | - // Store conversation_id for later use | |
| 132 | - if ($eventType === 'conversation_id' && !empty($eventDataLines)) { | |
| 133 | - $conversationId = $eventDataLines[0]; | |
| 134 | - } | |
| 135 | - | |
| 136 | - flush(); | |
| 137 | - } | |
| 138 | 102 | } |
| 139 | 103 | } |
| 140 | 104 | |
| 141 | 105 | return strlen($data); |
| @@ -163,19 +127,164 @@ | ||
| 163 | 127 | // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_getinfo |
| 164 | 128 | // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_error |
| 165 | 129 | |
| 166 | 130 | if ($httpCode === 200) { |
| 167 | - do_action('fluent_support/ai_response_success', $ticketId, $prompt, 0, "Fluent Bot"); | |
| 131 | + do_action('fluent_support/ai_response_success', $ticketId, $prompt, $streamTokens, "FluentBot"); | |
| 168 | 132 | } |
| 169 | 133 | } |
| 170 | 134 | |
| 135 | + /** | |
| 136 | + * Forward every complete SSE frame in $data to the browser, return the parsed frames. | |
| 137 | + * $buffer holds the partial trailing frame across cURL writes, so pass it by reference. | |
| 138 | + * | |
| 139 | + * @param string $data raw bytes from cURL | |
| 140 | + * @param string $buffer incomplete frame carried over from the previous write | |
| 141 | + * @return array<int, array{event: string, data: array<int, string>}> | |
| 142 | + */ | |
| 143 | + private function drainSseChunk(string $data, string &$buffer): array | |
| 144 | + { | |
| 145 | + $buffer .= $data; | |
| 146 | + | |
| 147 | + // Process complete SSE events from the AI API | |
| 148 | + $events = explode("\n\n", $buffer); | |
| 149 | + $buffer = array_pop($events); // Keep incomplete event in buffer | |
| 150 | + | |
| 151 | + $parsed = []; | |
| 152 | + | |
| 153 | + foreach ($events as $event) { | |
| 154 | + if (!trim($event)) { | |
| 155 | + continue; | |
| 156 | + } | |
| 157 | + | |
| 158 | + // SSE comment/keepalive (starts ":"): forward raw so proxies keep seeing bytes, no idle-timeout. | |
| 159 | + if (strpos(ltrim($event), ':') === 0) { | |
| 160 | + echo $event . "\n\n"; | |
| 161 | + flush(); | |
| 162 | + continue; | |
| 163 | + } | |
| 164 | + | |
| 165 | + $lines = explode("\n", $event); | |
| 166 | + $eventType = ''; | |
| 167 | + $eventId = ''; | |
| 168 | + $eventDataLines = []; | |
| 169 | + | |
| 170 | + foreach ($lines as $line) { | |
| 171 | + if (strpos($line, 'event: ') === 0) { | |
| 172 | + $eventType = trim((string)substr($line, 7)); | |
| 173 | + } elseif (strpos($line, 'id: ') === 0) { | |
| 174 | + $eventId = trim((string)substr($line, 4)); | |
| 175 | + } elseif (strpos($line, 'data: ') === 0) { | |
| 176 | + $eventDataLines[] = (string)substr($line, 6); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + | |
| 180 | + // Forward the event to the browser with proper formatting | |
| 181 | + if (!$eventType) { | |
| 182 | + continue; | |
| 183 | + } | |
| 184 | + | |
| 185 | + echo "event: ".esc_html($eventType)."\n"; | |
| 186 | + | |
| 187 | + // Include ID if present | |
| 188 | + if ($eventId !== '') { | |
| 189 | + echo "id: ".esc_html($eventId)."\n"; | |
| 190 | + } | |
| 191 | + | |
| 192 | + // Handle multiple data lines properly | |
| 193 | + if (!empty($eventDataLines)) { | |
| 194 | + foreach ($eventDataLines as $dataLine) { | |
| 195 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- raw SSE payload from trusted bot endpoint; rendered output sanitized client-side | |
| 196 | + echo "data: ".$dataLine."\n"; | |
| 197 | + } | |
| 198 | + } else { | |
| 199 | + echo "data: \n"; | |
| 200 | + } | |
| 201 | + | |
| 202 | + echo "\n"; | |
| 203 | + flush(); | |
| 204 | + | |
| 205 | + $parsed[] = ['event' => $eventType, 'data' => $eventDataLines]; | |
| 206 | + } | |
| 207 | + | |
| 208 | + return $parsed; | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Reconnect to an in-flight turn: replay its buffered SSE and tail it live. Same wire | |
| 213 | + * format as makeStreamRequest. Upstream ends on `__done__` or an `idle` frame; a total | |
| 214 | + * cap still applies — see the timeout block below. | |
| 215 | + */ | |
| 216 | + public function makeResumeStreamRequest() | |
| 217 | + { | |
| 218 | + // Use cURL for streaming | |
| 219 | + // Note: Using cURL here because WordPress HTTP API doesn't support streaming SSE responses | |
| 220 | + // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_init | |
| 221 | + // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 222 | + // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_exec | |
| 223 | + // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_close | |
| 224 | + // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_error | |
| 225 | + // PluginCheck:ignoreFile | |
| 226 | + $ch = curl_init(); | |
| 227 | + curl_setopt($ch, CURLOPT_URL, $this->apiUrl); | |
| 228 | + curl_setopt($ch, CURLOPT_HTTPGET, true); | |
| 229 | + | |
| 230 | + $streamHeaders = ['Accept: text/event-stream']; | |
| 231 | + if ($this->apiKey !== '') { | |
| 232 | + $streamHeaders[] = 'Authorization: Bearer ' . $this->apiKey; | |
| 233 | + } | |
| 234 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $streamHeaders); | |
| 235 | + | |
| 236 | + $buffer = ''; | |
| 237 | + | |
| 238 | + curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$buffer) { | |
| 239 | + // Stop as soon as the agent navigates away — nothing here needs to | |
| 240 | + // outlive the client, the turn itself is persisted upstream. | |
| 241 | + if (connection_aborted()) { | |
| 242 | + return 0; | |
| 243 | + } | |
| 244 | + | |
| 245 | + $this->drainSseChunk($data, $buffer); | |
| 246 | + | |
| 247 | + return strlen($data); | |
| 248 | + }); | |
| 249 | + | |
| 250 | + // Bounded on purpose: connection_aborted() can lag (abort travels browser -> | |
| 251 | + // proxy -> fluent-bot, Apache buffers writes), so a refresh mid-turn can leave | |
| 252 | + // the old tail pinning a worker. The cap makes that self-limiting — the client | |
| 253 | + // treats a cut tail as a body ended without __done__ and refetches. | |
| 254 | + curl_setopt($ch, CURLOPT_TIMEOUT, apply_filters('fs_ai_resume_timeout', 120)); | |
| 255 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); | |
| 256 | + // Abort sooner when genuinely stalled: the upstream sends ': keepalive' | |
| 257 | + // during quiet gaps, so a healthy tail always beats this window. | |
| 258 | + curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1); | |
| 259 | + curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, apply_filters('fs_ai_resume_stall_timeout', 60)); | |
| 260 | + curl_setopt($ch, CURLOPT_TCP_KEEPALIVE, 1); | |
| 261 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
| 262 | + curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // Smaller buffer for faster streaming | |
| 263 | + | |
| 264 | + curl_exec($ch); | |
| 265 | + | |
| 266 | + if (curl_error($ch)) { | |
| 267 | + echo "event: error\n"; | |
| 268 | + echo "data: " . wp_json_encode(['error' => curl_error($ch)]) . "\n\n"; | |
| 269 | + flush(); | |
| 270 | + } | |
| 271 | + | |
| 272 | + curl_close($ch); | |
| 273 | + // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_init | |
| 274 | + // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 275 | + // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_exec | |
| 276 | + // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_close | |
| 277 | + // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_error | |
| 278 | + } | |
| 279 | + | |
| 171 | 280 | protected function sendRequest(array $payload) |
| 172 | 281 | { |
| 173 | 282 | $headers = [ |
| 174 | - 'Content-Type' => 'application/json', | |
| 283 | + 'Content-Type' => 'application/json', | |
| 175 | 284 | ]; |
| 176 | - // Add Authorization header only if API key is provided | |
| 177 | - if (!empty($this->apiKey)) { | |
| 285 | + | |
| 286 | + if ($this->apiKey !== '') { | |
| 178 | 287 | $headers['Authorization'] = 'Bearer ' . $this->apiKey; |
| 179 | 288 | } |
| 180 | 289 | |
| 181 | 290 | $timeout = apply_filters('fs_ai_request_timeout', 60); |