|
| 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 OCA\Richdocuments\TemplateManager; |
| 11 | +use OCP\IConfig; |
| 12 | +use OCP\TaskProcessing\Exception\Exception; |
| 13 | +use OCP\TaskProcessing\Exception\NotFoundException; |
| 14 | +use OCP\TaskProcessing\Exception\PreConditionNotMetException; |
| 15 | +use OCP\TaskProcessing\Exception\UnauthorizedException; |
| 16 | +use OCP\TaskProcessing\Exception\ValidationException; |
| 17 | +use OCP\TaskProcessing\IManager; |
| 18 | +use OCP\TaskProcessing\Task; |
| 19 | +use OCP\TaskProcessing\TaskTypes\TextToText; |
| 20 | +use RuntimeException; |
| 21 | + |
| 22 | +class SlideDeckService { |
| 23 | + public const PROMPT = <<<EOF |
| 24 | +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: |
| 25 | +
|
| 26 | +``` |
| 27 | +[{"headline": "Headline 1", points: ["Bullet point 1", "Bullet point 2"]}, {"headline": "Headline 2", points: ["Bullet point 1", "Bullet point 2"]}] |
| 28 | +``` |
| 29 | +
|
| 30 | +Here is the presentation text: |
| 31 | +EOF; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + private IManager $taskProcessingManager, |
| 35 | + private TemplateManager $templateManager, |
| 36 | + private RemoteService $remoteService, |
| 37 | + private IConfig $config, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function generateSlideDeck(?string $userId, string $presentationText) { |
| 42 | + $rawModelOutput = $this->runLLMQuery($userId, $presentationText); |
| 43 | + |
| 44 | + $ooxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml'; |
| 45 | + $format = $ooxml ? 'pptx' : 'odp'; |
| 46 | + $emptyPresentation = $this->getBlankPresentation($format); |
| 47 | + |
| 48 | + try { |
| 49 | + $parsedStructure = $this->parseModelJSON($rawModelOutput); |
| 50 | + } catch (\JsonException) { |
| 51 | + throw new RuntimeException('LLM generated faulty JSON data'); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + $transformedPresentation = $this->remoteService->transformDocumentStructure( |
| 56 | + 'presentation.' . $format, |
| 57 | + $emptyPresentation, |
| 58 | + $parsedStructure |
| 59 | + ); |
| 60 | + |
| 61 | + return $transformedPresentation; |
| 62 | + } catch (\Exception) { |
| 63 | + throw new RuntimeException('Unable to apply transformations to presentation file'); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Parses the JSON output from the LLM into |
| 69 | + * JSON that Collabora expects |
| 70 | + * |
| 71 | + * @param string $jsonString |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + private function parseModelJSON(string $jsonString): array { |
| 75 | + $modelJSON = json_decode( |
| 76 | + $jsonString, |
| 77 | + associative: true, |
| 78 | + flags: JSON_THROW_ON_ERROR |
| 79 | + ); |
| 80 | + |
| 81 | + $slideCommands = []; |
| 82 | + foreach ($modelJSON as $index => $slide) { |
| 83 | + if (count($slideCommands) > 0) { |
| 84 | + $slideCommands[] = [ 'JumpToSlide' => 'last' ]; |
| 85 | + $slideCommands[] = [ 'InsertMasterSlide' => 0 ]; |
| 86 | + } else { |
| 87 | + $slideCommands[] = [ 'JumpToSlide' => $index]; |
| 88 | + } |
| 89 | + |
| 90 | + $slideCommands[] = [ 'ChangeLayoutByName' => 'AUTOLAYOUT_TITLE_CONTENT' ]; |
| 91 | + $slideCommands[] = [ 'SetText.0' => $slide['headline'] ]; |
| 92 | + |
| 93 | + $editTextObjectCommands = [ |
| 94 | + [ 'SelectParagraph' => 0 ], |
| 95 | + [ 'InsertText' => implode(PHP_EOL, $slide['points']) ], |
| 96 | + ]; |
| 97 | + |
| 98 | + $slideCommands[] = [ 'EditTextObject.1' => $editTextObjectCommands ]; |
| 99 | + } |
| 100 | + |
| 101 | + return [ 'SlideCommands' => $slideCommands ]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates a blank presentation file in memory |
| 106 | + * |
| 107 | + * @param string $format |
| 108 | + * @return resource |
| 109 | + */ |
| 110 | + private function getBlankPresentation(string $format) { |
| 111 | + $emptyPresentationContent = $this->templateManager->getEmptyFileContent($format); |
| 112 | + $memoryStream = fopen('php://memory', 'r+'); |
| 113 | + |
| 114 | + if (!$memoryStream) { |
| 115 | + throw new RuntimeException('Unable to open file stream'); |
| 116 | + } |
| 117 | + |
| 118 | + fwrite($memoryStream, $emptyPresentationContent); |
| 119 | + rewind($memoryStream); |
| 120 | + |
| 121 | + return $memoryStream; |
| 122 | + } |
| 123 | + |
| 124 | + private function runLLMQuery(?string $userId, string $presentationText) { |
| 125 | + $prompt = self::PROMPT; |
| 126 | + $task = new Task( |
| 127 | + TextToText::ID, |
| 128 | + ['input' => $prompt . "\n\n" . $presentationText], |
| 129 | + Application::APPNAME, |
| 130 | + $userId |
| 131 | + ); |
| 132 | + |
| 133 | + try { |
| 134 | + $this->taskProcessingManager->scheduleTask($task); |
| 135 | + } catch (PreConditionNotMetException|UnauthorizedException|ValidationException|Exception $e) { |
| 136 | + throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
| 137 | + } |
| 138 | + |
| 139 | + while (true) { |
| 140 | + try { |
| 141 | + $task = $this->taskProcessingManager->getTask($task->getId()); |
| 142 | + } catch (NotFoundException|Exception $e) { |
| 143 | + throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
| 144 | + } |
| 145 | + if (in_array($task->getStatus(), [Task::STATUS_SUCCESSFUL, Task::STATUS_FAILED, Task::STATUS_CANCELLED])) { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if ($task->getStatus() !== Task::STATUS_SUCCESSFUL) { |
| 151 | + throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' failed or was cancelled'); |
| 152 | + } |
| 153 | + |
| 154 | + return $task->getOutput()['output']; |
| 155 | + } |
| 156 | +} |
0 commit comments