5
5
namespace WordPress \AiClient \Providers ;
6
6
7
7
use InvalidArgumentException ;
8
+ use WordPress \AiClient \Providers \Contracts \ModelMetadataDirectoryInterface ;
9
+ use WordPress \AiClient \Providers \Contracts \ProviderAvailabilityInterface ;
10
+ use WordPress \AiClient \Providers \Contracts \ProviderInterface ;
8
11
use WordPress \AiClient \Providers \DTO \ProviderMetadata ;
9
12
use WordPress \AiClient \Providers \DTO \ProviderModelsMetadata ;
13
+ use WordPress \AiClient \Providers \Models \Contracts \ModelInterface ;
10
14
use WordPress \AiClient \Providers \Models \DTO \ModelConfig ;
11
15
use WordPress \AiClient \Providers \Models \DTO \ModelMetadata ;
12
16
use WordPress \AiClient \Providers \Models \DTO \ModelRequirements ;
@@ -26,10 +30,6 @@ class AiProviderRegistry
26
30
*/
27
31
private array $ providerClassNames = [];
28
32
29
- /**
30
- * @var array<string, object> Cache of instantiated provider instances.
31
- */
32
- private array $ providerInstances = [];
33
33
34
34
/**
35
35
* Registers a provider class with the registry.
@@ -47,19 +47,16 @@ public function registerProvider(string $className): void
47
47
);
48
48
}
49
49
50
- // TODO: Add interface validation when ProviderInterface is available
51
-
52
- // Get provider metadata to extract ID
53
- $ instance = new $ className ();
54
-
55
- // Check if provider has metadata method
56
- if (!method_exists ($ instance , 'metadata ' )) {
50
+ // Validate that class implements ProviderInterface
51
+ if (!is_subclass_of ($ className , ProviderInterface::class)) {
57
52
throw new InvalidArgumentException (
58
- sprintf ('Provider must implement metadata() method : %s ' , $ className )
53
+ sprintf ('Provider class must implement %s : %s ' , ProviderInterface::class , $ className )
59
54
);
60
55
}
61
56
62
- $ metadata = $ instance ->metadata ();
57
+ // Get provider metadata to extract ID (using static method from interface)
58
+ /** @var class-string<ProviderInterface> $className */
59
+ $ metadata = $ className ::metadata ();
63
60
64
61
if (!$ metadata instanceof ProviderMetadata) {
65
62
throw new InvalidArgumentException (
@@ -115,11 +112,13 @@ public function getProviderClassName(string $id): string
115
112
public function isProviderConfigured (string $ idOrClassName ): bool
116
113
{
117
114
try {
118
- $ this ->getProviderInstance ($ idOrClassName );
115
+ $ className = $ this ->resolveProviderClassName ($ idOrClassName );
119
116
120
- // TODO: Call availability() method when ProviderInterface is available
121
- // For now, assume configured if we can instantiate without exception
122
- return true ;
117
+ // Use static method from ProviderInterface
118
+ /** @var class-string<ProviderInterface> $className */
119
+ $ availability = $ className ::availability ();
120
+
121
+ return $ availability ->isConfigured ();
123
122
} catch (InvalidArgumentException $ e ) {
124
123
return false ;
125
124
}
@@ -140,17 +139,9 @@ public function findModelsMetadataForSupport(ModelRequirements $modelRequirement
140
139
foreach ($ this ->providerClassNames as $ providerId => $ className ) {
141
140
$ providerResults = $ this ->findProviderModelsMetadataForSupport ($ providerId , $ modelRequirements );
142
141
if (!empty ($ providerResults )) {
143
- $ providerInstance = $ this ->getProviderInstance ($ providerId );
144
-
145
- // Validate that provider has metadata method
146
- if (!method_exists ($ providerInstance , 'metadata ' )) {
147
- continue ;
148
- }
149
-
150
- $ providerMetadata = $ providerInstance ->metadata ();
151
- if (!$ providerMetadata instanceof ProviderMetadata) {
152
- continue ;
153
- }
142
+ // Use static method from ProviderInterface
143
+ /** @var class-string<ProviderInterface> $className */
144
+ $ providerMetadata = $ className ::metadata ();
154
145
155
146
$ results [] = new ProviderModelsMetadata (
156
147
$ providerMetadata ,
@@ -175,11 +166,21 @@ public function findProviderModelsMetadataForSupport(
175
166
string $ idOrClassName ,
176
167
ModelRequirements $ modelRequirements
177
168
): array {
178
- $ instance = $ this ->getProviderInstance ($ idOrClassName );
169
+ $ className = $ this ->resolveProviderClassName ($ idOrClassName );
170
+
171
+ // Use static method from ProviderInterface
172
+ /** @var class-string<ProviderInterface> $className */
173
+ $ modelMetadataDirectory = $ className ::modelMetadataDirectory ();
179
174
180
- // TODO: Get model metadata directory when ProviderInterface is available
181
- // For now, return empty array as placeholder
182
- return [];
175
+ // Filter models that meet requirements
176
+ $ matchingModels = [];
177
+ foreach ($ modelMetadataDirectory ->listModelMetadata () as $ modelMetadata ) {
178
+ if ($ modelMetadata ->meetsRequirements ($ modelRequirements )) {
179
+ $ matchingModels [] = $ modelMetadata ;
180
+ }
181
+ }
182
+
183
+ return $ matchingModels ;
183
184
}
184
185
185
186
/**
@@ -189,26 +190,27 @@ public function findProviderModelsMetadataForSupport(
189
190
*
190
191
* @param string $idOrClassName The provider ID or class name.
191
192
* @param string $modelId The model identifier.
192
- * @param ModelConfig $modelConfig The model configuration.
193
- * @return object The configured model instance.
193
+ * @param ModelConfig|null $modelConfig The model configuration.
194
+ * @return ModelInterface The configured model instance.
194
195
* @throws InvalidArgumentException If provider or model is not found.
195
196
*/
196
- public function getProviderModel (string $ idOrClassName , string $ modelId , ModelConfig $ modelConfig ): object
197
+ public function getProviderModel (string $ idOrClassName , string $ modelId , ? ModelConfig $ modelConfig = null ): ModelInterface
197
198
{
198
- $ instance = $ this ->getProviderInstance ($ idOrClassName );
199
+ $ className = $ this ->resolveProviderClassName ($ idOrClassName );
199
200
200
- // TODO: Call model() method when ProviderInterface is available
201
- throw new InvalidArgumentException ('Model instantiation not yet implemented ' );
201
+ // Use static method from ProviderInterface
202
+ /** @var class-string<ProviderInterface> $className */
203
+ return $ className ::model ($ modelId , $ modelConfig );
202
204
}
203
205
204
206
/**
205
- * Gets or creates a provider instance .
207
+ * Gets the class name for a registered provider (handles both ID and class name input) .
206
208
*
207
209
* @param string $idOrClassName The provider ID or class name.
208
- * @return object The provider instance .
210
+ * @return string The provider class name .
209
211
* @throws InvalidArgumentException If provider is not registered.
210
212
*/
211
- private function getProviderInstance (string $ idOrClassName ): object
213
+ private function resolveProviderClassName (string $ idOrClassName ): string
212
214
{
213
215
// Handle both ID and class name
214
216
$ className = $ this ->providerClassNames [$ idOrClassName ] ?? $ idOrClassName ;
@@ -219,14 +221,6 @@ private function getProviderInstance(string $idOrClassName): object
219
221
);
220
222
}
221
223
222
- // Use cached instance if available
223
- if (isset ($ this ->providerInstances [$ className ])) {
224
- return $ this ->providerInstances [$ className ];
225
- }
226
-
227
- // Create and cache new instance
228
- $ this ->providerInstances [$ className ] = new $ className ();
229
-
230
- return $ this ->providerInstances [$ className ];
224
+ return $ className ;
231
225
}
232
226
}
0 commit comments