Skip to content

Commit

Permalink
Merge pull request #9 from newfold-labs/sitegen
Browse files Browse the repository at this point in the history
fix sitegen homepages api
  • Loading branch information
amartya-dev authored Dec 12, 2023
2 parents 623eaab + fc9fb50 commit 04d0c25
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions includes/RestApi/AISearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function homepages( \WP_REST_Request $request ) {

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

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

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

Expand Down
Empty file added includes/SiteGen/Parser.php
Empty file.
17 changes: 11 additions & 6 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ private static function get_patterns_for_category( $category ) {
* @param string $site_description The site description (the user prompt)
* @param array $content_style The generated content style.
* @param array $target_audience The generated target audience.
* @param array $content_structure The content structures generated / cached
* @param array $content_structures The content structures generated / cached
* @param boolean $skip_cache If we need to skip cache.
*/
private static function generate_pattern_content(
$site_description,
$content_style,
$target_audience,
$content_structure,
$content_structures,
$skip_cache = false
) {
if ( ! $skip_cache ) {
Expand All @@ -179,7 +179,7 @@ private static function generate_pattern_content(
);

$unique_categories = array();
foreach ( $content_structure as $homepage => $structure ) {
foreach ( $content_structures as $homepage => $structure ) {
foreach ( $structure as $category ) {
if ( ! in_array( $category, $unique_categories, true ) ) {
array_push( $unique_categories, $category );
Expand All @@ -200,8 +200,7 @@ private static function generate_pattern_content(

$category_pattern_map[ $category ] = array();
foreach ( $random_selected_patterns as $pattern_slug ) {
$pattern = $patterns_for_category[ $pattern_slug ];
// Generate content for these patterns
$pattern = $patterns_for_category[ $pattern_slug ];
$response = wp_remote_post(
NFD_AI_BASE . 'generateSiteMeta',
array(
Expand Down Expand Up @@ -325,6 +324,12 @@ public static function get_home_pages( $site_description, $content_style, $targe
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;
}

// Check if we have the response in cache already
if ( ! $regenerate ) {
$generated_homepages = self::get_sitegen_from_cache( 'homepages' );
Expand All @@ -350,7 +355,7 @@ public static function get_home_pages( $site_description, $content_style, $targe
}
// Get a random pattern for the category.
$random_pattern = array_rand( $generated_patterns[ $pattern_category ] );
$random_pattern = $generated_patterns[ $random_pattern ];
$random_pattern = $generated_patterns[ $pattern_category ][ $random_pattern ];
array_push( $generated_homepages[ $slug ], $random_pattern );
}
}
Expand Down

0 comments on commit 04d0c25

Please sign in to comment.