Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Validate like nc_py_api wants it and transform result correctly for a…
…ssistant

Signed-off-by: Lukas Schaefer <[email protected]>
  • Loading branch information
lukasdotcom authored and backportbot[bot] committed Apr 24, 2025
commit 2ea9a2a4ec47090c745832a9a659cb7fe709f6b5
51 changes: 30 additions & 21 deletions lib/Service/ProvidersAI/TaskProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getExAppTaskProcessingProvider(string $appId, string $name): ?Ta
}
}

private function everyElementHasKeys(array|null $array, array $keys, bool $properties_have_array=true): bool {
private function everyElementHasKeys(array|null $array, array $keys): bool {
if (!is_array($array)) {
return false;
}
Expand All @@ -88,20 +88,12 @@ private function everyElementHasKeys(array|null $array, array $keys, bool $prope
if (!is_string($propertyName) || !is_array($properties)) {
return false;
}
if ($properties_have_array) {
foreach ($properties as $property) {
if (!is_array($property)) {
return false;
}
foreach ($keys as $key) {
if (!array_key_exists($key, $property)) {
return false;
}
}
foreach ($properties as $property) {
if (!is_array($property)) {
return false;
}
} else {
foreach ($keys as $key) {
if (!array_key_exists($key, $properties)) {
if (!array_key_exists($key, $property)) {
return false;
}
}
Expand All @@ -111,6 +103,21 @@ private function everyElementHasKeys(array|null $array, array $keys, bool $prope
return true;
}

private function everyArrayElementHasKeys(array|null $array, array $keys): bool {
if (!is_array($array)) {
return false;
}

foreach ($array as $element) {
foreach ($keys as $key) {
if (!array_key_exists($key, $element)) {
return false;
}
}
}
return true;
}

private function validateTaskProcessingProvider(array $provider): void {
if (!isset($provider['id']) || !is_string($provider['id'])) {
throw new Exception('"id" key must be a string');
Expand All @@ -124,10 +131,10 @@ private function validateTaskProcessingProvider(array $provider): void {
if (!isset($provider['expected_runtime']) || !is_int($provider['expected_runtime'])) {
throw new Exception('"expected_runtime" key must be an integer');
}
if (!$this->everyElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'], false)) {
if (!$this->everyArrayElementHasKeys($provider['optional_input_shape'], ['name', 'description', 'shape_type'])) {
throw new Exception('"optional_input_shape" should be an array and must have "name", "description" and "shape_type" keys');
}
if (!$this->everyElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'], false)) {
if (!$this->everyArrayElementHasKeys($provider['optional_output_shape'], ['name', 'description', 'shape_type'])) {
throw new Exception('"optional_output_shape" should be an array and must have "name", "description" and "shape_type" keys');
}
if (!$this->everyElementHasKeys($provider['input_shape_enum_values'], ['name', 'value'])) {
Expand Down Expand Up @@ -278,23 +285,25 @@ public function getExpectedRuntime(): int {
}

public function getOptionalInputShape(): array {
return array_map(function ($shape) {
return new ShapeDescriptor(
return array_reduce($this->provider['optional_input_shape'], function (array $input, array $shape) {
$input[$shape['name']] = new ShapeDescriptor(
$shape['name'],
$shape['description'],
EShapeType::from($shape['shape_type']),
);
}, $this->provider['optional_input_shape']);
return $input;
}, []);
}

public function getOptionalOutputShape(): array {
return array_map(static function (array $shape) {
return new ShapeDescriptor(
return array_reduce($this->provider['optional_output_shape'], function (array $input, array $shape) {
$input[$shape['name']] = new ShapeDescriptor(
$shape['name'],
$shape['description'],
EShapeType::from($shape['shape_type']),
);
}, $this->provider['optional_output_shape']);
return $input;
}, []);
}

public function getInputShapeEnumValues(): array {
Expand Down
Loading