| 1 |
<?php |
| 2 |
/** |
| 3 |
* DelegatedProvider — adapts a foreign provider object to our interface. |
| 4 |
* |
| 5 |
* Used for the Mail Mint bridge: when the site opts into Mail Mint's AI |
| 6 |
* connection, Mail Mint hands back its own adapter and we forward calls to it. |
| 7 |
* The API key never crosses the plugin boundary — only the call does. |
| 8 |
* |
| 9 |
* @package WPFunnels\AI |
| 10 |
* @since 3.13.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPFunnels\AI\Providers; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class DelegatedProvider |
| 19 |
*/ |
| 20 |
class DelegatedProvider implements ProviderInterface { |
| 21 |
|
| 22 |
/** |
| 23 |
* Foreign provider adapter. Must expose chat(), validateKey(), slug(). |
| 24 |
* |
| 25 |
* @var object |
| 26 |
*/ |
| 27 |
private $delegate; |
| 28 |
|
| 29 |
/** |
| 30 |
* Where the delegate came from, for diagnostics. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $source; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @param object $delegate Foreign provider adapter. |
| 40 |
* @param string $source Source label, e.g. 'mail-mint'. |
| 41 |
*/ |
| 42 |
public function __construct( $delegate, $source = 'mail-mint' ) { |
| 43 |
$this->delegate = $delegate; |
| 44 |
$this->source = (string) $source; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Whether an object can stand in as a provider. |
| 49 |
* |
| 50 |
* @param mixed $candidate Object to check. |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
public static function isCompatible( $candidate ) { |
| 54 |
return is_object( $candidate ) |
| 55 |
&& method_exists( $candidate, 'chat' ) |
| 56 |
&& method_exists( $candidate, 'validateKey' ) |
| 57 |
&& method_exists( $candidate, 'slug' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Source label of the delegate. |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public function source() { |
| 66 |
return $this->source; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Run one model turn through the delegate. |
| 71 |
* |
| 72 |
* @param string $system System prompt. |
| 73 |
* @param array $messages Normalized message list. |
| 74 |
* @param array $tools Normalized tool definitions. |
| 75 |
* @param bool $force_tool_use Force a tool call instead of a text-only reply. |
| 76 |
* @return array|\WP_Error |
| 77 |
*/ |
| 78 |
public function chat( $system, $messages, $tools, $force_tool_use = false ) { |
| 79 |
try { |
| 80 |
return $this->delegate->chat( $system, $messages, $tools, $force_tool_use ); |
| 81 |
} catch ( \Throwable $e ) { |
| 82 |
return new \WP_Error( |
| 83 |
'ai_delegate_error', |
| 84 |
sprintf( |
| 85 |
/* translators: %1$s: source plugin, %2$s: error message. */ |
| 86 |
__( 'The AI connection borrowed from %1$s failed: %2$s', 'wpfnl' ), |
| 87 |
$this->source, |
| 88 |
$e->getMessage() |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Validate the delegate's credentials. |
| 96 |
* |
| 97 |
* @return true|\WP_Error |
| 98 |
*/ |
| 99 |
public function validateKey() { |
| 100 |
try { |
| 101 |
return $this->delegate->validateKey(); |
| 102 |
} catch ( \Throwable $e ) { |
| 103 |
return new \WP_Error( 'ai_delegate_error', $e->getMessage() ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Provider slug reported by the delegate. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public function slug() { |
| 113 |
try { |
| 114 |
return (string) $this->delegate->slug(); |
| 115 |
} catch ( \Throwable $e ) { |
| 116 |
return 'delegated'; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|