Skip to content

Commit 73d25a8

Browse files
authored
Merge pull request #548 from Lukasdotcom/fix/registration_validation
Fix task processing registration validation
2 parents bb3976d + 73cb8d8 commit 73d25a8

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lib/Service/ProvidersAI/TaskProcessingService.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ private function everyElementHasKeys(array|null $array, array $keys): bool {
103103
return true;
104104
}
105105

106+
private function everyArrayElementHasKeys(array|null $array, array $keys): bool {
107+
if (!is_array($array)) {
108+
return false;
109+
}
110+
111+
foreach ($array as $element) {
112+
foreach ($keys as $key) {
113+
if (!array_key_exists($key, $element)) {
114+
return false;
115+
}
116+
}
117+
}
118+
return true;
119+
}
120+
106121
private function validateTaskProcessingProvider(array $provider): void {
107122
if (!isset($provider['id']) || !is_string($provider['id'])) {
108123
throw new Exception('"id" key must be a string');
@@ -116,10 +131,10 @@ private function validateTaskProcessingProvider(array $provider): void {
116131
if (!isset($provider['expected_runtime']) || !is_int($provider['expected_runtime'])) {
117132
throw new Exception('"expected_runtime" key must be an integer');
118133
}
119-
if (!$this->everyElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'])) {
134+
if (!$this->everyArrayElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'])) {
120135
throw new Exception('"optional_input_shape" should be an array and must have "name", "description" and "shape_type" keys');
121136
}
122-
if (!$this->everyElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'])) {
137+
if (!$this->everyArrayElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'])) {
123138
throw new Exception('"optional_output_shape" should be an array and must have "name", "description" and "shape_type" keys');
124139
}
125140
if (!$this->everyElementHasKeys($provider['input_shape_enum_values'], ['name', 'value'])) {
@@ -270,23 +285,25 @@ public function getExpectedRuntime(): int {
270285
}
271286

272287
public function getOptionalInputShape(): array {
273-
return array_map(function ($shape) {
274-
return new ShapeDescriptor(
288+
return array_reduce($this->provider['optional_input_shape'], function (array $input, array $shape) {
289+
$input[$shape['name']] = new ShapeDescriptor(
275290
$shape['name'],
276291
$shape['description'],
277292
EShapeType::from($shape['shape_type']),
278293
);
279-
}, $this->provider['optional_input_shape']);
294+
return $input;
295+
}, []);
280296
}
281297

282298
public function getOptionalOutputShape(): array {
283-
return array_map(static function (array $shape) {
284-
return new ShapeDescriptor(
299+
return array_reduce($this->provider['optional_output_shape'], function (array $input, array $shape) {
300+
$input[$shape['name']] = new ShapeDescriptor(
285301
$shape['name'],
286302
$shape['description'],
287303
EShapeType::from($shape['shape_type']),
288304
);
289-
}, $this->provider['optional_output_shape']);
305+
return $input;
306+
}, []);
290307
}
291308

292309
public function getInputShapeEnumValues(): array {

0 commit comments

Comments
 (0)