Merge pull request #41088 from nextcloud/enh/text-processing-provider-with-id

enh(TextProcessing): Allow providers and task types to declare a dynamic ID instead of using className
pull/42389/head
Marcel Klehr 5 months ago committed by GitHub
commit c3d5b46f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,6 +34,7 @@ use OCP\SpeechToText\ISpeechToTextManager;
use OCP\SpeechToText\ISpeechToTextProviderWithId;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithId;
use OCP\TextProcessing\ITaskType;
use OCP\Translation\ITranslationManager;
use Psr\Container\ContainerExceptionInterface;
@ -76,15 +77,15 @@ class ArtificialIntelligence implements IDelegatedSettings {
}
$textProcessingProviders = [];
/** @var array<class-string<ITaskType>, class-string<IProvider>> $textProcessingSettings */
/** @var array<class-string<ITaskType>, string|class-string<IProvider>> $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) {

@ -692,6 +692,7 @@ return array(
'OCP\\TextProcessing\\IManager' => $baseDir . '/lib/public/TextProcessing/IManager.php',
'OCP\\TextProcessing\\IProvider' => $baseDir . '/lib/public/TextProcessing/IProvider.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',

@ -725,6 +725,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\TextProcessing\\IManager' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IManager.php',
'OCP\\TextProcessing\\IProvider' => __DIR__ . '/../../..' . '/lib/public/TextProcessing/IProvider.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',

@ -40,6 +40,7 @@ use OCP\TextProcessing\Exception\TaskFailureException;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithExpectedRuntime;
use OCP\TextProcessing\IProviderWithId;
use OCP\TextProcessing\Task;
use OCP\TextProcessing\Task as OCPTask;
use Psr\Log\LoggerInterface;
@ -282,7 +283,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_values(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()])));
$provider = current(array_values(array_filter($providers, function ($provider) use ($preferences, $task) {
if ($provider instanceof IProviderWithId) {
return $provider->getId() === $preferences[$task->getType()];
}
$provider::class === $preferences[$task->getType()];
})));
if ($provider !== false) {
$providers = array_filter($providers, fn ($p) => $p !== $provider);
array_unshift($providers, $provider);

@ -50,7 +50,7 @@ interface IManager {
public function getProviders(): array;
/**
* @return class-string<ITaskType>[]
* @return string[]
* @since 27.1.0
*/
public function getAvailableTaskTypes(): array;

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
*
* @author Marcel Klehr <mklehr@gmx.net>
*
* @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 <http://www.gnu.org/licenses/>.
*/
namespace OCP\TextProcessing;
/**
* @since 28.0.0
* @extends IProvider<T>
* @template T of ITaskType
*/
interface IProviderWithId extends IProvider {
/**
* The id of this provider
* @since 28.0.0
*/
public function getId(): string;
}
Loading…
Cancel
Save