settings = $settings; } /** * Provider base URL without a trailing slash. * * @return string */ protected function endpoint(): string { $endpoint = (string) $this->settings->get( 'llmProviders.aisentic.endpoint', AISENTIC_AI_PROVIDER ); return rtrim( $endpoint, '/' ); } /** * Provider API key. * * @return string */ protected function api_key(): string { return (string) $this->settings->get( 'llmProviders.aisentic.apiKey', '' ); } /** * True when both the endpoint and the key are present. * * @return bool */ public function is_configured(): bool { return '' !== $this->endpoint() && '' !== $this->api_key(); } /** * Standard error for a provider that is not set up. * * @return WP_Error */ protected function not_configured(): WP_Error { return new WP_Error( 'aisentic_voice_not_configured', __( 'The Aisentic provider is not configured.', 'aisentic' ), [ 'status' => 503 ] ); } /** * Turn a wp_remote_* result into the raw body string or a WP_Error. * * @param array|WP_Error $response Raw response. * @param string $context Short label used in the error message. * @return array|string|WP_Error */ protected function unwrap( $response, string $context ) { if ( is_wp_error( $response ) ) { return new WP_Error( 'aisentic_voice_transport', sprintf( /* translators: 1: step name, 2: underlying error. */ __( 'Could not reach the voice service (%1$s): %2$s', 'aisentic' ), $context, $response->get_error_message() ), [ 'status' => 502 ] ); } $code = (int) wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( $code < 200 || $code >= 300 ) { $detail = ''; $parsed = json_decode( $body, true ); if ( is_array( $parsed ) && ! empty( $parsed['message'] ) ) { $detail = (string) $parsed['message']; } // 402 means the site is out of credits. Say so plainly. if ( 402 === $code ) { return new WP_Error( 'aisentic_voice_no_credits', __( 'This site has run out of AI credits.', 'aisentic' ), [ 'status' => 402 ] ); } return new WP_Error( 'aisentic_voice_upstream', sprintf( /* translators: 1: step name, 2: HTTP status, 3: detail. */ __( 'The voice service failed (%1$s, HTTP %2$d) %3$s', 'aisentic' ), $context, $code, $detail ), [ 'status' => 502 ] ); } return $body; } }