| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Controllers\Ai; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 |
use IvyForms\Common\Exceptions\ServiceUnavailableException; |
| 12 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 13 |
use IvyForms\Controllers\Controller; |
| 14 |
use IvyForms\Services\Ai\AiChoiceGenerationService; |
| 15 |
use IvyForms\Services\Translations\BackendStrings; |
| 16 |
use WP_REST_Request; |
| 17 |
use WP_REST_Response; |
| 18 |
|
| 19 |
/** |
| 20 |
* Handles AI choice generation from a prompt and prior dialog exchanges. |
| 21 |
*/ |
| 22 |
class GenerateChoicesController extends Controller |
| 23 |
{ |
| 24 |
private AiChoiceGenerationService $aiChoiceGenerationService; |
| 25 |
|
| 26 |
public function __construct(AiChoiceGenerationService $aiChoiceGenerationService) |
| 27 |
{ |
| 28 |
$this->aiChoiceGenerationService = $aiChoiceGenerationService; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param WP_REST_Request<array<string, mixed>> $data |
| 33 |
* |
| 34 |
* @return WP_REST_Response |
| 35 |
* |
| 36 |
* @throws InvalidArgumentException |
| 37 |
* @throws ServiceUnavailableException |
| 38 |
*/ |
| 39 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 40 |
{ |
| 41 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 42 |
|
| 43 |
$prompt = sanitize_textarea_field((string) ($data->get_param('prompt') ?? '')); |
| 44 |
if ($prompt === '') { |
| 45 |
throw new InvalidArgumentException( |
| 46 |
BackendStrings::getAiStrings()['ai_choices_prompt_required'] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
$fieldType = sanitize_key((string) ($data->get_param('fieldType') ?? '')); |
| 51 |
$conversationHistory = $this->sanitizeConversationHistory($data->get_param('history')); |
| 52 |
$choices = $this->aiChoiceGenerationService->generateChoices( |
| 53 |
$prompt, |
| 54 |
$fieldType, |
| 55 |
$conversationHistory |
| 56 |
); |
| 57 |
|
| 58 |
return new WP_REST_Response($choices, 200); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param mixed $history |
| 63 |
* |
| 64 |
* @return array<int, array<string, mixed>> |
| 65 |
*/ |
| 66 |
private function sanitizeConversationHistory($history): array |
| 67 |
{ |
| 68 |
if (!is_array($history)) { |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
$sanitizedHistory = []; |
| 73 |
foreach ($history as $turn) { |
| 74 |
if (!is_array($turn)) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
$sanitizedChoices = []; |
| 79 |
$choices = $turn['choices'] ?? []; |
| 80 |
if (is_array($choices)) { |
| 81 |
foreach ($choices as $choice) { |
| 82 |
if (!is_array($choice)) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
$sanitizedChoices[] = [ |
| 87 |
'label' => sanitize_text_field((string) ($choice['label'] ?? '')), |
| 88 |
'value' => sanitize_text_field((string) ($choice['value'] ?? '')), |
| 89 |
]; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$sanitizedHistory[] = [ |
| 94 |
'prompt' => sanitize_textarea_field((string) ($turn['prompt'] ?? '')), |
| 95 |
'title' => sanitize_text_field((string) ($turn['title'] ?? '')), |
| 96 |
'choices' => $sanitizedChoices, |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
return $sanitizedHistory; |
| 101 |
} |
| 102 |
} |
| 103 |
|