provider = $provider; $this->model = $model; $this->key = $key; $this->request = $request; } /** * Ask the model. * * @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 ) { $token_param = isset( $this->request['token_param'] ) ? $this->request['token_param'] : 'max_tokens'; $body = array( 'model' => $this->model, 'messages' => array( array( 'role' => 'system', 'content' => self::system_text( $system ), ), array( 'role' => 'user', 'content' => $user, ), ), 'stream' => false, $token_param => (int) $max_tokens, ); if ( ! isset( $this->request['temperature'] ) || $this->request['temperature'] ) { $body['temperature'] = 0.2; } if ( null !== $schema ) { $body['response_format'] = array( 'type' => 'json_object' ); } if ( ! empty( $this->request['extra'] ) && is_array( $this->request['extra'] ) ) { $body = array_merge( $body, $this->request['extra'] ); } $result = $this->post( $body ); for ( $i = 0; $i < 4 && ! is_wp_error( $result ) && 400 === $result['code']; $i++ ) { $fixed = self::without_rejected( $body, Softvolt_Ai_Bodyguard_Providers::error_text( $result ) ); if ( null === $fixed ) { break; } $body = $fixed; $result = $this->post( $body ); } 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'] ) ); } $choice = isset( $result['body']['choices'][0] ) && is_array( $result['body']['choices'][0] ) ? $result['body']['choices'][0] : array(); $message = isset( $choice['message'] ) && is_array( $choice['message'] ) ? $choice['message'] : null; if ( null === $message ) { /* translators: %s: provider name. */ return new WP_Error( 'svabg_empty', sprintf( __( '%s sent no message.', 'ai-guardwall' ), $this->provider['name'] ) ); } $text = ''; if ( isset( $message['content'] ) && is_string( $message['content'] ) ) { $text = $message['content']; } elseif ( isset( $message['content'] ) && is_array( $message['content'] ) ) { foreach ( $message['content'] as $part ) { if ( is_array( $part ) && isset( $part['text'] ) ) { $text .= $part['text']; } } } return array( 'text' => trim( $text ), 'json' => null !== $schema ? Softvolt_Ai_Bodyguard_Providers::parse_json( $text ) : null, 'stop' => isset( $choice['finish_reason'] ) ? (string) $choice['finish_reason'] : '', '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.', 400 ); } private static function system_text( $system ) { if ( is_string( $system ) ) { return $system; } $parts = array(); foreach ( (array) $system as $block ) { $parts[] = is_array( $block ) ? (string) $block['text'] : (string) $block; } return implode( "\n\n", $parts ); } private static function without_rejected( $body, $error ) { $error = strtolower( $error ); foreach ( array( 'response_format', 'thinking', 'reasoning_effort', 'temperature' ) as $param ) { if ( isset( $body[ $param ] ) && ( false !== strpos( $error, $param ) || ( 'response_format' === $param && false !== strpos( $error, 'json' ) ) ) ) { unset( $body[ $param ] ); return $body; } } if ( isset( $body['max_tokens'] ) && false !== strpos( $error, 'max_tokens' ) ) { $body['max_completion_tokens'] = $body['max_tokens']; unset( $body['max_tokens'] ); return $body; } if ( isset( $body['max_completion_tokens'] ) && false !== strpos( $error, 'max_completion_tokens' ) ) { $body['max_tokens'] = $body['max_completion_tokens']; unset( $body['max_completion_tokens'] ); return $body; } return null; } private function post( $body ) { $headers = array(); if ( '' !== $this->key ) { $headers['Authorization'] = 'Bearer ' . $this->key; } if ( 'openrouter' === $this->provider['id'] ) { $headers['HTTP-Referer'] = home_url( '/' ); $headers['X-Title'] = 'AI GuardWall – AI-Powered Digital Security Guardian'; } return Softvolt_Ai_Bodyguard_Providers::post_json( $this->provider['base_url'] . '/chat/completions', $headers, $body, (int) Softvolt_Ai_Bodyguard_Settings::get( 'ai_timeout', 120 ) ); } }