| 1 |
<?php |
| 2 |
/** |
| 3 |
* Logging integration with the AI Client SDK. |
| 4 |
* |
| 5 |
* @package WordPress\AI\Logging |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Logging; |
| 11 |
|
| 12 |
use WordPress\AiClient\AiClient; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Initializes logging integration with the AI Client SDK. |
| 18 |
* |
| 19 |
* This class handles wrapping the SDK's HTTP transporter with a logging |
| 20 |
* decorator using the public setHttpTransporter() API. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Logging_Integration { |
| 25 |
|
| 26 |
/** |
| 27 |
* Shared log manager instance. |
| 28 |
* |
| 29 |
* @var \WordPress\AI\Logging\AI_Request_Log_Manager|null |
| 30 |
*/ |
| 31 |
private static ?AI_Request_Log_Manager $log_manager = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Whether logging has been initialized. |
| 35 |
* |
| 36 |
* @var bool |
| 37 |
*/ |
| 38 |
private static bool $initialized = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Initialize logging integration with the AI Client SDK. |
| 42 |
* |
| 43 |
* This method wraps the SDK's HTTP transporter with a logging decorator. |
| 44 |
* It should be called after AI_Client::init() to ensure the registry exists. |
| 45 |
* |
| 46 |
* @param \WordPress\AI\Logging\AI_Request_Log_Manager $log_manager The log manager instance. |
| 47 |
*/ |
| 48 |
public static function init( AI_Request_Log_Manager $log_manager ): void { |
| 49 |
if ( self::$initialized ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
self::$log_manager = $log_manager; |
| 54 |
|
| 55 |
// Check if the AiClient SDK is available. |
| 56 |
if ( ! class_exists( AiClient::class ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// Hook into the first request to wrap the transporter. |
| 61 |
// This ensures the registry is fully initialized before we wrap it. |
| 62 |
add_action( 'wp_loaded', array( self::class, 'wrap_transporter' ), 1 ); |
| 63 |
|
| 64 |
// Also hook early in admin for immediate availability. |
| 65 |
add_action( 'admin_init', array( self::class, 'wrap_transporter' ), 1 ); |
| 66 |
|
| 67 |
self::$initialized = true; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Gets the shared log manager instance. |
| 72 |
* |
| 73 |
* Returns null when the AI Request Logging experiment is disabled, since |
| 74 |
* nothing has initialized the manager in that case. |
| 75 |
* |
| 76 |
* @since 1.3.0 |
| 77 |
* |
| 78 |
* @return \WordPress\AI\Logging\AI_Request_Log_Manager|null The log manager, or null when logging is inactive. |
| 79 |
*/ |
| 80 |
public static function get_log_manager(): ?AI_Request_Log_Manager { |
| 81 |
return self::$log_manager; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Wraps the SDK's HTTP transporter with logging. |
| 86 |
* |
| 87 |
* Uses the public setHttpTransporter() API to replace the transporter |
| 88 |
* with a logging decorator. |
| 89 |
*/ |
| 90 |
public static function wrap_transporter(): void { |
| 91 |
static $wrapped = false; |
| 92 |
|
| 93 |
if ( $wrapped || ! self::$log_manager ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
try { |
| 98 |
$registry = AiClient::defaultRegistry(); |
| 99 |
|
| 100 |
// Get the current transporter. |
| 101 |
$current_transporter = $registry->getHttpTransporter(); |
| 102 |
|
| 103 |
// Don't wrap if already wrapped. |
| 104 |
if ( $current_transporter instanceof Logging_Http_Transporter ) { |
| 105 |
$wrapped = true; |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
// Create a logging wrapper around the existing transporter. |
| 110 |
$logging_transporter = new Logging_Http_Transporter( |
| 111 |
$current_transporter, |
| 112 |
self::$log_manager |
| 113 |
); |
| 114 |
|
| 115 |
// Replace the transporter with the logging version. |
| 116 |
$registry->setHttpTransporter( $logging_transporter ); |
| 117 |
|
| 118 |
$wrapped = true; |
| 119 |
} catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 120 |
// Silently fail - logging is optional and shouldn't break the site. |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|