| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentSupport\Framework\Http\Request\Request; |
| 7 |
use FluentSupport\App\Http\Controllers\Controller; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\Integrations\FluentBot\FluentBotService; |
| 11 |
|
| 12 |
class FluentBotController extends Controller |
| 13 |
{ |
| 14 |
public function getPresetPrompts(Request $request) |
| 15 |
{ |
| 16 |
$type = $request->getSafe('type', 'sanitize_text_field'); |
| 17 |
|
| 18 |
try { |
| 19 |
return (new FluentBotService())->getPresetPrompts($type); |
| 20 |
} catch (\Exception $e) { |
| 21 |
return $this->sendError([ |
| 22 |
'message' => Helper::getSafeErrorMessage($e) |
| 23 |
]); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function generateResponse(Request $request) |
| 28 |
{ |
| 29 |
$ticketId = $request->getSafe('id', 'intval'); |
| 30 |
$productId = $request->getSafe('product_id', 'intval'); |
| 31 |
$prompt = $request->getSafe('content', 'sanitize_text_field'); |
| 32 |
$conversationId = $request->getSafe('chat_id', 'sanitize_text_field', ''); |
| 33 |
$selectedText = $request->getSafe('selectedText', 'sanitize_text_field', ''); |
| 34 |
$type = $request->getSafe('type', 'sanitize_text_field', 'response'); |
| 35 |
|
| 36 |
try { |
| 37 |
$customAI = new FluentBotService(); |
| 38 |
|
| 39 |
$ticket = Ticket::findOrFail($ticketId); |
| 40 |
$this->ensureCanAccessTicket($ticket); |
| 41 |
|
| 42 |
if ($type === 'modifyResponse') { |
| 43 |
$result = $customAI->modifyResponse($prompt, $selectedText, $ticketId); |
| 44 |
} else { |
| 45 |
$ticket->load('responses'); |
| 46 |
$result = $customAI->generateResponse($prompt, $ticket, $productId, $conversationId ?: null); |
| 47 |
} |
| 48 |
|
| 49 |
return $result; |
| 50 |
} catch (\Exception $e) { |
| 51 |
return $this->sendError([ |
| 52 |
'message' => Helper::getSafeErrorMessage($e) |
| 53 |
]); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function generateStreamResponse(Request $request) |
| 58 |
{ |
| 59 |
$ticketId = $request->getSafe('id', 'intval'); |
| 60 |
$productId = $request->getSafe('product_id', 'intval'); |
| 61 |
$prompt = $request->getSafe('content', 'sanitize_text_field'); |
| 62 |
$conversationId = $request->getSafe('chat_id', 'sanitize_text_field', ''); |
| 63 |
$selectedText = $request->getSafe('selectedText', 'sanitize_text_field', ''); |
| 64 |
$type = $request->getSafe('type', 'sanitize_text_field', 'response'); |
| 65 |
|
| 66 |
try { |
| 67 |
$customAI = new FluentBotService(); |
| 68 |
$ticket = Ticket::findOrFail($ticketId); |
| 69 |
$this->ensureCanAccessTicket($ticket); |
| 70 |
|
| 71 |
if ($type === 'modifyResponse') { |
| 72 |
$result = $customAI->modifyResponse($prompt, $selectedText, $ticketId); |
| 73 |
return $result; |
| 74 |
} else { |
| 75 |
$ticket->load('responses'); |
| 76 |
|
| 77 |
// Disable all output buffering (with safety limit) |
| 78 |
$maxLevels = 10; |
| 79 |
while (ob_get_level() && $maxLevels-- > 0) { |
| 80 |
ob_end_clean(); |
| 81 |
} |
| 82 |
|
| 83 |
// Set headers for Server-Sent Events |
| 84 |
header('Content-Type: text/event-stream'); |
| 85 |
header('Cache-Control: no-cache'); |
| 86 |
header('Connection: keep-alive'); |
| 87 |
header('X-Accel-Buffering: no'); // Disable nginx buffering |
| 88 |
header('Access-Control-Allow-Origin: *'); |
| 89 |
header('Access-Control-Allow-Headers: Cache-Control'); |
| 90 |
|
| 91 |
// Disable WordPress output buffering |
| 92 |
remove_action('shutdown', 'wp_ob_end_flush_all', 1); |
| 93 |
|
| 94 |
// Send initial connection event |
| 95 |
echo "event: connected\n"; |
| 96 |
echo "data: Connection established\n\n"; |
| 97 |
flush(); |
| 98 |
|
| 99 |
// Start streaming response |
| 100 |
$customAI->generateStreamResponse($prompt, $ticket, $productId, $conversationId ?: null); |
| 101 |
|
| 102 |
// Send end event |
| 103 |
echo "event: end\n"; |
| 104 |
echo "data: Stream completed\n\n"; |
| 105 |
flush(); |
| 106 |
|
| 107 |
exit; |
| 108 |
} |
| 109 |
} catch (\Exception $e) { |
| 110 |
// Send error as SSE event |
| 111 |
echo "event: error\n"; |
| 112 |
echo "data: " . json_encode(['message' => esc_html(Helper::getSafeErrorMessage($e))]) . "\n\n"; |
| 113 |
flush(); |
| 114 |
exit; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function getTicketSummary(Request $request) |
| 119 |
{ |
| 120 |
try { |
| 121 |
$ticketId = $request->getSafe('id', 'intval'); |
| 122 |
$ticket = Ticket::with('responses')->findOrFail($ticketId); |
| 123 |
$this->ensureCanAccessTicket($ticket); |
| 124 |
|
| 125 |
return (new FluentBotService())->getTicketSummary($ticket); |
| 126 |
} catch (\Exception $e) { |
| 127 |
return $this->sendError([ |
| 128 |
'message' => Helper::getSafeErrorMessage($e) |
| 129 |
]); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public function getTicketTone(Request $request) |
| 134 |
{ |
| 135 |
try { |
| 136 |
$ticketId = $request->getSafe('id', 'intval'); |
| 137 |
$ticket = Ticket::with('responses')->findOrFail($ticketId); |
| 138 |
$this->ensureCanAccessTicket($ticket); |
| 139 |
|
| 140 |
return (new FluentBotService())->getTicketTone($ticket); |
| 141 |
} catch (\Exception $e) { |
| 142 |
return $this->sendError([ |
| 143 |
'message' => Helper::getSafeErrorMessage($e) |
| 144 |
]); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|