encryption = $encryption; } /** * {@inheritDoc} */ public function complete( string $prompt, int $max_tokens = 600 ): string { $api_key = $this->get_api_key(); if ( empty( $api_key ) ) { throw new AIProviderException( esc_html__( 'Anthropic API key is not configured.', 'ai-discoverability-kit' ) ); } $body = array( 'model' => self::MODEL, 'max_tokens' => $max_tokens, 'messages' => array( array( 'role' => 'user', 'content' => $prompt, ), ), ); $response = wp_remote_post( self::ENDPOINT, array( 'timeout' => 45, 'headers' => array( 'x-api-key' => $api_key, 'anthropic-version' => self::API_VERSION, 'content-type' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { throw new AIProviderException( esc_html( $response->get_error_message() ) ); } $status = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 200 !== $status ) { $error_msg = $body['error']['message'] ?? __( 'Unknown Anthropic API error.', 'ai-discoverability-kit' ); throw new AIProviderException( esc_html( $error_msg ) ); } return trim( $body['content'][0]['text'] ?? '' ); } /** * {@inheritDoc} */ public function validate_api_key(): bool { $api_key = $this->get_api_key(); if ( empty( $api_key ) ) { return false; } try { $response = wp_remote_post( self::ENDPOINT, array( 'timeout' => 15, 'headers' => array( 'x-api-key' => $api_key, 'anthropic-version' => self::API_VERSION, 'content-type' => 'application/json', ), 'body' => wp_json_encode( array( 'model' => self::MODEL, 'max_tokens' => 5, 'messages' => array( array( 'role' => 'user', 'content' => 'hi', ), ), ) ), ) ); if ( is_wp_error( $response ) ) { return false; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return isset( $body['content'] ) && is_array( $body['content'] ); } catch ( \Exception $e ) { return false; } } /** * {@inheritDoc} */ public function get_provider_name(): string { return 'anthropic'; } /** * {@inheritDoc} */ public function estimate_cost_usd( int $input_tokens, int $output_tokens ): float { return ( $input_tokens / 1000 * self::INPUT_COST_PER_1K ) + ( $output_tokens / 1000 * self::OUTPUT_COST_PER_1K ); } /** * Get the decrypted API key. * * @return string Decrypted API key, or empty string if not set. */ private function get_api_key(): string { $encrypted = get_option( 'aicommerce_anthropic_api_key', '' ); if ( empty( $encrypted ) ) { return ''; } return $this->encryption->decrypt( $encrypted ); } }