store->get('provider.base_url')); if ($url === '') { $url = (string) Schema::defaults()['provider.base_url']; } $url = rtrim($url, '/'); if (str_ends_with($url, '/v1')) { return $url; } $url = (string) preg_replace('~^(.*/v1)/.*$~', '$1', $url); return str_ends_with($url, '/v1') ? $url : $url . '/v1'; } /** * @param string|null $model falls back to `models.default` when null or empty * @throws \UnexpectedValueException when an `alpaca_bot/provider` filter returns something else */ public function make(?string $model = null): ProviderInterface { $model = ($model === null || $model === '') ? (string) $this->store->get('models.default') : $model; // Both kinds go through the same filter, the extension point for swapping in another // provider: a filter that wraps or replaces the provider sees whichever kind is selected. $provider = $this->usesWpAi() ? new WpAiClientProvider($model, $this->wpAi) : $this->ollama($model); /** * Filters the provider built for a model, whichever kind Settings selected (Ollama or the * WordPress AI Client adapter): the extension point for another backend, or for wrapping * this one with logging or a retry. The filter must return a ProviderInterface; anything else * throws UnexpectedValueException, loudly, because a silent fallback would send a turn to a * backend the site did not choose. Fires per make(), so once per turn and once per model * listing. * * @since 0.5.0 * @param ProviderInterface $provider the provider as built from settings * @param string $model the model id it was built for * @param Store $store the plugin settings * @var mixed $filtered what the filter returned, checked before it is trusted */ $filtered = apply_filters('alpaca_bot/provider', $provider, $model, $this->store); if (!$filtered instanceof ProviderInterface) { throw new \UnexpectedValueException('alpaca_bot/provider must return a ' . ProviderInterface::class . ', got ' . get_debug_type($filtered)); } return $filtered; } /** * The message for the administrator when `wp-ai` is selected but make() is building Ollama, * null otherwise. The verdict lives here, next to the decision it reports, and Plugin prints * it on admin_notices; the factory itself prints nothing, since it also runs under REST and * WP-CLI. */ public function fallbackNotice(): ?string { if ($this->store->get('provider.kind') !== 'wp-ai' || $this->wpAi->available()) { return null; } return sprintf( /* translators: %s: the Ollama base URL replies are coming from */ __('Alpaca Bot is set to the WordPress AI provider, but this WordPress has no AI client (it arrived in WordPress 7.0), so replies are coming from Ollama at %s instead. Choose Ollama on the Alpaca Bot settings page to make that the setting, or update WordPress.', 'alpaca-bot'), $this->baseUrl(), ); } private function usesWpAi(): bool { return $this->store->get('provider.kind') === 'wp-ai' && $this->wpAi->available(); } private function ollama(string $model): OllamaProvider { // Symfony's `timeout` is the idle timeout between chunks, which is the one that // matters for a streamed reply; the library default would be 300s. HttpTransport, not // Symfony's HttpClient::create(), because create() picks its Curl client on a host that // has disabled curl_init, and the turn would die with an Error (HttpTransport says why). $http = HttpTransport::create(['timeout' => (int) $this->store->get('provider.timeout')]); // OllamaProvider pins 'ollama-local' as its Bearer token on every request, so a // configured key is swapped in at the wire: the provider (and the tool-schema // sanitising in its formatTools()) stays, and the key is never dropped. That // sanitising is this kind's only: before Ollama sees a toolkit schema it flattens the // combinators, demotes the validation keywords (`minimum`..`maxItems`, `pattern`, // `format`, `const`, `default`) into the description and strips them, and strips // `additionalProperties`, `uniqueItems`, `$ref`, `$defs` and `patternProperties` with // no hint; `enum` is untouched (the full lists are on WpAiClientProvider's docblock). // WpAiClientProvider sends the schemas to core raw, so a toolkit schema that works // here may be rejected on `wp-ai` by the core provider (WpAiClientProvider says why // the sanitising is not copied there). // The row carries MASK for a kept key; the key itself is in its own option. $apiKey = ProviderKey::resolve($this->store->get('provider.api_key')); if ($apiKey !== '') { $http = new BearerHttpClient($http, $apiKey); } return new OllamaProvider( model: $model, baseUrl: $this->baseUrl(), httpClient: $http, numCtx: (int) $this->store->get('models.num_ctx'), ); } }