ai_client_factory = $ai_client_factory; $this->style_descriptions = $style_descriptions; $this->title_generator_service = $title_generator_service ?? new TitleGeneratorService(); $this->governing_blueprint_resolver = $governing_blueprint_resolver ?? new GoverningBlueprintResolver(); } /** * Generate product description * * @param array $params Generation parameters (product_id, is_short, improveExistingDescription, keepExistingDescription) * @return array|\WP_Error Generated content or error */ public function generateDescription( array $params ) { try { $product_id = $params['product_id']; $is_short = isset( $params['is_short'] ) ? $params['is_short'] : false; $execution_path = BlueprintIntegration::executionPath( BlueprintGenerators::DESCRIPTION, array() ); if ( BlueprintIntegration::PATH_BLUEPRINT === $execution_path['path'] ) { return $this->generateDescriptionBlueprint( (int) $product_id, (bool) $is_short, $params ); } $api_provider = get_option( 'aipt_api_provider', 'openai' ); $model = $this->ai_client_factory->getDefaultModel( $api_provider ); $model = get_option( 'aipt_model', $model ); $model = $this->ai_client_factory->normalizeModel( $api_provider, $model ); $product = wc_get_product( $product_id ); if ( ! $product ) { return new \WP_Error( 'invalid_product', 'Invalid product ID', array( 'status' => 404 ) ); } $subject_error = $this->invalidGenerationSubjectError( $product ); if ( null !== $subject_error ) { return $subject_error; } $permission_error = $this->checkProductPermission( (int) $product_id ); if ( null !== $permission_error ) { return $permission_error; } $language = get_option( 'aipt_language', Activator::get_default_value( 'language' ) ); $writing_style = get_option( 'aipt_writing_style', Activator::get_default_value( 'writing_style' ) ); $max_length = (int) ( $is_short ? get_option( 'aipt_max_short_length', Activator::get_default_value( 'max_short_length' ) ) : get_option( 'aipt_max_length', Activator::get_default_value( 'max_length' ) ) ); $prompt_type = $is_short ? CustomPrompts::SHORT : CustomPrompts::FULL; $custom_prompt = CustomPrompts::stored( $prompt_type ); if ( '' === trim( $custom_prompt ) ) { $custom_prompt = CustomPrompts::defaultFor( $prompt_type ); } $style_description = isset( $this->style_descriptions[ $writing_style ] ) ? $this->style_descriptions[ $writing_style ] : $this->style_descriptions['Professional']; $product_info = $this->getProductInfo( $product ); $attributes_string = ! empty( $product_info['attributes'] ) ? implode( ', ', array_map( function ( $k, $v ) { return "$k: $v"; }, array_keys( $product_info['attributes'] ), $product_info['attributes'] ) ) : ''; $categories_string = implode( ', ', $product_info['categories'] ); $tags_string = implode( ', ', $product_info['tags'] ); $safe_variables = VariableConverter::convertVariables( array( '{max_length}' => intval( $max_length ), '{SKU}' => $product_info['sku'], '{ATTRIBUTES}' => $attributes_string, '{CATEGORIES}' => $categories_string, '{TAGS}' => $tags_string, '{PRICE}' => $product_info['price'], '{REGULAR_PRICE}' => $product_info['regular_price'], '{SALE_PRICE}' => $product_info['sale_price'], '{SHORT_DESCRIPTION}' => $product_info['short_description'], '{LONG_DESCRIPTION}' => $product_info['description'], '{language}' => sanitize_text_field( $language ), '{style}' => sanitize_text_field( $style_description ), '{title}' => $product_info['name'], ) ); $custom_prompt = str_replace( array_keys( $safe_variables ), array_values( $safe_variables ), $custom_prompt ); $provider_data = ProductDataProvider::get( (int) $product_id, false ); $custom_variables = is_array( $provider_data['custom_variables'] ?? null ) ? $provider_data['custom_variables'] : array(); $custom_prompt = PromptFormatter::replaceCustomVariables( $custom_prompt, $custom_variables ); $client = $this->ai_client_factory->create(); $custom_prompt = $this->cleanPromptContent( $custom_prompt, $product_info ); $brand_context_block = \AIPT\Api\BulkGenerator\Common\BrandContext::block(); $system_prompt = '' !== $brand_context_block ? $brand_context_block : 'You are a professional product copywriter for an online store.'; $system_prompt .= " Ensure the output is formatted in HTML (using

,