diff options
author | Julien Veyssier <julien-nc@posteo.net> | 2024-08-28 11:50:23 +0200 |
---|---|---|
committer | Julien Veyssier <julien-nc@posteo.net> | 2024-08-30 10:07:01 +0200 |
commit | 5ab0866341ac21f732117833e41b7deb12971b08 (patch) | |
tree | e211d35ebe5b83e91b158956e2275d8c6cd173e8 /lib/private/TextProcessing | |
parent | 1cc7851209ce378867805c037eaaacfc50c02550 (diff) | |
download | nextcloud-server-5ab0866341ac21f732117833e41b7deb12971b08.tar.gz nextcloud-server-5ab0866341ac21f732117833e41b7deb12971b08.zip |
feat(textprocessing): TextProcessingManager::runTask calls TaskProcessingManager::runTask
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
Diffstat (limited to 'lib/private/TextProcessing')
-rw-r--r-- | lib/private/TextProcessing/Manager.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index 4cc5ff77527..c73a6a9ee37 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -20,13 +20,22 @@ use OCP\DB\Exception; use OCP\IConfig; use OCP\IServerContainer; use OCP\PreConditionNotMetException; +use OCP\TaskProcessing\IManager as TaskProcessingIManager; +use OCP\TaskProcessing\TaskTypes\TextToText; +use OCP\TaskProcessing\TaskTypes\TextToTextHeadline; +use OCP\TaskProcessing\TaskTypes\TextToTextSummary; +use OCP\TaskProcessing\TaskTypes\TextToTextTopics; use OCP\TextProcessing\Exception\TaskFailureException; +use OCP\TextProcessing\FreePromptTaskType; +use OCP\TextProcessing\HeadlineTaskType; use OCP\TextProcessing\IManager; use OCP\TextProcessing\IProvider; use OCP\TextProcessing\IProviderWithExpectedRuntime; use OCP\TextProcessing\IProviderWithId; +use OCP\TextProcessing\SummaryTaskType; use OCP\TextProcessing\Task; use OCP\TextProcessing\Task as OCPTask; +use OCP\TextProcessing\TopicsTaskType; use Psr\Log\LoggerInterface; use RuntimeException; use Throwable; @@ -42,6 +51,7 @@ class Manager implements IManager { private IJobList $jobList, private TaskMapper $taskMapper, private IConfig $config, + private TaskProcessingIManager $taskProcessingManager, ) { } @@ -98,6 +108,52 @@ class Manager implements IManager { * @inheritDoc */ public function runTask(OCPTask $task): string { + // try to run a task processing task if possible + $taskTypeClass = $task->getType(); + $taskProcessingCompatibleTaskTypes = [ + FreePromptTaskType::class => TextToText::ID, + HeadlineTaskType::class => TextToTextHeadline::ID, + SummaryTaskType::class => TextToTextSummary::ID, + TopicsTaskType::class => TextToTextTopics::ID, + ]; + if (isset($taskProcessingCompatibleTaskTypes[$taskTypeClass])) { + try { + $taskProcessingTaskTypeId = $taskProcessingCompatibleTaskTypes[$taskTypeClass]; + $taskProcessingTask = new \OCP\TaskProcessing\Task( + $taskProcessingTaskTypeId, + ['input' => $task->getInput()], + $task->getAppId(), + $task->getUserId(), + $task->getIdentifier(), + ); + + $task->setStatus(OCPTask::STATUS_RUNNING); + if ($task->getId() === null) { + $taskEntity = $this->taskMapper->insert(DbTask::fromPublicTask($task)); + $task->setId($taskEntity->getId()); + } else { + $this->taskMapper->update(DbTask::fromPublicTask($task)); + } + $this->logger->debug('Running a TextProcessing (' . $taskTypeClass . ') task with TaskProcessing'); + $taskProcessingResultTask = $this->taskProcessingManager->runTask($taskProcessingTask); + if ($taskProcessingResultTask->getStatus() === \OCP\TaskProcessing\Task::STATUS_SUCCESSFUL) { + $task->setOutput($taskProcessingResultTask->getOutput()['output'] ?? ''); + $task->setStatus(OCPTask::STATUS_SUCCESSFUL); + $this->taskMapper->update(DbTask::fromPublicTask($task)); + return $task->getOutput(); + } + } catch (\Throwable $e) { + $this->logger->error('TextProcessing to TaskProcessing failed', ['exception' => $e]); + $task->setStatus(OCPTask::STATUS_FAILED); + $this->taskMapper->update(DbTask::fromPublicTask($task)); + throw new TaskFailureException('TextProcessing to TaskProcessing failed: ' . $e->getMessage(), 0, $e); + } + $task->setStatus(OCPTask::STATUS_FAILED); + $this->taskMapper->update(DbTask::fromPublicTask($task)); + throw new TaskFailureException('Could not run task'); + } + + // try to run the text processing task if (!$this->canHandleTask($task)) { throw new PreConditionNotMetException('No text processing provider is installed that can handle this task'); } |