Skip to content

Commit 73cb8d8

Browse files
committed
Validate like nc_py_api wants it and transform result correctly for assistant
Signed-off-by: Lukas Schaefer <[email protected]>
1 parent c228e14 commit 73cb8d8

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

lib/Service/ProvidersAI/TaskProcessingService.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getExAppTaskProcessingProvider(string $appId, string $name): ?Ta
7979
}
8080
}
8181

82-
private function everyElementHasKeys(array|null $array, array $keys, bool $properties_have_array=true): bool {
82+
private function everyElementHasKeys(array|null $array, array $keys): bool {
8383
if (!is_array($array)) {
8484
return false;
8585
}
@@ -88,20 +88,12 @@ private function everyElementHasKeys(array|null $array, array $keys, bool $prope
8888
if (!is_string($propertyName) || !is_array($properties)) {
8989
return false;
9090
}
91-
if ($properties_have_array) {
92-
foreach ($properties as $property) {
93-
if (!is_array($property)) {
94-
return false;
95-
}
96-
foreach ($keys as $key) {
97-
if (!array_key_exists($key, $property)) {
98-
return false;
99-
}
100-
}
91+
foreach ($properties as $property) {
92+
if (!is_array($property)) {
93+
return false;
10194
}
102-
} else {
10395
foreach ($keys as $key) {
104-
if (!array_key_exists($key, $properties)) {
96+
if (!array_key_exists($key, $property)) {
10597
return false;
10698
}
10799
}
@@ -111,6 +103,21 @@ private function everyElementHasKeys(array|null $array, array $keys, bool $prope
111103
return true;
112104
}
113105

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+
114121
private function validateTaskProcessingProvider(array $provider): void {
115122
if (!isset($provider['id']) || !is_string($provider['id'])) {
116123
throw new Exception('"id" key must be a string');
@@ -124,10 +131,10 @@ private function validateTaskProcessingProvider(array $provider): void {
124131
if (!isset($provider['expected_runtime']) || !is_int($provider['expected_runtime'])) {
125132
throw new Exception('"expected_runtime" key must be an integer');
126133
}
127-
if (!$this->everyElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'], false)) {
134+
if (!$this->everyArrayElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'])) {
128135
throw new Exception('"optional_input_shape" should be an array and must have "name", "description" and "shape_type" keys');
129136
}
130-
if (!$this->everyElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'], false)) {
137+
if (!$this->everyArrayElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'])) {
131138
throw new Exception('"optional_output_shape" should be an array and must have "name", "description" and "shape_type" keys');
132139
}
133140
if (!$this->everyElementHasKeys($provider['input_shape_enum_values'], ['name', 'value'])) {
@@ -278,23 +285,25 @@ public function getExpectedRuntime(): int {
278285
}
279286

280287
public function getOptionalInputShape(): array {
281-
return array_map(function ($shape) {
282-
return new ShapeDescriptor(
288+
return array_reduce($this->provider['optional_input_shape'], function (array $input, array $shape) {
289+
$input[$shape['name']] = new ShapeDescriptor(
283290
$shape['name'],
284291
$shape['description'],
285292
EShapeType::from($shape['shape_type']),
286293
);
287-
}, $this->provider['optional_input_shape']);
294+
return $input;
295+
}, []);
288296
}
289297

290298
public function getOptionalOutputShape(): array {
291-
return array_map(static function (array $shape) {
292-
return new ShapeDescriptor(
299+
return array_reduce($this->provider['optional_output_shape'], function (array $input, array $shape) {
300+
$input[$shape['name']] = new ShapeDescriptor(
293301
$shape['name'],
294302
$shape['description'],
295303
EShapeType::from($shape['shape_type']),
296304
);
297-
}, $this->provider['optional_output_shape']);
305+
return $input;
306+
}, []);
298307
}
299308

300309
public function getInputShapeEnumValues(): array {

0 commit comments

Comments
 (0)