*/ private const ALLOWED_PROVIDERS = array( 'openai', 'gemini', 'openrouter', 'claude' ); /** * Upper bound on per-side token counts (10 million). * * Guards against absurd inputs that would let the UI fabricate * astronomical preview cost numbers. */ private const MAX_TOKENS = 10000000; /** * Callable returning {0:float, 1:float} per-million USD rates. * * Signature: fn(string $provider, string $model): array{0:float,1:float} * * @since 2.6.1 * @var callable */ private $pricing_resolver; /** * Callable returning PricingFeedManager::get_snapshot_meta() shape. * * Signature: fn(): array{status:string, ...} * * @since 2.6.1 * @var callable */ private $snapshot_meta_resolver; /** * Callable telling whether a published rate exists for the model. * * Signature: fn(string $provider, string $model): bool * * @since 2.8.3 * @var callable */ private $rate_known_resolver; /** * Capability check callable. Defaults to WP's current_user_can(). * * Signature: fn(string $capability): bool * * @since 2.6.1 * @var callable */ private $current_user_can; /** * Constructor. * * Accepts an optional overrides map so unit tests can inject pure callables * without hitting the real PricingFeedManager singleton or WP globals. * Uses the same optional-overrides dependency-injection pattern as the * other Phase 0 REST controllers. * * Recognised keys: `pricing_resolver`, `snapshot_meta_resolver`, * `rate_known_resolver`, `current_user_can`. * * @since 2.6.1 * * @param array $overrides Optional dependency overrides. */ public function __construct( array $overrides = array() ) { $this->pricing_resolver = isset( $overrides['pricing_resolver'] ) && \is_callable( $overrides['pricing_resolver'] ) ? $overrides['pricing_resolver'] : array( PricingFeedManager::instance(), 'resolve' ); $this->snapshot_meta_resolver = isset( $overrides['snapshot_meta_resolver'] ) && \is_callable( $overrides['snapshot_meta_resolver'] ) ? $overrides['snapshot_meta_resolver'] : array( PricingFeedManager::instance(), 'get_snapshot_meta' ); $this->rate_known_resolver = isset( $overrides['rate_known_resolver'] ) && \is_callable( $overrides['rate_known_resolver'] ) ? $overrides['rate_known_resolver'] : array( PricingFeedManager::instance(), 'has_known_rate' ); $this->current_user_can = isset( $overrides['current_user_can'] ) && \is_callable( $overrides['current_user_can'] ) ? $overrides['current_user_can'] : 'current_user_can'; } /** * Register REST routes. * * @since 2.6.1 * * @return void */ public function register_routes(): void { \register_rest_route( self::NAMESPACE, self::ROUTE, array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_preview' ), 'permission_callback' => array( $this, 'check_permissions' ), 'args' => array( 'provider' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key', ), 'model' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'input_tokens' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), 'output_tokens' => array( 'type' => 'integer', 'required' => false, 'default' => 0, 'sanitize_callback' => 'absint', ), ), ) ); } /** * Handle GET aipt/v1/pricing/preview. * * @since 2.6.1 * * @param \WP_REST_Request $request Incoming REST request. * @return \WP_REST_Response|\WP_Error Pricing preview payload or error. */ public function get_preview( \WP_REST_Request $request ) { $provider = (string) $request->get_param( 'provider' ); $model = (string) $request->get_param( 'model' ); if ( '' === $provider ) { return new \WP_Error( 'missing_provider', \__( 'The provider parameter is required.', 'ai-product-tools' ), array( 'status' => 400 ) ); } if ( '' === $model ) { return new \WP_Error( 'missing_model', \__( 'The model parameter is required.', 'ai-product-tools' ), array( 'status' => 400 ) ); } if ( ! \in_array( $provider, self::ALLOWED_PROVIDERS, true ) ) { return new \WP_Error( 'invalid_provider', \__( 'The provider is not one of the supported AIPT providers.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $input_tokens = (int) $request->get_param( 'input_tokens' ); $output_tokens = (int) $request->get_param( 'output_tokens' ); if ( $input_tokens > self::MAX_TOKENS || $output_tokens > self::MAX_TOKENS ) { return new \WP_Error( 'tokens_out_of_range', \__( 'Token counts must be at most 10,000,000 per side.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $rate = \call_user_func( $this->pricing_resolver, $provider, $model ); $rate_resolved = \is_array( $rate ) && isset( $rate[0], $rate[1] ); $input_per_mil = $rate_resolved ? (float) $rate[0] : 0.0; $output_per_mil = $rate_resolved ? (float) $rate[1] : 0.0; $rate_known = (bool) \call_user_func( $this->rate_known_resolver, $provider, $model ); $is_unresolved = ! $rate_resolved || ! $rate_known || ( 0.0 === $input_per_mil && 0.0 === $output_per_mil ); if ( $is_unresolved ) { return new \WP_REST_Response( array( 'provider' => $provider, 'model' => $model, 'inputTokens' => $input_tokens, 'outputTokens' => $output_tokens, 'inputPerMillionUsd' => null, 'outputPerMillionUsd' => null, 'estimatedUsd' => null, 'source' => 'fallback', ), 200 ); } $source = $this->detect_source(); $estimated_usd = ( $input_tokens / 1000000 ) * $input_per_mil + ( $output_tokens / 1000000 ) * $output_per_mil; return new \WP_REST_Response( array( 'provider' => $provider, 'model' => $model, 'inputTokens' => $input_tokens, 'outputTokens' => $output_tokens, 'inputPerMillionUsd' => $input_per_mil, 'outputPerMillionUsd' => $output_per_mil, 'estimatedUsd' => round( $estimated_usd, 6 ), 'source' => $source, ), 200 ); } /** * Permission callback. * * React UI is mounted under WooCommerce-managed product screens which run * under shop_manager/editor too, so we gate on `edit_products` rather than * `manage_options`. This is a GET-only route, so capability is the whole * gate: a read never needs the `wp_rest` nonce, and requiring one anyway * would permanently 403 an Application-Password/Basic-Auth REST client, * which has no way to obtain one (@see \AIPT\Utils\RestNonceGuard). * * @since 2.6.1 * * @param \WP_REST_Request $request Incoming REST request. * @return bool|\WP_Error True when `edit_products` is held, \WP_Error with * status 403 otherwise. */ public function check_permissions( \WP_REST_Request $request ) { if ( ! \call_user_func( $this->current_user_can, 'edit_products' ) ) { return new \WP_Error( 'rest_forbidden', \__( 'You do not have permission to access this endpoint.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return true; } /** * Best-effort source attribution for the resolved response payload. * * Reports `snapshot` when the cached remote snapshot is fresh, `baseline` * otherwise. Callers must only invoke this on the resolved code path; the * unresolved branch hardcodes `fallback` upstream. The full source chain * inside PricingFeedManager (snapshot/filter/baseline/zero) is collapsed * here because the manager does not expose introspection hooks — see the * task playbook for the agreed approximation. * * @since 2.6.1 * * @return string Either `snapshot` or `baseline`. */ private function detect_source(): string { $meta = \call_user_func( $this->snapshot_meta_resolver ); if ( ! \is_array( $meta ) ) { return 'baseline'; } $status = isset( $meta['status'] ) ? (string) $meta['status'] : ''; return 'fresh' === $status ? 'snapshot' : 'baseline'; } }