Skip to content

Commit

Permalink
add other pages generation
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya-dev committed Jan 30, 2024
1 parent 17e4883 commit 5a06dc1
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 17 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
167 changes: 150 additions & 17 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 @@ -396,9 +400,22 @@ 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'];
Expand Down Expand Up @@ -430,4 +447,120 @@ public static function get_home_pages( $site_description, $content_style, $targe
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 ( 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 );
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 5a06dc1

Please sign in to comment.