Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(TextProcessing): Allow providers and task types to declare a dynamic ID instead of using className #41088

Merged
merged 9 commits into from
Dec 19, 2023
8 changes: 7 additions & 1 deletion lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -95,12 +96,12 @@
/**
* @inheritDoc
*/
public function getAvailableTaskTypes(): array {

Check failure on line 99 in lib/private/TextProcessing/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MoreSpecificReturnType

lib/private/TextProcessing/Manager.php:99:43: MoreSpecificReturnType: The declared return type 'array<array-key, class-string<OCP\TextProcessing\ITaskType>>' for OC\TextProcessing\Manager::getAvailableTaskTypes is more specific than the inferred return type 'list<string>' (see https://psalm.dev/070)
$tasks = [];
foreach ($this->getProviders() as $provider) {
$tasks[$provider->getTaskType()] = true;
}
return array_keys($tasks);

Check failure on line 104 in lib/private/TextProcessing/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

LessSpecificReturnStatement

lib/private/TextProcessing/Manager.php:104:10: LessSpecificReturnStatement: The type 'list<string>' is more general than the declared return type 'array<array-key, class-string<OCP\TextProcessing\ITaskType>>' for OC\TextProcessing\Manager::getAvailableTaskTypes (see https://psalm.dev/129)
}

public function canHandleTask(OCPTask $task): bool {
Expand All @@ -120,7 +121,12 @@
$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);
Expand Down
2 changes: 1 addition & 1 deletion lib/public/TextProcessing/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function process(string $prompt): string;
* provider handles
*
* @since 27.1.0
* @return class-string<T>
* @return class-string<T>|string
*/
public function getTaskType(): string;
}
39 changes: 39 additions & 0 deletions lib/public/TextProcessing/IProviderWithId.php
Original file line number Diff line number Diff line change
@@ -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
*/
interface IProviderWithId extends IProvider {

Check failure on line 32 in lib/public/TextProcessing/IProviderWithId.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

lib/public/TextProcessing/IProviderWithId.php:32:35: MissingTemplateParam: OCP\TextProcessing\IProviderWithId has missing template params when extending OCP\TextProcessing\IProvider, expecting 1 (see https://psalm.dev/182)

Check failure on line 32 in lib/public/TextProcessing/IProviderWithId.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

MissingTemplateParam

lib/public/TextProcessing/IProviderWithId.php:32:35: MissingTemplateParam: OCP\TextProcessing\IProviderWithId has missing template params when extending OCP\TextProcessing\IProvider, expecting 1 (see https://psalm.dev/182)
Fixed Show fixed Hide fixed

/**
* The id of this provider
* @since 28.0.0
*/
public function getId(): string;
}
37 changes: 37 additions & 0 deletions lib/public/TextProcessing/ITaskTypeWithId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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
*/
interface ITaskTypeWithId {
/**
* The id of this provider
* @since 28.0.0
*/
public function getId(): string;
}
4 changes: 2 additions & 2 deletions lib/public/TextProcessing/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* This is a text processing task
* @since 27.1.0
* @psalm-template T of ITaskType
* @psalm-template S as class-string<T>
* @psalm-template P as IProvider<T>
* @psalm-template S as class-string<T> | string
* @psalm-template P as IProvider<T> | IProvider
*/
final class Task implements \JsonSerializable {
protected ?int $id = null;
Expand Down
Loading