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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@
'OCP\\Talk\\ITalkBackend' => $baseDir . '/lib/public/Talk/ITalkBackend.php',
'OCP\\TaskProcessing\\EShapeType' => $baseDir . '/lib/public/TaskProcessing/EShapeType.php',
'OCP\\TaskProcessing\\Events\\AbstractTaskProcessingEvent' => $baseDir . '/lib/public/TaskProcessing/Events/AbstractTaskProcessingEvent.php',
'OCP\\TaskProcessing\\Events\\GetTaskProcessingProvidersEvent' => $baseDir . '/lib/public/TaskProcessing/Events/GetTaskProcessingProvidersEvent.php',
'OCP\\TaskProcessing\\Events\\TaskFailedEvent' => $baseDir . '/lib/public/TaskProcessing/Events/TaskFailedEvent.php',
'OCP\\TaskProcessing\\Events\\TaskSuccessfulEvent' => $baseDir . '/lib/public/TaskProcessing/Events/TaskSuccessfulEvent.php',
'OCP\\TaskProcessing\\Exception\\Exception' => $baseDir . '/lib/public/TaskProcessing/Exception/Exception.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Talk\\ITalkBackend' => __DIR__ . '/../../..' . '/lib/public/Talk/ITalkBackend.php',
'OCP\\TaskProcessing\\EShapeType' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/EShapeType.php',
'OCP\\TaskProcessing\\Events\\AbstractTaskProcessingEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/AbstractTaskProcessingEvent.php',
'OCP\\TaskProcessing\\Events\\GetTaskProcessingProvidersEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/GetTaskProcessingProvidersEvent.php',
'OCP\\TaskProcessing\\Events\\TaskFailedEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/TaskFailedEvent.php',
'OCP\\TaskProcessing\\Events\\TaskSuccessfulEvent' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Events/TaskSuccessfulEvent.php',
'OCP\\TaskProcessing\\Exception\\Exception' => __DIR__ . '/../../..' . '/lib/public/TaskProcessing/Exception/Exception.php',
Expand Down
46 changes: 45 additions & 1 deletion lib/private/TaskProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\SpeechToText\ISpeechToTextProvider;
use OCP\SpeechToText\ISpeechToTextProviderWithId;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Events\GetTaskProcessingProvidersEvent;
use OCP\TaskProcessing\Events\TaskFailedEvent;
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
use OCP\TaskProcessing\Exception\NotFoundException;
Expand Down Expand Up @@ -81,8 +82,13 @@
private IAppData $appData;
private ?array $preferences = null;
private ?array $providersById = null;

/** @var ITaskType[]|null */
private ?array $taskTypes = null;
private ICache $distributedCache;

private ?GetTaskProcessingProvidersEvent $eventResult = null;

public function __construct(
private IConfig $config,
private Coordinator $coordinator,
Expand Down Expand Up @@ -488,6 +494,20 @@
return $newProviders;
}

/**
* Dispatches the event to collect external providers and task types.
* Caches the result within the request.
*/
private function dispatchGetProvidersEvent(): GetTaskProcessingProvidersEvent {
if ($this->eventResult !== null) {
return $this->eventResult;
}

$this->eventResult = new GetTaskProcessingProvidersEvent();
$this->dispatcher->dispatchTyped($this->eventResult);
return $this->eventResult ;
}

/**
* @return IProvider[]
*/
Expand Down Expand Up @@ -516,6 +536,16 @@
}
}

$event = $this->dispatchGetProvidersEvent();
$externalProviders = $event->getProviders();
foreach ($externalProviders as $provider) {
if (!isset($providers[$provider->getId()])) {
$providers[$provider->getId()] = $provider;
} else {
$this->logger->info('Skipping external task processing provider with ID ' . $provider->getId() . ' because a local provider with the same ID already exists.');
}
}

$providers += $this->_getTextProcessingProviders() + $this->_getTextToImageProviders() + $this->_getSpeechToTextProviders();

return $providers;
Expand All @@ -531,6 +561,10 @@
return [];
}

if ($this->taskTypes !== null) {
return $this->taskTypes;
}

// Default task types
$taskTypes = [
\OCP\TaskProcessing\TaskTypes\TextToText::ID => \OCP\Server::get(\OCP\TaskProcessing\TaskTypes\TextToText::class),
Expand Down Expand Up @@ -568,9 +602,19 @@
}
}

$event = $this->dispatchGetProvidersEvent();
$externalTaskTypes = $event->getTaskTypes();
foreach ($externalTaskTypes as $taskType) {
if (isset($taskTypes[$taskType->getId()])) {
$this->logger->warning('External task processing task type is using ID ' . $taskType->getId() . ' which is already used by a locally registered task type (' . get_class($taskTypes[$taskType->getId()]) . ')');
}
$taskTypes[$taskType->getId()] = $taskType;
}

$taskTypes += $this->_getTextProcessingTaskTypes();

return $taskTypes;
$this->taskTypes = $taskTypes;
return $this->taskTypes;
}

/**
Expand Down Expand Up @@ -999,7 +1043,7 @@
$task->setEndedAt(time());
$error = 'The task was processed successfully but the provider\'s output doesn\'t pass validation against the task type\'s outputShape spec and/or the provider\'s own optionalOutputShape spec';
$task->setErrorMessage($error);
$this->logger->error($error . ' Output was: ' . var_export($result, true), ['exception' => $e]);

Check failure on line 1046 in lib/private/TaskProcessing/Manager.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHtml

lib/private/TaskProcessing/Manager.php:1046:64: TaintedHtml: Detected tainted HTML (see https://psalm.dev/245)
} catch (NotPermittedException $e) {
$task->setProgress(1);
$task->setStatus(Task::STATUS_FAILED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\TaskProcessing\Events;

use OCP\EventDispatcher\Event;
use OCP\TaskProcessing\IProvider;
use OCP\TaskProcessing\ITaskType;

/**
* Event dispatched by the server to collect Task Processing Providers
* and custom Task Types from listeners (like AppAPI).
*
* Listeners should add their providers and task types using the
* addProvider() and addTaskType() methods.
*
* @since 32.0.0
*/
class GetTaskProcessingProvidersEvent extends Event {
/** @var IProvider[] */
private array $providers = [];

/** @var ITaskType[] */
private array $taskTypes = [];

/**
* Add a Task Processing Provider.
*
* @param IProvider $provider The provider instance to add.
* @since 32.0.0
*/
public function addProvider(IProvider $provider): void {
$this->providers[] = $provider;
}

/**
* Get all collected Task Processing Providers.
*
* @return IProvider[]
* @since 32.0.0
*/
public function getProviders(): array {
return $this->providers;
}

/**
* Add a custom Task Processing Task Type.
*
* @param ITaskType $taskType The task type instance to add.
* @since 32.0.0
*/
public function addTaskType(ITaskType $taskType): void {
$this->taskTypes[] = $taskType;
}

/**
* Get all collected custom Task Processing Task Types.
*
* @return ITaskType[]
* @since 32.0.0
*/
public function getTaskTypes(): array {
return $this->taskTypes;
}
}
Loading
Loading