inner = $inner; $this->provider = $provider; $this->scope = $scope; $this->sleeper = $sleeper ?? array( self::class, 'defaultSleeper' ); } /** * 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; try { return $request(); } catch ( ProviderCallException $e ) { $wait = $this->retryWait( $e, $attempt ); if ( null !== $wait ) { call_user_func( $this->sleeper, $wait ); continue; } ErrorHandler::log_error( sprintf( '[%s] %s', $this->provider, $e->getMessage() ), 'error' ); ProviderCircuit::record( $this->scope, $model, $e ); throw $e; } } } /** * 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(); $wait = ( null !== $retry_after && $retry_after > 0 ) ? (float) $retry_after : self::DEFAULT_RETRY_SECONDS; return $wait <= self::MAX_RETRY_WAIT_SECONDS ? $wait : null; } private static function defaultSleeper( float $seconds ): void { usleep( (int) round( $seconds * 1000000 ) ); } }