From 3dcedb7900534091c73f9344780a02653e131c14 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 24 Oct 2023 14:41:24 +0200 Subject: [PATCH 1/7] enh(TextProcessing): Allow providers and task types to declare a dynamic ID instead of using className this allows AppAPI to register anonymous classes as TextProcessing providers and task types Signed-off-by: Marcel Klehr --- lib/private/TextProcessing/Manager.php | 8 +++- lib/public/TextProcessing/IProvider.php | 2 +- lib/public/TextProcessing/IProviderWithId.php | 39 +++++++++++++++++++ lib/public/TextProcessing/ITaskTypeWithId.php | 37 ++++++++++++++++++ lib/public/TextProcessing/Task.php | 4 +- 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 lib/public/TextProcessing/IProviderWithId.php create mode 100644 lib/public/TextProcessing/ITaskTypeWithId.php diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index b9cb06c298e..47af57bf3ec 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -28,6 +28,7 @@ namespace OC\TextProcessing; use OC\AppFramework\Bootstrap\Coordinator; use OC\TextProcessing\Db\Task as DbTask; use OCP\IConfig; +use OCP\TextProcessing\IProviderWithId; use OCP\TextProcessing\Task; use OCP\TextProcessing\Task as OCPTask; use OC\TextProcessing\Db\TaskMapper; @@ -120,7 +121,12 @@ class Manager implements IManager { $preferences = json_decode($json, true); if (isset($preferences[$task->getType()])) { // If a preference for this task type is set, move the preferred provider to the start - $provider = current(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()])); + $provider = current(array_filter($providers, function ($provider) use ($preferences, $task) { + if ($provider instanceof IProviderWithId) { + return $provider->getId() === $preferences[$task->getType()]; + } + return $provider::class === $preferences[$task->getType()]; + })); if ($provider !== false) { $providers = array_filter($providers, fn ($p) => $p !== $provider); array_unshift($providers, $provider); diff --git a/lib/public/TextProcessing/IProvider.php b/lib/public/TextProcessing/IProvider.php index 6132e60b493..01c17f4bfbf 100644 --- a/lib/public/TextProcessing/IProvider.php +++ b/lib/public/TextProcessing/IProvider.php @@ -56,7 +56,7 @@ interface IProvider { * provider handles * * @since 27.1.0 - * @return class-string + * @return class-string|string */ public function getTaskType(): string; } diff --git a/lib/public/TextProcessing/IProviderWithId.php b/lib/public/TextProcessing/IProviderWithId.php new file mode 100644 index 00000000000..3d86b50b2c8 --- /dev/null +++ b/lib/public/TextProcessing/IProviderWithId.php @@ -0,0 +1,39 @@ + + * + * @author Marcel Klehr + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + +namespace OCP\TextProcessing; + +/** + * @since 28.0.0 + */ +interface IProviderWithId extends IProvider { + + /** + * The id of this provider + * @since 28.0.0 + */ + public function getId(): string; +} diff --git a/lib/public/TextProcessing/ITaskTypeWithId.php b/lib/public/TextProcessing/ITaskTypeWithId.php new file mode 100644 index 00000000000..b86b831f266 --- /dev/null +++ b/lib/public/TextProcessing/ITaskTypeWithId.php @@ -0,0 +1,37 @@ + + * + * @author Marcel Klehr + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCP\TextProcessing; + +/** + * @since 28.0.0 + */ +interface ITaskTypeWithId { + /** + * The id of this provider + * @since 28.0.0 + */ + public function getId(): string; +} diff --git a/lib/public/TextProcessing/Task.php b/lib/public/TextProcessing/Task.php index 446e414cb04..52a7637bd7c 100644 --- a/lib/public/TextProcessing/Task.php +++ b/lib/public/TextProcessing/Task.php @@ -29,8 +29,8 @@ namespace OCP\TextProcessing; * This is a text processing task * @since 27.1.0 * @psalm-template T of ITaskType - * @psalm-template S as class-string - * @psalm-template P as IProvider + * @psalm-template S as class-string | string + * @psalm-template P as IProvider | IProvider */ final class Task implements \JsonSerializable { protected ?int $id = null; From be0a0166e8879a88677adb105f8f41a2dcfa4f13 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 24 Oct 2023 15:34:51 +0200 Subject: [PATCH 2/7] fix(TextProcessing): fix psalm typing Signed-off-by: Marcel Klehr --- lib/public/TextProcessing/IManager.php | 2 +- lib/public/TextProcessing/IProviderWithId.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php index dec0baba4bb..4604dc4d663 100644 --- a/lib/public/TextProcessing/IManager.php +++ b/lib/public/TextProcessing/IManager.php @@ -48,7 +48,7 @@ interface IManager { public function getProviders(): array; /** - * @return class-string[] + * @return string[] * @since 27.1.0 */ public function getAvailableTaskTypes(): array; diff --git a/lib/public/TextProcessing/IProviderWithId.php b/lib/public/TextProcessing/IProviderWithId.php index 3d86b50b2c8..6723168988e 100644 --- a/lib/public/TextProcessing/IProviderWithId.php +++ b/lib/public/TextProcessing/IProviderWithId.php @@ -23,11 +23,11 @@ declare(strict_types=1); * along with this program. If not, see . */ - namespace OCP\TextProcessing; /** * @since 28.0.0 + * @extends IProvider */ interface IProviderWithId extends IProvider { From f6d765fc1f4837caf289e4aee4f27a56c852a72d Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 8 Nov 2023 16:47:22 +0100 Subject: [PATCH 3/7] fix: Use getId() in ArtificialIntelligence settings Signed-off-by: Marcel Klehr --- .../settings/lib/Settings/Admin/ArtificialIntelligence.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php index eb1983690a5..72efca66636 100644 --- a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php +++ b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php @@ -33,6 +33,7 @@ use OCP\Settings\IDelegatedSettings; use OCP\SpeechToText\ISpeechToTextManager; use OCP\TextProcessing\IManager; use OCP\TextProcessing\IProvider; +use OCP\TextProcessing\IProviderWithId; use OCP\TextProcessing\ITaskType; use OCP\Translation\ITranslationManager; use Psr\Container\ContainerExceptionInterface; @@ -74,15 +75,15 @@ class ArtificialIntelligence implements IDelegatedSettings { } $textProcessingProviders = []; - /** @var array, class-string> $textProcessingSettings */ + /** @var array, string|class-string> $textProcessingSettings */ $textProcessingSettings = []; foreach ($this->textProcessingManager->getProviders() as $provider) { $textProcessingProviders[] = [ - 'class' => $provider::class, + 'class' => $provider instanceof IProviderWithId ? $provider->getId() : $provider::class, 'name' => $provider->getName(), 'taskType' => $provider->getTaskType(), ]; - $textProcessingSettings[$provider->getTaskType()] = $provider::class; + $textProcessingSettings[$provider->getTaskType()] = $provider instanceof IProviderWithId ? $provider->getId() : $provider::class; } $textProcessingTaskTypes = []; foreach ($textProcessingSettings as $taskTypeClass => $providerClass) { From 8fe993c06fdff6f9c6c1f502244cb4db79408f4c Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 8 Nov 2023 16:58:31 +0100 Subject: [PATCH 4/7] fix: Remove ITaskTypeWithId Signed-off-by: Marcel Klehr --- lib/public/TextProcessing/IProvider.php | 2 +- lib/public/TextProcessing/IProviderWithId.php | 3 +- lib/public/TextProcessing/ITaskTypeWithId.php | 37 ------------------- lib/public/TextProcessing/Task.php | 4 +- 4 files changed, 5 insertions(+), 41 deletions(-) delete mode 100644 lib/public/TextProcessing/ITaskTypeWithId.php diff --git a/lib/public/TextProcessing/IProvider.php b/lib/public/TextProcessing/IProvider.php index 01c17f4bfbf..6132e60b493 100644 --- a/lib/public/TextProcessing/IProvider.php +++ b/lib/public/TextProcessing/IProvider.php @@ -56,7 +56,7 @@ interface IProvider { * provider handles * * @since 27.1.0 - * @return class-string|string + * @return class-string */ public function getTaskType(): string; } diff --git a/lib/public/TextProcessing/IProviderWithId.php b/lib/public/TextProcessing/IProviderWithId.php index 6723168988e..f7b6ecfda0e 100644 --- a/lib/public/TextProcessing/IProviderWithId.php +++ b/lib/public/TextProcessing/IProviderWithId.php @@ -27,7 +27,8 @@ namespace OCP\TextProcessing; /** * @since 28.0.0 - * @extends IProvider + * @extends IProvider + * @template T of ITaskType */ interface IProviderWithId extends IProvider { diff --git a/lib/public/TextProcessing/ITaskTypeWithId.php b/lib/public/TextProcessing/ITaskTypeWithId.php deleted file mode 100644 index b86b831f266..00000000000 --- a/lib/public/TextProcessing/ITaskTypeWithId.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * @author Marcel Klehr - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -namespace OCP\TextProcessing; - -/** - * @since 28.0.0 - */ -interface ITaskTypeWithId { - /** - * The id of this provider - * @since 28.0.0 - */ - public function getId(): string; -} diff --git a/lib/public/TextProcessing/Task.php b/lib/public/TextProcessing/Task.php index 52a7637bd7c..446e414cb04 100644 --- a/lib/public/TextProcessing/Task.php +++ b/lib/public/TextProcessing/Task.php @@ -29,8 +29,8 @@ namespace OCP\TextProcessing; * This is a text processing task * @since 27.1.0 * @psalm-template T of ITaskType - * @psalm-template S as class-string | string - * @psalm-template P as IProvider | IProvider + * @psalm-template S as class-string + * @psalm-template P as IProvider */ final class Task implements \JsonSerializable { protected ?int $id = null; From ab736429ce1bf126bd8b1bef1db4cac9a31e139e Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 8 Nov 2023 17:02:00 +0100 Subject: [PATCH 5/7] fix: Make linters happy Signed-off-by: Marcel Klehr --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/public/TextProcessing/IProviderWithId.php | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 548d930e758..d57b1925d8e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -668,6 +668,7 @@ return array( 'OCP\\TextProcessing\\HeadlineTaskType' => $baseDir . '/lib/public/TextProcessing/HeadlineTaskType.php', 'OCP\\TextProcessing\\IManager' => $baseDir . '/lib/public/TextProcessing/IManager.php', 'OCP\\TextProcessing\\IProvider' => $baseDir . '/lib/public/TextProcessing/IProvider.php', + 'OCP\\TextProcessing\\IProviderWithId' => $baseDir . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\ITaskType' => $baseDir . '/lib/public/TextProcessing/ITaskType.php', 'OCP\\TextProcessing\\SummaryTaskType' => $baseDir . '/lib/public/TextProcessing/SummaryTaskType.php', 'OCP\\TextProcessing\\Task' => $baseDir . '/lib/public/TextProcessing/Task.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 4af4beb4867..768a7a956e0 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -701,6 +701,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\TextProcessing\\HeadlineTaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/HeadlineTaskType.php', 'OCP\\TextProcessing\\IManager' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IManager.php', 'OCP\\TextProcessing\\IProvider' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProvider.php', + 'OCP\\TextProcessing\\IProviderWithId' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\ITaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/ITaskType.php', 'OCP\\TextProcessing\\SummaryTaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/SummaryTaskType.php', 'OCP\\TextProcessing\\Task' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/Task.php', diff --git a/lib/public/TextProcessing/IProviderWithId.php b/lib/public/TextProcessing/IProviderWithId.php index f7b6ecfda0e..1bd02278d1c 100644 --- a/lib/public/TextProcessing/IProviderWithId.php +++ b/lib/public/TextProcessing/IProviderWithId.php @@ -31,7 +31,6 @@ namespace OCP\TextProcessing; * @template T of ITaskType */ interface IProviderWithId extends IProvider { - /** * The id of this provider * @since 28.0.0 From 52d729c69ec2dc84245f25b327ee7c286c7a7131 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 19 Dec 2023 13:32:15 +0100 Subject: [PATCH 6/7] cs:fix Signed-off-by: Marcel Klehr --- lib/private/TextProcessing/Manager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index 70f5f322aa5..34f0b4e7964 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -284,11 +284,11 @@ class Manager implements IManager { if (isset($preferences[$task->getType()])) { // If a preference for this task type is set, move the preferred provider to the start $provider = current(array_values(array_filter($providers, function ($provider) use ($preferences, $task) { - if ($provider instanceof IProviderWithId) { + if ($provider instanceof IProviderWithId) { return $provider->getId() === $preferences[$task->getType()]; } - $provider::class === $preferences[$task->getType()]; - }))); + $provider::class === $preferences[$task->getType()]; + }))); if ($provider !== false) { $providers = array_filter($providers, fn ($p) => $p !== $provider); array_unshift($providers, $provider); From e5622171b45639302c173e427c424963a0ff7b9f Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 19 Dec 2023 15:01:03 +0100 Subject: [PATCH 7/7] Update autoloaders Signed-off-by: Marcel Klehr --- lib/composer/composer/autoload_classmap.php | 2 +- lib/composer/composer/autoload_static.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6cbb31b7fbf..887f4688bce 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -690,8 +690,8 @@ return array( 'OCP\\TextProcessing\\HeadlineTaskType' => $baseDir . '/lib/public/TextProcessing/HeadlineTaskType.php', 'OCP\\TextProcessing\\IManager' => $baseDir . '/lib/public/TextProcessing/IManager.php', 'OCP\\TextProcessing\\IProvider' => $baseDir . '/lib/public/TextProcessing/IProvider.php', - 'OCP\\TextProcessing\\IProviderWithId' => $baseDir . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\IProviderWithExpectedRuntime' => $baseDir . '/lib/public/TextProcessing/IProviderWithExpectedRuntime.php', + 'OCP\\TextProcessing\\IProviderWithId' => $baseDir . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\IProviderWithUserId' => $baseDir . '/lib/public/TextProcessing/IProviderWithUserId.php', 'OCP\\TextProcessing\\ITaskType' => $baseDir . '/lib/public/TextProcessing/ITaskType.php', 'OCP\\TextProcessing\\SummaryTaskType' => $baseDir . '/lib/public/TextProcessing/SummaryTaskType.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 3fb9f20d72d..2d46eacfc86 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -723,8 +723,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\TextProcessing\\HeadlineTaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/HeadlineTaskType.php', 'OCP\\TextProcessing\\IManager' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IManager.php', 'OCP\\TextProcessing\\IProvider' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProvider.php', - 'OCP\\TextProcessing\\IProviderWithId' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\IProviderWithExpectedRuntime' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProviderWithExpectedRuntime.php', + 'OCP\\TextProcessing\\IProviderWithId' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProviderWithId.php', 'OCP\\TextProcessing\\IProviderWithUserId' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProviderWithUserId.php', 'OCP\\TextProcessing\\ITaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/ITaskType.php', 'OCP\\TextProcessing\\SummaryTaskType' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/SummaryTaskType.php',