'custom', 'name' => __( 'Other (OpenAI-compatible)', 'ai-guardwall' ), 'group' => 'custom', 'api' => 'openai', 'base_url' => (string) Softvolt_Ai_Bodyguard_Settings::get( 'custom_base_url' ), 'default_model' => (string) Softvolt_Ai_Bodyguard_Settings::get( 'custom_model' ), 'models' => array(), 'request' => array( 'token_param' => 'max_tokens', 'temperature' => true, ), ); } $providers = self::providers(); return isset( $providers[ $id ] ) ? $providers[ $id ] : null; } /** * The label of a model, or its id. * * @since 1.0.0 * @param string $provider_id Provider. * @param string $model Model id. * @return string */ public static function model_label( $provider_id, $model ) { $provider = self::provider( $provider_id ); if ( $provider ) { foreach ( $provider['models'] as $entry ) { if ( $entry['id'] === $model ) { return $entry['label']; } } } return $model; } /** * Request tweaks for a model (the provider's, overridden by the model's). * * @since 1.0.0 * @param array $provider Provider. * @param string $model Model id. * @return array */ public static function request_for( $provider, $model ) { $request = isset( $provider['request'] ) && is_array( $provider['request'] ) ? $provider['request'] : array(); foreach ( isset( $provider['models'] ) ? $provider['models'] : array() as $entry ) { if ( $entry['id'] === $model && ! empty( $entry['request'] ) ) { $extra = array_merge( isset( $request['extra'] ) ? $request['extra'] : array(), isset( $entry['request']['extra'] ) ? $entry['request']['extra'] : array() ); $request = array_merge( $request, $entry['request'] ); if ( $extra ) { $request['extra'] = $extra; } } } return $request; } /** * Every free model OpenRouter lists right now (cached six hours). * * @since 1.0.0 * @return array|WP_Error */ public static function openrouter_free_models() { $cached = get_transient( 'svabg_openrouter_free' ); if ( is_array( $cached ) ) { return $cached; } $provider = self::provider( 'openrouter' ); if ( ! $provider ) { return new WP_Error( 'svabg_models', __( 'OpenRouter is not in the model list.', 'ai-guardwall' ) ); } $response = wp_remote_get( $provider['base_url'] . '/models', array( 'timeout' => 15, 'limit_response_size' => 8388608, 'user-agent' => Softvolt_Ai_Bodyguard_Providers::user_agent(), ) ); if ( is_wp_error( $response ) ) { return $response; } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) || ! is_array( $data ) || empty( $data['data'] ) ) { return new WP_Error( 'svabg_models', __( 'OpenRouter did not return its model list. Try again in a minute.', 'ai-guardwall' ) ); } $models = array(); foreach ( $data['data'] as $model ) { if ( ! is_array( $model ) || empty( $model['id'] ) || empty( $model['pricing'] ) ) { continue; } $free = isset( $model['pricing']['prompt'], $model['pricing']['completion'] ) && 0.0 === (float) $model['pricing']['prompt'] && 0.0 === (float) $model['pricing']['completion']; if ( $free ) { $models[] = array( 'id' => self::model_id( $model['id'] ), 'label' => sanitize_text_field( isset( $model['name'] ) ? $model['name'] : $model['id'] ), 'context' => isset( $model['context_length'] ) ? absint( $model['context_length'] ) : 0, ); } } usort( $models, function ( $a, $b ) { return $b['context'] - $a['context']; } ); set_transient( 'svabg_openrouter_free', $models, 6 * HOUR_IN_SECONDS ); return $models; } private static function clean( $data, $trusted ) { $out = array( 'version' => isset( $data['version'] ) && preg_match( '/^\d{4}\.\d{1,2}\.\d{1,2}$/', (string) $data['version'] ) ? (string) $data['version'] : '0.0.0', 'checked' => isset( $data['checked'] ) ? sanitize_text_field( (string) $data['checked'] ) : '', 'providers' => array(), ); $seen = array(); foreach ( isset( $data['providers'] ) && is_array( $data['providers'] ) ? $data['providers'] : array() as $provider ) { $clean = self::clean_provider( $provider, $trusted ); if ( $clean && ! isset( $seen[ $clean['id'] ] ) && count( $out['providers'] ) < 30 ) { $seen[ $clean['id'] ] = true; $out['providers'][] = $clean; } } return $out; } private static function clean_provider( $provider, $trusted ) { if ( ! is_array( $provider ) || empty( $provider['id'] ) || empty( $provider['name'] ) || empty( $provider['base_url'] ) ) { return null; } $id = sanitize_key( $provider['id'] ); if ( '' === $id || 'custom' === $id ) { return null; } $base_url = esc_url_raw( (string) $provider['base_url'], array( 'https' ) ); if ( '' === $base_url ) { return null; } $api = isset( $provider['api'] ) && 'anthropic' === $provider['api'] ? 'anthropic' : 'openai'; $group = isset( $provider['group'] ) && 'free' === $provider['group'] ? 'free' : 'pro'; $models = array(); foreach ( isset( $provider['models'] ) && is_array( $provider['models'] ) ? $provider['models'] : array() as $model ) { if ( ! is_array( $model ) || empty( $model['id'] ) ) { continue; } $entry = array( 'id' => self::model_id( $model['id'] ), 'label' => sanitize_text_field( isset( $model['label'] ) ? $model['label'] : $model['id'] ), ); if ( ! empty( $model['request'] ) && is_array( $model['request'] ) ) { $entry['request'] = self::clean_request( $model['request'] ); } if ( '' !== $entry['id'] && count( $models ) < 40 ) { $models[] = $entry; } } $default = self::model_id( isset( $provider['default_model'] ) ? $provider['default_model'] : '' ); if ( '' === $default && $models ) { $default = $models[0]['id']; } $urls = array(); foreach ( array( 'key_url', 'signup_url', 'terms_url', 'privacy_url' ) as $field ) { $urls[ $field ] = isset( $provider[ $field ] ) ? esc_url_raw( (string) $provider[ $field ], array( 'https' ) ) : ''; } return array_merge( array( 'id' => $id, 'name' => sanitize_text_field( $provider['name'] ), 'group' => $group, 'api' => $api, 'base_url' => untrailingslashit( $base_url ), 'default_model' => $default, 'live_models' => ! empty( $provider['live_models'] ) && 'openrouter' === $id, 'models' => $models, 'summary' => isset( $provider['summary'] ) ? sanitize_text_field( $provider['summary'] ) : '', 'limits' => isset( $provider['limits'] ) ? sanitize_text_field( $provider['limits'] ) : '', 'privacy' => isset( $provider['privacy'] ) ? sanitize_text_field( $provider['privacy'] ) : '', 'steps' => self::lines( isset( $provider['steps'] ) ? $provider['steps'] : array(), 12 ), 'request' => self::clean_request( isset( $provider['request'] ) && is_array( $provider['request'] ) ? $provider['request'] : array() ), 'remote' => ! $trusted, ), $urls ); } private static function clean_request( $request ) { $out = array(); if ( isset( $request['token_param'] ) && in_array( $request['token_param'], array( 'max_tokens', 'max_completion_tokens' ), true ) ) { $out['token_param'] = $request['token_param']; } if ( isset( $request['temperature'] ) ) { $out['temperature'] = (bool) $request['temperature']; } if ( isset( $request['effort'] ) && in_array( $request['effort'], array( 'low', 'medium', 'high' ), true ) ) { $out['effort'] = $request['effort']; } if ( isset( $request['fallbacks'] ) && 'default' === $request['fallbacks'] ) { $out['fallbacks'] = 'default'; } $extra = array(); if ( isset( $request['extra']['reasoning_effort'] ) && in_array( $request['extra']['reasoning_effort'], array( 'none', 'minimal', 'low', 'medium', 'high' ), true ) ) { $extra['reasoning_effort'] = $request['extra']['reasoning_effort']; } if ( isset( $request['extra']['thinking']['type'] ) && in_array( $request['extra']['thinking']['type'], array( 'enabled', 'disabled' ), true ) ) { $extra['thinking'] = array( 'type' => $request['extra']['thinking']['type'] ); } if ( $extra ) { $out['extra'] = $extra; } return $out; } private static function lines( $lines, $limit ) { $out = array(); foreach ( is_array( $lines ) ? $lines : array() as $line ) { $line = sanitize_text_field( is_scalar( $line ) ? (string) $line : '' ); if ( '' !== $line && count( $out ) < $limit ) { $out[] = $line; } } return $out; } private static function model_id( $value ) { return (string) preg_replace( '/[^A-Za-z0-9._:\/@+\-]/', '', substr( is_string( $value ) ? trim( $value ) : '', 0, 200 ) ); } }