|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +namespace OCA\Richdocuments\Service; |
| 8 | + |
| 9 | +use OCA\Richdocuments\AppInfo\Application; |
| 10 | +use OCP\TaskProcessing\Exception\Exception; |
| 11 | +use OCP\TaskProcessing\Exception\NotFoundException; |
| 12 | +use OCP\TaskProcessing\Exception\PreConditionNotMetException; |
| 13 | +use OCP\TaskProcessing\Exception\UnauthorizedException; |
| 14 | +use OCP\TaskProcessing\Exception\ValidationException; |
| 15 | +use OCP\TaskProcessing\IManager; |
| 16 | +use OCP\TaskProcessing\Task; |
| 17 | +use OCP\TaskProcessing\TaskTypes\TextToText; |
| 18 | +use RuntimeException; |
| 19 | + |
| 20 | +class SlideDeckService { |
| 21 | + public const PROMPT = <<<EOF |
| 22 | +Draft a presentation slide deck with headlines and a maximum of 5 bullet points per headline. Use the following JSON structure for your whole output and output only the JSON array, no introductory text: |
| 23 | +
|
| 24 | +``` |
| 25 | +[{"headline": "Headline 1", points: ["Bullet point 1", "Bullet point 2"]}, {"headline": "Headline 2", points: ["Bullet point 1", "Bullet point 2"]}] |
| 26 | +``` |
| 27 | +
|
| 28 | +Here is the presentation text: |
| 29 | +EOF; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private IManager $taskProcessingManager, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function generateSlideDeck(?string $userId, string $presentationText) { |
| 37 | + $prompt = self::PROMPT; |
| 38 | + $task = new Task( |
| 39 | + TextToText::ID, |
| 40 | + ['input' => $prompt . "\n\n" . $presentationText], |
| 41 | + Application::APPNAME, |
| 42 | + $userId |
| 43 | + ); |
| 44 | + try { |
| 45 | + $this->taskProcessingManager->scheduleTask($task); |
| 46 | + } catch (PreConditionNotMetException|UnauthorizedException|ValidationException|Exception $e) { |
| 47 | + throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
| 48 | + } |
| 49 | + while (true) { |
| 50 | + try { |
| 51 | + $task = $this->taskProcessingManager->getTask($task->getId()); |
| 52 | + } catch (NotFoundException|Exception $e) { |
| 53 | + throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
| 54 | + } |
| 55 | + if (in_array($task->getStatus(), [Task::STATUS_SUCCESSFUL, Task::STATUS_FAILED, Task::STATUS_CANCELLED])) { |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + if ($task->getStatus() !== Task::STATUS_SUCCESSFUL) { |
| 60 | + throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' failed or was cancelled'); |
| 61 | + } |
| 62 | + |
| 63 | + $output = $task->getOutput(); |
| 64 | + if (isset($output['output'])) { |
| 65 | + throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' does not have output key'); |
| 66 | + } |
| 67 | + |
| 68 | + $headlines = json_decode($output['output'], associative: true); |
| 69 | + |
| 70 | + return $output['output']; |
| 71 | + } |
| 72 | +} |
0 commit comments