getKey( $aipt_provider ); if ( ! is_string( $api_key ) || '' === $api_key ) { throw ProviderCallException::keyMissing( $aipt_provider, $resolver->getSourceLabel( $aipt_provider ) ); } // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped if ( $aipt_provider === 'gemini' ) { $client = new GeminiClient( $api_key ); } elseif ( $aipt_provider === 'openrouter' ) { $client = new OpenRouterClient( $api_key ); } elseif ( $aipt_provider === 'claude' ) { $client = new ClaudeClient( $api_key, (string) get_option( 'aipt_claude_workspace_id', '' ) ); } else { $client = new OpenAIClient( $api_key ); } return new ProviderCallGuard( $client, $aipt_provider, ProviderCircuit::scope( $aipt_provider, $api_key ) ); } /** * Get default model for provider * * @param string $provider Provider name * @return string Default model identifier */ public function getDefaultModel( string $provider ): string { return ModelCatalog::get_default_model( $provider ); } /** * Normalize model identifier for provider * * Validates model against provider's supported models and returns normalized identifier. * If model is invalid, returns the fallback. The fallback is persisted to * the global model option only when the input is the stored provider and * model pair; a per-run override must never overwrite the saved choice. * * @param string $provider Provider name * @param string|null $model Model identifier to normalize * @return string Normalized model identifier */ public function normalizeModel( string $provider, ?string $model ): string { $normalized = ModelCatalog::normalize_model( $provider, $model ); if ( $model !== $normalized && $this->isStoredModelSelection( $provider, $model ) ) { update_option( 'aipt_model', $normalized ); } return $normalized; } private function isStoredModelSelection( string $provider, ?string $model ): bool { return get_option( 'aipt_api_provider', 'openai' ) === $provider && get_option( 'aipt_model', '' ) === $model; } }