400 ) ); } $provider = ( isset( $input['provider'] ) && is_string( $input['provider'] ) ) ? $input['provider'] : (string) get_option( 'aipt_api_provider', 'openai' ); if ( ! in_array( $provider, ProviderNameMapper::aiptProviders(), true ) ) { return new \WP_Error( 'aipt_invalid_provider', sprintf( /* translators: %s: provider ID supplied by caller */ __( 'Invalid provider "%s". Must be one of: openai, gemini, claude, openrouter.', 'ai-product-tools' ), sanitize_text_field( $provider ) ), array( 'status' => 400 ) ); } $gate = AiSupportGate::guard(); if ( $gate instanceof \WP_Error ) { return $gate; } $rate_error = AbilityRateLimiter::consume( get_current_user_id(), 1 ); if ( null !== $rate_error ) { return $rate_error; } $model = ( isset( $input['model'] ) && is_string( $input['model'] ) && '' !== $input['model'] ) ? $input['model'] : ''; /** * Filter the AI client factory used by the invoke-provider ability. * * Third-party code and unit tests may replace the factory to inject * a mock or alternative implementation. * * @since 2.6.0 * * @param \AIPT\Api\Services\AIClientFactoryInterface $factory Default RestAPIClientFactory. */ $factory = apply_filters( 'aipt_invoke_provider_factory', new RestAPIClientFactory() ); try { $client = $factory->create( $provider ); $resolved_model = '' !== $model ? $model : $factory->getDefaultModel( $provider ); $text = $client->generate_description( '', wp_check_invalid_utf8( $input['prompt'] ), $resolved_model, null ); } catch ( \Throwable $e ) { if ( $e instanceof ProviderCallException ) { return self::providerErrorResponse( $e, $provider ); } return new \WP_Error( 'aipt_provider_error', esc_html( $e->getMessage() ), array( 'status' => 502 ) ); } return array( 'text' => $text, 'provider' => $provider, 'model' => $resolved_model, 'tokenUsage' => null, 'providerMetadata' => null, ); } /** * The WP_Error for a classified provider failure: the merchant sentence * as the message, the structured fields a caller needs to decide * whether to retry, and the technical text only in error_details. * * @param ProviderCallException $e Caught provider failure. * @param string $provider Provider id resolved for this call, used when the exception carries none. * @return \WP_Error * * @since 2.8.0 */ private static function providerErrorResponse( ProviderCallException $e, string $provider ): \WP_Error { $provider_error = $e->toResponseArray( $provider ); return new \WP_Error( $provider_error['code'], $provider_error['message'], array( 'status' => ProviderCallException::httpStatusFor( $e->reason() ), 'reason' => $provider_error['reason'], 'retryable' => $provider_error['retryable'], 'runWide' => $provider_error['runWide'], 'retryAfter' => $provider_error['retryAfter'], 'provider' => $provider_error['provider'], 'helpUrl' => $provider_error['helpUrl'], 'error_details' => $e->getMessage(), ) ); } /** * Permission check for the invoke-provider ability. * * Restricts access to users with the manage_woocommerce capability * because invoking AI providers incurs real usage against the site's * own configured provider account (BYOK). * * @since 2.6.0 * * @return bool True if the current user can manage WooCommerce, false otherwise. */ public static function permission_check(): bool { return current_user_can( 'manage_woocommerce' ); } }