| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\AI; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Admin\DB; |
| 6 |
use Better_Payment\Lite\AI\Providers\ClaudeProvider; |
| 7 |
use Better_Payment\Lite\AI\Providers\GeminiProvider; |
| 8 |
use Better_Payment\Lite\AI\Providers\OpenAIProvider; |
| 9 |
use Better_Payment\Lite\AI\Providers\OpenRouterProvider; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Entry point for the AI module. |
| 17 |
* |
| 18 |
* Registers the built-in providers and resolves the currently-active provider |
| 19 |
* (with its runtime config) from the plugin settings option. Wired from |
| 20 |
* Better_Payment::init_ai_module(). |
| 21 |
* |
| 22 |
* Settings live in the single `better_payment_settings` option (see |
| 23 |
* Admin/DB.php defaults) so they round-trip through the existing settings REST. |
| 24 |
* |
| 25 |
* @see ProviderRegistry |
| 26 |
* @see \Better_Payment\Lite\API\AIAPI |
| 27 |
*/ |
| 28 |
class AIManager { |
| 29 |
|
| 30 |
/** |
| 31 |
* Boot the module: register providers. |
| 32 |
*/ |
| 33 |
public function init(): void { |
| 34 |
self::register_default_providers(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the four built-in providers. |
| 39 |
*/ |
| 40 |
public static function register_default_providers(): void { |
| 41 |
ProviderRegistry::register( 'openai', [ |
| 42 |
'label' => 'OpenAI', |
| 43 |
'class' => OpenAIProvider::class, |
| 44 |
'models' => [ 'gpt-4o', 'gpt-4o-mini', 'gpt-4.1', 'gpt-4.1-mini' ], |
| 45 |
'image_models' => [ 'gpt-image-1' ], |
| 46 |
'supports_images' => true, |
| 47 |
] ); |
| 48 |
ProviderRegistry::register( 'claude', [ |
| 49 |
'label' => 'Claude (Anthropic)', |
| 50 |
'class' => ClaudeProvider::class, |
| 51 |
'models' => [ 'claude-sonnet-5', 'claude-opus-4-8', 'claude-haiku-4-5-20251001' ], |
| 52 |
'image_models' => [], |
| 53 |
'supports_images' => false, |
| 54 |
] ); |
| 55 |
ProviderRegistry::register( 'gemini', [ |
| 56 |
'label' => 'Google Gemini', |
| 57 |
'class' => GeminiProvider::class, |
| 58 |
'models' => [ 'gemini-2.5-flash', 'gemini-2.5-pro', 'gemini-2.0-flash' ], |
| 59 |
'image_models' => [ 'imagen-3.0-generate-002' ], |
| 60 |
'supports_images' => true, |
| 61 |
] ); |
| 62 |
ProviderRegistry::register( 'openrouter', [ |
| 63 |
'label' => 'OpenRouter', |
| 64 |
'class' => OpenRouterProvider::class, |
| 65 |
'models' => [ 'openai/gpt-4o', 'anthropic/claude-sonnet-5', 'google/gemini-2.5-pro' ], |
| 66 |
'image_models' => [], |
| 67 |
'supports_images' => false, |
| 68 |
] ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Whether the AI Assistant is enabled in settings. |
| 73 |
*/ |
| 74 |
public static function is_enabled(): bool { |
| 75 |
return 'yes' === DB::get_settings( 'better_payment_settings_ai_enabled' ) |
| 76 |
|| '1' === (string) DB::get_settings( 'better_payment_settings_ai_enabled' ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* The active provider id from settings (default openai). |
| 81 |
*/ |
| 82 |
public static function active_provider_id(): string { |
| 83 |
$id = (string) DB::get_settings( 'better_payment_settings_ai_provider' ); |
| 84 |
return '' !== $id ? $id : 'openai'; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Build the runtime config for a provider id from settings. |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public static function config_for( string $provider_id ): array { |
| 93 |
$temperature = DB::get_settings( 'better_payment_settings_ai_temperature' ); |
| 94 |
$max_tokens = DB::get_settings( 'better_payment_settings_ai_max_tokens' ); |
| 95 |
|
| 96 |
return [ |
| 97 |
'api_key' => (string) DB::get_settings( 'better_payment_settings_ai_api_key_' . $provider_id ), |
| 98 |
'model' => (string) DB::get_settings( 'better_payment_settings_ai_model' ), |
| 99 |
'image_model' => (string) DB::get_settings( 'better_payment_settings_ai_image_model' ), |
| 100 |
'temperature' => is_numeric( $temperature ) ? (float) $temperature : 0.7, |
| 101 |
'max_tokens' => is_numeric( $max_tokens ) ? (int) $max_tokens : 4096, |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Instantiate the active provider, or null when unknown/unconfigured. |
| 107 |
* |
| 108 |
* @return \Better_Payment\Lite\AI\Contracts\AIProviderInterface|null |
| 109 |
*/ |
| 110 |
public static function active_provider() { |
| 111 |
$id = self::active_provider_id(); |
| 112 |
return ProviderRegistry::make( $id, self::config_for( $id ) ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* The configured system prompt, or a sensible default. |
| 117 |
*/ |
| 118 |
public static function system_prompt(): string { |
| 119 |
$custom = trim( (string) DB::get_settings( 'better_payment_settings_ai_system_prompt' ) ); |
| 120 |
return '' !== $custom ? $custom : ''; |
| 121 |
} |
| 122 |
} |
| 123 |
|