Skip to content

Commit

Permalink
move to new sitegen with most stuff on worker
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya-dev committed Dec 18, 2023
1 parent 54fdda8 commit 86d5382
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,7 @@ public static function generate_site_meta( $site_info, $identifier, $skip_cache
* @param array $target_audience Generated target audience.
* @param boolean $regenerate If we need to regenerate.
*/
public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = true ) {
$generated_content_structures = self::generate_site_meta(
array( 'site_description' => $site_description ),
'contentstructure'
);

// If we got an error, return that right away
if ( array_key_exists( 'error', $generated_content_structures ) ) {
return $generated_content_structures;
}
public static function get_home_pages( $site_description, $content_style, $target_audience, $regenerate = false ) {

// Check if we have the response in cache already
if ( ! $regenerate ) {
Expand All @@ -359,14 +350,64 @@ public static function get_home_pages( $site_description, $content_style, $targe
return $generated_homepages;
}
}

$generated_content_structures = self::get_sitegen_from_cache(
'contentstructure'
);
$keywords = self::generate_site_meta(
array(
'site_description' => $site_description,
'content_style' => $content_style,
),
'keywords'
);
if ( ! $generated_content_structures ) {
$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,
'keywords' => wp_json_encode( $keywords ),
'content_style' => wp_json_encode( $content_style ),
'target_audience' => wp_json_encode( $target_audience ),
),
'page' => 'home',
)
),
)
);
$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'],
);
}
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'];
self::cache_sitegen_response( 'contentStructures', $generated_content_structures );
self::cache_sitegen_response( 'generatedPatterns', $generated_patterns );
self::cache_sitegen_response( 'homepages', $generated_homepages );
return $generated_homepages;
}

$random_homepages = array_rand( $generated_content_structures, 3 );
$generated_homepages = array();
$generated_patterns = self::generate_pattern_content(
$site_description,
$content_style,
$target_audience,
$generated_content_structures
);
$generated_patterns = self::get_sitegen_from_cache( 'generatedPatterns' );

// Choose random categories for the generated patterns and return
foreach ( $random_homepages as $slug ) {
Expand Down

0 comments on commit 86d5382

Please sign in to comment.