[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/ApiBasedImplementation/ -> AbstractApiBasedModelMetadataDirectory.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\ApiBasedImplementation;
   5  
   6  use WordPress\AiClient\AiClient;
   7  use WordPress\AiClient\Common\Contracts\CachesDataInterface;
   8  use WordPress\AiClient\Common\Exception\InvalidArgumentException;
   9  use WordPress\AiClient\Common\Traits\WithDataCachingTrait;
  10  use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
  11  use WordPress\AiClient\Providers\Http\Contracts\WithHttpTransporterInterface;
  12  use WordPress\AiClient\Providers\Http\Contracts\WithRequestAuthenticationInterface;
  13  use WordPress\AiClient\Providers\Http\Traits\WithHttpTransporterTrait;
  14  use WordPress\AiClient\Providers\Http\Traits\WithRequestAuthenticationTrait;
  15  use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
  16  /**
  17   * Base class for an API-based model metadata directory for a provider.
  18   *
  19   * @since 0.1.0
  20   */
  21  abstract class AbstractApiBasedModelMetadataDirectory implements ModelMetadataDirectoryInterface, WithHttpTransporterInterface, WithRequestAuthenticationInterface, CachesDataInterface
  22  {
  23      use WithHttpTransporterTrait;
  24      use WithRequestAuthenticationTrait;
  25      use WithDataCachingTrait;
  26      /**
  27       * The cache key suffix for the models list.
  28       *
  29       * @since 0.4.0
  30       *
  31       * @var string
  32       */
  33      private const MODELS_CACHE_KEY = 'models';
  34      /**
  35       * {@inheritDoc}
  36       *
  37       * @since 0.1.0
  38       */
  39      final public function listModelMetadata(): array
  40      {
  41          $modelsMetadata = $this->getModelMetadataMap();
  42          return array_values($modelsMetadata);
  43      }
  44      /**
  45       * {@inheritDoc}
  46       *
  47       * @since 0.1.0
  48       */
  49      final public function hasModelMetadata(string $modelId): bool
  50      {
  51          $modelsMetadata = $this->getModelMetadataMap();
  52          return isset($modelsMetadata[$modelId]);
  53      }
  54      /**
  55       * {@inheritDoc}
  56       *
  57       * @since 0.1.0
  58       */
  59      final public function getModelMetadata(string $modelId): ModelMetadata
  60      {
  61          $modelsMetadata = $this->getModelMetadataMap();
  62          if (!isset($modelsMetadata[$modelId])) {
  63              throw new InvalidArgumentException(sprintf('No model with ID %s was found in the provider', $modelId));
  64          }
  65          return $modelsMetadata[$modelId];
  66      }
  67      /**
  68       * Returns the map of model ID to model metadata for all models from the provider.
  69       *
  70       * @since 0.1.0
  71       *
  72       * @return array<string, ModelMetadata> Map of model ID to model metadata.
  73       */
  74      private function getModelMetadataMap(): array
  75      {
  76          /** @var array<string, ModelMetadata> */
  77          return $this->cached(self::MODELS_CACHE_KEY, fn() => $this->sendListModelsRequest(), 86400);
  78      }
  79      /**
  80       * {@inheritDoc}
  81       *
  82       * @since 0.4.0
  83       */
  84      protected function getCachedKeys(): array
  85      {
  86          return [self::MODELS_CACHE_KEY];
  87      }
  88      /**
  89       * {@inheritDoc}
  90       *
  91       * @since 0.4.0
  92       */
  93      protected function getBaseCacheKey(): string
  94      {
  95          return 'ai_client_' . AiClient::VERSION . '_' . md5(static::class);
  96      }
  97      /**
  98       * Sends the API request to list models from the provider and returns the map of model ID to model metadata.
  99       *
 100       * @since 0.1.0
 101       *
 102       * @return array<string, ModelMetadata> Map of model ID to model metadata.
 103       */
 104      abstract protected function sendListModelsRequest(): array;
 105  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref