client_factory = $client_factory ?? array( AIClientFactory::class, 'create' ); } /** * Test the saved key of a provider. * * @param string $provider AIPT provider id. * @return array|\WP_Error See the class description. */ public function test( string $provider ) { $provider = strtolower( trim( $provider ) ); $model = $this->modelFor( $provider ); try { $client = call_user_func( $this->client_factory, $provider ); ProviderCallDeadline::run( microtime( true ) + self::DEADLINE_SECONDS, static function () use ( $client, $model ) { return $client->generate_description( self::SYSTEM_PROMPT, self::USER_PROMPT, $model ); } ); return $this->works( $provider, $model ); } catch ( ProviderCallException $e ) { return $this->fromProviderFailure( $e, $provider, $model ); } catch ( \Throwable $e ) { Logger::error( 'provider', 'API key test failed unexpectedly', array( 'provider' => $provider, 'error' => get_class( $e ), ) ); return new \WP_Error( 'key_test_failed', __( 'The key test failed unexpectedly. Please try again.', 'ai-product-tools' ), array( 'status' => 500, 'provider' => $provider, 'model' => $model, ) ); } } /** * The model the store would use for this provider: the saved model for * the default provider, otherwise the provider's default model. * * @param string $provider AIPT provider id. * @return string */ private function modelFor( string $provider ): string { $default_provider = strtolower( trim( (string) get_option( 'aipt_api_provider', 'openai' ) ) ); if ( $provider === $default_provider ) { return ModelCatalog::normalize_model( $provider, (string) get_option( 'aipt_model', '' ) ); } return ModelCatalog::get_default_model( $provider ); } /** * @param string $provider AIPT provider id. * @param string $model Model id the test used. * @return array */ private function works( string $provider, string $model ): array { return array( 'success' => true, 'message' => sprintf( /* translators: 1: AI provider name, e.g. "OpenAI". 2: model id. */ __( '%1$s answered a test request with the model %2$s. The key can generate.', 'ai-product-tools' ), ProviderCallException::providerLabel( $provider ), $model ), 'data' => $this->data( $provider, $model, 'works' ), ); } /** * @param ProviderCallException $e The provider failure. * @param string $provider AIPT provider id. * @param string $model Model id the test used. * @return array|\WP_Error */ private function fromProviderFailure( ProviderCallException $e, string $provider, string $model ) { $reason = $e->reason(); if ( in_array( $reason, self::ANSWERED_REASONS, true ) ) { return $this->works( $provider, $model ); } if ( in_array( $reason, ProviderCallException::TRANSIENT_REASONS, true ) ) { return array( 'success' => false, 'message' => sprintf( /* translators: %s: why the provider could not be reached or did not answer properly. */ __( 'The test did not finish, so the key was not checked. %s The key stays saved.', 'ai-product-tools' ), $e->merchantMessage( $provider ) ), 'data' => array_merge( $this->data( $provider, $model, 'inconclusive' ), array( 'reason' => $reason, 'helpUrl' => ProviderCallException::helpUrl( $reason ), ) ), ); } $unavailable = ProviderCallException::AI_DISABLED === $reason || ProviderCallException::SPEND_LIMIT_REACHED === $reason; return new \WP_Error( 'key_test_failed', ProviderCallException::MODEL_NOT_FOUND === $reason ? $this->modelNotOffered( $provider, $model ) : ProviderCallException::displayMessage( $e, $provider ), array( 'status' => $unavailable ? 503 : 400, 'reason' => $reason, 'helpUrl' => ProviderCallException::helpUrl( $reason ), 'provider' => $provider, 'model' => $model, ) ); } /** * The generic model-not-found sentence speaks of "the selected model", * which the merchant did not pick when the tested provider is not the * default one: name the model the test used and where to look. * * @param string $provider AIPT provider id. * @param string $model Model id the test used. * @return string */ private function modelNotOffered( string $provider, string $model ): string { return sprintf( /* translators: 1: AI provider name, e.g. "OpenAI". 2: model id. */ __( '%1$s does not offer the model %2$s to this key. Check which models the key may use in your %1$s account, or choose another model.', 'ai-product-tools' ), ProviderCallException::providerLabel( $provider ), $model ); } /** * @param string $provider AIPT provider id. * @param string $model Model id the test used. * @param string $outcome `works` or `inconclusive`. * @return array */ private function data( string $provider, string $model, string $outcome ): array { return array( 'provider' => $provider, 'model' => $model, 'outcome' => $outcome, 'tested_at' => current_time( 'mysql' ), ); } }