provider = $provider; $this->model = $model; $this->key = $key; $this->request = $request; } /** * Ask Claude. * * @since 1.0.0 * @param string|array $system Instructions (a string, or blocks: array( array( 'text' => ..., 'cache' => bool ) )). * @param string $user The task. * @param int $max_tokens Longest answer. * @param array|null $schema JSON schema of the wanted answer (null for free text). * @return array|WP_Error array( 'text' => string, 'json' => array|null, 'stop' => string, 'usage' => array ). */ public function complete( $system, $user, $max_tokens = 6000, $schema = null ) { $body = array( 'model' => $this->model, 'max_tokens' => max( 2048, (int) $max_tokens ), 'system' => self::system_blocks( $system ), 'messages' => array( array( 'role' => 'user', 'content' => $user, ), ), ); $headers = array( 'x-api-key' => $this->key, 'anthropic-version' => '2023-06-01', ); $effort = (string) Softvolt_Ai_Bodyguard_Settings::get( 'ai_effort', 'medium' ); if ( ! empty( $this->request['effort'] ) ) { $effort = $this->request['effort']; } if ( in_array( $effort, array( 'low', 'medium', 'high' ), true ) ) { $body['output_config'] = array( 'effort' => $effort ); } if ( null !== $schema ) { $body['output_config'] = isset( $body['output_config'] ) ? $body['output_config'] : array(); $body['output_config']['format'] = array( 'type' => 'json_schema', 'schema' => $schema, ); } if ( ! empty( $this->request['fallbacks'] ) ) { $body['fallbacks'] = 'default'; $headers['anthropic-beta'] = 'server-side-fallback-2026-07-01'; } $result = $this->post( $body, $headers ); for ( $i = 0; $i < 4 && ! is_wp_error( $result ) && 400 === $result['code']; $i++ ) { $error = strtolower( Softvolt_Ai_Bodyguard_Providers::error_text( $result ) ); if ( isset( $body['fallbacks'] ) && ( false !== strpos( $error, 'fallback' ) || false !== strpos( $error, 'beta' ) ) ) { unset( $body['fallbacks'], $headers['anthropic-beta'] ); } elseif ( isset( $body['output_config']['format'] ) && ( false !== strpos( $error, 'format' ) || false !== strpos( $error, 'schema' ) ) ) { unset( $body['output_config']['format'] ); if ( empty( $body['output_config'] ) ) { unset( $body['output_config'] ); } } elseif ( isset( $body['output_config']['effort'] ) && ( false !== strpos( $error, 'effort' ) || false !== strpos( $error, 'output_config' ) ) ) { unset( $body['output_config']['effort'] ); if ( empty( $body['output_config'] ) ) { unset( $body['output_config'] ); } } elseif ( false !== strpos( $error, 'cache_control' ) ) { $body['system'] = self::system_blocks( $system, false ); } else { break; } $result = $this->post( $body, $headers ); } if ( is_wp_error( $result ) ) { return new WP_Error( 'svabg_unreachable', /* translators: 1: provider name, 2: error message. */ sprintf( __( 'Could not reach %1$s: %2$s', 'ai-guardwall' ), $this->provider['name'], $result->get_error_message() ) ); } if ( $result['code'] < 200 || $result['code'] >= 300 ) { return new WP_Error( 'svabg_http', /* translators: 1: provider name, 2: HTTP status code, 3: error message. */ sprintf( __( '%1$s answered %2$d: %3$s', 'ai-guardwall' ), $this->provider['name'], $result['code'], Softvolt_Ai_Bodyguard_Providers::error_text( $result ) ), array( 'status' => $result['code'] ) ); } $stop = isset( $result['body']['stop_reason'] ) ? (string) $result['body']['stop_reason'] : ''; if ( 'refusal' === $stop ) { $category = isset( $result['body']['stop_details']['category'] ) ? (string) $result['body']['stop_details']['category'] : ''; return new WP_Error( 'svabg_refusal', '' !== $category /* translators: %s: refusal category. */ ? sprintf( __( 'The model declined to analyse this item (category: %s). Try another model, or review it by hand.', 'ai-guardwall' ), $category ) : __( 'The model declined to analyse this item. Try another model, or review it by hand.', 'ai-guardwall' ) ); } $content = isset( $result['body']['content'] ) && is_array( $result['body']['content'] ) ? $result['body']['content'] : array(); $text = array(); foreach ( $content as $block ) { if ( isset( $block['type'] ) && 'text' === $block['type'] && isset( $block['text'] ) ) { $text[] = (string) $block['text']; } } $text = trim( implode( "\n", $text ) ); if ( 'max_tokens' === $stop && null !== $schema ) { return new WP_Error( 'svabg_truncated', __( 'The answer was cut off by the token limit. Raise "Longest answer" on the AI model tab and try again.', 'ai-guardwall' ) ); } return array( 'text' => $text, 'json' => null !== $schema ? Softvolt_Ai_Bodyguard_Providers::parse_json( $text ) : null, 'stop' => $stop, 'usage' => isset( $result['body']['usage'] ) && is_array( $result['body']['usage'] ) ? $result['body']['usage'] : array(), 'model' => isset( $result['body']['model'] ) ? (string) $result['body']['model'] : $this->model, ); } /** * A one-line check that the key and the model work. * * @since 1.0.0 * @return array|WP_Error */ public function test() { return $this->complete( 'You are a connection test. Reply with the single word OK.', 'Say OK.', 2048 ); } /** * System blocks, with the stable knowledge text marked for prompt caching. * * @param string|array $system Text or blocks. * @param bool $cache Whether to add cache_control. * @return array */ private static function system_blocks( $system, $cache = true ) { $blocks = array(); if ( is_string( $system ) ) { return array( array( 'type' => 'text', 'text' => $system, ), ); } foreach ( (array) $system as $block ) { $entry = array( 'type' => 'text', 'text' => is_array( $block ) ? (string) $block['text'] : (string) $block, ); if ( $cache && is_array( $block ) && ! empty( $block['cache'] ) ) { $entry['cache_control'] = array( 'type' => 'ephemeral' ); } $blocks[] = $entry; } return $blocks; } private function post( $body, $headers ) { return Softvolt_Ai_Bodyguard_Providers::post_json( $this->provider['base_url'] . '/messages', $headers, $body, (int) Softvolt_Ai_Bodyguard_Settings::get( 'ai_timeout', 120 ) ); } }