/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/AI/API
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/AI/API/Chat.php (4084B)
$mode,
'userPrompt' => $user_prompt,
'scopes' => $context['scopes'] ?? [],
'availableScopes' => $context['availableScopes'] ?? [],
'context' => $context,
'history' => $history,
'sessionId' => $session_id,
];
if ( $reclassify_hint !== '' ) {
$body['reclassifyHint'] = $reclassify_hint;
}
$response = $this->request->post( self::ENDPOINT, $body, $this->build_request_headers( $batch_id ) );
if ( $response->has_errors() ) {
return $this->build_error_response( $response );
}
return wp_parse_args( $response->get_body(), $this->response_defaults() );
}
/**
* Build the request headers for a chat request.
*
* Batch ID propagates the classify round's `responseId` to the answer round
* via the `x-wpforms-batch-id` request header — middleware uses it to claim
* the rate-limit exemption granted by the prior classify.
*
* @since 2.0.0
*
* @param string $batch_id Batch ID from the prior classify round; empty to omit the header.
*
* @return array Request headers.
*/
private function build_request_headers( string $batch_id ): array {
$headers = [];
if ( $batch_id !== '' ) {
$headers['x-wpforms-batch-id'] = $batch_id;
}
return $headers;
}
/**
* Build the error response envelope from a failed request.
*
* @since 2.0.0
*
* @param Response $response Failed API response.
*
* @return array
*/
private function build_error_response( Response $response ): array {
$error_data = $response->get_error_data();
return array_merge(
$this->response_defaults(),
[
'error' => (string) ( $error_data['error'] ?? '' ),
'error_code' => (int) ( $error_data['code'] ?? 0 ),
]
);
}
/**
* The canonical chat response shape, with every field defaulting to null.
*
* Shared by the success path (as `wp_parse_args` defaults) and the error path
* so both return the same key set.
*
* @since 2.0.0
*
* @return array
*/
private function response_defaults(): array {
return [
'message' => null,
'tool_calls' => null,
'error' => null,
'sessionId' => null,
'responseId' => null,
'selectedScopes' => null,
];
}
}