Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Providers/Contracts/ModelMetadataDirectoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;

/**
* Interface for accessing model metadata within a provider.
*
* Provides methods to list, check, and retrieve model metadata
* for all models supported by a provider.
*
* @since n.e.x.t
*/
interface ModelMetadataDirectoryInterface
{
/**
* Lists all available model metadata.
*
* @since n.e.x.t
*
* @return list<ModelMetadata> Array of model metadata.
*/
public function listModelMetadata(): array;

/**
* Checks if metadata exists for a specific model.
*
* @since n.e.x.t
*
* @param string $modelId Model identifier.
* @return bool True if metadata exists, false otherwise.
*/
public function hasModelMetadata(string $modelId): bool;

/**
* Gets metadata for a specific model.
*
* @since n.e.x.t
*
* @param string $modelId Model identifier.
* @return ModelMetadata Model metadata.
* @throws InvalidArgumentException If model metadata not found.
*/
public function getModelMetadata(string $modelId): ModelMetadata;
}
25 changes: 25 additions & 0 deletions src/Providers/Contracts/ProviderAvailabilityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Contracts;

/**
* Interface for checking provider availability.
*
* Determines whether a provider is configured and available
* for use based on API keys, credentials, or other requirements.
*
* @since n.e.x.t
*/
interface ProviderAvailabilityInterface
{
/**
* Checks if the provider is configured.
*
* @since n.e.x.t
*
* @return bool True if the provider is configured and available, false otherwise.
*/
public function isConfigured(): bool;
}
60 changes: 60 additions & 0 deletions src/Providers/Contracts/ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Providers\DTO\ProviderMetadata;
use WordPress\AiClient\Providers\Models\Contracts\ModelInterface;
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;

/**
* Interface for AI providers.
*
* Providers represent AI services (Google, OpenAI, Anthropic, etc.)
* and provide access to models, metadata, and availability information.
*
* @since n.e.x.t
*/
interface ProviderInterface
{
/**
* Gets provider metadata.
*
* @since n.e.x.t
*
* @return ProviderMetadata Provider metadata.
*/
public static function metadata(): ProviderMetadata;

/**
* Creates a model instance.
*
* @since n.e.x.t
*
* @param string $modelId Model identifier.
* @param ?ModelConfig $modelConfig Model configuration.
* @return ModelInterface Model instance.
* @throws InvalidArgumentException If model not found or configuration invalid.
*/
public static function model(string $modelId, ?ModelConfig $modelConfig = null): ModelInterface;

/**
* Gets provider availability checker.
*
* @since n.e.x.t
*
* @return ProviderAvailabilityInterface Provider availability checker.
*/
public static function availability(): ProviderAvailabilityInterface;

/**
* Gets model metadata directory.
*
* @since n.e.x.t
*
* @return ModelMetadataDirectoryInterface Model metadata directory.
*/
public static function modelMetadataDirectory(): ModelMetadataDirectoryInterface;
}
47 changes: 47 additions & 0 deletions src/Providers/Models/Contracts/ModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Models\Contracts;

use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;

/**
* Interface for AI models.
*
* Models represent specific AI models from providers and define
* their capabilities, configuration, and execution methods.
*
* @since n.e.x.t
*/
interface ModelInterface
{
/**
* Gets model metadata.
*
* @since n.e.x.t
*
* @return ModelMetadata Model metadata.
*/
public function metadata(): ModelMetadata;

/**
* Sets model configuration.
*
* @since n.e.x.t
*
* @param ModelConfig $config Model configuration.
* @return void
*/
public function setConfig(ModelConfig $config): void;

/**
* Gets model configuration.
*
* @since n.e.x.t
*
* @return ModelConfig Current model configuration.
*/
public function getConfig(): ModelConfig;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Models\Contracts;

use InvalidArgumentException;
use WordPress\AiClient\Operations\DTO\GenerativeAiOperation;

/**
* Interface for models that support generative AI operations.
*
* Provides methods to retrieve and manage long-running generative AI
* operations such as text, image, or speech generation.
*
* @since n.e.x.t
*/
interface WithGenerativeAiOperationsInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to question whether this approach makes sense: Looking up an existing operation is technically subject to the provider support, not model-specific, so maybe this shouldn't be an interface for models to implement?

Maybe we could instead have a ProviderOperationsHandlerInterface (something like that) with the relevant instance methods (like getOperation), and another ProviderWithOperationsInterface which the actual provider class would implement, with a single static method operationsHandler() which would return the provider-specific instance. That would align well with how the rest works, but keep it optional, as not every provider would need this.

LMKWYT. If this needs more thinking and more discussion, we could leave it out of this PR entirely, as we'll only implement long-running operations later anyway. The key features for the MVP and the big three providers don't need it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know more than I do in this case, but are we confident that looking up operations will always be on a per-provider and not per-model basis? I'd hate to make assumptions that back us into a corner where a provider exists with varying model operational abilities.

I do agree, though, that we shouldn't spend too much time on this right now.

Copy link
Member

@felixarntz felixarntz Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All existing long-running operation implementations in APIs I've seen are model independent - i.e. tied to the entire provider, not specific models. It also makes sense conceptually because the model is only relevant once you trigger/start the operation. At that point you pass all the same information you'd pass to a regular request (model ID, model config etc.), but after that you just get an operation ID back that you can look up provider-wide - no need to remember which model was used at that point.

From that perspective, I'd suggest we go with what I mentioned in the previous comment:

  • ProviderOperationsHandlerInterface with (non-static) method getOperation (just like the current interface)
  • ProviderWithOperationsHandlerInterface with static method operationsHandler(): ProviderOperationsHandlerInterface

Would you be okay replacing this current interface with that? This way we have a starting point in place for operations, which IMO makes sense for a comprehensive picture (similar to #35 (comment)), even if we'll only use it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! Appreciate the insight! I resolved this in 5b46445

{
/**
* Gets a generative AI operation by ID.
*
* @since n.e.x.t
*
* @param string $operationId Operation identifier.
* @return GenerativeAiOperation The generative AI operation.
* @throws InvalidArgumentException If operation not found.
*/
public function getOperation(string $operationId): GenerativeAiOperation;
}
56 changes: 56 additions & 0 deletions src/Providers/Models/DTO/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class ModelMetadata extends AbstractDataValueObject
*/
protected array $supportedOptions;

/**
* @var array<string, true> Map of supported capabilities for O(1) lookups.
*/
private array $capabilitiesMap = [];

/**
* @var array<string, SupportedOption> Map of supported options by name for O(1) lookups.
*/
private array $optionsMap = [];

/**
* Constructor.
*
Expand All @@ -80,6 +90,16 @@ public function __construct(string $id, string $name, array $supportedCapabiliti
$this->name = $name;
$this->supportedCapabilities = $supportedCapabilities;
$this->supportedOptions = $supportedOptions;

// Build capability map for efficient lookups
foreach ($supportedCapabilities as $capability) {
$this->capabilitiesMap[$capability->value] = true;
}

// Build options map for efficient lookups
foreach ($supportedOptions as $option) {
$this->optionsMap[$option->getName()] = $option;
}
}

/**
Expand Down Expand Up @@ -189,6 +209,42 @@ public function toArray(): array
];
}

/**
* Checks whether this model meets the specified requirements.
*
* @since n.e.x.t
*
* @param ModelRequirements $requirements The requirements to check against.
* @return bool True if the model meets all requirements, false otherwise.
*/
public function meetsRequirements(ModelRequirements $requirements): bool
{
// Check if all required capabilities are supported using map lookup
foreach ($requirements->getRequiredCapabilities() as $requiredCapability) {
if (!isset($this->capabilitiesMap[$requiredCapability->value])) {
return false;
}
}

// Check if all required options are supported with the specified values
foreach ($requirements->getRequiredOptions() as $requiredOption) {
// Use map lookup instead of linear search
if (!isset($this->optionsMap[$requiredOption->getName()])) {
return false;
}

$supportedOption = $this->optionsMap[$requiredOption->getName()];

// Check if the required value is supported by this option
if (!$supportedOption->isSupportedValue($requiredOption->getValue())) {
return false;
}
}

return true;
}


/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts;

use WordPress\AiClient\Messages\DTO\Message;
use WordPress\AiClient\Results\DTO\GenerativeAiResult;

/**
* Interface for models that support image generation.
*
* Provides synchronous methods for generating images from text prompts.
*
* @since n.e.x.t
*/
interface ImageGenerationModelInterface
{
/**
* Generates images from a prompt.
*
* @since n.e.x.t
*
* @param list<Message> $prompt Array of messages containing the image generation prompt.
* @return GenerativeAiResult Result containing generated images.
*/
public function generateImageResult(array $prompt): GenerativeAiResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts;

use WordPress\AiClient\Messages\DTO\Message;
use WordPress\AiClient\Operations\DTO\GenerativeAiOperation;

/**
* Interface for models that support asynchronous image generation operations.
*
* Provides methods for initiating long-running image generation tasks.
*
* @since n.e.x.t
*/
interface ImageGenerationOperationModelInterface
{
/**
* Creates an image generation operation.
*
* @since n.e.x.t
*
* @param list<Message> $prompt Array of messages containing the image generation prompt.
* @return GenerativeAiOperation The initiated image generation operation.
*/
public function generateImageOperation(array $prompt): GenerativeAiOperation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\Models\SpeechGeneration\Contracts;

use WordPress\AiClient\Messages\DTO\Message;
use WordPress\AiClient\Results\DTO\GenerativeAiResult;

/**
* Interface for models that support speech generation.
*
* Provides synchronous methods for generating speech from prompts.
*
* @since n.e.x.t
*/
interface SpeechGenerationModelInterface
{
/**
* Generates speech from a prompt.
*
* @since n.e.x.t
*
* @param list<Message> $prompt Array of messages containing the speech generation prompt.
* @return GenerativeAiResult Result containing generated speech audio.
*/
public function generateSpeechResult(array $prompt): GenerativeAiResult;
}
Loading