[ 'id' => 'grok-4.7', 'name' => 'Grok 4.7 (Flagship)', 'context' => 500000, 'input_price' => 2.00, 'output_price' => 6.00, ], 'grok-4.6' => [ 'id' => 'grok-4.6', 'name' => 'Grok 4.6', 'context' => 500000, 'input_price' => 2.00, 'output_price' => 6.00, ], 'grok-4.5' => [ 'id' => 'grok-4.5', 'name' => 'Grok 4.5 (Flagship)', 'context' => 500000, 'input_price' => 2.00, 'output_price' => 6.00, ], 'grok-4.3' => [ 'id' => 'grok-4.3', 'name' => 'Grok 4.3', 'context' => 1000000, 'input_price' => 1.25, 'output_price' => 2.50, ], ]; } public function get_api_key(): string { $options = get_option( 'aibp_settings', [] ); return $options['grok_api_key'] ?? ''; } public function generate( string $prompt, array $args = [] ): string { $api_key = $this->get_api_key(); if ( empty( $api_key ) ) { throw new \Exception( esc_html__( 'Grok API key is not configured.', 'ai-bulk-post' ) ); } $defaults = [ 'model' => 'grok-4.5', 'messages' => [ [ 'role' => 'user', 'content' => $prompt, ], ], 'temperature' => $args['temperature'] ?? 0.7, 'max_tokens' => $args['max_tokens'] ?? 4000, 'frequency_penalty' => 0, 'presence_penalty' => 0, ]; $request_args = wp_parse_args( $args, $defaults ); unset( $request_args['temperature'], $request_args['max_tokens'] ); $response = wp_remote_post( self::api_base_url() . '/chat/completions', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ], 'body' => wp_json_encode( $request_args ), 'timeout' => 120, ] ); if ( is_wp_error( $response ) ) { throw new \Exception( esc_html( $response->get_error_message() ) ); } $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code !== 200 ) { $message = $body['error']['message'] ?? esc_html__( 'Unknown API error.', 'ai-bulk-post' ); throw new \Exception( esc_html( sprintf( 'Grok API error (%d): %s', $code, $message ) ) ); } return trim( $body['choices'][0]['message']['content'] ?? '' ); } public function validate_key( string $api_key ): bool { $response = wp_remote_get( self::api_base_url() . '/models', [ 'headers' => [ 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 15, ] ); if ( is_wp_error( $response ) ) { return false; } return wp_remote_retrieve_response_code( $response ) === 200; } }