diff --git a/src/StoreApi/Routes/V1/AI/BusinessDescription.php b/src/StoreApi/Routes/V1/AI/BusinessDescription.php index f377eff9a6f..1a30a9f503d 100644 --- a/src/StoreApi/Routes/V1/AI/BusinessDescription.php +++ b/src/StoreApi/Routes/V1/AI/BusinessDescription.php @@ -2,7 +2,6 @@ namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI; -use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater; use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute; /** diff --git a/src/StoreApi/Routes/V1/AI/StoreTitle.php b/src/StoreApi/Routes/V1/AI/StoreTitle.php new file mode 100644 index 00000000000..353a29a0b45 --- /dev/null +++ b/src/StoreApi/Routes/V1/AI/StoreTitle.php @@ -0,0 +1,146 @@ + \WP_REST_Server::CREATABLE, + 'callback' => [ $this, 'get_response' ], + 'permission_callback' => [ Middleware::class, 'is_authorized' ], + 'args' => [ + 'business_description' => [ + 'description' => __( 'The business description for a given store.', 'woo-gutenberg-products-block' ), + 'type' => 'string', + ], + ], + ], + 'schema' => [ $this->schema, 'get_public_item_schema' ], + 'allow_batch' => [ 'v1' => true ], + ]; + } + + /** + * Update the store title powered by AI. + * + * @param \WP_REST_Request $request Request object. + * + * @return bool|string|\WP_Error|\WP_REST_Response + */ + protected function get_route_post_response( \WP_REST_Request $request ) { + + $business_description = $request->get_param( 'business_description' ); + + if ( ! $business_description ) { + return $this->error_to_response( + new \WP_Error( + 'invalid_business_description', + __( 'Invalid business description.', 'woo-gutenberg-products-block' ) + ) + ); + } + + $store_title = get_option( 'blogname' ); + if ( ! ( empty( $store_title ) || self::DEFAULT_TITLE === $store_title ) ) { + return rest_ensure_response( array( 'ai_content_generated' => false ) ); + } + + $ai_generated_title = $this->generate_ai_title( $business_description ); + if ( is_wp_error( $ai_generated_title ) ) { + return $this->error_to_response( $ai_generated_title ); + } + + update_option( self::STORE_TITLE_OPTION_NAME, $ai_generated_title ); + + return rest_ensure_response( + array( + 'ai_content_generated' => true, + ) + ); + } + + /** + * Generate the store title powered by AI. + * + * @param string $business_description The business description for a given store. + * + * @return string|\WP_Error|\WP_REST_Response The store title generated by AI. + */ + private function generate_ai_title( $business_description ) { + $ai_connection = new Connection(); + + $site_id = $ai_connection->get_site_id(); + if ( is_wp_error( $site_id ) ) { + return $this->error_to_response( $site_id ); + } + + $token = $ai_connection->get_jwt_token( $site_id ); + if ( is_wp_error( $token ) ) { + return $this->error_to_response( $token ); + } + + $prompt = "Generate a store title for a store that has the following: '$business_description'. The length of the title should be 1 and 3 words. The result should include only the store title without any other explanation, number or punctuation marks"; + + $ai_response = $ai_connection->fetch_ai_response( $token, $prompt ); + if ( is_wp_error( $ai_response ) ) { + return $this->error_to_response( $ai_response ); + } + + if ( ! isset( $ai_response['completion'] ) ) { + return ''; + } + + return $ai_response['completion']; + } +} diff --git a/src/StoreApi/RoutesController.php b/src/StoreApi/RoutesController.php index aace314ca39..de734454396 100644 --- a/src/StoreApi/RoutesController.php +++ b/src/StoreApi/RoutesController.php @@ -61,10 +61,11 @@ public function __construct( SchemaController $schema_controller ) { ], // @todo Migrate internal AI routes to WooCommerce Core codebase. 'private' => [ - Routes\V1\AI\Images::IDENTIFIER => Routes\V1\AI\Images::class, - Routes\V1\AI\Patterns::IDENTIFIER => Routes\V1\AI\Patterns::class, - Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, - Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, + Routes\V1\AI\StoreTitle::IDENTIFIER => Routes\V1\AI\StoreTitle::class, + Routes\V1\AI\Images::IDENTIFIER => Routes\V1\AI\Images::class, + Routes\V1\AI\Patterns::IDENTIFIER => Routes\V1\AI\Patterns::class, + Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, + Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, Routes\V1\AI\BusinessDescription::IDENTIFIER => Routes\V1\AI\BusinessDescription::class, ], ]; diff --git a/src/StoreApi/SchemaController.php b/src/StoreApi/SchemaController.php index bc1a284ca7e..f836f7ce4e6 100644 --- a/src/StoreApi/SchemaController.php +++ b/src/StoreApi/SchemaController.php @@ -54,6 +54,7 @@ public function __construct( ExtendSchema $extend ) { Schemas\V1\ProductCategorySchema::IDENTIFIER => Schemas\V1\ProductCategorySchema::class, Schemas\V1\ProductCollectionDataSchema::IDENTIFIER => Schemas\V1\ProductCollectionDataSchema::class, Schemas\V1\ProductReviewSchema::IDENTIFIER => Schemas\V1\ProductReviewSchema::class, + Schemas\V1\AI\StoreTitleSchema::IDENTIFIER => Schemas\V1\AI\StoreTitleSchema::class, Schemas\V1\AI\ImagesSchema::IDENTIFIER => Schemas\V1\AI\ImagesSchema::class, Schemas\V1\AI\PatternsSchema::IDENTIFIER => Schemas\V1\AI\PatternsSchema::class, Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, diff --git a/src/StoreApi/Schemas/V1/AI/StoreTitleSchema.php b/src/StoreApi/Schemas/V1/AI/StoreTitleSchema.php new file mode 100644 index 00000000000..4310ca5044d --- /dev/null +++ b/src/StoreApi/Schemas/V1/AI/StoreTitleSchema.php @@ -0,0 +1,47 @@ + true, + ]; + } +}