inner = $inner; $this->provider = $provider; $this->scope = $scope; $this->sleeper = $sleeper ?? array( self::class, 'defaultSleeper' ); $this->jitter = $jitter ?? array( self::class, 'defaultJitter' ); } /** * The wrapped provider client. * * @return AIClientInterface */ public function inner(): AIClientInterface { return $this->inner; } public function generate_description( string $system_prompt, string $user_prompt, string $model = '', ?int $maximum_length = null ): string { $args = func_get_args(); return $this->call( $model, function () use ( $args ) { return $this->inner->generate_description( ...$args ); } ); } public function generate_faq_content( string $system_prompt, string $user_prompt, ?string $model = null ): string { $args = func_get_args(); return $this->call( (string) $model, function () use ( $args ) { return $this->inner->generate_faq_content( ...$args ); } ); } public function generate_from_vision( string $system_prompt, string $user_prompt, string $image_url, ?string $model = null ): string { $args = func_get_args(); return $this->call( (string) $model, function () use ( $args ) { return $this->inner->generate_from_vision( ...$args ); } ); } public function generate_structured( string $system_prompt, string $user_prompt, array $json_schema, string $model = '', array $options = array() ): array { if ( ! $this->inner instanceof StructuredOutputClientInterface ) { throw new \BadMethodCallException( 'The wrapped client does not support structured output.' ); } $inner = $this->inner; $args = func_get_args(); return $this->call( $model, function () use ( $inner, $args ) { return $inner->generate_structured( ...$args ); } ); } /** * Key validation is a static call on the provider client itself. * * @param string $api_key Key to validate. * @return bool * @throws \LogicException Always. */ public static function validate_api_key( string $api_key ): bool { throw new \LogicException( 'Validate a key through the provider client class, not through ProviderCallGuard.' ); } public function get_available_models(): array { return $this->inner->get_available_models(); } /** * Forward every other client method (setParameters and the like). * * @param string $name Method name. * @param array $arguments Arguments. * @return mixed */ public function __call( string $name, array $arguments ) { return $this->inner->{$name}( ...$arguments ); } /** * @param string $model Model id the call uses ('' for the client default). * @param callable $request Performs the call. * @return mixed The call's result. * @throws ProviderCallException The provider failure, possibly remembered from an earlier call. */ private function call( string $model, callable $request ) { $open = ProviderCircuit::openFailure( $this->scope, $model ); if ( null !== $open ) { throw $open; } $attempt = 0; while ( true ) { ++$attempt; ProviderCallHeartbeat::beat(); SpendLimit::refuseIfReached( $this->provider ); RequestClaimStore::touchOwned(); try { return $request(); } catch ( ProviderCallException $e ) { $wait = $this->retryWait( $e, $attempt ); if ( null !== $wait ) { call_user_func( $this->sleeper, $wait ); continue; } if ( ProviderCallException::isSetupReason( $e->reason() ) ) { Logger::debug( 'provider', 'Provider call not made, setup incomplete', array( 'provider' => $this->provider, 'reason' => $e->reason() ) ); } else { Logger::error( 'provider', 'Provider call failed', array( 'provider' => $this->provider, 'reason' => $e->reason(), 'error' => $e->getMessage() ) ); } ProviderCircuit::record( $this->scope, $model, $e ); $this->markUnavailableModel( $model, $e ); throw $e; } } } /** * Remember that the provider no longer offers a custom model the owner added. * An empty model means the client's own default, which is always a catalog id, * so there is nothing to mark; a catalog id is never marked (the store ignores it). * * @param string $model Model id the call used ('' for the client default). * @param ProviderCallException $e The failure. */ private function markUnavailableModel( string $model, ProviderCallException $e ): void { if ( '' !== $model && ProviderCallException::MODEL_NOT_FOUND === $e->reason() ) { CustomModelStore::mark_unavailable( $this->provider, $model ); } } /** * Seconds to wait before retrying, or null when the failure must not be * retried. */ private function retryWait( ProviderCallException $e, int $attempt ): ?float { if ( $attempt >= self::MAX_ATTEMPTS || ! in_array( $e->reason(), ProviderCallException::TRANSIENT_REASONS, true ) ) { return null; } $retry_after = $e->retryAfterSeconds(); if ( null !== $retry_after && $retry_after > 0 ) { $wait = (float) $retry_after; } else { $wait = self::DEFAULT_RETRY_SECONDS + (float) call_user_func( $this->jitter ); } return $wait <= self::MAX_RETRY_WAIT_SECONDS && ProviderCallDeadline::allowsRetryAfter( $wait ) ? $wait : null; } /** * 0 to 0.5 seconds from wp_rand(), the WordPress source phpcs asks for. The value only spreads retries * out and protects nothing; unlike random_int() it never throws. */ private static function defaultJitter(): float { return wp_rand( 0, 500 ) / 1000; } private static function defaultSleeper( float $seconds ): void { usleep( (int) round( $seconds * 1000000 ) ); } }