\d+)/voice-test', array( array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'voice_test' ), 'permission_callback' => array( __CLASS__, 'check_permissions_read' ), 'args' => array( 'id' => array( 'required' => true, 'sanitize_callback' => 'absint' ) ), ), ) ); register_rest_route( 'aipt/v1', '/content-structure/brand-profiles/(?P\d+)/suggestions', array( array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'suggestions' ), 'permission_callback' => array( __CLASS__, 'check_permissions_manage' ), 'args' => array( 'id' => array( 'required' => true, 'sanitize_callback' => 'absint' ) ), ), ) ); } /** * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public static function voice_test( WP_REST_Request $request ) { $profile = ( new BrandProfileStore() )->get( (int) $request->get_param( 'id' ) ); if ( null === $profile ) { return self::error( 'aipt_blueprint_brand_profile_not_found', __( 'This voice no longer exists.', 'ai-product-tools' ), 404 ); } $gate = AiSupportGate::guard(); if ( is_wp_error( $gate ) ) { return $gate; } $user_id = get_current_user_id(); $body = (array) $request->get_json_params(); $client_request_id = isset( $body['clientRequestId'] ) ? sanitize_text_field( (string) $body['clientRequestId'] ) : ''; $claim = self::dedupeClaim( $user_id, $client_request_id ); if ( is_wp_error( $claim ) ) { return $claim; } if ( is_array( $claim ) ) { return self::success( $claim ); } $rate_check = self::checkRateLimitStatic( $user_id, self::RATE_LIMIT_SCOPE ); if ( ! $rate_check['allowed'] ) { self::dedupeRelease( $user_id, $client_request_id ); return self::rateLimitErrorStatic( $rate_check ); } $source_text = self::resolveVoiceTestSource( $body ); if ( is_wp_error( $source_text ) ) { self::dedupeRelease( $user_id, $client_request_id ); return $source_text; } $profile_data = ( new EntitlementPolicy() )->clampBrandProfile( is_array( $profile['profile'] ) ? $profile['profile'] : array() )['profile']; $voice = ( new BrandVoiceCompiler() )->compile( $profile_data, array( 'expression' => null, 'note' => '' ) ); $compiled = self::compileVoiceTestRequest( $source_text, $voice ); $caller = self::caller(); $result = $caller->call( $compiled ); if ( ! $result['ok'] ) { self::dedupeRelease( $user_id, $client_request_id ); return self::error( (string) ( $result['error']['code'] ?? 'aipt_blueprint_provider_error' ), (string) ( $result['error']['message'] ?? __( 'The AI provider did not return a usable answer.', 'ai-product-tools' ) ), 502 ); } $decoded = $result['decoded']; $data = array( 'without' => sanitize_textarea_field( (string) ( $decoded['without'] ?? '' ) ), 'with' => sanitize_textarea_field( (string) ( $decoded['with'] ?? '' ) ), 'notes' => array_slice( array_map( 'sanitize_text_field', array_map( 'strval', (array) ( $decoded['notes'] ?? array() ) ) ), 0, 5 ), ); self::dedupeStore( $user_id, $client_request_id, $data ); return self::success( $data ); } /** * @param array $body Request body. * @return string|WP_Error */ private static function resolveVoiceTestSource( array $body ) { $product_id = isset( $body['productId'] ) ? (int) $body['productId'] : 0; if ( $product_id > 0 ) { if ( ! current_user_can( 'edit_post', $product_id ) ) { return self::error( 'rest_forbidden', __( 'You do not have permission to use this product.', 'ai-product-tools' ), 403 ); } $product = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false; if ( ! ( $product instanceof \WC_Product ) ) { return self::error( 'aipt_blueprint_subject_not_found', __( 'This product could not be found.', 'ai-product-tools' ), 404 ); } $text = trim( HtmlToText::convert( $product->get_description() . ' ' . $product->get_short_description() ) ); if ( '' === $text ) { return self::error( 'aipt_blueprint_invalid_request', __( 'This product has no description to test with.', 'ai-product-tools' ), 400 ); } return mb_substr( $text, 0, self::VOICE_TEST_TEXT_MAX_LEN, 'UTF-8' ); } $text = isset( $body['text'] ) ? trim( sanitize_textarea_field( (string) $body['text'] ) ) : ''; if ( '' === $text ) { return self::error( 'aipt_blueprint_invalid_request', __( 'Enter text or choose a product to test with.', 'ai-product-tools' ), 400 ); } return mb_substr( $text, 0, self::VOICE_TEST_TEXT_MAX_LEN, 'UTF-8' ); } /** * @param string $source_text Plain-text source. * @param array $voice BrandVoiceCompiler::compile()'s result. * @return array StructuredCallerInterface request. */ private static function compileVoiceTestRequest( string $source_text, array $voice ): array { $lines = array(); $lines[] = 'You compare how the same product copy reads with and without a brand voice. Answer with ONE JSON object only: no prose before or after it, no Markdown code fence.'; $lines[] = 'Use facts only from the given text. Never invent a specification, certification, compliance claim, award, warranty term, or a claim about how the product is delivered, shipped, redeemed, installed or used, or its available amounts or options, unless it is already present in it.'; $lines[] = 'Write "without": a short, neutral, plain professional rewrite of the given text (same facts, no particular voice).'; $lines[] = 'Write "with": the SAME facts rewritten using the brand voice described below.'; $lines[] = 'Write "notes": 1-3 short plain-text sentences naming the concrete differences (e.g. shorter sentences, warmer tone), never a vague claim like "100% brand match".'; if ( '' !== $voice['block'] ) { $lines[] = $voice['block']; } if ( ! empty( $voice['banned'] ) ) { $lines[] = 'Never use these words or phrases in "with": ' . implode( ', ', $voice['banned'] ) . '.'; } $user = '' . "\n" . $source_text . "\n" . ''; return array( 'system' => implode( "\n\n", $lines ), 'user' => $user, 'schema' => array( 'type' => 'object', 'properties' => array( 'without' => array( 'type' => 'string' ), 'with' => array( 'type' => 'string' ), 'notes' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ), ), 'required' => array( 'without', 'with', 'notes' ), 'additionalProperties' => false, ), 'schemaName' => 'aipt_blueprint_voice_test', 'maxCalls' => 2, 'deadline' => microtime( true ) + 60.0, 'validate' => static function ( $decoded ) { return ( is_array( $decoded ) && isset( $decoded['without'], $decoded['with'] ) ) ? array( 'ok' => true, 'errors' => array() ) : array( 'ok' => false, 'errors' => array( 'missing without/with' ) ); }, ); } /** * Learning a voice from the store's own descriptions is Business (the * same paid capability as a voice's writing samples). This route only * refuses it; the Business build registers its own handler in its * place (`Premium\ContentStructure\VoiceSuggestionsController`). * * @param WP_REST_Request $request Request object. * @return WP_Error */ public static function suggestions( WP_REST_Request $request ): WP_Error { if ( null === ( new BrandProfileStore() )->get( (int) $request->get_param( 'id' ) ) ) { return self::error( 'aipt_blueprint_brand_profile_not_found', __( 'This voice no longer exists.', 'ai-product-tools' ), 404 ); } $reason = PlanDenial::reason( 'example_descriptions', PlanDenial::PLAN_PAID ); return self::error( 'aipt_blueprint_plan_required', PlanDenial::message( $reason, __( 'Suggestions from your own descriptions are part of the Business plan.', 'ai-product-tools' ) ), 403, array( 'reason' => $reason ) ); } /** * @return StructuredCallerInterface */ private static function caller(): StructuredCallerInterface { return self::$callerOverride ?? new StructuredCaller(); } }