| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\AI; |
| 4 |
|
| 5 |
use Better_Payment\Lite\AI\Contracts\AIProviderInterface; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Registry of available AI providers. |
| 13 |
* |
| 14 |
* Mirrors {@see \Better_Payment\Lite\Campaign\Elements\ElementRegistry}: a static |
| 15 |
* store PHP populates, third parties extend via filter, and the rest of the AI |
| 16 |
* layer reads. Definitions are lazy — a provider is only instantiated (with its |
| 17 |
* runtime config) when {@see self::make()} is called. |
| 18 |
* |
| 19 |
* Register a custom provider: |
| 20 |
* add_filter( 'better_payment/ai/providers', function ( $providers ) { |
| 21 |
* $providers['myllm'] = [ |
| 22 |
* 'label' => 'My LLM', |
| 23 |
* 'class' => My\Provider::class, // implements AIProviderInterface |
| 24 |
* 'models' => [ 'my-model-1' ], |
| 25 |
* 'supports_images' => false, |
| 26 |
* ]; |
| 27 |
* return $providers; |
| 28 |
* } ); |
| 29 |
*/ |
| 30 |
class ProviderRegistry { |
| 31 |
|
| 32 |
/** |
| 33 |
* @var array<string, array> |
| 34 |
*/ |
| 35 |
private static array $providers = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register a provider definition. |
| 39 |
* |
| 40 |
* @param string $id Stable provider id. |
| 41 |
* @param array $def { @type string $label; @type string $class; @type array $models; @type bool $supports_images; } |
| 42 |
*/ |
| 43 |
public static function register( string $id, array $def ): void { |
| 44 |
self::$providers[ $id ] = array_merge( [ |
| 45 |
'label' => $id, |
| 46 |
'class' => '', |
| 47 |
'models' => [], |
| 48 |
'image_models' => [], |
| 49 |
'supports_images' => false, |
| 50 |
], $def ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* All provider definitions, after the extension filter. |
| 55 |
* |
| 56 |
* @return array<string, array> |
| 57 |
*/ |
| 58 |
public static function get_all(): array { |
| 59 |
/** |
| 60 |
* Filter registered AI providers. |
| 61 |
* |
| 62 |
* @param array<string, array> $providers |
| 63 |
*/ |
| 64 |
return apply_filters( 'better_payment/ai/providers', self::$providers ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* A single provider definition, or null. |
| 69 |
* |
| 70 |
* @return array|null |
| 71 |
*/ |
| 72 |
public static function get( string $id ) { |
| 73 |
$all = self::get_all(); |
| 74 |
return $all[ $id ] ?? null; |
| 75 |
} |
| 76 |
|
| 77 |
public static function exists( string $id ): bool { |
| 78 |
return null !== self::get( $id ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Instantiate a provider with runtime config. |
| 83 |
* |
| 84 |
* @param string $id |
| 85 |
* @param array $config Passed to the provider constructor (api_key, model, ...). |
| 86 |
* @return AIProviderInterface|null Null when the id is unknown or the class is invalid. |
| 87 |
*/ |
| 88 |
public static function make( string $id, array $config = [] ) { |
| 89 |
$def = self::get( $id ); |
| 90 |
if ( null === $def || empty( $def['class'] ) || ! class_exists( $def['class'] ) ) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
$instance = new $def['class']( $config ); |
| 94 |
return $instance instanceof AIProviderInterface ? $instance : null; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Public-safe descriptors for the UI (no secrets), one per provider. |
| 99 |
* |
| 100 |
* @return array<int, array{id: string, label: string, models: array, supports_images: bool}> |
| 101 |
*/ |
| 102 |
public static function descriptors(): array { |
| 103 |
$out = []; |
| 104 |
foreach ( self::get_all() as $id => $def ) { |
| 105 |
$out[] = [ |
| 106 |
'id' => $id, |
| 107 |
'label' => $def['label'], |
| 108 |
'models' => array_values( $def['models'] ), |
| 109 |
'image_models' => array_values( $def['image_models'] ?? [] ), |
| 110 |
'supports_images' => (bool) $def['supports_images'], |
| 111 |
]; |
| 112 |
} |
| 113 |
return $out; |
| 114 |
} |
| 115 |
} |
| 116 |
|