diff options
Diffstat (limited to 'lib/public/TextProcessing')
-rw-r--r-- | lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php | 35 | ||||
-rw-r--r-- | lib/public/TextProcessing/Events/TaskFailedEvent.php | 35 | ||||
-rw-r--r-- | lib/public/TextProcessing/Events/TaskSuccessfulEvent.php | 14 | ||||
-rw-r--r-- | lib/public/TextProcessing/Exception/TaskFailureException.php | 15 | ||||
-rw-r--r-- | lib/public/TextProcessing/FreePromptTaskType.php | 51 | ||||
-rw-r--r-- | lib/public/TextProcessing/HeadlineTaskType.php | 51 | ||||
-rw-r--r-- | lib/public/TextProcessing/IManager.php | 112 | ||||
-rw-r--r-- | lib/public/TextProcessing/IProvider.php | 47 | ||||
-rw-r--r-- | lib/public/TextProcessing/IProviderWithExpectedRuntime.php | 26 | ||||
-rw-r--r-- | lib/public/TextProcessing/IProviderWithId.php | 24 | ||||
-rw-r--r-- | lib/public/TextProcessing/IProviderWithUserId.php | 26 | ||||
-rw-r--r-- | lib/public/TextProcessing/ITaskType.php | 34 | ||||
-rw-r--r-- | lib/public/TextProcessing/SummaryTaskType.php | 51 | ||||
-rw-r--r-- | lib/public/TextProcessing/Task.php | 226 | ||||
-rw-r--r-- | lib/public/TextProcessing/TopicsTaskType.php | 51 |
15 files changed, 798 insertions, 0 deletions
diff --git a/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php b/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php new file mode 100644 index 00000000000..8b944254020 --- /dev/null +++ b/lib/public/TextProcessing/Events/AbstractTextProcessingEvent.php @@ -0,0 +1,35 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCP\TextProcessing\Events; + +use OCP\EventDispatcher\Event; +use OCP\TextProcessing\Task; + +/** + * @since 27.1.0 + * @deprecated 30.0.0 + */ +abstract class AbstractTextProcessingEvent extends Event { + /** + * @since 27.1.0 + */ + public function __construct( + private Task $task, + ) { + parent::__construct(); + } + + /** + * @return Task + * @since 27.1.0 + */ + public function getTask(): Task { + return $this->task; + } +} diff --git a/lib/public/TextProcessing/Events/TaskFailedEvent.php b/lib/public/TextProcessing/Events/TaskFailedEvent.php new file mode 100644 index 00000000000..dfdb9137f95 --- /dev/null +++ b/lib/public/TextProcessing/Events/TaskFailedEvent.php @@ -0,0 +1,35 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCP\TextProcessing\Events; + +use OCP\TextProcessing\Task; + +/** + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class TaskFailedEvent extends AbstractTextProcessingEvent { + /** + * @param Task $task + * @param string $errorMessage + * @since 27.1.0 + */ + public function __construct( + Task $task, + private string $errorMessage, + ) { + parent::__construct($task); + } + + /** + * @return string + * @since 27.1.0 + */ + public function getErrorMessage(): string { + return $this->errorMessage; + } +} diff --git a/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php b/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php new file mode 100644 index 00000000000..0716f4d45c6 --- /dev/null +++ b/lib/public/TextProcessing/Events/TaskSuccessfulEvent.php @@ -0,0 +1,14 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCP\TextProcessing\Events; + +/** + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class TaskSuccessfulEvent extends AbstractTextProcessingEvent { +} diff --git a/lib/public/TextProcessing/Exception/TaskFailureException.php b/lib/public/TextProcessing/Exception/TaskFailureException.php new file mode 100644 index 00000000000..06fbdf5e765 --- /dev/null +++ b/lib/public/TextProcessing/Exception/TaskFailureException.php @@ -0,0 +1,15 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCP\TextProcessing\Exception; + +/** + * Exception thrown when a task failed + * @since 28.0.0 + * @deprecated 30.0.0 + */ +class TaskFailureException extends \RuntimeException { +} diff --git a/lib/public/TextProcessing/FreePromptTaskType.php b/lib/public/TextProcessing/FreePromptTaskType.php new file mode 100644 index 00000000000..2433f24dbd7 --- /dev/null +++ b/lib/public/TextProcessing/FreePromptTaskType.php @@ -0,0 +1,51 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +use OCP\IL10N; +use OCP\L10N\IFactory; + +/** + * This is the text processing task type for free prompting + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class FreePromptTaskType implements ITaskType { + private IL10N $l; + + /** + * Constructor for FreePromptTaskType + * + * @param IFactory $l10nFactory + * @since 27.1.0 + */ + public function __construct( + IFactory $l10nFactory, + ) { + $this->l = $l10nFactory->get('core'); + } + + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getName(): string { + return $this->l->t('Free prompt'); + } + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getDescription(): string { + return $this->l->t('Runs an arbitrary prompt through the language model.'); + } +} diff --git a/lib/public/TextProcessing/HeadlineTaskType.php b/lib/public/TextProcessing/HeadlineTaskType.php new file mode 100644 index 00000000000..00eb66466ae --- /dev/null +++ b/lib/public/TextProcessing/HeadlineTaskType.php @@ -0,0 +1,51 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +use OCP\IL10N; +use OCP\L10N\IFactory; + +/** + * This is the text processing task type for creating headline + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class HeadlineTaskType implements ITaskType { + private IL10N $l; + + /** + * Constructor for HeadlineTaskType + * + * @param IFactory $l10nFactory + * @since 27.1.0 + */ + public function __construct( + IFactory $l10nFactory, + ) { + $this->l = $l10nFactory->get('core'); + } + + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getName(): string { + return $this->l->t('Generate headline'); + } + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getDescription(): string { + return $this->l->t('Generates a possible headline for a text.'); + } +} diff --git a/lib/public/TextProcessing/IManager.php b/lib/public/TextProcessing/IManager.php new file mode 100644 index 00000000000..701ad18940f --- /dev/null +++ b/lib/public/TextProcessing/IManager.php @@ -0,0 +1,112 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\TextProcessing; + +use OCP\Common\Exception\NotFoundException; +use OCP\DB\Exception; +use OCP\PreConditionNotMetException; +use OCP\TextProcessing\Exception\TaskFailureException; +use RuntimeException; + +/** + * API surface for apps interacting with and making use of LanguageModel providers + * without known which providers are installed + * @since 27.1.0 + * @deprecated 30.0.0 + */ +interface IManager { + /** + * @since 27.1.0 + */ + public function hasProviders(): bool; + + /** + * @return IProvider[] + * @since 27.1.0 + */ + public function getProviders(): array; + + /** + * @return string[] + * @since 27.1.0 + */ + public function getAvailableTaskTypes(): array; + + /** + * @param Task $task The task to run + * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called + * @throws TaskFailureException If running the task failed + * @since 27.1.0 + */ + public function runTask(Task $task): string; + + /** + * Will schedule an LLM inference process in the background. The result will become available + * with the \OCP\LanguageModel\Events\TaskSuccessfulEvent + * If inference fails a \OCP\LanguageModel\Events\TaskFailedEvent will be dispatched instead + * + * @param Task $task The task to schedule + * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called + * @throws Exception storing the task in the database failed + * @since 27.1.0 + */ + public function scheduleTask(Task $task) : void; + + /** + * If the designated provider for the passed task provides an expected average runtime, we check if the runtime fits into the + * max execution time of this php process and run it synchronously if it does, if it doesn't fit (or the provider doesn't provide that information) + * execution is deferred to a background job + * + * @param Task $task The task to schedule + * @returns bool A boolean indicating whether the task was run synchronously (`true`) or offloaded to a background job (`false`) + * @throws PreConditionNotMetException If no or not the requested provider was registered but this method was still called + * @throws TaskFailureException If running the task failed + * @throws Exception storing the task in the database failed + * @since 28.0.0 + */ + public function runOrScheduleTask(Task $task): bool; + + /** + * Delete a task that has been scheduled before + * + * @param Task $task The task to delete + * @since 27.1.0 + */ + public function deleteTask(Task $task): void; + + /** + * @param int $id The id of the task + * @return Task + * @throws RuntimeException If the query failed + * @throws NotFoundException If the task could not be found + * @since 27.1.0 + */ + public function getTask(int $id): Task; + + /** + * @param int $id The id of the task + * @param string|null $userId The user id that scheduled the task + * @return Task + * @throws RuntimeException If the query failed + * @throws NotFoundException If the task could not be found + * @since 27.1.0 + */ + public function getUserTask(int $id, ?string $userId): Task; + + /** + * @param string $userId + * @param string $appId + * @param string|null $identifier + * @return array + * @since 27.1.0 + */ + public function getUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array; +} diff --git a/lib/public/TextProcessing/IProvider.php b/lib/public/TextProcessing/IProvider.php new file mode 100644 index 00000000000..24efbc1ec8c --- /dev/null +++ b/lib/public/TextProcessing/IProvider.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\TextProcessing; + +use RuntimeException; + +/** + * This is the interface that is implemented by apps that + * implement a text processing provider + * @psalm-template-covariant T of ITaskType + * @since 27.1.0 + * @deprecated 30.0.0 + */ +interface IProvider { + /** + * The localized name of this provider + * @since 27.1.0 + */ + public function getName(): string; + + /** + * Processes a text + * + * @param string $prompt The input text + * @return string the output text + * @since 27.1.0 + * @throws RuntimeException If the text could not be processed + */ + public function process(string $prompt): string; + + /** + * Returns the task type class string of the task type, that this + * provider handles + * + * @since 27.1.0 + * @return class-string<T> + */ + public function getTaskType(): string; +} diff --git a/lib/public/TextProcessing/IProviderWithExpectedRuntime.php b/lib/public/TextProcessing/IProviderWithExpectedRuntime.php new file mode 100644 index 00000000000..b3986e8b3a8 --- /dev/null +++ b/lib/public/TextProcessing/IProviderWithExpectedRuntime.php @@ -0,0 +1,26 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\TextProcessing; + +/** + * This interface allows the system to learn the provider's expected runtime + * @since 28.0.0 + * @template T of ITaskType + * @template-extends IProvider<T> + * @deprecated 30.0.0 + */ +interface IProviderWithExpectedRuntime extends IProvider { + /** + * @return int The expected average runtime of a task in seconds + * @since 28.0.0 + */ + public function getExpectedRuntime(): int; +} diff --git a/lib/public/TextProcessing/IProviderWithId.php b/lib/public/TextProcessing/IProviderWithId.php new file mode 100644 index 00000000000..359ec9cef71 --- /dev/null +++ b/lib/public/TextProcessing/IProviderWithId.php @@ -0,0 +1,24 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +/** + * @since 28.0.0 + * @extends IProvider<T> + * @template T of ITaskType + * @deprecated 30.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/IProviderWithUserId.php b/lib/public/TextProcessing/IProviderWithUserId.php new file mode 100644 index 00000000000..197dd3d9740 --- /dev/null +++ b/lib/public/TextProcessing/IProviderWithUserId.php @@ -0,0 +1,26 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\TextProcessing; + +/** + * This interface allows providers to access the user that initiated the task being run. + * @since 28.0.0 + * @template T of ITaskType + * @template-extends IProvider<T> + * @deprecated 30.0.0 + */ +interface IProviderWithUserId extends IProvider { + /** + * @param ?string $userId the current user's id + * @since 28.0.0 + */ + public function setUserId(?string $userId): void; +} diff --git a/lib/public/TextProcessing/ITaskType.php b/lib/public/TextProcessing/ITaskType.php new file mode 100644 index 00000000000..5ec1e8dd049 --- /dev/null +++ b/lib/public/TextProcessing/ITaskType.php @@ -0,0 +1,34 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +/** + * This is a task type interface that is implemented by text processing + * task types + * @since 27.1.0 + * @deprecated 30.0.0 + */ +interface ITaskType { + /** + * Returns the localized name of this task type + * + * @since 27.1.0 + * @return string + */ + public function getName(): string; + + /** + * Returns the localized description of this task type + * + * @since 27.1.0 + * @return string + */ + public function getDescription(): string; +} diff --git a/lib/public/TextProcessing/SummaryTaskType.php b/lib/public/TextProcessing/SummaryTaskType.php new file mode 100644 index 00000000000..656a50ebc62 --- /dev/null +++ b/lib/public/TextProcessing/SummaryTaskType.php @@ -0,0 +1,51 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +use OCP\IL10N; +use OCP\L10N\IFactory; + +/** + * This is the text processing task type for summaries + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class SummaryTaskType implements ITaskType { + private IL10N $l; + + /** + * Constructor for SummaryTaskType + * + * @param IFactory $l10nFactory + * @since 27.1.0 + */ + public function __construct( + IFactory $l10nFactory, + ) { + $this->l = $l10nFactory->get('core'); + } + + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getName(): string { + return $this->l->t('Summarize'); + } + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getDescription(): string { + return $this->l->t('Summarizes text by reducing its length without losing key information.'); + } +} diff --git a/lib/public/TextProcessing/Task.php b/lib/public/TextProcessing/Task.php new file mode 100644 index 00000000000..d968ad9ba3e --- /dev/null +++ b/lib/public/TextProcessing/Task.php @@ -0,0 +1,226 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +/** + * This is a text processing task + * @since 27.1.0 + * @psalm-template-covariant T of ITaskType + * @deprecated 30.0.0 + */ +final class Task implements \JsonSerializable { + protected ?int $id = null; + protected ?string $output = null; + private ?\DateTime $completionExpectedAt = null; + + /** + * @since 27.1.0 + */ + public const TYPES = [ + FreePromptTaskType::class, + SummaryTaskType::class, + HeadlineTaskType::class, + TopicsTaskType::class, + ]; + + /** + * @since 27.1.0 + */ + public const STATUS_FAILED = 4; + /** + * @since 27.1.0 + */ + public const STATUS_SUCCESSFUL = 3; + /** + * @since 27.1.0 + */ + public const STATUS_RUNNING = 2; + /** + * @since 27.1.0 + */ + public const STATUS_SCHEDULED = 1; + /** + * @since 27.1.0 + */ + public const STATUS_UNKNOWN = 0; + + /** + * @psalm-var self::STATUS_* + */ + protected int $status = self::STATUS_UNKNOWN; + + /** + * @psalm-param class-string<T> $type + * @param string $type + * @param string $input + * @param string $appId + * @param string|null $userId + * @param string $identifier An arbitrary identifier for this task. max length: 255 chars + * @since 27.1.0 + */ + final public function __construct( + protected string $type, + protected string $input, + protected string $appId, + protected ?string $userId, + protected string $identifier = '', + ) { + } + + /** + * @psalm-param IProvider<T> $provider + * @param IProvider $provider + * @return string + * @since 27.1.0 + */ + public function visitProvider(IProvider $provider): string { + if ($this->canUseProvider($provider)) { + if ($provider instanceof IProviderWithUserId) { + $provider->setUserId($this->getUserId()); + } + return $provider->process($this->getInput()); + } else { + throw new \RuntimeException('Task of type ' . $this->getType() . ' cannot visit provider with task type ' . $provider->getTaskType()); + } + } + + /** + * @psalm-param IProvider<T> $provider + * @param IProvider $provider + * @return bool + * @since 27.1.0 + */ + public function canUseProvider(IProvider $provider): bool { + return $provider->getTaskType() === $this->getType(); + } + + /** + * @psalm-return class-string<T> + * @since 27.1.0 + */ + final public function getType(): string { + return $this->type; + } + + /** + * @return string|null + * @since 27.1.0 + */ + final public function getOutput(): ?string { + return $this->output; + } + + /** + * @param string|null $output + * @since 27.1.0 + */ + final public function setOutput(?string $output): void { + $this->output = $output; + } + + /** + * @psalm-return self::STATUS_* + * @since 27.1.0 + */ + final public function getStatus(): int { + return $this->status; + } + + /** + * @psalm-param self::STATUS_* $status + * @since 27.1.0 + */ + final public function setStatus(int $status): void { + $this->status = $status; + } + + /** + * @return int|null + * @since 27.1.0 + */ + final public function getId(): ?int { + return $this->id; + } + + /** + * @param int|null $id + * @since 27.1.0 + */ + final public function setId(?int $id): void { + $this->id = $id; + } + + /** + * @return string + * @since 27.1.0 + */ + final public function getInput(): string { + return $this->input; + } + + /** + * @return string + * @since 27.1.0 + */ + final public function getAppId(): string { + return $this->appId; + } + + /** + * @return string + * @since 27.1.0 + */ + final public function getIdentifier(): string { + return $this->identifier; + } + + /** + * @return string|null + * @since 27.1.0 + */ + final public function getUserId(): ?string { + return $this->userId; + } + + /** + * @psalm-return array{id: ?int, type: class-string<T>, status: 0|1|2|3|4, userId: ?string, appId: string, input: string, output: ?string, identifier: string, completionExpectedAt: ?int} + * @since 27.1.0 + */ + public function jsonSerialize(): array { + return [ + 'id' => $this->getId(), + 'type' => $this->getType(), + 'status' => $this->getStatus(), + 'userId' => $this->getUserId(), + 'appId' => $this->getAppId(), + 'input' => $this->getInput(), + 'output' => $this->getOutput(), + 'identifier' => $this->getIdentifier(), + 'completionExpectedAt' => $this->getCompletionExpectedAt()?->getTimestamp(), + ]; + } + + /** + * @param null|\DateTime $completionExpectedAt + * @return void + * @since 28.0.0 + */ + final public function setCompletionExpectedAt(?\DateTime $completionExpectedAt): void { + $this->completionExpectedAt = $completionExpectedAt; + } + + /** + * @return \DateTime|null + * @since 28.0.0 + */ + final public function getCompletionExpectedAt(): ?\DateTime { + return $this->completionExpectedAt; + } +} diff --git a/lib/public/TextProcessing/TopicsTaskType.php b/lib/public/TextProcessing/TopicsTaskType.php new file mode 100644 index 00000000000..c693ee3d27f --- /dev/null +++ b/lib/public/TextProcessing/TopicsTaskType.php @@ -0,0 +1,51 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\TextProcessing; + +use OCP\IL10N; +use OCP\L10N\IFactory; + +/** + * This is the text processing task type for topics extraction + * @since 27.1.0 + * @deprecated 30.0.0 + */ +class TopicsTaskType implements ITaskType { + private IL10N $l; + + /** + * Constructor for TopicsTaskType + * + * @param IFactory $l10nFactory + * @since 27.1.0 + */ + public function __construct( + IFactory $l10nFactory, + ) { + $this->l = $l10nFactory->get('core'); + } + + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getName(): string { + return $this->l->t('Extract topics'); + } + + /** + * @inheritDoc + * @since 27.1.0 + */ + public function getDescription(): string { + return $this->l->t('Extracts topics from a text and outputs them separated by commas.'); + } +} |