Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use OCA\Richdocuments\Preview\OpenDocument;
use OCA\Richdocuments\Preview\Pdf;
use OCA\Richdocuments\Reference\OfficeTargetReferenceProvider;
use OCA\Richdocuments\TaskProcessing\SlideDeckGenerationProvider;
use OCA\Richdocuments\TaskProcessing\SlideDeckGenerationTaskType;
use OCA\Richdocuments\TaskProcessing\TextToDocumentProvider;
use OCA\Richdocuments\TaskProcessing\TextToDocumentTaskType;
use OCA\Richdocuments\TaskProcessing\TextToSpreadsheetProvider;
Expand Down Expand Up @@ -88,10 +90,13 @@ public function register(IRegistrationContext $context): void {
$context->registerPreviewProvider(Pdf::class, Pdf::MIMETYPE_REGEX);
$context->registerFileConversionProvider(ConversionProvider::class);
$context->registerNotifierService(Notifier::class);

$context->registerTaskProcessingTaskType(TextToDocumentTaskType::class);
$context->registerTaskProcessingProvider(TextToDocumentProvider::class);
$context->registerTaskProcessingTaskType(TextToSpreadsheetTaskType::class);
$context->registerTaskProcessingProvider(TextToSpreadsheetProvider::class);
$context->registerTaskProcessingProvider(SlideDeckGenerationProvider::class);
$context->registerTaskProcessingTaskType(SlideDeckGenerationTaskType::class);
}

public function boot(IBootContext $context): void {
Expand Down
99 changes: 99 additions & 0 deletions lib/Service/RemoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class RemoteService {
public function __construct(
private AppConfig $appConfig,
private IClientService $clientService,
private CapabilitiesService $capabilitiesService,
private LoggerInterface $logger,
) {
}
Expand Down Expand Up @@ -96,6 +97,104 @@ public function convertTo(string $filename, $stream, string $format, bool $sendF
}
}

/**
* @param string $filename
* @param resource $stream
* @return array
*/
public function extractDocumentStructure(string $filename, $stream, string $filter): array {
if (!$this->capabilitiesService->hasFormFilling()) {
return [];
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$client = $this->clientService->newClient();

$options = RemoteOptionsService::getDefaultOptions();
$options['expect'] = false;

if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}

$options['query'] = ['filter' => $filter];
$options['multipart'] = [
[
'name' => 'data',
'filename' => $filename,
'contents' => $stream,
'headers' => [ 'Content-Type' => 'multipart/form-data' ],
],
];

try {
$response = $client->post(
$collaboraUrl . '/cool/extract-document-structure',
$options
);

return json_decode($response->getBody(), true)['DocStructure'] ?? [];
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return [];
}
}

/**
* @param string $filename
* @param resource $stream
* @return string|resource
*/
public function transformDocumentStructure(string $filename, $stream, array $values, ?string $format = null) {
if (!$this->capabilitiesService->hasFormFilling()) {
throw new \RuntimeException('Form filling not supported by the Collabora server');
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$client = $this->clientService->newClient();

$options = RemoteOptionsService::getDefaultOptions();
$options['expect'] = false;

if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}

$data = [
'name' => 'data',
'filename' => $filename,
'contents' => $stream,
'headers' => [ 'Content-Type' => 'multipart/form-data' ],
];

$transform = [
'name' => 'transform',
'contents' => '{"Transforms": ' . json_encode($values) . '}',
'headers' => [ 'Content-Type' => 'application/json' ],
];

$options['multipart'] = [$data, $transform];

if ($format !== null) {
$options['multipart'][] = [
'name' => 'format',
'contents' => $format,
];
}

try {
$response = $client->post(
$collaboraUrl . '/cool/transform-document-structure',
$options
);

return $response->getBody();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw $e;
}
}

private function getRequestOptionsForFile(File $file, ?string $target = null): array {
$localFile = $file->getStorage()->getLocalFile($file->getInternalPath());
if (!is_string($localFile)) {
Expand Down
156 changes: 156 additions & 0 deletions lib/Service/SlideDeckService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Richdocuments\Service;

use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\TemplateManager;
use OCP\IConfig;
use OCP\TaskProcessing\Exception\Exception;
use OCP\TaskProcessing\Exception\NotFoundException;
use OCP\TaskProcessing\Exception\PreConditionNotMetException;
use OCP\TaskProcessing\Exception\UnauthorizedException;
use OCP\TaskProcessing\Exception\ValidationException;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
use OCP\TaskProcessing\TaskTypes\TextToText;
use RuntimeException;

class SlideDeckService {
public const PROMPT = <<<EOF
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just remembered, Daphne raised to me that this likely should support different languages as well (especially German). Is that feasible? Just using different prompts then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add an instruction sentence to make sure the model outputs in the same language as the "presentation text" coming from the user.

The "system" prompt can stay in english. We do it for summary, headlines, topics etc... and it works pretty well with most tested models.


```
[{"headline": "Headline 1", points: ["Bullet point 1", "Bullet point 2"]}, {"headline": "Headline 2", points: ["Bullet point 1", "Bullet point 2"]}]
```

Here is the presentation text:
EOF;

public function __construct(
private IManager $taskProcessingManager,
private TemplateManager $templateManager,
private RemoteService $remoteService,
private IConfig $config,
) {
}

public function generateSlideDeck(?string $userId, string $presentationText) {
$rawModelOutput = $this->runLLMQuery($userId, $presentationText);

$ooxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml';
$format = $ooxml ? 'pptx' : 'odp';
$emptyPresentation = $this->getBlankPresentation($format);

try {
$parsedStructure = $this->parseModelJSON($rawModelOutput);
} catch (\JsonException) {
throw new RuntimeException('LLM generated faulty JSON data');
}

try {
$transformedPresentation = $this->remoteService->transformDocumentStructure(
'presentation.' . $format,
$emptyPresentation,
$parsedStructure
);

return $transformedPresentation;
} catch (\Exception) {
throw new RuntimeException('Unable to apply transformations to presentation file');
}
}

/**
* Parses the JSON output from the LLM into
* JSON that Collabora expects
*
* @param string $jsonString
* @return array
*/
private function parseModelJSON(string $jsonString): array {
$modelJSON = json_decode(
$jsonString,
associative: true,
flags: JSON_THROW_ON_ERROR
);

$slideCommands = [];
foreach ($modelJSON as $index => $slide) {
if (count($slideCommands) > 0) {
$slideCommands[] = [ 'JumpToSlide' => 'last' ];
$slideCommands[] = [ 'InsertMasterSlide' => 0 ];
} else {
$slideCommands[] = [ 'JumpToSlide' => $index];
}

$slideCommands[] = [ 'ChangeLayoutByName' => 'AUTOLAYOUT_TITLE_CONTENT' ];
$slideCommands[] = [ 'SetText.0' => $slide['headline'] ];

$editTextObjectCommands = [
[ 'SelectParagraph' => 0 ],
[ 'InsertText' => implode(PHP_EOL, $slide['points']) ],
];

$slideCommands[] = [ 'EditTextObject.1' => $editTextObjectCommands ];
}

return [ 'SlideCommands' => $slideCommands ];
}

/**
* Creates a blank presentation file in memory
*
* @param string $format
* @return resource
*/
private function getBlankPresentation(string $format) {
$emptyPresentationContent = $this->templateManager->getEmptyFileContent($format);
$memoryStream = fopen('php://memory', 'r+');

if (!$memoryStream) {
throw new RuntimeException('Unable to open file stream');
}

fwrite($memoryStream, $emptyPresentationContent);
rewind($memoryStream);

return $memoryStream;
}

private function runLLMQuery(?string $userId, string $presentationText) {
$prompt = self::PROMPT;
$task = new Task(
TextToText::ID,
['input' => $prompt . "\n\n" . $presentationText],
Application::APPNAME,
$userId
);

try {
$this->taskProcessingManager->scheduleTask($task);
} catch (PreConditionNotMetException|UnauthorizedException|ValidationException|Exception $e) {
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}

while (true) {
try {
$task = $this->taskProcessingManager->getTask($task->getId());
} catch (NotFoundException|Exception $e) {
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
if (in_array($task->getStatus(), [Task::STATUS_SUCCESSFUL, Task::STATUS_FAILED, Task::STATUS_CANCELLED])) {
break;
}
}

if ($task->getStatus() !== Task::STATUS_SUCCESSFUL) {
throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' failed or was cancelled');
}

return $task->getOutput()['output'];
}
}
60 changes: 10 additions & 50 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public function __construct(
* @throws NotFoundException
*/
public function extractFields(Node|int $file): array {
if (!$this->capabilitiesService->hasFormFilling()) {
return [];
}

if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}
Expand Down Expand Up @@ -74,23 +70,12 @@ public function extractFields(Node|int $file): array {
return [];
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

$form = RemoteOptionsService::getDefaultOptions();
$form['query'] = ['filter' => 'contentcontrol'];
$form['multipart'] = [[
'name' => 'data',
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'),
'headers' => ['Content-Type' => 'multipart/form-data'],
]];

$response = $httpClient->post(
$collaboraUrl . '/cool/extract-document-structure',
$form
$documentStructure = $this->remoteService->extractDocumentStructure(
$file->getName(),
$file->getStorage()->fopen($file->getInternalPath(), 'r'),
'contentcontrol'
);

$documentStructure = json_decode($response->getBody(), true)['DocStructure'] ?? [];
$fields = [];

foreach ($documentStructure as $index => $attr) {
Expand Down Expand Up @@ -138,10 +123,6 @@ public function extractFields(Node|int $file): array {
* @return string|resource
*/
public function fillFields(Node|int $file, array $fields = [], ?string $destination = null, ?string $format = null) {
if (!$this->capabilitiesService->hasFormFilling()) {
throw new \RuntimeException('Form filling not supported by the Collabora server');
}

if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}
Expand All @@ -161,39 +142,18 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat
return $content;
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

$formData = [
'name' => 'data',
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'),
'headers' => ['Content-Type' => 'multipart/form-data'],
];

$formTransform = [
'name' => 'transform',
'contents' => '{"Transforms": ' . json_encode($fields) . '}',
];

$formFormat = [
'name' => 'format',
'contents' => $format === null ? $file->getExtension() : $format,
];

$form = RemoteOptionsService::getDefaultOptions();
$form['multipart'] = [$formData, $formTransform, $formFormat];

try {
$response = $httpClient->post(
$collaboraUrl . '/cool/transform-document-structure',
$form
$content = $this->remoteService->transformDocumentStructure(
$file->getName(),
$file->getStorage()->fopen($file->getInternalPath(), 'r'),
$fields,
$format === null ? $file->getExtension() : $format
);

$content = $response->getBody();

if ($destination !== null) {
$this->writeToDestination($destination, $content);
}

return $content;
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
Expand Down
Loading