'OpenAI', 'gemini' => 'Google Gemini', 'openrouter' => 'OpenRouter', ); } /** * Providers that can generate embeddings. * * All three can. OpenRouter exposes the same OpenAI-compatible `/embeddings` endpoint and * serves its own embedding catalogue — 33 models at the time of writing, including Google's * and OpenAI's, some of them free — so refusing it here would be an artificial limit. * * The one difference is naming: OpenAI and Gemini use flat model names, while OpenRouter * namespaces its catalogue ("openai/text-embedding-3-small"). A saved flat name therefore * has to be qualified before it is sent, which is what MYBOT_Settings::embedding_model() * does. Without that step the request fails with an unknown-model error while the settings * screen still looks perfectly correct — the exact trap this list used to avoid by leaving * OpenRouter out. * * This list is the single source of truth: the settings allow-list and the dropdown in * the admin form are both built from it, so a provider can never be offered but rejected * on save. * * @return array */ public static function embedding_options() { return array( 'openai' => 'OpenAI', 'gemini' => 'Google Gemini', 'openrouter' => 'OpenRouter', ); } /** * Normalises a model-list filter. * * Shared by every provider so an unknown value can never reach the filtering logic and * quietly turn into a different list. * * @param string $filter Requested filter. * @return string One of 'all', 'free' or 'paid'. */ public static function clean_model_filter( $filter ) { return in_array( $filter, array( 'all', 'free', 'paid' ), true ) ? $filter : 'all'; } /** * Asks a provider what it can read — the ONLY safe way to do it. * * Never call `$provider->supports_vision()` directly. The method is optional on purpose (see the * note in the interface): a plugin folder with an old provider file next to a new core must not * fatal, and PHP fatals on an unimplemented *declared* method when the class loads. A **mixed * folder** is what a File Manager upload produces when one file fails to copy, and on 14/10/2026 it * took a whole site down with `Class MYBOT_LLM_OpenAI contains 2 abstract methods…` * (`docs/08` muc 3.65; same family as 6.50). * * So a missing method answers **false** — *"the feature is unavailable"*, never *"the site is * down"*. False is also the safe answer: no picture is sent to a model that cannot be asked. * * @param mixed $provider Provider object, or a WP_Error from the factory. * @param string $capability 'vision' or 'audio'. * @param string $model Model ID; empty means "not stated", which answers false. * @return bool */ public static function can( $provider, $capability, $model = '' ) { $method = ( 'audio' === $capability ) ? 'supports_audio' : 'supports_vision'; if ( ! is_object( $provider ) || ! method_exists( $provider, $method ) ) { return false; } return (bool) $provider->$method( $model ); } /** * Whether a model name matches one of a provider's capability prefixes. * * One matcher for all three providers: the filter is applied identically, so a capability cannot * quietly mean something slightly different per provider. Callers pass the model they mean — see * the fail-closed note on `MYBOT_LLM_Provider::supports_vision()`. * * @param string $model Model ID. * @param array $prefixes Lower-case prefixes this provider believes support the capability. * @param string $provider Provider ID, passed to the filter. * @param string $capability 'vision' or 'audio'. * @return bool */ public static function model_matches( $model, array $prefixes, $provider, $capability ) { $model = strtolower( trim( (string) $model ) ); $found = false; if ( '' !== $model ) { foreach ( $prefixes as $prefix ) { $prefix = strtolower( (string) $prefix ); if ( '' !== $prefix && 0 === strpos( $model, $prefix ) ) { $found = true; break; } } } /** * Corrects a provider's capability list without editing core. * * The lists in the provider classes are curated by hand and can be wrong in both directions — * a new model that does accept images, or one this plugin wrongly believes does. A site (or an * add-on) can answer for its own catalogue here instead of waiting for a release. * * @param bool $found Whether the model matched the provider's own list. * @param string $capability 'vision' or 'audio'. * @param string $provider Provider ID. * @param string $model Lower-cased model ID, empty when it was not stated. */ return (bool) apply_filters( 'mybot_model_capability', $found, $capability, $provider, $model ); } /** * Cache key for one provider's model list. * * The provider is part of the key: three providers now cache their catalogue under the * same transient prefix, and without it one provider would be served another one's list. * * @param string $provider Provider ID. * @param string $filter One of 'all', 'free' or 'paid'. * @return string */ public static function models_cache_key( $provider, $filter ) { return MYBOT_MODELS_TRANSIENT . sanitize_key( $provider ) . '_' . self::clean_model_filter( $filter ); } /** * Creates the provider used for chat. * * @param MYBOT_Settings $settings Settings. * @return MYBOT_LLM_Provider|WP_Error */ public static function chat_provider( $settings ) { $provider = (string) $settings->get( 'llm_provider', 'openai' ); return self::make( $provider, $settings->provider_key( $provider ) ); } /** * Creates the provider used for embeddings. * * @param MYBOT_Settings $settings Settings. * @return MYBOT_LLM_Provider|WP_Error */ public static function embedding_provider( $settings ) { return self::make( (string) $settings->get( 'embedding_provider', 'openai' ), $settings->embedding_key() ); } /** * Creates a provider from its ID. * * @param string $provider Provider ID. * @param string $api_key API key. * @return MYBOT_LLM_Provider|WP_Error */ public static function make( $provider, $api_key ) { $provider = sanitize_key( $provider ); if ( '' === (string) $api_key ) { return new WP_Error( 'mybot_missing_key', sprintf( /* translators: %s: provider name */ __( 'No API key configured for %s.', 'aihouse' ), isset( self::options()[ $provider ] ) ? self::options()[ $provider ] : $provider ) ); } switch ( $provider ) { case 'openai': return new MYBOT_LLM_OpenAI( $api_key ); case 'gemini': return new MYBOT_LLM_Gemini( $api_key ); case 'openrouter': return new MYBOT_LLM_OpenRouter( $api_key ); } return new WP_Error( 'mybot_unknown_provider', /* translators: %s: provider slug */ sprintf( __( 'Unsupported provider: %s', 'aihouse' ), $provider ) ); } /** * API key read from a constant in wp-config.php, when one is defined. * * @param string $provider Provider ID. * @return string */ public static function constant_key( $provider ) { $map = array( 'openai' => array( 'MYBOT_OPENAI_KEY', 'MYBOT_LLM_KEY' ), 'gemini' => array( 'MYBOT_GEMINI_KEY', 'MYBOT_LLM_KEY' ), 'openrouter' => array( 'MYBOT_OPENROUTER_KEY', 'MYBOT_LLM_KEY' ), ); $provider = sanitize_key( $provider ); if ( ! isset( $map[ $provider ] ) ) { return ''; } foreach ( $map[ $provider ] as $constant ) { if ( defined( $constant ) && '' !== (string) constant( $constant ) ) { return (string) constant( $constant ); } } return ''; } }