PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Ai / GenerateChoicesController.php

GenerateChoicesController.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Controllers/Ai/GenerateChoicesController.php

103 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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