Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move task processing provider and task type from https://github.com/n… #4390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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\Template\CollaboraTemplateProvider;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
Expand Down Expand Up @@ -82,6 +84,8 @@ public function register(IRegistrationContext $context): void {
$context->registerPreviewProvider(OpenDocument::class, OpenDocument::MIMETYPE_REGEX);
$context->registerPreviewProvider(Pdf::class, Pdf::MIMETYPE_REGEX);
$context->registerNotifierService(Notifier::class);
$context->registerTaskProcessingProvider(SlideDeckGenerationProvider::class);
$context->registerTaskProcessingTaskType(SlideDeckGenerationTaskType::class);
}

public function boot(IBootContext $context): void {
Expand Down
72 changes: 72 additions & 0 deletions lib/Service/SlideDeckService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 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
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
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,
) {
}

public function generateSlideDeck(?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');
}

$output = $task->getOutput();
if (isset($output['output'])) {
throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' does not have output key');
}

$headlines = json_decode($output['output'], associative: true);

return $output['output'];
}
}
90 changes: 90 additions & 0 deletions lib/TaskProcessing/SlideDeckGenerationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace OCA\Richdocuments\TaskProcessing;

use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\Service\SlideDeckService;
use OCP\IL10N;
use OCP\TaskProcessing\ISynchronousProvider;

class SlideDeckGenerationProvider implements ISynchronousProvider {

public function __construct(
private SlideDeckService $slideDeckService,
private IL10N $l10n,
) {
}

public function getId(): string {
return Application::APPNAME . '-slide_deck_generator';
}

public function getName(): string {
return $this->l10n->t('Nextcloud Assistant Slide Deck Generator');
}

public function getTaskTypeId(): string {
return SlideDeckGenerationTaskType::ID;
}

public function getExpectedRuntime(): int {
return 120;
}

public function getInputShapeEnumValues(): array {
return [];
}

public function getInputShapeDefaults(): array {
return [];
}

public function getOptionalInputShape(): array {
return [];
}

public function getOptionalInputShapeEnumValues(): array {
return [];
}

public function getOptionalInputShapeDefaults(): array {
return [];
}

public function getOutputShapeEnumValues(): array {
return [];
}

public function getOptionalOutputShape(): array {
return [];
}

public function getOptionalOutputShapeEnumValues(): array {
return [];
}
/**
* @inheritDoc
*/
public function process(?string $userId, array $input, callable $reportProgress): array {
if ($userId === null) {
throw new \RuntimeException('User ID is required to process the prompt.');
}

if (!isset($input['text']) || !is_string($input['text'])) {
throw new \RuntimeException('Invalid input, expected "text" key with string value');
}

$response = $this->slideDeckService->generateSlideDeck(
$userId,
$input['text'],
);

return $response;
}
}
71 changes: 71 additions & 0 deletions lib/TaskProcessing/SlideDeckGenerationTaskType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace OCA\Richdocuments\TaskProcessing;

use OCA\Richdocuments\AppInfo\Application;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\ITaskType;
use OCP\TaskProcessing\ShapeDescriptor;

class SlideDeckGenerationTaskType implements ITaskType {
public const ID = Application::APPNAME . ':slide_deck_generation';

public function __construct(
private IL10N $l,
) {
}

/**
* @inheritDoc
*/
public function getName(): string {
return $this->l->t('Generate Slide Deck');
}

/**
* @inheritDoc
*/
public function getDescription(): string {
return $this->l->t('Generate a slide deck from a presentation script');
}

/**
* @return string
*/
public function getId(): string {
return self::ID;
}

/**
* @return ShapeDescriptor[]
*/
public function getInputShape(): array {
return [
'text' => new ShapeDescriptor(
$this->l->t('Presentation script'),
$this->l->t('Write the text for your presentation here'),
EShapeType::Text,
),
];
}

/**
* @return ShapeDescriptor[]
*/
public function getOutputShape(): array {
return [
'slide_deck' => new ShapeDescriptor(
$this->l->t('Generated slide deck'),
$this->l->t('The slide deck generated'),
EShapeType::Text,
),
];
}
}
Loading