| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureForms - AI Form Builder. |
| 4 |
* |
| 5 |
* @package sureforms |
| 6 |
* @since 0.0.8 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\AI_Form_Builder; |
| 10 |
|
| 11 |
use SRFM\Inc\Traits\Get_Instance; |
| 12 |
use SRFM\Inc\Helper; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* SureForms AI Form Builder Class. |
| 21 |
*/ |
| 22 |
class AI_Form_Builder { |
| 23 |
use Get_Instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Fetches ai data from the middleware server |
| 27 |
* |
| 28 |
* @param \WP_REST_Request $request request object. |
| 29 |
* @since 0.0.8 |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function generate_ai_form( $request ) { |
| 33 |
|
| 34 |
// Get the params. |
| 35 |
$params = $request->get_params(); |
| 36 |
|
| 37 |
// If the message array doesn't exist, abandon ship. |
| 38 |
if ( empty( $params['message_array'] ) || ! is_array( $params['message_array'] ) ) { |
| 39 |
wp_send_json_error( [ 'message' => __( 'The message array was not supplied', 'sureforms' ) ] ); |
| 40 |
} |
| 41 |
|
| 42 |
// Set the token count to 0, and create messages array. |
| 43 |
$messages = []; |
| 44 |
|
| 45 |
// Start with the last message - going upwards until the token count hits 2000. |
| 46 |
foreach ( array_reverse( $params['message_array'] ) as $current_message ) { |
| 47 |
// If the message content doesn't exist, skip it. |
| 48 |
if ( empty( $current_message['content'] ) ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
// Add the message to the start of the messages to send to the SCS Middleware. |
| 53 |
array_unshift( $messages, $current_message ); |
| 54 |
} |
| 55 |
|
| 56 |
// Get the response from the endpoint. |
| 57 |
$response = AI_Helper::get_chat_completions_response( |
| 58 |
array_merge( |
| 59 |
[ |
| 60 |
'query' => $messages[0]['content'], |
| 61 |
], |
| 62 |
defined( 'SRFM_AI_SYSTEM_PROMPT_ARGS' ) ? Helper::get_array_value( SRFM_AI_SYSTEM_PROMPT_ARGS ) : [] |
| 63 |
) |
| 64 |
); |
| 65 |
|
| 66 |
// check if response is an array if not then send error. |
| 67 |
if ( ! is_array( $response ) ) { |
| 68 |
wp_send_json_error( [ 'message' => __( 'The SureForms AI Middleware encountered an error.', 'sureforms' ) ] ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! empty( $response['error'] ) ) { |
| 72 |
// If the response has an error, handle it and report it back. |
| 73 |
$message = ''; |
| 74 |
if ( ! empty( $response['error']['message'] ) ) { // If any error message received from OpenAI. |
| 75 |
$message = $response['error']['message']; |
| 76 |
} elseif ( is_string( $response['error'] ) ) { // If any error message received from server. |
| 77 |
if ( ! empty( $response['code'] && is_string( $response['code'] ) ) ) { |
| 78 |
$message = __( 'The SureForms AI Middleware encountered an error.', 'sureforms' ); |
| 79 |
} |
| 80 |
$message = ! empty( $message ) ? $message : $response['error']; |
| 81 |
} |
| 82 |
wp_send_json_error( [ 'message' => $message ] ); |
| 83 |
} elseif ( is_array( $response['form'] ) && ! empty( $response['form']['formTitle'] ) && is_array( $response['form']['formFields'] ) && ! empty( $response['form']['formFields'] ) ) { |
| 84 |
// If the message was sent successfully, send it successfully. |
| 85 |
wp_send_json_success( $response ); |
| 86 |
} else { |
| 87 |
// If you've reached here, then something has definitely gone amuck. Abandon ship. |
| 88 |
wp_send_json_error( $response ); |
| 89 |
}//end if |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|