Skip to content

Commit

Permalink
Merge pull request #15 from newfold-labs/features/otherpages
Browse files Browse the repository at this point in the history
add other pages generation
  • Loading branch information
amartya-dev authored Feb 1, 2024
2 parents 17e4883 + a04a1ed commit 0265f65
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 24 deletions.
57 changes: 57 additions & 0 deletions includes/RestApi/AISearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ public function register_routes() {
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/otherpages',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'other_pages' ),
'args' => array(
'site_description' => array(
'required' => true,
'type' => 'string',
),
'content_style' => array(
'required' => true,
'type' => 'object',
),
'target_audience' => array(
'required' => true,
'type' => 'object',
),
'sitemap' => array(
'required' => true,
'type' => 'object',
),
),
'permission_callback' => array( $this, 'check_permission' ),
),
)
);
}

/**
Expand Down Expand Up @@ -152,6 +182,33 @@ public function homepages( \WP_REST_Request $request ) {
return new \WP_REST_Response( $response, 200 );
}

/**
* Proxy to the AI service to get the responses.
*
* @param \WP_REST_Request $request Request object
*
* @returns \WP_REST_Response|\WP_Error
*/
public function other_pages( \WP_REST_Request $request ) {
$site_description = $request['site_description'];
$content_style = $request['content_style'];
$target_audience = $request['target_audience'];
$sitemap = $request['sitemap'];

$response = SiteGen::get_pages(
$site_description,
$content_style,
$target_audience,
$sitemap
);

if ( array_key_exists( 'error', $response ) ) {
return new \WP_Error( 'bad_request', $response['error'], 400 );
}

return new \WP_REST_Response( $response, 200 );
}

/**
* Proxy to the AI service to get the responses.
*
Expand Down
202 changes: 178 additions & 24 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@ private static function validate_site_info( $site_info, $identifier ) {
* @param string $identifier The identifier to be used for generating the required meta
*/
private static function get_sitegen_from_cache( $identifier ) {
$site_gen = get_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), null );
if ( ! $site_gen ) {
update_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), array() );
}
if ( $site_gen && array_key_exists( $identifier, $site_gen ) ) {
return $site_gen[ $identifier ];
}
return null;
return get_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), null );
}

/**
Expand All @@ -98,9 +91,7 @@ private static function get_sitegen_from_cache( $identifier ) {
* @param array $response The response from the sitegen API.
*/
private static function cache_sitegen_response( $identifier, $response ) {
$site_gen = get_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), array() );
$site_gen[ $identifier ] = $response;
update_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), $site_gen );
update_option( NFD_SITEGEN_OPTION . '-' . strtolower( $identifier ), $response );
}

/**
Expand Down Expand Up @@ -314,9 +305,22 @@ public static function generate_site_meta( $site_info, $identifier, $skip_cache
'error' => $error['payload']['reason'],
);
}
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
try {
$error = json_decode( wp_remote_retrieve_body( $response ), true );
if ( array_key_exists( 'payload', $error ) ) {
return array(
'error' => $error['payload'],
);
} else {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
} catch ( \Exception $exception ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
}

$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
Expand Down Expand Up @@ -359,6 +363,9 @@ public static function get_home_pages( $site_description, $content_style, $targe
$generated_content_structures = self::get_sitegen_from_cache(
'contentStructures'
);
$generated_images = self::get_sitegen_from_cache(
'generatedImages'
);
$keywords = self::generate_site_meta(
array(
'site_description' => $site_description,
Expand Down Expand Up @@ -396,14 +403,31 @@ public static function get_home_pages( $site_description, $content_style, $targe
'error' => $error['payload']['reason'],
);
}
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
try {
$error = json_decode( wp_remote_retrieve_body( $response ), true );
if ( array_key_exists( 'payload', $error ) ) {
return array(
'error' => $error['payload'],
);
} else {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
} catch ( \Exception $exception ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
}
$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
$generated_content_structures = $parsed_response['contentStructures'];
$generated_patterns = $parsed_response['generatedPatterns'];
$generated_homepages = $parsed_response['pages'];
if ( array_key_exists( "generatedImages", $parsed_response ) ) {
$generated_images = $parsed_response['generatedImages'];
self::cache_sitegen_response( 'generatedImages', $generated_images );
}
self::cache_sitegen_response( 'contentStructures', $generated_content_structures );
self::cache_sitegen_response( 'generatedPatterns', $generated_patterns );
self::cache_sitegen_response( 'homepages', $generated_homepages );
Expand All @@ -413,21 +437,151 @@ public static function get_home_pages( $site_description, $content_style, $targe
$generated_homepages = array();
$generated_patterns = self::get_sitegen_from_cache( 'generatedPatterns' );

$categories_to_separate = array('header', 'footer');
// Choose random categories for the generated patterns and return
foreach ( $random_homepages as $slug ) {
foreach ( $random_homepages as $homepage_index => $slug ) {
$generated_homepages[ $slug ] = array();
$homepage_patterns = array();
foreach ( $generated_content_structures[ $slug ] as $pattern_category ) {
if ( ! $generated_patterns[ $pattern_category ] ) {
if ( empty( $generated_patterns[ $pattern_category ] ) ) {
continue;
}
// Get a random pattern for the category.
$random_pattern = array_rand( $generated_patterns[ $pattern_category ] );
$random_pattern = $generated_patterns[ $pattern_category ][ $random_pattern ];
array_push( $generated_homepages[ $slug ], $random_pattern );
// Get a random pattern for the category when regenerating otherwise pick in sequence
// so that the 3 previews are as different as much as possible.
$pattern_index = ( $regenerate ) ? array_rand( $generated_patterns[ $pattern_category ] ) : $homepage_index;
$random_pattern = $generated_patterns[ $pattern_category ][ $pattern_index ];

if( in_array( $pattern_category, $categories_to_separate ) ) {
$homepage_patterns[ $pattern_category ] = $random_pattern;
} else {
$homepage_patterns[ 'content' ] = $random_pattern;
}

}
$generated_homepages[ $slug ] = $homepage_patterns;
}


$generated_homepages['generatedImages'] = $generated_images;
self::cache_sitegen_response( 'homepages', $generated_homepages );
return $generated_homepages;
}

/**
* Function to get the content for a page
*
* @param string $site_description The site description (user prompt).
* @param array $content_style Generated from sitegen.
* @param array $target_audience Generated target audience.
* @param array $keywords Generated keywords for page.
* @param string $page The page
*/
public static function get_content_for_page(
$site_description,
$content_style,
$target_audience,
$keywords,
$page
) {
$response = wp_remote_post(
NFD_AI_BASE . 'generatePageContent',
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => HiiveConnection::get_auth_token(),
'prompt' => array(
'site_description' => $site_description,
'content_style' => wp_json_encode( $content_style ),
'target_audience' => wp_json_encode( $target_audience ),
),
'page' => $page,
'keywords' => wp_json_encode( $keywords ),
)
),
)
);
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
if ( 400 === $response_code ) {
$error = json_decode( wp_remote_retrieve_body( $response ), true );
return array(
'error' => $error['payload']['reason'],
);
}
try {
$error = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $error ) && array_key_exists( 'payload', $error ) ) {
return array(
'error' => $error['payload'],
);
} else {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
} catch ( \Exception $exception ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
}
}
$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
if( ! array_key_exists( 'error', $parsed_response['content'] ) ) {
$parsed_response['content'] = implode( '', $parsed_response['content'] );
}
return $parsed_response['content'];
}

/**
* Function to get the page patterns
*
* @param string $site_description The site description (user prompt).
* @param array $content_style Generated from sitegen.
* @param array $target_audience Generated target audience.
* @param array $site_map The site map
* @param boolean $skip_cache To skip or not to skip
*/
public static function get_pages(
$site_description,
$content_style,
$target_audience,
$site_map,
$skip_cache = false
) {
if ( ! self::check_capabilities() ) {
return array(
'error' => __( 'You do not have the permissions to perform this action' ),
);
}

$identifier = 'generatePages';

if ( ! $skip_cache ) {
$site_gen_cached = self::get_sitegen_from_cache( $identifier );
if ( $site_gen_cached ) {
return $site_gen_cached;
}
}

$pages_content = array();

foreach ( $site_map as $site_menu => $site_menu_options ) {
$page = $site_menu_options['slug'];
$keywords = $site_menu_options['keywords'];

$response = self::get_content_for_page(
$site_description,
$content_style,
$target_audience,
$keywords,
$page
);

$pages_content[ $page ] = $response;
}
return $pages_content;
}
}

0 comments on commit 0265f65

Please sign in to comment.