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