Skip to content

Commit 258f7c9

Browse files
Merge pull request #35 from WordPress/provider-contracts
2 parents 1f5763c + 5b46445 commit 258f7c9

13 files changed

+472
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Contracts;
6+
7+
use InvalidArgumentException;
8+
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
9+
10+
/**
11+
* Interface for accessing model metadata within a provider.
12+
*
13+
* Provides methods to list, check, and retrieve model metadata
14+
* for all models supported by a provider.
15+
*
16+
* @since n.e.x.t
17+
*/
18+
interface ModelMetadataDirectoryInterface
19+
{
20+
/**
21+
* Lists all available model metadata.
22+
*
23+
* @since n.e.x.t
24+
*
25+
* @return list<ModelMetadata> Array of model metadata.
26+
*/
27+
public function listModelMetadata(): array;
28+
29+
/**
30+
* Checks if metadata exists for a specific model.
31+
*
32+
* @since n.e.x.t
33+
*
34+
* @param string $modelId Model identifier.
35+
* @return bool True if metadata exists, false otherwise.
36+
*/
37+
public function hasModelMetadata(string $modelId): bool;
38+
39+
/**
40+
* Gets metadata for a specific model.
41+
*
42+
* @since n.e.x.t
43+
*
44+
* @param string $modelId Model identifier.
45+
* @return ModelMetadata Model metadata.
46+
* @throws InvalidArgumentException If model metadata not found.
47+
*/
48+
public function getModelMetadata(string $modelId): ModelMetadata;
49+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Contracts;
6+
7+
/**
8+
* Interface for checking provider availability.
9+
*
10+
* Determines whether a provider is configured and available
11+
* for use based on API keys, credentials, or other requirements.
12+
*
13+
* @since n.e.x.t
14+
*/
15+
interface ProviderAvailabilityInterface
16+
{
17+
/**
18+
* Checks if the provider is configured.
19+
*
20+
* @since n.e.x.t
21+
*
22+
* @return bool True if the provider is configured and available, false otherwise.
23+
*/
24+
public function isConfigured(): bool;
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Contracts;
6+
7+
use InvalidArgumentException;
8+
use WordPress\AiClient\Providers\DTO\ProviderMetadata;
9+
use WordPress\AiClient\Providers\Models\Contracts\ModelInterface;
10+
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
11+
12+
/**
13+
* Interface for AI providers.
14+
*
15+
* Providers represent AI services (Google, OpenAI, Anthropic, etc.)
16+
* and provide access to models, metadata, and availability information.
17+
*
18+
* @since n.e.x.t
19+
*/
20+
interface ProviderInterface
21+
{
22+
/**
23+
* Gets provider metadata.
24+
*
25+
* @since n.e.x.t
26+
*
27+
* @return ProviderMetadata Provider metadata.
28+
*/
29+
public static function metadata(): ProviderMetadata;
30+
31+
/**
32+
* Creates a model instance.
33+
*
34+
* @since n.e.x.t
35+
*
36+
* @param string $modelId Model identifier.
37+
* @param ?ModelConfig $modelConfig Model configuration.
38+
* @return ModelInterface Model instance.
39+
* @throws InvalidArgumentException If model not found or configuration invalid.
40+
*/
41+
public static function model(string $modelId, ?ModelConfig $modelConfig = null): ModelInterface;
42+
43+
/**
44+
* Gets provider availability checker.
45+
*
46+
* @since n.e.x.t
47+
*
48+
* @return ProviderAvailabilityInterface Provider availability checker.
49+
*/
50+
public static function availability(): ProviderAvailabilityInterface;
51+
52+
/**
53+
* Gets model metadata directory.
54+
*
55+
* @since n.e.x.t
56+
*
57+
* @return ModelMetadataDirectoryInterface Model metadata directory.
58+
*/
59+
public static function modelMetadataDirectory(): ModelMetadataDirectoryInterface;
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Models\Contracts;
6+
7+
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
8+
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
9+
10+
/**
11+
* Interface for AI models.
12+
*
13+
* Models represent specific AI models from providers and define
14+
* their capabilities, configuration, and execution methods.
15+
*
16+
* @since n.e.x.t
17+
*/
18+
interface ModelInterface
19+
{
20+
/**
21+
* Gets model metadata.
22+
*
23+
* @since n.e.x.t
24+
*
25+
* @return ModelMetadata Model metadata.
26+
*/
27+
public function metadata(): ModelMetadata;
28+
29+
/**
30+
* Sets model configuration.
31+
*
32+
* @since n.e.x.t
33+
*
34+
* @param ModelConfig $config Model configuration.
35+
* @return void
36+
*/
37+
public function setConfig(ModelConfig $config): void;
38+
39+
/**
40+
* Gets model configuration.
41+
*
42+
* @since n.e.x.t
43+
*
44+
* @return ModelConfig Current model configuration.
45+
*/
46+
public function getConfig(): ModelConfig;
47+
}

src/Providers/Models/DTO/ModelMetadata.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ class ModelMetadata extends AbstractDataTransferObject
5454
*/
5555
protected array $supportedOptions;
5656

57+
/**
58+
* @var array<string, true> Map of supported capabilities for O(1) lookups.
59+
*/
60+
private array $capabilitiesMap = [];
61+
62+
/**
63+
* @var array<string, SupportedOption> Map of supported options by name for O(1) lookups.
64+
*/
65+
private array $optionsMap = [];
66+
5767
/**
5868
* Constructor.
5969
*
@@ -80,6 +90,16 @@ public function __construct(string $id, string $name, array $supportedCapabiliti
8090
$this->name = $name;
8191
$this->supportedCapabilities = $supportedCapabilities;
8292
$this->supportedOptions = $supportedOptions;
93+
94+
// Build capability map for efficient lookups
95+
foreach ($supportedCapabilities as $capability) {
96+
$this->capabilitiesMap[$capability->value] = true;
97+
}
98+
99+
// Build options map for efficient lookups
100+
foreach ($supportedOptions as $option) {
101+
$this->optionsMap[$option->getName()] = $option;
102+
}
83103
}
84104

85105
/**
@@ -189,6 +209,42 @@ public function toArray(): array
189209
];
190210
}
191211

212+
/**
213+
* Checks whether this model meets the specified requirements.
214+
*
215+
* @since n.e.x.t
216+
*
217+
* @param ModelRequirements $requirements The requirements to check against.
218+
* @return bool True if the model meets all requirements, false otherwise.
219+
*/
220+
public function meetsRequirements(ModelRequirements $requirements): bool
221+
{
222+
// Check if all required capabilities are supported using map lookup
223+
foreach ($requirements->getRequiredCapabilities() as $requiredCapability) {
224+
if (!isset($this->capabilitiesMap[$requiredCapability->value])) {
225+
return false;
226+
}
227+
}
228+
229+
// Check if all required options are supported with the specified values
230+
foreach ($requirements->getRequiredOptions() as $requiredOption) {
231+
// Use map lookup instead of linear search
232+
if (!isset($this->optionsMap[$requiredOption->getName()])) {
233+
return false;
234+
}
235+
236+
$supportedOption = $this->optionsMap[$requiredOption->getName()];
237+
238+
// Check if the required value is supported by this option
239+
if (!$supportedOption->isSupportedValue($requiredOption->getValue())) {
240+
return false;
241+
}
242+
}
243+
244+
return true;
245+
}
246+
247+
192248
/**
193249
* {@inheritDoc}
194250
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts;
6+
7+
use WordPress\AiClient\Messages\DTO\Message;
8+
use WordPress\AiClient\Results\DTO\GenerativeAiResult;
9+
10+
/**
11+
* Interface for models that support image generation.
12+
*
13+
* Provides synchronous methods for generating images from text prompts.
14+
*
15+
* @since n.e.x.t
16+
*/
17+
interface ImageGenerationModelInterface
18+
{
19+
/**
20+
* Generates images from a prompt.
21+
*
22+
* @since n.e.x.t
23+
*
24+
* @param list<Message> $prompt Array of messages containing the image generation prompt.
25+
* @return GenerativeAiResult Result containing generated images.
26+
*/
27+
public function generateImageResult(array $prompt): GenerativeAiResult;
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts;
6+
7+
use WordPress\AiClient\Messages\DTO\Message;
8+
use WordPress\AiClient\Operations\DTO\GenerativeAiOperation;
9+
10+
/**
11+
* Interface for models that support asynchronous image generation operations.
12+
*
13+
* Provides methods for initiating long-running image generation tasks.
14+
*
15+
* @since n.e.x.t
16+
*/
17+
interface ImageGenerationOperationModelInterface
18+
{
19+
/**
20+
* Creates an image generation operation.
21+
*
22+
* @since n.e.x.t
23+
*
24+
* @param list<Message> $prompt Array of messages containing the image generation prompt.
25+
* @return GenerativeAiOperation The initiated image generation operation.
26+
*/
27+
public function generateImageOperation(array $prompt): GenerativeAiOperation;
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Models\SpeechGeneration\Contracts;
6+
7+
use WordPress\AiClient\Messages\DTO\Message;
8+
use WordPress\AiClient\Results\DTO\GenerativeAiResult;
9+
10+
/**
11+
* Interface for models that support speech generation.
12+
*
13+
* Provides synchronous methods for generating speech from prompts.
14+
*
15+
* @since n.e.x.t
16+
*/
17+
interface SpeechGenerationModelInterface
18+
{
19+
/**
20+
* Generates speech from a prompt.
21+
*
22+
* @since n.e.x.t
23+
*
24+
* @param list<Message> $prompt Array of messages containing the speech generation prompt.
25+
* @return GenerativeAiResult Result containing generated speech audio.
26+
*/
27+
public function generateSpeechResult(array $prompt): GenerativeAiResult;
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Models\SpeechGeneration\Contracts;
6+
7+
use WordPress\AiClient\Messages\DTO\Message;
8+
use WordPress\AiClient\Operations\DTO\GenerativeAiOperation;
9+
10+
/**
11+
* Interface for models that support asynchronous speech generation operations.
12+
*
13+
* Provides methods for initiating long-running speech generation tasks.
14+
*
15+
* @since n.e.x.t
16+
*/
17+
interface SpeechGenerationOperationModelInterface
18+
{
19+
/**
20+
* Creates a speech generation operation.
21+
*
22+
* @since n.e.x.t
23+
*
24+
* @param list<Message> $prompt Array of messages containing the speech generation prompt.
25+
* @return GenerativeAiOperation The initiated speech generation operation.
26+
*/
27+
public function generateSpeechOperation(array $prompt): GenerativeAiOperation;
28+
}

0 commit comments

Comments
 (0)