default_context = $default_context; } /** * Create AI client instance * * @param string|null $provider Provider name (openai|gemini|openrouter|claude). If null, uses configured provider. * @return AIClientInterface AI client instance, wrapped in ProviderCallGuard. * @throws ProviderCallException For an unsupported provider, AI turned off on the site, or no saved key. */ public function create( ?string $provider = null, ?IdempotencyKeyContext $context = null ): AIClientInterface { $aipt_provider = strtolower( trim( (string) ( $provider ?? '' ) ) ); if ( '' === $aipt_provider ) { $aipt_provider = strtolower( trim( (string) get_option( 'aipt_api_provider', 'openai' ) ) ); } if ( ! in_array( $aipt_provider, AIClientFactory::PROVIDERS, true ) ) { throw ProviderCallException::unsupportedProvider( $aipt_provider ); } $ai_gate = \AIPT\Core\Services\AiSupportGate::guard(); if ( $ai_gate instanceof \WP_Error ) { throw ProviderCallException::aiDisabled( $ai_gate, $aipt_provider ); } $context = $context ?? $this->default_context; $resolver = CredentialResolver::instance(); $api_key = $resolver->getKey( $aipt_provider ); if ( ! is_string( $api_key ) || '' === $api_key ) { throw ProviderCallException::keyMissing( $aipt_provider, $resolver->getSourceLabel( $aipt_provider ) ); } 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, $context, (string) get_option( 'aipt_claude_workspace_id', '' ) ); } else { $client = new OpenAIClient( $api_key, $context ); } 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; } }