ai_suggestions_unifier = $ai_suggestions_unifier;
$this->suggestion_processor = $suggestion_processor;
}
/**
* Builds a response for the AI Optimize route by comparing the response to the input.
* We output the diff as an HTML string and will parse this string on the JavaScript side.
* The differences are marked with `` and `` tags.
*
* @param string $assessment The assessment to improve.
* @param string $original The original text.
* @param object $response The response from the API.
*
* @return string The HTML containing the suggested content.
*
* @throws Bad_Request_Exception Bad_Request_Exception.
*/
public function build_optimize_response( string $assessment, string $original, object $response ): string {
$raw_fixes = $this->suggestion_processor->get_suggestion_from_ai_response( $response->body );
if ( $raw_fixes === '' ) {
return '';
}
$diff = $this->suggestion_processor->calculate_diff( $original, $raw_fixes );
// For the paragraph length assessment, we need to replace any introduced paragraph breaks with a special class.
if ( $assessment === 'read-paragraph-length' ) {
$diff = $this->suggestion_processor->mark_new_paragraphs_in_suggestions( $diff );
}
$diff = $this->suggestion_processor->remove_html_from_suggestion( $diff );
$diff = $this->suggestion_processor->keep_nbsp_in_suggestions( $diff );
// If we end up with no suggestions, we have to show an error to the user.
if ( \preg_match( '/<(ins|del) class="yst-/', $diff ) === false ) {
throw new Bad_Request_Exception();
}
$suggestion = new Suggestion();
$suggestion->set_content( $diff );
return $this->ai_suggestions_unifier->unify_diffs( $suggestion );
}
}